﻿//页面load时，先加载地址信息
function loadmobile()
{
    showtime(parseInt($("#hid_js_timeCha").html()));
    $.ajax({
	        url:"/ajax.aspx?q=loadmobile&time="+new Date().toString(),
	        type:'GET',	
	        timeout:30000,
	        beforeSend: function() {  
                $('#loading').show();
            },
            error:function(){
                $('#loading').hide();
                $('#frm').show();
            },
	        success:function(){
	            var $result=arguments[0];
	            //没有找到符合条件的收货信息
	            if($result=="0")
	            {
	                $('#loading').hide();    
	                $('#frm').show();    
	            }
	            else
	            {
	                if($result.split(",")[0]!="")
	                    $("#txtmember").val($result.split(",")[0]);
	                if($result.split(",")[1]!="")
	                    $("#txtmobile").val($result.split(",")[1]);
	                $('#loading').hide();    
	                $('#frm').show(); 
	                
	            }
	        }		
        });
}

function clearmobile()
{
    var $mobile=$("#txtmobile");
    if($mobile.val()=="预留手机号码，获得预售短信提醒")
        $mobile.val("");    
}

function returnmobile()
{
    var $mobile=$("#txtmobile");
    if($mobile.val()=="")
        $mobile.val("预留手机号码，获得预售短信提醒");
}

function clearmember()
{
    var $member=$("#txtmember");
    if($member.val()=="预留会员名，获得活动详情及活动攻略")
        $member.val("");
}

function returnmember()
{
    var $member=$("#txtmember");    
    if($member.val()=="")
        $member.val("预留会员名，获得活动详情及活动攻略");
}

//倒計時
function showtime(num)
{
  if(num<0)//預賣結束
  {
    alert("预卖活动已结束");
     document.location=("http://www.meilieasy.com/zengsong/");
  }
  
  var days = Math.floor((num/3600)/24)//计算天数
  var hour=Math.floor(num/3600);//计算时 
  var hours = hour%24;
  hour=hour<10?"0"+hour:hour;  
  var tnum=num%3600;//计算分
  var minute=Math.floor(tnum/60);   
  minute=minute<10?"0"+minute:minute;   
  var second=tnum%60;//计算秒
  second=second<10?"0"+second:second; 

  $("#js_showtime").html("<div style=\" text-align:center; width:56px; float:left; margin-right:6px;\">"+days+"</div><div style=\" text-align:center; float:left; width:56px; margin-right:6px;\">"+ hours+"</div><div style=\" text-align:center; float:left; width:56px; margin-right:6px;\">"+minute+"</div><div style=\" text-align:center; float:left; width:56px;\">"+second+"</div>");
  setTimeout("showtime("+(--num)+")",1000);   
}

function strLength(str)
{            
    return str.replace(/[^\x00-\xFF]/g,'**').length;
}
        
