//document.domain = "legou888.com";
var WEBROOT = "http://www.legou888.com/legou";
var IDENTIFYIMAGEURL = WEBROOT + "/identifyimage.jpg";
var XmlHttpObject = CreateXmlHttp();
var shoppingCar = null;
/* shoppingCar example*/
//var shoppingCar = new Object();
//shoppingCar.goods = new Array();
//var good;
//good = new Object();
//good.id = "22";
//good.img = "http://lookb.vicp.net:8080/legou/userfiles/1/1257920221845.jpg";
//good.name = "诺基亚5800i（红色）";
//good.price = 2466.0;
//good.number = "0";
//shoppingCar.goods.push(good);
//good = new Object();
//good.id = "44";
//good.img = "http://lookb.vicp.net:8080/legou/userfiles/1/1257910406287.jpg";
//good.name = "索尼爱立信T715（绿色）";
//good.price = 2180.0;
//good.number = "0";
//shoppingCar.goods.push(good);
//var shoppingCar = new Object();
//shoppingCar.goods = new Array();
//var good = new Object();
//good.img = "#";
//good.name = "诺加压呀呀（黑色）";
//good.price = 1890;
//good.number = 1;
//shoppingCar.goods.push(good);
//shoppingCar.goods.push(good);
function $_(obj){
    return document.getElementById(obj);
}
function refreshIdentifyImage(obj){
	if(obj != null && obj != 'undefined'){
		if( typeof(obj) == "object"){
			if(obj.src){
				obj.src = IDENTIFYIMAGEURL + "?" + (new Date()).getTime();
			}
		}else{
			if(document.getElementById(obj)){
				document.getElementById(obj).src = IDENTIFYIMAGEURL + "?" + (new Date()).getTime();
			}
		}
	}
}

function setIndexCookie(name, value, expiretime){
    var expires = new Date();
    expires.setTime(expires.getTime() + expiretime);

    var nameString = name + "=" + escape(value);
    var expiryString = " ;expires = " + expires.toGMTString();
    var pathString = " ;path = /";
    document.cookie = nameString + expiryString + pathString;
}

