﻿
var myoptions = new Hash.Cookie('myoptions', {duration: 3600});

//添加选件到购物车
function addDataToShoppingCar(optionId,b){
	var t = "{";
		t += "'pic':'"+b.split("#")[0]+"',"
		t += "'num':'1',"
		t += "'name':'"+b.split("#")[1]+"',"
	    t += "'price':'"+b.split("#")[2]+"',"  
	    t += "'firstLevel':'"+b.split("#")[3]+"'," 
	    t += "'secondLevel':'"+b.split("#")[4]+"'" 
	    t += "}";
	    t = eval('('+t+')')
	myoptions.set(optionId, t);
	document.location.href="/option/shoppingCart.do";
}

//显示购物车的产品
function showShoppingCar(){
	var scline = "";
	var i=0;
	
	myoptions.each(function(d,optionId){
		i += 1;
		var bl="";
		if (i!=myoptions.getLength()){
			bl = "bike-bottom";
		}

		scline += "<div class='divwidth'>";
		scline += "<input type='hidden' id='optionId"+i+"' name='optionId' value='"+optionId+"'>";
		scline += "<input type='hidden' id='price"+i+"' name='price' value='"+d.price+"'>";		
		scline += "<input type='hidden' id='firstLevel"+i+"' name='firstLevel' value='"+d.firstLevel+"'>";
		scline += "<input type='hidden' id='secondLevel"+i+"' name='secondLevel' value='"+d.secondLevel+"'>";
			scline += "<div class='buy-div1 bike-right "+bl+"'>";
				scline += "<div class='buy-pic'>";
				scline += "<img src='"+d.pic+"' width='65' height='60' />";
				scline += "</div>";
				scline += "<div class='buy-text'>";
				scline += "<span class='clolor'>"+d.name+"</span><br/>";
				scline += "</div>";
			scline += "</div>";
			scline += "<div class='buy-div2 bike-right "+bl+"' style='padding-top: 20px;height:46px;'>";
				scline += "<span class='minus' onclick='updateTotalPriceForDown("+i+")'>-</span><input type='text' id='optionNum"+i+"' maxlength='3' name='optionNum' onkeyup='valNum(this)' onblur='updateTotalPrice(this.value,"+i+")' value='"+d.num+"' style='width:30px; height:20px; text-align:center; line-height:20px' /><span class='plus' onclick='updateTotalPriceForUp("+i+")'>+</span>";
			scline += "</div>";
			scline += "<div class='buy-div2 bike-right "+bl+"'>";
				scline += "<span class='clolor'>"+d.price+"元</span>";
				scline += "</p>";
			scline += "</div>";
			scline += "<div class='buy-div2 bike-right "+bl+"'>";
				scline += "无";
			scline += "</div>";
			scline += "<div class='buy-div2 bike-right "+bl+"'>";
				scline += "<span class='clolor titalPrice'  id='totalPrice"+i+"'>"+parseInt(d.price)*parseInt(d.num)+"元</span>";
			scline += "</div>";
			scline += "<div class='buy-div2 "+bl+"'>";
				scline += "<a href='javascript:' onclick='delRow("+optionId+",this)' class='blue-t'>删除</a>";
			scline += "</div>";
		scline += "</div>";
	});	
	
	$("shoppingCarList").setHTML(scline);
	getTotlePrice();
}

//加
function updateTotalPriceForUp(s){
	var t = parseInt($("optionNum"+s).value)+1;
	$("optionNum"+s).value=t;
	$("totalPrice"+s).innerText=t*parseInt($("price"+s).value)+"元";
	var n = myoptions.get($("optionId"+s).value);
		n.num=t
	myoptions.set($("optionId"+s).value,n);
	getTotlePrice();
}

//减
function updateTotalPriceForDown(s){
	var t = parseInt($("optionNum"+s).value)-1;
	if ($("optionNum"+s).value<1){
		alert("请至少购买一件产品");
		t = 1;
	}	
	$("optionNum"+s).value=t;
	$("totalPrice"+s).innerText=t*parseInt($("price"+s).value)+"元";
	var n = myoptions.get($("optionId"+s).value);
		n.num=t
	myoptions.set($("optionId"+s).value,n);	
	getTotlePrice();
}

//更新产品总价
function updateTotalPrice(num,s){
	if (num<1){
		num=1;
		$("totalPrice"+s).innerText=parseInt($("price"+s).value)+"元";
	}
	$("totalPrice"+s).innerText=(parseInt($("price"+s).value)*num)+"元";
	
	var n = myoptions.get($("optionId"+s).value);
	n.num=num	
	myoptions.set($("optionId"+s).value,n);		
	getTotlePrice();
}

function valNum(obj){
	//先把非数字的都替换掉，除了数字和.
	obj.value = obj.value.replace(/[^\d.]/g,"");
	//必须保证第一个为数字而不是.
	obj.value = obj.value.replace(/^\./g,"");
	//保证只有出现一个.而没有多个.
	obj.value = obj.value.replace(/\.{2,}/g,".");
	//保证.只出现一次，而不能出现两次以上
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}

//删除一行
function delRow(optionId,r){
	myoptions.erase(optionId);
	$(r.parentNode.parentNode).remove();
	getTotlePrice();
}

//清除所有
function delAllRow(){
	myoptions.each(function(d,optionId){
		myoptions.erase(optionId);
	});
}

//结算
function balanceClick(){
	var t = "[";
	var i=0;
	$ES('.row-line', $('shoppingCarList')).each(function(f) {
		i += 1;
	   	t += "{"
	   	var j=0;	
	   	$ES('input', f).each(function(d) {
			if (d.name=="optionId"){
				t += "'option':{'id':'"+d.value+"'},";
			}
			if (d.name=="num"){
				t += "'num':'"+d.value+"'";
			}	
			if (d.name=="firstLevel"){
				t += "'firstLevel':{'id':'"+d.value+"'},";
			}	
			if (d.name=="secondLevel"){
				t += "'secondLevel':{'id':'"+d.value+"'},";
			}	
			if (d.name=="price"){
				t += "'offerPrice':'"+d.value+"'";
			}	
	   	});
	   	
	    if (i<$ES('.row-line', $('shoppingCarList')).length){
	    	t += "},";
	    }else{
	    	t += "}";
	    }		   	
	});
	t += "]";
}

function getTotlePrice(){
	var totalPrice = 0;
	$ES('.titalPrice', $('shoppingCarList')).each(function(f) {
		totalPrice += parseInt(f.innerText);
	});
	$("sumPrice").innerText=totalPrice;
}

function checkSize(){
	if($ES('.divwidth', $('shoppingCarList')).length>0){
		window.location.href="/option/orderEdit.do";
	}else{
		alert("您的购物车中,没有任何选件");
	}
}

function setSCNum(){
	$("opnum").innerText=myoptions.getLength();	
}

