var ns4 = (document.layers) ? true : false;
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

//checks all DataGrid CheckBoxes with the given name with the given value
function dgCheckAll(chkForm, chkID, chkValue) {
//	re = new RegExp('.*:' + chkID + '$');  //generated control name starts with a colon
	for(i = chkForm.elements.length-1; i>=0 ; i--) {
//	for(i = 0; i < chkForm.elements.length; i++) {
		elm = chkForm.elements[i];
		if (elm.type == 'checkbox') {
			elm.checked = chkValue;
//			if (re.test(elm.name)) {
//				elm.checked = chkValue;
//			}
		}
	}
}

function dgCheckState(chkForm, chkID, chkAll) {
	var chkState = true;
	re = new RegExp('.*:' + chkID + '$');  //generated control name starts with a colon

	for(i = chkForm.elements.length-1; i>=0 ; i--) {
		elm = chkForm.elements[i];
//		alert(elm.name);
		if (elm.type == 'checkbox') {
			if (re.test(elm.name)) {
				chkState = chkState && elm.checked;
			}
		}
	}
	chkForm.elements[chkAll].checked = chkState;
}

function htmlCheckAll(chkID, chkAll, chkValue) {
//	for(i = 0; i < chkID.length; i++) {
	for(i=chkID.length-1; i >= 0; i--) {
		chkID[i].checked = chkValue;
	}

	for(i = 0; i < chkAll.length; i++) {
		chkAll[i].checked = chkValue;
	}
}

function htmlCheckState(chkID, chkAll) {
	var chkState = true;
//	for(i = 0; i < chkID.length; i++) {
	for(i=chkID.length-1; i >= 0; i--) {
		chkState = chkState && chkID[i].checked;
	}

	for (i = 0; i < chkAll.length; i++) {
		chkAll[i].checked = chkState;
	}
}

function SortSubmit(frm, field, datatype, direction, page) {
	frm.elements['Mode'].value='SORT';
	frm.elements['SortField'].value=field;
	frm.elements['SortDataType'].value=datatype;
	frm.elements['SortOrder'].value=direction;
	if (page == '')
		{frm.elements['Page'].value='0';}
	else
		{frm.elements['Page'].value=page;}
	frm.submit();
}

function onKeyPress (btnSubmit) {
	
	var keycode;
	if (window.event) {keycode = window.event.keyCode;}
	else if (e) {keycode = e.which;}
	else {return true;}
	//alert(keycode);
	if (keycode == 13) {
		//document.frmLogin.btnForgotPassword.focus();
		//document.frmLogin.btnForgotPassword.click();
		document.getElementById(btnSubmit).click();
		event.cancelBubble=true;
		event.returnValue=false;
		return false;}
	else {return true;}
}
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}

/* used by admin tools for dumb behavior of the radEditor */
function OnClientLoad(editor)
{
   var style = editor.GetContentArea().style;
   style.backgroundColor = "white";
   style.color= "black";
}

function showDiv(IsVisible, ctrl) {
	if (IsVisible && (ie4 || ie5) && document.all[ctrl]) { document.all[ctrl].style.display = ''; document.all[ctrl].style.visibility = 'visible';}
	if (!IsVisible && (ie4 || ie5) && document.all[ctrl]) { document.all[ctrl].style.display = 'none'; document.all[ctrl].style.visibility = 'hidden'; }
	if (IsVisible && (ns6) && document.getElementById(ctrl)) { document.getElementById(ctrl).style.visibility = 'visible'; }
	if (!IsVisible && (ns6) && document.getElementById(ctrl)) { document.getElementById(ctrl).style.visibility = 'hidden'; }
	if (IsVisible && ns4 && document.layers[ctrl]) { document.layers[ctrl].visibility = 'visible'; }
	if (!IsVisible && ns4 && document.layers[ctrl]) { document.layers[ctrl].visibility = 'hidden'; }
	//if (IsVisible && ns4) { document.layers[ctrl].visibility = 'visible'; }
	//if (!IsVisible && ns4) { document.layers[ctrl].visibility = 'hidden'; }
}

function setcookie(name, value, expires)
{
	// expires is an integer representing the number of 
	// milliseconds into the future the cookie should expire
	// oneYear = 1000*60*60*24*365
	// very important to include the path otherwise the cookie scope is a sub-directory
	var today = new Date();
	var expiredate = new Date(today.getTime()+expires);
  if (expires > 0)
		{document.cookie = name + '=' + escape(value) + '; path=/; expires=' + expiredate.toUTCString();}
	else
		{document.cookie = name + '=' + escape(value) + '; path=/';}
}

// set a validation cookie, this runs on page load because it's outside of any function
setcookie('validate','js',1000*60*60*24*365);
