// REVISED 2009-08 to fix VA tax calculation glitch


// The code exists to have promotional codes, but there's no longer a form element to handle it.
// To re-activate, pull up an older version of the store page, re-implement the form, and change this to true.
// This is to prevent people from reading the JavaScript and plugging in codes.
var promosactive = false;

// Weight of each IN OUNCES
DVDweight = 3.6;
VHSweight = 8.1;

var confirmed = false; // Detects whether submitOrder was already run, to save processing time
var discount = 0;
var alreadycheckedpromocode = false;
var currentpromo = "";
currentpromos = new Array(
	promo1 = new Array('33502','2.00','-$2.00','-$2 Promo')
);

if (promosactive) {
	preload = new Image;
	preload.src = "apply-on.gif";
}

function promoCheck(){
	if(!promosactive) return discount;
	// Cancel if we've already checked and applied the code:	
	if(alreadycheckedpromocode) return discount;
	// Cancel if no promo-code field exists:
	if (typeof(document.forms.orderform.promocode)=="undefined") return discount;
	code = document.forms.orderform.promocode;
	
	if(code.value.length==5) {
	
	 for(var i in currentpromos){
		if(code.value==currentpromos[i][0]) {
		 code.value=currentpromos[i][2];
		 currentpromo = currentpromos[i][3];
		 alreadycheckedpromocode=i;
		 discount = currentpromos[i][1];
		 if(!alreadycheckedpromocode) alert('Your promotional code checks out!\nYou are going to get a $'+discount+' discount.\nDiscount will be applied when you Submit Order.');
		 return discount;
		 break;
		};
	 };
	
	if(!alreadycheckedpromocode) {
		alert('This is not a valid promotional code.\nEither you typed it wrong or you are\ntotally hoping to get lucky.');
		discount=0;
		code.value="";
		return "wrongcode";
	 };
	
	} else if(code.value!="") {
	 alert('This is not a valid promotional code.\nEither you typed it wrong or you are\ntotally hoping to get lucky.');
	 discount=0;
	 alreadycheckedpromocode=false;
	 code.value="";
	 return "wrongcode";
	
	} else {
	 alreadycheckedpromocode=false;
	 return 0;
	};
	
}

// Media Mail US prices, per POUND
ShippingArray = new Array(0.00,1.59,2.07,2.55,3.03,3.51,3.99,4.47,4.81,5.15,5.49,5.83,6.17,6.51,6.85,7.19,7.53,7.87,8.21,8.55,8.89,9.23,9.57,9.91,10.25,10.59,10.93,11.27,11.61,11.95,12.29);
// Letter Airmail prices to Canada, per OUNCE
canadaShippingArray = new Array(0.00,0.63,0.90,1.15,1.40,1.70,1.95,2.20,2.50,2.50,2.50,2.50,3.25,3.25,3.25,3.25,3.95,3.95,3.95,3.95,4.65,4.65,4.65,4.65,5.30,5.30,5.30,5.30,6.00,6.00,6.00,6.00,6.70,6.70,6.70,6.70,7.40,7.40,7.40,7.40,8.05,8.05,8.05,8.05,8.75,8.75,8.75,8.75,9.45,9.45,9.45,9.45,10.15,10.15,10.15,10.15,10.90,10.90,10.90,10.90,11.65,11.65,11.65,11.65,12.40,12.40,12.40,12.40)
//canadaShippingArray = new Array(5.95,6.50,6.50,7.00,7.00,7.20,7.20,7.50,7.50,8.00,8.00,8.00,8.00,8.00,8.00,8.00,8.00,8.00,8.00,8.00);

pricearrayDVD = new Array(0.00,14.95,13.95,13.00,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50,12.50);
pricearrayVHS = new Array(0.00,9.95,8.50,7.50,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00,6.00);
taxrate = .045;
myState = 46; //46 is the index number for VA in the select list

function toDollarsAndCents(n) {
  var s = "" + Math.round(n * 100) / 100;
  var i = s.indexOf('.');
  if (i < 0) return s + ".00";
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3);
  if (i + 2 == s.length) t += "0";
  return t;
}

function calculateShipping(DVDs,VHSs,country) {
	var totalounces = (DVDs * DVDweight) + (VHSs * VHSweight);
	if (country == "US")  {
		return ShippingArray[ Math.ceil(totalounces/16) ]; // US goes per pound
	} else {
		return canadaShippingArray[ Math.ceil(totalounces) ]; // Canada goes per ounce
	}
}

