//** 数据合法性验证 */

/*
 * 1、定义jsp页面校验的正则表达式
 * 2、页面提交时数据校验
 * 3、格式化金额、百分比、整数等
 * */
var PatternsDict = new Object();

// 邮政编码
PatternsDict.zipPat = /^\d{6}$/;

// 整数
PatternsDict.integerPat = /^(\+|\-|\d{0})(\d)+$/;

// 年
PatternsDict.yearPat = /^\d{4}$/;

// 金额（包括正负，保留两位小数）
PatternsDict.currencyPat = /^(\+|\-|\d{0})(\d{0,18})(\d{0}|(\.\d{0,2}))$/;

// 金额（正，保留两位小数）
PatternsDict.currencyNegPat = /^(\+|\d{0})(\d{0,18})(\d{0}|(\.\d{0,2}))$/;

// 汇率
PatternsDict.ratePat = /^((\d{0,5}(\.)\d{0,4})|\d{0,5})$/;

// 时间 matches 5:04 or 12:34 but not 75:83
PatternsDict.timePat2=/^([1-9]|1[0-2]):[0-5]\d$/;

// 日期 matches 1999-01-01 but not 99-01-01, 99-1-1, 99-01-1, 99-1-01, 1999-1-1, 1999-01-1, 1999-01-01
PatternsDict.datePat =/^(19[0-9][0-9]|20[0-9][0-9])-([1-9]|0[1-9]|1[0-2])-([1-9]|0[1-9]|[1-2][0-9]|3[0-1])$/;

// 年月 month,match "YYYY-MM" format
PatternsDict.monthPat =/^(19[0-9][0-9]|200[0-9])-([1-9]|0[1-9]|1[0-2])$/;

// 小数（包括正负，小数点后10位）
PatternsDict.decimalPat = /^(\+|\-|\d{0})(\d{0,7})(\d{0}|(\.\d{0,10}))$/;

// 任意字符串
PatternsDict.stringPat=/\S{0,2000}$/;

// 纯字符
PatternsDict.charPat=/^\w{0,4000}$/;

// 纯数字
PatternsDict.numberPat=/^\d+$/;

// 颜色
PatternsDict.colorPat=/^#[0-9a-fA-F]{6}$/;

/**
 * 将传入的数据转换为两位小数的float型
 * */
function floatCut( float1 )
{
  var symbol = "";
  if((new Number(float1))<0)
  {
    symbol = "-";
  }

  var num1 = new String(Math.round(Math.abs((new Number(float1)))*100));
  
  var length = num1.length;
  if( num1 == 0 )
  {
    symbol = "";
  }
  var num2="";
  if(length>2)
  {
    num2 = num1.substring(0,length-2)+"."+num1.substring(length-2);
  }
  else if(length>1)
  {
    num2 = "0."+num1;
  }
  else
  {
    num2 = "0.0"+num1;
  }
  return symbol+num2;
}

/**
 * cut the left and right blanks of the input string variable;
 * a trimed string for the input string variable;
 * */
function trimStr(inputStr)
{
  var str = inputStr+"";          // variable to get the input string
  var i = str.indexOf(" ");
  while (i==0)
  {            // cut left blanks
    str = str.substring(1);
    i = str.indexOf(" ");
  }
  i = str.lastIndexOf(" ");
  while (i==0)
  {    // cut right blanks
    str = str.substring(0,str.length-1);
    i = str.lastIndexOf(" ");
  }
  return str;                        // return result string
}

/**
 * trim the input string variable and remove all of the commas in it;
 * a trimed string variable without any comma in it
 * */
function withoutCommaStr(inputStr)
{
  var str = trimStr(inputStr);    // variable to get the input string that has been trimed
  var i = str.indexOf(",");

  while (i>-1) 
  {// no "," return -1 , then + 1 is 0, means false
    str = str.substring(0,i) + str.substring(i+1,str.length);
    i = str.indexOf(",");            // get index of the leftest ","
  }
  return str;
}

/**
 * trim the input string variable and remove all of the commas in it;
 * a trimed string variable without any comma in it
 * */
