/*checkdate(objName)*/
/*chkdate(objName)*/
/*doDateCheck(from,to)*/
/*isNumeric(elmnt)*/
/*isEmpty(elmnt)*/
/*isYearLTCurrentYear(elmnt)*/
/*isYearLTECurrentYear(elmnt)*/
/*isYearGTECurrentYear(elmnt)*/
/*isYearGTEffectiveYear(elmnt, minusYears)*/
/*isValidMonth(elmnt)*/
/*isValidDay(elmnt)*/
/*isValidYear(elmnt)*/
/*isLTEMaxLen(elmnt)*/
/*isGTEMinLen(elmnt,minLength)*/
/*isEquelMaxLen(elmnt)*/
/*isValidEmail(elmnt)*/
/*isValidUserID(elmnt)*/
/*funOnKeyUpAutoTab(elmnt,Content)*/
/*enableDisableElmnt(elmnt, elmntState)*/
/*checkPassword(ref, ref1) */
/*validateAddress(ref) */
/*isValidNumLength(ref, length) */
/*allDigits(str) */
/*inValidCharSet(str,charset) */
/*validFullDate(formField,fieldLabel) */
/*isDateStartLTEnd(start, end) */
/*isDateRangeWithin(start, end, range) */
/*printSpecial(printDivId) */
/*haveSpaces(elmnt) */
/*getPCDate() */
/*checkEnter(e) to call have this in the <input> : onkeypress="checkEnter(event)" */
/*isRadioGroupSelected */
/*trim(inputString) */

function encodeValue(strVal) {
	if (strVal=="") { alert("Unable To Validate Empty String");  }
	encodedValue = escape(strVal);
	return encodedValue;
}

function isNumeric(elmnt)
{
   RefString     = "1234567890";

   for (i=0; i<elmnt.value.length; i++)
   {
      TempChar = elmnt.value.charAt(i);

      /* Check if current character is valid */

      if ( RefString.indexOf (TempChar,0) < 0 ) 
      {
         /* character not found in RefString */
         return false;
      }
   }
   return true;
 }

function isEmpty(elmnt)
{

  enteredValue = elmnt.value;
  
  if(enteredValue.length == 0){
	return true;
  }
  return false;
}
function isYearLTCurrentYear(elmnt)
{
  CurDate = new Date ();
  Curyear = CurDate.getFullYear(); 
  EnteredYear = elmnt.value;

  if ( EnteredYear < Curyear ) 
     {
         /*  */
         return true;
      }
   return false;
}

function isYearLTECurrentYear(elmnt)
{
  CurDate = new Date ();
  Curyear = CurDate.getFullYear(); 
  EnteredYear = elmnt.value;

  if ( EnteredYear <= Curyear ) 
     {
         /*  */
         return true;
      }
   return false;
}

function isYearGTECurrentYear(elmnt)
{
  CurDate = new Date ();
  Curyear = CurDate.getFullYear(); 
  EnteredYear = elmnt.value;

  if ( EnteredYear > Curyear ) 
     {
         /*  */
         return true;
      }
   return false;
}

function isValidMonth(elmnt)
{
	if(isEmpty(elmnt)){
		return false;
	}
	
   RefString     = "'1','2','3','4','5','6','7','8','9','10','11','12','01','02','03','04','05','06','07','08','09'";
   TempChar = elmnt.value;

      /* Check if month is valid */
      if ( RefString.indexOf (TempChar,0) == -1)
     {
         /*  */
         return false;
      }
   return true;
 }

function isValidDay(elmnt)
{
	if(isEmpty(elmnt)){
		return false;
	}
   RefString     = "'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','01','02','03','04','05','06','07','08','09'";
   TempChar = elmnt.value;

      /* Check if current character is valid */
      if ( RefString.indexOf (TempChar,0) == -1)
      {
         /* character not found in RefString */
         return false;
      }
   return true;
 }

function isValidYear(elmnt){
 	if(isEmpty(elmnt)){
 		return false;
 	}
 	if(!isNumeric(elmnt)){
 		return false;
 	}
 	if(elmnt.value.length !=4 ){
 		return false;
 	}
 	return true;
 }

function isLTEMaxLen(elmnt)
{
  elmntValLen = elmnt.value.length;
  elmntMaxLen = elmnt.maxLength;

  if ( elmntValLen <= elmntMaxLen ) 
     {
         /*  */
         return true;
      }
   return false;
}  

