
function LTrim(str)
{  var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
       var j=0, i = s.length;
       while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
              j++;
       s = s.substring(j, i);
   }
return s;
}
function RTrim(str)
{  var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
       var i = s.length - 1;       
       while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
              i--;
       s = s.substring(0, i+1);
   }
return s;
}
var TodayDate=new Date();
var TodayMonth=TodayDate.getMonth()+1;
var TodayYear=TodayDate.getFullYear();
var TodayDay=TodayDate.getDate();
//alert(TodayDay);
function Trim(str)  { return RTrim(LTrim(str)); }
function strip(number) {
		var sOut = '';
		mask = '1234567890';
		for(count = 0; count <= number.length; count++) {
 			if(mask.indexOf(number.substring(count, count+1),0) != -1 ) sOut += number.substring(count,count+1);
 		}
		return sOut;
	}

function ValidateGift(theForm) {
	if (Trim(theForm.Denominations.value) == "")
  {
    alert("Please select gift card amount");
    theForm.Denominations.focus();
    return (false);
  }
  if (Trim(theForm.Denominations.value) < 50)
  {
    alert("Minimum of $50.00 gift card required.");
    theForm.Denominations.focus();
    return (false);
  }
  var checkOK = "0123456789.$";
	var checkStr = theForm.Denominations.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
				if (j == checkOK.length) {
					allValid = false;
					break;
				}
	}
	if (!allValid) {
		alert("Please enter only digit in the \"Gift Card Amount\" field.");
		theForm.Denominations.focus();
		return (false);
	}
	if (Trim(theForm.Quantity.value) == "")
  {
    alert("Please select quantity.");
    theForm.Quantity.focus();
    return (false);
  }
  if (Trim(theForm.FirstName.value) == "")
  {
    alert("Please enter your first name.");
    theForm.FirstName.focus();
    return (false);
  }
  if (Trim(theForm.LastName.value) == "")
  {
    alert("Please enter your last name.");
    theForm.LastName.focus();
    return (false);
  }
  if (Trim(theForm.Email.value) == "")
  {
    alert("Please enter your email.");
    theForm.Email.focus();
    return (false);
  }

	var checkStr = theForm.Email.value;
	var ch = checkStr.indexOf('@');

	if (ch==0 || ch==-1) {
		alert("Please enter a valid email address.");
		theForm.Email.focus();
		return (false);
	}
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzfSOZsozYÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-@.-_'";
	var checkStr = theForm.Email.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
				if (j == checkOK.length) {
					allValid = false;
					break;
				}
	}
	if (!allValid) {
		alert("Please enter only letter, digit and \"@.-_'\" characters in the \"Email\" field.");
		theForm.Email.focus();
		return (false);
	}
	if (Trim(theForm.Address.value) == "")
  {
    alert("Please enter your address.");
    theForm.Address.focus();
    return (false);
  }
  if (Trim(theForm.City.value) == "")
  {
    alert("Please enter your city.");
    theForm.City.focus();
    return (false);
  }
   if ((Trim(theForm.State.value) == "")&& ((Trim(theForm.Country.value) == "US") || (Trim(theForm.Country.value) == "Canada")))
  {
    alert("Please enter your state.");
    theForm.State.focus();
    return (false);
  }
 if ((Trim(theForm.Zip.value) == "")&& ((Trim(theForm.Country.value) == "US") || (Trim(theForm.Country.value) == "Canada")))
  {
    alert("* Zip Code is required for US and Canada Residence.");
    theForm.Zip.focus();
    return (false);
  }
  if (Trim(theForm.Country.value) == "")
  {
    alert("Please enter your country.");
    theForm.Country.focus();
    return (false);
  }
  if (Trim(theForm.Phone.value) == "")
  {
    alert("Please enter your phone number.");
    theForm.Phone.focus();
    return (false);
  }
    if (Trim(theForm.Phone.value) != "")
  {
 if((theForm.Phone.value.length != 12) && (theForm.Phone.value.length != 14)){
				alert("Please enter phone number in correct format:  either (xxx) xxx-xxxx OR xxx-xxx-xxxx.");
				theForm.Phone.focus();
				return false;			
			}else{
				if(theForm.Phone.value.length == 12){
					for(x=1;x< theForm.Phone.value.length;x++){
						if(x == 3){
							if(theForm.Phone.value.charAt(x) != "-" && theForm.Phone.value.charAt(x) != " "){
								alert("Please enter phone number in correct format:  either (xxx) xxx-xxxx OR xxx-xxx-xxxx.");
								theForm.Phone.focus();
								return false;
							} 
						}
					}
				}else if(theForm.Phone.value.length == 14){
					for(x=1;x< theForm.Phone.value.length;x++){
						//if(x == 0 || x == 7){
						if(x == 0){
							if(theForm.Phone.value.charAt(x) != "("){
								alert("Please enter phone number in correct format:  either (xxx) xxx-xxxx OR xxx-xxx-xxxx.");
								theForm.Phone.focus();
								return false;
							}
						}
						if(x == 4){
							if(theForm.Phone.value.charAt(x) != ")"){
								alert("Please enter phone number in correct format:  either (xxx) xxx-xxxx OR xxx-xxx-xxxx.");
								theForm.Phone.focus();
								return false;
							}
						}
						if(x == 9){
							if(theForm.Phone.value.charAt(x) != "-" && theForm.Phone.value.charAt(x) != " "){
								alert("Please enter phone number in correct format:  either (xxx) xxx-xxxx OR xxx-xxx-xxxx.");
								theForm.Phone.focus();
								return false;
							}
						}
					}
				}	
			}
		}
  
  var checkOK = "0123456789-() ";
	var checkStr = theForm.Phone.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
				if (j == checkOK.length) {
					allValid = false;
					break;
				}
	}
	if (!allValid) {
		alert("Please enter only digit in the \"Phone\" field.");
		theForm.Phone.focus();
		return (false);
	}
  
  var myOption = -1;
