/**
 *此js文件包含在各个网页中,包含了各种表单输入验证函数,还包含一些公用数据处理函数
 */

/****************************************************************/

//列表页删除记录链接单击事件
function list_delbutton_onclick(frmObj){
    var isAnySelected=false;
    var rows=frmObj.elements;
    for (i=0,l=rows.length;i<l&&!(isAnySelected=rows[i].checked);i++);
    if (!isAnySelected){
        return false;
    }
    else
        return confirm('确定删除所选记录吗？');
}

//设定页删除记录链接单击事件
function set_delbutton_onclick(){
    return (confirm('确定删除该记录吗？'))
}

//去一个表单元素值的首尾空格
function trimThis(objThis,chkObj){
    try{
        objThis.value=objThis.value.replace(/(^\s*)|(\s*$)/g,"");
    }
    catch(e){
    }
}

//消除一个表单中所有元素首尾空格
function trimAll(frmObj){
    for (var i=1;i<frmObj.elements.length-1;i++)    {
        frmObj.elements[i].value
          =frmObj.elements[i].value.replace(/(^\s*)|(\s*$)/g,"");
    }
}

function gotoPage(pageNo){
	document.all.curPage.value = pageNo;
	document.all.curPage.form.submit();
}

function buildPageCtrl(curPage,pageSize,totalRecords){
	

    var STEPS=3;  //以当前页码为中心向前/后索引的页码数
    var pageCount=0;
    pageCount=Math.round(((totalRecords-1)/pageSize)-0.5) + 1;
    beginPage=curPage-STEPS;
    if (beginPage<1)
        beginPage=1;
    endPage=beginPage+STEPS*2;
    if (endPage>pageCount)
        endPage=pageCount;
    beginPage=endPage-STEPS*2;
    if (beginPage<1)  beginPage=1;


    document.writeln("<input type=hidden name=curPage value="+curPage+">");
    
    document.writeln("<p>共<strong>"+totalRecords+"</strong>条记录 每页显示<input type=text size=1 maxlength=4 name=pageSize value="
			+ pageSize + "  onkeypress='subPageSize(this)'>条 共<strong>"+pageCount+"</strong>页</p>");

    document.writeln("<p>");
    if(curPage > 1){
    		document.write("<a class='tofirst' title='首页' href='javascript:gotoPage(1)'></a>");
		document.write("<a class='back' title='上一页' href='javascript:gotoPage(" + (curPage-1) + ")'></a>");
    }

    for (i=beginPage;i<=endPage;i++){
        if (i==curPage)
            document.write("<a ><font color=red>"+i+"</font></a>");
        else
            document.write("<a title='到第"+i+"页' href='javascript:gotoPage(" + i + ")'>" + i + "</a>");
    }

    if(curPage < pageCount){
	document.write("<a class='forward' title='下一页' href='javascript:gotoPage(" + (curPage+1) + ")'></a>");	
	document.write("<a class='tolast'title='尾页' href='javascript:gotoPage("+pageCount+")'></a>");
    }
    
    document.write("第<input type=text size=1 maxlength=4 name=toPage value=" + curPage + " onkeypress='subToPage(this.value)'>页");
    document.write("</p>");
	
    if (pageCount >0 && curPage > pageCount)
    {
      gotoPage(1);
    }
}
function subToPage(toPage)
{
 if ((event.keyCode<48||event.keyCode>57)&&event.keyCode!=13){
        event.keyCode=0;
        event.returnValue=false;
    }

    if (event.keyCode==13 ){
        gotoPage(toPage);
    }

}
function subForm(txtObj,pageCount){
    //只允许输入数字
    if ((event.keyCode<48||event.keyCode>57)&&event.keyCode!=13){
        event.keyCode=0;
        event.returnValue=false;
    }

    if (event.keyCode==13 ){
        gotoPage=txtObj.value;
        if (isNaN(gotoPage))
            gotoPage=1;
        if (gotoPage>pageCount)
            gotoPage=pageCount;
        if (gotoPage<1)
            gotoPage=1;
        txtObj.form.reset();
        txtObj.value=gotoPage;
        txtObj.form.submit();
    }
}

function subPageSize(txtObj){
    //只允许输入数字
    if ((event.keyCode<48||event.keyCode>57)&&event.keyCode!=13){
        event.keyCode=0;
        event.returnValue=false;
    }

    if (event.keyCode==13 ){
        txtObj.form.submit();
    }
}

//弹出检索窗口,参数:(返回对象的编号)
function popIndex(returnId){
    var popURL="/pop_index.jsp?hidden="+returnId.name;
    size=screen.height/1.5;
    var winStyle='menubar=no,scrollbars=yes,resizeable=no,width='+size+',height='+size+',left='+(screen.width-size)/2+',top='+(screen.height-size)/2;
    window.open(popURL,'pop_index',winStyle);
}
/********************************************************************/
/********************************************************************/

