﻿// JavaScript Document
function getOvalue(obj){
    var objO = obj.options[obj.selectedIndex].text;// 写法一
    //var objO = obj.options[obj.selectedIndex].innerHTML; //写法二
    var inputT = document.getElementById("board");
    inputT.value = objO;
//form1.board.value = objO;
}

//以下，通用表单验证，可添加不同的验证内容

//身份证验证

function checkIdcard(obj,idcard){
    var Errors=new Array(
        "验证通过!",
        "身份证号码位数不对!",
        "身份证号码出生日期超出范围或含有非法字符!",
        "身份证号码校验错误!",
        "身份证号填写不规范!"
        );
    var area={
        11:"北京",
        12:"天津",
        13:"河北",
        14:"山西",
        15:"内蒙古",
        21:"辽宁",
        22:"吉林",
        23:"黑龙江",
        31:"上海",
        32:"江苏",
        33:"浙江",
        34:"安徽",
        35:"福建",
        36:"江西",
        37:"山东",
        41:"河南",
        42:"湖北",
        43:"湖南",
        44:"广东",
        45:"广西",
        46:"海南",
        50:"重庆",
        51:"四川",
        52:"贵州",
        53:"云南",
        54:"西藏",
        61:"陕西",
        62:"甘肃",
        63:"青海",
        64:"宁夏",
        65:"新疆",
        71:"台湾",
        81:"香港",
        82:"澳门",
        91:"国外"
    }

    var obj,idcard,Y,JYM;
    var S,M;
    var idcard_array = new Array();
    idcard_array = idcard.split("");
    //地区检验
    if(area[parseInt(idcard.substr(0,2))]==null){
        alert(Errors[4]);
        formstyle(obj,0);
        obj.select();
        return false;
    }//return Errors[4];
    //身份号码位数及格式检验
    switch(idcard.length){
        case 15:
            if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
                ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
            } else {
                ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
            }
            if(ereg.test(idcard)){
                formstyle(obj,1);
                return true;
            }//return Errors[0];
            else {
                alert(Errors[2]);
                formstyle(obj,0);
                obj.select();
                return false;
            }//return Errors[2];
            break;
        case 18:
            //18位身份号码检测
            //出生日期的合法性检查
            //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
            //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
            if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){
                ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
            } else {
                ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
            }
            if(ereg.test(idcard)){//测试出生日期的合法性
                //计算校验位
                S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
                + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
                + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
                + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
                + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
                + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
                + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
                + parseInt(idcard_array[7]) * 1
                + parseInt(idcard_array[8]) * 6
                + parseInt(idcard_array[9]) * 3 ;
                Y = S % 11;
                M = "F";
                JYM = "10X98765432";
                M = JYM.substr(Y,1);//判断校验位
                if(M == idcard_array[17]) {
                    formstyle(obj,1);
                    return true;
                }//return Errors[0]; //检测ID的校验位
                else{
                    alert(Errors[3]);
                    formstyle(obj,0);
                    obj.select();
                    return false;
                }//return Errors[3];
            }
            else {
                alert(Errors[2]);
                formstyle(obj,0);
                obj.select();
                return false;
            }//return Errors[2];
            break;
        default:
            //return Errors[1];
            alert(Errors[1]);
            formstyle(obj,0);
            obj.select();
            return false;
            break;
    }

}//身份证验证

//去除右空格
function RTrim(str)
{
    var whitespace=new String(" \t\n\r　");
    var s=new String(str);
    if (whitespace.indexOf(s.charAt(s.length-1))!=-1)
    {
        var i=s.length-1;
        while (i>=0 && whitespace.indexOf(s.charAt(i))!=-1)
        {
            i--;
        }
        s=s.substring(0,i+1);
    }
    return s;
}

//去除左空格
function LTrim(str)
{
    var whitespace=new String(" \t\n\r　");
    var s=new String(str);
    if (whitespace.indexOf(s.charAt(0))!=-1)
    {
        var j=0, i = s.length;
        while (j<i && whitespace.indexOf(s.charAt(j))!=-1)
        {
            j++;
        }
        s=s.substring(j,i);
    }
    return s;
}
//去除两边的空格
function Trim(str)
{
    return RTrim(LTrim(str));
}