function addInfo()
{

   var $username = $("#txtmember");
        
   var $mobile=$("#txtmobile");
   
   var $code=$("#txtcode");
    
   if($code.val()=="")
   {
        alert("验证码必须填写");
        $code.focus();
        return false;
   }
   
   if($mobile.val()=="")
   {
        alert("手机号码必须填写");
        $mobile.focus();
        return false;
   }
   if($username.val()!="")
   { 
       var $reg=/^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/ ;
       if($username.val().match($reg))
       {
            if($username.val()==""|| strLength($username.val())<4 || strLength($username.val())>16)
            {
                alert("会员名不合法，请检查会员名的正确性");
                $username.focus();
                return false;
            }
        }
        else
        {
           alert("会员名不合法，请检查会员名的正确性");
            $username.focus();
            return false;
        }
    }
   
    
    if($mobile.val()!="")
    {
        var $mo=/^13\d{9}$|^15\d{9}$|^0\d{11}$|^0\d{10}$|^02\d{9}$|^02\d{8}$|^010\d{8}$|^18\d{9}$/gi;
         if(!$mo.test($mobile.val()))
         {
            alert("手机号码非法");
            $mobile.focus()
            return false;
         }
     }
    $.ajax({
	        url:"/ajax.aspx?q=addInfo&member="+escape($username.val())+"&mobile="+$mobile.val()+"&code="+$code.val()+"&time="+new Date().toString(),
	        type:'GET',	
	        timeout:30000,
	        beforeSend: function() {  
                $('#status').show();
            },
            error:function(){
                $('#status').hide();                
            },
	        success:function(){
	            var $result=arguments[0];
	            //没有找到符合条件的收货信息
	            
	            if($result=="-4")
	            {
	                $('#status').hide();    
	                alert("验证码不正确!");
	                $code.focus();
	            }
	            if($result=="-3")
	            {
	                $('#status').hide();    
	                alert("手机号不能重复!");
	                $mobile.focus();
	            }
	            else if($result=="-1")
	            {
	                $('#status').hide();    
	                alert("当前会员名不存在!");
	                $username.focus();
	            }
	            else if($result=="1")
	            {
	                alert("信息提交成功");
	                window.location=("http://www.meilieasy.com/zengsong/");
	            }
	            else if($result=="-2")
	            {
	                $('#status').hide();    
	                alert("信息提交失败!");
	                $username.focus();
	            }
	        }		
        });
}
function checkcookie()
{
     $.ajax({
	        url:"/ajax.aspx?q=checkcookie&time="+new Date().toString(),
	        type:'GET',	
	        timeout:30000,
	        beforeSend: function() {  
                
            },
            error:function(){
                $('#unlogin').show(); 
                $('#logined').hide();                
            },
	        success:function(){
	            var $result=Number(arguments[0].split("-|-")[0]);
	            if($result>0)
	            {
	                $('#unlogin').hide(); 
	                $("#username").html(arguments[0].split("-|-")[1]);
                    $('#logined').show();  
	            }
	            else{
	                $('#unlogin').show(); 
                    $('#logined').hide();  
	            }
	        }
	    });
}

function keyCheckForm($button)
{
     $("#"+$button).keypress(function(e){
        if(e.keyCode == 13){
            CheckForm();
        }
     })
}

//验证输入
function CheckForm()
{
    if($("#txtUserName").val()==""){
       alert("请输入用户名！");
        $("#txtUserName").focus();
        return false;
    }
    if($("#txtPassWord").val()==""){
       alert('请输入登录密码！');
        $("#txtPassWord").focus();
        return false;
    }
    var $name = $("#txtUserName").val();
    var $password=$("#txtPassWord").val();
    var $saveStatus=1;
    
    $.ajax({
        url:"/ajaxLogin.aspx?saveType="+$saveStatus+"&name="+escape($name)+"&password="+MD5($password)+"&time="+new Date().toString(),
        type:'GET',	
        timeout:30000,
        beforeSend: function() {  
            
        },
        error:function(){
           $("#txtUserName").focus();
        },
        success:function(){
        
            var $result=Number(arguments[0]);
                        
            if($result>1)//登录成功
            {
                $('#unlogin').hide();  
                $("#username").html($name);  
                $('#logined').show();    
            }
            else if($result==0)
            {
                alert("会员名不正确");
                $("#txtUserName").focus();
            }
            else if($result==-1)
            {
               alert("密码不正确");
               $("#txtPassWord").focus();
            }            
        }		
    });
}    

//Soyou产品的购买
function addSoyouToCart($p_ID)
 {
    $.ajax({
        url:"/ajax.aspx?q=addsoyoutocart&p_ID="+$p_ID+"&time="+new Date().toString(),
        type:"GET",
        timeout:30000,
        beforeSend:function(){
            $('#status').show();
        },
        error:function(){
            $("#status").hide();
            ymPrompt.alert({message:'购买商品失败，请联系管理员！',title:'系统提示',btn:[['确定','ok']],handler:null});
        },
        success:function(){
            var $status=arguments[0];
            $("#status").hide();
            if($status=="1")
                window.location=("/list/shopcart.shtml");
            else if($status=="-1")
                ymPrompt.confirmInfo("此商品库存不足，是否添加缺货登记?",null,null,"系统提示",function(tp){tp=='ok'?AddOOS($p_ID):null})
            else
                ymPrompt.alert({message:'购买商品失败，请联系管理员！',title:'系统提示',btn:[['确定','ok']],handler:null});
        }
    
    });
} 

//选购结束我要去结帐
function addSoyou()
{
    var str="";
    var c=document.getElementsByName("checkbox");
    var l=c.length;
    var m=0;
    for(var i=0;i<l;i++)
    {
        if(c[i].checked)
        {
            m++;
            str+=c[i].value+",";
        }
    }
    
    if(m>0)
        addSoyouToCart(str);
    else
        alert("至少选择一个产品");
}
