//copyright www.soundjet.net

//得到新增时的 buylist_newpro  buylist_count  buylist_var 值
//通过buyed_style(temp.paraid)，给参数行加颜色与title属性值，在参数表页，要在生成参数表后执行
function buy_base(){
	buylist_count = 0;buylist_newpro = "";buylist_var = ""; buylist_provar = "";buylist_paravar = "";
	for(var i=0;i <= buylist_maxcount-1; i++) {
		var temp = new Cookie(document,"buy" + i);
		if (temp.load()) {
			if (isNaN(parseInt(temp.proid)))temp.remove();//防止写入cookie意外出错，增加判断proid值
			else{
			buylist_count++;
			buyed_style(temp.paraid);
			if (buylist_var != "") buylist_var += '&';
			if (buylist_provar != "") buylist_provar += ',';
			if (buylist_paravar != "") buylist_paravar += ',';
			buylist_var += "buy"+i + ":" + temp.proid + ":" + temp.paraid + ":" + escape(temp.num) + ":" + escape(temp.descript);
			buylist_provar += temp.proid;
			buylist_paravar += temp.paraid;
			}
		}
		else
		if (buylist_newpro == "") buylist_newpro = "buy" + i;
	}
	if (buylist_count == buylist_maxcount) {alert(buylist_alert);return false;}//如果有max个产品，则提示
}

document.write("<style type='text/css'>"); 
document.write("#lBuy{background-color: #D7EDFF;color:#000000; border:1px #333333 solid;font-size:12px; padding-right: 4px; padding-left: 4px; height: 111px; width:242px; padding-top: 2px; padding-bottom: 2px;visibility: hidden;filter:Alpha(Opacity=90);position:absolute;z-index:2;}"); 
document.write("</style>"); 
document.write("<div id='lBuy'></div>");

function para(proid,p){
if (document.getElementById("lBuy").style.visibility == 'visible') { ClsBuyPara(document.buyFrm.paraid.value,"add");}
var ptd = document.getElementById("para");
ptd.innerHTML = "<img src=images/loading.gif width=44 height=44 border=0>";
showrequest(para_page + 'proid=' + proid + '&page=' + p,'para',false);buy_base();
}

function BuyPara(proid,paraid){
	var act,act_str,buy_num,buy_des;
	if (indexofstr(paraid,buylist_paravar,",") == -1) {
		act="add";buy_num="";buy_des="";
		act_str = "<input type=submit value='" + lan_str[4] + "' onClick='buySub(\"add\");ClsBuyPara(" + paraid + ",\"mod\")'>"
	}
	else{
		var buypro;
		act="mod";
		buypro = buylist_var.split("&")[indexofstr(paraid,buylist_paravar,",")];
		buypro = buypro.slice(0,buypro.indexOf(":"));
		act_str = "<input type=submit value='" + lan_str[5] + "' onClick='buySub(\"mod\",\"" + buypro + "\");ClsBuyPara(" + paraid + ",\"mod\");'>" + " " + "<input type=submit value='" + lan_str[6] + "' onClick='if (buySub(\"del\",\"" + buypro + "\") != -1) ClsBuyPara(" + paraid + ",\"del\");'>"
		var temp = new Cookie(document,buypro);
		if (temp.load()) {buy_num=temp.num;	buy_des=temp.descript;}
	}
	if (act == "add" && buylist_count == buylist_maxcount) {alert(buylist_alert);return false;}

	var t,lBuy,g,tb = document.getElementById("tbpara").offsetTop;
	var x,y,p,lbuy_H;
	lBuy = document.getElementById("lBuy");
	p = document.getElementById("t" + paraid);
	t = p.title;
	g = false;
	if (lBuy.style.visibility == 'visible'){
		if (paraid != document.buyFrm.paraid.value)
		{ClsBuyPara(document.buyFrm.paraid.value,document.buyFrm.act.value);g=true}
		else g = false;
	}else g=true;
	x = event.x;
	y = event.y;
	if (g == true){
	lBuy.innerHTML  = "<form name=buyFrm method=post action='' onsubmit='return false'><div align=right><input type=image title='' src=images/close.gif width=12 height=12 onClick=\"ClsBuyPara(" + paraid + ",'" + act + "')\"></div><table width=100% border=0 cellspacing=0><input type=hidden name=paraid value=" + paraid + "><input type=hidden name=act value=" + act + "><input type=hidden name=proid value=" + proid + "><tr valign=top><td nowrap>" + lan_str[7] + "</td><td><input type='text' name='buy_num' size=4 maxlength=6 value='" + buy_num + "'></td></tr><tr valign=top><td>" + lan_str[8] + "</td><td><textarea name='buy_des' style='width:180px;overflow-y:visible' cols='2'>" + buy_des +"</textarea></td></tr><tr><td></td><td>" + act_str + "</td></tr></table></form>";
	lbuy_H=lBuy.offsetHeight;
	lBuy.style.left = (lBuy.offsetWidth + x <= document.body.clientWidth )?x:(document.body.clientWidth - lBuy.offsetWidth); 
	lBuy.style.top = (y + lbuy_H + p.offsetHeight <= document.body.clientHeight)?(p.offsetTop + 376 + tb + p.offsetHeight):(p.offsetTop + 376 + tb - lbuy_H);
	lBuy.style.visibility = "visible";
	document.getElementById("buy_list").style.visibility = "hidden";
	document.buyFrm.buy_num.focus();chg_row(p,"click");
	g = false;
	}
}

