//公共的联系人验证规则
function initCheckForm(cform){
    initCheckForm.wh_form = cform;
    cform.pushInput('cLastname',Object.extend({check:check_cName},common_opts));
    cform.pushInput('cFirstname',Object.extend({check:check_cName},common_opts));
    cform.pushInput('Lastname',Object.extend({check:check_Name},common_opts));
    cform.pushInput('Firstname',Object.extend({check:check_Name},common_opts));
    cform.pushInput('cCity',Object.extend({check:check_cCity},common_opts));
    cform.pushInput('cAddress1',Object.extend({check:check_cAddress1},common_opts));
    cform.pushInput('PostalCode',Object.extend({check:check_PostalCode},common_opts));
    cform.pushInput('Email',Object.extend({check:c_mail},common_opts));
    cform.pushInput('mobile',Object.extend({check:c_mobile},common_opts));
    cform.pushInput('qu',Object.extend({check:check_qu},common_opts));
    cform.pushInput('phonenum',Object.extend({check:check_phonenum},common_opts));
    cform.pushInput('fax_qu',Object.extend({check:function(v){return ''==v||check_qu(v)}},common_opts));
    cform.pushInput('fax_phonenum',Object.extend({check:check_fax_phonenum},common_opts));
    cform.pushInput('cOrganize',Object.extend({check:check_cOrganize},common_opts));
    cform.pushInput('cJobtitle',Object.extend({check:check_cJobtitle},common_opts));
    cform.pushInput('City',Object.extend({check:check_City},common_opts));
    cform.pushInput('Address1',Object.extend({check:check_Address1},common_opts));
    cform.pushInput('Organize',Object.extend({check:check_Organize},common_opts));
    cform.pushInput('Jobtitle',Object.extend({check:check_Jobtitle},common_opts));
    if(cform.form.elements['Companynum'])
        cform.pushInput('Companynum',Object.extend({check:check_Companynum},common_opts));
}

//表单验证完成之后,一些附加处理
function contactorAffix(elms){
    var usertype_co = $('usertype_co');
    if(usertype_co&&usertype_co.checked&&(elms['cOrganize']['value']==''||elms['Organize']['value']=='')){
        alert('当用户类型为“公司用户”时，请务必填写“公司名称”');
        try{elms.cOrganize.focus();}catch(e){}
        return false;
    }
    
    if(elms['qu'].value!=''&&elms['phonenum'].value!='')
        elms['phone'].value = elms['qu'].value+'-'+elms['phonenum'].value;
    if(elms['fax_qu'].value!=''&&elms['fax_phonenum'].value!='')
        elms['fax'].value = elms['fax_qu'].value+'-'+elms['fax_phonenum'].value;
    if(elms['cCountry'].value=='其他')
        elms['cStateProvince'].selectedIndex=elms['cStateProvince'].options.length-1;
    if(elms['Country'].value=='Others')
        elms['StateProvince'].selectedIndex=elms['StateProvince'].options.length-1;
        
    var name = elms['Firstname'].value;
    elms['Firstname'].value = name.replace(/[^a-z]/ig,'');
    name = elms['Lastname'].value;
    elms['Lastname'].value = name.replace(/[^a-z]/ig,'');
    
    return true;
}

var errmsgs = {
    username:'用户名只能由数字/字母组成，长度在6-16之间',
    userpass1:'密码只能由数字/字母组成，长度在6-16之间',
    userpass2:'两次输入的密码不一致',
    cLastname:'中文姓输入有误',
    cAddress1:'中文联系地址有误(须包含4个以上汉字)',
    PostalCode:'邮编有误',
    cCity:'城市中文名有误',
    Email:'邮箱格式有误',
    mobile:'手机号码有误',
    cOrganize:'公司/组织中文名有误',
    cJobtitle:'请正确填写联系人在公司/组织中的职务中文名称',
    Organize:'公司/组织名(英文/拼音)输入有误',
    Jobtitle:'请正确填写联系人在公司/组织中的职务英文/拼音名称',
    Address1:'联系地址(英文/拼音)输入有误(长度在8-64个字符)',
    City:'城市(英文/拼音)输入有误',
    IDCard:'身份证号码填写有误',
    qqnumber:'QQ号码有误',
    Companynum:'证件号码为空或包含特殊字符，请重新填写',
    validateCode1:'验证码错误',
    validateCode:'验证码错误'
}