function isGTEMinLen(elmnt,minLength)
{
  elmntValLen = elmnt.value.length;
  elmntMinLen = minLength;

  if ( elmntValLen >= elmntMinLen ) 
     {
         /*  */
         return true;
      }
   return false;
}
function isEquelMaxLen(elmnt)
{
  elmntValLen = elmnt.value.length;
  elmntMaxLen = elmnt.maxLength;

  if ( elmntValLen == elmntMaxLen ) 
     {
         /*  */
         return true;
      }
   return false;
}

function isValidEmail(elmnt)
{
   RefString1 = "'@'";
   RefString2 = "'.'";
   elmntVal = elmnt.value;

   if ((elmntVal.indexOf (RefString1,0) == -1) || (elmntVal.indexOf (RefString2,0) == -1))
      {
         /* character not found */
         return false;
      }
   return true;
 }
function isValidUserID(elmnt, length)
{
	if(isEmpty(elmnt)){
		return false;
	}
	 
	if(!isGTEMinLen(elmnt, length)){
		return false;
	}
	 
   RefString     = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";

   for (i=0; i<elmnt.value.length; i++)
   {
      TempChar = elmnt.value.substring ( i, i+1 );

      /* Check if current character is valid */

      if ( RefString.indexOf (TempChar,0) == -1)
      {
         /* character not found in RefString */
         return (false);
      }
   }
   return(true);
 }
 
 function isValidNewPwd(elmnt){
	if(isEmpty(elmnt)){
		return false;
	}
	 
   RefString     = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$_-";

   for (i=0; i<elmnt.value.length; i++)
   {
      TempChar = elmnt.value.substring ( i, i+1 );

      /* Check if current character is valid */

      if ( RefString.indexOf (TempChar,0) == -1)
      {
         /* character not found in RefString */
         return (false);
      }
   }
   return(true);
 }

function funOnKeyUpAutoTab(elmnt,Content)
{  
  if (Content.length==elmnt.maxLength)
  {
	next=elmnt.tabIndex;
	if (next<document.forms[0].elements.length)
	{ tab=next+1;
	  for(next=0; next<document.forms[0].elements.length; next++){
	      if(document.forms[0].elements[next].tabIndex==tab){
	         document.forms[0].elements[next].focus();
               break;
	      }
        }
	}
  }
}  

function enableDisableElmnt(elmnt, elmntState) {
	if (elmntState == false || elmntState == true ){
		elmnt.disabled = elmntState;
		return;
	} 
	if (elmnt.disabled == true) {
		elmnt.disabled = false;
		return;
	}	
	if (elmnt.disabled == false) {
		elmnt.disabled = true;
		return;
	}	
}
function isValidNumLength(ref, length){
	if(isEmpty(ref)){
 		return false;
 	}
 	if(!isNumeric(ref)){
 		return false;
 	}
 	
 	if(ref.value.length != length ){
 		return false;
 	}
 	return true; 
}

function checkPassword(ref, ref1){
	if(isEmpty(ref)){
		alert("Password is required.");
		ref.focus();
		return false;
	}
	if(isEmpty(ref1)){
		alert("Password is required.");
		ref.focus();
		return false;
	}
	if(!isGTEMinLen(ref, 7)){
		alert("Password is at least seven characters.");
		ref.focus();
		return false;
	} 
	if(ref.value != ref1.value ){
		alert("The 2 passwords you entered did not match.  Please double check your password.");
		ref1.focus();
		return false;
	}
	return true;
}

function validateAddress(ref) {
	var emailstring = ref.value;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
		//old regex did not allow subdomains and dots in names
		//var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
		// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
		  return true;
	} else {
	//		alert("Please check your email address!");
			return false;
	}
}

