﻿var arrCon=new Array();
var arrPro=new Array();
var arrCity=new Array();

function getcity()
{
    ajax("/handler/city.ashx?com=get","getcityCallback");
}
function getcityCallback(res)
{
    var arrTmp=res.split("\n");
    arrCon=arrTmp[0].split("|");
    arrPro=arrTmp[1].split("|");
    arrCity=arrTmp[2].split("|");
    loadPageControl();
}

function loadPageControl()
{ 
    var objCountry=$("selLocalityCountry"); 
    var objCountry2=$("selNativeplaceCountry");
    objCountry.options.length = 0;
    objCountry2.options.length = 0;
    
    for (i=0;i<arrCon.length;i++)
    {
        if (arrCon[i]=="") continue;
        var tmpArray=new Array();
        tmpArray=arrCon[i].split(",");
        addSelectItem(objCountry,tmpArray[1],tmpArray[0]);
        addSelectItem(objCountry2,tmpArray[1],tmpArray[0]);
    }
    
    addSelectItem(objCountry,"请选择","0");
    addSelectItem(objCountry2,"请选择","0");
    
    objCountry.onchange=function (e){objCountry_OnChange(this);};
    objCountry2.onchange=function (e){objCountry_OnChange(this);};
    
    objCountry.selectedIndex=objCountry.options.length-2;
    objCountry2.selectedIndex=objCountry2.options.length-2;
    objCountry_OnChange(objCountry);
    objCountry_OnChange(objCountry2);
    
}

function objCountry_OnChange(obj)
{
    var objCity=$(obj.id+"selProvince"+"selCity");
    if (objCity) obj.parentNode.removeChild(objCity);
        
    var id=obj.options[obj.selectedIndex].value;
    if (obj.id.indexOf("Nativeplace")!=-1)
    {
        $("hdNCountry").value=id;
        $("hdNProvinces").value="0";
        $("hdNCity").value="0";
        //checkNativeplace();
    }
    if (obj.id.indexOf("Locality")!=-1)
    {
        $("hdLCountry").value=id;
        $("hdLProvinces").value="0";
        $("hdLCity").value="0";
        //checkLocality();
    }
    var hasP=false;
    var objPro=$(obj.id+"selProvince");
    if (!objPro)
    {
        objPro = document.createElement("select");
        objPro.id=obj.id+"selProvince";
        objPro.className="select_2";
        obj.parentNode.appendChild(objPro);
        objPro.onchange=function (e){objPro_OnChange(this);};
    }
    objPro.options.length = 0;
    
    addSelectItem(objPro,"请选择","0");
    for (i=0;i<arrPro.length;i++)
    {
        var tmpArray=new Array();
        tmpArray=arrPro[i].split(",");
        if (tmpArray[1]==id)
        {
            addSelectItem(objPro,tmpArray[2],tmpArray[0]);
            hasP=true;
        }
    }
    if (!hasP) obj.parentNode.removeChild(objPro);
    else objPro_OnChange(objPro);
    
    
}

function objPro_OnChange(obj)
{
    var id=obj.options[obj.selectedIndex].value;
    if (obj.id.indexOf("Nativeplace")!=-1)
    {
        $("hdNProvinces").value=id;
        $("hdNCity").value="0";
    }
    if (obj.id.indexOf("Locality")!=-1)
    {
        $("hdLProvinces").value=id;
        $("hdLCity").value="0";
    }
    
    var hasP=false;
    var objCity=$(obj.id+"selCity");
    if (!objCity)
    {
        objCity = document.createElement("select");
        objCity.id=obj.id+"selCity";
        objCity.className="select_3";
        obj.parentNode.appendChild(objCity);
        objCity.onchange=function (e){objCity_OnChange(this);};
    }
    objCity.options.length = 0;
    
    addSelectItem(objCity,"请选择","0");
    for (i=0;i<arrCity.length;i++)
    {
        var tmpArray=new Array();
        tmpArray=arrCity[i].split(",");
        if (tmpArray[1]==id)
        {
            addSelectItem(objCity,tmpArray[2],tmpArray[0]);
            hasP=true;
        }
    }
    if (!hasP) obj.parentNode.removeChild(objCity);
    
}

function objCity_OnChange(obj)
{
    var id=obj.options[obj.selectedIndex].value;
    if (obj.id.indexOf("Nativeplace")!=-1)
    {
        $("hdNCity").value=id;
    }
    if (obj.id.indexOf("Locality")!=-1)
    {
        $("hdLCity").value=id;
    }
}

