﻿// JScript 文件
//创建回传XML文档给服务器的对象**********************************************************************************
function CallBackXML(requestxml)
{
	this.requestxml = requestxml;
	this.xmlHttp = this.CreateXMLHttpRequest();
}
 
CallBackXML.prototype.CreateXMLHttpRequest = function()
{ 
	var xmlHttp;
	if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}

CallBackXML.prototype.onComplete = function(responseText, responseXml)
{
    //alert("responseText:"+responseText);
}

CallBackXML.prototype.onLoading = function(){}

CallBackXML.prototype.onStatechange = function () {
	if (this.xmlHttp)
	{
		if(this.xmlHttp.readyState != 4)
			this.onLoading();
		else if(this.xmlHttp.status == 200)
			this.onComplete(this.xmlHttp.responseText,this.xmlHttp.responseXML);
	}
}
 
CallBackXML.prototype.DoCallBack = function(url)
{ 
	if( this.xmlHttp )
	{
		var othis=this;
		this.xmlHttp.open('POST',url,true);
		this.xmlHttp.onreadystatechange = function(){othis.onStatechange();};
		if (this.requestxml != null) {
			this.xmlHttp.setRequestHeader('Content-Type', 'text/xml');
		}
		this.xmlHttp.send(this.requestxml);
	}
}
function getshopCar()
{
    //返回购物车内的商品　
    var callbackobject = new CallBackXML("");
    callbackobject.onComplete = function(responseText, responseXml)
							    {
								    document.getElementById("divshopcar").innerHTML= responseText;
							    };
    							
    callbackobject.DoCallBack("shopcartInfo.aspx?getShopcart=1");
}

function Addshopcart(pid)
{
    //向购物车增加商品
    var callbackobject = new CallBackXML("");
    callbackobject.onComplete = function(responseText, responseXml)
							    {
							        if(responseText=="1")
							        {
							            getshopCar();
								    }
								    else
								    {
								        alert("操作失败，请重试！");
								    }
							    };
    							
    callbackobject.DoCallBack("shopcartInfo.aspx?Addshopcar="+pid);
}

function editCount(id,pid)
{
//修改购物车中商品的数量
    if(isDigit(document.getElementById(id).value))
    {
        var iquantity=document.getElementById(id).value;
        var callbackobject = new CallBackXML("");
        callbackobject.onComplete = function(responseText, responseXml)
							        {
							            if(responseText=="1")
							            {
							                getshopCar();
								        }
								        else
								        {
								            alert("操作失败，请重试！");
								        }
							        };
        							
        callbackobject.DoCallBack("shopcartInfo.aspx?EditShopCart="+pid+"&quantity="+iquantity);
}
    else
    {
        alert("只能输入数字！");
        document.getElementById(id).focus();
    }
}

function deletePro(pid)
{
    //删除购物车中的商品
    if(confirm("确定删除此商品吗？"))
    {
        var callbackobject = new CallBackXML("");
        callbackobject.onComplete = function(responseText, responseXml)
							        {
							            if(responseText=="1")
							            {
							                getshopCar();
								        }
								        else
								        {
								            alert("操作失败，请重试！");
								        }
							        };
        							
        callbackobject.DoCallBack("shopcartInfo.aspx?deleteID="+pid);
    }

}

function clearShopCart()
{
    //清空购物车中的商品
    if(confirm("确定清空购物车吗？"))
    {
        var callbackobject = new CallBackXML("");
        callbackobject.onComplete = function(responseText, responseXml)
							        {
							            if(responseText=="1")
							            {
							                getshopCar();
								        }
								        else
								        {
								            alert("操作失败，请重试！");
								        }
							        };
        							
        callbackobject.DoCallBack("shopcartInfo.aspx?clear=1");
    }
}
 function isDigit(value)//判断整数
{
    var patrn=/^[0-9]*$/;
    if (patrn.exec(value)==null||value=="") 
    {
        return false;
    }
    else
    {
        return true;
    }
}
function getQueryString(queryStringName)//获取url传来的参数值
{
 var returnValue="";
 var URLString=new String(document.location);
 if(URLString.toString().indexOf("#")>=0)
 {
     URLString=URLString.substring(0,URLString.length-1);
 }
 var serachLocation=-1;
 var queryStringLength=queryStringName.length;
 do
 {
  serachLocation=URLString.indexOf(queryStringName+"\=");
  if (serachLocation!=-1)
  {
   if ((URLString.charAt(serachLocation-1)=='?') || (URLString.charAt(serachLocation-1)=='&'))
   {
    URLString=URLString.substr(serachLocation);
    break;
   }
   URLString=URLString.substr(serachLocation+queryStringLength+1);
  }
  
 }
 while (serachLocation!=-1)
 if (serachLocation!=-1)
 {
  var seperatorLocation=URLString.indexOf("&");
  if (seperatorLocation==-1)
  {
   returnValue=URLString.substr(queryStringLength+1);
  }
  else
  {
   returnValue=URLString.substring(queryStringLength+1,seperatorLocation);
  } 
 }
 return returnValue;
}
