///////////
//GLOBALS//
///////////

// COSTS only, nothing to do with P+P prices
iCostPerBundle=243.00;
iCostPerBook=30.00;
iCostPerCareerBundle=80.00;

function init() {
	
	// place £0.00 in all avalible cost cells
	arrCosts=["costcomplete","costamount1","costamount2","costamount3","costamount4","costamount5","costamount6","costamount7","costamount8","costamount9","costcareer"];
	for (val in arrCosts) {
		$(arrCosts[val]).innerHTML="&pound;0.00";
	}
	$("pandp").innerHTML="&pound;0.00";
	$("subtotal").innerHTML="&pound;0.00";
	$("subtotalWidthDiscount").innerHTML="&pound;0.00";
	$("total").innerHTML="&pound;0.00";
	
	// assign keyup events to complete bundle
	var ref=$("amountcomplete");
	ref.onkeyup=function() {
		// keyup has been pressed on a book amount - work out cost
		
		// force an integer
		this.value=intOnly(this.value);
		
		$("costcomplete").innerHTML="&pound;" + currency(this.value * iCostPerBundle);
		updateTotal();
	}
	 
	// assign keyup events to books 1-9
	for (var i=1;i<=9;i++) {
		var ref=$("amount"+i);
		// what number of book is this? (1-9)
		ref.actualNumber=i;
		ref.onkeyup=function() {
			// keyup has been pressed on a book amount - work out cost
			
			// force an integer
			this.value=intOnly(this.value);
		
			var iCost=this.value * iCostPerBook;
			$("costamount"+this.actualNumber).innerHTML="&pound;" + currency(iCost);
			
			updateTotal();
		}
	 }
	 
	// assign keyup events to the career bundle
	var ref=$("amountcareer");
	ref.onkeyup=function() {
		
		// force an integer
		this.value=intOnly(this.value);
		
		// keyup has been pressed on a book amount - work out cost
		$("costcareer").innerHTML="&pound;" + currency(this.value * iCostPerCareerBundle);
		updateTotal();
	}	 
	
	// assign keyup events to the promotional code
	var ref=$("guide");
	ref.onkeyup=function() {	
		if (hex_md5(this.value)=="eb9cc98ec57fffec2a3dea0c80d2b8da") {
			// correct
			$('newSubTotalRow').style.display="";
			$('newSubTotalRow').discount=0.83;
		} else if (hex_md5(this.value)=="63e9a35f3e0d425e011aff6028bd4047") {;
			$('newSubTotalRow').style.display="";
			$('newSubTotalRow').discount=0.85;			
		} else {
			$('newSubTotalRow').style.display="none";				
		}
		updateTotal();
	}	  
	// switch off promo code extra discount row by default
	$('newSubTotalRow').style.display="none";
	
	
	function updateTotal() {
		// iterate through all avalible cost cells
		var iTotal=0;
			for (val in arrCosts) {
				// get displayed cost
				var cost=$(arrCosts[val]).innerHTML;
				// ditch pound sign and work out float value
				if (cost.length>1) {
					iTotal+=parseFloat(cost.substr(1));
				}

			}
		
		// populate subtotal, pandp and grand total
		$("subtotal").innerHTML="&pound;" + currency(iTotal);
		
		// do we populate the discounted total?
		if ($('newSubTotalRow').style.display=="") {
			var iTotal=iTotal * $('newSubTotalRow').discount;
			$("subtotalWidthDiscount").innerHTML="&pound; "+ currency(iTotal);
		}
		updatePP();
		updateGrandTotal();
	}
	
	function updatePP() {
		// update postage and packing
		var iTotal=0;
		
		// first, work out how many complete sets we have and add the p+p
		
		if ($("amountcomplete").value.length>0) {
			iTotal+=parseInt($("amountcomplete").value)*15.00;
		}
		
		// now, work out how many 'three package' bundle buys we have
		if ($("amountcareer").value.length>0) {
			iTotal+=parseInt($("amountcareer").value)*4.00;
		}
		
		// ok, every three books costs 4.00
		// first, work out how many singular books we have
		var iSingularBooks=0;
		for (var i=1;i<=9;i++) {
			ref=$("amount"+i).value;
			if (ref.length>0) {
				iSingularBooks+=parseInt($("amount"+i).value);
			}
		}
		if (iSingularBooks>0) {
			// work out how many 'packages' that is
			var iPackages=Math.ceil(iSingularBooks/3);
			iTotal+=iPackages*4.00;
		}
		$("pandp").innerHTML="&pound;" + currency(iTotal);		
	
	}
	
	function updateGrandTotal() {
		if ($('newSubTotalRow').style.display=="") {
			var subTot=$("subtotalWidthDiscount").innerHTML	
		} else {
			var subTot=$("subtotal").innerHTML	
		}
	
		var pandp=$("pandp").innerHTML
		var subTot=parseFloat(subTot.substr(1));
		var pandp=parseFloat(pandp.substr(1));
		var grandtotal=subTot+pandp;
		
		$("total").innerHTML="&pound;" + currency(grandtotal);	
	}
}

function currency(iWhat) {
	// formats curerency to two decimal places
	return(iWhat.toFixed(2))
}

function intOnly(i) {
	i = i.replace(/[^\d]+/g, '');
	if (i=="0") {
		return "";
	} else {
		return i;
	}	
}

function validateForm(ref) {
	var strError="";
		if (ref.name.value.length==0) {
			strError+="- Name\n";	
		}
		if (ref.org.value.length==0) {
			strError+="- Organisation\n";	
		}
		if (ref.add.value.length==0) {
			strError+="- Address\n";	
		}
		if (ref.town.value.length==0) {
			strError+="- Town\n";	
		}
		if (ref.county.value.length==0) {
			strError+="- County\n";	
		}
		if (ref.postcode.value.length==0) {
			strError+="- Postcode\n";	
		}
		if (ref.email.value.length==0) {
			strError+="- eMail\n";	
		}	
 
   	// work out if theres been a book selected or not
	var tot=$("total").innerHTML
	tot=parseFloat(tot.substr(1));
   	   if (tot==0) {
			   strError+="\n\nPlease select the books you wish to order.";
	   }
	
	if (strError.length>0) {
		var strFinalAlert="- Please supply the following information:\n\n" + strError;
		alert(strFinalAlert);
		return false;
	} else {
		return true;	
	}
	
}

function $(id) {
	return (document.getElementById(id));	
}

window.onload=init;