var blnIsCorrect=true; //检查值有效性标志量，初始值为真（有效）
var strError='';       //当发生错误时显示的错误提示信息
var objItem;           //当发生错误时，将焦点定位到哪一个表单元素

document.oncontextmenu=lkj;
function lkj(){
if (event.srcElement.nodeName=="A" || event.srcElement.parentElement.nodeName=="A")
    event.returnValue=false;
}

//返回检查标志量，如果标志量为否，则显示错误警告，并将光标定位在出错的文本框内
function checkAll(){
    if (!blnIsCorrect){
        alert(strError);
        try{
            objItem.focus();
        }
        catch(e){
        }
        blnIsCorrect=true; //返回否之前重置标志量为真
        return false;
    }
    else
        return true;
}

//非空
function isNotEmpty(txtObj,message){
    if (blnIsCorrect){
        //如果txtObj是文本框或下拉列表框(value属性不为空)则判断txtObj的value是否为空,
        if (txtObj.value!=null){
            trimThis(txtObj);
            if (txtObj.value.length==0){
                strError=(message==null?'该值不能为空.':message);
                blnIsCorrect=false;
                if (txtObj.style.display=="none")
                    objItem=txtObj.previousSibling;
                else
                    objItem=txtObj;
            }
        }
        //否则txtObj是复选框或单选框,则判断txtObj每一个元素是否选中
        else{
            blnIsCorrect=false;
            for (x=0;x<txtObj.length;x++){
                if (txtObj[x].checked){
                    blnIsCorrect=true;
                    break;
                }
            }
            if (!blnIsCorrect){
                strError=(message==null?'该值不能为空.':message);
                objItem=txtObj[0];
            }
        }
    }
}

//两次输入的密码是否相等
function isEquals(txtObj1,txtObj2){
    if (blnIsCorrect&&(txtObj1.value.length!=0 ||txtObj2.value.length!=0)){
        trimThis(txtObj1);
        trimThis(txtObj2);
        if (txtObj1.value!=txtObj2.value){
            strError='两次输入的密码不一致.';
            blnIsCorrect=false;
            objItem=txtObj1;
        }
    }
}

//长度不符合
function checkLength(txtObj1,min,max,message){
    if (blnIsCorrect&&(txtObj1.value.length<min ||txtObj1.value.length>max)){
        if (message!=null)
            strError=message;
        else
            strError='该项长度不符合！';
        blnIsCorrect=false;
        objItem=txtObj1;
    }
}