function withoutZeroCommaStr(inputStr)
{
  var str = trimStr(inputStr);    // variable to get the input string that has been trimed
  var i = str.indexOf(",");

  while (i>-1) 
  {               // no "," return -1 , then + 1 is 0, means false
    str = str.substring(0,i) + str.substring(i+1,str.length);                // generate new string
    i = str.indexOf(",");            // get index of the leftest ","
  }
  
  if ( isNaN(str) ) 
    return "0";
  else 
    return str;                        // return result string
}

/**
 * trim the field's value and add commas in it to display number standard,
 * but this function can't validate the field;
 * a trimed string variable with commas to display number
 * */
function withCommaStr(inputStr)
{
  if ( isNaN(inputStr) ) return inputStr;

  var str =  new String (withoutCommaStr(inputStr));  // variable to get the input string that has been trimed
  str = floatCut(str);              // trim the currency number
  var str1 = new String ("");             // variable to cut symbol part
  var str2 = new String ("");                  // variable to cut float part
  var str3 = new String ("");            // variable to add comma
  var str4 = new String ("");            // variable to add comma
  if (str.charAt(0) == "+" || str.charAt(0) == "-") 
  {
    str1 = str.substring(0,1);            // save "+" of "-" if has
    str = str.substring(1,str.length);      // cut the symbol
  }
  if (str.lastIndexOf(".") != -1) 
  {
    var i = str.lastIndexOf(".");
    str2 = str.substring(i,str.length);      // save float part if has "."
    str = str.substring(0,i);            // cut the float part
  }
  str = str + ",";                    // trick to simplify the comma adding
  var n = str.indexOf(",");
  while (n > 3) 
  {                    // need to add comma
    str3 = str.substring(0,n-3);
    str4 = str.substring(n-3,str.length);
    str = str3 + "," + str4;            // add a comma into the interger part
    n = str.indexOf(",");
  }
  str = str.substring(0,str.length-1);      // cut the last useless comma
  str = str1 + str + str2;

  return str;                        // return result string

}

/**
 * 将整数型字符串格式化为千分位形式
 * */
function withCommaStrInt(inputStr)
{
  if ( inputStr == null || inputStr == "" ) inputStr = "0";

  if ( isNaN(inputStr) ) return inputStr;

  var str =  new String (withoutCommaStr(inputStr));  // variable to get the input string that has been trimed
  str = withCommaStr(str);
  str = str.substring(0,str.length-3);
  
  return str;                        // return result string
}

/**
 * trim and add commas of the currency type field in a form
 * no return but alter the form;
 * */
function withCommaForm(theForm)
{
  var inputFields = theForm.elements;        // put the form elements into an array
  var i = 0;                        // define the cycle-variable
  
  for (i = 0; i < inputFields.length; i++)    // begin to cycle-process
  {
    with(inputFields[i])
    {
      if ( inputFields[i].validator == "currencyPat" || inputFields[i].validator == "currencyNegPat" )
      {
        inputFields[i].value = withCommaStr(inputFields[i].value);
      }
      else if ( inputFields[i].validator == "integerPat" )
      {
        inputFields[i].value = withCommaStrInt(inputFields[i].value);
      }
      else if ( inputFields[i].validator == "ratePat" )
      {
        inputFields[i].value = withCommaStrRate(inputFields[i].value);
      }
    }
  }
}

/**
 * trim and remove commas of the currency type field in a form
 * no return but alter the form
 * */
function withoutCommaForm(theForm)
{
  var inputFields = theForm.elements;        // put the form elements into an array
  var i = 0;                        // define the cycle-variable
  for (i = 0; i < inputFields.length; i++)    // begin to cycle-process
  {
    with(inputFields[i])
    {
      if ( inputFields[i].validator == "currencyPat" || inputFields[i].validator == "currencyNegPat" || inputFields[i].validator == "integerPat" )
      {
        inputFields[i].value = withoutCommaStr(inputFields[i].value);
      }
      else if ( inputFields[i].validator == "ratePat" )
      {
        inputFields[i].value = withoutCommaStrRate(inputFields[i].value);
      }
    }
  }
}

/**
 * validate a field
 * no validator and match validator return 0,
 * not match return 1 and when flag == 0 select the field;
 * */