function getIndexCookie(name){
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1){
        begin = dc.indexOf(prefix);
        if (begin != 0)
            return null;
    }else{
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1){
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function registerSubmit(){
	var shopid = document.getElementById("shopid").value;
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var password2 = document.getElementById("password2").value;
	var email = document.getElementById("email").value;
	var identify = document.getElementById("identify").value;
	
	if(username == null || username == ""){
		setErrorInfo("msg","请输入用户名");
		return;
	}
	if(username.length < 5 || username.length > 18){
		setErrorInfo("msg","用户名由5-18个字符组成");
		return;
	}
	if(password == null || password == ""){
		setErrorInfo("msg","请输入密码");
		return;
	}
	if(password.length < 6 || password.length > 18){
		setErrorInfo("msg","密码长度为6-18位字符");
		return;
	}
	if(password != password2){
		setErrorInfo("msg","两次输入的密码不一致");
		return;
	}
	if(identify == null || identify == ""){
		setErrorInfo("msg","请输入验证码");
		return;
	}
	//<span>电子邮箱可以取回忘记的密码，请正确填写如：yourname@youmail.com</span>
    var ParamString = "";
	ParamString = addPostParam(ParamString, "opr", "register");
	ParamString = addPostParam(ParamString, "shopid", shopid);
	ParamString = addPostParam(ParamString, "username", username);
	ParamString = addPostParam(ParamString, "password", password);
	ParamString = addPostParam(ParamString, "email", email);
	ParamString = addPostParam(ParamString, "identify", identify);
	getScriptByPost(WEBROOT + "/customer.php", ParamString, "submitOrderSuccess(null)");
}
function findPwdSubmit(){
	var username = document.getElementById("username").value;
	var email = document.getElementById("email").value;
	var identify = document.getElementById("identify").value;
	if(username == null || username == "" || username.length < 4 || username.length > 16){
		setErrorInfo("username","用户名最少4位，最多12位，请重新输入");
		return;
	}
	if(email==""){
		setErrorInfo("email","请填写邮箱");
		return;
	}
	var patrn=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;     
  	if(!patrn.exec(email)){   
		setErrorInfo("email","邮件地址格式不正确");
 	 	return ;   
  	}  
	if(identify == null || identify == ""){
		setErrorInfo("identify","请输入验证码");
		return;
	}
    var ParamString = "";
	ParamString = addPostParam(ParamString, "opr", "findpwd");
	ParamString = addPostParam(ParamString, "username", username);
	ParamString = addPostParam(ParamString, "email", email);
	ParamString = addPostParam(ParamString, "identify", identify);
	getScriptByPost(WEBROOT + "/customer.php", ParamString, "submitOrderSuccess(null)");
}
function setErrorInfo(id, str){
	var obj = document.getElementById(id);
	if(obj != null && typeof(obj) == "object"){
		obj.innerHTML = str;
		//resetErrorInfo(id);
		//var v = obj.value;
		//var s = obj.outerHTML;
		//var i = document.createElement("span");
		//i.style.color = "red";
		//i.innerHTML = str;
		//obj.outerHTML = s + "<br/>" + i.outerHTML;
		//document.getElementById(id).value = v;
	}
}
function resetErrorInfo(id){
	var obj = document.getElementById(id);
	if(obj != null && typeof(obj) == "object"){
		obj.innerHTML = "";
		//var v = obj.value;
		//var s = obj.outerHTML;
		//obj.parentNode.innerHTML = s;
		//document.getElementById(id).value = v;
		//var br = obj.nextSibling;
		//if(br != null && br.tagName && br.tagName == "BR"){
		//	var span = null;
		//	if(br!=null) span = obj.nextSibling.nextSibling;
		//	if(span != null && span.tagName && span.tagName == "SPAN"){
		//		span.outerHTML = "";
		//	}
		//	br.outerHTML = "";
		//}
	}
}

function customerLogin(needRefer){
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var identify = document.getElementById("identify").value;
	var refer = getHttpRoot();
	
	if(username == null || username == ""){
		setErrorInfo("msg","请输入用户名");
		return;
	}
	if(password == null || password == ""){
		setErrorInfo("msg","请输入密码");
		return;
	}
	if(identify == null || identify == ""){
		setErrorInfo("msg","请输入验证码");
		return;
	}
    var ParamString = "";
	ParamString = addPostParam(ParamString, "opr", "login");
	ParamString = addPostParam(ParamString, "username", username);
	ParamString = addPostParam(ParamString, "password", password);
	ParamString = addPostParam(ParamString, "identify", identify);
	if(needRefer==1){
		ParamString = addPostParam(ParamString, "refer", refer);
	}
	getScriptByPost(WEBROOT + "/customer.php", ParamString, "submitOrderSuccess(null)");
}

function customerLogout(){
	setIndexCookie('LGSE','',0);
	setIndexCookie('LGUN','',0);
	setIndexCookie('LGID','',0);
	checkLeftInfo();
	checkTopInfo();
}

function checkCustomerLogin(){
	var sessionid = getIndexCookie('LGSE');
	var username = getIndexCookie('LGUN');
	var userid = getIndexCookie('LGID');
	if(sessionid != null && sessionid != ""
		&& username != null && username != ""
		&& userid != null && userid != ""){
		return username;
	}
	return "";
}

function customerLoginRB(refer){
	customerLogin(refer);
	Sleep(this,2000);   
	this.NextStep=function(){   
		checkLeftInfo();
		checkTopInfo();
	}
}

function checkLeftInfo(){
	var username = checkCustomerLogin();
	var loginInfo = document.getElementById("loginInfo");
	if(loginInfo != null && loginInfo != "undefined"){
		if(username != ""){
			var str = "<li class=\"car_login_info\">" + username + "，您好，欢迎来到乐购吧！</li>"
			var price = getIndexCookie('gp');
			var number = getIndexCookie('gn');
			if(price == null || price == ""){
				setIndexCookie('gp', '', 0);
				price = 0;
			}
			if(number == null || number == ""){
				setIndexCookie('gn', '', 0);
				number = 0;
			}
			str += "<li class=\"car_login_info\" style=\"color:#333333;\">购物车共计商品 <span style='color:#F00;'>"+number+"</span>件, 合计 <span style='color:#F00;'>￥"+price+"</span> 元</li>";
			loginInfo.innerHTML = str;
		}else{
			var str = "<li class=\"car_login_info\">您好，为了方便您的账户操作，请先登录！</li>"
					+ "<li class=\"car_login_item\"></li><li class=\"car_login_input\"><span id=\"msg\" style=\"color:red\"></span></li>"
					+ "<li class=\"car_login_item\">用户名：</li><li class=\"car_login_input\"><input type=\"text\" style=\"width:120px;height:16px;\" id=\"username\" name=\"username\" onblur=\"resetErrorInfo('msg')\"></li>"
					+ "<li class=\"car_login_item\">密码：</li><li class=\"car_login_input\"><input style=\"width:120px;height:16px;\" type=\"password\" id=\"password\" name=\"password\" onblur=\"resetErrorInfo('msg')\"></li>"
					+ "<li class=\"car_login_item\">验证码：</li><li class=\"car_login_input\"><input style=\"width:120px;height:16px;\" type=\"text\" id=\"identify\" name=\"identify\" onblur=\"resetErrorInfo('msg')\"></li>"
					+ "<li class=\"car_login_item\">&nbsp;</li><li class=\"car_login_input\"><a href=\"#\" onclick=\"javascript:refreshIdentifyImage('identifyimg');\" class=\"img_yz\"><img id=\"identifyimg\" src=\"\" alt=\"点击刷新\"/></a></li>"
					+ "<li class=\"car_login_item\">&nbsp;</li><li class=\"car_login_input\"><input type=\"image\" onclick=\"customerLoginRB(0)\" style=\"margin-top:4px;\" src=\"http://style_1.legou888.com/img/login_2009.png\"></li>";
			loginInfo.innerHTML = str;
			refreshIdentifyImage('identifyimg');
		}
	}
}

function checkTopInfo(){
	var username = checkCustomerLogin();
	var httproot = getHttpRoot();
	var toplink = document.getElementById("toplink");
	var myshopname = "";
	var myshopnameObj = document.getElementById("myshopname");
	if(myshopnameObj == null || myshopnameObj == "undefined"){
		myshopname = "乐购吧";
	}else{
		myshopname = myshopnameObj.innerHTML;
	}
	if(toplink != null && toplink != "undefined"){
		if(username == ""){
			toplink.innerHTML = "您好！欢迎来到 <font color='#fad232'>"+myshopname+"</font> 商城 ，[<a href='" + httproot + "customerLogin.html'>顾客登录</a>]&nbsp;&nbsp;[<a href='" + httproot + "newCustomer.html'>顾客注册</a>]";
		}else{
			toplink.innerHTML = "您好！<B>" + username + "</B>，欢迎来到"+myshopname+"！[<a href='" + httproot + "userHome.html'>用户中心</a>]&nbsp;&nbsp;[<a href='javascript:customerLogout()'>退出</a>]";
		}
	}
}

function checkCustomerBox(id){
	var username = checkCustomerLogin();
	if(username != null && username != ""){
		var box = document.getElementById(id);
		if(box != null && box != "undefined"){
			box.innerHTML = "<li class=\"login_item\">&nbsp;</li><li class=\"login_input\">您好！" + username + "，欢迎来到乐购吧！</li><li class=\"login_info\">&nbsp;</li>";
		}
	}
}

function getShoppingCar(){
	var gids = getIndexCookie('gids');
	if(gids != null && gids != ""){
		var ParamString = "";
		ParamString = addPostParam(ParamString, "gids", gids);
		getScriptByPost(WEBROOT + "/shoppingcart.php", ParamString, "submitOrderSuccess('initShoppingCar()')");
	}
}
function initShoppingCar(){
	if(shoppingCar != null && typeof(shoppingCar) == "object"){
		var goods = shoppingCar.goods;
		if(goods != null){
			var table = document.getElementById("cartab");
			if(table != null && typeof(table) == "object"){
				while(table.rows.length>1){
					table.deleteRow(table.rows.length-1);
				}
				var totalPrice = 0;
				var totalNumber = 0;
				for(var i=0; i<goods.length; i++){
					var row = table.insertRow(table.rows.length);
					var good = goods[i];
					var img = row.insertCell(-1);
					//img.className = "imga";
					img.innerHTML = "<img src=\"" + good.img + "\"/>";
					var name = row.insertCell(-1);
					//name.className = "leftAlign s1";
					name.innerHTML = "<strong>" + good.name + "</strong>";
					var price = row.insertCell(-1);
					//price.className = "s1";
					price.innerHTML = "￥" + good.price;
					var cell = row.insertCell(-1);
					//cell.className = "s1";
					cell.innerHTML = "免运费";
					var num = row.insertCell(-1);
					//num.className = "s1";
					num.innerHTML = "<input type=\"text\"  value=\"" + good.number + "\" style=\"width:35px;\" onchange=\"changeGoodsNumber("+good.id+", this.value)\"/>";
					var total = row.insertCell(-1);
					//total.className = "s1";
					total.innerHTML = "￥" + (good.price*good.number);
					var exec = row.insertCell(-1);
					//exec.className = "s1";
					exec.innerHTML = "<a href=\"javascript:deleteGoodsFromShoppingCart("+good.id+")\">移除</a>";
					totalPrice += good.price*good.number;
					totalNumber += good.number*1;
				}
				document.getElementById("totalPrice").innerHTML = "合计金额:" + totalPrice + "元";
				document.getElementById("totalPriceCopy").innerHTML = "合计金额:" + totalPrice + "元";
				document.getElementById("totalPriceCopy2").innerHTML = "合计金额:" + totalPrice + "元";
				document.getElementById("totalPriceCopy3").innerHTML = "合计金额:" + totalPrice + "元";
				setIndexCookie('gp', totalPrice, 365*24*3600*1000);
				setIndexCookie('gn', totalNumber, 365*24*3600*1000);
			}
			
		}else{
			setIndexCookie('gp', totalPrice, 0);
			setIndexCookie('gn', totalNumber, 0);
		}
		checkLeftInfo();
		checkTopInfo();
	}
}

function changeGoodsNumber(id, n){
	if(isNaN(id) || isNaN(n)){
		return ;
	}
	var gids = getIndexCookie('gids');
	if(gids != null && gids != ""){
		var ParamString = "";
		ParamString = addPostParam(ParamString, "opr", "cha");
		ParamString = addPostParam(ParamString, "id", id);
		ParamString = addPostParam(ParamString, "n", n);
		ParamString = addPostParam(ParamString, "gids", gids);
		getScriptByPost(WEBROOT + "/shoppingcart.php", ParamString, "submitOrderSuccess('initShoppingCar()')");
	}
}

function addGoodsToShoppingCart(id){
	if(isNaN(id)){
		return ;
	}
	var gids = getIndexCookie('gids');
	if(gids == null) gids = "";
	var ParamString = "";
	ParamString = addPostParam(ParamString, "opr", "add");
	ParamString = addPostParam(ParamString, "id", id);
	ParamString = addPostParam(ParamString, "n", "1");
	ParamString = addPostParam(ParamString, "gids", gids);
	getScriptByPost(WEBROOT + "/shoppingcart.php", ParamString, "submitOrderSuccess(null)");
}

function deleteGoodsFromShoppingCart(id){
	if(isNaN(id)){
		return ;
	}
	var gids = getIndexCookie('gids');
	if(gids != null && gids != ""){
		var ParamString = "";
		ParamString = addPostParam(ParamString, "opr", "del");
		ParamString = addPostParam(ParamString, "id", id);
		ParamString = addPostParam(ParamString, "gids", gids);
		getScriptByPost(WEBROOT + "/shoppingcart.php", ParamString, "submitOrderSuccess('initShoppingCar()')");
	}
}

function stepForward(step){
	switch(step){
	case 2 :
		var gids = getIndexCookie('gids');
		if(gids == null || gids == ""){
			setErrorInfo("carinfo", "请选购商品");
			return;
		}
		document.getElementById("consignee").value = '';
		document.getElementById("phone").value = '';
		document.getElementById("mobile").value = '';
		document.getElementById("sex").value = 1;
		document.getElementById("address").value = '';
		document.getElementById("zipCode").value = '';
		document.getElementById("stepBox_1").style.display = "none";
		document.getElementById("stepBox_2").style.display = "block";
		copyTable("cartab_2","cartab","cartab_1");
		document.getElementById("consignee").focus();
		break;
	case 3 :
		var consignee = document.getElementById("consignee").value;
		var phone = document.getElementById("phone").value;
		var mobile = document.getElementById("mobile").value;
		var address = document.getElementById("address").value;
		var zipCode = document.getElementById("zipCode").value;
		var remarks = document.getElementById("remarks").value;
		var way = document.getElementsByName("way");
		var sex = document.getElementsByName("sex");
		if(consignee == null || consignee == ""){
			setErrorInfo("msginfo", "请填写收货人");
			return;
		}
		if((mobile == null || mobile == "")
			&& (phone == null || phone == "")){
			setErrorInfo("msginfo", "固定电话和手机你必须填写一项");
			return;
		}
		if(address == null || address == ""){
			setErrorInfo("msginfo", "请填写收货地址");
			return;
		}
		var wayValue = "";
		for(var i=0; i<way.length; i++){
			if(way[i].checked){
				wayValue = way[i].title;
				break;
			}
		}
		var sexValue = 0;
		for(var i=0; i<sex.length; i++){
			if(sex[i].checked){
				sexValue = sex[i].value;
				break;
			}
		}
		document.getElementById("consigneeTD").innerHTML = consignee;
		document.getElementById("phoneTD").innerHTML = phone;
		document.getElementById("mobileTD").innerHTML = mobile;
		document.getElementById("sexTD").innerHTML = (sex=="1"?"先生":"女士");
		document.getElementById("addressTD").innerHTML = address;
		document.getElementById("zipCodeTD").innerHTML = zipCode;
		document.getElementById("wayTD").innerHTML = wayValue;
		document.getElementById("remarksTD").innerHTML = remarks;
		document.getElementById("stepBox_2").style.display = "none";
		document.getElementById("stepBox_3").style.display = "block";
		copyTable("cartab_3","cartab","cartab_2");
		resetErrorInfo("ordermsg");
		break;
	case 4 :
		var customerid = getIndexCookie('LGID');
		if(customerid == null || customerid == ""){
			setErrorInfo("ordermsg", "请先在左侧登录再提交订单，感谢您的支持！");
			return;
		}
		var shopid = document.getElementById("shopid").value;
		if(shopid == null || shopid == ""){
			setErrorInfo("ordermsg", "无法获取店铺信息，请刷新后重试！");
			return;
		}
		var gids = getIndexCookie('gids');
		if(gids == null || gids == ""){
			setErrorInfo("ordermsg", "无法获取已购商品信息，请刷新后重试！");
			return;
		}
		var consignee = document.getElementById("consignee").value;
		var phone = document.getElementById("phone").value;
		var mobile = document.getElementById("mobile").value;
		var address = document.getElementById("address").value;
		var zipCode = document.getElementById("zipCode").value;
		var remarks = document.getElementById("remarks").value;
		var way = document.getElementsByName("way");
		var sex = document.getElementsByName("sex");
		if(consignee == null || consignee == ""){
			setErrorInfo("ordermsg", "请填写收货人");
			return;
		}
		if((mobile == null || mobile == "")
			&& (phone == null || phone == "")){
			setErrorInfo("ordermsg", "固定电话和手机你必须填写一项");
			return;
		}
		if(address == null || address == ""){
			setErrorInfo("ordermsg", "请填写收货地址");
			return;
		}
		var wayValue = "";
		for(var i=0; i<way.length; i++){
			if(way[i].checked){
				wayValue = way[i].value;
				break;
			}
		}
		var sexValue = 0;
		for(var i=0; i<sex.length; i++){
			if(sex[i].checked){
				sexValue = sex[i].value;
				break;
			}
		}
		//document.getElementById("sid").value = shopid;
		//document.getElementById("cid").value = customerid;
		//document.getElementById("gids").value = gids;
		//var orderForm = document.getElementById("orderForm");
		//orderForm.action = WEBROOT + "/orderService.php";
		//orderForm.method = "post";
		//orderForm.target = "orderFrame";
		//orderForm.submit();
		var ParamString = "";
		ParamString = addPostParam(ParamString, "opr", "add");
		ParamString = addPostParam(ParamString, "sid", shopid);
		ParamString = addPostParam(ParamString, "cid", customerid);
		ParamString = addPostParam(ParamString, "gids", gids);
		ParamString = addPostParam(ParamString, "consignee", consignee);
		ParamString = addPostParam(ParamString, "phone", phone);
		ParamString = addPostParam(ParamString, "mobile", mobile);
		ParamString = addPostParam(ParamString, "sex", sexValue);
		ParamString = addPostParam(ParamString, "address", address);
		ParamString = addPostParam(ParamString, "zipCode", zipCode);
		ParamString = addPostParam(ParamString, "way", wayValue);
		ParamString = addPostParam(ParamString, "remarks", remarks);
		getScriptByPost(WEBROOT + "/orderService.php", ParamString, "submitOrderSuccess(null)");
		break;
	}
}

function stepGoBack(step){
	switch(step){
		case 1 :
			document.getElementById("stepBox_1").style.display = "block";
			document.getElementById("stepBox_2").style.display = "none";
			copyTable("cartab_1","cartab","cartab_2");
			resetErrorInfo("carinfo");
			break;
		case 2 :
			document.getElementById("stepBox_2").style.display = "block";
			document.getElementById("stepBox_3").style.display = "none";
			copyTable("cartab_2","cartab","cartab_3");
			break;
	}
}

//用srcTable内容代替destTable，并将srcTable重建为id为newSrcId的table
function copyTable(destId, srcId, newSrcId){
	var destTable = document.getElementById(destId);
	var srcTable = document.getElementById(srcId);
	if(destTable != null && typeof(destTable) == "object"
		&& srcTable != null && typeof(srcTable) == "object"){
		var html = srcTable.outerHTML;
		srcTable.outerHTML = "<table width=\"100%\" border=\"0\" class=\"car_prolist\" cellspacing=\"1\" id=" + newSrcId + "><tr><th></th></tr></table>";
		destTable.outerHTML = html;
	}
}

function Sleep(obj,iMinSecond){
	if (window.eventList==null)
		window.eventList=new Array();
	var ind=-1;
	for (var i=0;i<window.eventList.length;i++){
		if (window.eventList[i]==null){
			window.eventList[i]=obj;
			ind=i;
			break;
		}
	}
	if(ind==-1){
		ind=window.eventList.length;
		window.eventList[ind]=obj;
	}
	setTimeout("GoOn(" + ind + ")",iMinSecond);
}
function GoOn(ind){
	var obj=window.eventList[ind];
	window.eventList[ind]=null;
	if (obj.NextStep) obj.NextStep();
	else obj();
}
  
  
function Test()   
{   
   alert("sleep");   
   Sleep(this,10000);   
   this.NextStep=function()   
   {   
   alert("continue");   
   }   
}    

function addPostParam(sParams, sParamName, sParamValue){
    if(sParams.length > 0){sParams += "&"};
    return sParams + encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
}

function CreateXmlHttp(){
	if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}else if (window.XMLHttpRequest){
		return new XMLHttpRequest();
	}
}

