/*
对用户资料进行简单验证，很多项目是选填的
与contactor_common.js不同，后者是按域名联系人的标准进行严格限制
*/

function onFocusHandle(whInputObj){
    var name = whInputObj.input.name;
    var tip = $(name+'_msg');
    if(tip&&ContactorPromptMsgs[name]){
        tip.className = 'field-validation field-validation-default';
        tip.innerHTML = ContactorPromptMsgs[name];
    }
}

//公共的联系人验证规则
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:check_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:check_fax_qu},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){
        if(elms['cOrganize']['value']==''||elms['Organize']['value']==''){
            alert('当用户类型为“公司用户”时，请务必填写“公司名称”');
            try{elms.cOrganize.focus();}catch(e){}
            return false;
        }
        if(elms['Companynum']){
            if($('IDCardtype_sel').value!=0){
                if(!check_Companynum(elms['Companynum'].value)){
                    alert('请您填写证件号码！');
                    return false;
                }
            }
        }
    }
    if(!c_mobile(elms['mobile'].value)&&
       !check_phonenum(elms['phonenum'].value,elms['phonenum'])){
        alert('座机和手机号码请至少填写一项');
        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 common_opts = {
    event_name:'blur',
    rclass:'field-validation field-validation-right',
    wclass:'field-validation field-validation-error',
    wstr:'',
    checking_str:'',
    onFocus:onFocusHandle,
    onWrong:function(wh_input){
        var input = wh_input.input;
        var name = input.name;

        if(name=='qu' || name=='fax_qu'){
            switch(name){
            case 'Firstname':
            case 'Lastname':
                var name = 'Name';
                var tip = $('Name_tip');
                break;
            case 'qu':
                var name = 'phonenum';
                break;
            case 'fax_qu':
            case 'fax_phonenum':
                var name = 'fax_phonenum';
                break;                
            }
        }

        if(name=='qu'||name=='phonenum'){
            if(c_mobile(input.form.mobile.vlaue))
                return;
        }

        //如果手机为空，只要固话正确，仍然通过
        if(name=='mobile'&&input.value==''){
            var phonenum = input.form.phonenum.value;
            if(check_phonenum(phonenum,input)&&phonenum!='')
                return;
        }

        var tip = $(name+'_msg');
        if(!tip) return;
        tip.innerHTML = ContactorErrorMsgs[name];
        tip.className = 'field-validation field-validation-error';
        tip.style.display = '';
    },
    onRight:function(wh_input){
        var input = wh_input.input;
        var name = input.name;

        if(name=='qu' || name=='fax_qu'){
            switch(name){
            case 'Firstname':
            case 'Lastname':
                var name = 'Name';
                var tip = $('Name_tip');
                break;
            case 'qu':
                var name = 'phonenum';
                break;
            case 'fax_qu':
            case 'fax_phonenum':
                var name = 'fax_phonenum';
                break;                
            }
        }
        var tip = $(name+'_msg');
        if(!tip) return;

        if(name=='qu'||name=='phonenum'){
            var mobile_n = input.form.mobile.value;
            var m_tip = $('mobile_msg');
            if(c_mobile(mobile_n)){
                m_tip.innerHTML = '&nbsp;';
                m_tip.className = 'field-validation field-validation-right';
            }else if(mobile_n==''){
                m_tip.innerHTML = '';
                m_tip.className = 'field-validation-normal';
            }

            //由于手机号正确填写而使空值验证通过，不提示正确图标
            if(input.form.qu.value==''&&input.form.phonenum.value==''){
                tip.innerHTML = '';
                tip.className = 'field-validation-normal';
                return;
            }
        }

        if(name=='mobile'){
            var phonenum = input.form.phonenum.value;
            var m_tip = $('phonenum_msg');
            if(phonenum==''&&input.form.qu.value==''){
                m_tip.innerHTML = '';
                m_tip.className = 'field-validation-normal';
            }else if(check_phonenum(phonenum,input) &&
                     check_qu(input.form.qu.value,input)){
                m_tip.innerHTML = '&nbsp;';
                m_tip.className = 'field-validation field-validation-right';
            }
            //由于固话正确填写而使空值验证通过，不提示正确图标
            if(input.value==''){
                tip.innerHTML = '';
                tip.className = 'field-validation-normal';
                return;
            }
        }


        if(name=='username') tip.innerHTML = '用户名可以使用';
        else tip.innerHTML = '&nbsp;';
        tip.className = 'field-validation field-validation-right';
    }
};

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 ''==v||/^\d{6}$/.test(v);
}

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

function check_mobile(v,input){
    var res = c_mobile(v);
    if(!res&&v==''){ //为空则检查固话
        var phonenum = input.form.phonenum.value;
        return check_phonenum(phonenum,input)&&phonenum!='';
    }
    return res;
}

function check_qu(v,input){
    var reg = /^\d{2,5}$/;
    var num = input.form.elements.phonenum.value;
    if(c_tel(num)){
        return reg.test(v);
    }else if(num==''){
        if(''==v||reg.test(v))
            return c_mobile(input.form.mobile.value);
    }
    return false;
}

function check_phonenum(v,input){
    var qu = input.form.elements.qu.value;
    if(/^\d{2,5}$/.test(qu)){
        return c_tel(v);
    }else if(qu==''){
        if(''==v) return c_mobile(input.form.mobile.value);
    }
    return false;
}

function check_fax_qu(v){
    var reg = /^\d{2,5}$/;
    var num = $('fax_phonenum').value;
    if(c_tel(num)){
        return reg.test(v);
    }else if(num=='') return ''==v;
    else return false;
}

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

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

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;
        }
    }
}