//邮箱验证函数
function isemail(obj){
    if(Trim(obj.value!=null)){
        var rege = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if(!rege.test(obj.value)){
            alert(obj.title + " 格式不正确!");
            formstyle(obj,0);
            obj.select();
            return false;
        }else{
            formstyle(obj,1);
            return true;
        }
    }else{
        alert("请输入"+obj.title+"！");
        formstyle(obj,0);
        return false;
    }
}//check is email ending

//检测电话号码（国内）
function phonecheck(obj){
    var regp = /(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)/;
    if (!regp.test(obj.value)){
        alert("您输入的"+obj.title+"格式不正确。");
        formstyle(obj,0);
        obj.select();
        return false;
    } else {
        formstyle(obj,1);
        return true;
    }
}

//检测手机号（国内）
function mobilecheck(obj){
    var reg=/^0{0,1}(13|15)[0-9]{9}$/;
    //alert(reg.test(str));
    if (!reg.test(obj.value)){
        alert("您输入的"+obj.title+"格式不正确。");
        formstyle(obj,0);
        obj.select();
        return false;
    } else {
        formstyle(obj,1);
        return true;
    }
}
//check mixture phone
function mixphone(obj){
    var reg=/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}(13|15)[0-9]{9}$)/;
    //alert(reg.test(str));
    if (!reg.test(obj.value)){
        alert("您输入的"+obj.title+"格式不正确。");
        formstyle(obj,0);
        obj.select();
        return false;
    } else {
        formstyle(obj,1);
        return true;
    }
}

//check date.The format is YYYY-MM-DD
function checkdate(obj){
    var reg = /^\d{4}-\d{1,2}-\d{1,2}$/;
    if(!reg.test(obj.value)){
        alert("您输入的"+obj.title+"格式不正确。");
        formstyle(obj,0);
        obj.select();
        return false;
    } else {
        formstyle(obj,1);
        return true;
    }
}


//check username.Must be latin and number
function checkuser(obj){
    var myID = /^[A-Za-z][A-Za-z0-9]{2,11}$/;
    if(!myID.test(obj.value)){
        alert("您输入的"+obj.title+"格式不正确。");
        formstyle(obj,0);
        obj.select();
        return false;
    } else {
        formstyle(obj,1);
        return true;
    }
}

//check no "|" string
function checkchar(obj){
	
    if (obj.value!=""){
        var mychar = obj.value;
        var str = /\|/g;
        var Results = mychar.replace(str,"");
        obj.value = Results;
        formstyle(obj,1);
        return true;
    }else{
        alert("请输入"+obj.title+"！");
        formstyle(obj,0);
        return false;
    }
}

//check is time
function checkisTime(obj){ 

    if(obj.value!=null){
        var regt = /^(0\d{1}|1\d{1}|2[0-3]):([0-5]\d{1})$/;
        if(!regt.test(obj.value)){
            alert(obj.title + "格式不正确(例:10:15)\n\n请按24小时格式填写时间。\n零点后十点前(00:00-09:59)\n十点后零点前(10:00-23:59)");
            formstyle(obj,0);
            obj.select();
            return false;
        }else{
            formstyle(obj,1);
            return true;
        }
    }else{
        alert("请填写"+obj.title+"！");
        formstyle(obj,0);
        return false;
    }
		
}//isTime function ending

//check is Flights
function checkisFlights(obj){ 

    if(obj.value!=null){
        var regf = /^([0-9]{1}[A-Z]{1}|[A-Z]{1}[0-9]{1}|[A-Z]{2})([0-9]{4})$/;
        if(!regf.test(obj.value)){
            alert(obj.title + "格式不正确(例:CA2046)\n航班号格式：航空公司代码+4位数字航班号");
            formstyle(obj,0);
            obj.select();
            return false;
        }else{
            formstyle(obj,1);
            return true;
        }
    }else{
        alert("请填写"+obj.title+"！");
        formstyle(obj,0);
        return false;
    }
		
}//checkisFlights function ending


