// JavaScript Document
var valNegFloat = /^\d+(\.\d+)?$/;　　//非负浮点数（正浮点数 + 0
var valUser = /^([a-zA-Z]+[_0-9a-zA-Z@.-]*){1,20}$/;  //用户名的正则
var valString = /^[a-zA-Z]\w*$/;  
var valEmail = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;  //email的正则
var valNegInt = /^\d+$/;　　//非负整数（正整数 + 0

// JavaScript Document
var noteAjaxNotFound="Not Found Page !";
var noteAjaxLoading="Loading....";
var noteUsernameError="Username Error!";
var notePasswordError="Password Error!";
var noteUsernameInput = "请填写用户名";

var noteEmailInput = "请填写Email";
var noteUsernameFormatError = "请填写正确格式的用户名";
var notePasswordInput = "请填写密码";
var noteDoublePasswordError = "两次填写密码不一样";
var noteEmailError = "Email格式错误";

var noteCheckUsernameDefeat = "用户名验证发生错误，请重新验证";
var noteCheckEmailDefeat		= "Email验证发生错误，请重新验证";
var noteTextAjaxBoard="info";
var noteMessage = "您的评论已经提交请等待审核，感谢您的参与！";
var noteError = "Abnormal error, please try again later!";

var notekeyWordError="Please enter keywords.";
var noteFavoritesSuccess = "Favorites Success.";
var noteNoLogin = "Sorry, please login first.";
var noteBuyQuantity = "Please enter order quantity.";
var noteSize = "Please select size.";

var noteEmptyCart= "Your Cart is currently empty.";
var noteDelCartItem = "Do you want to delete this goods or the cart?";
var noteUpdateSuccess = "Update Success.";
var noteSubmit = "Please confirm your order information.\n\rif all right pls confirm.";
var noteUserRepeat = "Sorry, that user has been registered, please replace!";

var noteSuccess = "Successful operation!";
var noteFailure = "Failed operation!";
var noteRepeat = "Repeat operation!";
var noteLoginError = "Login failure,Please re-enter!";
var noteFailurePassword = "Original password error,Please re-enter!";

var noteDelItem = "Do you really want to delete?";
var noteCancelItem = "Do you really want to cancel?";
var shoppingUlr = 'shopping.php';

var noteSuccessForgot = "Your password has been sent to your mailbox.";
var noteFailureForgot = "Sorry, can't find your information,Please re-enter!";

function searchWord(id)
{
	if(getId(id).value == "" || getId(id).value == "Search...")
	{
		alert(notekeyWordError);
		getId(id).focus();
		return false;
	}
}

function checkSearchWordFocus()
{
	if(getId("keyWord").value == "Search...")
	{
		getId("keyWord").value = "";
	}
}

function checkSearchWordBlur(){

	if(getId("keyWord").value == "")
	{
		getId("keyWord").value = "Search...";
	}
}

function checkOrderSearch()
{
	if(getId("orderNumber").value == "")
	{
		alert("Please enter Order Number.");
		getId("orderNumber").focus();
		return false;
	}
}

//加入购物车
function addCart()
{	
/*	if(getId("product_attribute").value == "")
	{
		alert(noteSize);
		return false;
	}*/

	//if(getId("quantity").value == "" || getId("quantity").value == 0)
	if(!getId("quantity").value.match(valNegInt))
	{
		alert(noteBuyQuantity);
		return false;
	}
	document.getElementById('cartForm').submit();
}

//AJAX addOther
function addOther(step,id)
{
	$.ajax({
	   type: "POST",
	   url: shoppingUlr,
	   //data: $("#search_form").serialize(),
	   data: "step="+step+"&id="+id+"",
	   success: function(msg){
		if(msg=="ok")
		{
			alert(noteSuccess);
		}else if(msg=="repeat"){
			alert(noteRepeat);			
		}else if(msg=="login"){
			alert(noteNoLogin);
			location='./';
		}else{
			location='./';
		}
	   }
	});
}

//ajax删除记录,如favorites
function delOther(id,postUrl,backUrl)
{
	if(!confirm(noteDelItem))return false;
	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: "act=del&id="+id+"",
	   success: function(msg){
		if(msg=="ok")
		{
			alert(noteSuccess);
			location=backUrl;
		}else{
			alert(noteFailure);
		}
	   }
	});
}