function isValidNumLength(ref, length){
	if(isEmpty(ref)){
 		return false;
 	}
 	if(!isNumeric(ref)){
 		return false;
 	}
 	if(ref.value.length != length ){
 		return false;
 	}
 	return true; 
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function allAlphaNumeric(str){
    return inValidCharSet(str,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validFullDate(formField,fieldLabel)
{
	var result = true;
  
 	if (result)
 	{
 		var elems = formField.value.split("-");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && (elems[2].length == 4);
 		}
 		
  		//if (!result)
 		//{
 		//	alert('Please enter a date in the format MM-DD-YYYY for the "' + fieldLabel +'" field.');
		//	formField.focus();		
		//}
	} 
	
	return result;
}

function isDateStartLTEnd(start, end) 
{
	if (Date.parse(start.value) > Date.parse(end.value) ) 
		{
			return false;
		}
	return true;
}

function isDateRangeWithin(start, end, range) 
{
	if (range == "31") 
	{
		if ( (Date.parse(end.value) - Date.parse(start.value)) > 2592000000 ) 
		{
			return false;
		}
	}
	if (range == "366") 
	{
		if ( (Date.parse(end.value) - Date.parse(start.value)) > 31536000000) 
		{
			return false;
		}
	}
	return true;
} 


function printSpecial(printDivId,logoId)
{
	var gAutoPrint = true; // Flag for whether or not to automatically call the print function
	if (document.getElementById != null)
	{
		var html = '<HTML>\n<HEAD>\n';

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}
		
		html += '\n</HE' + 'AD>\n<BODY>\n';
		if (logoId!=null)
		{
			var logo = '<table>\n<thead>\n<tr><td align="left"><IMG border="0" src='+logoId+'></td></tr>\n</thead>\n</table>\n';
			html += logo;
		}
		
		var printReadyElem = document.getElementById(printDivId);
		
		if (printReadyElem != null)
			{
				html += printReadyElem.innerHTML;
			}
		else
			{
				alert("Could not find the"+printDivId+" section in the HTML");
				return;
			}
			
		html += '\n</BO' + 'DY>\n</HT' + 'ML>';
		
		var printWin = window.open("","printableVersion","width=800,height=600,toolbar=0,location=0,directories=0,status=1,menuBar=0,scrollBars=1,resizable=1");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		if (gAutoPrint)
			printWin.print();
	}
	else
	{
		alert("Sorry, the print ready feature is only available in modern browsers.");
	}
}

function haveSpaces(elmnt) {
	strElmnt = elmnt.value;
	if (strElmnt.match(" ")!=null) {
		return true;	
	}
	return false;	
}

function getPCDate() {
	pcdt = new Date();
	day = pcdt.getDate();
	if (day.toString().length == 1) {day = "0"+day;}
	year = pcdt.getFullYear();
	month = pcdt.getMonth()+1;
	if (month.toString().length==1) {month = "0"+month;}
	pcDate = month+"-"+day+"-"+year;
	return pcDate;
}

function isNum(value){
	var regExp = /^\d/
	if(regExp.test(value)){
		return true;
	}else{
		return false;
	}
}

function checkRadioGroup(groupRef){
	if(!isNum(groupRef.length)  ){
		if(groupRef.checked){
			return true;
		} else {
			return false;
		}
	} 
	for(counter=0; counter<groupRef.length; counter++){
		if(groupRef[counter].checked){
			return true;
		}
	}
	return false;
}

function checkEnter(e)
{	//e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable
	if(e && e.which)
	{ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else {
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		return false; 
	} else {
		return true; 
	}
}

function isRadioGroupSelected(ref)
{	for( count=0; count < ref.length ; count ++) 
	{	if(ref[count].checked) {
			return true;
		}
	}
	return false;
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function	

//BEGIN date validation related functions
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c < "0") || (c > "9")) {return false;}
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this;
}
function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) {strDay=strDay.substring(1);}
	if (strMonth.charAt(0)=="0" && strMonth.length>1) {strMonth=strMonth.substring(1);}
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) { strYr=strYr.substring(1); }
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm-dd-yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true;
}


function isValidDateLength(formField) 
{
	var date = trim(formField.value);
	if (date.length != 10)
		return false;
	else
		return true;	
}

/**
	validates date in format MM-DD-YYYY or MM/DD/YYYY
**/
function isValidDate(formField, splitStr)
{
	var result = true;
  
 	if (result)
 	{
 		var elems = formField.value.split(splitStr);
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && (elems[2].length == 4);
 		}
 		
	} 
	
	return result;
}
	
/**
	convert date from MM/DD/YYYY or MM-DD-YYYY to YYYYMMDD
**/ 
function convertDate(formField, splitStr)
{

			  
 		var elems = formField.value.split(splitStr);
 		
 		var result = (elems.length == 3); // should be three components
 		
 		var date = "19000101";
 		if (result)
 		{
 			date = elems[2] + elems[0] + elems[1];
 		}
		return date;
}

//END date validation related functions