function fieldValidate(theField,selectFlag)
{
  var v = theField.validator;            // get the validator property of the field

  if (!v) return 1;                    // no validator property return 0
  theField.value = trimStr(theField.value);    // trim the field who has validator property
  var thePat = PatternsDict[v];            // select the regular-expression
  if ( thePat == null ) return 1;

  if ( thePat == "datePat")
  {
    var gotIt = checkdate(theField.value);
  }
  else
  {
    var gotIt = thePat.exec(theField.value);    // check,not match return null
  }
  if (!gotIt) 
  {                      // if not match return 1
    if (selectFlag == 0) 
    {
      theField.select();              // select the problem field
    }
    return 1;
  }
  return 0;                        // if match return 0
}

/**
 * validate a field. match return true and alert agreement
 * not match false and alert disagreement;
 * */
function validateField(theField) 
{
  theField.value = withoutCommaStr(theField.value);
  var matchflag = fieldValidate(theField,0);
  if(matchflag == 1) 
  {
    window.alert(theField.fname + "：输入的值不符合规范。");// alert user the disagreement
    return false;
  }
  theField.value = withCommaStr(theField.value);
  return true;
}

/**
 * function to be evoked when onFocus event
 * */
function focusDeal(theField) 
{
  theField.value = withoutCommaStr(theField.value);
  theField.select();
}

/**
 * function to be evoked when onBlur event
 * */
function blurDeal(theField) 
{
  theField.value = withoutCommaStr(theField.value);  // remove the , first
  var matchflag = fieldValidate(theField,1);      // check field if it's a no-comma type
  if(matchflag == 0) 
  {
    if(theField.value == "" || theField.value == "+" || theField.value == "-" || theField.value == "+." || theField.value == "-." || theField.value == ".") 
    {
      theField.value = "0";    // convert the meaningless input that pass the validation
    }
  }
  theField.value = withCommaStr(theField.value);   // alter the no-comma type
}

/**
 * function to be evoked when onFocus event
 * */
function intFocusDeal(theField) 
{
  theField.value = withoutCommaStr(theField.value);
  theField.select();
}

/**
 * function to be evoked when onBlur event
 * */
function intBlurDeal(theField) 
{
  theField.value = withoutCommaStr(theField.value);  // remove the , first
  var matchflag = fieldValidate(theField,1);      // check field if it's a no-comma type
  if(matchflag == 0)
  {
    if(theField.value == "" || theField.value == "+" || theField.value == "-" || theField.value == "+." || theField.value == "-." || theField.value == ".") 
    {
      theField.value = "0";    // convert the meaningless input that pass the validation
    }
  }
  theField.value = withCommaStrInt(theField.value);   // alter the no-comma type
}

/**
 * function to be evoked when onFocus event
 * */
function rateFocusDeal(theField) 
{
  theField.value = withoutCommaStrRate(theField.value);
  theField.select();
}

/**
 * 将百分比数据转换为数字，去掉格式化信息
 * */
function withoutCommaStrRate(inputStr)
{
  var str = trimStr(inputStr);    // variable to get the input string that has been trimed
  var i = str.indexOf(",");
  while (i>-1) 
  {               // no "," return -1 , then + 1 is 0, means false
    str = str.substring(0,i) + str.substring(i+1,str.length);                // generate new string
    i = str.indexOf(",");            // get index of the leftest ","
  }
  str = str.substring(0,str.length-1);
  if ( str == "" || isNaN ( str ) )
  {
    return inputStr;
  }
  str = parseFloat(str)/100 + "";
  str = doubleToString ( str );
  return str;                        // return result string
}

/**
 * 将double型转换为string型
 * */
function doubleToString ( inputStr )
{
  var i;
  var j;
  var c;
  var str = "" + inputStr;
  i = str.indexOf ( "." );
  j = str.length;
  if ( j <= ( i + 5 ) || i == -1 )
  {
    return str;
  }

  c = str.charAt ( i + 5 );
  str = str.substring ( 0, i + 5 );
  i = parseInt ( "" + c );
  if ( i >= 5 )
  {
    str = "" + ( parseFloat ( str ) + 0.0001 );
  }

  return str;
}

/**
 * function to be evoked when onBlur event
 * */