function submitOrderSuccess(processAfter){
	//if(XmlHttpObject.readyState == 4){
	//	if(XmlHttpObject.status == 200){
	//		var response = XmlHttpObject.responsetext;
	//		evalCode(response);
	//		if(processAfter != null){
	//			evalCode(processAfter);
	//		}
	//	}
	//}
	if(processAfter != null){
		evalCode(processAfter);
	}
}

function getScriptByPost(targetURL, ParamString, stateEvent){
	//XmlHttpObject.open("post", targetURL, true);
	//XmlHttpObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); //设置服务器响应请求体参数
	//XmlHttpObject.onreadystatechange = function(){evalCode(stateEvent);};
	//XmlHttpObject.send(ParamString);
	var objDynamic = document.createElement("script");
    objDynamic.src = targetURL + "?" + ParamString;
    objDynamic.type = "text/javascript";
    objDynamic.language = "javascript";
	if(Sys.ie) {
		objDynamic.onreadystatechange = function() {
			var state = this.readyState;
			if (state == "loaded" || state == "interactive" || state == "complete") {
				evalCode(stateEvent);
			}
		};
	}else if(Sys.firefox) {
		objDynamic.onload = function() { 
			evalCode(stateEvent);
		};
	}
    document.getElementsByTagName("head")[0].appendChild(objDynamic); //将创建的对象插入到HEAD节中
}

