///////////////////////////////////////////////////////////////////////////////////////
// Author: Ali Salman
// Date: July 9, 2003
// GUI input validation functions.
// (Starts here)
function validateMandatoryFields(fieldNames,labels,doc) 
{

	var flag = true;

	for(var i=0; i<fieldNames.length; i++) 
	{
		var field = doc.getElementById(fieldNames[i]);		
		var val = field.value;
		val = trim(val);

		if( (val.toLowerCase() == "please specify here" && field.style.visibility != "hidden") || val==-1) {
			val = "";
		}
		
		if(val == "") {
			flag = false;
			alert("'" + labels[i] + "' field value is required. Please try again.");
			field.focus();			
			break;
		} else if(val=="null") {
			flag = false;
			alert("'" + labels[i] + "' field can not have 'null' as value. Please try again.");
			field.focus();			
			break;
		}
	}	
	return flag;
}

function validatePassword(field1, field2) {
	var retVal = true;
	
	if(field1.value=="null" || field2.value=="null") {
		retVal = false;
		alert("'null' can not be used as password. Please enter a different password.");		
		field1.focus();
	} else if(field1.value != field2.value) {
		retVal = false;
		alert("Passwords do not match. Please try again.");		
		field1.focus();
	} 
	return retVal;	
}

function isNumber(val) {

	val = trim(val);					

	for(var j=0; j<val.length; j++) {
		var ch = val.charCodeAt(j);
		if(ch < 48 || ch > 57) {
			return false;
		}
	}			
	return true;
}

function validateNumberFields(fieldNames,labels,doc) {

	for(var i=0; i<fieldNames.length; i++){
		var field = doc.getElementById(fieldNames[i]);		
		
		if(field==null) {
			continue;
		}

		var val = field.value;		

		if(!isNumber(val) || val=="null"){
			alert("'" + labels[i] + "' field does not contain a valid number. Please enter again.");
			field.focus();				
			return false;
		}
	}	
	return true;
}

function validateEmailField(fieldName,label,doc) {
 
	var field = doc.getElementById(fieldName);		
	var val = trim(field.value);		


	if((val.length > 0) && (val.indexOf(" ")!=-1 || val.indexOf("@")==-1 || val.indexOf(".")==-1 || val=="null")) {
		alert("'" + label + "' field does not contain a valid email address. Please enter again.");
		field.focus();				
		return false;
	}

	return true;
}

function isText(val) {

	val = trim(val);					

	for(var j=0; j<val.length; j++) {
		var ch = val.charCodeAt(j);
		if( ((ch < 65) || (ch > 90 && ch < 97) || (ch > 122)) && ch!=39 && ch!=32) {			
			return false;
		}
	}			
	return true;
}

function validateUserName(fieldName,label,doc) {

	var field = doc.getElementById(fieldName);		
	var val = field.value;		
	val = trim(val);


	if(val.indexOf("/")!=-1) {
		alert("'" + label + "' field contains an illegal character. Please try again.");
		field.focus();				
		return false;
	}
	return true;
}

function validateOtherFields(fieldNames,labels,doc) 
{

	for(var i=0; i<fieldNames.length; i++){
		var field = doc.getElementById(fieldNames[i]);		
		var val = field.value;		
		val = trim(val);

	
		if( (val.toLowerCase() == "please specify here" && field.style.visibility != "hidden") || val=="" || val=="null") {
			alert("'" + labels[i] + "' field can not be empty. Please enter again.");
			field.focus();				
			return false;
		}

	}	
	return true;
}


function validateTextFields(fieldNames,labels,doc) {

	for(var i=0; i<fieldNames.length; i++){
		var field = doc.getElementById(fieldNames[i]);		
		var val = field.value;		

		if(!isText(val)) {
			alert("'" + labels[i] + "' field can not have non-alphabet value. Please enter again.");
			field.focus();				
			return false;
		} else if(val=="null") {
			alert("'" + labels[i] + "' field can not have 'null' as value. Please try again.");
			field.focus();				
			return false;
		}

	}	
	return true;
}