function rateBlurDeal(theField)
{
  var matchflag = fieldValidate(theField,1);      // check field if it's a no-comma type
  if(matchflag == 0)
  {
    if(theField.value == "" || theField.value == "+" || theField.value == "-" || theField.value == "+." || theField.value == "-." || theField.value == ".")
    {
      theField.value = "0";    // convert the meaningless input that pass the validation
    }
  }
  theField.value = withCommaStrRate(theField.value);   // alter the no-comma type
}

/**
 * 格式化百分比
 * */
function withCommaStrRate(inputStr)
{
  if ( inputStr == null || inputStr == "" ) inputStr = "0";
  if ( isNaN(inputStr) ) return inputStr;

  var str =  inputStr;  // variable to get the input string that has been trimed
  var str1 = new String ("");             // variable to cut symbol part
  var str2 = new String ("");             // variable to cut float part
  var str3 = new String ("");            // variable to add comma
  var str4 = new String ("");            // variable to add comma

  str = parseFloat(str)*100 + "";
  str = withCommaStr(str);
  str = str + "%";
  return str;                        // return result string
}

/**
 * function to be evoked when onSubmit event
 * */
function submitDeal(theForm) 
{
  withoutCommaForm(theForm);
  return true;
}

/**
 * function to be evoked when form reset
 * */
function resetDeal(theForm) 
{
  theForm.reset();
  withCommaForm(theForm);
}

/**
 * 检查一个输入field是否为空
 * */
function IsEmptyValue(value)
{
  var num=0;
  var i=0;
  if(value==null)
    return true;
  if(value.length==0)
  {
    return true;
  }

  for(i=0;i<value.length;i++)
  {
    num=value.charCodeAt(i)
    if((num!=32)&&(num!=13)&&(num!=10))
      return false;
  }
  return true
}

/**
 * 执行按页查询时对页号校验
 * */
function checkPageNo(frm,pageCount)
{ 
  if ( frm.currentPageNo.value.indexOf(".") != -1 )
  {
  	alert("输入页数错误!");
  	return false;
  }
  if ( isNaN(frm.currentPageNo.value) )
  {
    alert("请输入有效数字。");
    return false;
  }
  if(IsEmptyValue(frm.currentPageNo.value))
  {
    alert("页号码没有输入，请输入页号。");
    return false;
  }
  if(parseInt(frm.currentPageNo.value)>pageCount)
  {
    alert("超出最大页数，请重新输入最大页。");
    document.msForm.currentPageNo.selected;
    return false;
  }

  if(parseInt(frm.currentPageNo.value)<1)
  {
    alert("超出最小页数，请重新输入最大页。");
    document.msForm.currentPageNo.selected;
    return false;
  }
  return true;
}

/**
 * 校验翻页手工输入的页数
 * */
function checkPageNumber(txtObj,pageCount)
{
  if ( txtObj.value.indexOf(".") != -1 )
  {
  	alert("输入页数错误!");
  	return false;
  }
  if ( isNaN(txtObj.value) )
  {
    alert("请输入有效数字。");
    return false;
  }
  if(IsEmptyValue(txtObj.value))
  {
    alert("页号码没有输入，请输入页号。");
    return false;
  }
  if(parseInt(txtObj.value)>pageCount)
  {
    alert("超出最大页数，请重新输入最大页。");
    txtObj.selected;
    return false;
  }

  if(parseInt(txtObj.value)<1)
  {
    alert("超出最小页数，请重新输入最大页。");
    txtObj.selected;
    return false;
  }
  return true;
}

/**
 * 校验日期是否合法
 * */
