/*========================================================================
  Cookie management
========================================================================*/
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

/*========================================================================
  Action and Input Validation
========================================================================*/
var action;
function setAction(newAction) 
  {
	action = newAction;
  }
/*==========================================================================================
  Form validation utillity methods
==========================================================================================*/
function MaxLengthCheck(varElement,varMaxLength)
  {			
     if(varElement.value.length>varMaxLength)
    	{
    	  varElement.value = varElement.value.substr(0,varMaxLength);
    	}
  }	
function ZeroLengthCheck(varElementName)
  {
	 var El = xbGetElementById(varElementName)
	 if(El.value.length==0)
    	return true;
	 else
		return false;
  }	
function FormSubmit(objDoc, strForm)
  {
     var frm = objDoc.forms[strForm];     
     frm.submit();
     //submit();
  }	
function ZeroValueCheck(varElementName)
  {
	 var El = xbGetElementById(varElementName);
	 if(parseInt(El.value)==0)
    	return true;
	 else
		return false;	
  }
function ShowErrorMsg(varElementName,varMsg)
  {
	var El = xbGetElementById(varElementName)
	xbSetInnerHTML(El,varMsg);
  }

function SwitchChkBoxes(chkbox, frmName) {
	var frm;
	var frmElements;
	var blnChecked;
				 
					
	frm = document.forms[frmName];
	frmElements = frm.elements;
	
	blnChecked = chkbox.checked;
	
	//Clear All checkboxes/radio buttons	  	
	for(i=0;i < frmElements.length;i++)
	{	
		if(frmElements[i].type=="checkbox")
		{
		frmElements[i].checked = blnChecked;						
		}
	}
}

function CheckLength(length) {
	var nKey;
	if (document.all) { //IE trap
		nKey = window.event.keyCode;
		//alert(nKey);
		if (window.event.srcElement.value.length >= length) {
			/*
			8 = backspace
			9 = tab
			46 = delete
			37-40 = arrow keys
			*/
			if ((nKey == 8) || (nKey == 9) || (nKey == 46) || ((nKey >= 37) && (nKey <= 40))) {
				return true;
			} else {
				return false;
			}
		 }				
	 } else {                 //must be NS then
		nKey = evt.which;
		return true; //requires server side validation too
	}             
}

function clearCheckBoxes(chkbox, frmName) {
	var frm;
	var frmElements;
	var varID;
					
	frm = document.forms[frmName];
	frmElements = frm.elements;
	
	varID = chkbox.id;
	var blnChecked = chkbox.checked;
	
	//Clear All checkboxes/radio buttons	  	
	
	for(i=0;i < frmElements.length;i++)
	{	
		if(frmElements[i].type=="checkbox" && frmElements[i].id==varID)
		{
			frmElements[i].checked = false;
			//alert('hit');						
		}
	}	
	chkbox.checked = blnChecked;	
}


function Truncate(length) {
	if (document.all) { //IE trap
		if (window.event.srcElement.value.length > length) {
			window.event.srcElement.value=window.event.srcElement.value.substring(0,length);
			alert('Your text exceeded the maximum size for this field: ' + length);
		}
	}
}

function transferDivContents(varSource,varTarget){
	var s_el = xbGetElementById(varSource);
	var t_el = xbGetElementById(varTarget);	
	
	if ((s_el != typeof undefined) && (t_el != typeof undefined)) {
		t_el.innerHTML = s_el.innerHTML;
	}
}

function transferDivHTML(varSource,varTarget) {
	transferDivContents(varSource,varTarget);
}

function showReportPrintVersion(varReportName,varSourceDivs){
	var s;
	var strHTML;
	var newWindow;
	var newPopup;
	var newPopupBody;
	var Anchors;
	
	var strBodyStart;
	strBodyStart = "";
	strBodyStart += "<!doctype html public '-//w3c//dtd html 4.0 transitional//en'><html><head><title>";
	strBodyStart += varReportName;
	strBodyStart += "</title><meta name='author' content='studyserve'><link rel='stylesheet' type='text/css' href='/styles/studyserve.css'></head><body style='padding:15px;'>";
	strBodyStart += "<a href='JavaScript:window.print();'>  <span style='vertical-align: top;' class='imagebutton_small'>Print</span><a href='JavaScript:window.close();'><span style='vertical-align: top;' class='imagebutton_small'>Close</span></a><br>";
	
	var strBodyEnd;
	strBodyEnd = "";
	strBodyEnd += "</body></html>";
	
	strHTML = "";
	if(varSourceDivs.length > 0){
		varSourceDivArray = varSourceDivs.split(",");
		for (i=0; i < varSourceDivArray.length; i++) {			
			s = xbGetElementById(varSourceDivArray[i]);
			if(s != typeof undefined && s != typeof null){
				strHTML += "<br>";
				strHTML += s.innerHTML;
			}
		}
	}
	
	
	if(strHTML != ""){
		
		newWindow = window.open("",varReportName,"width=600, height=400, toolbar=yes, scrollbars=yes, resizable");
		
		//newPopup = window.createPopup();
		//newPopupBody = newPopup.document.body;
		
		newWindow.document.write(strBodyStart);
		newWindow.document.write(strHTML);
		newWindow.document.write(strBodyEnd);
		newWindow.document.close();
		
		//newPopupBody.innerHTML = strHTML;
		
		//newPopup.show(200,200,600,400, document.body);
		
		
		//Anchors = newWindow.innerHTML;
		//alert(Anchors);
		
		
		
		
		//alert(Anchors.length);
		//if(Anchors.length > 0){
		//	for (i=0; i < Anchors.length;i++){
		//		alert(Anchors[i]);
		//		Anchors[i].href = "#";
		//	}
		
		//}
		
		
	}
}
