var freeShipping = true;
var fullSetCost = 99.95;

var integer = /^\d+$/;

$(document).ready(function() { calculate() });
									 
function isInteger(value) {
  return (parseInt(value) == value);
}

function WithoutContent(ss) {
    if(ss.length > 0) { return false; }

    return true;
}

function WithoutCheck(ss) {
    if(ss.checked) { return false; }

    return true;
}

function checkSubmit(){
  if (!calculate()) return false;

  if (!validateForm()) return false;

  // get product string
  var prodstr="";
  if (document.form.fullsets.value > 0) {
    prodstr = prodstr + document.form.fullsets.value + " Full Set";
	if (document.form.fullsets.value > 1) prodstr = prodstr + "s";
  }
  if (document.form.cuffs.value > 0) {
    if (prodstr.length > 0) prodstr = prodstr + "; ";
    prodstr = prodstr + document.form.cuffs.value + " Add'l Cuff Set";
	if (document.form.cuffs.value > 1) prodstr = prodstr + "s";
  }
  if (document.form.chains.value > 0) {
    if (prodstr.length > 0) prodstr = prodstr + "; ";
    prodstr = prodstr + document.form.chains.value + " Add'l Chain Set";
	if (document.form.chains.value > 1) prodstr = prodstr + "s";
  }

  // move the data from the input form to the Paypal form
	
  document.PPForm.item_name.value="EZ-Lats plus: "+prodstr;
  /* document.PPForm.item_name.value=document.form.cuffs.value+" Cuff(s) and "+document.form.chains.value+" Chain Sets"; */
  document.PPForm.amount.value=FormatDollar(document.SumForm.MerchandiseTotal.value*1+
    document.SumForm.StateTax.value*1);
  //    document.form.ShippingCost.value*1 +
  // document.PPForm.tax.value=FormatDollar(document.form.StateTax.value*1);
  document.PPForm.shipping.value=FormatDollar(document.SumForm.ShippingCost.value*1);
  document.PPForm.os0.value=document.form.company.value+" / "+document.form.telephone.value+" / "+document.form.email.value+"";
  document.PPForm.os1.value="Retun Policy Accepted"+
    (!WithoutCheck(document.form.WebSurfing) ? " / Websurfing" : "") +
    (!WithoutCheck(document.form.Advertisement) ? " / Advertisement: " + document.form.Magazine.value : "") +
    (!WithoutCheck(document.form.TradeShow) ? " / TradeShow" : "") +
    (!WithoutCheck(document.form.WordOfMouth) ? " / WordOfMouth" : "")
  document.PPForm.first_name.value=document.form.fname.value + "";
  document.PPForm.last_name.value=document.form.lname.value + "";
  document.PPForm.address1.value=document.form.street.value + "";
  document.PPForm.city.value=document.form.city.value + "";
  document.PPForm.state.value=document.form.state.value + "";
  document.PPForm.zip.value=document.form.zipcode.value + "";
  document.PPForm.email.value=document.form.email.value + "";

  return true;
}

function FormatDollar(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num)) num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10) cents = "0" + cents;

  return(num + "." + cents);
}

function validateForm() { 
   var errormessage = new String();

   if(document.SumForm.TotalCost.value * 1 < 1)
	{ errormessage += "\n\nNothing ordered! Please select an item quantity"; }
   if(WithoutContent(document.form.fname.value))
	{ errormessage += "\n\nFirst Name field must be completed!"; }
   if(WithoutContent(document.form.lname.value))
	{ errormessage += "\n\nLast Name field must be completed!"; }
   if(WithoutContent(document.form.street.value))
	{ errormessage += "\n\nStreet field must be completed!"; }
   if(WithoutContent(document.form.city.value))
	{ errormessage += "\n\nCity field must be completed!"; }
   if(WithoutContent(document.form.state.value))
	{ errormessage += "\n\nState field must be completed!"; }
   if(WithoutContent(document.form.zipcode.value))
	{ errormessage += "\n\nZip Code field must be completed!"; }
   if(WithoutContent(document.form.telephone.value))
	{ errormessage += "\n\nTelephone field must be completed!"; }
   else
        if(document.form.telephone.value.length < 10)
	    { errormessage += "\n\nTelephone number must include area code!"; }
   if(WithoutContent(document.form.email.value))
	{ errormessage += "\n\nE-mail address field must be completed!"; }
   if(WithoutCheck(document.form.ReturnPolicy))
	{ errormessage += "\n\nThe \"Return Policy\" must be agreed to!"; }
   if(WithoutCheck(document.form.WebSurfing) &&
       WithoutCheck(document.form.Advertisement) &&
       WithoutCheck(document.form.TradeShow) &&
       WithoutCheck(document.form.WordOfMouth) )
	{ errormessage += "\n\nAt least one \"heard from\" option must be selected!"; }
   if(document.form.Advertisement.checked && WithoutContent(document.form.Magazine.value))
      { errormessage += "\n\nAdvertisement: please provide name of magazine."; }

   if(errormessage.length > 2) {
	alert('NOTE:' + errormessage);
	return false;
	}

  return true;
}