/***********用户中心**************/
function changeUserHomeTab(id, len, obj){
	if(isNaN(id) || isNaN(len)) return;
	var iid = parseInt(id, 10);
	var ilen = parseInt(len, 10);
	if(iid > ilen || iid < 0) return;
	 
	for(var i=1; i<=ilen; i++){
		var box = document.getElementById("contentBox_" + i);
		var menu = document.getElementById("menu_" + i);
		if(box != null && typeof(box) == "object"){
			if(i == iid){
				box.style.display = 'block';
				menu.className = 'c';
			}else{
				box.style.display = 'none';
				menu.className = '';
			}
		}
	}
}

function getUserOrders(){
	var customerid = getIndexCookie('LGID');
	if(customerid == null || customerid == ""){
		goCustomerLogin();
		return;
	}
	var shopid = document.getElementById("shopid").value;
	if(shopid == null || shopid == ""){
		setErrorInfo("userInfoBox", "无法获取店铺信息，请刷新后重试！");
		return;
	}
	var ParamString = "";
	ParamString = addPostParam(ParamString, "opr", "query");
	ParamString = addPostParam(ParamString, "sid", shopid);
	ParamString = addPostParam(ParamString, "cid", customerid);
	getScriptByPost(WEBROOT + "/orderService.php", ParamString, "submitOrderSuccess('initUserOrders()')");
}

