// Validation
var msg = "";
var d = new Date();
var curr_year = d.getFullYear(); 
var curr_month = d.getMonth();
var targetDate = new Date(d.getFullYear(), 3, 29)
var one_week = 1000*60*60*24*7
var weeks = Math.ceil((targetDate.getTime()-d.getTime())/(one_week));

function validate(frm){
	msg = "";

	// Order Information Required
	if (frm.blue_qty.value=="0" && frm.black_qty.value=="0" && frm.brown_qty.value=="0" && frm.green_qty.value=="0" && frm.cran_qty.value=="0" && 	frm.lilac_qty.value=="0" && frm.yellow_qty.value=="0"){ 
		msg+="* [Order Information] Product selection is required!<br />";
	}

	// Shipping Information Required
	if (frm.First_Name.value==""){ 
		msg+="* [Shipping Information] First Name is required!<br />";
	}
	if (frm.Last_Name.value==""){ 
		msg+="* [Shipping Information] Last Name is required!<br />";
	}
	if (frm.Street.value==""){ 
		msg+="* [Shipping Information] Street is required!<br />";
	}
	if (frm.City.value==""){ 
		msg+="* [Shipping Information] City is required!<br />";
	}
	if (frm.State.value==""){ 
		msg+="* [Shipping Information] State is required!<br />";
	}
	if (frm.ZIP.value==""){ 
		msg+="* [Shipping Information] ZIP is required!<br />";
	}

	// Billing Information Required
	if (frm.bFirst_Name.value==""){ 
		msg+="* [Billing Information] First Name is required!<br />";
	}
	if (frm.bLast_Name.value==""){ 
		msg+="* [Billing Information] Last Name is required!<br />";
	}
	if (frm.bStreet.value==""){ 
		msg+="* [Billing Information] Street is required!<br />";
	}
	if (frm.bCity.value==""){ 
		msg+="* [Billing Information] City is required!<br />";
	}
	if (frm.bState.value==""){ 
		msg+="* [Billing Information] State is required!<br />";
	}
	if (frm.bZIP.value==""){ 
		msg+="* [Billing Information] ZIP is required!<br />";
	}
	if (frm.Email.value==""){ 
		msg+="* [Billing Information] E-mail Address is required!<br />";
	}
	if (frm.Phone_Area.value=="" || frm.Phone_3.value=="" || frm.Phone_4.value==""){ 
		msg+="* [Billing Information] Telephone Number is required!<br />";
	}

	// Payment Information Required
	if (frm.CCName.value==""){ 
		msg+="* [Payment Information] Card Holder's Name is required!<br />";
	}
	if (frm.CCNum.value==""){ 
		msg+="* [Payment Information] Card Number is required!<br />";
	}
	if (frm.CCType.value==""){ 
		msg+="* [Payment Information] Credit Card Type is required!<br />";
	}
	if (frm.CCExpMonth.value==""){ 
		msg+="* [Payment Information] Exp Date Month is required!<br />";
	}
	if (frm.CCExpYear.value==""){ 
		msg+="* [Payment Information] Exp Date Year is required!<br />";
	}
	if (frm.CICDigit.value==""){ 
		msg+="* [Payment Information] Card Verification # is required!<br />";
	}

	if (msg!=""){
		document.getElementById("msg").innerHTML=msg;
		return false;
	}

	if (msg==""){
		if (frm.ZIP.value.length < 5){
			msg+="* [Shipping Information] ZIP must have 5 digits!<br />";
		}
		if (frm.bZIP.value.length < 5){
			msg+="* [Billing Information] ZIP must have 5 digits!<br />";
		}
		if (invalidEmail(frm.Email) || invalidEmail2(frm.Email)){
			msg+="* [Billing Information] Email is invalid!<br />";
		}
		if (frm.Phone_Area.value.length < 3 || frm.Phone_3.value.length < 3 || frm.Phone_4.value.length < 4){ 
			msg+="* [Billing Information] Telephone Number is invalid!<br />";
		}

		Mod10(frm.CCNum.value,frm);

		if (frm.CCExpYear.value == curr_year && frm.CCExpMonth.value < curr_month + 1){
			msg+="* [Payment Information] Exp month has already expired!<br />";
		}
		if (frm.CCExpYear.value < curr_year){
			msg+="* [Payment Information]  Exp Date Year must be equal or greater than current year!<br />";
		}
		if (frm.CCType.value != "amex" && frm.CICDigit.value.length != 3){
			msg+="* [Payment Information] Card Verification # for Visa or Mastercard must equal 3 digits!<br />";
		}
		if (frm.CCType.value == "amex" && frm.CICDigit.value.length != 4){
			msg+="* [Payment Information] Card Verification # for American Express must equal 4 digits!<br />";
		}
	}

	if (msg!=""){
		document.getElementById("msg").innerHTML=msg;
		return false;
	}
	else {
		document.getElementById("snhAmt").value = document.getElementById("snh").innerHTML;
		document.getElementById("totAmt").value = document.getElementById("tot").innerHTML;
		return true;
	}
}

