var regInfo = {
        
        error: {
            email: "请填写有效Email地址，以便用来确认邮件完成注册！",
	   		strEmail:"Email格式不正确",
            password: "密码长度不正确，6-16位。",
            repassword: "两次输入不一致。",
            username: "用户名不能为空。",
	    	ajaxEmail:"Email已存在,换一个？",
	    	code:"验证码填写错误",
			username: "真实姓名不能空。",
			mobile: "手机号验证不对！"
        }
}
function isUsedUsername(name){
	$.post('/index.php?m=signup&act=checkUsername',
										 {'name':name},
										 function(callback){
											 if(callback=='1') {
												// alert(1);
												 return false;
											 //setTimeout("close_tip()",10);
										 	}else{
												//alert(2);
												return true;
											}
										 }
		 );
}
function isUsedEmail(email){
	//alert(email);
	
	$.post('/index.php?m=signup&act=checkMail',
										 {'email':email},
										 function(callback){
											// alert(callback);
											return callback;
/*											 if(callback=='1'){
												 //alert(typeof(callback));
											 	return false;
											 }else{
												return true; 
											 }*/
										 }
		 );
	
}
function isUsedMobile(mobile){
	
	$.post('/index.php?m=signup&act=checkMobile',
			{'mobile':mobile},
			function(res){
				alert(res);
				//var res = res;
			}
		 );
	//return res;
}
function signup()
{
   var email=$("#signup-email-address").val();
   var username=$("#signup-username").val();
   var password=$("#signup-password").val();
   var password2=$("#signup-password-confirm").val();
   var realname=$("#signup-realname").val();
   var mobile=$("#signup-mobile").val();
   
   if(email=='')
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.email);
       return false ;
   }else if(isEmail(email)==false)
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.strEmail);
       return false ;
   }else if(username =="")
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.username);
       return false ;
   }else if(realname =="")
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.realname);
       return false ;
   }else if(password=='' || password.length < 4)
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.password);
       return false ;
   }else if(password != password2)
   {
	$("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.repassword);
       return false ;
   }else if(isMobile(mobile)==false)
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.mobile);
       return false ;
   }else{
		return true;
   }
}

function checkAddress(){
   var realname=$("#address-realname").val();
   var mobile=$("#address-mobile").val();
   var address=$("#address-address").val();
   var postcode=$("#address-postcode").val();
  // alert(address.length);
   if(realname=='')
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html('真实姓名不能为空');
       return false ;
   }else if(address=='')
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html('详细地址不能为空');
       return false ;
   }else if(address.length < 5)
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html('详细地址不能少于5个字。');
       return false ;
   }else if(isMobile(mobile)==false)
   {
       $("#sysmsg-error").show();
       $("#sysmsg").html(regInfo.error.mobile);
       return false ;
   }else if(isPostalCode(postcode)==false||postcode==''){
       $("#sysmsg-error").show();
       $("#sysmsg").html("请正确填写邮编！");
       return false ;
	}else{
		return true;
   }
   return true;
}
function isMobile(value){
	
if(/^13\d{9}$/.test(value)|| /^15[0-35-9]\d{8}$/.test(value) || /^18[5-9]\d{8}$/.test(value)){
	
	return true; 
	}else{ 
	
	return false; 
	}
}

function isPostalCode(postcode)
{
	var pattern =/^[0-9]{6}$/;
	if(postcode!="")
	 {
		 if(!pattern.exec(postcode))
		 {
			return false;
		 }
	}
}
function showMSN()
{
        var show= $("#show").val();
	
	if(show==1){
	$("#deal-share-im-c").hide();
	 $("#show").val('0');
	}else{
	 $("#deal-share-im-c").show();
	 $("#show").val('1');
	}
	
}
function isEmail(strEmail) {
if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
return true;
else
return false ; 
}

