// JavaScript Document

//by lyh 08.06.29

//去空格
function trim(s) {
	s = s.replace(/^\s*(.*?)\s*$/ig, '$1');
	return (s);
}

function trims(s) {
	s = s.replace(/(\s)*(&nbsp;)*/ig, "");
	return (s);

}

//字符长度控制
function textCounter(obj, num) {
	var objValue = $(obj).value;
	if (objValue.length > num) {
		$(obj).value = $(obj).value.substring(0, num);
		alert("内容超出300字");
	}
}

//商品收藏
function goodsCollHandle(goodsId, userId) {
	if (goodsId == '0' || trim(goodsId) == '') {
		alert("请选择商品！");
		return false;
	}

	if (userId == '0' || trim(userId) == '') {
		location.href = '/home/login.php';
		return false;
	}

	var url = "/goods/goodsCollHandle.php?";
	var pars = 'goodsId=' + goodsId + '&userId=' + userId + '&'
			+ new Date().getTime();
	var myAjax = new Ajax.Request(url, {
		method : 'get',
		parameters : pars,
		onComplete : showColl
	});
}

function showColl(response) {
	var o = response.responseText;
	alert(o);
}

//商品购买
function buyCheck(nums, goodsId, no, name, shopPrice, weight, isAbate,attrVal,option,sId,store,isShowNewAttr) {
	if(option != '' || isShowNewAttr == '1'){
		alert("请选择颜色、尺码及数量");
		window.open("/goods/goodsView.php?goodsId="+goodsId);
		return false;
	}
	
	if(checkAttr()){
		var attrVal=trims(attrVal);
		attrVal=attrVal.substring(1);
		if(nums <= '0'){
			alert("库存不足！");
			return false;
		}else{
			var buyNums = $("buyNums");
			var goodsNums = '1';
			if(buyNums){
				goodsNums = buyNums.value;
			}
			window.open("/goods/buyHandle.php?type=addCart&goodsId="+goodsId+"&goodsNo="+no+"&goodsNums="+goodsNums+"&goodsName="+encodeURIComponent(name)+"&shopPrice="+shopPrice+"&weight="+weight+"&isAbate="+isAbate+"&attrmode="+encodeURIComponent(attrVal)+"&slaveId="+sId);	
		}
	}
}

//显示评论
function showComm(page, oDiv) {

	var request_url = page;
	var request_pars = '';
	var _oDiv = document.getElementById(oDiv);
	var myAjax = new Ajax.Updater(_oDiv, request_url, {
		method : "get",
		evalScripts : true,
		parameters : request_pars
	});

}

//商品评论
function checkGoodsComm(goodsId, userId, commUser, isComment) {
	var content = document.getElementById("content").value;
	if (userId == '0') {
		alert("对不起,用户只能登陆后才能对商品进行评论");
		location.href = "/home/login.php";
		return false;
	}
	if (commUser == 2) {
		if (isComment != 2) {
			alert("对不起,只能购买过此商品的会员才可发表评论（并且订单状态为已支付状态）");
			return false;
		}
	}
	if (trim(content) == '') {
		alert("请输入留言内容！");
		return false;
	}

	commentHandle("handle/goodsCommHandle.php", goodsId, userId, content);
}

function commentHandle(page, goodsId, userId, content) {
	var page = page;
	var pars = 'goodsId=' + goodsId + '&' + 'userId=' + userId + '&'
			+ 'content=' + encodeURIComponent(content) + '&'
			+ new Date().getTime();
	var myAjax = new Ajax.Request(page, {
		method : 'get',
		parameters : pars,
		onComplete : commResult
	});
}

function commResult(response) {
	var re = response.responseText;

	if (re == "ERROR_GOODS_COMM_SUCC") {
		document.getElementById("content").value = "";
		var goodsId = document.getElementById("goodsId").value
		showComm(
				"showComm.php?goodsId=" + goodsId + '&' + new Date().getTime(),
				'showComm');
	}

}

