//Checks or unchecks all checkboxes that have the specified ID within their name.
//
//Arguments:
//	checkBoxID - ID of the checkboxes to update.
//	isChecked - check or uncheck the checkboxes (true/false).
function CheckAll(checkBoxID, isChecked)
{
	var re = new RegExp(checkBoxID + '\\$');
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		elm = document.forms[0].elements[i];
		if(elm.type == 'checkbox' && elm.disabled == false && re.test(elm.name))
			elm.checked = isChecked;
	}
}


//Opens a window for a chat room. If the window already exists, it's focus is set.
//
//Arguments:
//	appPath - the path to the application.
//	objectID - ID of the object to open the chat room for.
//	tutorID - ID of the tutor for the chat room.
function OpenChatRoomWindow(appPath, objectID, tutorID)
{
	var WindowName = objectID.replace(/-/g, "");
	var Url = appPath + "/student/ChatRooms/ChatRoom.aspx?CourseGuid=" + objectID;
	if(tutorID)
		Url += "&TutorGuid=" + tutorID;

	var Win = window.open(Url, "ChatRoom" + WindowName, "width=700,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes");
	if(Win && !Win.closed && Win.focus)
		Win.focus();
}


//Opens a window for a PMB. If the window already exists, it's focus is set.
//
//Arguments:
//	appPath - the path to the application.
//	objectID - ID of the object to open the PMB for.
function OpenPMBWindow(appPath, objectID)
{
	var WindowName = objectID.replace(/-/g, "");
	var Url = appPath + "/admin/frmUserViewPDR.aspx?PMBGuid=" + objectID;

	var Win = window.open(Url, WindowName, "width=800,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes");
	if(Win && !Win.closed && Win.focus)
		Win.focus();
}


//Opens a window for a PMB. If the window already exists, it's focus is set.
//
//Arguments:
//	appPath - the path to the application.
//	objectID - ID of the object to open the PMB for.
function OpenForumWindow(appPath, ForumID)
{
	var WindowName = ForumID.replace(/-/g, "");
	var Url = appPath + "/frmForum.aspx";
	
	if(WindowName.length>0)
		Url = Url + "?folderid=" + ForumID;

	var Win = window.open(Url, WindowName, "width=800,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes");
	if(Win && !Win.closed && Win.focus)
		Win.focus();
}


//Opens a window for a lesson certificate. If the window already exists, it's focus is set.
//
//Arguments:
//	appPath - the path to the application.
//	courseID - ID of the course to open the certificate for.
// lessonID - ID of the lesson to open the certificate for.
function OpenCertificateWindow(appPath, courseID, lessonID)
{
    var windowName = lessonID.replace(/-/g, "");
    var url = appPath + "/Portal/Certificate.aspx";
    url += "?CourseGuid=" + courseID;
    url += "&LearningObjectGuid=" + lessonID;
    
    var win = window.open(url, windowName, "toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes");
    if (win && !win.closed && win.focus)
        win.focus();
}


//Opens a window to show a report.
//
//Arguments:
//	strUrl - the URL to open.
//	isLandscape - whether the window should be landscape or portrait.
function OpenReportWindow(strUrl, isLandscape)
{
	var WindowWidth = (isLandscape ? 1000 : 680);
	var Win = window.open(strUrl, "_blank", "width=" + WindowWidth + ",toolbar=no,location=no,directories=no,status=no,menubar=yes,resizable=yes,scrollbars=yes");
}


//Checks whether the amount entered into the textarea has reached the maximum length allowed.
//If it has then it prevents the user from entering any further characters. 
//
//Arguments:
//	e - the JS event that was raised.
//	txtBox - the textbox control that is being checked.
function CheckMaxLenOnKeypress(e, txtBox){
	var cr = 13;
	var lf = 12;
	maxLength = txtBox.attributes["maxLength"].value;
	if(maxLength && txtBox.value.length >= parseInt(maxLength))
	{
		evt = e || window.event;
		var keyPressed = evt.which || evt.keyCode;		
		
		//handle arrow keys and Home, End, etc. in firefox
		if(!window.event) // Not IE
		{
			if(e.keyCode > 32 && e.keyCode < 41) 
			{
				return true;
			}
		}

		if ((keyPressed < 32 && keyPressed != cr && keyPressed != lf) || evt.ctrlKey)
			return true;
		else
			return false;
	}
}


//Checks whether the amount pasted into the textarea has reached the maximum length allowed.
//If it exceeds the maximum length then the text is truncated. (This only works with IE)
//
//Arguments:
//	e - the JS event that was raised.
//	txtBox - the textbox control that is being checked.
function CheckMaxLenAfterPaste(e, txtBox){
    maxLength = txtBox.attributes["maxLength"].value;
    if(maxLength)
    {
		event.returnValue = false;
		maxLength = parseInt(maxLength);
        var oTR = txtBox.document.selection.createRange();
        var iInsertLength = maxLength - txtBox.value.length + oTR.text.length;
        var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
        oTR.text = sData;
		UpdateCharacterCount(txtBox);
	}	
}


