﻿
var check = 0;
var cut = 1;	
var copy = 1;
function initUserId(check)
{
	check=check;
	cut= 1;	
	copy=1;
}
function CookieEnable() 
{ 
   var result=false; 
   if(navigator.cookiesEnabled) 
     return true; 
   document.cookie = "testcookie=yes;"; 
   var cookieSet = document.cookie; 
   if (cookieSet.indexOf("testcookie=yes") > -1) 
     result=true; 
   document.cookie = ""; 
   return result; 
} 
 

function GetCookie(cookiename)
{
    var thebigcookie = document.cookie;
    var firstchar = thebigcookie.indexOf(cookiename);
    if (firstchar != -1) {
        firstchar += cookiename.length + 1;
        lastchar = thebigcookie.indexOf(";",firstchar);
        if(lastchar == -1) lastchar = thebigcookie.length;
        return unescape(thebigcookie.substring(firstchar, lastchar));
    }
    return "";
}
function SetCookie(cookiename,cookievalue,cookieexpdate,domainname)
{
    document.cookie = cookiename + "=" + escape(cookievalue)
    + "; domain=" + domainname
    + "; path=" + "/"
    + "; expires=" + cookieexpdate.toGMTString();
}
function ClearCookie()
{
	 var  host = window.location.host;//获取域名
	 var d = new Date();
    d.setYear(d.getYear()+1);
    SetCookie('history','[]',d,host); //清空商品
    Show(check);
}
function GetProduct(cookiename)
{
    var str = GetCookie(cookiename);
    var obj = [];
    if (str != null && str != '') obj = str.evalJSON();
   
    return obj;
}
//增加产品点击添加
function AddProduct(id,name,price,product_url,no)
{
	
	AddProductCookie(id,name,price,product_url,'1',no);		
	getCartProductNum();
	
}
//增加产品修改数量
function AddProduct2(id,name,price,product_url,no)
{

	AddProductCookie(id,name,price,product_url,'0',no);	
	getCartProductNum();
}
//保存到cookie
function AddProductCookie(id,name,price,product_url,isAdd,no)
{	
   
     var count = 1;
    if(isAdd=='0') {    	
    	if(document.getElementById("product_num"+id)) {
    		count = parseInt(document.getElementById("product_num"+id).value);
   		}
    }
    	
    	  if(!CookieEnable()) 
      { 
        alert("对不起，您的浏览器的Cookie功能被禁用，请开启"); 
      } 


   
    var obj = GetProduct('history');  
    for (var i=0;i<obj.length;i++)
   	
        if (id==obj[i][0])
        {
	        if(isAdd=='0'){
	        	obj[i][3]=count;
	        	if(count==0)
	        	{
	        		DeleteProduct(id,'history');
	        	}
	        }else{
	       		
	       		obj[i][3]=parseInt(obj[i][3])+count;
	        }
	        break;
        }  
    	if (i==obj.length)
        obj.push([id,name,price,count,product_url,no]);
    
     
    var d = new Date();
    d.setYear(d.getYear()+1);
   	var  host = window.location.host;//获取域名	
   	
    SetCookie('history', obj.toJSON(), d, host);
   Show(check);
  
}
//删除产品
function DeleteProduct(id,cookiename)
{
    var result=[];
    var obj = GetProduct(cookiename);
    for (var i=0;i<obj.length;i++)
        if (id!=obj[i][0]){result.push(obj[i]);}
   
    var d = new Date();
    d.setYear(d.getYear()+1);
    var host = window.location.host;//获取域名
    SetCookie(cookiename, result.toJSON(), d, host);
   Show(check);
}
//获取购物车产品数量总合

function getCartProductNum()
{
	var o = GetProduct('history');
	var sum = 0;
	for(var i=0;i<o.length;i++)
    {
        sum += o[i][3];
    } 
    if(document.getElementById("productNumWrap")){
   		if(getnum(sum)==0) {
   		 	document.getElementById("productNumWrap").innerHTML = '购物车有<font color="#C80000">'+sum+'</font>件商品';
   		 }
   		 else {
   		 	document.getElementById("productNumWrap").innerHTML = '<a href="/cart/show.jsp">购物车有<font color="#C80000">'+sum+'</font>件商品</a>';
   		 }
    }
}
//显示购物车产品信息
function Show(v)
{
	var o1 = GetProduct('history');	
	getCartProductNum();
	if(v==0)
	{
		ShowCart(o1);
	}
	
}
String.prototype.replaceAll = stringReplaceAll;
function stringReplaceAll(oldStr,reStr){
    return this.split(oldStr).join(reStr);
}


