// JavaScript Document

//Sliding Menu code
var currentOpenNav;

window.addEvent('domready', function() {
	initLeftNav();
	
});
function initLeftNav() {
	for(i=1;i<20;i++) {
	if($('subNav'+i)){
		var leftnavFX = new Fx.Slide('subNav'+i);
		leftnavFX.hide();
	}}
}
function toggleLeftNav(which) {
	//alert($(currentOpenNav));
	if(currentOpenNav != null) {
		var leftnavFX = new Fx.Slide(currentOpenNav);
		leftnavFX.toggle();	
	}
	if (currentOpenNav == which) {
		currentOpenNav=null;
		which=null;
	}else{
		var leftnavFX = new Fx.Slide(which);
		leftnavFX.toggle();
		currentOpenNav = which;
	}
}

//AJAX code
function ajaxAddToBasket(which,whichButton,linkButton){
	//var xmlhttp=null
	var linkItem=document.getElementById('addtobasketLinkText');
	var optionValue="";
	
	if (document.getElementById('shaftFlex')!=null) optionValue = "Shaft Flex: " + document.getElementById('shaftFlex').value;
	if (document.getElementById('size')!=null) optionValue = optionValue + ", Loft: " + document.getElementById('size').value;
	
	var quantity=1;
	//var quantity=document.getElementById('quantity' + whichButton).value;
	linkItem.innerHTML='<strong>adding</strong>...';
	
	//if (optionValue.value>0) {
		linkButton.onclick=function() {
	
		}
		
		// code for Mozilla, etc.
		if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest()
		}
		// code for IE
		else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		if (xmlhttp!=null){
		//xmlhttp.onreadystatechange=ajaxAddToFittingRoomAction linkItem
		xmlhttp.onreadystatechange=function() {
			ajaxAddToBasketAction(linkButton)
			//$("basketQuantity"+whichButton).fade(0);
			//document.getElementById("basketQuantity"+whichButton).style.display="none";
		}
		
		//alert("../../global/ajax/ajax.asp?action=add&id=" + which + "&quantity=" + quantity);
					 //+ "&option=" + optionValue.value + "",true)
		
		xmlhttp.open("GET","../global/ajax/ajax.asp?action=add&id=" + which + "&quantity=" + quantity + "&option=" + optionValue,true)
		xmlhttp.send(null)
		
		}
	//}
	//else {
	//	alert('Please choose a size before adding your item to the basket');
	//	linkItem.innerHTML='add to <strong>basket</strong>';
	//}
}
function ajaxAddToBasketAction(which){
	// if xmlhttp shows "loaded"
	var linkItem=document.getElementById('addtobasketLinkText' + which)
	
	if (xmlhttp.readyState==4){
		// if "OK"
		if (xmlhttp.status==200){
			which.href="index.asp?action=showbasket"
			which.innerHTML="<strong>go to basket!</strong>"
			ajaxUpdateFittingRoomItems();
			alert("Your item has been added to the basket");
			//alert(which.href);
		}else{
			//alert("Problem retrieving XML data:" + xmlhttp.statusText)
		}
	}
}



//AJAX code
function ajaxUpdateFittingRoomItems(){
	//var xmlhttp=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
	xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject){
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp!=null){
	//xmlhttp.onreadystatechange=ajaxAddToFittingRoomAction linkItem
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			// if "OK"
			if (xmlhttp.status==200){
				document.getElementById("labalBasketItems").innerHTML=xmlhttp.responseText;
			}else{
				//alert("Problem retrieving XML data:" + xmlhttp.statusText)
			}
		}
	}
	
	
	
	xmlhttp.open("GET","../global/ajax/ajax.asp?ajaxAction=showbasketitems",true)
	xmlhttp.send(null)
	}
}





