// JScript 文件
function IsNumeric(f,fld)
{
   var form=document.forms[f];
   var obj=form.item(fld);

   if(obj==null)
      return false;

   if(obj.value=="")
      return true;

   reg=/^\d{1,8}(\.\d{0,4})?$/gi;

   return reg.test(obj.value);
}
/**
 * check whether input is numeric.
 * @param  string  form/field name
 * @return boolean
 */
function IsNumeric(stringValue)
{
	if(stringValue == null)
		return true;

   reg=/^\d{1,8}(\.\d{0,4})?$/gi;
   return reg.test(stringValue);
}
/**
 * check whether input is validate date.
 * if not, focus the field, and make the filed selected
 * @param  string  form/field name(NOT control object)
 * @return boolean
 * @note   call this function in onblur
 */
function validateDate(fld)
{
	var oDate = document.forms[0].item(fld);	// the date input control
	if(oDate == null)
	{
		return true;
	}
	var sDate = oDate.value;				// date in String
	if(sDate == "")
	{
		return true;
	}

	if(!isDateString(sDate, "-"))	//not validate date
	{
		alert('无效日期格式，请按“年年年年-月月-日日”格式输入。');
		oDate.value = "";
		oDate.focus();
		return false;
	}
	return true;
}

/**
 * multi control using the same name
 * @param  string  form/field name(NOT control object)
 * @param  string  index  index of the control (01, 02......)
 * @return boolean
 * @note   call this function in onblur
 */
function validateDate(fld, index)
{
	var oMultiDate = document.forms[0].item(fld);	// the date input control
	var nIndex = index;

	if(oMultiDate == null)	//field not exist
	{
		return true;
	}

	if(oMultiDate.length > 1) // multi
	{
		oDate = oMultiDate[nIndex-1];
	}
	else
	{
		oDate = oMultiDate;
	}

	if(oDate == null )
	{
		return true;
	}

	var sDate = oDate.value;				// date in String
	if(sDate == "")
	{
		return true;
	}

	if(!isDateString(sDate, "-"))	//not validate date
	{
		alert('无效日期格式，请按“年年年年-月月-日日”格式输入。');
		oDate.focus();
		oDate.value = "";
		return false;
	}
	return true;
}

/*
 * validate a date, the date must be seperate by "-" ,like 2002-2-2
 * @param String
 *
 * @return boolean
 */
function isDateString(sDate)
{
	return isDateString(sDate, "-");
}

/*
 * validate a date
 * @param String sDate string date to validate
 * @param String sSpit split
 *
 * @return boolean
 */
function isDateString(sDate, sSpit)
{	var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
	var iaDate = new Array(3)
	var year, month, day

	iaDate = sDate.split(sSpit)
	if (iaDate.length != 3) return false
	if (iaDate[1].length > 2 || iaDate[2].length > 2) return false

	//check numeric
	if(!IsNumeric(iaDate[0]) || !IsNumeric(iaDate[1]) || !IsNumeric(iaDate[2]) )
	{
		return false;
	}

	year = parseFloat(iaDate[0])
	month = parseFloat(iaDate[1])
	day=parseFloat(iaDate[2])

	if (year < 1900 || year > 2200) return false
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
	if (month < 1 || month > 12) return false
	if (day < 1 || day > iaMonthDays[month - 1]) return false
	return true
}


//==================================数据检查方法======================================================

function isNaNStr(value){
 	if (value =="") return false;
	else{
  	    	return isNaN(value);
 	}
}

function checkEmpty(txtField,Name){
    if(txtField==null || txtField.value==null) return false;
	var tav=txtField.value.replace(/[\u3000]/g,'');  //去除中文空格
	if (trim(tav)=="") {
         alert("请输入“"+ Name +"”");
	    try{ txtField.focus();}catch(e){}
		return false;
	}
    return true;
}

function checkNum(txtField,Name){
	if (txtField.value==null||txtField.value=="")
	{
		return true;
	}
	if (isNaNStr(txtField.value)) {
		alert("请在“"+ Name +"”中输入有效数字!");
		txtField.focus();
		txtField.select();
		return false;
	}
	/*
	else if (txtField.value<0){
		alert("不能在“"+ Name +"”中输入负数!");
		txtField.focus();
		txtField.select();
		return false;
	}*/
	return true;
}
function checkNumCls(txtField,Name){
	if (txtField.value==null||txtField.value=="")
	{
		return true;
	}
	if (isNaNStr(txtField.value)) {
		alert("请在“"+ Name +"”中输入有效数字!");
                txtField.value="";
		txtField.focus();
		txtField.select();
		return false;
	}else if (txtField.value<0){
		alert("不能在“"+ Name +"”中输入负数!");
                txtField.value="";
		txtField.focus();
		txtField.select();
		return false;
	}
	return true;
}
function checkDate(txtField,Name){
	if (txtField.value=="") return true;
    if (!isDateString(txtField.value,"-")){
  		alert("在“"+Name+"”中输入无效的日期格式，请按“YYYY-MM-DD”的格式输入");
		return false;
	}
	return true;
}
//参数为 2003-11-12 和 提示信息
function checkDateValue(txtField,Name){	
	if (txtField=="") return true;	
    if (!isDateString(txtField,"-")){
  		//alert("在“"+Name+"”中输入的日期无效！");
		alert("输入的日期无效！");
		return false;
	}
	return true;
}


