//---------------------------------------------------------------------------------------------------------
function check_clickstate (f,url) {
	   var clickstatus = f.ClickState.value;
	   
	   if (clickstatus==0) {
	       f.ClickState.value=1;
	       document.location=url
	   } else {
	       alert("You have already submitted this purchase.");
	   }
}

//---------------------------------------------------------------------------------------------------------
function is_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
				
		for(i=0; i < e.length ;i++){
			if(ok.indexOf(e.charAt(i))<0){ 
			   return (false);
			   }	
		} 
		
		if (document.images) {
			re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
			re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			if (!e.match(re) && e.match(re_two)) {
			   return (-1);		
			   } 
		}
}

//---------------------------------------------------------------------------------------------------------

function ValidateGiftMessage ( ) {
		
		gmform = document.Form;
        
         if(check_email(gmform.GiftMessageEmail.value)){
		    gmform.GiftMessage.disabled = false;
            gmform.GiftMessage.focus(); 
            return false;
            }
			
         if(!check_email(gmform.GiftMessageEmail.value)){
		    alert("You must specify a valid email address!");
		    gmform.GiftMessage.disabled = true;
			gmform.GiftMessage.value = '';
            gmform.GiftMessageEmail.focus(); 
            return false;
            }
			

}

//---------------------------------------------------------------------------------------------------------
function ValidateCheckoutForm ( cyear, cmonth) {
	theform = document.Form;
	/*
	var GWProducts = theform.GWProductID;
  	var hasGW = false;
	
	myOption = -1;
	for (i=theform.GiftWrap.length-1; i > -1; i--) {
	if (theform.GiftWrap[i].checked) {
		myOption = i; i = -1;
		}
	}
	if (myOption != -1) {
  	for (var x = 0; x <= GWProducts.length-1; x++) {
      if (GWProducts[x].checked) {
	  hasGW = true;
	  }
   	}
	alert(hasGW);
  if (!hasGW) {
      alert("Please select at least one Product to be gift wrapped!");
      return false;
	}
	return true;
  }
	*/
	
	seccode  = theform.SecurityCode.value;
	ccnum    = theform.CreditCardNumber.value;
	cctype   = theform.CardType.selectedIndex;
	hiddenCC = theform.HiddenCreditCardNumber.value;
	CheckForCC = theform.CheckForCC.value;
	GiftCardNumber = theform.GiftCardNumber.value;
	hiddenCCval = hiddenCC;
	
	Points = parseFloat(theform.points.value);
	AvailablePoints = parseFloat(theform.maxpoints.value);
	MaxPoints = parseFloat(theform.maxvalue.value);
	
	if (Points > MaxPoints){
	    alert("You may not enter more points than your current purchase ("+MaxPoints+" points)!");
		theform.points.value = MaxPoints;
		theform.points.focus();
		return false;
	}
	
	if (Points > AvailablePoints){
	    alert("You may not enter more points than you currently have ("+AvailablePoints+" points)!");
		theform.points.value = AvailablePoints;
		theform.points.focus();
		return false;
	}
	
	validateCC = true
	
	if (GiftCardNumber == "") {
	CheckForCC = "1";
		if (Points == MaxPoints){
			CheckForCC = "0";
		}
	}
	else {
	CheckForCC = "0";
	}
	if (CheckForCC == 1) {
			// if the cc
			if (ccnum!="") {
			   if (ccnum==hiddenCCval) { 
			      validateCC = false;
			      }
			}
		
			//-----------------------------------------------------------------------------------------------
			if (cctype==0) {
			    alert("Please select the credit card you would like to use for this purchase.");
				theform.CardType.focus();
				return false; 
				}
					
			cctypeval = theform.CardType.options[cctype].value;
			
			if (cctypeval=="AMERICAN EXPRESS") {
			    if (seccode.length!=4) {
			    alert("Please enter the four digit security code on the back of your American Express card.");
				theform.SecurityCode.focus();
				return false;
				} 
			} else {
			    if (seccode.length!=3) {
			    alert("Please enter the 3 digit security code on the back of your Visa/Mastercard card.");
				theform.SecurityCode.focus();
				return false;
				} 
				}
				
			//-----------------------------------------------------------------------------------------------
			if (validateCC) {
				if (!validateCCNum( cctypeval, ccnum )) {
				    alert("Please enter a valid "+cctypeval+" number.");
					theform.CreditCardNumber.focus();
					return false; }
			    }	
			
			//-----------------------------------------------------------------------------------------------
			ExpireMonth = theform.ExpireMonth.options[theform.ExpireMonth.selectedIndex].value;
			ExpireYear = theform.ExpireYear.options[theform.ExpireYear.selectedIndex].value;
			
			if (ExpireYear<cyear) {
			    if (ExpireMonth<cmonth) {
			    alert("The expiration date you entered for this credit card has already passed.");
				theform.ExpireMonth.focus();
				return true;
				}
			}
		}

	theform.submit();
}
//---------------------------------------------------------------------------------------------------------
function validateCCNum(cardType,cardNum) {
	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMERICAN EXPRESS":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTERCARD":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER CARD":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}
//----------------------------------------------------------------------------------------------------------
function ValidateQuantity(cnt) {
	 qty = cnt.value;
        if (qty!="") { 
	 if (isInteger(qty)==false) {
	     alert("Please enter a valid quantity!")
		 cnt.value='';
		 cnt.focus();
		 return true;
	     }
	 
	 if (qty==0) {
	     alert("Please enter a valid quantity!")
		 cnt.value='';
		 cnt.focus();
		 return true;
	 }
	 }
 }
//-------------------------------------------------------------------------------------------------------------
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;
}