function getMaxMonthDays(month,year) {

	switch(parseInt(month,10)) {
		case 1: case 3:	case 5:	case 7:	case 8:	case 10: case 12:	
			return 31;
		case 11: case 9: case 6: case 4:
			return 30;
		case 2:
			if(year%4==0 && (year%100!=0 || year%400==0) ) {
				return 29;
			} else {
				return 28;
			}
	}
}

function isDate(val) 
{
	val = trim(val);					
	if(val=="") return false;
	var arr = val.split("/");
	if(arr.length != 3) 
	{
		return false;
	}

	var year = 0;
	var month = 0;
	var date = 0;

	for(var j=0; j<arr.length; j++) {
		switch(j) {
			case 0:
				month = arr[j];
				break;
			case 1:
				date = arr[j];
				break;
			case 2:
				year = arr[j];
				break;
		}
	}

	if(!isNumber(month) || !isNumber(date) || !isNumber(year)) {
		return false;
	} else if(year.length<4 || year.length>4 || year<1900 || year>2999) { 	//1.check year
		return false;
	} else if(month.length<1 || month.length>2 || month>12 || month <1) {	//2.check month
		return false;
	} else if(date.length<1 || date.length>2 || date<1 || date > getMaxMonthDays(month,year) ) {	//3.check date
		return false;
	}	

	return true;
}


function validateDateFields(fieldNames,labels,doc) {
	var flag = true;

	for(var i=0; i<fieldNames.length; i++){
		var field = doc.getElementById(fieldNames[i]);		
		var val = field.value;		
		val = trim(val);					
		
		if(val=="") continue;

		flag = isDate(val);
		if(flag == false) {
			break;
		}
		
	}	
	
	if(flag == false) {
		alert("'" + labels[i]+ "' field is not a valid date. Valid format is mm/dd/yyyy. Please enter again.");
		field.focus();				

	}
	
	return flag;
}


function createDateFromStr(val) {

	val = trim(val);							

	if(!isDate(val) || val=="") {
		return null;
	}

	var year = 0;
	var month = 0;
	var date = 0;

	var arr = val.split("/");

	for(var j=0; j<arr.length; j++) {
		switch(j) {
			case 0:
				month = arr[j]-1;
				break;
			case 1:
				date = arr[j];
				break;
			case 2:
				year = arr[j];
				break;
		}
	}
	
	return new Date(year,month,date,0,0,0,0);

}


function isAnyCheckBoxChecked(frm) {

	var flag = false;	
	var count = frm.elements.length;	
	
	for(var i=0; i<count; i++) {
		if(frm.elements[i].type == "checkbox" && frm.elements[i].checked == true) {
			flag = true;
			break;
		}
	}

	return  flag;
}

function isAnyCheckBoxCheckedInArr(fieldNames,label, doc) {

	var flag = false;

	for(var i=0; i<fieldNames.length; i++) {
		var field = doc.getElementById(fieldNames[i]);		

		if(field.checked == true) {
			flag = true;
			break;
		}		
	}	
	
	if(flag == false) {
		alert("Please select atleast one " + label + ".");

	}
	
	return flag;
}




/*
function trimStr(str) {	
	var startIndex=0;
	var endIndex=str.length;	

	for(var i=0; i<endIndex; i++) {
		var val = str.charAt(i);
		if(val != " ") break;
	}
	startIndex = i;

	for(var i=endIndex-1; i>=0; i--) {
		var val = str.charAt(i);
		if(val != " ") break;
	}
	endIndex = i+1;	
	
	if(endIndex < startIndex) {
		startIndex = 0;
	}
	
	return str.substring(startIndex,endIndex);	
}
*/


// (Ends here)
///////////////////////////////////////////////////////////////////////////////////////


/*
	Author:		Abdul Rauf Mughal.
*/
function showHide(oObj, bHide)
{
	if (bHide)
		oObj.style.visibility = "hidden";
	else
	{
		oObj.style.visibility = "visible";
		oObj.focus();		
		oObj.select(true);
		
	}
//document.frmMain.txtSchoolOther.style.visibility="hidden";
}

