//    Script Editor:  Mark , Jack & Richard

//Check if field is empty
function emptyField(textObj) {
if (textObj.value.length == 0) return true;
        for (var i=0; i<textObj.value.length; ++i) {
                var ch = textObj.value.charAt(i);
                if (ch != ' ' && ch != '\t') return false;
		}
return true;
}

//if (!isValidZip(Customer.Zipcode.value)) {
//	document.Customer.Zipcode.focus();
//	return false;
//}		

function isValidZip(field) {
	var valid = "0123456789";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) {
		alert("Please enter your 5 digit NNNNN" );
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			alert("There are invalid characters in your zip code.  Please try again.");
			return false;
		}
	}

	return true;
}

//if (!SSNValidation(Customer.ssn.value)) {
//	document.Customer.ssn.focus();
//	return false;
//}		

function SSNValidation(ssn) {
	var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var matchArr1 = ssn.match(/^(\d{2})-?\d{7}$/);
	var numDashes = ssn.split('-').length - 1;
	if (matchArr == null && matchArr1 == null || numDashes == 0) {
			alert("Invalid Federal ID or Social Security Number. Must contain 9 digits in the form of NN-NNNNNNN or NNN-NN-NNNN.");
			return false;
	}
	else 
		//if (parseInt(matchArr[1],10)==0) {
		//	alert("Invalid Federal ID or Social Security Number: Numbers cannot start with 00.");
		//	return false;
		//}
		//else
		{
			return true;
	}
}

//if (!isEmailValid(Customer.Email.value)) {
//	alert("You're Email Address is Invalid");
//	document.Customer.Email.focus();
//	return false;
//}		

function isEmailValid(Emailvalue) {

	var atsym=false
	var periodsym=false

	for (var i = 0; i <= Emailvalue.length; i++){
		if (Emailvalue.charAt(i) == '@'){ 
			atsym=true;
			}
		if (Emailvalue.charAt(i) == '.'){ 
			periodsym=true;
			}
		}
	if ((atsym==false) ||(periodsym==false)) {
			return false;
			}
	return true;
}

//if (!isValidEmail(Customer.Email.value)) {
//	document.Customer.Email.focus();
//	return false;
//}		

function isValidEmail(email){
	
   var invalidChars = "/:,;";

	if (email.value == "") {
		alert("Please enter an E-Mail address.");
		return false;
	}

   for (i=0; i<invalidChars.length; i++) {  //does it contain any invalid characters?
       badChar = invalidChars.charAt(i);
       if (email.value.indexOf(badChar,0) > -1){
		  alert("The E-Mail address contains invalid characters.");
          return false;
       }
   }

   atPos = email.value.indexOf("@",1)
   if (atPos == -1){
	  alert("Please enter a valid E-Mail address.");
      return false
   }
   
   if (email.value.indexOf("@",atPos+1) != -1) {
	  alert("Please enter a valid E-Mail address.");
      return false
   }
   
   periodPos = email.value.indexOf(".", atPos)
   if (periodPos == -1){
	  alert("Please enter a valid E-Mail address.");
      return false
   }
   if (periodPos+3 > email.value.length){
	  alert("Please enter a valid E-Mail address.");
      return false
   }

	return true
}


//if (!isCurrency(FormData.Price.value)) {
//	alert("Please enter a value without dollar signs or commas ");
//	document.FormData.Price.focus();
//	return false;
//}

function isCurrency(curr) {
	for (var i=0;i<curr.length;i++) {
		aChar=curr.substring(i,i+1)
			if((aChar < "0" || aChar > "9") && aChar != ".") //Identify what is legal
			{
			return false
			}
	}
	return true
}

//if (!isNumber(document.FormData.CreditCardNo))
//	alert("Must be numbers only no spaces or - for Credit Card No. ");
//	document.FormData.CreditCardNo.focus();
//	return false;
//}


// Number Validation
function isNumber(num) {
	for (var i=0;i<num.length;i++) {
		aChar=num.substring(i,i+1)
		if(aChar < "0" || aChar > "9")//Identify what is legal
			{
			return false
			}
	}
	return true
}

//if (!isString(document.FormData.CreditCardName))
//	alert("Please enter Valid Characters for Credit Card Name. ");
//	document.FormData.CreditCardName.focus();
//	return false;
//}

// String Validation
function isString(textObj) {
	var newValue = textObj
	var newLength = newValue.length
	var extraChars=". -,"
	var search
	for(var i = 0; i != newLength; i++) 
	{
	aChar = newValue.substring(i,i+1)
	aChar = aChar.toUpperCase()
	search = extraChars.indexOf(aChar)
		if(search == -1 && (aChar < "A" || aChar > "Z") ) 
		{
	return false
		}
	}
return true
}

/*	if (!isCCdateExpired((document.CC.CCMonth.options[document.CC.CCMonth.selectedIndex].value),(document.CC.CCYear.options[document.CC.CCYear.selectedIndex].value))) {

	alert("This Credit Card has Expired!!" );
	document.CC.CCMonth.focus();
	return false;
	} */


//Check if Date has Expired
function isCCdateExpired(CCMonth1,CCyear1) {

	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth()+1;
	var strmonth = "";
	
	if (month<10)
	{
		strmonth='0'+month
		}  
		else  
			{
		   strmonth=""+month
		   } 

	var strMyStartDate = year + strmonth;
	var strCCSelMonth = "" + CCMonth1;
	var strCCSelYear = "20" + CCyear1;
	var strCCExpireDate = strCCSelYear + strCCSelMonth;
	if (strCCExpireDate < strMyStartDate)  
		{
	   return false;
           }
   return true;
	
}



//if (!isCreditCard(document.FormData.CreditCardNo.value)) {
//	alert("Invalid Credit Card Number. Please try again.");
//	document.FormData.CreditCardNo.focus();
//	return false;
//}

// Credit Card Mode 10 Validation
function isCreditCard(CCNumber) {
// 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 = CCNumber
		for(var i = 0; i != ccNum.length; i++)
	{
        aChar = ccNum.substring(i,i+1)
        if(aChar == '-') 
		{
                continue
                }
                ccNum2 = ccNum2 + aChar
        }
        cc = parseInt(ccNum2)
        if(cc == 0)
		{
                return false
	        }
        r = ccNum.length / 2
        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
		     }
}

// Validate Trans Route no
function isValidTransRoute(trnStr) {
if (trnStr.length < 9)
   {
   return false
   }
V='01 02 03 04 05 06 07 08 09 10 11 12 21 22 23 24 25 26 27 28 29 30 31 32 64 65 66 67'
F2=trnStr.substr(0,2)
if (V.indexOf(F2) == -1)
   {
   return false
   }
Weight="37137137"
J=0
Sums=0
while (J < 8)
   {
   i=trnStr.charAt(J)
   k=Weight.charAt(J)
   Sums=Sums+(i*k)
   J=J+1
   }
J=Sums % 10
if (J != 0)
   {
   J=10-J
   }
Cd=(""+J)
if (trnStr.substr(8,1) != Cd)
   {
   return false
   }
else
   {
   return true
   }
}