function initUserOrders(){
	if(query != null && typeof(query) == "object"){
		var orderContent = document.getElementById("contentBox_1");
		if(orderContent != null && typeof(orderContent) == "object"){
			var userInfoBox = document.getElementById("userInfoBox");
			if(userInfoBox != null && typeof(userInfoBox) == "object"){
				var userInfo = "<ul>\r\n"
							 + "<li class=\"h\">欢迎回到您的帐户</li>\r\n"
							 + "<li class=\"name\">用户：<B>" + checkCustomerLogin() + "</B><span><a href=\"javascript:customerLogout()\">[安全退出]</a></span></li>\r\n"
							 + "<li class=\"m\">最近一年内的合格购物金额为<B> ￥" + query.billThisYear + "</B></li>\r\n"
							 + "</ul>\r\n";
				userInfoBox.innerHTML = userInfo;
			}
			var orders = query.orders;
			if(orders != "" && typeof(orders) == "object"){
				var shoppingInfoBox = document.getElementById("shoppingInfoBox");
				if(shoppingInfoBox != null && typeof(shoppingInfoBox) == "object"){
					var shoppingInfo = "";
					for(var i=1; i<orders.length; i++){
						if(orders[i].status == 2){
							shoppingInfo += "<li class=\"m\">您的订单<B>" + orders[i].name + "</B>已于" + ordre[i].modifyTime + "发货，请注意查收。</li>\r\n";
						}
					}
					if(shoppingInfo.length > 0){
						shoppingInfo = "<ul>\r\n" + shoppingInfo + "</ul>\r\n";
					}
					shoppingInfoBox.innerHTML = shoppingInfo;
				}
				var orderListBox = document.getElementById("orderListBox");
				if(orderListBox != null && typeof(orderListBox) == "object"){
					var table = document.createElement("table");
					table.className = "biaodan";
					table.width = "100%";
					table.border = 1;
					table.borderColor = "#d5d5d5";
					table.cellPadding = 0;
					table.cellSpacing = 0;
					table.style.borderCollapse = "collapse";
					var title = table.insertRow(table.rows.length);
					var th = title.insertCell(-1);
					th.innerHTML = "订单号";
					th.noWrap = true;
					th = title.insertCell(-1);
					th.innerHTML = "收货人";
					th.noWrap = true;
					th = title.insertCell(-1);
					th.innerHTML = "付款方式";
					th.noWrap = true;
					th = title.insertCell(-1);
					th.innerHTML = "商品";
					th.noWrap = true;
					th = title.insertCell(-1);
					th.innerHTML = "订单总金额";
					th.noWrap = true;
					th = title.insertCell(-1);
					th.innerHTML = "订单状态";
					th.noWrap = true;
					th = title.insertCell(-1);
					th.innerHTML = "下单时间";
					th.noWrap = true;
					th = title.insertCell(-1);
					th.innerHTML = "店铺";
					th.noWrap = true;
					for(var i=0; i<orders.length; i++){
						var row = table.insertRow(table.rows.length);
						var order = orders[i];
						var items = order.items;
						var td = row.insertCell(-1);
						td.innerHTML = order.name;
						td.noWrap = true;
						td = row.insertCell(-1);
						td.innerHTML = order.consignee;
						td.noWrap = true;
						td = row.insertCell(-1);
						td.innerHTML = order.payment;
						td.noWrap = true;
						td = row.insertCell(-1);
						td.innerHTML = "";
						if(items != "" && typeof(items) == "object"){
							var goods = "";
							for(var j=0; j<items.length; j++){
								var item = items[j];
								goods += "<li><a href='/goods/goods_" + item.id + ".html' target='_blank' title='" + item.name + "'>" + item.name.substring(0,5) + "...</a> ×" + item.number + "</li>\r\n";
							}
							if(goods.length > 0){
								goods = "<ur>\r\n" + goods + "</ur>\r\n";
							}
							td.innerHTML = goods;
						}
						td.noWrap = true;
						td = row.insertCell(-1);
						td.innerHTML = order.bill;
						td.noWrap = true;
						td = row.insertCell(-1);
						td.innerHTML = order.statusString;
						td.noWrap = true;
						td = row.insertCell(-1);
						td.innerHTML = order.orderTime;
						td.noWrap = true;
						td = row.insertCell(-1);
						td.innerHTML = order.shop;
						td.noWrap = true;
					}
					orderListBox.innerHTML = table.outerHTML;
				}
			}
		}
	}
}