var common_opts = {
    event_name:'blur',
    wstr:'',
    checking_str:'',
    onWrong:function(wh_input){
        var input = wh_input.input;
        //Element.setStyle(input,{/*borderColor:'#CC624C',*/backgroundColor:'#F7B3A0'});
        var name = input.name;
        if(name=='cFirstname'||name=='cLastname'){
            $('cName_err').style.display = 'inline';
            return;
        }
        if(name=='Firstname'||name=='Lastname'){
            $('Name_err').style.display = 'inline';
            return;
        }
        if(name=='qu'||name=='phonenum'){
            $('phone_err').style.display = 'inline';
            return;
        }
        
        if(name=='fax_qu'||name=='fax_phonenum'){
            $('fax_err').style.display = 'inline';
            return;
        }
        var msgbox = $(name+'_msg');
        msgbox.innerHTML = '<br />'+errmsgs[name];
        msgbox.style.color = 'red';
        msgbox.style.display = 'inline';
    },
    onRight:function(wh_input){
        var input = wh_input.input;
        //Element.setStyle(input,{/*borderColor:'',*/backgroundColor:''});
        var name = input.name;
        if(name=='cFirstname'||name=='cLastname'){
            $('cName_err').style.display = 'none';
        }
        if(name=='Firstname'||name=='Lastname'){
            $('Name_err').style.display = 'none';
        }
        if(name=='qu'||name=='phonenum'){
            $('phone_err').style.display = 'none';
        }
        if(name=='fax_qu'||name=='fax_phonenum'){
            $('fax_err').style.display = 'none';
        }
        var msgbox = $(name+'_msg');
        msgbox.style.color = 'green';
        if(input.value == '') msgbox.innerHTML = '';
        else msgbox.innerHTML = '√';
        if(name=='username') msgbox.innerHTML += ' 用户名可以使用';
        //$(name+'_err').style.visibility = 'hidden';
    }
};

function check_cName(v,input){
    return c_chinese(v)&&c_ChineseCount(v,1);
}

function check_Name(v,input){
    input.value = v.replace(/[^a-z]/ig,'');   //去掉英文名中的非英文字符
    return c_e_LetterCount(v,1);
}

function check_cAddress1(v){
    return c_ChineseCount(v,4);
}

function check_PostalCode(v){
    return /^\d{6}$/.test(v);
}

function check_cCity(v){
    return c_ChineseCount(v,2);
}

function check_qu(v){
//座机号强制填写,20080602    
//    return ''==v||/^\d{2,5}$/.test(v);
    return /^\d{2,5}$/.test(v);
}

function check_Companynum(v){
    return ''==v||(/^[-_0-9a-z]+( [-_0-9a-z]+)*$/i.test(v)&&v.length>=4);
}

function check_fax_phonenum(v,input){
    var fax_qu = input.form.elements.fax_qu;
    if(''==fax_qu.value&&''==v) return true;
    else return c_tel(v)&&(/^\d{2,5}$/.test(fax_qu.value));
}

function check_phonenum(v,input){
    var qu = input.form.elements.qu;
//座机号强制填写,20080602    
//    if(''==qu.value&&''==v) return true;
//    else
    return c_tel(v)&&(/^\d{2,5}$/.test(qu.value));
}

function check_cOrganize(v,input){
    var usertype_co = $('usertype_co');
    if(usertype_co&&usertype_co.checked) return c_ChineseCount(v,2);
    return ''==v||c_ChineseCount(v,2);
}

function check_cJobtitle(v,input){
    var usertype_co = $('usertype_co');
    if(usertype_co&&usertype_co.checked){
        return c_ChineseCount(v,2);
    }else return ''==v||c_ChineseCount(v,2);
}

function check_Organize(v){
    var usertype_co = $('usertype_co');
    if(usertype_co&&usertype_co.checked) return c_e_LetterCount(v,2);
    return ''==v||c_e_LetterCount(v,2);
}

function check_Jobtitle(v,input){
    var usertype_co = $('usertype_co');
    if(usertype_co&&usertype_co.checked){
        return c_e_LetterCount(v,1);
    }else return ''==v||c_e_LetterCount(v,1);
}

function check_Address1(v){
    return c_e_LetterCount(v,8)&&(v.length<=64);
}

function check_City(v){
    return c_e_LetterCount(v,2);
}

//获得一个字符串的拼音
function getSpell(str,target_ele){
    if(!c_ChineseCount(str,1)) return;
    new Ajax.Request('/ajax/reg_simple/get_spell.php',{
        method:'get',
        parameters:'hz='+encodeURIComponent(str),
        onSuccess:function(req){
            var rtext = req.responseText;
            var name = target_ele.name;
            if(name=='Firstname'||name=='Lastname'||'City'==name){
                //if(target_ele.value!='') return;
                rtext = rtext.replace(/\s/g,'');
            }
            if(name=='Address1'&&rtext.length>64){ //英文地址
                rtext = rtext.replace(/ /mg,'');
                rtext = rtext.substr(0,64);
            }
            if(name=='Organize'){
                if(rtext.length>64) rtext = rtext.substr(0,64);
                rtext = rtext.replace(/ /mg,'');
                var midd = Math.floor(rtext.length/2);
                rtext = rtext.substr(0,midd)+' '+rtext.substr(midd);
            }
            target_ele.value = rtext;
            try{    //填充过后将文本框设置为正确状态
                var inputObj = initCheckForm.wh_form.inputs[target_ele.name];
                inputObj.handle(rtext,target_ele);
            }catch(e){}
        }
    });
}


/** 根据值选中一个下拉列表的项
* @param HTMLSelectElement select
* @param string|int val
* @param bool case_insensitive 忽略大小写. default false
*/
function selectOption(select,val,case_insensitive){
    var opts = select['options'],
    opt,opt_val;
    if(case_insensitive) val = val.toLowerCase();
    for(var i=0,len=opts.length;i<len;i++){
        opt = opts[i];
        opt_val = opt['value'];
        if(case_insensitive) opt_val = opt_val.toLowerCase();
        if(opt_val==val){
            opt.selected = true;
            return;
        }
    }
}