function populateDataCombo(combo, arr, selText)
{
	for(var i=0; i<arr.length; i++)
	{
		var opt = new Option(arr[i][0],arr[i][1]);
		combo.options.length=combo.options.length+1;
		combo.options[combo.options.length-1] = opt;
		if (selText == arr[i][1])
			combo.options[combo.options.length-1].selected=true;
	}
}

function populateDataComboIndexed(combo, arr, dataIndex, selIndex)
{
	for(var i=0; i<arr.length; i++)
	{
		var opt = new Option(arr[i][dataIndex],i);
		combo.options.length=combo.options.length+1;
		combo.options[combo.options.length-1] = opt;
		if (selIndex == i)
			combo.options[combo.options.length-1].selected=true;
	}
}

function setActionAndSubmit(obj,val) {
	obj.actid.value = val;
	obj.submit();
}

//Windows specific functions
/*
	Author:		Abdul Rauf Mughal.
*/
function popupWin(URL, name, width, height)
{
	openWindow(URL, name, width, height, "toolbar=no");
}

/*
	Author:		Abdul Rauf Mughal.
*/
function openWindow(URL, name, width, height, winStyles)
{
	if (winStyles == null)
		winStyles="";

	if (width != null)
		winStyles += ", width=" + width;
	if (height != null)
		winStyles += ", height=" + height;

	x = window.screen.width; 
	y = window.screen.height; 
	var top_= parseInt(y/2-height/2-16);
	var left_=parseInt(x/2-width/2-5);
	width= width;
	height= height;
	
	winStyles += ", top=" + top_;
	winStyles += ", left=" + left_;	


	var win = window.open(URL, name, winStyles);
	if ( win != null )
		win.focus();
//	return win;		
}

//Forms/Controls specific functions

/*
	Author:		Abdul Rauf Mughal.
	function:	markCheckBox()
	Purpose:	Checks/Unchecks a group of checkboxes by name
	Input:		chkList:	list of checkboxes as an array or one checkbox
				bChecked:	true/false
	Output:		None
*/
function markCheckBox(chkList, bChecked, nStart, nEnd)
{
	if ( (chkList.length == null) && (chkList.type=="checkbox") )
	{
		chkList.checked = bChecked;
	}
	else if (chkList.length!=null)
	{
		if ( nStart == null )
			nStart = 0;

		if ( nEnd == null )
			nEnd = chkList.length;

		for(var i=nStart; i<nEnd; i++)
		{
			chkList[i].checked = bChecked;
		}
	}
}

//String Functions
function trim(s)
{
	if ( s==null ) return null;

	for(var i=0; i<s.length && s.charAt(i)==' '; i++);
	s = s.substring(i);

	for(i=s.length-1; i>=0 && s.charAt(i)==' '; i--);
	s = s.substring(0,i+1);
	
	return s;
}

function strReplace(src, srep, swith)
{
	var s = src;
	var foundAt = s.indexOf(srep);
	while ( foundAt > -1 )
	{
		var stemp = s.substring(0, foundAt);
		stemp = stemp + swith + s.substring(foundAt + srep.length);
		s = stemp;
		foundAt = s.indexOf(srep);
	}
	return s;
}

///
function getInnerText(oObj)
{
	if (browser.ie)
		return oObj.innerText;

	var s = oObj.innerHTML;
	var i = s.indexOf('>');
	var j = s.lastIndexOf('<');
	if (i!=-1 && j!=-1)
		s = s.substring(i+1, j);

	return s;
}

//Validation function
function isEmpty(s)
{
	if ( (s==null) || (s=='') )
		return true;
	return false;
}
function validateSize(s, n)
{
	if ( (s==null) || (s.length<=n) )
		return true;
	alert("Input text exceeded the limit of " + n + " characters. Please try again.");
	return false;
}