function setPicSize(width, imgObj) {
	var imgObj = document.getElementById(imgObj);
	if (width > 300) {
		imgObj.height = imgObj.height / (width / 300);
		imgObj.width = 300;
	}
}

//交提在线调查
function bacHandle(number) {

	var flag = false;

	var number = number - 1;

	var online = document.onlines[number].onLine;
	if (typeof (online) == 'undefined') {
		online = document.onlines.onLine;
		for ($i = 0; $i < online.length; $i++) {
			if (online[$i].checked) {
				var onlineId = online[$i].value;
				flag = true;
			}
		}
	} else {
		for ($i = 0; $i < online.length; $i++) {
			if (online[$i].checked) {
				var onlineId = online[$i].value;
				flag = true;
			}
		}
	}

	if (flag == false) {
		alert('您好，请选择选项！');
		return false;
	}

	var url = "/goods/handle/onlineHandle.php?online=" + onlineId + '&'
			+ new Date().getTime();
	var pars = '';
	var myAjax = new Ajax.Request(url, {
		method : 'get',
		parameters : pars,
		onComplete : show
	});
}

function show(response) {

	var lastId = response.responseText;

	if (lastId == 'go') {
		alert('投票成功，谢谢您对我们的支持！');
	}
	if (lastId == 'fal') {
		alert('对不起，不能重复投票！');
	}

}

//商品详细页购买时传递商品属性值
var attrmode = ""
function attrfun(total) {

	for (i = 0; i < total; i++) {
		if (document.getElementById('modeid' + i) != null) {
			attrmode = attrmode + "+"
					+ document.getElementById('modeid' + i).innerHTML
		}
		if (document.getElementById('modeids' + i) != null) {
			attrmode = attrmode + "+"
					+ document.getElementById('modeids' + i).value
		}
	}
}

//商品详细页购买时清空商品属性值
function disattr() {
	attrmode = ""
}

//删除购物车商品
function delCart(goodsId, attrMode) {
	location.href = 'buyHandle.php?type=delCart&goodsId=' + goodsId
			+ '&attrmode=' + encodeURIComponent(attrMode);

}

//修改购物车商品数量
function modNums(goodsId, shopPrice, weight, attrmode) {
	var nums = $("nums" + goodsId + attrmode).value;
	var pattern = /[^0-9]/g;
	if (pattern.test(nums) || nums < 1) {
		nums = '1';
		$("nums" + goodsId).value = '1';
	}
	var url = "buyHandle.php?";
	var pars = 'type=modCart&goodsId=' + goodsId + '&goodsNums=' + nums
			+ '&shopPrice=' + shopPrice + '&weight=' + weight + '&attrmode='
			+ encodeURIComponent(attrmode);

	var myAjax = new Ajax.Request(url, {
		method : 'get',
		parameters : pars,
		onComplete : showPrice
	});

}
function showPrice(response) {
	var text = response.responseText;

	if (text == 'GOODS_IS_LACK') {
		alert("库存不足，请减少数量！");
		return false;
	}
	var textArr = text.split(',');

	var goodsTotalPrice = parseFloat(textArr[1]);

	$(textArr[0] + textArr[2]).innerHTML = "￥" + goodsTotalPrice.toFixed(2);

	var tableObj = $("cartList");

	var tr = tableObj.getElementsByTagName("tr");
	var cartTotal = '0.00';

	for (i = 0; i < tr.length; i++) {
		cartTotal = parseFloat(cartTotal)
				+ parseFloat($("goodsTotal" + i).innerText.substring(1));
	}

	$("cartTotal").innerHTML = "￥" + parseFloat(cartTotal).toFixed(2);

}

//begin cartCreateOrder.tpl.html

