function ReadtheForm(name)//theForm 从COOKIE中读出购物车数据
{
var cookieString=document.cookie;
if (cookieString=="")
{
return false;
}
else
{
var firstChar,lastChar;
firstChar=cookieString.indexOf(name);
if(firstChar!=-1)
{
firstChar+=name.length+1;
lastChar = cookieString.indexOf(';', firstChar);
if(lastChar == -1) lastChar=cookieString.length;
return cookieString.substring(firstChar,lastChar);
}
else
{
return false;
}
} 
}
//item_no,商品编码
//item_name,商品名称
//item_amount，选购数量
//item_price，商品单价

function addcart(item_no,item_name,item_amount,item_price,item_vip,item_id,item_link)//theForm添加商品到购物车
{
var cookieString=document.cookie;
if (cookieString.length>=4000)//<--在此设置购物车的容量
{
alert("您的购物车已满，\n请先到收银台结帐后再选购商品！");
}
else if(isNaN(item_amount)||item_amount<1||item_amount.indexOf('.')!=-1)
{
alert("商品数量输入有误！");
}
else
{
var pro_list=ReadtheForm('cnhanscart');
var Then = new Date();
Then.setTime(Then.getTime()+60*60*1000);
var item_detail="|"+item_no+"&"+item_name+"&"+item_amount+"&"+item_price+"&"+item_vip+"&"+item_id+"&"+item_link;
if(pro_list==false)
{
document.cookie="cnhanscart="+escape(item_detail)+";expires=" + Then.toGMTString()+";path=/";
alert("谢谢！商品["+item_name+"]已经放入您的购物车！购物完毕后，要修改数量或提交订单请点击左边的“购物车”！");
}
else
{
if (pro_list.indexOf(item_no)!=-1)
{
alert('在您的购物车中已有此商品，\n您可以查看购物车并进行修改与删除！')
}
else
{
document.cookie="cnhanscart="+pro_list+escape(item_detail)+";expires=" + Then.toGMTString()+";path=/";
alert("谢谢！商品["+item_name+"]已经放入您的购物车！购物完毕后，要修改数量或提交订单请点击左边的“购物车”！");
}
}
}
}


function updateCookie()//theForm修改更新购物车
{
var limit=document.theForm.elements.length-3;

var Then = new Date();
Then.setTime(Then.getTime()+30*60*1000);
var values=document.theForm.storage.value
document.cookie="cnhanscart="+escape(values)+";expires=" + Then.toGMTString()+";path=/";

}


function Delete(TableID,id)//theForm删除购物车里的商品
{
var confirm_delete=window.confirm("确定要放弃选购此商品吗？")
if (confirm_delete)
{
var deletedList=document.theForm.storage.value;
var product_deletedList=deletedList.split("|");

for (i=1;i<product_deletedList.length;i++)
{
if(product_deletedList[i].indexOf(id)!=-1) delete product_deletedList[i];
}

var new_deletedList="";

for (i=1;i<product_deletedList.length;i++)
{

if (product_deletedList[i]!=undefined) new_deletedList=new_deletedList+"|"+product_deletedList[i];

}

document.theForm.storage.value=new_deletedList;
TableID.deleteRow();
updateTotal();
}
}


function updateTotal()//theForm更新价格总计
{
var new_total=0;
var limit=document.theForm.elements.length-3;

for (i=2;i<limit;i=i+3)
{
new_total=new_total*100+document.theForm.elements[i].value*100;
new_total=new_total/100;
}

if(isNaN(new_total))
{
document.theForm.total.value="商品数量输入有误";
}
else
{
document.theForm.total.value=new_total;
}
}


function updateSubTotal(m_id,new_amount,price)//theForm更新价格小计
{
var price=price*100;
var new_subtotal=new_amount*price/100;
var n_m_id="id"+m_id;
if(isNaN(new_amount)||new_amount<1||new_amount.indexOf('.')!=-1)
{
document.theForm.elements[n_m_id].value="商品数量输入有误";
}
else
{
document.theForm.elements[n_m_id].value=new_subtotal;
}
updateTotal();
}


function updateStorage(m_id,new_amount)//theForm更新购物车的商品数量
{
if(isNaN(new_amount)||new_amount<1||new_amount.indexOf('.')!=-1)
{
alert("数量填写错误！")
document.theForm.elements["aid"+m_id].focus();
document.theForm.elements["aid"+m_id].select();
}
else
{
var list=document.theForm.storage.value;
var product_list=list.split("|")

for (i=1;i<product_list.length;i++)
{
if (product_list[i].indexOf(m_id)!=-1)
{
var old_product=product_list[i];
var product1_list=product_list[i].split("&");
product1_list[2]=new_amount;
var new_product=product1_list[0]+"&"+product1_list[1]+"&"+product1_list[2]+"&"+product1_list[3]+"&"+product1_list[4]+"&"+product1_list[5]+"&"+product1_list[6];
list=list.replace(old_product,new_product);
}
}
document.theForm.storage.value=list;
}
}