function ShowCart(o1){	
    var str1='';
    var sum = 0.0;
   
    var str = '';
    str = '<TABLE   cellSpacing=0 cellPadding=0 width="100%"  border=0>';
       
     if(o1.length>0)
  	str1 = '<tr class="h-dashed"><td colspan="7">购买的商品</td></tr><tr><td>配件编号(Item NO.)</td><td>配件名称(Description)</td><td>单价(Unit Price)</td><td>数量(QTY)</td><td>小计(Amount)</td><td>删除</td></tr>';    
    for(var i=0;i<o1.length;i++)
    {    
     
       str1 += '<tr><td><img onerror="setDefaultImg(this);" src="'+o1[i][4]+'" width="40" height="24"/>'+o1[i][5]+'</td><td>'+o1[i][1]+'</td><td>'+o1[i][2]+'</td><td><input type="text" name="product_num" id="product_num'+o1[i][0]+'"  value="'+o1[i][3]+'" onchange="AddProduct2(\''+o1[i][0]+'\',\''+o1[i][1]+'\',\''+o1[i][2]+'\',\''+o1[i][4]+'\',\''+o1[i][5]+'\');" size="3"  /></td><td>'+ForDight(o1[i][2]*o1[i][3],2)+'</td><td><a href="javascript:DeleteProduct(\''+o1[i][0]+'\',\'history\');void(0);">删除</a></td></tr>';
       sum += o1[i][2]*o1[i][3];
    }
    
   
    if(str1=='')
    {
    	str += '<tr><td colspan="7" align="center"><font color="red">您还没有选购商品！<a href="/">请选购商品</a></font></td></tr></table>';
    }
    str = str+ str1;
    str += '<tr><td colspan="7"><hr width=100% size=3 color=#F60; style="FILTER: alpha(opacity=100,finishopacity=0,style=3)"/></td></tr>';
	
    if(o1.length>0){   
   		str += '<tr><td colspan="7" align="right">合计金额(Total Amount)：￥'+ForDight(sum,2)+'元，</tr>';
     	str += '<tr><td colspan="7" align="right">&nbsp;<a href="/product/product_sort_show.jsp" >继续购物</a>&nbsp;&nbsp;<a href="javascript:ClearCookie();">清空购物车</a>&nbsp;&nbsp;<a href="/cart/payment.jsp?dt='+Math.random()+'">结算中心</a></td></tr></table>';
    }   
    if(document.getElementById('car')){
    	 document.getElementById('car').innerHTML = str;    	
    }
    return str;
}

//显示购物车产品信息
function showCartWrap()
{
	var o = GetProduct('history');	

	return ShowCartInfo(o);
	
}
//组合产品信息
function ShowCartInfo(o){	
	
    var str1='';
    var sum = 0.0;
   
    var str = '';
    str = '<TABLE   cellSpacing=0 cellPadding=0 width="100%" bgColor=#E7E7E7 border=0>'
    if(o.length>0)
    {
    	str1 += '<tr><td style="FONT-SIZE: 12px;COLOR: #1f336b;">配件编号(Item NO.)</td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">配件名称(Description)</td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">单价(Unit Price)</td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">数量(QTY)</td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">小计(Amount)</td></tr>';
    }
    for(var i=0;i<o.length;i++)
    {
       	str1 += '<tr><td style="FONT-SIZE: 12px;COLOR: #1f336b;"><img onerror="javascript:this.src = \'/images/small.jpg\'" src="'+o[i][4]+'" width="40" height="24"/>'+o1[i][5]+'</td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">'+o[i][1]+'</td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">'+o[i][2]+'</td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">'+o[i][3]+' </td><td style="FONT-SIZE: 12px;COLOR: #1f336b;">'+ForDight(o[i][2]*o[i][3],2)+'</td></tr>';
        sum += o[i][2]*o[i][3];
    }
    if(o.length>0) str1 += '<tr><td colspan="7"><hr width=100% size=3 color=#F60; style="FILTER: alpha(opacity=100,finishopacity=0,style=3)"/></td></tr>';
   
    str = str+ str1 
    if(o.length>0){   
   		 str += '<tr><td style="FONT-SIZE: 12px;COLOR: #1f336b;" colspan="5" align="right">合计金额(Total Amount)：￥<font color="red">'+ForDight(sum,2)+'</font>元，</tr>';
    }
   
    return str;
}
function ForDight(Dight,How) 
{ 
	Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How); 
	return Dight; 
}
function collection_product(product_id){
	alert('您还没有登录请先登录！');
	document.location.href='/account/login.jsp';	
}
function setDefaultImg(obj)
{
	obj.src = '/images/small.jpg';
}