function ajax_mail(strEmail)
{
	 var callback;
	 $.get("index.php?m=signup&act=checkMail", {email: strEmail},function(callback){
	 //alert (callback);
        
    });
}
function close_tip()
{
	$("#sysmsg-error").hide();
}
function returnUrl()
{
	window.location.href=host;
}
function subscribe()
{
	var cityid=$("#cityid").val();
	var email=$("#email").val();
	if(email=='')
	{
		alert("Email不能为空");
	}else if(isEmail(email)==false){
		alert("Email格式不正确");
	}else{
	     $.get(host+"/index.php?m=maillist&act=ajax", {email: email,cityid:cityid},function(callback){
		if(callback=='1')
		{
			$("#tip-deal-subscribe-body").html("<span class=\"subscribe-succ\">您已经成功订阅！</span>");
		}
		});
	}
}
function Sidesubscribe()
{
	var cityid=$("#sideCityid").val();
	var email=$("#sideEmail").val();
	if(email=='')
	{
		alert("Email不能为空");
	}else if(isEmail(email)==false){
		alert("Email格式不正确");
	}else{
	     $.get(host+"/index.php?m=maillist&act=ajax", {email: email,cityid:cityid},function(callback){
		if(callback=='1')
		{
			$("#deal-subscribe-body").html("<span class=\"subscribe-succ\">您已经成功订阅！</span>");
		}
		});
	}
}

function checkBuyNum()
{
	var residue=Number($("#residue").val());
	var quantity=Number($("#quantity").val());
	var advance=Number($("#advance").val());
	var singlebuylimit = Number($("#singlebuylimit").val())
	
	var strP=/^\d+(\.\d+)?$/;
	var buyPrice=Number($("#deal-buy-price").html());
	
	if(!strP.test(quantity))
	{
		$("#sysmsg-error").show();
		$("#sysmsg").html("请输入正确的购买数量");
		$("#quantity").val(Math.abs(quantity));
	}else if(quantity > residue)
	{
		
		$("#sysmsg-error").show();
		$("#sysmsg").html("本单只剩 "+residue+" 件了");
	}else if(quantity=='0' || quantity < 0){
		$("#sysmsg-error").show();
		$("#sysmsg").html("购买数量不能小于1");
		$("#quantity").val('1');
	}else if (singlebuylimit>0){	
		if(quantity>singlebuylimit){
			$("#sysmsg-error").show();
			$("#sysmsg").html("单次最多购买"+singlebuylimit+"个。");

			$("#quantity").val(singlebuylimit);
			costPrice=singlebuylimit*buyPrice-advance;
			$("#deal-buy-total").html(number_format(costPrice,2));
			$("#deal-buy-total-t").html(number_format(costPrice,2));
		}else{
			costPrice=quantity*buyPrice-advance;
			$("#deal-buy-total").html(number_format(costPrice,2));
			$("#deal-buy-total-t").html(number_format(costPrice,2));
		}
	}else{
		costPrice=quantity*buyPrice-advance;
		$("#deal-buy-total").html(number_format(costPrice,2));
		$("#deal-buy-total-t").html(number_format(costPrice,2));
	}

	
	
}

function addQuestion()
{
	var gid=$("#gid").val();
	var question=$("#question").val();
	if(question=='')
	{
		alert("您想问点什么呢？");
	}else{
		$.get(host+"/index.php?m=consultations&act=ajax", {gid:gid,question:question},function(callback){
		
		if(callback=='1')
		{
			$("#question-info").hide();
			$("#consult-add-succ").show();
		}
		});
	}
}
function question()
{
	$("#question").val("");
	$("#question-info").show();
	$("#consult-add-succ").hide();
}
// open hidden layer
function mopen()
{	
	

	$("#myaccount-menu").show();
	

}
function mclosetime(){
	$("#myaccount-menu").hide();
	}
// close showed layer
function mclose()
{
	$("#myaccount-menu").hide();
}

function changeCity()
{
	$("#guides-city-list").show();
}
function CityClose()
{
	$("#guides-city-list").hide();
}
// close layer when click-out
document.onclick = mclose;
function showDiv()
{
	var html=$("#showDiv").html();
	jQuery.facebox(html);
}
//订单取消
function orderCancle(orderid)
{
	var op="#op_"+orderid;
	if(confirm("确定要取消订单吗?")==true){     
	   $.get(host+"/index.php?m=order&act=cancle", {orderid:orderid},function(callback){
		
		if(callback=='1')
		{
		$(op).html("已成功取消");	
		}
		});
	}   
}
//设置新地址
function newaddress()
{
	$("#consignee").val("");
	$("#telephone").val("");
	$("#address").val("");
	$("#zipcode").val("");
	$("#zipcode").val("");
}
$(document).ready(function(){
  $("#myaccount-menu").mouseover(function(){
		mopen()
		});
    $("#myaccount-menu").mouseout(function(){
		mclose()
		})
    $("#guides-city-list").mouseover(function(){
		changeCity()
		});
    $("#guides-city-list").mouseout(function(){
		CityClose()
		})
});
function closeIntroduction(){ 
            document.getElementById("sysmsg-guide").style.display = "none"; 
            document.cookie = "isClose";
            
        }