function ClsBuyPara(paraid,act){
  var obj,obj1;obj=document.getElementById("lBuy");
  if (obj.style) {obj.style.visibility = 'hidden';}
  obj1=document.getElementById("t" + paraid);
  switch (act)
  {
  case "add":
  chg_row(obj1,"none");break;
  case "del":
  chg_row(obj1,"none");break;
  case "mod":
  chg_row(obj1,"is");break;
  }
document.getElementById("buy_list").style.visibility = "hidden";
}

function chg_row(obj1,s){//行状态 s 有三种 "click" "is" "none"
switch (s)
{
case "click":
	obj1.title="";obj1.style.color = "#FF6A00";obj1.style.backgroundColor = "#D7EDFF";obj1.onmouseover = null;obj1.onmouseout = null;break;
case "is":
	obj1.style.color = "#FF6A00";obj1.style.backgroundColor = "#D7EDFF";obj1.onmouseover = null;obj1.onmouseout = null;obj1.title=lan_str[9];break;
case "none":
	obj1.style.color = "#333333";obj1.style.backgroundColor = "";obj1.onmouseover = function(){style.backgroundColor='#FFFFCC';style.color='#FF6A00';window.status='';return true;};obj1.onmouseout = function(){style.backgroundColor='';style.color='#333333';window.status='';return true;};obj1.title=lan_str[10];break;
}
}

function showatten_tr(objtr,str,_width){//在行上显示
var objtr = document.getElementById("t" + objtr);	
var a = document.getElementById("attension");a.style.backgroundColor = "#FF9900";a.style.color = "#000000";
if (_width) a.style.width= _width + "px";else a.style.width = "200px";
a.style.height = objtr.offsetHeight;
a.innerHTML="<span style='line-height:" + objtr.offsetHeight + "px'>" + str + "</span>";a.filters.Alpha.opacity = 0;
a.style.top = objtr.offsetTop + 376 + document.getElementById("tbpara").offsetTop;
a.style.left = (document.body.clientWidth)/2;
objFade = a;objFade.filters.Alpha.opacity=0;fadeOut();setTimeout("fadeIn()",1000);
}

function showatten(poSition,str,_width){//在窗口显示tl,tr,center,bl,br
var a = document.getElementById("attension");
a.style.backgroundColor = "#D7EDFF";a.style.color = "#000000";
if (_width) a.style.width= _width + "px";else a.style.width = "";
a.innerHTML="<div style='padding:15px'>" + str + "</div>";a.filters.Alpha.opacity = 0;
var s_l = (document.body.scrollLeft||window.pageXOffset), s_t = document.body.scrollTop || window.pageYOffset;
if (!s_l) s_l =0;if (!s_t) s_t =0;
var x,y,l=document.body.clientWidth - a.offsetWidth,t=document.body.clientHeight - a.offsetHeight;

switch (poSition)
{
case "tl":
x=s_l;y=s_t;
break;
case "tr":
y=s_t;x=document.body.clientWidth - a.offsetWidth + s_l;
break;
case "center":
x=l/2 + s_l;y=t/2 + s_t;
break;
case "bl":
x=s_l;y=t+s_t;
break;
case "br":
x=l+s_l;y=t + s_t;
break;
}
a.style.top = y;a.style.left =x;
objFade = a;objFade.filters.Alpha.opacity=0;fadeOut();setTimeout("fadeIn()",1000);
}

function buyed_style(paraid){//修改行颜色
	var temp = (document.getElementById('t'+paraid))?document.getElementById('t'+paraid):null;if (temp == null) return;
	chg_row(temp,"is");
}

//写、删cookie act: add, mod, del,如果为add 则buypro不理，如果del，则只用到buypro的值
function buy(act,buypro,proid,paraid,num,descript){
	var buypro1 = ((buypro) && (act == "mod" || act=="del"))?buypro:buylist_newpro;
	var buythis = new Buypro(buypro1,proid,paraid,num,descript);
	switch (act)
	{
	case "add":
		buythis.newpro();
		buy_base();
		break;
	case "mod":
		buythis.modify();
		buy_base();
		break;
	case "del":
		buythis.remove();
		buy_base();
		break;
	}
}

