// validation support functions:
//
// checkZIP(field)
// checkblank_sub(what,whatname)
// phoneformat(what)
// isCreditCard(textObj)
// expired( month, year )
// validEmail(eAddr) 
// phoneDisplay(i, delKey) 
//validateNotEmpty - checks for blank form field

function checkZIP(field)
	{

	if ( field.value.length != 5 && field.value.length != 10) return false;

	if (field.value.length == 5) {

        for (i = 0; i < field.value.length; i++)
        if ((field.value.substring(i, i + 1) < "0") || (field.value.substring(i, i + 1) > "9") ) return false
	    }

	if (field.value.length == 10) {
	        for (i = 0; i < 5; i++){
            if ((field.value.substring(i, i + 1) < "0") || (field.value.substring(i, i + 1) > "9") ) return false
	        }

	        for (i = 6; i < 10; i++){
            if ((field.value.substring(i, i + 1) < "0") || (field.value.substring(i, i + 1) > "9") ) return false
	        }
	
            if (field.value.substring(5, 6) != "-")  return false
	
	}

	        return true;
	
	}


function checkblank_sub(what,whatname) {
	// submit check with alerts
	tocheck=what.value

	if (tocheck=="" ||tocheck.substring(0,5)=="Enter"){
	alert("Empty required field: "+ whatname+".","")
		what.focus()
		return false
		}
	return true
}


function phoneformat(num){
	var phonestr = num.value;
	if (phonestr.length == 10){
	phonestr = phonestr.substring(0,3) + "-" + phonestr.substring(3,6) + "-" + phonestr.substring(6,10);
	num.value = phonestr;
	num.style.Color="#000000";
	}
			
	if (phonestr.length != 12) return false;
	for (var i=0; i<phonestr.length; i++){
		if ((i<3 )||
		   (i>3 && i<7)||
		   (i>7 && i<12)){
		   if (phonestr.charAt(i)<"0"||
			  phonestr.charAt(i)>"9") return false;
	    }
	    else if (phonestr.charAt(3) != "-" ||
				 phonestr.charAt(7) != "-" ) return false;
	 }
 return true;
		
}

function isCreditCard(textObj) {
 /*
  *  This function validates a credit card entry.
  *  If the checksum is ok, the function returns true.
  */
   var ccNum
   var odd = 1
   var even = 2
   var calcCard = 0
   var calcs = 0
   var ccNum2 = ""
   var aChar = ''
   var cc
   var r

   ccNum = textObj.value
   

   for(var i = 0; i != ccNum.length; i++) {
      aChar = ccNum.substring(i,i+1)
      if(aChar == '-') {
         continue
      }
      ccNum2 = ccNum2 + aChar
   }
	
   cc = parseInt(ccNum2)
   r = ccNum.length / 2
   if(cc == 0) {
      return 1
   }
   if(ccNum.length - (parseInt(r)*2) == 0) {
      odd = 2
      even = 1
   }

   for(var x = ccNum.length - 1; x > 0; x--) {
      r = x / 2
      if(r < 1) {
         r++
      }
      if(x - (parseInt(r) * 2) != 0) {
         calcs = (parseInt(ccNum.charAt(x - 1))) * odd
      }
      else {
         calcs = (parseInt(ccNum.charAt(x - 1))) * even
      }
      if(calcs >= 10) {
         calcs = calcs - 10 + 1
      }
      calcCard = calcCard + calcs
   }

   calcs = 10 - (calcCard % 10)
   if(calcs == 10) {
      calcs = 0
   }
   
   if(calcs == (parseInt(ccNum.charAt(ccNum.length - 1)))) {
      return true
   }
   else {
      return false
   }
}


function expired( month, year ) {
	
	var now = new Date();							// this function is designed to be Y2K compliant.
	var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
	expiresIn.setMonth(expiresIn.getMonth());		// adjust the month, to first day, hour, minute &amp; second of expired month
	if( now.getTime() < expiresIn.getTime() ) return false;
	return true;									// then we get the miliseconds, and do a long integer comparison
}



function validEmail(eAddr) 
{ 


   var goodAddr = false;
   var ndxAt = ndxDot = 0;

   ndxAt  = eAddr.indexOf("@");
   ndxDot = eAddr.indexOf(".");

   if ( (ndxDot < 0) || (ndxAt < 0) )
      alert("Your email address lacks '.' or '@'.\n\nThe format is 'you@domainname.suf'");  
    else
      goodAddr = true;

   return (goodAddr);                       

}


//******************************************
//The next section automatically adds '(', ')',
//and '-' characters to phone number fields
//as the user types (so the user only needs
//to type the 10 numbers).
//******************************************/

function phoneDisplay(i, delKey){
  

	if(delKey!=8 && delKey!=46 && delKey!=9 && delKey!=16 && !(delKey>36 && delKey<41)){//if the delete, backspace, tab, shift, are not the keys that caused the keyup event.
		var fieldLen = i.value.length;
		var areaCode = "";
		var exchange = "";
		var lastFour = "";
		var currVal = i.value;
		var firstParens = 0;
		var firstParens = parseInt(firstparens);
		var secondParens = 3;
		var secondParens = parseInt(i.value.indexOf("-")) + parseInt(1);
		if(firstParens < 0){ firstParens=0; }
		if(secondParens < 1){ secondParens=4; }
		//if(fieldLen==0 ){ areaCode = i.value; }
		if(fieldLen>0){
			areaCode = i.value.substring(firstParens,3);
		}
		if(fieldLen>3){
			exchange = i.value.substring(secondParens,7);
		}
		if(fieldLen>6){
			lastFour = i.value.substring(8,12);
		}
//alert("x" + lastFour + "y");
		if(fieldLen<3){
		i.value = areaCode ;
			i.focus();
		} else
		if(fieldLen<7){
			i.value = areaCode + "-" + exchange;
			i.focus();
		} else
		if(fieldLen<12){
			i.value = areaCode + "-" + exchange + "-" + lastFour;
			i.focus();
		}
	}//end if delete key=8,46
}

function isLeapYear (Year) { 
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) { 
		return (true);
	} else 
	{ return (false) 
	}
}

function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}