function getUserAddresses(){
	var customerid = getIndexCookie('LGID');
	if(customerid == null || customerid == ""){
		return;
	}
	var ParamString = "";
	ParamString = addPostParam(ParamString, "opr", "addr");
	ParamString = addPostParam(ParamString, "cid", customerid);
	getScriptByPost(WEBROOT + "/customer.php", ParamString, "submitOrderSuccess('initUserAddresses()')");
}

function initUserAddresses(){
	if(address != null && typeof(address) == "object" && address.length > 0){
		var addressContent = document.getElementById("contentBox_2");
		if(addressContent != null && typeof(addressContent) == "object"){
			var addressListBox = document.getElementById("addressListBox");
			if(addressListBox != null && typeof(addressListBox) == "object"){
				var table = document.createElement("table");
				table.className = "biaodan";
				table.width = "100%";
				table.borderColor = "#d5d5d5";
				table.cellPadding = 0;
				table.cellSpacing = 0;
				table.style.borderCollapse = "collapse";
				table.style.border = "0px";
				var row = table.insertRow(table.rows.length);
				for(var i=0; i<address.length; i++){
					if(i > 0 && i%4 == 0){
						row = table.insertRow(table.rows.length);
					}
					var td = row.insertCell(-1);
					var html = "<input type='radio' name='address' value='" + i + "' onclick='showAddressInfo(this.value)'/>"
							 + address[i].consignee + "&nbsp;&nbsp;&nbsp;&nbsp;" + address[i].address.substring(0,5);
					td.width = "25%";
					td.innerHTML = html;
				}
				if(address.length%4 > 0){
					for(var i=0; i<(4-address.length%4); i++){
						var td = row.insertCell(-1);
						var html = "&nbsp;";
						td.width = "25%";
						td.innerHTML = html;
					}
				}
				addressListBox.innerHTML = table.outerHTML;
			}
		}
	}
}