function addSelectItem(objSelect,text,value) 
{              
    var item = new Option(text, value);      
    objSelect.options.add(item);
}
　　    
function dataini()
{
    objyear=$("selYear");
    objmonth=$("selMonth");
    objday=$("selDay");
    objyear.options.length = 0;
    objmonth.options.length = 0;
    objday.options.length = 0;
    var currentYear=new Date().getFullYear();   

    for (var i=1950;i<=currentYear;i++)
        addSelectItem(objyear,i,i);
    for (var i=1;i<=12;i++)
        addSelectItem(objmonth,i,i);
    
    objyear.onchange=function (e){data_OnChange();};
    objmonth.onchange=function (e){data_OnChange();};
    objyear.selectedIndex=objyear.options.length/2;
    data_OnChange();
}
function data_OnChange()
{
    var MonDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    
    objyear=$("selYear");
    objmonth=$("selMonth");
    objday=$("selDay");
    var year=objyear.options[objyear.selectedIndex].value;
    var month=objmonth.options[objmonth.selectedIndex].value;
    var day = MonDays[month - 1];
    if ( month ==2 && isLeapYear(year)) day++;
    objday.options.length = 0;
    for (var i=1;i<=day;i++)
        addSelectItem(objday,i,i);
    objday.onchange=function (e){day_OnChange();};
    day_OnChange();
}
function day_OnChange()
{
    objyear=$("selYear");
    objmonth=$("selMonth");
    objday=$("selDay");
    var year=objyear.options[objyear.selectedIndex].value;
    var month=objmonth.options[objmonth.selectedIndex].value;
    var day = objday.options[objday.selectedIndex].value;
    $("hdBirthday").value=year+"-"+month+"-"+day;
}

function FormIni()
{
    getcity();
    dataini();
    $("form1").onsubmit=function (){return Form_OnSubmit()};
    $("txtEmail").onblur=function(e){chekEmail();}
    $("txtPwd").onblur=function(e){checkPwd();}
    $("txtPwd2").onblur=function(e){checkPwd();}
    $("txtMobile").onblur=function(e){checkMobile();}
    $("txtNickName").onblur=function(e){checkNickName();}
    //$("selBloodtype").onblur=function(e){checkBloodtype();}
    $("selHeight").onblur=function(e){checkHeight();}
    //$("selNativeplaceCountry").onblur=function(e){checkNativeplace();}
    $("selLocalityCountry").onblur=function(e){checkLocality();}
    //$("txtVerifyCode").onblur=function(e){chekV();}
    $("selEducation").onblur=function(e){chekEducation();}
    $("selSalary").onblur=function(e){chekSalary();}
    $("selCivilstate").onblur=function(e){chekCivilstate();}
}

function Form_OnSubmit()
{
    if (!chekEmailOnSubmit()) 
    {
        $("txtEmail").focus();
        return false;
    }
    if (!checkPwd()) 
    {
        $("txtPwd").focus();
        return false;
    }
    if (!checkMobile())
    {
        $("txtMobile").focus();
        return false;
    }
    if (!chekCivilstate())
    {
        $("selCivilstate").focus();
        return false;
    }
    if (!checkNickName()) 
    {
        $("txtNickName").focus();
        return false;
    }
//    if (!checkBloodtype()) 
//    {
//        $("selBloodtype").focus();
//        return false;
//    }

    if (!checkHeight()) 
    {
        $("selHeight").focus();
        return false;
    }
    
    if (!chekEducation())
    {
        $("selEducation").focus();
        return false;
    }
    if (!chekSalary())
    {
        $("selSalary").focus();
        return false;
    }

    if (!checkSex()) 
    {
        $("radioMan").focus();
        return false;
    }
//    if (!checkNativeplace()) 
//    {
//        $("selNativeplaceCountry").focus();
//        return false;
//    }
    if (!checkLocality()) 
    {
        $("selLocalityCountry").focus();
        return false;
    }
//    if (!chekVOnSubmit()) 
//    {
//        //$("txtVerifyCode").focus();
//        return false;
//    }
}

function chekEmailOnSubmit()
{
    if ($("txtEmail").value.Trim()=="")
    {
        $("tip_txtEmail").innerHTML="请填写您的邮箱";
        return false;
    }
    if (!IsEmail($("txtEmail").value.Trim()))
    {
        $("tip_txtEmail").innerHTML="您填写的邮箱格式有误";
        return false;
    }
    if ($("tip_txtEmail").innerHTML!="")
        return false;
    $("tip_txtEmail").innerHTML="";
    return true;
}

function chekEmail()
{
    $("tip_txtEmail").innerHTML="";
    if (chekEmailOnSubmit())
    {
        $("tip_txtEmail").innerHTML="正在检测Email...";
        ajax("/handler/Validate.ashx?type=IsExistsEmail&email="+escape($("txtEmail").value.Trim()),"chekEmailCallBack");
    }
}

function chekEmailCallBack(res)
{
    if (res=="1")
        $("tip_txtEmail").innerHTML="该Email已被注册";
    else
        $("tip_txtEmail").innerHTML="";
}

function chekVOnSubmit()
{
    if ($("txtVerifyCode").value.Trim()=="")
    {
        $("tip_txtVerifyCode").innerHTML="请填写验证码";
        return false;
    }
    if ($("tip_txtVerifyCode").innerHTML!="")
        return false;
    $("tip_txtVerifyCode").innerHTML="";
    return true;
}

