﻿// JScript 文件


function show_img(imgurl,id)
{   
    //alert("aa");
    var tempstr ="this.style.visibility='visible';document.getElementById(\"loaderInfo\").style.display='none'";
   
	document.getElementById("img_show" + id).innerHTML="<div id='loaderInfo'><img src='/images/ajax-loader.gif'><br />&nbsp;&nbsp;&nbsp;Loading...</div><img onerror=javascript:this.src='/images/uploadpic/noimage.gif' onload=" + tempstr + " src='/images/uploadpic/"+ imgurl + "' />";
	 //alert(document.getElementById("img_show" + id));
}

function show_img_clear(id)
{
    
	document.getElementById("img_show" + id).innerHTML ="";
}

function show_img_paimai(imgurl,id)
{   
    var tempstr ="this.style.visibility='visible';loaderInfo.style.display='none'";
	document.getElementById("img_show" + id).innerHTML="<div id='loaderInfo'><img src='images/ajax-loader.gif'><br />&nbsp;&nbsp;&nbsp;Loading...</div><img onerror=javascript:this.src='images/uploadpic/noimage.gif' onload=" + tempstr + " src='images/paimai_upload/"+ imgurl + "' />";
}
function show_img_SpecialOffer(imgurl,id)
{   
//    var tempstr ="this.style.visibility='visible';loaderInfo.style.display='none';alert('a');";
//	document.getElementById("img_show" + id).innerHTML="<div id='loaderInfo'><img src='images/ajax-loader.gif'><br />&nbsp;&nbsp;&nbsp;Loading...</div><img onerror=javascript:this.src='images/uploadpic/noimage.gif' onload=" + tempstr + " src='images/uploadpic/"+ imgurl + "' />";
	//alert(new Date().getDate());
	    var tempstr ="this.style.visibility='visible';document.getElementById(\"loaderInfo\").style.display='none'";
   
	document.getElementById("img_show" + id).innerHTML="<div id='loaderInfo'><img src='/images/ajax-loader.gif'><br />&nbsp;&nbsp;&nbsp;Loading...</div><img onerror=javascript:this.src='/images/uploadpic/noimage.gif' onload=" + tempstr + " src='/images/uploadpic/"+ imgurl + "' />";
}