//CL10 Starts
function showElement(elem, show, doc)
{
	if (doc==null) doc=document;
	var oElem = doc.getElementById( elem );
	if ( oElem )
	{
		if ( show )	//if show == true
			oElem.style.display = "inline";
		else
			oElem.style.display = "none";
		return true;
	}
	return false;	//Element not found
}

function enableElement(elem, enable, doc)
{
	if (doc==null) doc=document;
	var oElem = doc.getElementById( elem );
	if ( oElem )
	{
		oElem.disabled = !enable;
		return true;
	}
	return false;	//Element not found
}

function getValue(elem, doc)
{
	var val = null;
	if ( doc == null ) doc = document;
	var oElem = doc.getElementById( elem );
	if ( oElem )
		val = oElem.value;
	return val;
}

function setValue(elem, value, doc)
{
	if (doc == null) doc = document;
	var oElem = doc.getElementById(elem);
	if ( oElem )
	{
		oElem.value = value;
		return true;
	}
	return false;
}

function addTableRow(tblID, rowID, arrColList, srep, srepwith, clsRow, clsCol, colAlign, colVAlign)
{
	var doc = document;
	var oTbl = doc.getElementById(tblID);
	if ( oTbl == null ) return null;
	var oRow = doc.createElement("TR");
	if ( clsRow != null ) oRow.className = clsRow;
	if ( colAlign == null ) colAlign="center";
	if ( colVAlign == null ) colVAlign="top";
	for( var i=0; i<arrColList.length; i++)
	{
		var oCol = doc.createElement("TD");
		oCol.align=colAlign;
		oCol.vAlign = colVAlign;
		if ( srep!=null )
			oCol.innerHTML = strReplace( arrColList[i], srep, srepwith );
		else
			oCol.innerHTML = arrColList[i];

		if ( clsCol != null )	oCol.className=clsCol;

		oRow.appendChild( oCol );
	}
	if ( rowID != null ) oRow.id = rowID;
	oTbl.appendChild( oRow );
	return oRow;
}

function applyRowStyle( tblID, clsR1, clsR2, rowCnt, rowIDPrefix )
{
	var rowStyleToggle = false;

	for( var i=0; i<rowCnt; i++ )
	{
		var oRow = document.getElementById( rowIDPrefix + i );
		if ( oRow != null )
		{
			if ( oRow.style.display=="" || oRow.style.display=="inline" )
			{
				if ( rowStyleToggle )
					oRow.className = clsR2;
				else
					oRow.className = clsR1;
				rowStyleToggle = !rowStyleToggle;
			}
		}
	}
}

function swapImage(imgElem)
{
	if(document.all || document.layers)
	{
		var ts=imgElem.src
		imgElem.src=imgElem.lowsrc
		imgElem.lowsrc=ts
	}
}

function validateTimePart( elem, timePart, ampmTime, forceReent, dispError )
{
	var bValid = false;
	var timValMin = 0;
	var timValMax = 12;

	if ( ampmTime == null ) ampmTime=true;	//check against AM/PM 12 hours time
	if ( !ampmTime ) timValMax = 23;
	if ( dispError==null ) dispError=true;
	if ( timePart==null ) timePart='H';
	if ( timePart=='M' ) timValMax = 59;
	if ( forceReent==null ) forceReent=true;
	if ( elem != null )
	{
		var timVal = elem.value;
		if ( (timVal == null) || (timVal=='') ) timVal=0;
		bValid = ( timVal>=timValMin && timVal<=timValMax );
		if ( !bValid )
		{
			if ( dispError )
				alert("Invalid Time Value. Please enter a value from " + timValMin + " to " + timValMax);
			if ( forceReent )
				elem.focus();
		}
	}
	return bValid;
}

function inRange(val, mn, mx, msg)
{
	val = parseFloat(val);
	mn = parseFloat(mn);
	mx = parseFloat(mx);
	if ( isNaN(val) || isNaN(mn) || isNaN(mx) ) return false;
	if ( val<mn || val>mx )
	{
		if ( msg ) alert(msg);
		return false;
	}
	return true;
}