function removeFromBasket(id,which) {
	ajaxRemoveFromBasket(id)
	//new Fx.Slide(which).slideOut();
	
	
	//var rightnavFX = new Fx.Slide(which);
	//rightnavFX.slideOut();
	
	var myFx = new Fx.Tween(which);
	myFx.start('height', '0');
	$(which).fade('out');
	myFx.onComplete=function() {
		$(which).setStyle('display','none');
	}
	
	
}


function ajaxRemoveFromBasket(id){
	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp != null){
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4){
				if (xmlhttp.status==200){
					document.getElementById("labalBasketItems").innerHTML=xmlhttp.responseText;
					ajaxUpdateBasketPrice();
					ajaxUpdateBasketDeliveryPrice();
					ajaxUpdatePayPalForm();
				}
			}
		}
	}
	xmlhttp.open("GET","../global/ajax/ajax.asp?ajaxAction=removefrombasket&id=" + id + "",true)
	xmlhttp.send(null)
}
function ajaxUpdateBasketPrice(){
	if (window.XMLHttpRequest){
		xmlhttpBasketPrice=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		xmlhttpBasketPrice=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttpBasketPrice != null){
		xmlhttpBasketPrice.onreadystatechange=function() {
			if (xmlhttpBasketPrice.readyState==4){
				if (xmlhttpBasketPrice.status==200){
					document.getElementById("labelBasketPrice").innerHTML=xmlhttpBasketPrice.responseText;
				}
			}
		}
	}
	xmlhttpBasketPrice.open("GET","../global/ajax/ajax.asp?ajaxAction=updatebasketprice",true)
	xmlhttpBasketPrice.send(null)
}
function ajaxUpdateBasketDeliveryPrice(){
	if (window.XMLHttpRequest){
		xmlhttpDelPrice=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		xmlhttpDelPrice=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttpDelPrice != null){
		xmlhttpDelPrice.onreadystatechange=function() {
			if (xmlhttpDelPrice.readyState==4){
				if (xmlhttpDelPrice.status==200){
					document.getElementById("labelBasketDeliveryPrice").innerHTML=xmlhttpDelPrice.responseText;
				}
			}
		}
	}
	xmlhttpDelPrice.open("GET","../global/ajax/ajax.asp?ajaxAction=updatebasketdeliveryprice",true)
	xmlhttpDelPrice.send(null)
}
function ajaxUpdatePayPalForm(){
	if (window.XMLHttpRequest){
		xmlhttpPaypal=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		xmlhttpPaypal=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttpPaypal != null){
		xmlhttpPaypal.onreadystatechange=function() {
			if (xmlhttpPaypal.readyState==4){
				if (xmlhttpPaypal.status==200){
					document.getElementById("holderPaypalForm").innerHTML=""
					document.getElementById("holderPaypalForm").innerHTML=xmlhttpPaypal.responseText;
				}
			}
		}
	}
	xmlhttpPaypal.open("GET","../global/ajax/ajax.asp?ajaxAction=updatepaypalform",true)
	xmlhttpPaypal.send(null)
}



function ajaxUpdateDeliveryPaypal(newValue,totalPrice) {
	
	//alert(newValue);
	
	var newTotalPrice = Math.abs(newValue) + Math.abs(totalPrice);
	document.getElementById('updatedShipping').value = newValue;
	document.getElementById('labelDeliveryCost').innerHTML = newValue;
	//$("labelBasketPrice").innerHTML = "&pound;" + newTotalPrice.toFixed(2);
	//$("worldpayTotalAmount").value = newTotalPrice.toFixed(2);
	
}

/*
function ajaxUpdateWorldPay(newValue) {
	if (window.XMLHttpRequest){
		xmlhttpWorldpay=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		xmlhttpWorldpay=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttpWorldpay != null){
		xmlhttpWorldpay.onreadystatechange=function() {
			if (xmlhttpWorldpay.readyState==4){
				if (xmlhttpPaypal.status==200){
					document.getElementById("MC_shipping").value=newValue;
				}
			}
		}
	}
}
*/