// 选择优惠券
function selCoup() {
	var canceDiv = $("canceDiv");
	canceDiv.style.display = '';

	canceDiv.style.position = "absolute";

	if (!window.XMLHttpRequest) {//ie6
		canceDiv.style.left = (document.documentElement.clientWidth - canceDiv.clientWidth)
				/ 2 + document.documentElement.scrollLeft + "px";
		canceDiv.style.top = (document.documentElement.clientHeight - canceDiv.clientHeight)
				/ 2 + document.documentElement.scrollTop + "px";
	} else {//IE 7,mozilla
		canceDiv.style.left = (document.body.clientWidth - canceDiv.clientWidth)
				/ 2 + document.body.scrollLeft + "px";
		canceDiv.style.top = (document.body.clientHeight - canceDiv.clientHeight)
				/ 2 + document.body.scrollTop + "px";
	}
}

function selCoupS(obj) {
	$("coupSn").value = obj.value;
	colseCanceDiv();
}

function colseCanceDiv() {
	var canceDiv = $("canceDiv");
	canceDiv.style.display = 'none';
}
//使用优惠券
function useCoup() {
	var coupSn = $("coupSn").value;

	var orderPrice = $("orderPriceDiv").innerHTML.substring(1);

	var coupInfoTr = $("coupInfoTr");
	var coupInfo = $("coupInfo");
	if (trim(coupSn) == '') {
		coupInfoTr.style.display = '';
		coupInfo.innerHTML = '请输入优惠券号码';
		$("coupMoneyDiv").innerHTML = '￥0.00';
		return false;
	}

	//验证是否可使用
	var page = "handle/coupCheckHandle.php?";
	var pars = 'coupSn=' + encodeURIComponent(coupSn) + '&orderPrice='
			+ orderPrice + '&' + new Date().getTime();
	var myAjax = new Ajax.Request(page, {
		method : 'get',
		parameters : pars,
		onComplete : checkRs
	});
}

function checkRs(response) {
	var info = response.responseText;
	var infoArr = info.split(',');

	var coupInfoTr = $("coupInfoTr");
	var coupInfo = $("coupInfo");
	var coupMoney = $("coupMoney");
	var payMoney = $("payMoney");

	var infoS = '';
	if (infoArr[0] == '1') {
		infoS = '该优惠券不存在'
	}
	if (infoArr[0] == '2') {
		infoS = '该优惠券未启用'
	}
	if (infoArr[0] == '3') {
		infoS = '该优惠券已被使用'
	}
	if (infoArr[0] == '4') {
		infoS = '该优惠券已过期'
	}
	if (infoArr[0] == '5') {
		infoS = '订单金额不足'
	}
	if (infoArr[0] == '0') {
		infoS = '优惠券可使用'
	}

	coupMoneyDiv.innerHTML = '￥' + infoArr[1];
	coupMoney.value = infoArr[1];

	var orderPrice = parseFloat($("orderPriceDiv").innerHTML.substring(1));

	var advPayMoney = $("advPayMoney").innerHTML.substring(1);
	if (parseFloat(advPayMoney) > parseFloat(orderPrice)) {
		$("advPayMoney").innerHTML = '￥' + (parseFloat(advPayMoney) - (parseFloat(infoArr[1])))
				.toFixed(2)
	}

	payMoney.innerHTML = '￥' + (parseFloat(orderPrice) - (parseFloat(infoArr[1]) + parseFloat($("advPayMoney").innerHTML
			.substring(1)))).toFixed(2);
	if (parseFloat(payMoney.innerHTML.substring(1)) <= 0) {
		payMoney.innerHTML = '￥0.00';
	}

	coupInfoTr.style.display = '';
	coupInfo.innerHTML = infoS;

}