function showAddressInfo(id){
	if(address != null && typeof(address) == "object"){
		var addressContent = document.getElementById("contentBox_2");
		if(addressContent != null && typeof(addressContent) == "object"){
			var addressInfoBox = document.getElementById("addressInfoBox");
			if(addressInfoBox != null && typeof(addressInfoBox) == "object"){
				var addr = address[id];
				if(addr != null && typeof(addr) == "object"){
					document.getElementById("addressID").value = addr.id;
					document.getElementById("addressID").disabled = true;
					document.getElementById("consignee").value = addr.consignee;
					document.getElementById("consignee").disabled = true;
					document.getElementById("phone").value = addr.phone;
					document.getElementById("phone").disabled = true;
					document.getElementById("mobile").value = addr.mobile;
					document.getElementById("mobile").disabled = true;
					document.getElementById("addressdesc").value = addr.address;
					document.getElementById("addressdesc").disabled = true;
					document.getElementById("zipCode").value = addr.zipcode;
					document.getElementById("zipCode").disabled = true;
					var sex = document.getElementsByName("sex");
					for(var i=0; i<sex.length; i++){
						sex[i].disabled = true;
						if(addr.sex == sex[i].value){
							sex[i].checked = true;
						}
					}
				}
			}
		}
	}
}

/***************搜索**************/
function tosearch()
{
	window.document.location.href = getHttpRoot()+"search.html?s="+$_("searchname").value;
}
function s_selectAll(theField) 
{
  var evalobj = document.getElementById(theField);
  evalobj.select();
}
function s_getQueryStr(str)
{
    var LocString=String(window.document.location.href);
    var rs = new RegExp("(^|)"+str+"=([^\&]*)(\&|$)","gi").exec(LocString), tmp;
    if(tmp=rs){
		return tmp[2];
	}
 	return "";
 }


function evalCode(code){
	if(Sys.firefox){
		window.eval(code); 
	}else{
		execScript(code);
	}
}
//判断浏览器类型
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
window.ActiveXObject ? Sys.ie = ua.match(/msie ([\d.]+)/)[1] :
document.getBoxObjectFor ? Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1] :
window.MessageEvent && !document.getBoxObjectFor ? Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1] :
window.opera ? Sys.opera = ua.match(/opera.([\d.]+)/)[1] :
window.openDatabase ? Sys.safari = ua.match(/version\/([\d.]+)/)[1] : 0;
//以下测试案例
//if(Sys.ie) document.write('IE: '+Sys.ie);
//if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
//if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
//if(Sys.opera) document.write('Opera: '+Sys.opera);
//if(Sys.safari) document.write('Safari: '+Sys.safari);

function getHttpRoot(){
	return "http://" + window.location.href.split("//")[1].split("/")[0] + "/";
}
function loadgoodsremark(goodsid,p,ps){
	var ParamString = "";
	ParamString = addPostParam(ParamString, "action", "loadremark");
	ParamString = addPostParam(ParamString, "gid", goodsid);
	ParamString = addPostParam(ParamString, "p", p);
	ParamString = addPostParam(ParamString, "ps", ps);
	getScriptByPost(WEBROOT + "/remarkService.php", ParamString, "submitOrderSuccess(null)");
}

function addremarkdo(goodsid,userid,remark){
	var ParamString = "";
	ParamString = addPostParam(ParamString, "action", "addremark");
	ParamString = addPostParam(ParamString, "pub_goodsid", goodsid);
	ParamString = addPostParam(ParamString, "pub_userid", userid);
	ParamString = addPostParam(ParamString, "pubremark", remark);
	getScriptByPost(WEBROOT + "/remarkService.php", ParamString, "submitOrderSuccess('addremarkok()')");
}
function searchdo(){
    var ParamString = "";
    var shopid = s_getQueryStr("s_id");
    var pageSize = s_getQueryStr("pageSize");
    var pageNum = s_getQueryStr("pageNum");
    var searchname = s_getQueryStr("s");
    if(shopid == null || shopid == "")
	    shopid = $_("s_id").value;
    if(pageSize == null || pageSize == "")
        pageSize = $_("pageSize").value;
    if(pageNum == null || pageNum == "")
        pageNum = $_("pageNum").value;
    if(searchname == null || searchname == "")
        searchname = $_("searchname").value;
	ParamString = addPostParam(ParamString, "s_id", shopid);
	ParamString = addPostParam(ParamString, "pageSize", pageSize);
	ParamString = addPostParam(ParamString, "pageNum", pageNum);
    ParamString = addPostParam(ParamString, "searchname", searchname);
	getScriptByPost(WEBROOT + "/search.php", ParamString, "submitOrderSuccess(null)");
}

function goShoppingCar()
{
	window.location.href = getHttpRoot() + "shoppingCart.html";
}

function goCustomerLogin()
{
	window.location.href = getHttpRoot() + "customerLogin.html";
}