function copyToClipBoard(){ 
var clipBoardContent=$("#share-copy-text").val(); 
window.clipboardData.setData("Text",clipBoardContent); 
}
function showCard()
{
	
	$("#cardcode-link-t").show();
	
}
function hideCard()
{
	$("#cardcode-link-t").hide();
	
	
}
//礼品卡处理
function checkCard()
{
	var seleprice=Number($("#deal-buy-total-t").html());
	var cardcode=$("#cardcode-text").val();
	var residue=Number($("#residue").val());
	var quantity=Number($("#quantity").val());
	var buyPrice=Number($("#deal-buy-price").html());
	
	$.post(host+"/index.php?m=deal&act=checkCard",{cardcode:cardcode},function(callback){
		
		if(callback=='000')
		{
			$("#sysmsg-error").show();
			$("#sysmsg").html("礼品卡不存在,请重试");
			
		}else if(callback=='001')
		{
			$("#sysmsg-error").show();
			$("#sysmsg").html("礼品卡已使用");
			
		}else if(callback=='002')
		{
			$("#sysmsg-error").show();
			$("#sysmsg").html("礼品卡未到使用时间");
			
		}else if(callback=='003')
		{
			$("#sysmsg-error").show();
			$("#sysmsg").html("礼品卡已过期");
			
		}else{
			var html="礼品卡金额："+callback;
			$("#cardcode-link-t").html(html);
			$("#cardcode-row").show();
			$("#cardcode-row-n").html(cardcode);
			$("#cardcode-row-t").html(callback);
			costPrice=quantity*buyPrice-Number(callback);
			$("#deal-buy-total-t").html(costPrice);
			$("#deal-buy-cardcode").val(cardcode);
			
		}
	})
	
}
//虚拟卡充值
function topup()
{
	var cardno=$("#cardno").val();
	var cardpwd=$("#cardpwd").val();
	if(cardpwd=='')
	{
	  $("#credit-card-notice").html("充值卡密码不能为空");
	}else{
	  $.post(host+"/index.php?m=credit&act=topup",{cardno:cardno,cardpwd:cardpwd},function(callback){
		
		if(callback=='0')
		{
			$("#credit-card-notice").html("充值卡已使用");	
		}else if(callback=='-1')
		{
			$("#credit-card-notice").html("充值卡信息不正确，请重试 ");	
		}else if(callback=='1')
		{
			$("#credit-card-notice").html("充值成功！<a href='javascript:location.reload()'>刷新页面？</a>");
		}
	   })	
	}
	
}
function union()
{
	$.post(host+"/index.php?m=api",function(callback){
	if(callback!='1')
	{
		$("#union").html(callback);
	}else{
		$("#union_big").hide();
	}
	})
	
}
function sendCouponsMsg(id)
{

	var formid="#sms_form_"+id;
	
	var html='<input type="text" id="mobile'+id+'" class="f-input" name="mobile" maxlength="11" ><br><input type="button" class="formbutton" value="发送" onclick="SMS_SEND('+id+',$(\'#mobile'+id+'\').val())">';
	//alert(typeof $('#sms_form_'+ id +':empty'));
	//alert($('#sms_form_' + id).html().length);
	if($('#sms_form_' + id).is(':empty')){
		$('#sms_form_' + id).html(html);
		//alert(1);
	}else{
		//alert(2);
		$('#sms_form_'+id).empty();
	}
	
	
}
function SMS_SEND(id,mobile)
{
	
	//var mobile=$("#mobile"+id).val();
	//var formid="#sms_form_"+id;

	if(mobile=='' || isMobil(mobile)==false)
	{
		alert("手机号码格式不正确!")
	}else{
	
		$.post(host+"/index.php?m=coupons&act=issend",
			   {mobile:mobile,couponsid:id},
			   function(callback){	
			   
				   if(callback<='3'){
					   
						if(id>0){
							$.post(host+"/index.php?m=coupons&act=send",{couponsid:id,mobile:mobile},function(callback){
																											   //alert(callback);
								if(callback=='0')
								{ 
									$.post(host+"/index.php?m=coupons&act=smslog",
										   {mobile:mobile,couponsid:id}
										   );
									$('#sms_form_'+id).html("发送成功");	
								}
							})
						}else{
							alert("验证码还未生成，请确定自己是否支付。");
						}
				   
					   
				   }else{
					   $(formid).html("你已经发送过了。请不要重复发送。");
				   }																		   
		})

	}
}
function isMobil(s)    
{    
    var patrn = /^[0-9]{11}$/;  
    if(!patrn.exec(s)) {  
        return false;  
    }  
    return true;  
}
function checkUserForm(){
	if($('#ptypeid').val()==1){
		if($('input[name=addressid]:checked').val()==0){
			var mobile =$("#mobile").val();
			var realname = $('#realname').val();
			var address = $('#address').val();
			var postcode = $('#postcode').val()
			//alert(address.length);
			//alert(mobile);
			if(realname==''){
				alert('警告！姓名不能为空。');
				return false;
			}else if(mobile==''){
				alert('警告！请正确输入手机号。');
				return false ;
			}else if(isMobile(mobile)==false){
				alert('警告！请正确输入手机号。');
				return false ;
			}else if(address==''|| address <= 5){
				alert('警告！详细地址不能空或是小于5个字。');
				return false;	
			}else if(isPostalCode(postcode)==false||postcode==''){
				alert('警告！请正确输入邮编。');
				return false;	
			}else{
				return true;
			}
		}else if($('input[name=addressid]:checked').val()==undefined ){
			alert('警告！请选择派送地址，如果没有请选择添加新地址。');
			return false;
		}else{
			return true;
		}
	}else{
		return true;	
	}

	
}
function del(url){
	if (confirm("确定删除它吗？"))
	{
		if(typeof(url)=='function')url();
		else
		window.location.href=url;				
	}
}
function getArea(id,element){
	var dir_temp = '/temp';
	if(element!=''){
		JQ.ajax({
		   type:'GET',
		   url: dir_temp+"/"+id+".html",
		   dataType:'html',
		   cache:false,
		   success:function(callback){
				$('#'+element).empty().append(callback).change(); 
			   },
		   error:function(XMLHttpRequest, textStatus){
					if(textStatus=='error'){
						createAreaHtml(element,id);
					}
		   		}	
		   });
	}

/*	JQ.post("/index.php?m=area&act=get",
		   {'pid':id},
		   function(callback){
			 // alert(callback);
			   //alert(element);
			   if(element){
				   //alert(element);
				   //alert($('#'+element).html());
				   //$('#'+element).empty().html(callback);
				  // $('#asdf').empty().append(callback);
				  JQ('#'+element).empty().append(callback).change();
				  
			   }
		   })
*/
}
function getAreaName(name,element,selected){
	//alert(id);
	$.post("/index.php?m=area&act=get",
		   {'name':name,'selected':selected},
		   function(callback){
			   if(element){
				   $('#'+element).empty().append(callback);
			   }
		   })
	
	//$.post("/index.php?m=area&act=get",{'pid':id},function(callback){$('#'+element).empty().html(callback);})
	//alert(element);
}
function defaultArea(province,city,region){
	if(province&&city&&region){
		getAreaName('中国','province',province);
		getAreaName(province,'city',city);
		getAreaName(city,'region',region);	
	}else{
		getArea(1,'province')
	}
}

 function startclock(num) {
  if(num == (wait)) {
   $('#button_regcode').val('发送');
  } else {
   wut = (wait)-num;
   //document.forms.register.Submit.value = " 同意前请先阅读 (" + wut + ")";
   $('#button_regcode').val('等待'+ wut+'秒');
  }
 }

 function stopclock() {
	 $('#button_regcode').attr('disabled','');

}
function createAreaHtml(element,id){
	
	JQ.ajax({
					   type:'GET',
					   url: "index.php",
					   data:"m=area&act=get&pid="+id,
					   cache:false,
					   success:function(callback){
						 		//$('#province').empty().append(callback).change();  	
							},
						error:function(XMLHttpRequest, textStatus){
								//$('#province').empty().append(callback).change(); 
							},
					    complete:function (XMLHttpRequest, textStatus) {	
							$('#'+element).empty().append(XMLHttpRequest.responseText).change();  
							//alert(XMLHttpRequest.HandleStateChange);
							//alert(textStatus);
							
							}
					   });
}
function defaultAreaHtml(element,id){		
				//province
				
				var dir_temp = '/temp';
				
				JQ.ajax({
					   type:'GET',
					   url: dir_temp+"/"+id+".html",
					   dataType:'html',
					   cache:false,
					   success:function(callback){
						  	 
								$('#'+element).empty().append(callback).change(); 
						   },
					   error:function(XMLHttpRequest, textStatus){
						   
						  		if(textStatus=='error'){
									createAreaHtml(element,id);
								}
						   }	   
					   
					   });

}