//在前台显示使用预存款支付之后还需支付金额
function setAdvance(myMoney) {
	var useAdvance = $("useAdvance");
	var orderPriceDiv = $("orderPriceDiv").innerHTML;
	var orderPrice = parseFloat(orderPriceDiv.substring(1));

	if (useAdvance.checked == true) {
		if (myMoney < orderPrice) {//预存款不足
			var payMoney = parseFloat(orderPrice) - parseFloat(myMoney)
					- parseFloat($("coupMoneyDiv").innerHTML.substring(1));

			$("payMoney").innerHTML = "￥" + payMoney.toFixed(2);
		} else {//预存款足够

			var payModeId = document.form1.payModeId;
			for ( var i = 0; i < payModeId.length; i++) {
				payModeId[i].checked = false;
			}
			var payPriceDiv = parseFloat($("payPriceDiv").innerHTML
					.substring(1));
			orderPrice = parseFloat(orderPrice) - parseFloat(payPriceDiv);

			$("orderPriceDiv").innerHTML = "￥" + orderPrice.toFixed(2);

			$("payPriceDiv").innerHTML = "￥0.00";

			$("payMoney").innerHTML = "￥0.00";
		}

	} else {
		$("payMoney").innerHTML = "￥"
				+ (parseFloat(orderPrice) - parseFloat($("coupMoneyDiv").innerHTML
						.substring(1))).toFixed(2);
	}

	$("advPayMoney").innerHTML = "￥"
			+ (parseFloat(orderPriceDiv.substring(1))
					- parseFloat($("payMoney").innerHTML.substring(1)) - parseFloat($("coupMoneyDiv").innerHTML
					.substring(1))).toFixed(2);

}

//支付费用
function setPayPrice(payPrice) {

	$("payPriceDiv").innerHTML = "￥" + parseFloat(payPrice).toFixed(2);

	var cartTotalPrice = parseFloat($("cartTotalPrice").innerHTML.substring(1));
	var deliPrice = parseFloat($("deliPriceDiv").innerHTML.substring(1));
	var protPrice = parseFloat($("protPriceDiv").innerHTML.substring(1));
	var orderPrice = cartTotalPrice + deliPrice + protPrice
			+ parseFloat(payPrice);

	$("orderPriceDiv").innerHTML = "￥" + parseFloat(orderPrice).toFixed(2);// 订单总金额

	// 还需要支付金额
	var useAdvance = $("useAdvance");
	var myMoney = $("myMoney").value;
	if (useAdvance.checked == true) {
		if (parseFloat(myMoney) < parseFloat(orderPrice)) {
			var payMoney = parseFloat(orderPrice) - parseFloat(myMoney)
					- parseFloat($("coupMoneyDiv").innerHTML.substring(1));
			$("payMoney").innerHTML = "￥" + payMoney.toFixed(2);
		} else {
			$("payMoney").innerHTML = "￥0.00";
		}
	} else {
		$("payMoney").innerHTML = "￥"
				+ (parseFloat(orderPrice) - parseFloat($("coupMoneyDiv").innerHTML
						.substring(1))).toFixed(2);
	}

	$("advPayMoney").innerHTML = "￥"
			+ (parseFloat($("orderPriceDiv").innerHTML.substring(1))
					- parseFloat($("payMoney").innerHTML.substring(1)) - parseFloat($("coupMoneyDiv").innerHTML
					.substring(1))).toFixed(2);

	// 如果预存款够的情况下，取消预存款
	var payMoney = $("payMoney").innerHTML.substring(1);
	var advPayMoney = $("advPayMoney").innerHTML.substring(1);
	if (parseFloat(payMoney) == 0.00) {
		useAdvance.checked = false;
		$("payMoney").innerHTML = "￥" + advPayMoney;
		$("advPayMoney").innerHTML = "￥0.00";
	}
}