//Checks whether the amount pasted into the textarea has reached the maximum length allowed.
//If it exceeds the maximum length then the text is truncated. (This only works with Firefox)
//
//Arguments:
//	e - the JS event that was raised.
//	txtBox - the textbox control that is being checked.
function CheckMaxLenAfterInput(e, txtBox){
    maxLength = txtBox.attributes["maxLength"].value;
    if(maxLength)
    {
    	if (txtBox.value.length >= maxLength) 
    	{
    		txtBox.value = txtBox.value.substring(0, maxLength);
    		txtBox.scrollTop = 999999; //Keeps the cursor at the last character rather going to the top.
    	}
        UpdateCharacterCount(txtBox);
	}
}

//Updates the correct character count label based on the postioning of the controls
//
//Arguments:
//	txtBox - the textbox control that is being checked.
function UpdateCharacterCount(txtBox){
	var breakTag = txtBox.nextSibling;
	while (breakTag && breakTag.nodeType != 1) 
	{
		breakTag = breakTag.nextSibling;
	}
	var divTag = breakTag.nextSibling;
	while (divTag && divTag.nodeType != 1) 
	{
		divTag = divTag.nextSibling;
	}
	var charCount = divTag.firstChild;
	if (charCount)
	{
		charCount.innerHTML = txtBox.value.length;
	}
}

//Will make the Enter key ignored by the control, useful to stop single line text boxes posting the page back
//
//Arguments:
//	e - the event from the form
function DisableEnterKey(e){ 
      var evt = e ? e : window.event;
          if (evt.keyCode == 13)
          { return false; }
}


// On the staff review page shows/hides the score dropdown list depending on the radio button selected
//
// Arguments:
//	hide - 1 = hide the list, 0 = show it
//	controlID - the ClientID of the grade drop down list to show/hide
function HideControl(hide, controlID)
{
	control = document.getElementById(controlID);

	if(control)
	{	
		if(hide == "1")
		{
			control.style.display = "none";
		}
		else
		{
			control.style.display = "";
		}
	}			
}

// On the staff review page selects the disgree radio button if a value is selected from the score
// drop down list
//
// Arguments:
//	theDropdownList - the instance of the dropdownlist that has been clicked.

function enableDisagreeRadiobutton(theDropdownList)
{
	dropdownID = theDropdownList.id;
	
	cntrlCount = dropdownID.substring(dropdownID.indexOf("dlCompetencies_ctl")+20,dropdownID.indexOf("dlCompetencies_ctl")+18);
	strControlToFind = "dlCompetencyCategories_ctl00_rptGroups_ctl00_dlCompetencies_ctl" + cntrlCount + "_arCompetencyGradeLevel1_rbReject";
	
	control = document.getElementById(strControlToFind);
	if(control)
	{
		control.checked = true;
	}
}

// Allows Unique radio buttons to be used in a repeater control
function SetUniqueRadioButton(match, current) {
	for (i = 0; i < document.forms[0].elements.length; i++)
	{
		elm = document.forms[0].elements[i]
		if (elm.type == 'radio' && elm.id.indexOf(match, 0) > -1)
			elm.checked = false;
	}
	current.checked = true;
}

// Sets the .checked property of the parentControl to true if thisControl.checked is true
function CheckParentControls(thisControl, parentControls) {
	if (thisControl && thisControl.checked == true) {
		var ctr;

		for (ctr = 0; ctr < parentControls.length; ctr++) {
			if (parentControls[ctr] && parentControls[ctr].checked == false)
				parentControls[ctr].checked = true;
		}
	}
}

// Sets the .checked properties of the controlArray to false if thisControl.checked is false
function UnCheckChildControls(thisControl, childControls) {
	if (thisControl && thisControl.checked == false) {
		var ctr;

		for (ctr = 0; ctr < childControls.length; ctr++) {
			if (childControls[ctr] && childControls[ctr].checked)
				childControls[ctr].checked = false;
		}
	}
}

//function getHTTPObject() {
//		var xmlhttp = false;

//		/*@cc_on@*/
//		/*@if (@_jscript_version >= 5)
//		// JScript gives us conditional compilation, we can cope with old IE versions.
//		// and security blocked creation of the objects.
//		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
//		catch (e) {
//			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
//			catch (E) { xmlhttp = false; }
//		}
//		@end@*/

//		if (!xmlhttp && typeof (XMLHttpRequest) != 'undefined') {
//			try { xmlhttp = new XMLHttpRequest(); }
//			catch (e) { xmlhttp = false; }
//		}

//		if (!xmlhttp && window.createRequest) {
//			try { xmlhttp = window.createRequest(); }
//			catch (e) { xmlhttp = false; }
//		}

//		return xmlhttp;
//	}

//function keepAlive() {
//	var objRequest = getHTTPObject();
//    objRequest.open('get', 'keepalive.aspx');
//    objRequest.send('');
//}

//setInterval('keepAlive();', 30000);