//修改购物车
function updateCart(id)
{
	var quantity = getId('cart_quantity_'+id).value;
	$.ajax({
	   type: "POST",
	   url: shoppingUlr,
	   data: "step=add_to_cart&action=updata&cartId="+id+"&quantity="+quantity,
	   success: function(msg){
		location=shoppingUlr+"?step=cart";
	   }
	});	
}


//清除/删除购物车
function delCart(id)
{
	if(!confirm(noteDelCartItem)) return false;
	
	if(id>0){
		cId = 'cartId='+id;
	}else{
		cId = '';
	}
	
	$.ajax({
	   type: "POST",
	   url: shoppingUlr,
	   data: "step=add_to_cart&action=del&"+cId+"",
	   success: function(msg){
		if(msg=="all")
		{
			alert(noteEmptyCart);
			location='./';
		}else{
			location=shoppingUlr+"?step=cart";
		}
	   }
	});
}



//统计费用
function setFeeTotal(id)
{
	var num = $("#orders_cart_num").val();
	
	$.ajax({
	   type: "POST",
	   url: "ajaxdata.php",
	   data: "id="+id+"&cartNum="+num,
	   success: function(msg){
			$("#feeTotal").removeClass("loading");
			$("#feeTotal").html(msg); 
	   },
	    beforeSend: function(){
			$("#feeTotal").addClass("loading");
			$("#feeTotal").html('<img src="images/loading.gif" alt="Loading..." /> Loading...'); 
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown){
		   	$("#feeTotal").html(textStatus); 
	   }
	});	

}

//取消订单
function cancelOrder(id,postUrl,backUrl)
{
	if(!confirm(noteCancelItem)) return false;
	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: "act=cancel&id="+id+"",
	   success: function(msg){
		if(msg=="ok")
		{
			alert(noteSuccess);
			location=backUrl;
		}else{
			alert(noteFailure);
		}
	   }
	});	
}

//会员登陆
function checkLogin(postUrl,backUrl)
{
	if(getId("loginUsername").value == "" || getId("loginUsername").value == "USERNAME")
	{
		alert("Please enter your username.");
		getId("loginUsername").focus();
		return false;
	}

	if(getId("loginPassword").value == "" || getId("loginPassword").value == "0000")
	{
		alert("Please enter your password.");
		getId("loginPassword").focus();
		return false;
	}

	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: $("#loginForm").serialize(),
	   success: function(msg){
			if(msg=="ok"){
				location = backUrl;
			}else{
				$("#loginButton").attr("disabled",false); 
				$("#loginButton").val("Log in"); 
				alert(noteLoginError);
			}
	   },
	    beforeSend: function(){
			getId("loginButton").disabled = true;
			getId("loginButton").value = "loading";
	   }
	});
	return false;
}

//验证会员注册表单
function checkRegister(str,postUrl,backUrl)
{
	//Your Personal Details	
	if(!getName(str+"_sex")[0].checked && !getName(str+"_sex")[1].checked){
		alert("Please select your Gender.");
		return false;
	}
	
	if(!getId(str+"_username").value.match(valUser))
	{
		alert("Please enter your username.");
		getId(str+"_username").focus();
		return false;
	}
	
	if(getId(str+"_username").value.length<4 || getId(str+"_username").value.length>15)
	{
		alert("Username length must be greater than 4.");
		getId(str+"_username").focus();
		return false;
	}

	if(getId(str+"_first_name").value==""){
		alert("Please enter your first name.");
		getId(str+"_first_name").focus();
		return false;
	}

	if(getId(str+"_last_name").value==""){
		alert("Please enter your last name.");
		getId(str+"_last_name").focus();
		return false;
	}

/*	if(getId(str+"_birthday").value==""){
		alert("Please enter your Date of Birth.");
		getId(str+"_birthday").focus();
		return false;
	}*/

	if(!getId(str+"_email").value.match(valEmail))
	{
		alert("Please enter your email address.!");
		getId(str+"_email").focus();
		return false;
	}

	//Your Password
	if(getId(str+"_password").value == "" || getId(str+"_password").value.length<5  || getId(str+"_password").value.length>20)
	{
		alert("Password length must be greater than 5.");
		getId(str+"_password").focus();
		return false;
	}

	if(getId(str+"_password").value != getId(str+"_password_confirm").value)
	{
		alert("Please enter your confirm password.");
		getId(str+"_password_confirm").focus();
		return false;
	}
	
	//Your Address
/***
 	if(getId(str+"_address").value==""){
		alert("Please enter address.");
		getId(str+"_address").focus();
		return false;
	}

 	if(getId(str+"_zipcode").value==""){
		alert("Please enter zipcode.");
		getId(str+"_zipcode").focus();
		return false;
	}
	
 	if(getId(str+"_city").value==""){
		alert("Please enter City.");
		getId(str+"_city").focus();
		return false;
	}

 	if(getId(str+"_province").value==""){
		alert("Please enter State/Province.");
		getId(str+"_province").focus();
		return false;
	}

 	if(getId(str+"_country").value==""){
		alert("Please select Country.");
		getId(str+"_country").focus();
		return false;
	}

	if(getId(str+"_tel").value==""){
		alert("Please enter your tel.");
		getId(str+"_tel").focus();
		return false;
	}
**/		

	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: $("#registerForm").serialize(),
	   success: function(msg){
		   	getId("regButton").disabled = false;
			$("#loadingInfo").removeClass("loading");
			if(msg=="repeat"){
				$("#loadingInfo").addClass("red");
				$("#loadingInfo").html(noteUserRepeat);
			}else if(msg=="ok"){
				$("#loadingInfo").html(noteSuccess);
				location = backUrl;
			}else{
				$("#loadingInfo").html(msg);	
			}
	   },
	    beforeSend: function(){
			getId("regButton").disabled = true;
			$("#loadingInfo").addClass("loading");
			$("#loadingInfo").html('<img src="images/loading_small.gif" alt="Loading..." /> Loading...'); 
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown){
		   	$("#loadingInfo").html(textStatus); 
	   }
	});	
	return false;
}