function Buypro(buypro,proid,paraid,num,descript){
this.bpro = new Cookie(document, buypro, 100);
this.newpro=function(){
	this.bpro.proid = proid;this.bpro.paraid = paraid;this.bpro.num = num;this.bpro.descript = descript;this.bpro.store();
	}
this.remove=function(){
	this.bpro.remove();
	}
this.modify=function(){
	this.bpro.proid = proid;this.bpro.paraid = paraid;this.bpro.num = num;this.bpro.descript = descript;this.bpro.store();
	}
}

function buySub(act,buypro,frm){
if ((act=="del") && (!confirm(lan_str[11]))) return -1;
var tfrm;
tfrm=(!frm)?document.getElementsByName("buyFrm")[0]:document.getElementsByName(frm)[0];
var proid = tfrm.proid.value;
var paraid = tfrm.paraid.value;
var num = tfrm.buy_num.value;
if ((act != "del") && (num == "")) num = 1;//增加、修改时如没有输入数量，则设为1个
var descript = tfrm.buy_des.value;
buy(act,buypro,proid,paraid,num,descript);
if (!frm){
	switch (act)
	{
	case "add":
	showatten_tr(paraid,lan_str[12]);break;
	case "mod":
	showatten_tr(paraid,lan_str[13]);break;
	case "del":
	showatten_tr(paraid,lan_str[14]);break;
	}
}
else if (frm.indexOf("frm_buy") != -1)
{showatten("tr",lan_str[15]);}
}

function buy_list(_refresh){//输出订单详细内容buy_list.asp,_refresh参数为在buy.asp页面修改订单信息时刷新buy_list.asp使用
	if (window.location.toString().indexOf("/buy.") == -1){
		var b=document.getElementById("buy_list");
		if((_refresh) && (buylist_count < 1)) b.style.visibility = "hidden";
		if (buylist_count < 1){
			showatten("center",lan_str[16],400);
		}
		else {
			if ((b.style.visibility !="visible")||(_refresh)) {
				showrequest(order_page,'buy_list_in',true);
				var s_l = (document.body.scrollLeft||window.pageXOffset);if (!s_l) s_l =0;
				b.style.left = document.body.clientWidth - b.offsetWidth+s_l;
			}
			if (!_refresh){b.style.visibility = (b.style.visibility == "visible")?"hidden":"visible";}
		}
		if((document.getElementById("lBuy")) && (b.style.visibility == "visible")){document.getElementById("lBuy").style.visibility = "hidden"}
	}
	else{//如果是在buy.asp页面
		if (buylist_count < 1){
			showatten("center",lan_str[16],400);
			document.getElementById("buylist").innerHTML = "";
			document.getElementById("buylist_b").innerHTML="";
		}
		else {
			showrequest(order_page,'buylist',false);
			location.hash = 'buylist_anchor';
		}
	}
}

function bi_over(b){
var a = document.getElementById(b);
a.style.backgroundColor = "#ffffcc";
document.getElementById(b + '_s').style.visibility = "visible";
}

function bi_out(b){
var a = document.getElementById(b);
a.style.backgroundColor = "";
document.getElementById(b + '_s').style.visibility = "";
}

function bi_click(b){
var a = document.getElementById(b);
a.style.backgroundColor = "#FFFFCC";
document.getElementById(b + '_s').style.visibility = "visible";
}

function buy_list_del(cookie_str,frm,paraid){//在buy_list中删除一个产品时，更新相应行
if(buySub('del',cookie_str,frm)!=-1){
	buy_list(true);
	var a = document.getElementById('t'+paraid);
	if (a)chg_row(a,"none");
	}
}

function buylist(){//buy.asp页面展开订单的按钮
if ((cookieenabled)){
	buy_base();
	var buylist_b_s = true, buylist_b = document.getElementById("buylist_b"),buylist_b_t=lan_str[17];
	if (buylist_count >0){
		buylist_b.innerHTML = "<a href='javascript:void(null)' title=\"" + buylist_b_t + "\"><img src=images/icon_ex.gif width=24 height=25 border=0></a>" + buylist_b_t

		buylist_b.onclick=function(){
			if(buylist_b_s){document.getElementById("buylist").style.display = "block";buylist_b_t=lan_str[18];buylist_b.innerHTML = "<a href='javascript:void(null)' title=\"" + buylist_b_t + "\"><img src=images/icon_col.gif width=24 height=25 border=0></a>" + buylist_b_t}
			else {document.getElementById("buylist").style.display = "none";
			buylist_b_t=lan_str[17];buylist_b.innerHTML = "<a href='javascript:void(null)' title=\"" + buylist_b_t + "\"><img src=images/icon_ex.gif width=24 height=25 border=0></a>" + buylist_b_t}
			buylist_b_s = !buylist_b_s;
			}
		}
	}
}

function buylists(){
	if (buylist_count >0 && document.form1){form1.subject.value = "订单"}
	showrequest(order_page,'buylist',false);
}