function trim(strSrc) 
{
   if (strSrc == null) 
   {
      return "";
   }
   return strSrc.replace(/(^\s+)|(\s+$)/g,"");
}

function parseIntValue(intValue){
	if (trim(intValue)=="") return 0;
	else return parseInt(intValue);
}

function parseFloatValue(floatValue){
	if (trim(floatValue)=="") return 0;
	else return parseFloat(floatValue);
}

function checkLength2(txtTargetObject,intLength,txtTitle){
//alert("size:"+txtTargetObject.value.length+" eee:"+intLength);
	if (txtTargetObject.value.length > intLength){
		alert("“" + txtTitle + "” 太长了，请限制在" + intLength + "字以内" );
		txtTargetObject.focus();
		return false;
	}
	return true;
}

function checkInt(field)
{
	var theValue = field.value;
	if (isNaNStr(theValue) || theValue.charAt(0) == '-' || theValue.indexOf('.') != -1)
	{
		alert('请输入有效整数');
		field.value = '';
		field.focus();
	}
}

function checkInt(txtTargetObject,Name){
    var theValue = txtTargetObject.value;
	if (theValue =="") return true;
    if (isNaNStr(theValue) || theValue.charAt(0) == '-' || theValue.indexOf('.') != -1){
  		alert("在“"+Name+"”中输入的整数无效！");
		txtTargetObject.value = '';
		txtTargetObject.focus();
		return false;
	}
	return true;
}

function checkRadioEmpty(obj,str){  
   var value = null;
   alert(obj.length);
   for(i = 0; i < obj.length; i++){
     if(obj[i].checked){
		 alert(obj.length);
        value=obj[i].value;
        return true;
     }	 
   }
   if(value==null){
    alert("请选择“"+str+"”");
	 return false;
   }
    return true;
}


function checkRadio(obj,msg1)
{
   // var is_radio=document.forms[0].elements[obj];
    var s_msg1=(msg1==null || msg1=="")? "请选择radio!":"请选择"+msg1;
    var s_msg2="没有可选的 radio!";

   // if(is_radio)
    //{
         if (obj.value != null)
         {
            if (obj.checked)
            {
                return true;
            }
            else
            {
                alert(s_msg1);
                return false;
            }
         }
         else
         {
            var check_length = obj.length;
            var i_count=0
            for(var i=0;i<check_length;i++)
            {
                if (obj(i).checked)
                {
                    i_count=i_count+1;
                    return true;
                }
            }
            if(i_count==0)
            {
                alert(s_msg1);
                return false;
            }
         }
    //}//
   // else
   // {
     //   alert(s_msg2);
     //   return false;
    //}

}
/**
比较两个日期输入框。
如果Date1或Date2为空串，返回True
如果Date1 > Date2 , 返回False. 否则返回True，不报出错信息
*/
function compareDates(Date1, Date2)
{
	if (trim(Date1.value) != "" ) {
		var aryDate1 = Date1.value.split("-");
		var strDate1 = aryDate1[0];
		tmp = aryDate1[1];
		if ( aryDate1[1].length <= 1 ) strDate1 += '-0' + tmp;
		else strDate1 += '-' + tmp;
		
		tmp = aryDate1[2];
		if (aryDate1[2].length <= 1 ) strDate1 += '-0' + tmp;
		else strDate1 += '-' + tmp;
	
		Date1.value = strDate1;
	}
//===========================================

	if (trim(Date2.value) != "" ) {

		var aryDate2 = Date2.value.split("-");
		var strDate2 = aryDate2[0];
		tmp = aryDate2[1];
		if (aryDate2[1].length <= 1) strDate2 += '-0' + tmp;
		else strDate2 += '-' + tmp;
		
		
		tmp = aryDate2[2];

		if (aryDate2[2].length <= 1) strDate2 += '-0' + tmp;
		else strDate2 += '-' + tmp;
	
		Date2.value = strDate2;
		
	}


	if (trim(Date1.value) == "" || trim(Date2.value) == "") return true;
	if (strDate1 > strDate2)
	{
		return false;
	}
	return true;
}

