﻿// JScript 文件
var ajaxUrl="/"; //网站路径

//设置浏览产品历史记录
function setHistory(){
	//第一步取得需要写入cookie的内容 
	var pro_url = document.URL;//页面地址
	var pro_id = $("#pro_id").text();//产品id
	var pro_name = $("#pro_name").text();//产品名称 
	var pro_img = $("#pro_img").text();//图片路径 
	var pro_pricewhol = $("#pro_pricewhol").text();//批发价
	var pro_pricesale = $("#pro_pricesale").text();//零售价
	var pro_isprice = $("#pro_isprice").text();//是否标价
	var pro_isnegotiate = $("#pro_isnegotiate").text();//批发价是否面议

	var canAdd = true;//默认可以插入 
	//第二步，取得cookie并取得已有历史记录产品数目,对于cookie的操作，这里用了jquery的一个 
	var hisProduct = $.cookie('hisProduct'); 
	var len = 0; 
	if(hisProduct){ 
		hisProduct = eval('('+hisProduct+')');//静普通字符串转换成json 
		len = hisProduct.length; 
	} 
	//第三步，判断当前页面的产皮是否已经在记录中，用产品名称去比较 
	$(hisProduct).each(function(i){ 
		if(this.proid == pro_id){ 
		canAdd = false;//已经存在 
		return; 
		} 
	})
	
	//第四步，可以添加的话。 
	if(canAdd){ 
		var temp = "["; 
		var startNum = 0; 
		if(len > 3){startNum = 1;}//如果已经记录产品>3，前面说了，最多4个产品，所以这里其实是3，那么不需要第一个产品的记录 
		for(var m = startNum;m < len;m++){ 
			temp = temp + "{\"proid\":\""+hisProduct[m].proid+"\",\"url\":\""+hisProduct[m].url+"\",\"imgurl\":\""+hisProduct[m].imgurl+"\",\"proname\":\""+hisProduct[m].proname+"\",\"pricewhol\":\""+hisProduct[m].pricewhol+"\",\"pricesale\":\""+hisProduct[m].pricesale+"\",\"isprice\":\""+hisProduct[m].isprice+"\",\"isnegotiate\":\""+hisProduct[m].isnegotiate+"\"},"; 
		} 
		temp = temp + "{\"proid\":\""+pro_id+"\",\"url\":\""+pro_url+"\",\"imgurl\":\""+pro_img+"\",\"proname\":\""+pro_name+"\",\"pricewhol\":\""+pro_pricewhol+"\",\"pricesale\":\""+pro_pricesale+"\",\"isprice\":\""+pro_isprice+"\",\"isnegotiate\":\""+pro_isnegotiate+"\"}]"; 
		
		$.cookie("hisProduct",temp,{expires:1});//ie6下是否有写入
	} 
}
//读取浏览产品历史记录
function readHistory()
{
	//第五步，OK，输出吧
	//alert($.cookie("hisProduct"));
	var newtemp = eval('('+$.cookie("hisProduct")+')'); 
    if(newtemp != null)
    {
	    var newtemp_ = ""; 
	    for(var n = newtemp.length - 1;n > -1;n--){//这里你可以输出为自己要的格式
		    newtemp_ = newtemp_ + "<li><a href='"+newtemp[n].url+"' target='_blank'><img src='"+newtemp[n].imgurl+"' onerror=\"this.src='"+ajaxUrl+"System/Upload/nopic7.gif'\" width='70' height='70' class='imgborder' /></a>";
		    newtemp_ = newtemp_ + "<div class='txt'><a href='"+newtemp[n].url+"' target='_blank'>"+newtemp[n].proname+"</a></div>";

    //		if (newtemp[n].isprice == 'True')
    //        {
    //            if (newtemp[n].isnegotiate == 'True')
    //            {
    //                newtemp_ = newtemp_ + "<div class='div3'>批发价：商议</div>";
    //            }
    //            else
    //            {
    //                newtemp_ = newtemp_ + "<div class='div3'>批发价：￥" + newtemp[n].pricewhol + "</div>";
    //            }

    //            newtemp_ = newtemp_ + "<div class='div2'>零售价：￥" + newtemp[n].pricesale + "</div>";
    //        }
    //        else
    //        {
    //            newtemp_ = newtemp_ + "<div class='div1'>价格商议</div>";
    //        }
    		
		    newtemp_ = newtemp_ + "</li>";
	    } 
	    $("#prohistory").html(newtemp_);//我输出到prohistory这个div里面 
	    //$("#prohistory2").html($.cookie("hisProduct"));
	}
}


