var errorList 	=  "";
var errorFocus	= "";

function buildError(errorMsg, errorElem) {
	errorList 	= (errorList == "") ? errorMsg : errorList + "\n" + errorMsg;
	
	if(errorFocus == "") {
		errorFocus = errorElem;
	}
}

function errorFound(docForm) {
	if(errorList != "") {
		alert("The following error(s) occured:\n" + errorList);
		errorList = "";
		
		if(errorFocus != "") {			
			docForm[errorFocus].focus();
			if(docForm[errorFocus].value != "") {
				if(docForm[errorFocus].type == "text" || docForm[errorFocus].type == "password" || docForm[errorFocus].type == "textarea") {
					docForm[errorFocus].select();
				}
			}
			errorFocus = "";
		}	

		return true;
	}
	else {
		return false;
	}
}

function checkAlpha(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789()'&@-_/,.;:?!\"\r\n "
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 

function checkAlphaName(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'@-/,. "
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 

function checkAlphaEmail(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@-_."
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 


function checkNum(field) {
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 

function checkICPassport(field) {
	var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 


function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}

function chkFromTodate(sday,smonth,syear,eday,emonth,eyear)
{
   sday   = parseInt(sday);
   smonth = parseInt(smonth);
   eday   = parseInt(eday);
   emonth = parseInt(emonth);

   if (syear > eyear) { return false; }
   if (syear < eyear) { return true;  }
   if (smonth > emonth) { return false; }
   if (smonth < emonth) { return true; }               
   if (sday > eday) { return false; }
   return true;
}

function isValidEmail(strEmail)
{
	var x = strEmail;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) {
		return true;
	}
	else {
		return false;
	}
}

//BEGIN ###########################################################################
// chkBoxAll ( BOOLEAN chk [, OBJECT tg_form, STRING elemName ] )
// chkBoxAll( true, self.document.frmMailList, 'chkMsgID')
function chkBoxAll (chk) {
	if (chkBoxAll.arguments.length >= 2 )
    	{ var tg_form = chkBoxAll.arguments[1]; }
	else
		{ var tg_form = document.forms[0]; }

	for (var i=0; i<tg_form.elements.length; i++ )
	{
		var elem = tg_form.elements[i];
        if ( elem.type != "checkbox" ) { continue; }
		        
		if ( chkBoxAll.arguments.length >= 3 )
        {
        	var elemName = chkBoxAll.arguments[2];
            if ( elem.name == elemName ) { elem.checked = chk; }
		}
        else
        	{ elem.checked = chk; }
	}
}

// chkBoxCount ( [ OBJECT tg_form, STRING elemName ] )
// chkBoxCount ( self.document.frmMailList, 'chkMsgID')
function chkBoxCount()
{
	var chkCount = 0;

	if ( chkBoxCount.arguments.length >= 1 )
    	{ var tg_form = chkBoxCount.arguments[0]; }
	else
    	{ var tg_form = document.forms[0]; }

	for ( var i=0; i<tg_form.elements.length; i++ ) {
		var elem = tg_form.elements[i];
        if ( elem.type != "checkbox" ) { continue; }

        if ( chkBoxCount.arguments.length >= 2 )
        {
        	var elemName = chkBoxCount.arguments[1];
            if ( elem.name == elemName )
            	{ if ( elem.checked ) { chkCount += 1; } }
		}
        else
        	{ if ( elem.checked ) { chkCount += 1; } }
	}

	return chkCount;
}

// chkBoxValue ( [ OBJECT tg_form, STRING elemName ] )
// chkBoxValue ( self.document.frmMailList, 'chkMsgID')
function chkBoxValue()
{
	var chkValue = "";

	if ( chkBoxValue.arguments.length >= 1 )
    	{ var tg_form = chkBoxValue.arguments[0]; }
	else
    	{ var tg_form = document.forms[0]; }

	for ( var i=0; i<tg_form.elements.length; i++ ) {
		var elem = tg_form.elements[i];
        if ( elem.type != "checkbox" ) { continue; }
		
        if ( chkBoxValue.arguments.length >= 2 ) {
        	var elemName = chkBoxValue.arguments[1];
            if ( elem.name == elemName )
            {
            	if ( elem.checked )
                	{ chkValue = (chkValue==null || chkValue.length==0 || chkValue=="")? elem.value:chkValue+","+elem.value; }
			}
		}
        else {
			if ( elem.checked )
            	{ chkValue = (chkValue==null || chkValue.length==0 || chkValue=="")? elem.value:chkValue+","+elem.value; }
		}
	}

	return chkValue;
}
//END 	- Checkbox function #######################################################

//BEGIN	- Date Checking Utilities #################################################
//
//Supported Date Format
//mm-dd-yyyy, mm/dd/yyyy, mm.dd.yyyy, mm dd yyyy, 
//mmm dd yyyy, mmddyyyy, m-d-yyyy, m/d/yyyy, m.d.yyyy, 
//m d yyyy, mmm d yyyy, m-d-yy, m/d/yy, m.d.yy, m d yy, 
//mmm d yy (yy is 20yy) 
function checkdate(objName) {
	var datefield = objName;
	if (chkdate(objName) == false) {
		//datefield.select();
		//alert("That date is invalid.  Please try again.");
		//datefield.focus();
		return false;
	}
	else {
		return true;
	}
}

function chkdate(objName) {
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;

	if (strDate.length < 1) {
		return true;
	}

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);

			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}

	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}

	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}

	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}

	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}

	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
	   }
	}

	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}

	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}

	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}

		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}

	if (strDatestyle == "US") {
		datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function doDateCheck(from, to) {
	if (Date.parse(from) <= Date.parse(to.value)) {
		//alert("The dates are valid.");
		return true;
	}
	else {
		if (from.value == "" || to.value == "") 
			//alert("Both dates must be entered.");
			return false;
		else 
			return false;
			//alert("To date must occur after the from date.");
	}
}

function validDateRange(effDateChar) { 
	//Date format to pass in is D-M-Y
	//M must be of minus 1 as JS array begins with 0 and not 1
	var cal = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
	var sysDate		= new Date(); 

   var bufArray		= effDateChar.split("-"); 
   var effDateMonth = cal[bufArray[1]].toUpperCase(); 
   var effDateDay	= bufArray[0]; 
   var effDateYear	= bufArray[2]; 
   var effDateDate	= new Date(effDateMonth + " " + effDateDay + ", " + effDateYear + " 23:59:59"); 

   // compare the 2 dates 
  // alert("effDateDate : " + effDateDate + "\nsysDate : " + sysDate);

	if (effDateDate < sysDate) 
	{
		//alert("The effective date must be today's date or a later date."); 
		return false; 
	} 
	else {
		return true; 
	}
} 
//END	- Date Checking Utilities #################################################




