function ajaxHttpRequest(){
	if(navigator.appName == "Microsoft Internet Explorer") 
		http = new ActiveXObject("Microsoft.XMLHTTP");
	else 
		http = new XMLHttpRequest();
}

function addShoppingItem(productID, qty){
	ajaxHttpRequest();
	var params='/shop.php?action=add&productID='+productID+'&qty='+qty;
	http.open("GET", params);
	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			var add=document.getElementById('address')
			if(!add)
				document.getElementById('shoppingButton'+productID).src="/images/shopping_green.jpg";
				addSideShop(productID);
		}
	}
	http.send(null);
}

function addSideShop(productID){
	if(!document.getElementById('sideShop')){
		document.getElementById('side').innerHTML='<table id="sideShop" style="background:#f5f5f5;border:5px solid #2653a4;"><tr><td><a href="/shop.php" class="topMenu"><b>SHOPPING CART</b></a></td></tr><tr><td rowspan="2">Total:<br><a href="/shop.php"><img src="/images/checkout_down.jpg" border="0"></a></td><td id="total" style="border-top:2px solid" valign="top"></td></tr></table>';
	}
	if(navigator.appName == "Microsoft Internet Explorer") 
		http2 = new ActiveXObject("Microsoft.XMLHTTP");
	else 
		http2 = new XMLHttpRequest();
	var params='/shop.php?sideShop=1&productID='+productID;
	http2.open("GET", params);
	http2.onreadystatechange=function() {
		if(http2.readyState == 4) {
			var temp=http2.responseText
			var temp=temp.split(",,")
			var tbl=document.getElementById('sideShop');
			var len=tbl.rows.length;
			var row = tbl.insertRow(len-1);
			var cellLeft = row.insertCell(0);
			cellLeft.innerHTML=temp[1];
			var cellRight = row.insertCell(1);
			cellRight.innerHTML=temp[0]+'<input type="hidden" value="'+temp[0]+'" id="price'+(len-1)+'">';
			var total=0;
			for(var i=1;i<len;i++)
				total=Number(total)+Number(document.getElementById('price'+i).value)
			document.getElementById('total').innerHTML='$'+total;
		}
	}
	http2.send(null);
}

function payItem(){		//unsecure
	ajaxHttpRequest();
	var params='/shop.php?action=pay';
	http.open("GET", params);
	http.onreadystatechange=function() {
		if(http.readyState == 4) 
			alert(http.responseText);
	}
	http.send(null);
}

function updateShopTotal(productID){
	var qty=document.getElementById('qty'+productID).value;
	var single=document.getElementById('single'+productID).value
	var upAmount=document.getElementById('amount'+productID).value
	var upPrice=document.getElementById('price'+productID).value
	if(parseInt(qty)>=parseInt(upAmount) && parseInt(upAmount)>1){
		document.getElementById('displayRow'+productID).cells[2].innerHTML='<s style="color:red">$'+single+'</s><br>$'+upPrice+'<input type="hidden" id="single'+productID+'" value="'+single+'">';
		single=upPrice
	}else{
		document.getElementById('displayRow'+productID).cells[2].innerHTML='$'+single+'<input type="hidden" id="single'+productID+'" value="'+single+'">';
	}
	var sub=(parseFloat(qty))*(parseFloat(single.replace(',','')));
	document.getElementById('displaySub'+productID).innerHTML='$'+sub
	document.getElementById('itemSub'+productID).value=sub;
	
	var tbl=document.getElementById('shoppingTable');
	var total=0;
	for(var i=1;i<tbl.rows.length-2;i++){
		var temp = tbl.rows[i].cells[5];
		var sub=Number(tbl.rows[i].cells[5].firstChild.value)
		if(sub>0)
			total=Number(total)+Number(sub);
	}
	document.getElementById('total').innerHTML='$'+total
}

function changeQty(productID){
	var qty=document.getElementById('qty'+productID).value;
	addShoppingItem(productID, qty);
	updateShopTotal(productID);
}

function paymentOption(){
	if(document.getElementById('deposit').checked==true)
		document.getElementById('depositDetails').style.display='block';
	else
		document.getElementById('depositDetails').style.display='none';
}

function removeItem(productID){
	document.getElementById('itemSub'+productID).value='';
	ajaxHttpRequest();
	
	var params='/shop.php?action=remove&productID='+productID
	http.open("GET", params);
	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			document.getElementById('itemSub'+productID).value='';
			var tbl=document.getElementById('shoppingTable');
			var total=0;
			for(var i=1;i<tbl.rows.length-2;i++){
				var temp = tbl.rows[i].cells[5];
				var sub=Number(tbl.rows[i].cells[5].firstChild.value)
				if(sub>0)
					total=Number(total)+Number(sub);
			}
			document.getElementById('total').innerHTML='$'+total
			document.getElementById('displayRow'+productID).style.display='none';
		}
	}
	http.send(null);
}

function checkForm(check){
	if(check==5){
		if(document.getElementById('form'+check).value.length==4 && Number(document.getElementById('form'+check).value)!='NaN')
			document.getElementById('check'+check).innerHTML='<img src="/images/green_tick.gif">';
		else
			document.getElementById('check'+check).innerHTML='<img src="/images/red_cross.png">';
		ajaxHttpRequest();
		var params='/shop.php?action=freight&postcode='+document.getElementById('form'+check).value;
		http.open("GET", params);
		http.onreadystatechange=function() {
			if(http.readyState == 4) {
				//alert(http.responseText);
				document.getElementById('freight').value=Math.round(http.responseText);
				document.getElementById('freightCost').innerHTML='$'+Math.round(http.responseText);
				var total=Number(Math.round(http.responseText))+Number(document.getElementById('subtotal').value);
				document.getElementById('finalTotal').innerHTML='$'+total;
				document.getElementById('totalHidden').value=''+total;
			}
		}
		http.send(null);
	}else{
		if(document.getElementById('form'+check).value.length>1)
			document.getElementById('check'+check).innerHTML='<img src="/images/green_tick.gif">';
		else
			document.getElementById('check'+check).innerHTML='<img src="/images/red_cross.png">';
	}
}