/**
* AJAX 购买商品
* @param ProductID 商品的ID
*
*
**/
function AddToShopCart(ProductID)
    {
		
        var xmlHttp;
	    if (window.ActiveXObject) { 
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
		else if (window.XMLHttpRequest) {
			    xmlHttp = new XMLHttpRequest();
		    }
		    else
		    {
		        alert("no");
		    }
	    
	    var now= new Date();
        url="/AjaxPage/Ajax_BuyProduct.aspx?ProductID=" + ProductID + "&key=" + Math.random();
        xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState == 4) {
			    if(xmlHttp.status == 200) {
				    var AllHtml =xmlHttp.responseText;
				   //alert("aa" + ProductID + "bb" + AllHtml);
				    if(AllHtml == "1")
				    {
				        FlyToCart(ProductID);
				        ViewShopCart(0);//更新顶部购物车显示的数量
				    }
				    else
				    {
				        alert(AllHtml);
				    }
				    xmlHttp = null;
			    }
		    }
        
        }
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        return false;
    }
    
    
    
        function edit(indexNum,ProductID)
        {
            var cell = document.getElementById('td_Edit' + indexNum);
            var v = cell.innerHTML;
            if(!document.getElementById("text" + indexNum))
            {
                cell.innerHTML = "<input type=\"text\" size=\"1\" maxlength=\"3\" style=\"width:25px;text-align:center;\" name=\"text" + indexNum + " \" id=\"text" + indexNum + "\" onchange=\"UpdateShopCart('" + ProductID + "',this);\">";  //动态添加文本框
                document.getElementById("text" + indexNum).value = v;
            }
            else
            {
                cell.innerHTML = document.getElementById("text" + indexNum).value;
                //alert();
            }
           
        }

    
    /**
    * AJAX 显示 顶部购物车信息
    * @param isThree 如果为 1 刚显示数组第三个 
    *
    *
    **/
    function ViewShopCart(isThree)
    {
         
        var xmlHttp;
	    if (window.ActiveXObject) { 
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
		else if (window.XMLHttpRequest) {
			    xmlHttp = new XMLHttpRequest();
		    }
		    else
		    {
		        alert("no");
		    }
	    
	    var now= new Date();
        url="/AjaxPage/Top_ShopCartCount.aspx?key=" + now.getMilliseconds();
        xmlHttp.onreadystatechange = function(){
        
        if(xmlHttp.readyState == 4) {
			    if(xmlHttp.status == 200) {
				    var AllHtml =xmlHttp.responseText;

				    
				    
				    var arr = AllHtml.split('$$$$$');
				    document.getElementById("Ajax_ShopCartCount").innerHTML = arr[0];
				    document.getElementById("Ajax_ShopCartPrice").innerHTML = arr[1];
				    if(isThree == 1)
				    {
				        document.getElementById("BigPrice_Ajax").innerHTML = arr[1];
				    }
				    xmlHttp = null;
				   
			    }
		    }
        
        }
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        return false;
    }
    
    /**
    * AJAX CheckOut页面..更改购物车 数量
    * @param ProductID:     商品ID
    * @param ProductNum:    商品数量
    * @param TotalPriceID:  控制改变商品总价格的ID
    * @param Price:         商品的单价
    **/
    function UpdateShopCart(ProductID,ProductNum)
    {   
        var xmlHttp;
	    if (window.ActiveXObject) { 
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
		else if (window.XMLHttpRequest) {
			    xmlHttp = new XMLHttpRequest();
		    }
		    else
		    {
		        alert("no");
		    }
	    
	    var now= new Date();
	    
	    url="/AjaxPage/Ajax_UpdateShopCart.aspx?ProductID=" + ProductID + "&ProductNum=" + ProductNum.value + "&key=" + now.getMilliseconds();
	    //alert(url);
        xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState == 4) {
        
                ViewShopCart(0);//更新顶部购物车显示的数量
                
			    if(xmlHttp.status == 200) {
				    var AllHtml =xmlHttp.responseText;
				    //alert(AllHtml);
				    //alert(document.getElementById("Ajax_UpdateShopCart_View").innerHTML);
				     var arr = AllHtml.split('$$$$$');
				    if (arr[0]=="0")
				    {
				        alert("Unable to add,not enough inventory!");
				        document.getElementById("Ajax_UpdateShopCart_View").innerHTML = arr[1];
				    }
				    else
				    {
				        document.getElementById("Ajax_UpdateShopCart_View").innerHTML = arr[1];
				    }
				    
				    xmlHttp = null;
				    
			    }
		    }
        
        }
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        return false;
    }
    
    /**
    * AJAX CheckOut页面..更改购物车 数量
    * @param ProductID:     商品ID
    * @param ProductNum:    商品数量
    * @param TotalPriceID:  控制改变商品总价格的ID
    * @param Price:         商品的单价
    **/
    function UpdateShopCartDel(ProductID)
    {   
        var xmlHttp;
	    if (window.ActiveXObject) { 
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
		else if (window.XMLHttpRequest) {
			    xmlHttp = new XMLHttpRequest();
		    }
		    else
		    {
		        alert("no");
		    }
	    
	    var now= new Date();
	    
	    url="/AjaxPage/Ajax_UpdateShopCart.aspx?ProductID=" + ProductID + "&key=" + now.getMilliseconds();
	    //alert(url);
        xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState == 4) {
        
                ViewShopCart(0);//更新顶部购物车显示的数量
                
			    if(xmlHttp.status == 200) {
				    var AllHtml =xmlHttp.responseText;
				    //alert(AllHtml);
				    //alert(document.getElementById("Ajax_UpdateShopCart_View").innerHTML);
				    var arr = AllHtml.split('$$$$$');
				    document.getElementById("Ajax_UpdateShopCart_View").innerHTML = arr[1];
				    
				    xmlHttp = null;
				    
			    }
		    }
        
        }
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        return false;
    }
    
    function UpdateShopCartFalse()
    {
        document.getElementById("Ajax_UpdateShopCart_View").style.display = "none";
        setTimeout("document.getElementById('Ajax_UpdateShopCart_View').style.display = 'block';",300); 
    }
    
    //JS 浮点数控制
function accMul(arg1,arg2)
    {
        var m=0,s1=arg1.toString(),s2=arg2.toString();
        try{m+=s1.split(".")[1].length}catch(e){}
        try{m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    }

//控件输入
function onlyNum(obj) 
    {
        if(obj.value != obj.value.replace(/\D/g,''))
        {
            if(obj.value.replace(/\D/g,'') == "")
            {
                obj.value = "1";
                obj.onchange();
            };
            obj.value=obj.value.replace(/\D/g,'');
            //return;
        }
        else
        {
            if(obj.value == "")
            {
               obj.value = "1";
               obj.onchange();
            }
        }
    } 
    
//求单选按纽的值，传radio名字作为参数。未选返回false；有选择项，返回选项值。
function getRadioValue(name){
    var radioes = document.getElementsByName(name);
    for(var i=0;i<radioes.length;i++)
    {
         if(radioes[i].checked){
          return radioes[i].value;
          //alert(radioes[i].value);
         }
    }
    return false;
}

function set_JS_SendAreaMoney()//送货
{   
    document.getElementById("tb_JS_SendAreaMoney").value = getRadioValue("radio_Area");
}

function set_JS_PayType()//支付方式
{
   document.getElementById("tb_JS_PayType").value = getRadioValue("radio_PayType");
}


    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('ShopCart');
		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 TopSearch()
         {
            window.location.href = "/SearchResults.aspx?keyWords=" + document.getElementById("topsearch").getAttribute("value") + "&GameID=" + document.getElementById("selectTop").value;
            return false;
        }