function checkdate(getdate)
{
	var flag=true;

  if (getdate.search(/^[0-9]{4}-(0[1-9]|1[0-2])-((0[1-9])|1[0-9]|2[0-9]|3[0-1])$/) == -1 )
  {
    flag=false;
  }
  else
  {
  	var year=getdate.substr(0,getdate.indexOf('-'))  // 获得年
  	// 下面操作获得月份
  	var transition_month=getdate.substr(0,getdate.lastIndexOf('-'));
  	var month=transition_month.substr(transition_month.lastIndexOf('-')+1,transition_month.length);
  	if (month.indexOf('0')==0)
  	{
  		month=month.substr(1,month.length);
  	}
  	// 下面操作获得日期
  	var day=getdate.substr(getdate.lastIndexOf('-')+1,getdate.length);
  	if (day.indexOf('0')==0)
  	{
  		day=day.substr(1,day.length);
  	}
  	flag=true;
  	if ((month==4 || month==6 || month==9 || month==11) && (day>30)) // 4,6,9,11月份日期不能超过30
  	{
  		flag=false;
  	}
  	if (month==2)  // 判断2月份
  	{
  		if (LeapYear(year))
  		{
  			if (day>29 || day<1){ flag=false; }
  		}
  		else
  		{
  			if (day>28 || day<1){flag=false; }
  		}
  	}
  	else
  	{
  		flag=true;
  	}
  }

	if (flag==false)
	{
		alert("您输入的日期不符合规范。");
		return false;
	}
	return true;
}

/**
 * 校验时间是否合法
 * */
function checktime(inpar)
{
	var flag=true;
	getdate=fob(inpar).value;
	if (getdate.search(/^[0-9]{4}-(0[1-9]|[1-9]|1[1-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]) ((0[1-9]|[1-9])|1[0-9]|2[0-4]):((0[1-9]|[1-9])|[1-5][0-9]):((0[1-9]|[1-9])|[1-5][0-9])$/)==-1) // 判断输入格式时候正确
	{
		flag=false;
	}
	else
	{
		var year=getdate.substr(0,getdate.indexOf('-'))  // 获得年
		// 下面操作获得月份
		var transition_month=getdate.substr(0,getdate.lastIndexOf(' '));
		transition_month=getdate.substr(0,getdate.lastIndexOf('-'));
		var month=transition_month.substr(transition_month.lastIndexOf('-')+1,transition_month.length);
		if (month.indexOf('0')==0)
		{
		  month=month.substr(1,month.length);
		}
		// 下面操作获得日期
		var transition_day=getdate.substr(0,getdate.lastIndexOf(' '));
		var day=transition_day.substr(transition_day.lastIndexOf('-')+1,transition_day.length);
		if (day.indexOf('0')==0)
		{
			day=day.substr(1,day.length);
		}
		flag=true;
	}
	if ((month==4 || month==6 || month==9 || month==11) && (day>30)) // 4,6,9,11月份日期不能超过30
	{
		flag=false;
	}
	if (month==2)  // 判断2月份
	{
		if (LeapYear(year))
		{
			if (day>29 || day<1){ flag=false; }
		}
		else
		{
			if (day>28 || day<1){flag=false; }
		}
	}
	else
	{
		flag=true;
	}

	if (flag==false)
	{
		alert("您输入的日期不符合规范。");
		return false;
	}
	return true;
}

/**
 * 判断是否闰年
 * */
