// COCC Java Utility Script 

// --- START OF SCRIPT INCLUDES ---

// inc() function
// - Used to include other JS files so the code does not need to be copied
// - Taken from: http://www.thescripts.com/forum/thread149165.html
function inc(filename)
{
    var head = document.getElementsByTagName('head').item(0);
    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';
    if (head != null)
      head.appendChild(script);
//    else
//      alert('Import of "' + filename + '" failed.');
}
function incInline(filename)
{
	document.write('<script language="jscript" src="' + filename + '" type="text/javascript"></script>');
}

// Here we include supporting files from the appropriate source so that
// we don't have additional dependencies outside our community web service
if (window.inCOCCCommunity != undefined)
{
    // load our includes from our community path
    inc(document.location.protocol+"//www.cocc.edu/coccweb/ShowHide.js");
    inc(document.location.protocol+"//www.cocc.edu/coccweb/Validation.js");
    inc(document.location.protocol+"//www.cocc.edu/coccweb/R25.js");
    inc(document.location.protocol+"//www.cocc.edu/coccweb/ga.js");
}
else
{
    // load our includes from our frontpage path
    inc(document.location.protocol+"//web.cocc.edu/coccweb/ShowHide.js");
    inc(document.location.protocol+"//web.cocc.edu/coccweb/Validation.js");
    inc(document.location.protocol+"//web.cocc.edu/coccweb/R25.js");
    inc(document.location.protocol+"//web.cocc.edu/coccweb/ga.js");
}

// --- END OF SCRIPT INCLUDES ---


// Set the focus to an input control with the specified name.
// Includes support for ASP.NET control renaming from server to client.
function SetFocusToTextbox(tbName)
{
	var inputs = document.getElementsByTagName('input'); 
	var endStr
	var len
  
	for(i=0;i<inputs.length;i++)
	{ 
	  // See if we have a control which is identically matched or
	  // ends with the requested name, prefixed with "_". Thx to .NET control renaming.
	  len = inputs[i].id.length
    endStr = "_" + inputs[i].id.substring(len-tbName.length)
		if ((tbName == inputs[i].id) || ("_"+tbName == endStr))
		{
			if (document.getElementById) // DOM3 = IE5, NS6 
    		inputs[i].focus();
		}
	} 
}