//验证会员修改表单
function checkUserInfo(str,postUrl,backUrl)
{
	//Your Personal Details	
	if(!getName(str+"_sex")[0].checked && !getName(str+"_sex")[1].checked){
		alert("Please select your Gender.");
		return false;
	}

	if(getId(str+"_first_name").value==""){
		alert("Please enter your first name.");
		getId(str+"_first_name").focus();
		return false;
	}

	if(getId(str+"_last_name").value==""){
		alert("Please enter your last name.");
		getId(str+"_last_name").focus();
		return false;
	}

	if(getId(str+"_birthday").value==""){
		alert("Please enter your Date of Birth.");
		getId(str+"_birthday").focus();
		return false;
	}

	if(!getId(str+"_email").value.match(valEmail))
	{
		alert("Please enter your email address.!");
		getId(str+"_email").focus();
		return false;
	}
	
	//Your Address
 	if(getId(str+"_address").value==""){
		alert("Please enter address.");
		getId(str+"_address").focus();
		return false;
	}

 	if(getId(str+"_zipcode").value==""){
		alert("Please enter zipcode.");
		getId(str+"_zipcode").focus();
		return false;
	}
	
 	if(getId(str+"_city").value==""){
		alert("Please enter City.");
		getId(str+"_city").focus();
		return false;
	}

 	if(getId(str+"_province").value==""){
		alert("Please enter State/Province.");
		getId(str+"_province").focus();
		return false;
	}

 	if(getId(str+"_country").value==""){
		alert("Please select Country.");
		getId(str+"_country").focus();
		return false;
	}

	if(getId(str+"_tel").value==""){
		alert("Please enter your tel.");
		getId(str+"_tel").focus();
		return false;
	}

	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: $("#userinfoForm").serialize(),
	   success: function(msg){
			$("#userinfoButton").attr("disabled",false);
			$("#loadingInfo").removeClass("loading");
			if(msg=="ok"){
				$("#loadingInfo").addClass("loadingOk");
				$("#loadingInfo").html(noteSuccess);
				alert(noteSuccess);
				$("#loadingInfo").html("");
				//location = backUrl;
			}else{
				$("#loadingInfo").html(msg);	
			}
	   },
	    beforeSend: function(){
			$("#userinfoButton").attr("disabled",true);
			$("#loadingInfo").addClass("loading");
			$("#loadingInfo").html('<img src="images/loading_small.gif" alt="Loading..." /> Loading...'); 
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown){
		   	$("#loadingInfo").html(textStatus); 
	   }
	});	
	return false;
}

