//fades layer in
ie5 = (document.all && document.getElementById);
ns6 = (!document.all && document.getElementById);
opac = 0;
function fadein(divid) {
if(opac!= 100){
opac+=1;
if(ie5) document.getElementById(divid).filters.alpha.opacity = opac;
if(ns6) document.getElementById(divid).style.MozOpacity = opac/100;
setTimeout('fadein("'+divid+'")', 10);
}else {
	opac = 0;
	return false;
}
}


function checkformlogin(theform)
{

	theControl="vchUserName,vchPassword";
	theMessage="Username,Password";
	theNumeric=",";
	theEmail="Y,";
	theURL=",";
	theConfirmPassword=",";
	thebadstuff=",";
	theinteger=",";
	thepercent=",";
	thesize=",";
	thedecimal=",";
	theimage=",";
	theempty="Y,Y";

	themin=",";
	themax=",";
	thecheckboxlength=",";
	thephoneno=",";

	if(!theValidator(theform,theControl,theMessage,theNumeric,theEmail,theURL,theConfirmPassword,thebadstuff,theinteger,thepercent,thesize,thedecimal,theimage,theempty,themin,themax,thecheckboxlength,thephoneno))
	  return false;
}


function checkCardExpiry()
{
	var result = true;
 	iMonth=document.frmCard.ExpMon.options[document.frmCard.ExpMon.selectedIndex].value;
	
	iYear=document.frmCard.ExpYear.options[document.frmCard.ExpYear.selectedIndex].value;

	iMonth = parseInt(iMonth)
	iYear = parseInt(iYear)
	
	var now = new Date();
	
	var nowMonth = now.getMonth() + 1;
	var nowYear = now.getFullYear();
	
	expired = (nowYear > iYear) || ((nowYear == iYear ) && (nowMonth > iMonth));
	if (expired)
	{
		result = false;
		alert("The date for Credit Card has expired.");
		document.frmCard.ExpMon.focus();
	}
	return result;
	
}

function isValidCreditCardNumber(formField,ccType,fieldLabel,required)
{
	var result = true;
 	var ccNum = formField.value;

	if (result && (formField.value.length>0))
 	{ 
 		if (result)
 		{  			
 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				alert('Please enter a valid card number for the "' + fieldLabel +'" field.');
				formField.focus();
				result = false;
			}	
		} 
	} 	
	return result;
}

function LuhnCheck(str) 
{
  var result = true;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;
    
  return result;
}

function validateCCNum(cardType,cardNum)
{
	var result = false;
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);
	
//enRouteCard JCBCard
	switch (cardType)
	{
		case "VisaCard":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AmExCard":
			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 "DiscoverCard":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DinersClubCard":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

function isValidccCardCode(formField,fieldLabel)
{
	var result = true;
 	var ccCardCode = formField.value;

  	if (result)
 	{ 
		if (formField.value.length<3)
		{
 			alert('Please enter 3 digit number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	} 
	return result;
}

function validCCForm(ccTypeField,ccNumField)
{	
	theform=document.frmCard;
	
	var result = isValidCreditCardNumber(ccNumField,ccTypeField.value,"Credit Card Number") && checkCardExpiry() && isValidccCardCode(theform.vchCCSecurityCode,"ID Verfication");
	return result
}
function checkccform(theform)
{

	theControl="CardType,CardNumber,vchCCSecurityCode,ExpMon,ExpYear";
	theMessage="card type,card number,security code,expiry month,expiry year";
	theNumeric=",,,,";
	theEmail=",,,,";
	theURL=",,,,";
	theConfirmPassword=",,,,";
	thebadstuff=",,,,";
	theinteger=",Y,Y,,";
	thepercent=",,,,";
	thesize=",,,,";
	thedecimal=",,,,";
	theimage=",,,,";
	theempty="Y,Y,Y,Y,Y";
	themin=",,,,";
	themax=",,,,";
	thecheckboxlength=",,,,";
	thephoneno=",,,,";
	
	if(!theValidator(theform,theControl,theMessage,theNumeric,theEmail,theURL,theConfirmPassword,thebadstuff,theinteger,thepercent,thesize,thedecimal,theimage,theempty,themin,themax,thecheckboxlength,thephoneno))
	  return false;
	
	if(!validCCForm(theform.CardType,theform.CardNumber))
		return false;
}

function checksubscribe(theform)
{
	theControl="vchSubscribeEmail";
	theMessage="Your email";
	theNumeric=",,,,";
	theEmail="Y,,,,";
	theURL=",,,,";
	theConfirmPassword=",,,,";
	thebadstuff=",,,,";
	theinteger=",Y,,,";
	thepercent=",,,,";
	thesize=",,,,";
	thedecimal=",,,,";
	theimage=",,,,";
	theempty="Y,,,,";
	themin=",,,,";
	themax=",,,,";
	thecheckboxlength=",,,,";
	thephoneno=",,,,";
	
	if(!theValidator(theform,theControl,theMessage,theNumeric,theEmail,theURL,theConfirmPassword,thebadstuff,theinteger,thepercent,thesize,thedecimal,theimage,theempty,themin,themax,thecheckboxlength,thephoneno))
	  return false;
}

function fillshipping()
{
	
	document.form2.vchCustShippingFName.value=document.form2.vchCustBillingFName.value;

	document.form2.vchCustShippingMName.value=document.form2.vchCustBillingMName.value;
	document.form2.vchCustShippingLName.value=document.form2.vchCustBillingLName.value;
	document.form2.vchCustShippingAddress1.value=document.form2.vchCustBillingAddress1.value;
	document.form2.vchCustShippingAddress2.value=document.form2.vchCustBillingAddress2.value;
	document.form2.vchCustShippingCity.value=document.form2.vchCustBillingCity.value;
	document.form2.vchCustShippingState.value=document.form2.vchCustBillingState.value;
	document.form2.vchCustShippingZip.value=document.form2.vchCustBillingZip.value;
	document.form2.vchCustShippingPhone.value=document.form2.vchCustBillingPhone.value;
	document.form2.vchCustShippingFax.value=document.form2.vchCustBillingFax.value;
	document.form2.vchCustShippingEmail.value=document.form2.vchCustBillingEmail.value;
	document.form2.vchSCompany.value=document.form2.vchBCompany.value;
	document.form2.vchCustShippingExt.value=document.form2.vchCustBillingExt.value;

	return false;	
}
