function eCart_copyBillingToShipping(cb)
{
	if(cb.checked)
	{ // Only copy when the checkbox is checked.
		var theForm = cb.form;
		// The number of elements must match in billingFields and shippingFields. The type of input element must also match between the arrays.
		var billingFields = new Array('firstname', 'lastname', 'street1', 'street2', 'city', 'state_province', 'postcode', 'country','coname','phone');
		var shippingFields = new Array('shipping_firstname', 'shipping_lastname', 'shipping_street1', 'shipping_street2', 'shipping_city', 'shipping_state_province', 'shipping_postcode', 'shipping_country','shipping_coname','shipping_phone');
		
		for(var i=0;i<billingFields.length;i++)
		{
			var billingObj = theForm.elements[billingFields[i]];
			var shippingObj = theForm.elements[shippingFields[i]];
			if(billingObj && shippingObj)
			{
				if(billingObj.tagName)
				{ // non-radio groups
					var tagName = billingObj.tagName.toLowerCase();
					if(tagName == 'select')
					{
						var foundValue = false;
						for(var j=0;j<shippingObj.length;j++)
						{
							
							if(shippingObj[j].value.toLowerCase() == billingObj[billingObj.selectedIndex].value.toLowerCase())
							{
								shippingObj.selectedIndex = j;
								foundValue = true;
								break;
							}
						}
						if(!foundValue)
						{
							shippingObj.selectedIndex = billingObj.selectedIndex;
						}
					}
					else if((billingObj.type && shippingObj.type ) && (billingObj.type == 'checkbox' || billingObj.type == 'radio'))
					{
						shippingObj.checked = billingObj.checked;
					}
					else
					{ // textareas and other inputs
						shippingObj.value = billingObj.value;
					}					
				}
				else if(billingObj.length)
				{ // radio group
					for(var r=0;r<billingObj.length;r++)
					{
						shippingObj[r].checked = billingObj[r].checked;
					}
				}
			}
		}
	}
}

function AvailableBalancePopUp()
{
	var theForm = document.ecart_checkout_form;
	var theUrl
	if (theForm.gc_number.value == "")
	{
		alert("You must specify the gift card number.")
		theForm.gc_number.focus()
	}else
	{
		if (isNaN(parseInt(theForm.gc_number.value,10)))
		{
			alert("Gift Card number must be numeric.")
			theForm.gc_number.select()
			theForm.gc_number.focus()
		}else
		{
			theUrl = "GiftCardBalance.cfm?card=" + theForm.gc_number.value
			window.open(theUrl, 'GiftCardBalance','menubar=yes,scrollbars=yes,resizable=yes,width=500,height=450')
		}
	}
}

//
//Check whether a field is empty
//If empty return true, otherwise, return false
//
function FieldEmpty(inputStr)
{
   if (inputStr == null || inputStr.length == 0)
   {
      return true
   }else
   {
      return false
   }
}

//
//Check whether the credit card has expired
//If expired return true, otherwise, return false
//
function ExpiredCard(monthStr, yearStr)
{
   var today = new Date()
   var year = today.getYear()
   var month = today.getMonth() + 1
   var testYear = parseInt(yearStr, 10)
   var testMonth = parseInt(monthStr, 10)

   if (year < 2000)
	  year = 1900 + year
         
   if ((testYear < year) ||
       ((testYear == year) && (testMonth < month)))
   {
      return true
   }

   return false
}

//
//Check if CVV field is numeric and at least 3 digits long
//
function InvalidCVV(inputStr)
{
	var destStr = inputStr;

	if (isNaN(destStr) ||
		destStr.length < 3)
	{
		return true;
	}else
	{
		return false;
	}
}