function LeapYear(intYear)
{
	if (intYear % 100 == 0)
	{
		if (intYear % 400 == 0) { return true; }
	}
	else
	{
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

/**
 * 格式化数字
 * */
function numberFormat(obj,value)
{
	value = zTrim(value);
	value = trim(value);
	var returnValue = "0";
	var headValue = "";
	var tag = /,/g;
	var noValue  = value.replace(tag, "");
  noValue = Math.round(noValue*100)/100 + "";
  var moneyArray = noValue.split(".");
	if(isNaN(noValue))
	{
		obj.value="0";
		return returnValue;
	}

  if(moneyArray.length > 2)
  {
    alert("输入数字不符合规范。");
    return returnValue;
  }

	if ( moneyArray.length == 1 )
	{
    headValue = moneyArray[0];
	}

	if ( headValue.length == 0 )
	{
		headValue = "0";
	}

	var num1 = headValue.length%3;
	var num2 = headValue.length/3;
	var realNum = "";

	if ( num1 > 0 )
	{
		realNum = headValue.substring(0,num1);
		if (num2 >= 1 )
		{
			realNum = realNum + ",";
		}
		headValue = headValue.substring(num1,headValue.length);
	}

	for ( var i=0; i<=num2-2; i++ )
	{
		realNum = realNum + headValue.substring(0,3) + ",";
		headValue = headValue.substring(3,headValue.length);
	}

	realNum = realNum + headValue;
	returnValue = realNum;

	obj.value = returnValue;
	return returnValue;
}

/**
 * 校验控件合法性
 * */
function validateItem( itemField )
{
  var v = itemField.validator;            // get the validator property of the field
  var strValue = itemField.value;
  
  // 校验必填项
  // 文本框：
  if( itemField.type == "password" || itemField.type == "text" || itemField.type == "textarea" )
  {
    strValue = trimStr ( strValue );
    if ( strValue == "" && itemField.nflag == 1 )
    {
      alert("错误提示：有必填项没有输入，请输入。\n\r[" + itemField.fname + ":" + strValue + "]");
      itemField.select();
      return false;
    }
  }
  // 下拉列表：
  else if(itemField.type=="select-one" )
  {
    if ( strValue == "" && itemField.nflag == 1 )
    {
      alert("错误提示：有必填项没有输入，请输入。\n\r[" + itemField.fname + ":" + strValue + "]");
      itemField.focus();
      return false;
    }
  }
  else
  {
    if ( strValue == "" && itemField.nflag==1 )
    {
      alert("错误提示：有必填项没有输入，请输入。\n\r[" + itemField.fname + ":" + strValue + "]");
      return false;
    }
  }    

  var thePat = PatternsDict[v];         // select the validating regular expr
  if ( thePat == null ) return true;
  var gotIt = thePat.exec( strValue );   // run it on value of itemField
  fn = itemField.fname;

  // 校验validator
  if(!gotIt)
  {
    // 金额
    if ( itemField.validator == "currencyPat" )
    {
      alert("错误提示：您输入了无效的金额。\n\r[" + fn + ":" + strValue + "]");
    }
    // 字符
    else if ( itemField.validator == "charPat" )
    {
      alert("错误提示：您输入了无效的值，只能输入数字、字母和“_”。\n\r[" + fn + ":" + strValue + "]");
    }
    // 数字
    else if ( itemField.validator == "integerPat" || itemField.validator == "numberPat" || itemField.validator == "decimalPat" )
    {
      alert("错误提示：您输入了无效的数字。\n\r[" + fn + ":" + strValue + "]");
    }
    // 汇率
    else if ( itemField.validator == "ratePat" )
    {
      alert("错误提示：您输入了无效的百分比。\n\r[" + fn + ":" + strValue + "]");
    }
    // 字符串
    else if ( itemField.validator == "stringPat" )
    {
      alert("错误提示：您输入了无效的值，不能输入【^<>\'\"】字符。\n\r[" + fn + ":" + strValue + "]");
    }
    else if( itemField.validator == "currencyNegPat" )
    {
      if(parseFloat(strValue)<0.00)
      {
        alert("错误提示：您输入了负数，请核对后重新输入。\n\r[" + fn + ":" + strValue + "]");
      }
    }
    else if ( itemField.validator == "colorPat" )
    {
      alert("错误提示：您输入了无效的颜色。\n\r[" + fn + ":" + strValue + "]");
    }
    // 时间
    else if ( itemField.validator == "datePat" )
    {
      alert("错误提示：您输入了无效的时间。\n\r[" + fn + ":" + strValue + "]");
    }
    else
    {
      alert("错误提示：您输入了无效的值。\n\r[" + fn + ":" + strValue + "]");
    }
    if ( itemField.type == "text" || itemField.type == "textarea" )
    {
      itemField.select();
    }
    return false;
  }
  // 校验其他参数
  else
  {
    var t_length = getLength ( strValue );
    
    if ( t_length > itemField.msMaxlength )
    {
      alert("错误提示：您输入长度超过最大长度。\n\r[" + fn + "]:" + itemField.msMaxlength );
      itemField.select();
      return false;
    }
    // 非修改状态下不做校验
    if ( itemField.maxvalue != "" && itemField.maxvalue != undefined && fn != undefined && !itemField.readOnly )
    {
      var va=withoutCommaStr(strValue);
      var maxvalue=withoutCommaStr(itemField.maxvalue);
      if(parseFloat(va)>parseFloat(maxvalue))
      {
        alert("错误提示：输入超过最大值。\n\r["+fn + ":" + value + "]可用余额:" + maxvalue );
        itemField.select();
        return false;
      }
    }
    
    if ( itemField.maxnumber != "" && itemField.maxnumber != undefined && fn != undefined )
    {
      var va=withoutCommaStr(strValue);
      var maxvalue=withoutCommaStr(itemField.maxnumber);
      if(parseFloat(va)>parseFloat(maxvalue))
      {
        alert("错误提示：输入超过最大值。\n\r["+fn + ":" + value + "]最大数:" + maxvalue);
        itemField.select();
        return false;
      }
    }
  }
  return true;
}

/**
 * 根据字符集判断字符串的长度，GBK字符集中文为两个字节，UTF-8为三个字节
 * */
function getLength( strValue )
{
  var metaElements = document.all.tags( 'META' )
  var metaKeywords = new Array();
  var strChar = "";
  var cArr = null;
  
  for ( var m = 0; m < metaElements.length; m++ )
  {
    metaKeywords[m] = metaElements[m].content;
    if ( ( metaElements[m].content.toLowerCase() ).indexOf( "charset" ) != -1 )
    {
      var ss = metaElements[m].content.split( ";" )
      strChar = ss[1].split( "=" )[1];
    }
  }
  if ( strChar.toLowerCase().indexOf("gbk") != -1 )
  {
    cArr = strValue.match(/[^\x00-\xff]/ig);
  }
  else if ( strChar.toLowerCase().indexOf("utf-8") != -1 )
  {
    // 好像不准确
    cArr = strValue.match(/[^\u4e00-\u9fa5]/ig);
  }
  
  var t_length = strValue.length + (cArr == null ? 0 : cArr.length);
  
  return t_length;
}

/**
 * 校验form合法性
 * */
function validateForm( theForm )
{
  var elArr = theForm.elements; // get all elements of the form into array

  for(var i = 0; i < elArr.length; i++)
  {
    // for each element of the form...
    with( elArr[i] )
    {
      // 未明对象不作处理：
      if ( elArr[i].type=="" ) continue;
      if ( elArr[i].style.display == "none" || elArr[i].disabled ) continue;
      
      // 未设置校验类型：
      if((elArr[i].nflag!=1) && elArr[i].value.length==0) continue; //1 not null, 0 null
      if( !elArr[i].validator ) continue; // no validator property, skip
      
      // 对当前控件进行合法性验证：
      if ( !validateItem( elArr[i] ) ) return false;
    }
  }
  return true;
}

/**
 * 通用控件获得焦点处理
 * */
function msOnFocus( itemField )
{
  // 控件类型对应的获得焦点事件：
  if ( itemField.validator == "currencyPat" )
  {
    // 取消金额格式化显示：
    focusDeal( itemField );
  }
  else if ( itemField.validator == "integerPat" )
  {
    // 取消数字格式化显示：
    intFocusDeal( itemField )
  }
  else if ( itemField.validator == "datePat" )
  {
    // 弹出日期选择控件：
    date( itemField )
  }
  else if ( itemField.validator == "monthPat" )
  {
    // 弹出月度选择控件：
    yearMonth( itemField )
  }
  else if ( itemField.validator == "ratePat" )
  {
    // 取消百分比格式化显示：
    rateFocusDeal( itemField )
  }
  return true;
}

/**
 * 通用控件离开焦点处理
 * */
function msOnBlur( itemField )
{
  // 控件内容合法性验证：
  if ( itemField.value != "" && itemField.msFocus == "1" )
  {
    validateItem( itemField );
  }
  
  // 控件类型对应的离开焦点事件：
  if ( itemField.validator == "currencyPat" )
  {
    // 金额格式化显示：
    blurDeal( itemField );
  }
  else if ( itemField.validator == "integerPat" )
  {
    // 数字格式化显示：
    intBlurDeal( itemField )
  }
  else if ( itemField.validator == "datePat" )
  {
    // 关闭弹出日历，选择指定的日期：
    SelectCalendar( )
  }
  else if ( itemField.validator == "monthPat" )
  {
    // 关闭弹出日历，选择指定的月份：
    SelectCalendar( )
  }
  else if ( itemField.validator == "ratePat" )
  {
    // 百分比格式化显示：
    rateBlurDeal( itemField )
  }
  return true;
}