// Pop-up a window containing the contents at the specified URL
function myPopUp(URL,w,h)
{
	winl = (screen.width - w) / 2;
	wint = (screen.height - h) / 2;
	
	day = new Date();
	id = day.getTime();
	//eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+w+",height="+h+",top="+wint+",left="+winl+"');");
	window.open(URL, id, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width='+w+',height='+h+',top='+wint+',left='+winl);
}

function SetDefaultButton(btnid, event)
{
    if (document.all)
    {
        var btn = document.getElementById(btnid);
        if (event.keyCode == 13)
        {
            event.returnValue=false;
            event.cancel = true;
            btn.click();
        }
    }
    else if (document.getElementById)
    {
        var btn = document.getElementById(btnid);
        if (event.which == 13)
        {
            event.returnValue=false;
            event.cancel = true;
            btn.click();
        }
    }
}

var C_Def_Search_Text = "Search Site"
function searchFocusGained(tb)
{
    if (tb.value == C_Def_Search_Text)
        tb.value = "";
}
function searchFocusLost(tb)
{
    if (tb.value == "")
        tb.value = C_Def_Search_Text;
}
function btnCOCCSearchBox_Click()
{
    var C_GoogleURL = "http://www.googlesyndicatedsearch.com/u/cocc?query=";

    if (typeof(document.all)=="undefined")
    {
      if (document.getElementById("txtCOCCSearchBox") != null)
      { location=C_GoogleURL + document.getElementById("txtCOCCSearchBox").value; }
    }
    else
    {  if (typeof(document.all["txtCOCCSearchBox"]) != "undefined")
      { location=C_GoogleURL + document.all['txtCOCCSearchBox'].value; }
    }
}
function isChecked(ctrlId)
{
	var ctrl = document.getElementById(ctrlId)
	if (ctrlId != null)
		return ctrlId.checked
	else
		return null;
}
// Client side validation function for .NET to ensure that a checkbox is checked.
function isCheckedValidator(sender, args)
{
	// This function REQUIRES that the control we need to validate is named the same as the calling EXCEPT that the validator control's id has a "_val" prefix.
	// Ex: checkbox control id = "chkVerify", the associated validator control's id must be "valchkVerify"
	var ctrl = document.getElementById(sender.id.replace(/(_val)/,"_"))
	if (ctrl != null)
	{
		args.IsValid = ctrl.checked;
		return ctrl.checked;
	}
	else
		return null;
}

// Check to see if the length of the text in the specified control exceeds the specified limit.
// Use in "onkeyup" event
function TextAreaLengthCheck(text,long) 
{
	var maxlength = new Number(long); // Change number to your max length.
	if (text.value.length > maxlength)
	{
		text.value = text.value.substring(0,maxlength);
		alert(" A maximum of " + long + " characters are allowed in this field.");
	}
}

// Check to see if the length of the text in the specified control exceeds the specified limit.
// Use in "onkeyup" event
// And then show the current count in the specified control
function TextAreaLengthCheckWithCounter(text,max,counterId)
{
	var maxlength = new Number(max); // Change number to your max length.
	if (text.value.length > maxlength)
	{
		text.value = text.value.substring(0,maxlength);
		alert(" A maximum of " + max + " characters are allowed in this field.");
	}
	if (counterId != null)
		if (counterId != '')
		{
			var ctrlCounter = document.getElementById(counterId);
			if (ctrlCounter != null)
				ctrlCounter.value = text.value.length;
		}
}

// Check to see if the length of the text in the specified control exceeds the specified limit.
// Use in "onkeyup" event
// And then show the current WORD count in the specified control
function TextAreaLengthCheckWithCounters(ctrlText,maxChars,ctrlIdCharCount,maxWords,ctrlIdWordCount)
{
    // Check max characters - assuming we were given a max to use.
	if (maxChars > 0)
	{
	    if (ctrlText.value.length > maxChars)
	    {
		    ctrlText.value = ctrlText.value.substring(0,maxChars);
		    alert(" A maximum of " + maxChars + " CHARACTERS are allowed in this field.");
	    }
	}
	
	// If we were given a CHARACTER count control, let's show our count
	if (ctrlIdCharCount != null)
	{
		if (ctrlIdCharCount != '')
		{
			var ctrlChars = document.getElementById(ctrlIdCharCount);
			if (ctrlChars != null)
				ctrlChars.value = ctrlText.value.length;
		}
	}
	
	// Get current WORD count
	var wordCount = getWordCountByCtrl(ctrlText);
	
	// If we were given a WORD count control, let's show our count
	if (ctrlIdWordCount != null)
	{
		if (ctrlIdWordCount != '')
		{
			var ctrlWords = document.getElementById(ctrlIdWordCount);
			if (ctrlWords != null)
				ctrlWords.value = wordCount;
		}
    }
    
	// Check max words - assuming we were given a max to use.
	if (maxWords > 0)
	{
	    if (wordCount > maxWords)
		    alert(" A maximum of " + maxWords + " WORDS are allowed in this field.");
	}
	
}

// Get word count for specified control
function getWordCountById(ctrlId)
{
	var ctrl = document.getElementById(ctrlId);
	if (ctrl == null)
	{
	    alert("getWordCount(): Could not find a control with ID=" + ctrlId);
	    return 0;
	}
	else
	{
	    // Get current WORD count
	    var wordArray;
	    var wordCount;
	    var inText;
	    inText = flattenWhiteSpace(ctrl.value)
	    if (inText == "")
	  	    wordCount = 0;
	    else
	    {
	  	    wordArray = inText.split(" ");
	  	    wordCount = wordArray.length;
	    }
	    return wordCount;
	}
}
function getWordCountByCtrl(ctrl)
{
	if (ctrl == null)
	{
	    alert("getWordCount(): No control was specified");
	    return 0;
	}
	else
	{
	    // Get current WORD count
	    var wordArray;
	    var wordCount;
	    var inText;
	    inText = flattenWhiteSpace(ctrl.value)
	    if (inText == "")
	  	    wordCount = 0;
	    else
	    {
	  	    wordArray = inText.split(" ");
	  	    wordCount = wordArray.length;
	    }
	    return wordCount;
	}
}

// Remove any extra whitespace at the start and end of the string
function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

// replace repeating whitespace chars with a single space character
function flattenWhiteSpace(value) {
   var temp = value;
   var obj = /[\s]+/g;
   temp = temp.replace(obj, " ");
   return trimString(temp);
}

function PadNumString(str, totalDigits) 
{ 
    var pd = ''; 
    if (totalDigits > str.length) 
        for (i=0; i < (totalDigits-str.length); i++) 
            pd += '0'; 
    return pd + str; 
} 

//simpleSec.js
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 z(a){1 b=3.h(a);4(b!=j){b.7=n(b.7,"");5 o}p 5 q};9 A(a){1 b=3.h(a);4(b!=j){b.7=r(b.7,"");5 o}p 5 q};9 n(a,b){4(a=="")5"";1 c=3.h("s").7+"";4((c==j)||(c==""))c="t";4(b=="")b="u"+(3.k+"").m(0,(3.k+"").v("\\/"));1 i=0;1 d="";1 e=b.w().x(/\\//g,"|");l(e.2<a.2)e=e+e;l(c.2<a.2)c=c+c;y(i=0;i<a.2;i++)d=d+B((a.8(i)*e.8(i)+c.8(i)).C(D),6);5 d};9 r(a,b){4(a=="")5"";1 c=3.h("s").7+"";4((c==j)||(c==""))c="t";4(b=="")b="u"+(3.k+"").m(0,(3.k+"").v("\\/"));1 d="";1 e=b.w().x(/\\//g,"|");l(e.2<a.2)e=e+e;l(c.2<a.2)c=c+c;1 i;1 f="";y(i=0;i<a.2;i+=6)f=f+E.F((G("H"+a.m(i,6))-c.8(i/6))/e.8(i/6));5 f};',44,44,'|var|length|document|if|return||value|charCodeAt|function||||||||getElementById||null|location|while|substr|simpleEncode|true|else|false|simpleDecode|sk8|00112233|_|lastIndexOf|toLowerCase|replace|for|encodeFieldText|decodeFieldText|PadNumString|toString|16|String|fromCharCode|parseInt|0x'.split('|'),0,{}))

// Function used by various community administrator features to display a list of available sections and capture the selected section ID.
function GetIDFromSectionSelector(destCtrl)
{
    var myScript = '/Engine/Framework/Sections/Controls/SectionSelector.aspx';

    // For now only supports IE (where "showModalDialog" is known)
    if (window.showModalDialog)
    {
        linkArr = showModalDialog(myScript,window,'dialogWidth:500px; dialogHeight:440px;help:0;status:0;resizeable:1;');
        if (linkArr != null)
        {
	        var ctrl = document.getElementById(destCtrl)
	        if (ctrl != null)
	        {
		        ctrl.value = linkArr['secID'];
	        }
        }
    }
    else
        alert("This feature is currently only supported by Internet Explorer.");
}

// Course Outcomes Popup
function CourseOutcomes(crse)
{
  myPopUp('http://webdata.cocc.edu/CourseOutcomes.aspx?crsn='+crse,600,500);
}