﻿function isEnoughLength(value, strlength, emptycallback) {
    ///	<summary>
    ///		验证value是长度大于等于length
    ///	</summary>
    if (!strlength) strlength = 1;
    if ($.trim(value).length < strlength) {
        if (emptycallback)
            emptycallback();
        return false;
    }
    return true;
}
function ValidateAll() {
    ///	<summary>
    ///		验证所有需要验证的内容
    ///	</summary>
    var result = true;
    if (!ValidateLength('firstname', 'firstnamespan', 2)) result = false;
    //    if (!ValidateLength('lastname', 'lastnamespan')) result = false;
    else if (!ValidateEmail('email_address', 'email_address_span', 'email_address_existspan')) result = false;
    //else if (!ValidateMatch('email_address', 'email_address_confirm', 'email_address_confirm_span')) result = false;
    //else if (!ValidateLength('street_address', 'street_addressspan', 5)) result = false;
    // else if (!ValidateLength('postcode', 'postcodespan', 4)) result = false;
    //else if (!ValidateLength('city', 'cityspan', 3)) result = false;
    else if (!ValidateLength('password', 'passwordspan', 1)) result = false;
    else if (!ValidateMatch('confirmPwd', 'password', 'conformationspan')) result = false;
    else if (!ValidateCountry('dic_Country', 'countryspan')) result = false;


    return result;
}

function ValidateCountry(textboxid, spanid) {
    textboxid = registerprofix + textboxid;
    if ($.GetValue($(textboxid)).charAt(0) == '(') { $.show($(spanid)); return false; }
    else { $.hide($(spanid)); return true; }
}
function ValidateLength(textboxid, spanid, strlength) {
    ///	<summary>
    ///		验证textbox内容的长度是否大于等于strlength
    ///	</summary>
    ///	<returns type="bool" />
    ///	<param name="textboxid" type="string">
    ///		文本框的ID(文本框如果是服务器控件，则需要将registerprofix赋予控件的前缀)
    ///	</param>
    ///	<param name="spanid" type="string">
    ///		显示的标签ID
    ///	</param>

    textboxid = registerprofix + textboxid;
    if (isEnoughLength($.GetValue(textboxid), strlength))
    { $.hide($(spanid)); return true; }
    else {
        $.show($(spanid));
        return false;
    }
}

function ValidateEmail(textboxid, spanid, existspanid) {
    ///	<summary>
    ///		验证textbox内容的长度是否大于等于strlength
    ///	</summary>
    ///	<returns type="bool" />
    ///	<param name="textboxid" type="string">
    ///		文本框的ID(文本框如果是服务器控件，则需要将registerprofix赋予控件的前缀)
    ///	</param>
    ///	<param name="spanid" type="string">
    ///		显示不符合要求的标签ID
    ///	</param>
    ///	<param name="existspanid" type="string">
    ///		如果邮件已存在要显示此标签
    ///	</param>
    var regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    textboxid = registerprofix + textboxid;
    var emailaddress = $.trim($.GetValue(textboxid));
    var passfirst = emailaddress.match(regex)/*是否匹配邮箱地址的要求*/;
    passfirst ? $.hide($(spanid)) : $.show($(spanid));
    $.hide($(existspanid));
    try {
        if (passfirst) {
            var exist = AjaxMethod.IsExistEmail(emailaddress);
            exist.value ? $.show($(existspanid)) : $.hide($(existspanid));
            if (!exist.value) return true;
        }
    } catch (e) {
        //如果触发异常，表示是无需验证邮箱是否存在的页面调用本函数，所以直接返回true
        return true;
    }
    return false;
}
function ValidateMatch(textboxid1, textboxid2, spanid) {
    ///	<summary>
    ///		验证两个Textbox的内容是否相同
    ///	</summary>
    textboxid1 = registerprofix + textboxid1;
    textboxid2 = registerprofix + textboxid2;
    if ($.trim($.GetValue(textboxid1)) == $.trim($.GetValue(textboxid2))) {
        $.hide($(spanid)); return true;
    } else { $.show($(spanid)); return false; }
}