//Function: 	strQuote(s)
//Description:	Takes a string and converts ' or " into a string usable by 
//				JavaScript function by appending a \
//Return:		NULL if s is null or properly quoted string
function strQuote(s)
{
	var sQ = null;
	if ( s != null ) 
	{
		sQ = "";
		for(var i=0; i<s.length(); i++)
		{
			var ch = s.substring(i, i+1);
			if ( ch == "\'" )
				ch = "\\\'";
			else if ( ch == "\"" )
				ch = "\\\"";
			sQ = sQ + ch;
		}
	}
	return sQ;
}

function enableTableElemsRecursive(oChildren, enable, dbg)
{
	for( var i=0; i<oChildren.length; i++)
	{
		var oNode = oChildren[ i ];
		if ( dbg ) alert("[Debug] enableTableElemsRecursive::oNode.tagName(" + oNode.tagName + ")");
		if ( oNode.tagName )
		{
			if ( (oNode.tagName=="TR") || (oNode.tagName=="TD") )
			{
				enableTableElemsRecursive( oNode.childNodes, enable );
			}
			else if ( (oNode.tagName=="A") || (oNode.tagName=="IMG") || ( (oNode.tagName=="INPUT") && ("text/checkbox/radio/select/button".indexOf(oNode.type) > -1) ) ) 
			{
				if ( oNode.tagName=="A" )
				{
					if ( oNode.childNodes )
						enableTableElemsRecursive( oNode.childNodes, enable );
				}
				else if ( oNode.tagName == "IMG" )
					showElement( oNode.id, enable );
				oNode.disabled = !enable;
			}
		}
	}
}

function enableTableElements(doc, tblID, enable)
{
	var oTable = doc.getElementById( tblID );
	if ( oTable )
	{
		var oChildren = oTable.childNodes;
		enableTableElemsRecursive( oChildren, enable );
	}
}

function getDateTime()
{
	var dt = new Date();
	var sd = (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getYear();
	var st = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();

	return (sd + " " + st);
}

function getElement(id, doc)
{
	if ( doc == null ) doc = document;
	var oElem = doc.getElementById(id);
	return oElem;
}

function clearCombo(combo)
{
	for(var i=0; i<combo.options.length; i++)
		combo.options[i] = null;
	combo.options.length = 0;
}

function setFocus(elId, doc)
{
	if ( doc == null ) doc = document;
	var oElem = getElement(elId, doc);
	if ( oElem != null )
		oElem.focus();
}
function showMessageWithFocus(msg, elem, doc)
{
	alert(msg);
	setFocus(elem, doc);
}
//CL10 Ends
/*
 CL14 Starts
 Input: event object,field name and regular expressions string
 Output: Checks that field contains any character other than those,specified by regular expression string, if so then that character
 		 will be truncated from field value.  
*/
function validateAlphaNumericCheck(objEvent,fld,regularExp)
{
 //mask function
	//browser detection
	//alert("a");
 var strUserAgent = navigator.userAgent.toLowerCase(); 
 var isIE = strUserAgent.indexOf("msie") > -1; 
 var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
 var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 
 var reAmountChars = /\d/;
 var optionSelected;
 //alert("b "+strUserAgent);
 ////////////////////////////////////////
 var iKeyCode, strKey;  
 //alert("c");
 //var obj = window.event.srcElement;
 //alert("D");
 var reValidChar   = regularExp; //FOR VALIDATING REGULAR EXPRESSION
 var szAccString = String(document.getElementById(fld).value);     
 		

	
		if (isIE)
	        {
		    iKeyCode = objEvent.keyCode;
		} else
                {
		    iKeyCode = objEvent.which;
		}
				
		strKey = String.fromCharCode(iKeyCode);
		//alert(iKeyCode);
		if (!reValidChar.test(strKey)&& iKeyCode!=8&&iKeyCode!=0)//KeyCode 8 for backspace & 0 for special keys 
		{
		   if(iKeyCode!=13)//For Enter key
		   	alert("This character is not allowed");
		   //fld.value="";	
		   return false;
		}

 	  

	

}

//CL14 ends