/**
比较两个日期输入框。
如果Date1或Date2为空串，返回True
如果Date1 > Date2 , 报出错信息，返回False. 否则返回True
*/
function compareTwoDates(Date1,DateLabel1,Date2,DateLabel2)
{
	if (compareDates(Date1, Date2) == false)
	{
		alert("“"+ DateLabel1 + "” 不能大于“" + DateLabel2 + "”");
		Date1.focus();
		return false;
	}
	return true;
}

/**
比较两个日期。
如果Date1或Date2为空串，返回True
如果Date1 > Date2 , 报出错信息，返回False. 否则返回True
*/
function compareTwoDates2(syear,smonth,sdate,DateLabel1,eyear,emonth,edate,DateLabel2,field)
{
	
	 if (smonth.length < 2)
      {
         smonth = "0"+smonth;
      }
	  if (sdate.length < 2)
      {
         sdate = "0"+sdate;
      }
	  if (emonth.length < 2)
      {
         emonth = "0"+emonth;
      }
	  if (edate.length < 2)
      {
         edate = "0"+edate;
      }
	Date1=syear+smonth+sdate;
	Date2=eyear+emonth+edate;
	
	if (trim(Date1) == "" || trim(Date2) == "") return true;   
	if (Date1>Date2)
	{
		alert("“"+ DateLabel1 + "” 不能大于“" + DateLabel2 + "”");		
		field.focus();
		return false;		  
	}  
	return true;	   
}

/**
 * 判断email的合法性
 * @param email email
 * @return boolean 合法 - true; 不合法 - false
 */
function checkEmail(email) { 
  var re = /[\w-]+@[\w-]+[\w-.]+/;
  
  if(email.value==null ||email.value.length == 0){
    return true;
  }
  if (re.test(email.value)){    
	  return true;
  }else{
    alert("输入的Email格式有误！");
	email.focus();
	return false;
  }
}
/**
 * email地址有效性检测
 */
function validateEmail(emailStr)
{
 var re=/^[\w.-]+@([0-9a-z][\w-]+\.)+[a-z]{2,3}$/i;
 //或 var re=new RegExp("^[\\w.-]+@([0-9a-z][\\w-]+\\.)+[a-z]{2,3}$","i");
 if(re.test(emailStr))
 {
  return true;
 }
 else
 {
  alert("无效email地址!");
  return false;
 }
}


//==========================================================================================

/**
 * 判断邮政编码的合法性
 * @param 
 * @return boolean 合法 - true; 不合法 - false
 */
function checkPostCode(obj)
{
	var code=trim(obj.value);
	var length = code.length;
	var birthday;

	if (length == 0)
	{
		return true;
	}
	if (length != 6||isNaN(code))
	{
		alert("输入的邮政编码有误！");
	    obj.focus();
		return false;
	}	
		
	return true;
}
//===================身份证======================begin======================================
/**
 * 判断身份证号码的合法性
 * @param strIdNo 身份证号码
 * @return boolean 合法 - true; 不合法 - false
 */
function isValidIdCardNo(strIdNo)
{
	strIdNo = trim(strIdNo);
	var length = strIdNo.length;
	var birthday;

	if (length == 0)
	{
		return true;
	}
	if (length != 15 && length != 18)
	{
		return false;
	}
	
	for (var i = 0; i < length - 2; i++)
	{
		if (strIdNo.charAt(i) < '0' || strIdNo.charAt(i) > '9')
		{
			return false;
		}
	}
	if (!((strIdNo.charAt(length - 1) >= '0' && strIdNo.charAt(length - 1) <= '9')
		|| (length == 18 && strIdNo.charAt(length - 1) >= 'a' && strIdNo.charAt(length - 1) <= 'z')
		|| (length == 18 && strIdNo.charAt(length - 1) >= 'A' && strIdNo.charAt(length - 1) <= 'Z')))
	{
		return false;
	}
	if (length == 15 && (strIdNo.charAt(i) < '0' || strIdNo.charAt(i) > '9'))
	{
		return false;
	}
	
	birthday = getBirthdayFromIdNo(strIdNo);
	if (isDateString(birthday, "-") == false)
	{
		return false;
	}
	
	return true;
}

/**
 * 检查“身份证号码”表单元素的内容是否为合法的身份证号，
 * 如果合法则从中提取出生日期，并自动填入“出生年月”表单中
 * @param theElement “身份证号码”表单元素
 * @param theBirthdayElement “出生年月”表单元素
 */
