﻿var year,month,day,country,province,city,gender,terms,regusernameagree,ispost;
var nationality,currented,witha,mothertongue,sought;
function regajaxcheckusername(){
    var url = "/ajax.aspx?action=checkusername&d=" + new Date();
    var pars = "username=" + $F("username");
    new Ajax.Request(url,{method: 'post', parameters: pars, onComplete: checkUsernameReturn});
}
function checkUsernameReturn(originalRequest){
    var t = originalRequest.responseText;
    if(t == -1){
        var m = "";
        if(regusernameagree.length > 0)
            m = error_username_3.interpolate({ agree : regusernameagree.replace(/\|/g, " ")});
        else
            m = error_username_4;
        regcreateicon(false, 'labelusername','divusername', m);
        if(pagelanguage.language==1){
            var len = $F("username").match(/[0-9]/g).length;
            if(len>4)
                alert("用户名不能有4个以上的数字.");
        }
        return false;
    }            
    if(t == 1){
        regcreateicon(false, 'labelusername', 'divusername', error_username_5);
        return false;
    }
    return true;
}
function regcheckusername(){
    var u = "username";
    $(u).value = $F(u).strip();
    var em = new Array();
    var b = (/^[0-9a-zA-Z]+$/.test($F(u)) && $F(u).length > 2 && $F(u).length < 17);
    regcreateicon(b,"labelusername","divusername",error_username_1);
    if($F("email").length > 0){
        em = $F("email").split("@");
        if(em.length > 1 && em[0] == $F(u)){ 
            b = false; 
            regcreateicon(b,"labelusername","divusername",error_username_2); 
        }
    }
    if(b) regajaxcheckusername();
    return b;
}
function regcheckpassword(){
    var u = $F("username");
    var p = $F("password");
    var b = (p.length > 5 && p.length < 21);
    //if(b) {
        //b = (u!=p);
        //if(b){
        //    regpasswordstrength();
        //    return b;
        //}
        //else{
        //    return regcreateicon(b,"labelpassword","divpassword",error_password_4);
        //}
    //}
    return regcreateicon(b,"labelpassword","divpassword",error_password_1);
} 
function CheckPasswordStrength(t,m){
    var d = PasswordStrength.Get(t.value);
    $(m).innerHTML = "";
    $(m).appendChild(d);
}
function regpasswordstrength(){
    var p = $F("password");
    var b = !(/^(\w)\1{1,}$/.test(p) || ("01234567890abcdefghijklmnopqrstuvwxyz".indexOf(p) > -1));
    return regcreateicon(b,"labelpassword","divpassword",error_password_3);
}
function regcheckconfirmpassword(){
    var b = ($F("confirmpassword") == $F("password") && !$F("confirmpassword").blank());
    return regcreateicon(b,"labelconfirmpassword","divconfirmpassword",error_password_2);
}
function regajaxcheckemail(){
    var url = "/ajax.aspx?action=checkemail&d=" + new Date();
    var pars = "email=" + $F("email");
    var myAjax = new Ajax.Request(url,{method: 'post', parameters: pars, onComplete: checkEmailReturn});
}
function checkEmailReturn(originalRequest){
    var t = originalRequest.responseText;
    if(t == -1){
        regcreateicon(false, 'labelemail', 'divemail', error_email_1);
        return false;
    }            
    if(t == 1){
        regcreateicon(false, 'labelemail', 'divemail',error_email_3 );
        return false;
    }
    return true;
}
function regcheckemail(){
    var eml = "email";
    $(eml).value =  $F(eml).strip();
    var em = new Array();
    var b = regcreateicon(CheckString.email($F(eml)),"labelemail","divemail",error_email_1);
    if(b){
        if($F(eml).length > 0){
            em = $F(eml).split("@");
            if(em.length > 1 && em[0] == $F("username")){
                regcreateicon(false, 'labelemail', 'divemail',error_email_2);
            } else regajaxcheckemail();
        }
    }
    return b;
}
function regcheckconfirmemail(){
    var b = (CheckString.email($F("confirmemail")) && $F("email") == $F("confirmemail"));
    return regcreateicon(b,"labelconfirmemail","divconfirmemail",error_email_4);
} 
function regcheckgender(){
    var b = ($("genderman").checked || $("genderwomen").checked);
    return regcreateicon(b,"labelgender","divgender",error_gender_1);
}
function regcheckbirthday(){
    var y = $F("year");
    var m = $F("month");
    var d = $F("day");
    if(y!=0 && m!=0 && d!=0)
        return regcreateicon(true,"labelbirthday","divbirthday", "");
    else
        return regcreateicon(false,"labelbirthday","divbirthday", error_birthday);
}
function regcheckcountry(){
    return regcreateicon(($F("country") != 0),"labelcountry","divcountry",error_country_1);
}
function regcheckprovince(){
    return regcreateicon(($F("province") != 0 && !$F("province").blank()),"labelprovince","divprovince",error_province);
}
function regcheckcity(){
    return regcreateicon(($F("city") != 0 && !$F("city").blank()),"labelcity","divcity",error_city);
}
function regcheckterms(){
    var b = ($("terms").checked);
    return regcreateicon(b,"labelterms","divterms",error_terms_1);
}
function regcheckuserinput()
{
    var err = "";
    var em = new Array();
    var bu = true;
    if(!regcheckusername()){
        if($F("email").length > 0){
            em = $F("email").split("@");
            if(em.length > 1 && em[0] == $F("username")){ bu = false; err += error_username_2 + "\r\n ";} 
        }
        if(bu) err += error_username_2 + "\r\n";
    }
    if(!regcheckpassword()) err += error_password_1 + "\r\n";
    //if(!regpasswordstrength()) err += error_password_3 + "\r\n";
    if(!regcheckconfirmpassword()) err += error_password_2 + "\r\n";   
    if(!regcheckemail()) err += error_email_1 + "\r\n";
    if(!regcheckconfirmemail()) err += error_email_3 + "\r\n";
    if(!regcheckgender()) err += error_gender_1 + "\r\n";
    if(!regcheckbirthday()) err += error_birthday + "\r\n";
    if(!regcheckcountry()) err += error_country_1 + "\r\n";
    if(province==0 && ($("province").options.length > 1)){
        if(!regcheckprovince()) err += error_province + "\r\n";
    }
    if(city==0 && ($("city").options.length > 1)){
        if(!regcheckcity()) err += error_city + "\r\n";
    }
    if(!regchecknationality()) err += error_nationality + "\r\n";
    if(!regcheckcurrented()) err += error_currented_1 + "\r\n";
    if(!regchecksought()) err += error_sought_1 + "\r\n";
    if(!regcheckwitha()) err += error_witha + "\r\n";
    if(!regcheckfirstlanguage()) err += error_mothertongue_1 + "\r\n";   
    if(!regcheckterms()) err += error_terms_1 + "\r\n";
    if(err.length > 0){ 
        alert(err); 
        return false; 
    }
    else return true;
} 
function regcreateicon(b,id1,id2,innerhtml){
    var rc = "red selectrightlabel";
    var gc = "green selectrightlabel";
   if(b){
        $(id1).innerHTML = "√"; // "<img src=\"/images/icons/tickTrue.gif\" />";
        if(Element.hasClassName($(id1), rc)) 
            Element.removeClassName($(id1), rc);  
        Element.addClassName($(id1), gc);
        if($(id2))
            $(id2).hide();
   }else{
        $(id1).innerHTML = "X";
        if(Element.hasClassName($(id1), gc)) 
            Element.removeClassName($(id1), gc);  
        Element.addClassName($(id1), rc);
        if($(id2)){
            $(id2).show();
            $(id2).innerHTML = "";
            if(!innerhtml.blank())
                $(id2).appendChild(GetErrorMessage(innerhtml));
        }
   }
   return b;
}
function optionsClear(e){
    e.options.length = 0;
}
function getProvince(){
    if($F("country") > 0){
        optionsClear($("province"));
        optionsClear($("city"));
        $("province").options.add(new Option(error_waitreturn, 0));
        $("city").options.add(new Option(error_waitreturn, 0));
        $("province").disabled = true;
        $("city").disabled = true;
        var url = "/ajax.aspx?action=getprovince&d=" + new Date();
        var pars = "countryid=" + $F("country")
        new Ajax.Request(url,{method: 'get', parameters: pars, onComplete: getProvinceReturn});
    }
    else{
        optionsClear($("province"));
        optionsClear($("city"));
        $("province").options.add(new Option(" ", 0));
        $("city").options.add(new Option(" ", 0));
        $("province").disabled = true;
        $("city").disabled = true;
    }
    regcheckcountry();
}
function getProvinceReturn(originalRequest){
    optionsClear($("province"));
    $("province").disabled = false;
    $("province").options.add(new Option(error_province_1, 0));
    var dataArray = originalRequest.responseXML.getElementsByTagName("list");
    if (dataArray.length > 0){
        for (var i=0; i<dataArray.length; i++){
            $("province").options.add(new Option(dataArray[i].getAttribute("text"), dataArray[i].getAttribute("value")));
        }
        if(province > 0){
            $("province").value = province;
            province = 0;
            getCity();
        }
        else {
            optionsClear($("city"));
            $("city").options.add(new Option(" ", 0));
            $("city").disabled = true;
        }
    }
}
function getCity(){
    if($F("province") > 0){
        optionsClear($("city"));
        $("city").options.add(new Option(error_waitreturn, 0));
        $("city").disabled = true;
        var url = "/ajax.aspx?action=getcity&d=" + new Date();
        var pars = "provinceid=" + $F("province") + "&countryid=" + $F("country")
        new Ajax.Request(url,{method: 'get', parameters: pars, onComplete: getCityReturn});
    }
    else {
        optionsClear($("city"));
        $("city").options.add(new Option(" ", 0));
        $("city").disabled = true;
    }
    regcheckprovince();
}
function getCityReturn(originalRequest){
    optionsClear($("city"));
    $("city").disabled = false;
    $("city").options.add(new Option(error_city_1, 0));
    var dataArray = originalRequest.responseXML.getElementsByTagName("list");
    if (dataArray.length > 0){
        for (var i=0; i<dataArray.length; i++){
            $("city").options.add(new Option(dataArray[i].getAttribute("text"), dataArray[i].getAttribute("value")));
        }
        if(city > 0){$("city").value = city;regcheckprovince();city = 0;}
    }
} 
function BindPage(){
    if(ispost){
        country = $F("country");
        BindBirthday();
        if(gender == 5) 
            $("genderman").checked = true;
        else if(gender == 6) 
            $("genderwomen").checked = true;
        $("nationality").value = nationality;
        $("currented").value = currented;
        $("witha").value = witha;
        $("mothertongue").value = mothertongue;
        if(!sought.blank()){
            var so = document.getElementsByName("sought");
            for(var i=0,len=so.length; i<len; i++){
                if(sought.indexOf(so[i].value)>-1)
                    so[i].checked = true;
            }
        }
        if(terms == 0) $("terms").checked = true;
        getProvince();
    
        regcheckuserinput();
    }
}
function BindBirthday(){
    $("year").value = year;
    $("month").value = month;
    $("day").value = day;
}
function checkuserinputstep2(){
    var err = "";
    if(!regcheckcurrented()) err += error_currented_1 + "\r\n";
    if(!regchecksought()) err += error_sought_1 + "\r\n";
    if(!regcheckfirstlanguage()) err += error_mothertongue_1 + "\r\n";   
    if(err.length > 0){alert(err);return false;} else return true;
}
function regcheckcurrented(){
    var b = ($("currented").value > 0);
    return regcreateicon(b,"labelcurrented","divcurrented",error_currented_1);
}
function regchecksought(){
    var ck = document.getElementsByName("sought");
    var b = false;
    for(var i=0; i<ck.length; i++){
        if(ck[i].checked){ b = true; break; }
    } 
    return regcreateicon(b,"labelsought","divsought",error_sought_1);
}
function regchecknationality(){
    var b = ($("nationality").value > 0);
    return regcreateicon(b,"labelnationality","divnationality",error_nationality);
}
function regcheckwitha(){
    var b = ($("witha").value > 0);
    return regcreateicon(b,"labelwitha","divwitha", error_witha);
}
function regcheckfirstlanguage(){
    var b = ($F("mothertongue") != 0);
    return regcreateicon(b,"labelmothertongue","divmothertongue",error_mothertongue_1);
}
function regcheckchinese(){
    var b = ($F("chinese") != 0);
    return regcreateicon(b,"labelchinese","divchinese",error_chinese_1);
}
function regcheckenglish(){
    var b = ($F("english") != 0);
    return regcreateicon(b,"labelenglish","divenglish",error_english_1);
}
function regcheckheadline2(){
    var b = ($F("headline2").length > headline_length_min && $F("headline2").length < headline_length_max);
    return regcreateicon(b,"labelheadline2","divheadline2",error_headline_1);
}
function regcheckaboutyou(){
    var b = ($F("aboutyou").length > aboutyou_length_min && $F("aboutyou").length < aboutyou_length_max);
    return regcreateicon(b,"labelaboutyou","divaboutyou1",error_aboutyou_1);
}
function regcheckdescription(){
    var b = ($F("description").length > description_length_min && $F("description").length < description_length_max);
    var f = regcreateicon(b, "labeldescription", "divdescription", error_description_1);
    if(!f){
        alert(error_description_1);
        $("description").focus();
        Element.scrollTo($("description"));
        return false;
    } else return true;
}
function TagTextareaLength(textareaId, divId, minlength, maxlength){
    var a = $(textareaId);
    var b = $(divId);
    var al = a.value.length;
    var txt = error_string_viewlength;
    if(al > maxlength){
        a.value = a.value.slice(0, maxlength);
        TagTextareaLength(textareaId, divId, minlength, maxlength);
    }
    else{
        b.innerHTML = txt.interpolate({minlength:minlength,maxlength:maxlength,color:((al < minlength) ? "red" : "green"),stringlength:a.value.length});
    }
}