function chekV()
{
    $("tip_txtVerifyCode").innerHTML="";
    if (chekVOnSubmit())
    {
        $("tip_txtVerifyCode").innerHTML="正在验证码...";
        ajax("/handler/Validate.ashx?type=checkcode&verifycode="+escape($("txtVerifyCode").value.Trim()),"chekVCallBack");
    }
}

function chekVCallBack(res)
{
    if (res=="0")
        $("tip_txtVerifyCode").innerHTML="验证码错误";
    else
        $("tip_txtVerifyCode").innerHTML="";
}

function checkPwd()
{
    if ($("txtPwd").value=="")
    {
        $("tip_txtPwd").innerHTML="请填写密码";
        return false;
    }
    else
        $("tip_txtPwd").innerHTML="";
        
    if ($("txtPwd2").value=="")
    {
        $("tip_txtPwd2").innerHTML="请填写确认密码";
        return false;
    }
    else
        $("tip_txtPwd2").innerHTML="";
        
    var pwd=$("txtPwd").value;
    if (pwd.length<6)
    {
        $("tip_txtPwd").innerHTML="您填写的密码长度太短";
        return false;
    }
    if (pwd.length>12)
    {
        $("tip_txtPwd").innerHTML="您填写的密码长度不能超过12位";
        return false;
    }
    if (pwd!=$("txtPwd2").value)
    {
        $("tip_txtPwd").innerHTML=$("tip_txtPwd2").innerHTML="两次输入的密码不一致";
        return false;
    }
    
     $("tip_txtPwd").innerHTML=$("tip_txtPwd2").innerHTML="";
    return true;
}

function checkMobile()
{
    if ($("txtMobile").value.Trim()=="")
    {
        $("tip_txtMobile").innerHTML="请填写您的手机号码";
        return false;
    }
    var reg = /^\d{8,14}$/;
    if (!reg.test($("txtMobile").value.Trim()))
    {
        $("tip_txtMobile").innerHTML="请正确填写您的手机号码";
        return false;
    }
    $("tip_txtMobile").innerHTML="";
    return true;
}

function chekCivilstate()
{
    var obj=$("selCivilstate");
    if (obj.options[obj.selectedIndex].value==0)
    {
        $("tip_selCivilstate").innerHTML="请选择您的婚姻状况";
        return false;
    }
    $("tip_selCivilstate").innerHTML="";
    return true;
}

function checkNickName()
{
    if ($("txtNickName").value.Trim()=="")
    {
        $("tip_txtNickName").innerHTML="请填写您的昵称";
        return false;
    }
    $("tip_txtNickName").innerHTML="";
    return true;
}

function checkBloodtype()
{
    var obj=$("selBloodtype");
    if (obj.options[obj.selectedIndex].value==0)
    {
        $("tip_selBloodtype").innerHTML="请选择您的血型";
        return false;
    }
    $("tip_selBloodtype").innerHTML="";
    return true;
}

function chekEducation()
{
    var obj=$("selEducation");
    if (obj.options[obj.selectedIndex].value==0)
    {
        $("tip_selEducation").innerHTML="请选择您的学历";
        return false;
    }
    $("tip_selEducation").innerHTML="";
    return true;
}

function chekSalary()
{
    var obj=$("selSalary");
    if (obj.options[obj.selectedIndex].value==0)
    {
        $("tip_selSalary").innerHTML="请选择您的月薪";
        return false;
    }
    $("tip_selSalary").innerHTML="";
    return true;
}

function checkHeight()
{
    var obj=$("selHeight");
    if (obj.options[obj.selectedIndex].value==0)
    {
        $("tip_selHeight").innerHTML="请选择您的身高";
        return false;
    }
    $("tip_selHeight").innerHTML="";
    return true;
}

function checkSex()
{
    if (($("radioMan").checked==false) && ($("radioWomen").checked==false))
    {
        $("tip_radiosex").innerHTML="请选择性别";
        return false;
    }
    $("tip_radiosex").innerHTML="";
    return true;
}

function checkNativeplace()
{
    var obj=$("selNativeplaceCountry");
    if (obj.value=="")
    {
        $("tip_selNativeplaceCountry").innerHTML="请选择您的籍贯";
        return false;
    }
    if (obj.options[obj.selectedIndex].value==0)
    {
        $("tip_selNativeplaceCountry").innerHTML="请选择您的籍贯";
        return false;
    }
    $("tip_selNativeplaceCountry").innerHTML="";
    return true;
}

function checkLocality()
{
    var obj=$("selLocalityCountry");
    if (obj.value=="")
    {
        $("tip_selLocalityCountry").innerHTML="请选择您的所在地";
        return false;
    }
    if (obj.options[obj.selectedIndex].value==0)
    {
        $("tip_selLocalityCountry").innerHTML="请选择您的所在地";
        return false;
    }
    $("tip_selLocalityCountry").innerHTML="";
    return true;
}


FormIni();