/*******************注册**********************/
function _regsubmit()
{
	if(!checkemail($_("email").value)) return;
	if(!checkpasswd($_("passwd1").value)) return;
	if($_("passwd2").value == ""){
		alert("请再输入一遍密码！");$_("passwd2").focus();return false;
	}
	if($_("passwd1").value != $_("passwd2").value){
		alert("两次输入的密码不一致！");$_("passwd2").focus();return false;
	}
	if($_("realname").value == ""){
		alert("真实姓名不能为空！");$_("realname").focus();return false;
	}
	if(!isNumber($_("mobile").value)) return;
	var username = $_("realname").value;
	var password1 = $_("passwd1").value;
    var password2 = $_("passwd2").value;
	var email = $_("email").value;
	var mobile = $_("mobile").value;
	var phone = $_("phone").value;
	var address = $_("address").value;
    if($_("identify").value == ""){alert("请输入验证码");$_("identify").focus(); return false;}
	var s = document.createElement("script");
    s.src=WEBROOT+"/regist.php?opr=register&username="+username+"&p1="+password1+
    "&email="+email+"&mobile="+mobile+"&phone="+phone+"&address="+address+"&identify="+$_("identify").value+"&p2="+password2;
    document.body.appendChild(s)
	$_("_submit").disabled = true;
}

function isNumber(oNum){
  if($_("mobile").value.length != 11 || !oNum){
  	alert("请输入长度为11位数字！");$_("mobile").focus();return false;
  } 
  var strP=/^\d+(\.\d+)?$/; 
  if(!strP.test(oNum)) {alert("请输入数字！");$_("mobile").focus();return false;}
  try{
  	if(parseFloat(oNum)!=oNum){ alert("请输入数字！");$_("mobile").focus();return false;} 
  } catch(ex){
   return false; 
  }
  return true;
}
function checkemail(str){
	var email = str; 
 	var pattern = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; 
 	flag = pattern.test(email); 
 	if(!flag){ 
  		alert("email格式不正确！"); 
  	$_("email").focus(); 
  	return false; 
 }
 return (true);
}
function checkpasswd(str){
	if(str == ""){
		alert("请输入密码！");$_("passwd1").focus();return false;
	}
	var reg = /^[a-zA-Z0-9]+$/;
	if (! reg.test(str)){
	 alert("密码只能为数字和字母！");$_("passwd1").focus();return false;
	}
	if (str.length < 6 || str.length > 16){
	 alert("密码长度6-16！");$_("passwd1").focus(); return false;
	}
	return true;
}

/****************导航栏特效*******************/
function mHoverLi(n)
{
    var navcount = parseInt($_("navcount").value);
    $_('mtb_'+n).className='menuli_hover';
}
function oHoverLi(n)
{
   var navcount = parseInt($_("navcount").value);
   for(var a=1;a<=navcount;a++)
    {
        $_('mtb_'+a).className='menuli_link';
    }
}
//如果要做成点击后再转到请将<li>中的onmouseover 改成 onclick; 

/**************************************************
将字符串maintext复制到剪贴板
**************************************************/ 
function setClipboard(maintext) {
	if (window.clipboardData) {
		return (window.clipboardData.setData("Text", maintext));
	}else if (window.netscape) {
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=maintext;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
		return true;
	}
	return false;
}
/**************************************************
返回剪贴板的内容
**************************************************/
function getClipboard() {
	if (window.clipboardData) {
		return(window.clipboardData.getData('Text'));
	}else if (window.netscape) {
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		clip.getData(trans,clip.kGlobalClipboard);
		var str = new Object();
		var len = new Object();
		try {
			trans.getTransferData('text/unicode',str,len);
		}catch(error) {
			return null;
		}
		if (str) {
			if (Components.interfaces.nsISupportsWString) str=str.value.QueryInterface(Components.interfaces.nsISupportsWString);
			else if (Components.interfaces.nsISupportsString) str=str.value.QueryInterface(Components.interfaces.nsISupportsString);
			else str = null;
		}
		if (str) {
			return(str.data.substring(0,len.value / 2));
		}
	}
	return null;
} 

<!--   
if(typeof(HTMLElement)!="undefined"   &&   !window.opera){   
    HTMLElement.prototype.__defineGetter__("outerHTML",function(){   
        var a=this.attributes, str="<"+this.tagName, i=0;
        for(; i<a.length; i++)   
        	if(a[i].specified)
        		str+=" "+a[i].name+'="'+a[i].value+'"';   
        if(!this.canHaveChildren)   
        	return str + "/>";   
        return str+">" + this.innerHTML + "</" + this.tagName + ">";   
    });   
    HTMLElement.prototype.__defineSetter__("outerHTML",function(s){   
        var d = document.createElement("DIV");
        d.innerHTML = s;
        for(var i=0; i<d.childNodes.length; i++)   
        	this.parentNode.insertBefore(d.childNodes[i], this);   
        this.parentNode.removeChild(this);
    });   
    HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){   
        return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase());   
    });   
}   
//-->   

function getPageContentHtml(stc,pn,ps,ref)
{
    var ParamString = "";
    ParamString = addPostParam(ParamString, "stc", stc);
    ParamString = addPostParam(ParamString, "pn", pn);
    ParamString = addPostParam(ParamString, "ps", ps);
	ParamString = addPostParam(ParamString, "ref", ref);
    getScriptByPost(WEBROOT + "/pagelist.php", ParamString, "submitOrderSuccess(null)");
}