// JavaScript Document


//this gets the nice name of the SWF so javascript can target the basket items function in the flash
function getSWF(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
      if(document[movieName].length != undefined){
          return document[movieName][1];
      }
        return document[movieName];
    }
}




//AJAX code
function ajaxAddToBasket(which,whichButton,linkButton,extras){
	//var xmlhttp=null
	var linkItem=document.getElementById('addtobasketLinkText' + whichButton)
	var quantity=document.getElementById('quantity' + whichButton).value;
	linkItem.innerHTML='<strong>adding</strong> to basket...';
	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,extras)
		$("basketQuantity"+whichButton).fade(0);
		//document.getElementById("basketQuantity"+whichButton).style.display="none";
	}
	
	
	xmlhttp.open("GET","global/ajax/ajax.asp?action=add&id=" + which + "&quantity=" + quantity + "&extras=" + extras + "",true)
	xmlhttp.send(null)
	}
}
function ajaxAddToBasketAction(which,extras){
	// if xmlhttp shows "loaded"
	var linkItem=document.getElementById('addtobasketLinkText' + which);
	//updateFlashBasket();
	
	if (xmlhttp.readyState==4){
		// if "OK"
		if (xmlhttp.status==200){
			if(extras=='trade') {
				which.href="trade.asp?action=showbasket"
			}else{
				which.href="eshop.asp?action=showbasket"
			}
			which.innerHTML="<strong>added!</strong> go to basket</a>"
			ajaxUpdateFittingRoomItems(extras)
			//alert(which.href);
		}else{
			//alert("Problem retrieving XML data:" + xmlhttp.statusText)
		}
	}
}



//AJAX code
function ajaxUpdateFittingRoomItems(extras){
	//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;
				if(extras=='trade') {
					//document.getElementById("labalBasketItems").innerHTML=xmlhttp.responseText;
					updateFlashBasket();
				}else{
					swfName=getSWF('swfTopNavigation');
					swfName.updateBasketItems(xmlhttp.responseText);
				}
			}else{
				//alert("Problem retrieving XML data:" + xmlhttp.statusText)
			}
		}
	}
	
	
	if(extras=='trade') {
		xmlhttp.open("GET","global/ajax/ajax.asp?ajaxAction=showbasketitemstrade",true)
		xmlhttp.send(null)
	}else{
		xmlhttp.open("GET","global/ajax/ajax.asp?ajaxAction=showbasketitems",true)
		xmlhttp.send(null)
	}
	}
}





function removeFromBasket(id,which) {
	ajaxRemoveFromBasket(id)
	var rightnavFX = new Fx.Slide(which);
	rightnavFX.slideOut('vertical');

	//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;
					updateFlashBasket();
					
					ajaxUpdateBasketPrice();
					ajaxUpdateBasketDeliveryPrice();
					ajaxUpdateTotalPrice();
					ajaxUpdateFittingRoomItems();
					//ajaxUpdateBasketDeliveryPrice();
					//ajaxUpdatePayPalForm();
					//swfName=getSWF('swfTopNavigation');
					//swfName.updateBasketItems(xmlhttp.responseText);
				}
			}
		}
	}
	xmlhttp.open("GET","global/ajax/ajax.asp?ajaxAction=removefrombasket&id=" + id + "&extras=trade",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("labelBasketSubtotal").innerHTML=xmlhttpBasketPrice.responseText;
				}
			}
		}
	}
	xmlhttpBasketPrice.open("GET","global/ajax/ajax.asp?ajaxAction=updatebasketprice",true)
	xmlhttpBasketPrice.send(null)
}




function ajaxUpdateTotalPrice(){
	if (window.XMLHttpRequest){
		xmlhttpTotalPrice=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		xmlhttpTotalPrice=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttpTotalPrice != null){
		xmlhttpTotalPrice.onreadystatechange=function() {
			if (xmlhttpTotalPrice.readyState==4){
				if (xmlhttpTotalPrice.status==200){
					document.getElementById("labelBasketPrice").innerHTML=xmlhttpTotalPrice.responseText;
				}
			}
		}
	}
	xmlhttpTotalPrice.open("GET","global/ajax/ajax.asp?ajaxAction=updatetotalprice",true)
	xmlhttpTotalPrice.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("labelDeliveryPrice").innerHTML="&pound;" + 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 ajaxUpdateQuantity(which,value) {
	if (window.XMLHttpRequest){
		xmlhttpQuantity=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		xmlhttpQuantity=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttpQuantity != null){
		xmlhttpQuantity.onreadystatechange=function() {
			if (xmlhttpQuantity.readyState==4){
				if (xmlhttpQuantity.status==200){
					ajaxUpdateBasketPrice();
					ajaxUpdateBasketDeliveryPrice();
					ajaxUpdateTotalPrice();
					ajaxUpdateFittingRoomItems();
					document.getElementById("labelBasketPriceStatus").innerHTML="";
				}
			}
		}
	}
	document.getElementById("labelBasketPriceStatus").innerHTML="updating price...";
	var ajaxURL="global/ajax/ajax.asp?ajaxAction=updatequantity&id=" + which + "&value=" + value + "";
	xmlhttpQuantity.open("GET",ajaxURL,true)
	xmlhttpQuantity.send(null)
}