for (i=0; i < theForm.DeliverTo.length; i ++) 
{
	if (theForm.DeliverTo[i].checked) 
	{
		myOption = i; 
		break;
	}
}
  if (myOption == theForm.DeliverTo.length || myOption == -1) 
{
	alert("Please select recipient.");
	theForm.DeliverTo[0].focus();
	return false;
}
if ((theForm.DeliverToN.checked==true)&&(theForm.RFirstName.value==""))
{
   alert("Please enter recipient first name.");
   theForm.RFirstName.focus();
   return (false);
}	

if ((theForm.DeliverToN.checked==true)&&(theForm.RLastName.value==""))
{
   alert("Please enter recipient last name.");
   theForm.RLastName.focus();
   return (false);
}	

if ((theForm.DeliverToN.checked==true)&&(theForm.RAddress.value==""))
{
   alert("Please enter recipient address.");
   theForm.RAddress.focus();
   return (false);
}	

if ((theForm.DeliverToN.checked==true)&&(theForm.RCity.value==""))
{
   alert("Please enter recipient city.");
   theForm.RCity.focus();
   return (false);
}	

if ((theForm.DeliverToN.checked==true)&&(theForm.RState.value==""))
{
   alert("Please enter recipient state.");
   theForm.RState.focus();
   return (false);
}

if ((theForm.DeliverToN.checked==true)&&(theForm.RZip.value==""))
{
   alert("Please enter recipient zip code.");
   theForm.RZip.focus();
   return (false);
}	
if ((theForm.DeliverToN.checked==true)&&(theForm.RCountry.value==""))
{
   alert("Please enter recipient country.");
   theForm.RCountry.focus();
   return (false);
}


 if ((theForm.NameonCard.value == "") )
  {
    alert("Please enter name on card.");
    theForm.NameonCard.focus();
    return (false);
  }
  if ((theForm.NameonCard.value == "") )
  {
    alert("Please enter name on card.");
    theForm.NameonCard.focus();
    return (false);
  }
   if ((theForm.CreditCard.value == "") )
  {
    alert("Please enter credit card type.");
    theForm.CreditCard.focus();
    return (false);
  }
if (theForm.CreditCardNumber.value=="")
  {
    alert("Please enter your credit card number.");
    theForm.CreditCardNumber.focus();
    return (false);
  }
		var checkOK = "0123456789";
		var checkStr = strip(theForm.CreditCardNumber.value) + '';
		var CrValid = true;
		var checksum=0;
		var ddigit=0;
		var kdig = 0;
		
		if (checkStr.length < 13) {
			alert ('You have not entered enough digits. Please check the number for errors.');
					return(false);
		}
		
		for (i = checkStr.length-1;  i >= 0;  i--) {
			kdig++;
			ch = checkStr.charAt(i);
			if ((kdig % 2) != 0) {
				checksum=checksum+parseInt(ch)
			}
			else {
				ddigit=parseInt(ch)*2;
				if (ddigit >= 10)
					checksum=checksum+1+(ddigit-10)
				else
					checksum=checksum+ddigit;
			}
			for (j = 0;  j < checkOK.length;  j++) {
				if (ch == checkOK.charAt(j)) {
					break;
				}
				if (j == checkOK.length) {
					alert('Please enter only digits. No dashes or non-numeric characters.');
					return(false);
				}
			}
		}
  
		if ((checksum % 10) != 0) {
			alert('You have entered an invalid credit card number. Please check the number for errors.');
			theForm.CreditCardNumber.focus();
			return (false);
		}


  if ((theForm.ExpireYear.value < TodayYear ) )
  {
	  
    alert("Please enter a valid expiration year");
    theForm.ExpireYear.focus();
    return (false);
  }
  
  if ((theForm.ExpireYear.value == TodayYear ) && (theForm.ExpireMonth.value < TodayMonth ) )
  {
    alert("Please enter a valid expiration month");
    theForm.ExpireMonth.focus();
    return (false);
  }
// if ((theForm.CID.value == "") )
//  {
//    alert("Please enter CID number.");
//    theForm.CID.focus();
//    return (false);
//  }
//  
//    var checkOK = "0123456789";
//	var checkStr = theForm.CID.value;
//	var allValid = true;
//	for (i = 0;  i < checkStr.length;  i++) {
//		ch = checkStr.charAt(i);
//		for (j = 0;  j < checkOK.length;  j++)
//			if (ch == checkOK.charAt(j))
//				break;
//				if (j == checkOK.length) {
//					allValid = false;
//					break;
//				}
//	}
//	if (!allValid) {
//		alert("Please enter only digit in the \"CID\" field.");
//		theForm.CID.focus();
//		return (false);
//	}
  
 

return (true);
}