function validatefullsets() {
  var valid = true;
  var output = '';
  if (!isInteger(document.form.fullsets.value)) {
    valid = false;
    output = 'The number of EZ-Lats plus full sets must be an integer\n';
  }
  if (window.RegExp && !integer.test(document.form.fullsets.value)) {
    valid = false;
    output = 'The number of EZ-Lats plus full sets must be an integer\n';
  }

  if (!valid) {
    alert(output);
    return valid;
  }

  document.SumForm.dfullsets.value=document.form.fullsets.value;
  return valid;
}

function validatecuffs() {
  var valid = true;
  var output = '';
  if (!isInteger(document.form.cuffs.value)) {
    valid = false;
    output = 'The number of additional cuffs must be an integer\n';
  }
  if (window.RegExp && !integer.test(document.form.cuffs.value)) {
    valid = false;
    output = 'The number of additional cuffs must be an integer\n';
  }

  if (!valid) {
    alert(output);
    return valid;
  }

  document.SumForm.dcuffs.value=document.form.cuffs.value;
  return valid;
}

function validatechains() {
  var valid = true;
  var output = '';
  if (!isInteger(document.form.chains.value)) {
    valid = false;
    output = 'The number of additional chains must be an integer\n';
  }
  if (window.RegExp && !integer.test(document.form.chains.value)) {
    valid = false;
    output = 'The number of additional chains must be an integer\n';
  }

  if (!valid) {
    alert(output);
    return valid;
  }

  document.SumForm.dchains.value=document.form.chains.value;
  return valid;
}

function calculate() {
  var sc = 0;	/* temp shipping cost */

  if (validatefullsets()) {
	if (document.form.fullsets.value > 0) {
      $("tr.fullsets").show();
	} else {
      $("tr.fullsets").hide();
	}
  } else {
    $("tr.fullsets").hide();
	$("div.dvd").hide()
	return false;
  }

  if (validatecuffs()) {
	if (document.form.cuffs.value > 0) {
      $("tr.cuffs").show();
	} else {
      $("tr.cuffs").hide();
	}
  } else {
    $("tr.cuffs").hide();
	$("div.dvd").hide()
	return false;
  }

  if (validatechains()) {
	if (document.form.chains.value > 0) {
      $("tr.chains").show();
	} else {
      $("tr.chains").hide();
	}
  } else {
    $("tr.chains").hide();
	$("div.dvd").hide()
    return false;
  }
  
  if (document.form.fullsets.value > 0 || document.form.cuffs.value > 0 || document.form.chains.value > 0) {
    $("div.paypal-submit").show();
  } else {
	$("div.paypal-submit").hide();
  }

  document.SumForm.dfullsets.value=document.form.fullsets.value;
  document.SumForm.dcuffs.value=document.form.cuffs.value;
  document.SumForm.dchains.value=document.form.chains.value;
  document.SumForm.MerchandiseTotal.value=FormatDollar(
    (document.form.fullsets.value*fullSetCost) + (document.form.cuffs.value*90.95) + (document.form.chains.value*9.00));
  document.SumForm.StateTax.value=0;
  if (document.form.state.value == "IL") {
    document.SumForm.StateTax.value=FormatDollar(document.SumForm.MerchandiseTotal.value*0.07);
	$("tr.tax").show();
  } else {
	$("tr.tax").hide();
  }
  if (!freeShipping || (freeShipping && document.SumForm.MerchandiseTotal.value < fullSetCost)) {
    sc = Math.max(document.SumForm.dfullsets.value, document.SumForm.dcuffs.value, document.SumForm.dchains.value) * 9.95;
    while (sc > 9.95 && sc > document.SumForm.MerchandiseTotal.value / 3) { /* ship cost < 1/3 prod value */
      sc = sc - 9.95;
    }
    document.SumForm.ShippingCost.value=FormatDollar(sc);
  } else {
      document.SumForm.ShippingCost.value=FormatDollar(0);
  }
  document.SumForm.TotalCost.value=FormatDollar(
    document.SumForm.MerchandiseTotal.value*1+document.SumForm.ShippingCost.value*1+document.SumForm.StateTax.value*1);
/*
  if (document.form.cuffs.value >= 1 && document.form.chains.value >= 1) {
    $("div.dvd").show();
  } else {
    $("div.dvd").hide();
  }
*/
  return true;
}