//check is number
function checkisnum(obj){
	
    if (obj.value!=""){
        var regx = /^[1-9]+[0-9]*]*$/ ;
        if (!regx.test(obj.value)){
            alert(obj.title + "必需为数字");
            formstyle(obj,0);
            obj.select();
            return false;
        }else{
            formstyle(obj,1);
            return true;
        }
    }else{
        alert("请填写"+obj.title+"！");
        formstyle(obj,0);
        return false;
    }
}//checkisnum function ending


//check is zip.
function checkzip(obj){
	
    if (obj.value!=""){
        var regx = /^[1-9]{1}[0-9]{5}$/ ;
        if (!regx.test(obj.value)){
            alert(obj.title + "格式不正确(例:100000)");
            formstyle(obj,0);
            obj.select();
            return false;
        }else{
            formstyle(obj,1);
            return true;
        }
    }else{
        alert("请填写"+obj.title+"！");
        formstyle(obj,0);
        return false;
    }
}//checkzip function ending


//check is null
function checkisNull(obj){
    var myValue = Trim(obj.value);
    if (myValue.length<1){
        alert(obj.title+"不允许为空!");
        formstyle(obj,0);
        return false;
    }else{
        formstyle(obj,1);
        return true;
    }
	
}//checkForm function ending

//check is selected
function checkselected(obj){
    if (obj.value==''){
        alert(obj.title+"必须选择一项！");
        formstyle(obj,0);
        return false;
    }else{
        formstyle(obj,1);
        return true;
    }
	
}//checkForm function ending

//check is selected
function checkcheckbox(obj){
    if (obj.checked==false){
        alert("必须同意"+obj.title+"才可以继续注册!");
        formstyle(obj,0);
        return false;
    }else{
        formstyle(obj,1);
        return true;
    }
	
}//checkForm function ending

//check radio is checked
function checkradio(obj){
    for(var i = 0; i< obj.length; i++){
        if(obj[i].checked){
            alert("this is checked");
            return false;
            break;
        }
    }
}//checkradio function ending



//check length
function checklength(obj){
    if (obj.value.length < 6 || obj.value.length > 20){
        alert(obj.title+"长度为6-20个字符！");
        formstyle(obj,0);
        return false;
    }else{
        formstyle(obj,1);
        return true;
    }
	
}//checkForm function ending


//change the input style function
function formstyle(obj,flag){
    if (flag==0){
        obj.style.borderColor="#ff6600";
        obj.style.borderStyle = "solid";
        obj.style.borderWidth = "1px";
        obj.focus();
    }else{
        //obj.focus();
        obj.style.borderColor="#f1e3bb";
        obj.style.borderStyle = "solid";
        obj.style.borderWidth = "1px";
    }
	
}//formstyle function ending
var $=function (id){
    return "string" == typeof id ? document.getElementById(id) : id;
}