// Set values of the shipping/tax/price calculator:
function updateForm() {

	var numberselectedDVD = document.forms.orderform.numbercopiesDVD;
	var numberselectedVHS = document.forms.orderform.numbercopiesVHS;
	var quantityDVD = document.forms.orderform.quantityfieldDVD;
	var quantityVHS = document.forms.orderform.quantityfieldVHS;
	var priceDVD = document.forms.orderform.pricefieldDVD;
	var priceVHS = document.forms.orderform.pricefieldVHS;
	var subtotalDVD = document.forms.orderform.subtotalfieldDVD;
	var subtotalVHS = document.forms.orderform.subtotalfieldVHS;
	var taxfield = document.forms.orderform.taxfield;
	var shipping = document.forms.orderform.shippingfield;
	var total = document.forms.orderform.totalfield;
	
	//if (numberselected.selectedIndex == "0") return;
	if (document.forms.orderform.country.selectedIndex == "0" && document.forms.orderform.state.selectedIndex == "0") return;
	if (document.forms.orderform.country.selectedIndex == "1" && document.forms.orderform.state.selectedIndex != "0") {
	 alert("Last time we checked, Canada didn't have 50 states.");
	 document.forms.orderform.state.selectedIndex = "0";
	 updateForm();
	}
	
	var DVDs = numberselectedDVD.selectedIndex;
	quantityDVD.value = DVDs;
	var priceDVDRaw = pricearrayDVD[DVDs];
	priceDVD.value = toDollarsAndCents(priceDVDRaw);
	var subtotalDVDRaw = quantityDVD.value * priceDVD.value;
	subtotalDVD.value = toDollarsAndCents(subtotalDVDRaw);
	
	var VHSs = numberselectedVHS.selectedIndex;
	quantityVHS.value = VHSs;
	var priceVHSRaw = pricearrayVHS[VHSs];
	priceVHS.value = toDollarsAndCents(priceVHSRaw);
	var subtotalVHSRaw = quantityVHS.value * priceVHS.value;
	subtotalVHS.value = toDollarsAndCents(subtotalVHSRaw);
	
	var taxfieldRaw; 
	if (document.forms.orderform.state.selectedIndex == myState) { taxfieldRaw = (subtotalDVDRaw + subtotalVHSRaw) * taxrate; }
	else { taxfieldRaw = 0.00; }
	taxfield.value = toDollarsAndCents(taxfieldRaw);
	if (document.forms.orderform.country.selectedIndex == "0") {
		var shippingRaw = calculateShipping(DVDs,VHSs,"US");
	}
	else {
		var shippingRaw = calculateShipping(DVDs,VHSs,"Canada");
	}
	shipping.value = toDollarsAndCents(shippingRaw);
	total.value = toDollarsAndCents(subtotalDVDRaw + subtotalVHSRaw + taxfieldRaw + shippingRaw);

}

// This is launched by clicking the button.
function formSubmit() {
	if (formCheck()) {
		confirmed = true;
		document.forms.orderform.submit();
	}
}

function formCheck() {

	// Function has just been run, returned true, and is now being run by the onsubmit event, so skip everything:
	if (confirmed) return true;
	
	// If someone has entered a promo code but hasn't hit the "apply" button, check it now:
	if(!alreadycheckedpromocode) {
	  if(promoCheck()=="wrongcode") return false;
	}
	
	var numberselectedDVD = document.forms.orderform.numbercopiesDVD;
	var numberselectedVHS = document.forms.orderform.numbercopiesVHS;
	
	// Validation checking, and create the item name and item number:
	if ((numberselectedDVD.selectedIndex == "0") && (numberselectedVHS.selectedIndex == "0")) {
		alert('You forgot to pick a quantity. Are you always this careless?')
		return false;
	}	else { 
		// First set the item number:
		document.forms.orderform.item_number.value = "433-d" + document.forms.orderform.numbercopiesDVD.value + "v" + document.forms.orderform.numbercopiesVHS.value;
		// Then the item name:
		document.forms.orderform.item_name.value = "Moving ";
		var copiesDVDText = ( numberselectedDVD.value > 1 ) ? 'copies' : 'copy';
		var copiesVHSText = ( numberselectedVHS.value > 1 ) ? 'copies' : 'copy';
		if ( numberselectedDVD.value > 0) document.forms.orderform.item_name.value += "DVD (" + numberselectedDVD.selectedIndex +" "+ copiesDVDText + ") "
		if ( numberselectedVHS.value > 0) document.forms.orderform.item_name.value += "VHS (" + numberselectedVHS.selectedIndex +" "+ copiesDVDText + ")"
	}
	
	if (document.forms.orderform.country.selectedIndex == "1") {
		document.forms.orderform.item_name.value = document.forms.orderform.item_name.value + " CANADA"
		document.forms.orderform.item_number.value = document.forms.orderform.item_number.value + "-CAN"
	}
	
	if (document.forms.orderform.state.selectedIndex == myState) {
		document.forms.orderform.item_name.value = document.forms.orderform.item_name.value + " VA"
		document.forms.orderform.item_number.value = document.forms.orderform.item_number.value + "-VA"
	}
	else if ((document.forms.orderform.country.selectedIndex == 0) && (document.forms.orderform.state.selectedIndex == 0)) {
		alert('You did not select a state. Put your reading glasses on and try again!')
		return false;
	}
	
	// Update the actual hidden fields with item name and number:
	var newvalue = toDollarsAndCents(document.forms.orderform.totalfield.value - discount)
	document.forms.orderform.item_name.value = document.forms.orderform.item_name.value + currentpromo;
	document.forms.orderform.item_number.value = document.forms.orderform.item_number.value + currentpromo;
	document.forms.orderform.amount.value = newvalue;
	
	// Everything went smoothly:
	return true;
	
}