//收货人信息验证
function checkCartInfo() {
	var consignee = $("consignee").value;
	var email = $("email").value;
	var tel = $("tel").value;
	var mobile = $("mobile").value;
	var address = $("address").value;
	var zipcode = $("zipcode").value;
	var province = $("province").value;
	var remark = $("remark").value;

	if (trim(consignee) == '') {
		alert("收货人姓名不能为空！");
		return false;
	}

	if (consignee.length > 15) {
		alert('收货人姓名过长，请将控制在15字以内，现在的长度是：' + consignee.length + '个字！');
		form1.consignee.focus();
		return false;
	}

	if (trim(address) == '') {
		alert("收货人地址不能为空！");
		return false;
	}

	if (address.length > 100) {
		alert('收货人地址过长，请将控制在100字以内，现在的长度是：' + address.length + '个字！');
		form1.address.focus();
		return false;
	}

	if (trim(zipcode) == '') {
		alert("邮政编码不能为空！");
		return false;
	}
	var pattern1 = /[^0-9]/g;
	if (pattern1.test(zipcode)) {
		alert("邮政编码为数字！");
		return false;
	}

	if (zipcode.length > 10) {
		alert('邮政编码过长，请将控制在10字以内，现在的长度是：' + zipcode.length + '个字！');
		form1.zipcode.focus();
		return false;
	}

	var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (trim(email) != '') {
		if (!pattern.test(email)) {
			alert("邮箱地址格式不正确！");
			return false;
		}
	}

	if (email.length > 50) {
		alert('邮箱地址过长，请将控制在50字以内，现在的长度是：' + email.length + '个字！');
		form1.email.focus();
		return false;
	}

	if (trim(mobile) == '' && trim(tel) == '') {
		alert("请输入任一联系电话！");
		return false;
	}

	if (tel.length > 50) {
		alert('电话号码过长，请将控制在20字以内，现在的长度是：' + tel.length + '个字！');
		form1.tel.focus();
		return false;
	}

	if (mobile.length > 50) {
		alert('手机号码过长，请将控制在20字以内，现在的长度是：' + mobile.length + '个字！');
		form1.mobile.focus();
		return false;
	}

	//alert(province);

	if (province == '0') {
		alert("请选择配送地区！");
		return false;
	}

	var deliveryIds = document.form1.deliveryId;
	if (deliveryIds == undefined) {
		alert("请选择配送方式！");
		return false;
	}
	if (deliveryIds.length == undefined) {
		if (deliveryIds.checked == false) {
			alert("请选择配送方式！");
			return false;
		}
	} else {
		var num = 0;
		for ( var j = 0; j < deliveryIds.length; j++) {
			if (deliveryIds[j].checked) {
				num++;
			}
		}
		if (num == '0') {
			alert("请选择配送方式！");
			return false;
		}
	}

	if (remark.length > 300) {
		alert('备注过长，请将内容控制在300字以内，现在的长度是：' + remark.length + '个字！');
		form1.remark.focus();
		return false;
	}
    
	var payModeId = document.form1.payModeId;
	var num = 0;
	for ( var i = 0; i < payModeId.length; i++) {
		if (payModeId[i].checked) {
			num++;
		}
	}

	var useAdvance = $("useAdvance");
	var payMoney = parseFloat($("payMoney").innerText.substring(1));
	if (useAdvance.checked == false && num == '0') {
		alert("请选择支付方式！");
		return false;
	}

	if (payMoney > 0 && (userType == "黄金代理"||userType == "钻石代理"||userType == "皇冠代理"||userType == "区域代理") ) {
		alert("对不起，您的预存款不足，请续费之后再下订单！如果有疑问，请与客服联系！");
		return false;
	}

	if(payMoney > 0 && num == '0'){
		alert("对不起,你的预付款不够,请再选择一个其他的支付方式!");
		return false;
	}
	return true;
}

//end cartCreateOrder.tpl.html

// begin 用户中心

// 订单列表操作
var cmenu = {
	show : function(obj) {
		cmenu.remove();
		cmenu.base = obj.cloneNode('true');
		cmenu.base.style.display = 'block';

		document.body.appendChild(cmenu.base);
		obj = obj.previousSibling;
		position = cmenu.cumulativeOffset(obj);
		cmenu.base.style.position = "absolute";
		cmenu.base.style.left = position[0] - 50 + 'px';
		cmenu.base.style.top = position[1] + 16 + 'px';
		document.onclick = cmenu.remove;
		cmenu.p = 0;
	},
	cumulativeOffset : function(element) {
		var valueT = 0, valueL = 0;
		do {
			valueT += element.offsetTop || 0;
			valueL += element.offsetLeft || 0;
			element = element.offsetParent;
		} while (element);
		return [ valueL, valueT ];
	},
	remove : function() {
		if (cmenu.base) {
			if (cmenu.p > 0) {
				document.body.removeChild(cmenu.base);
				cmenu.base = null;
			} else
				cmenu.p++;
		}
	}
}

