
//check the code against the database using an ajax call
/**

*/
function checkDiscount(){
	var code = document.getElementById("discountBox");

	new Ajax.Request("../dynamic/codes.php",
					{
					method: 'post',
					postBody: 'code='+ code.value,
					onComplete: showResponse
					});



}

function showResponse(req){
	var euro, dollar;
	var costEuro = document.getElementById("euroTotal");
	var costDollar = document.getElementById("dollarTotal");
	var code = document.getElementById("discountBox");
	var discountAmount = req.responseText;
	var euroShipping = 6;
	var dollarShipping = 10;


	//alert("hello");
	if(discountAmount>0){



		//	get the inividual item costs and reduce them


		var i=1;
		try{
			do{
				var localVal = document.getElementById("amount_"+i).value;
				localVal -= (localVal * discountAmount)/100;
				document.getElementById("amount_"+i).value = localVal;
				i++;
			}while(document.getElementById("amount_"+i).value!=null)
		}catch(err){

		}
		//alert(i);

		if(i>2){
			euroShipping=9;
			dollarShipping = 15;
		}

		//document.getElementById("amount_"+i).value=euroShipping;

		euro = costEuro.value - (((costEuro.value-euroShipping) * discountAmount)/100);
		dollar = costDollar.value - (((costDollar.value -dollarShipping)* discountAmount)/100);
		costEuro.value = euro.toFixed(2);
		costDollar.value = dollar.toFixed(2);

		document.getElementById("custom").value = code.value;
		document.getElementById("discountButton").disabled = true;
		document.getElementById("discountBox").disabled = true;



	}else{
		alert("invalid code");
	}


}