function checkIdCardNo(theElement, theBirthdayElement)
{
	if (isValidIdCardNo(theElement.value) == false)
	{
		alert("无效身份证号码");
		theElement.value = "";
		theElement.focus();
	}
	
	if (theBirthdayElement != null)
	{
		theBirthdayElement.value = getBirthdayFromIdNo(theElement.value);
	}
}

/**
 * 从身份证号码中提取出生日期
 * @param strIdNo 身份证号码
 * @return 出生日期（YYYY-MM-DD)。如果提取失败，返回""
 */
function getBirthdayFromIdNo(strIdNo)
{
	var length = strIdNo.length;
	var birthday = "";
	
	if (length == 15)
	{
		birthday = "19" + strIdNo.substring(6, 8)
			+ "-" + strIdNo.substring(8, 10)
			+ "-" + strIdNo.substring(10, 12);
	}
	else if (length == 18)
	{
		birthday = strIdNo.substring(6, 10)
			+ "-" + strIdNo.substring(10, 12)
			+ "-" + strIdNo.substring(12, 14);
	}
	if (isDateString(birthday, "-") == false)
	{
		birthday = "";
	}
	
	return birthday;
}
//============================身份证end==============================================


//示例======================
/*
    if (!checkEmpty(document.forms[0].LBCB472,"财政审批人")) return;
	if (!checkInt(document.forms[0].LBCB349,"总共补助人数")) return;
	if (!checkNum(document.forms[0].LBCB350,"申请补助金额")) return;
	if (!checkDate(document.forms[0].LBCB366,"填报日期")) return;
	if (!checkLength(document.forms[0].LCAA002,100,"备注")) return;
	if (!compareTwoDates(document.forms[0].LBCB347,"开始日期",document.forms[0].LBCB348,"结束日期")) return;  
	*/
	
//检查文件的后缀名是否相符	
function check_file(filePath ,ext)
{
	if (filePath == "" ) return false ;

	var fileName, fileExt
		
	fileName = filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length) ;
	fileExt	 = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length) ;

	fileName = fileName.toLowerCase() ;
	fileExt = fileExt.toLowerCase() ;
	ext =  ext.toLowerCase() ;
	if (fileExt == ext)
		return true ;
	
	return false;
}
	
function trim(value) {
    return value.replace(/(^\s*)|(\s*$)/g, "");
}
/* 
*hans 20060718
**************************** 
* 　　　 半角<=>全角 　　　* 
**************************** 
*　参数说明: 
* str:要转换的字符串 
* flag:标记，为０时半转全，为1时全转半 
* 返回值类型：字符串
*  如 alert(dbcToSbc("AＡabc",0));
**************************** 
*/ 
function dbcToSbc(str,flag) { 
	var i; 
	var result=''; 
	if (str.length<=0) return str;//{alert('字符串参数出错');return false;} 
	for(i=0;i<str.length;i++){ 
		 code=str.charCodeAt(i); 
		 if(code >= 65281 && code <= 65373 && flag!=0)//在这个unicode编码范围中的是所有的英文字母已经各种字符   
		  { 
			 result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码
		  }else if(code<125 && flag==0)
		  {
			 result += String.fromCharCode(str.charCodeAt(i) + 65248);//把半角字符的unicode编码转换为对应全角字符的unicode码  

		  }else   if   (code   ==   12288)//空格   
		  {   
			 result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);   
		  }else   
		  {   
			  result   +=   str.charAt(i);   
		  } 
	} 
	return result; 
}
/////////
/////////
/////////
function f_Center(ll_Width,ll_Height)
{
	var ll_Left;
	var ll_Top;
	var ls_Str;
	ll_Left=(screen.width -ll_Width)/2
	ll_Top=(screen.height -ll_Height)/2
	if (ll_Left<0)
	{
		ll_Left=0;
	}
	if (ll_Top<0)
	{
		ll_Top=0;
	}
	ls_Str = "left = " + ll_Left + ",top = " + ll_Top + ",width = " + ll_Width + ",height = " + ll_Height;
	return ls_Str;
}
function getLength(element){
	var s = element.value;
	var count = 0;
	var len = s.length;
	
	for(var i=0;i<len;i++){
		var ch=s.charCodeAt(i)&0xffff;
		if(ch<0xff){
			count++;
		}
		else{
			count += 2;
		}
	}
	return count;
}
function checkLength(element,ename,max_length){
	try{
//	    if(element.value.length == 0){
//		    alert(ename+"不能为空！");
//		    element.focus();
//		    return false;
//	    }
	    if(!checkEmpty(element,ename)) return false;
	    
	    if(getLength(element) > max_length){
		    alert(ename+" 不能超过 "+max_length+" 个字符！");
		    element.focus();
		    return false;
	    }
	}
	catch(e)
	{
	}
	return true;
}