//取消订单
function canceOrder(orderId) {
	if (confirm("确定取消吗？")) {
		location.href = 'orderCanceHandle.php?orderId=' + orderId;
	}
}

function setCance(orderId) {
	var canceDiv = $("canceDiv");
	canceDiv.style.display = '';
	$("remark").value = '';
	$("orderId").value = orderId;

	canceDiv.style.position = "absolute";

	if (!window.XMLHttpRequest) {//ie6
		canceDiv.style.left = (document.documentElement.clientWidth - canceDiv.clientWidth)
				/ 2 + document.documentElement.scrollLeft + "px";
		canceDiv.style.top = (document.documentElement.clientHeight - canceDiv.clientHeight)
				/ 2 + document.documentElement.scrollTop + "px";
	} else {//IE 7,mozilla
		canceDiv.style.left = (document.body.clientWidth - canceDiv.clientWidth)
				/ 2 + document.body.scrollLeft + "px";
		canceDiv.style.top = (document.body.clientHeight - canceDiv.clientHeight)
				/ 2 + document.body.scrollTop + "px";
	}
}

function colseCanceDiv() {
	var canceDiv = $("canceDiv");
	canceDiv.style.display = 'none';
}

function canceCheck() {
	var remark = $("remark").value;
	if (remark.length > 300) {
		alert('备注过长，请将控制在300字以内，现在的长度是：' + remark.length + '个字！');
		form2.remark.focus();
		return false;
	}

	return confirm('确定取消订单吗？');

}

//订单留言验证
function orderMsgCheck() {
	var newMsg = $("newMsg").value;
	var validateCode = $("validateCode").value;
	var msg = '';// $("msg").value;
	var orderId = $("orderId").value;

	if (trim(newMsg) == '') {
		alert("请输入留言信息！");
		return false;
	}

	if (newMsg.length > 300) {
		alert('留言内容过长，请将控制在300字以内，现在的长度是：' + newMsg.length + '个字！');
		return false;
	}

	if (trim(validateCode) == '') {
		alert("请输入验证码！");
		return false;
	}

	orderMsgHandle(orderId, newMsg, msg, validateCode);
}

function orderMsgHandle(orderId, newMsg, msg, validateCode) {
	var url = "handle/orderMsgHandle.php?";

	var pars = 'orderId=' + orderId + '&validateCode=' + validateCode
			+ '&newMsg=' + encodeURIComponent(newMsg) + '&msg='
			+ encodeURIComponent(msg) + '&' + new Date().getTime();
	var myAjax = new Ajax.Request(url, {
		method : 'get',
		parameters : pars,
		onComplete : orderMsgResult
	});
}
function orderMsgResult(response) {
	var text = trim(response.responseText);
	if(text == 'succ'){
		location.reload();
	}else{
		alert(text);	
	}
}

function orderSetPayMode() {
	$("setPayMode").style.display = '';
	parent.autoResize('ifrm');
}

function orderClosePayModeDiv() {
	$("setPayMode").style.display = 'none';
	var payModeId = document.payMode.payModeId;
	for ( var i = 0; i < payModeId.length; i++) {
		payModeId[i].checked = false;
	}
	parent.autoResize('ifrm');
}

function orderSetPayModeCheck() {
	var payModeId = document.payMode.payModeId;
	var num = 0;
	for ( var i = 0; i < payModeId.length; i++) {
		if (payModeId[i].checked) {
			num++;
		}
	}
	if (num <= 0) {
		alert("请选择支付方式！");
		return false;
	}
}
//end 用户中心

