// JavaScript Document

    var ToBuyxmlHttp;
	function createTobuyXMLHttpRequest() {
		if (window.ActiveXObject) {
			ToBuyxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		else if (window.XMLHttpRequest) {
			ToBuyxmlHttp = new XMLHttpRequest();
		}
	}
	//增加商品
	function BuyPro(spid) {
		
		createTobuyXMLHttpRequest();
		var now= new Date();
		url="/Ajax/AjaxToBuyPro.aspx?sp_id=" + spid +"&Key=" + now.getSeconds();
		ToBuyxmlHttp.onreadystatechange = function() {handletobuyStateChange(spid,"add");}
		ToBuyxmlHttp.open("GET", url, true);
		ToBuyxmlHttp.send(null);
	}
	
	function BuyServerPro(spid,serverid) {
		
		createTobuyXMLHttpRequest();
		var now= new Date();
		url="/Ajax/AjaxToBuyPro.aspx?act=buyserver&sp_id=" + spid +"&ServerID=" + serverid + "&Key=" + now.getSeconds();
		
		ToBuyxmlHttp.onreadystatechange = function() {handletobuyStateChange(spid,"add");}
		ToBuyxmlHttp.open("GET", url, true);
		ToBuyxmlHttp.send(null);
	}
	//删除商品
	function DelPro(spid) {
		
		createTobuyXMLHttpRequest();
		var now= new Date();
		url="/Ajax/AjaxToBuyPro.aspx?sp_id=" + spid +"&act=del&Key=" + now.getSeconds();
		ToBuyxmlHttp.onreadystatechange = function() {handletobuyStateChange(spid,"del");};
		ToBuyxmlHttp.open("GET", url, true);
		ToBuyxmlHttp.send(null);
	}
	
	function DelAllPro(spid){
		createTobuyXMLHttpRequest();
		var now= new Date();
		url="/Ajax/AjaxToBuyPro.aspx?sp_id=" + spid +"&act=delAll&Key=" + now.getSeconds();
		ToBuyxmlHttp.onreadystatechange = function() {handletobuyStateChange(spid,"delAll");};
		ToBuyxmlHttp.open("GET", url, true);
		ToBuyxmlHttp.send(null);
	}
	
		
	function handletobuyStateChange(spid,type) {
		if(ToBuyxmlHttp.readyState == 4) {
			if(ToBuyxmlHttp.status == 200) {
				if (ToBuyxmlHttp.responseText!="")
				{
					window.alert(ToBuyxmlHttp.responseText);
				}
				else
				{
					if (type!="del" && type!="delAll")
					{
						FlyToCart(spid);
					}
					ShowBaskInfo();
				}
			    //window.alert('成功');
			}
			//window.alert(xmlHttp.status);
		}
		else
		{
		
		}
	}
	
	
	var BaskxmlHttp;
	function createBaskXMLHttpRequest() {
		if (window.ActiveXObject) {
			BaskxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		else if (window.XMLHttpRequest) {
			BaskxmlHttp = new XMLHttpRequest();
		}
	}
	
	function ShowBaskInfo()
	{
		createBaskXMLHttpRequest();
		var now= new Date();
		url="/Ajax/ShowBastInfo.aspx?Key=" + now.getSeconds();
		BaskxmlHttp.onreadystatechange = ShowBaskStateChange;
		BaskxmlHttp.open("GET", url, true);
		BaskxmlHttp.send(null);
	}
	
		function ShowBaskStateChange() {
//		 if(BaskxmlHttp.readyState == 1)//ʾڼ
//         {
//             document.getElementById("ShopInfo").innerHTML ="<img style=\"margin-left:80px;\" src=\"images/5-0.gif\" />";
//        }
		if(BaskxmlHttp.readyState == 4) {
			if(BaskxmlHttp.status == 200) {
				//window.alert(xmlHttp.responseText);
				CheckBaskEtSHow(BaskxmlHttp.responseText)
			}
			//window.alert(xmlHttp.status);
		}
		else
		{
		
		}
	}
	
	function CheckBaskEtSHow(str)
	{
		if (str=="NoProduct")
		{
                info =  "<div id=\"shopbasket\">" +"\n"+
                  "                	<ul>" +"\n"+
                  "                    	<li id=\"shopbasket_top\"><a class=\"bailink\" href=\"#\">My Shopping Basket</a></li>" +"\n"+
                  "                        <li id=\"shopbasket_Noitems\"><span style=\"margin-top:10px;\">Your basket is empty</span></li>" +"\n"+
                  //"                         <!--<li id=\"shopbasket_Haveitems\">4 items <span class=\"text2\">43.96</span><br />View basket >><a href=\"#\"><img style=\" margin-left:120px;  margin-top:-40px;\" src=\"images/checkout.gif\" border=\"0\" /></a></li>-->" +"\n"+
                  "<li id=\"RateShow\">Currency: <a class=\"test2Link\" href='/Inc/SetRate.aspx?Rate=USD&url=" + escape(document.URL) + "'>USD</a> <a class=\"test2Link\" href='/Inc/SetRate.aspx?Rate=GBP&url=" + escape(document.URL) + "'>GBP</a> <a class=\"test2Link\" href='/Inc/SetRate.aspx?Rate=EUR&url=" + escape(document.URL) + "'>EUR</a></li>"
                  "                    </ul>" +"\n"+
                  "                </div>"
			   document.getElementById("ShopInfo").innerHTML = info;
		}
		else
		{
			   document.getElementById("ShopInfo").innerHTML = str;
		}
	}
	
	
	var flyingSpeed = 15;
	var shopping_cart_div = false;
	var flyingDiv = false;
	var currentProductDiv = false;
	
	var shopping_cart_x = false;
	var shopping_cart_y = false;
	
	var fly_diffX = false;
	var fly_diffY = false;
	
	var currentXPos = false;
	var currentYPos = false;
	function FlyToCart(productId)
	{
		if(!shopping_cart_div)shopping_cart_div = document.getElementById('ShopInfo');
		if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
		}	
		
		//获得购物车的位置
		shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
		shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);
		
		//获取当前商品位置
		currentProductDiv = document.getElementById('slidingProduct' + productId);
		currentXPos = shoppingCart_getLeftPos(currentProductDiv);
		currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
		fly_diffX = shopping_cart_x - currentXPos;
		fly_diffY = shopping_cart_y - currentYPos;
	
		var shoppingContentCopy = currentProductDiv.cloneNode(true);
		shoppingContentCopy.id='';
		flyingDiv.innerHTML = '';
		flyingDiv.style.left = currentXPos + 'px';
		flyingDiv.style.top = currentYPos + 'px';
		flyingDiv.appendChild(shoppingContentCopy);
		flyingDiv.style.display='block';
		flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
		flyingDiv.style.zoom = null;
		flyToBasketN(productId);	
	}
	
	
	function flyToBasketN(productId)
	{
		
		var maxDiff = Math.max(Math.abs(fly_diffX),Math.abs(fly_diffY));
		var moveX = (fly_diffX / maxDiff) * flyingSpeed;
		var moveY = (fly_diffY/ maxDiff) * flyingSpeed;
		
		var startZoom;
		var endZoom;
		startZoom=1;
		endZoom=0.1;
		currentXPos = currentXPos + moveX;
		currentYPos = currentYPos + moveY ;
		
		flyingDiv.style.left = Math.round(currentXPos) + 'px';
		flyingDiv.style.top = Math.round(currentYPos) + 'px';
		if(!flyingDiv.style.zoom){
			flyingDiv.style.zoom = startZoom;
		}else{
			flyingDiv.style.zoom = (flyingDiv.style.zoom -endZoom)*0.95+endZoom;
		}
		
		
		if(moveX>0 && currentXPos > shopping_cart_x){
			flyingDiv.style.display='none';		
		}
		if(moveX<0 && currentXPos < shopping_cart_x){
			flyingDiv.style.display='none';		
		}
		
		if(flyingDiv.style.display=='block'){
			
			setTimeout('flyToBasketN("' + productId + '")',10);
		}
		else{
			
		}
	}
	
	function shoppingCart_getTopPos(inputObj)
	{
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null){
		if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
	  }
	  return returnValue;
	}
	
	function shoppingCart_getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null){
		if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
	  }
	  return returnValue;
	}
	
	
	
	
	
	
	  function BuyGold(ServerID,SPID){
        
        var GoldxmlHttp;
        if (window.ActiveXObject) {
			GoldxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		else if (window.XMLHttpRequest) {
			GoldxmlHttp = new XMLHttpRequest();
		}
		
        var url;
        var now= new Date();
		    url="/Ajax/AjaxToBuyPro.aspx?sp_id="+ SPID +"&ServerID=" + ServerID + "&act=BuyGold&Key=" + now.getSeconds();
		    //document.getElementById("web_menu").innerHTML = url;
	        GoldxmlHttp.onreadystatechange = function (){        
	            if(GoldxmlHttp.readyState == 4) {
			            if(GoldxmlHttp.status == 200) {
			                if (GoldxmlHttp.responseText=="Succeed to Buy!")
			                {    
			                       ShowBaskInfo();
			                       location.href="/EditShoppingCart.aspx";
			               }
			               else
			               {
			                    alert(GoldxmlHttp.responseText);
			               }
			            }

		        }
		        else
		        {
        		
		        }
		        
	        };
		    GoldxmlHttp.open("GET", url, true);
		    GoldxmlHttp.send(null);
    }
	


    
	