// Email Validation Routines
function invalidEmail(field){
  str = field.value;
  for(j=0; j<str.length; j++)
	{
      if(str.charAt(j) == "@") 
		{		
		return false;	 
		}	  
	}  
	return true;
 }

 function invalidEmail2(field){
  str = field.value;
  for(j=0; j<str.length; j++)
	{
      if(str.charAt(j) == ".") 
		{
		 return false;	              
		}
	}  
	return true;
 }
  
// ShipTo to BillTo
function validateShipToBillTo(chk){
	if (chk){
		frm.bFirst_Name.value = frm.First_Name.value;
		frm.bLast_Name.value = frm.Last_Name.value;
		frm.bStreet.value = frm.Street.value;
		frm.bCity.value = frm.City.value;
		frm.bState.value = frm.State.value;
		frm.bZIP.value = frm.ZIP.value;
	}
	else
	{
		frm.bFirst_Name.value = "";
		frm.bLast_Name.value = "";
		frm.bStreet.value = "";
		frm.bCity.value = "";
		frm.bState.value = "";
		frm.bZIP.value = "";
	}	
}

// Credit Card Validation
	
function Mod10(ccNumb, frm) {
	var ccType = frm.CCType.value;
	var ccDigit = ccNumb.substring(0,1);
	var valid = "0123456789";
	var len = ccNumb.length;
	var iCCN = parseInt(ccNumb);
	var sCCN = ccNumb.toString();
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');
	var iTotal = 0;
	var bNum = true;
	var bResult = false;
	var temp;
	var calc; 

	for (var j=0; j<len; j++) {
	  temp = "" + sCCN.substring(j, j+1);
	  if (valid.indexOf(temp) == "-1"){bNum = false;}
	}

	if(!bNum){
	  bResult = false;
	}

	if((len == 0)&&(bResult)){
	  bResult = false;
	} else{
	  if(len >= 15){
		for(var i=len;i>0;i--){
		  calc = parseInt(iCCN) % 10;
		  calc = parseInt(calc);
		  iTotal += calc;
		  i--;
		  iCCN = iCCN / 10;
		  calc = parseInt(iCCN) % 10 ;
		  calc = calc *2;
		  switch(calc){
			case 10: calc = 1; break; 
			case 12: calc = 3; break;
			case 14: calc = 5; break;
			case 16: calc = 7; break;
			case 18: calc = 9; break;
			default: calc = calc;
		  }                                               
		iCCN = iCCN / 10;
		iTotal += calc;
	  }
	  if ((iTotal%10)==0){
		bResult = true; 
	  } else {
		bResult = false;
		}
	  }
	}
	if(!bResult){
	  msg+="* [Payment Information] This is NOT a valid Credit Card Number!<br />";
	}
	if(bResult && ccType=="visa" && ccDigit!="4"){
	  msg+="* [Payment Information] This is NOT a valid VISA Card Number!<br />";
	}
	if(bResult && ccType=="mastercard" && ccDigit!="5"){
	  msg+="* [Payment Information] This is NOT a valid MASTERCARD Card Number!<br />";
	}
	if(bResult && ccType=="discover" && ccDigit!="6"){
	  msg+="* [Payment Information] This is NOT a valid DISCOVER Card Number!<br />";
	}
	if(bResult && ccType=="amex" && ccDigit!="3"){
	  msg+="* [Payment Information] This is NOT a valid AMEX Card Number!<br />";
	}
}

// Calculate Order Totals
function calc(s,q){
	var sub=0;
	var qty=0;
	var snh=0.00;
	var tot=0.00;
	var GTot=0.00;

	sub=q*34.95;
	
	qty+=parseFloat(document.getElementById("blue_qty").value);
	qty+=parseFloat(document.getElementById("black_qty").value);
	qty+=parseFloat(document.getElementById("brown_qty").value);
	qty+=parseFloat(document.getElementById("green_qty").value);
	qty+=parseFloat(document.getElementById("cran_qty").value);
	qty+=parseFloat(document.getElementById("lilac_qty").value);
	qty+=parseFloat(document.getElementById("yellow_qty").value);
	
	snh=qty*10.65;
	tot=qty*34.95;
	GTot=snh+tot;

	document.getElementById(""+s+"").innerHTML=sub.toFixed(2);
	document.getElementById("snh").innerHTML=snh.toFixed(2);
	document.getElementById("tot").innerHTML=GTot.toFixed(2);
}

// Claer Fields
function clearFields(){
	document.getElementById("msg").innerHTML="";
	for (i=1;i<=7;i++){
		document.getElementById("sub"+i).innerHTML="0.00";
	}
	document.getElementById("snh").innerHTML="0.00";
	document.getElementById("tot").innerHTML="0.00";
}
