﻿if(document.addEventListener){//如果是Firefox  
document.addEventListener("keypress",fireFoxHandler, true);   
}
else{ 
 document.attachEvent("onkeypress",KeyDown);   
}  
 function fireFoxHandler(evt){  if(evt.keyCode==13){  
    LoginClick();
 } 
}  
function KeyDown()//回车焦点
{
    if (event.keyCode == 13)
    {
        event.returnValue=false;
        event.cancel = true;
        LoginClick();
    }
}
//替换特殊的字符' "
function replaceChar(specialcode)
{  
    specialcode = specialcode.replace(new RegExp("\"","g"), "“").replace(new RegExp("\'","g"),"‘");  
    
    return specialcode;
}

$(document).ready(function() {
    //焦点图
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "WebService.asmx/GetIndexFocusPics",
        data: "{}",
        dataType: 'json',
        success: function(result) {
            result = result;
            document.getElementById("focusPic").innerHTML = result;
            // alert("焦点图,result.d=" + result.d+",<br>result"+result);
        }
    });

    $("#checkCodeIMG").mouseup(function() {
        this.src = this.src + '?';
    });
    //获取用户名
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "WebService.asmx/GetIndexUserName",
        data: "{}",
        dataType: 'json',
        success: function(result) {
        result = result;
            if (result != '') {
                // alert("焦点图,result.d=" + result.d + ",<br>result=" + result);

                document.getElementById("l_userName").innerHTML = "<strong class=\"hong12\">" + result + "</strong>";
                document.getElementById("alogin").style.display = "block";
                document.getElementById("login").style.display = "none";


            }
            else {
                document.getElementById("alogin").style.display = "none";
                document.getElementById("login").style.display = "block";
            }
        }
    });
    //登出 
    $("#logOut").click(function() {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "WebService.asmx/logOut",
            data: "{}",
            dataType: 'json',
            success: function(result) {
            result = result;
                if (result) {

                    window.location.href = "http://www.sicent.com";
                }
            }
        });
    });
});       
function LoginClick(){
    //用户名
   var nbName = replaceChar($('#txt_nbName')[0].value);   
   //密码
   var pwd = replaceChar($('#txt_pwd')[0].value);
   //验证码
   var checkCode = replaceChar($('#txt_CheckCode')[0].value); 
   
   if(nbName=="")
   {
    alert("用户名不能为空");
    //$("#checkCodeIMG")[0].src=$("#checkCodeIMG")[0].src+'?'; 
   }
   else if(pwd=="")
   {
   alert("密码不能为空");
   // $("#checkCodeIMG")[0].src=$("#checkCodeIMG")[0].src+'?';
   }
   else if(checkCode=="")
   {
   alert("验证码不能为空");
  //  $("#checkCodeIMG")[0].src=$("#checkCodeIMG")[0].src+'?';
   }
   else{
       $.ajax({
           type: "POST",
           contentType: "application/json",
           url: "WebService.asmx/Login",
           data: "{userName:'" + nbName + "',pwd:'" + pwd + "',checkNum:'" + checkCode + "'}",
           dataType: 'json',
           success: function(result) {

           result = result;
               if (result == "0") {//登陆成功
                   window.location.href = "uCenter.aspx";
               }
               else if (result == "1") {//返回首页
                   window.location.href = "Default.aspx";
               }
               else if (result == "2") {//验证码错误
                   alert("对不起，验证码错误");
                   $("#checkCodeIMG")[0].src = $("#checkCodeIMG")[0].src + '?';
               }
               else if (result == "3") {//用户名或密码错误
                   alert("对不起，用户名或密码错误");
                   //$("#checkCodeIMG")[0].src=$("#checkCodeIMG")[0].src+'?';
               }
               else {
                   alert("对不起，请重新登陆");
                   $("#checkCodeIMG")[0].src = $("#checkCodeIMG")[0].src + '?';
               }
           }

       });  
   }
}
$(document).ready(function(){
$("#Login").click(function(){
   LoginClick();
});  
}); 