//
//Check whether the credit card number is valid
//If invalid, return true
//otherwise, return false
//
function InvalidCreditCardNumber(inputStr, cardType)
{
	var destStr = inputStr;

	if (isNaN(destStr))
	{
		return true;
	}
	
      // verify length of number and the prefix
   if (cardType == "VI")
   {  // Visa
      if ((destStr.length != 13) &&
          (destStr.length != 16))
      {
         return true
      }
         // verify prefix
      if (destStr.charAt(0) != "4")
      {
         return true
      }
   }else
   {
      if (cardType == "MC")
      {  // MasterCard
         if (destStr.length != 16)
         {
            return true
         }
            // verify prefix
         if ((destStr.charAt(0) != "5") ||
             (destStr.charAt(1) < "1")  ||
             (destStr.charAt(1) > "5"))
         {
            return true
         }
      }else
      {
         if (cardType == "DIS")
         {  // Discover/Novus
            if (destStr.length != 16)
            {
               return true
            }
               // verify prefix
            if ((destStr.charAt(0) != "6") ||
                (destStr.charAt(1) != "0") ||
                (destStr.charAt(2) != "1") ||
                (destStr.charAt(3) != "1"))
            {
				if ((destStr.charAt(0) != "6") ||
					(destStr.charAt(1) != "5") ||
					(destStr.charAt(2) != "0"))
				{
					return true
				}
            }
         }else
         {
            if (cardType == "AX")
            {  // American Express/Optima
               if (destStr.length != 15)
               {
                  return true
               }
                  // verify prefix
               if ((destStr.charAt(0) != "3")   ||
                   !((destStr.charAt(1) == "4") ||
                     (destStr.charAt(1) == "7")))
               {
                  return true
               }
            }else
            {
               if (cardType == "CBL")
               {  // Carte Blanche
                  if (destStr.length != 14)
                  {
                     return true
                  }
                     // verify prefix
                  if ((destStr.charAt(0) != "9")   ||
                      !((destStr.charAt(1) == "4") ||
                        (destStr.charAt(1) == "5")))
                  {
                     return true
                  }else
                  {
                     if ((destStr.charAt(0) != "3") ||
                         (destStr.charAt(1) != "8") ||
                         (destStr.charAt(2) != "9"))
                     {
                        return true
                     }
                  }
               }else
               {
                  if (cardType == "DIN")
                  {  // Diners Club
                     if (destStr.length != 14)
                     {
                        return true
                     }
                        // verify prefix
                     if ((destStr.charAt(0) != "3")    ||
                         !((destStr.charAt(1) == "0")  ||
                           (destStr.charAt(1) == "6")  ||
                           (destStr.charAt(1) == "8")) ||
                         (destStr.charAt(2) < "1")     ||
                         (destStr.charAt(2) > "8"))
                     {
                        return true
                     }
                  }else
                  {
                     if (cardType == "JCB")
                     {  // JCB
                        if (destStr.length != 16)
                        {
                           return true
                        }
                           // verify prefix
                        if ((destStr.charAt(0) != "3")   ||
                            (destStr.charAt(1) != "5")   ||
                            (destStr.charAt(2) < "2")    ||
                            (destStr.charAt(2) > "8")    ||
                            !((destStr.charAt(3) == "8") ||
                              (destStr.charAt(3) == "9")))
                        {
                           return true
                        }
                     }
                  }
               }
            }
         }
      }
   }
   // card number length is correct AND
   // number prefix is correct
      // verify check digit
   var weight = 2
   var sum = 0
   var accumulator = 0
   for (i=destStr.length-2; i>=0 ; i--)
   {
      if (weight == 2)
      {
         sum = 2 * parseInt(destStr.charAt(i))
         if (sum > 9)
         {
            accumulator++
            accumulator += (sum - 10)
         }else
         {
            accumulator += sum
         }
         weight = 1
      }else
      {
         accumulator += parseInt(destStr.charAt(i))
         weight = 2
      }
   }

      // compare to check digit
   var computedDigit = 10 - (accumulator % 10)
   if (computedDigit == 10)
   {
		computedDigit = 0
   }		
	
   var checkDigit = parseInt(destStr.charAt(destStr.length-1))

   if (computedDigit != checkDigit)
   {
      return true
   }

      // everything checks ok - valid (not invalid) number
   return false
}

//
//Check the credit card info for validity
//Return TRUE if everything is valid (to allow form to be posted)
//Return FALSE if anything is invalid (to prevent posting)
//Alerts will be issued if form does not pass all validity checks
//
function CheckCreditCard(form)
{
	var ccType = form.cc_type.value;
	var ccNumber = form.cc_number.value;
	var ccCVV = form.cvv.value;
	var indexMo = form.exp_month.selectedIndex;
	var ccMo = form.exp_month.options[indexMo].value;
	var indexYr = form.exp_year.selectedIndex;
	var ccYr = form.exp_year.options[indexYr].value;

	if (FieldEmpty(ccType) ||
		FieldEmpty(ccCVV) ||
		FieldEmpty(ccNumber))
	{
		alert("Please fill in all credit card fields.")
		return false
	}

	//validate credit card number
	ccNumber = ccNumber.replace(/\s+/g,'');
	if (InvalidCreditCardNumber(ccNumber, ccType))
	{
		alert("Sorry, the credit card number does not appear to be valid.");
		form.cc_number.select();
		return false;
	}

	ccCVV = ccCVV.replace(/\s+/g,'');
	//validate CVV
	if (InvalidCVV(ccCVV))
	{
		alert("Sorry, the CVV security code number does not appear to be valid.");
		form.cvv.select();
		return false;
	}
	
	//validate expiration date
	if (ExpiredCard(ccMo, ccYr))
	{
		alert("Sorry, the credit card has expired.");
		return false;
	}
	
	//save values in case some internal spaces were deleted
	form.cc_number.value = ccNumber;
	form.cvv.value = ccCVV;
	return true;
}

function CheckoutSubmit()
{
	var thisForm = document.ecart_checkout_form;
	
	if (!FieldEmpty(thisForm.cc_type.value))
	{//credit card selected
		if (CheckCreditCard(thisForm))
		{
			thisForm.task.value="checkout";
			thisForm.submit();
		}else
		{
			return false;
		}
	}else
	{//no credit card - gift card required
		if (FieldEmpty(thisForm.gc_number.value))
		{//no gift card - must use cc
			alert("You must specify either a Gift Card number or a Credit Card as payment.");
			return false;
		}else
		{
			thisForm.task.value="checkout";
			thisForm.submit();
		}
	}
}