//是否格式正确通用检查函数,入参:(文本框对象,正则表达示串,错误提示信息)
function isRight(txtObj,strCheck,message){
    if (blnIsCorrect && txtObj.value.length!=0){
        if (!strCheck.exec(txtObj.value)){
            if (message!=null)
                strError=message;
            else
                strError='该项格式不正确！';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}


/*前一值是否小于等于后一值*/
function isRange(txtObj1,txtObj2,message){
    trimThis(txtObj1);
    trimThis(txtObj2);

    if (blnIsCorrect && txtObj1.value.length!=0 &&txtObj2.value.length!=0){
        var blnIsRange=true;
        if (/^\d+\.{0,1}\d*$/.exec(txtObj1.value) && /^\d+\.{0,1}\d*$/.exec(txtObj2.value)){
            if (Number(txtObj1.value)>Number(txtObj2.value))
                blnIsRange=false;
        }
        else{
            if (txtObj1.value>txtObj2.value)
                blnIsRange=false;
        }
        if (!blnIsRange){
            if (message!=null)
                strError=message;
            else
                strError='左区间值应小于或等于右区间值！';
            blnIsCorrect=false;
            //进行日期验证时,文本框样式为不可见,则将焦点定在前一html元素(object)
            if (txtObj1.style.display=="none")
                objItem=txtObj1.previousSibling;
            else
                objItem=txtObj1;
        }
    }
}

/*前一值是否小于等于后一值,适用于六个下拉列表的情况*/
function isListRange(lstObj11,lstObj12,lstObj13,lstObj21,lstObj22,lstObj23){
     if (blnIsCorrect&&(lstObj21.value.length!=0 && lstObj22.value.length!=0 && lstObj23.value.length!=0)){
     var lstObj1=lstObj11.value+lstObj12.value+lstObj13.value;
     var lstObj2=lstObj21.value+lstObj22.value+lstObj23.value;
        if (lstObj1>lstObj2){
            strError="前面日期应小于等于后面日期！";
            blnIsCorrect=false;
            objItem=lstObj11;
        }
    }
}
/*前一值是否小于等于后一值,适用于十个下拉列表的情况*/
function isTenListRange(lstObj11,lstObj12,lstObj13,lstObj14,lstObj15,lstObj21,lstObj22,lstObj23,lstObj24,lstObj25){
     if (blnIsCorrect&&(lstObj21.value.length!=0 && lstObj22.value.length!=0 && lstObj23.value.length!=0 )){
     var lstObj1=lstObj11.value+lstObj12.value+lstObj13.value+lstObj14.value+lstObj15.value;
     var lstObj2=lstObj21.value+lstObj22.value+lstObj23.value+lstObj24.value+lstObj25.value;
        if (lstObj1>lstObj2){
            strError="前面日期应小于等于后面日期！";
            blnIsCorrect=false;
            objItem=lstObj11;
        }
    }
}

//是否是电话号码
function isTel(txtObj,message){
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^\d+(-{0,1}\d+)*$/;
        if (!strCheck.exec(txtObj.value)){
            if (message!=null)
                strError=message;
            else
                strError='电话号码格式不正确.';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否是正整数
function isInt(txtObj,message){
    trimThis(txtObj);
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^\d+$/;
        if (!strCheck.exec(txtObj.value)){
            if (message!=null)
                strError=message;
            else
                strError='数字格式不正确.';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//非零
function isNotZero(txtObj){
    if (blnIsCorrect){
        if (txtObj.value=='0'){
            strError='该值不能为零！';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否为正实数
function isFloat(txtObj,message){
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^\d+\.{0,1}\d*$/;
        if (!strCheck.exec(txtObj.value))
        {
            if (message!=null)
                strError=message;
            else
                strError='该项格式不正确！';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否是身份证
function isIdCard(txtObj){
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^\d{14}(\d|\d{4}|\d{3}[a-z])$/;
        if (!strCheck.exec(txtObj.value))
        {
            strError='身份证格式不正确.';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否是邮编
function isPostCode(txtObj){
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^\d{6}$/;
        if (!strCheck.exec(txtObj.value))
        {
            strError='邮编格式不正确.';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否是长度为3，6
function isLength(txtObj){
    trimThis(txtObj);
    if (blnIsCorrect && txtObj.value.length!=0){
        if(txtObj.value.length!=3&&txtObj.value.length!=6){
            strError='类别代码长度只能为3或6';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否是基本ASCII字符(ASCII字符集前127位可见字符)
function isASCII(txtObj,message){
    trimThis(txtObj);
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^[!-\x7f]*$/;
        if (!strCheck.exec(txtObj.value)){
            if (message!=null)
                strError=message;
            else
                strError='半角字符格式不正确.';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否是合法的标识符(以字母开头，字母、数字、下划线的组合)
function isIdentifier(txtObj){
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^[a-zA-Z]+(_{0,1}[a-zA-Z0-9])*$/;
        if (!strCheck.exec(txtObj.value)){
            if (message!=null)
                strError=message;
            else
                strError='标识符格式不正确.';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

//是否是合法的E_mail地址(以字母开头，字母、数字、下划线的组合，中间必须有一个“@”)
function isEmail(txtObj,message){
    trimThis(txtObj);
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^\S+@\S+\.\S+$/;
        if (!strCheck.exec(txtObj.value))    {
            strError='电子邮件信箱格式不正确.';
            blnIsCorrect=false;
            objItem=txtObj;
        }
    }
}

function formatDate(txtObj){
    isDate(txtObj);
    checkAll();
}
/*判断日期和时间的格式是否合法，并将日期格式变为yyyy-mm-dd hh:mm:ss*/
function isDateTime(txtObj,message){
    var strDate;
    var strTime;	
    var strDateTime = txtObj.value;
    if(blnIsCorrect && txtObj.value.length != 0){
    	var nSpaceIndex = strDateTime.indexOf(" ");
    	if(nSpaceIndex == -1){
    	     strDate = strDateTime;
    	     strTime = "";
	}else{
	     strDate = strDateTime.substring(0,nSpaceIndex);
	     strTime = strDateTime.substring(nSpaceIndex+1,strDateTime.length);
	     if(strTime==null){
	     	strTime="";
	     }
	}
    	txtObj.value = strDate;
    	isDate(txtObj,message);
    	if(blnIsCorrect){
    	     strDate = txtObj.value;
    	     txtObj.value = strTime;
    	     isTime(txtObj,message);
    	     if(blnIsCorrect){
    	     	 strTime = txtObj.value;
    	     	 txtObj.value = strDate + " " + strTime;
    	     }else{
    	         txtObj.value = strDateTime;
    	     }
    	}else{
    	     txtObj.value = strDateTime;
	}
    }
    
}

/*判断日期格式是否合法，并将日期格式变为yyyy-mm-dd*/
function isDate(txtObj,message){
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^(\d{4})[-\/]{0,1}(\d{1,2}){0,1}[-\/]{0,1}(\d{1,2}){0,1}$/; //检查日期格式是否合法
//      var strCheck2=/(^\d{5}$|^\d{5}-$|-\d{3}$)/;    //检查是否有4位年1位月连着写，以及月份位数+日期位数只有3位的情况
        blnIsCorrect=strCheck.exec(txtObj.value);   // && !strCheck2.exec(txtObj.value);
        /*判断年份是否合理*/
        var yy=txtObj.value.replace(strCheck,'$1');
        if( yy > 2050 || yy < 1950 ){
          blnIsCorrect = false;
        }
        
        if (blnIsCorrect){
            /*将yyyymmdd日期转化成yyyy-mm-dd*/
            var Year='',Month='',Date='';
            Year=txtObj.value.replace(strCheck,'$1'); //取出年
            Month=txtObj.value.replace(strCheck,'$2');//取出月份
            Date=txtObj.value.replace(strCheck,'$3'); //取出日期
            Month=Month.replace(/^$/,'01');        //月份值为空则月份值等于01
            Month=Month.replace(/^(\d{1})$/,'0$1');//月份值为1位则前面补0
            Date=Date.replace(/^$/,'01');          //日期值为空则日期值等于01
            Date=Date.replace(/^(\d{1})$/,'0$1');  //日期值为1位则前面补0
            txtObj.value=Year+'-'+Month+'-'+Date;  //格式化日期输入格式

               
            /*判断各月份的天数是否正确*/
            if ((Month==4||Month==6||Month==9||Month==11)&&Date<=30 && Date>0)
                return;
            if ((Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12)&&Date<=31&&Date>0)
                return;
            if (Month==2){
                //闰年(能被4整除，不能被100整除的年是闰年，但能被400整除的年也是闰年)2月29天，平年28天
                if ( ((Year%4==0 && Year%100!=0) || Year%400==0) && Date<=29 && Date>0)
                    return;
                else if (Date<=28)
                    return;
            }
        }
        if (message!=null)
            strError=message;
        else
            strError='日期格式不正确.';
        blnIsCorrect=false;
        objItem=txtObj;
    }
}


/*检证日期是否合法，适合于年、月、日是下拉列表的情况*/
function isListDate(lstYear,lstMonth,lstDay)
{
     if (blnIsCorrect && (lstYear.value.length!=0||lstMonth.value.length!=0||lstDay.value.length!=0)){
        if (/^\d{4}$/.exec(lstYear.value)){  //检查年是否是4位数字,因为年有可能是全角空格
            var Year=lstYear.value; //取年
            var Month=lstMonth.value;//取出月份值
            var Date=lstDay.value; //取日

            //4、6、9、11月30天
            if ((Month==4||Month==6||Month==9||Month==11)&&Date<=30){
                return;
            }

            //1、3、5、7、8、10、12月31天
            if ((Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12)&&Date<=31){
                return;
            }
            if (Month==2){
                //闰年(能被4整除，不能被100整除的年是闰年，但能被400整除的年也是闰年)2月29天
                if ( ((Year%4==0 && Year%100!=0) || Year%400==0) && Date<=29){
                    blnIsCorrect=true;
                    return;
                }
                else if (Date<=28){
                    return;
                }
            }
        }

        strError='日期格式不正确.';
        blnIsCorrect=false;
        objItem=lstDay;
     }
}


//是否是有效时间格式(时:分)
function isTime(txtObj,message){
    if (blnIsCorrect && txtObj.value.length!=0){
        var strCheck=/^(\d{1,2}):{1}(\d{1,2}):{1}(\d{1,2})$/;
        if (strCheck.exec(txtObj.value)){
            hour=txtObj.value.replace(strCheck,"$1");
            minute=txtObj.value.replace(strCheck,"$2");
            second=txtObj.value.replace(strCheck,"$3");
            
            hour=hour.replace(/^$/,'00');              //时的值为空则月份值等于00
            hour=hour.replace(/^(\d{1})$/,'0$1');      //时的值为1位则前面补0
            minute=minute.replace(/^$/,'00');          //分的值为空则日期值等于00
            minute=minute.replace(/^(\d{1})$/,'0$1');  //分的值为1位则前面补0
            second=second.replace(/^$/,'00');          //秒的值为空则日期值等于00
            second=second.replace(/^(\d{1})$/,'0$1');  //秒的值为1位则前面补0
            txtObj.value=hour+':'+minute+':'+second;   //格式化时间输入格式
            if (hour>=0 && hour<24 && minute>=0 && minute<60 && second >= 0 && second < 60 )
                return;
        }
        if (message!=null)
            strError=message;
        else
            strError='无效时间格式.';
        blnIsCorrect=false;
        objItem=txtObj;
    }
}

/*******************************************************************/