//下定单
function checkOrders(str,postUrl,backUrl)
{
	//收货人基本信息Your Personal Details
	if(getId(str+"_first_name").value==""){
		alert("Please enter first name.");
		getId(str+"_first_name").focus();
		return false;
	}
	
	if(getId(str+"_last_name").value==""){
		alert("Please enter last name.");
		getId(str+"_last_name").focus();
		return false;
	}
	
 	if(getId(str+"_email").value==""){
		alert("Please enter email address.");
		getId(str+"_email").focus();
		return false;
	}
	
	if(!getId(str+"_email").value.match(valEmail))
	{
		alert("Please enter right format email address.");
		getId(str+"_email").focus();
		return false;
	}

	//Your Address
 	if(getId(str+"_address").value==""){
		alert("Please enter address.");
		getId(str+"_address").focus();
		return false;
	}

 	if(getId(str+"_zipcode").value==""){
		alert("Please enter zipcode.");
		getId(str+"_zipcode").focus();
		return false;
	}
	
 	if(getId(str+"_city").value==""){
		alert("Please enter City.");
		getId(str+"_city").focus();
		return false;
	}

 	if(getId(str+"_province").value==""){
		alert("Please enter State/Province.");
		getId(str+"_province").focus();
		return false;
	}

 	if(getId(str+"_country").value==""){
		alert("Please select Country.");
		getId(str+"_country").focus();
		return false;
	}
	
	//Your Contact Information
	if(getId(str+"_tel").value==""){
		alert("Please enter Telephone.");
		getId(str+"_tel").focus();
		return false;
	}
	
	if(getId(str+"_fax").value==""){
		alert("Please enter fax.");
		getId(str+"_fax").focus();
		return false;
	}

	//Payment Option
	var payment = getName(str+"_payment").length;
	
	for (i = 0 ;i <payment;i++)
	{
		if(getName(str+"_payment")[i].checked){var isPayment="yes"};
	}
	
	if(isPayment!="yes"){
		alert("Please Select Payment.");
		return false;
	}
 
	//Fee Total
	if(getId(str+"_money").value<0){
		alert("Total money is "+getId(str+"_money").value+",can't submit orders.");
		return false;
	}
	
	if(!confirm(noteSubmit)) return false;

	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: $("#orderForm").serialize(),
	   success: function(msg){
			
			if(msg=="ok"){
				$("#loadingInfo").removeClass("red");
				$("#loadingInfo").addClass("loadingOk");
				$("#loadingInfo").html(noteSuccess);
				//alert(noteSuccess);
				//$("#loadingInfo").html("");
				if(backUrl!="")location = backUrl;
				//location = backUrl;
			}else{
				$("#saveButton").attr("disabled",false);
				$("#loadingInfo").removeClass("loading");
				$("#loadingInfo").addClass("red");
				$("#loadingInfo").html(noteFailurePassword);	
			}
	   },
	    beforeSend: function(){
			$("#saveButton").attr("disabled",true);
			$("#loadingInfo").addClass("loading");
			$("#loadingInfo").html('<img src="images/loading_small.gif" alt="Loading..." /> Loading...'); 
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown){
		   	$("#loadingInfo").html(textStatus); 
	   }
	});	
	return false;
	
	
	
	
}

//修改密码
function checkPassword(str,postUrl,backUrl)
{
	if(getId(str+"_old_password").value==""){
		alert("Please enter your old password.");
		getId(str+"_old_password").focus();
		return false; 
	}	
	
	if(getId(str+"_password").value=="" || getId(str+"_password").value.length<5){
		alert("Password length must be greater than 5.");
		getId(str+"_password").focus();
		return false; 
	}		

	if(getId(str+"_password").value!=getId(str+"_password_confirm").value){
		alert("Confirm password is not the same.");
		getId(str+"_password_confirm").focus();
		return false;
	}

	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: $("#passwordForm").serialize(),
	   success: function(msg){
			$("#saveButton").attr("disabled",false);
			$("#loadingInfo").removeClass("loading");
			if(msg=="ok"){
				$("#loadingInfo").removeClass("red");
				$("#loadingInfo").addClass("loadingOk");
				$("#loadingInfo").html(noteSuccess);
				alert(noteSuccess);
				$("#loadingInfo").html("");
				//location = backUrl;
			}else{
				//alert(msg);
				$("#loadingInfo").addClass("red");
				$("#loadingInfo").html(noteFailurePassword);	
			}
	   },
	    beforeSend: function(){
			$("#saveButton").attr("disabled",true);
			$("#loadingInfo").addClass("loading");
			$("#loadingInfo").html('<img src="images/loading_small.gif" alt="Loading..." /> Loading...'); 
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown){
		   	$("#loadingInfo").html(textStatus); 
	   }
	});	
	return false;
	
}