function checkform(frmObj){
    if($('pnum')){
        var value='';
        for(var i=1;i<=parseInt($('pnum').value);i++){
            //alert(i);
            var id="hmmmm"+i;
            //alert(id);
            var ob=$(id);
            var v= i==1 ? '' : '#';
            value+=v+ob.value;
            //alert(ob);
            if(!checkisNull($(id))){
                return false;
            }
        }
        $('hiddeninput').innerHTML="<input name='h' type='hidden' value='"+value+"' />";
    }
    var myInput = frmObj.getElementsByTagName("input");
        exitfor:
        //begin for
        for(var i = 0;i < myInput.length;i++){
            //alert(myInput[i].title);
            //check the input type is file
            if(myInput[i].type.toLowerCase()=="file"){
                //case the input
                switch (myInput[i].id){
                    case "mustnonull":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
                    default:
                        break;
                }//ending case
            }//ending check type is file
		
            //check the input type is password
            if(myInput[i].type.toLowerCase()=="password"){
                //case the input
                switch (myInput[i].id){
                    case "ilen":
                        if(!checklength(myInput[i])){
                            return false;
                        }
                        break;
                    case "password":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
                    default:
                        break;
                }//ending case
            }//ending check type is password

            //check the input type is checkbox
            if(myInput[i].type.toLowerCase()=="checkbox"){
                //case the input
                switch (myInput[i].id){
                    case "mustbecheck":
                        if(!checkcheckbox(myInput[i])){
                            return false;
                        }
                        break;
                    case "checkbox2":
                        if(!checkcheckbox(myInput[i])){
                            return false;
                        }
                        break;
                    default:
                        break;
                }//ending case
            }//ending check type is checkbox

            //check the input type is checkbox
            if(myInput[i].type.toLowerCase()=="radio"){
                //case the input
                switch (myInput[i].name){
                    case "st":
                        alert(checkradio(myInput[i].name));
                        break;
                        if(!checkradio(myInput[i].name)){
                            return false;
                        }
                        break;
                    default:
                        break;
                }//ending case
            }//ending check type is checkbox


            //check the input type is text
            if(myInput[i].type.toLowerCase()=="text"){
                //case the input
                switch (myInput[i].id){
                    case "mustnonull":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
                    case "outdate":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
                    case "getdate":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
                    case "mustnonull2":
                        if(checktitle){
                            if(!checkisNull(myInput[i])){
                                return false;
                            }
                        }
                        break;
                    case "fromw":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
                        break;
                    case "checknum":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
					
                    case "mustnum":
                        if(!checkisnum(myInput[i])){
                            return false;
                        }
                        break;
                    case "musttime":
                        if(!checkisTime(myInput[i])){
                            return false;
                        }
                        break;
                    case "username":
                        if(!checkisNull(myInput[i])){
                            return false;
                        }
                        break;
                    case "mustfly":
                        if(!checkisFlights(myInput[i])){
                            return false;
                        }
                        break;
                    case "testchar":
                        if(!checkchar(myInput[i])){
                            return false;
                        }
                        break;
                    case "testchar2":
                        if(!checkchar(myInput[i])){
                            return false;
                        }
                        break;
                    case "mustmail":
                        if(!isemail(myInput[i])){
                            return false;
                        }
                        break;
                    case "mustphone":
                        if(!mobilecheck(myInput[i])){
                            return false;
                        }
                        break;
                    case "mustmobile":
                        if(!mobilecheck(myInput[i])){
                            return false;
                        }
                        break;
                    case "mixphone":
                        if(!mixphone(myInput[i])){
                            return false;
                        }
                        break;
                    case "Wdate":
                        if(!checkdate(myInput[i])){
                            return false;
                        }
                        break;
                    case "mustzip":
                        if(!checkzip(myInput[i])){
                            return false;
                        }
                        break;
                    case "idcard":
                        //alert(checkIdcard(myInput[i].value));
                        if(!checkIdcard(myInput[i],myInput[i].value)){
                            return false;
                        }
                        break;
                    case "Enname":
                        if(!checkuser(myInput[i])){
                            return false;
                        }
                        break;
                    case "optidcard":
                        if(!myInput[i].value.length<1){
                            //alert(checkIdcard(myInput[i].value));
                            if(!checkIdcard(myInput[i],myInput[i].value)){
                                return false;
                            }
                        }
                        break;
                    case "optmail":
                        if(!myInput[i].value.length<1){
                            if(!isemail(myInput[i])){
                                return false;
                            }
                        }
                        break;
                    case "optnonull":
                        if(!myInput[i].value.length<1){
                            if(!checkisNull(myInput[i])){
                                return false;
                            }
                        }
                        break;
                    case "optphone":
                        if(!myInput[i].value.length<1){
                            if(!phonecheck(myInput[i])){
                                return false;
                            }
                        }
                        break;
                    default:
                        break;
                }//ending case
            }//ending check type is text
	
        }//ending for

    var myTextarea = frmObj.getElementsByTagName("Textarea");
    //begin for
    for(var i = 0;i < myTextarea.length;i++){
		
        switch (myTextarea[i].id){
            case "mustnonull":
                if(!checkisNull(myTextarea[i])){
                    return false;
                }
                break;
            case "contentb":
					
                if(!checkisNull(document.getElementById('contentb'))){
                    return false;
                }
                break;
            default:
                break;
        }//ending case
    }//ending for
    var mySelect = frmObj.getElementsByTagName("select");
    //begin for
    for(var i = 0;i < mySelect.length;i++){
		
	
        switch (mySelect[i].id){
            case "mustselect":
                if(!checkselected(mySelect[i])){
                    return false;
                }
                break;
            default:
                break;
        }//ending case
    }//ending for

}

