﻿function getXmlHttpRequestObject()
{
   var xmlhttp=false;
   try{
   xmlhttp= new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
   try{
    xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
   }catch(e){
    try{
     xmlhttp= new XMLHttpRequest();
    }catch(e){}
   }
}
return xmlhttp;
}

var getcarxml=getXmlHttpRequestObject();
function getcartxt()
{
getcarxml.open("post","/tools/ordercar.aspx",true);
getcarxml.onreadystatechange=handle;
getcarxml.send(null);
}

function handle()
{
if(getcarxml.readyState==4)
{
document.getElementById("cartxt").innerHTML = getcarxml.responseText;
}
}

var addcarxml=getXmlHttpRequestObject();
function addcar(itemid)
{
     
var norm1 = escape(document.getElementById('norm1').value);
var norm2 = escape(document.getElementById('norm2').value);
var pnum = escape(document.getElementById('pnum').value);
if(norm1 == "")
{
    norm1="0";
}
if(norm2 == "")
{
    norm2="0";
}
addcarxml.open("post","/tools/addcar.aspx?sx=1&pid="+itemid+"&norm1="+norm1+"&norm2="+norm2+"&count="+pnum,true);
addcarxml.onreadystatechange=handlecar;
addcarxml.send(null);
}
function handlecar()
{
if(addcarxml.readyState==4)
{
    if(addcarxml.responseText != "OK")
       {
        alert(addcarxml.responseText);
       }
   else
   {
    getcartxt();
	alert('已加入购物车');
    }
}
}
var delcarxml=getXmlHttpRequestObject();
function delcar(itemid)
{
delcarxml.open("post","/tools/addcar.aspx?sx=2&pid="+itemid,true);
delcarxml.onreadystatechange=delcarsub;
delcarxml.send(null);
}
function delcarsub()
{
    if(delcarxml.readyState==4)
    {
       if(delcarxml.responseText != "OK")
       {
        alert(delcarxml.responseText);
       }
       else
       {
        getcartxt();
        }
       
    }
}
var updatecarxml=getXmlHttpRequestObject();
function updatecar(pid,carid)
{
if(document.getElementById('pcount'+carid).value == "null" || document.getElementById('pcount'+carid).value =="0")
      {
        alert("请输入购买的商品数量!");
         return false;
      }
      else if(document.getElementById('pcount'+carid).value.replace(/[\d+]/ig,"").length>0)
      {
       alert("商品数量必须为整数!");
         return false;
      }
   var pcount=document.getElementById('pcount'+carid).value;
updatecarxml.open("post","/tools/addcar.aspx?sx=3&pid="+pid+"&carid="+carid+"&pcount="+pcount,true);
updatecarxml.onreadystatechange=updatecarsub;
updatecarxml.send(null);
}
function updatecarsub()
{
    if(updatecarxml.readyState==4)
    {
       if(updatecarxml.responseText != "OK")
       {
        alert(updatecarxml.responseText);
       }
       else
       {
        getcartxt();
        }
       
    }
}
 function carurl(pid){ 
       if(document.getElementById('pnum').value == "null" || document.getElementById('pnum').value =="0")
      {
        alert("请输入购买的商品数量!");
         return false;
      }
      else if(document.getElementById('pnum').value.replace(/[\d+]/ig,"").length>0)
      {
       alert("商品数量必须为整数!");
         return false;
      }

      addcar(pid)
      } 