//找回密码
function checkForgotPasswordForm(postUrl,backUrl)
{
	if(getId("forgot_username").value==""){
		alert("Please enter your username.");
		getId("forgot_username").focus();
		return false; 
	}	

	if(!getId("forgot_email").value.match(valEmail))
	{
		alert("Please enter your email address.");
		getId("forgot_email").focus();
		return false;
	}

	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: $("#forgotPasswordForm").serialize(),
	   success: function(msg){
			if(msg=="ok")
			{
				$("#saveButton").attr("disabled",false); 
				$("#loadingInfo").html(noteSuccessForgot);
				if(backUrl!="")location = backUrl;
			}else if(msg=="no"){
				$("#saveButton").attr("disabled",false); 
				$("#loadingInfo").html(noteFailureForgot)
			}else{
				$("#saveButton").attr("disabled",false); 
				$("#loadingInfo").html(msg)
				//$("#loadingInfo").html(msg)
			}
	   },
	    beforeSend: function(){
			$("#saveButton").attr("disabled",true);
			$("#loadingInfo").addClass("loading");
			$("#loadingInfo").html('<img src="images/loading_small.gif" alt="Loading..." /> Loading...'); 			
	   },
		error:function(XMLHttpRequest, textStatus, errorThrown){
		   	$("#loadingInfo").html(textStatus); 
	   }
	});
	return false;
}


//留言
function checkMessageForm(str,postUrl,backUrl)
{
	if(!getName(str+"_sex")[0].checked && !getName(str+"_sex")[1].checked){
		alert("Please select your Gender.");
		return false;
	}
 	
	if(getId(str+"_name").value == "")
	{
		alert("Please enter name");
		getId(str+"_name").focus();
		return false;
	} 

	if(!getId(str+"_tel").value.match(valEmail))
	{
		alert("Please enter your email address.!");
		getId(str+"_tel").focus();
		return false;
	}
	
	 if(getId(str+"_description").value == "")
	{
		alert("Please enter Content.");
		getId(str+"_description").focus();
		return false;
	}
	 

	$.ajax({
	   type: "POST",
	   url: postUrl,
	   data: $("#messageForm").serialize(),
	   success: function(msg){
			if(msg=="ok")
			{
				$("#saveButton").attr("disabled",false); 
				$("#loadingInfo").html(noteSuccess)
				alert(noteSuccess);
				document.getElementById("messageForm").reset();
				if(backUrl!="")location = backUrl;
			}else{
				$("#saveButton").attr("disabled",false); 
				//$("#loadingInfo").html(noteFailure)
				$("#loadingInfo").html(msg)
			}
	   },
	    beforeSend: function(){
			$("#saveButton").attr("disabled",true);
			$("#loadingInfo").addClass("loading");
			$("#loadingInfo").html('<img src="images/loading_small.gif" alt="Loading..." /> Loading...'); 			
	   },
		error:function(XMLHttpRequest, textStatus, errorThrown){
		   	$("#loadingInfo").html(textStatus); 
	   }	   
	});
	return false;
	
}

/**设置货币格式**/
function changecurrmoney(curr)
{
	$.ajax({
	   type: "POST",
	    url: "ajaxdata.php",
	   data: "type=clear_cache",
	   success: function(msg){
		setCookie("moneyunit",curr,10);	
		if (typeof(reloadit) == 'function') reloadit();
			else location.reload();
		}
	});
	//alert("llll");
	
}

//评论
function comment(purl){
	if(getId("comment_username").value == ""){	
		alert("Please enter your name.");
		getId("comment_username").focus();
		return false;
	}
	
	if(getId("comment_content").value == ""){	//评论内容为空
		alert("Please enter your content.");
		getId("comment_content").focus();
		return false;
	}

	noteTextAjaxBoard = "message";
	getId("commentButton").disabled = true;
	getId(noteTextAjaxBoard).className="loading";
	postUrl = purl;
	startRequest("POST",postUrl,true,sendData("commentForm"),"form",3);
	return false;
	
}

function openPage(url,name,id,Width,Height)
{
	//window.open(url+"?id="+id,name,'scrollbars=1,resizable=0,width='+Width+',height='+Height+',top=50,left=200');
	var availWidth = screen.availWidth;
	var availHeight = screen.availHeight
	scrollX=Math.round((availWidth-Width)/2);
	scrollY=Math.round((availHeight-Height)/2);
	window.open(url+"?"+id+"",name,'scrollbars=1,resizable=0,width='+Width+',height='+Height+',top='+scrollY+',left='+scrollX+'');
}
