var ControlsToChangeStyle;

function modifyValidation(){
  var HTMLControls=document.all;
  ControlsToChangeStyle=new Array();
  for(var i=0;i<HTMLControls.length;i++){
    if(HTMLControls[i].controltovalidate!=null){  //is Validatior Control
      matchValidation(HTMLControls[i]);
    }

    if(HTMLControls[i].ControlsToValidate!=null){  //is MultiValidatior Control
      initMultiValidator(HTMLControls[i]);
    } 

    if(HTMLControls[i].validExts!=null){  //is FileValidator Control
      initFileValidator(HTMLControls[i]);
     
    }
    
    if(HTMLControls[i].SpecifiedLength!=null){  //is FixedLengthValidator Control
      initFixedLengthValidator(HTMLControls[i]);
    }
  }  //for
  
  for(var i=0;i<ControlsToChangeStyle.length;i++)
    ChangeStyle(ControlsToChangeStyle[i]);
}  //modifyValidation()

function matchValidation(elem){
  var controltovalidate;
  eval("controltovalidate=document.all."+elem.controltovalidate);

  switch(controltovalidate.datatype){
    case "char":
    case "varchar":
              elem.evaluationfunction=customRequiredFieldValidatorEvaluateIsValid;
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate;//controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    case "datetime":
              elem.type="Date";
              if(elem.minimumvalue=="") elem.minimumvalue="1900/01/01";
              if(elem.maximumvalue=="") elem.maximumvalue="2900/12/31";
              elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    case "numeric":
    case "decimal":
    case "float":
    case "real":
              elem.type="Double";
              if(elem.minimumvalue=="") elem.minimumvalue="-9000000000.0000";
              if(elem.maximumvalue=="") elem.maximumvalue="9000000000.0000";
              elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
              elem.decimalchar=".";
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    case "bigint":
              elem.type="Integer";
              if(elem.minimumvalue=="") elem.minimumvalue="-900000000000000";
              if(elem.maximumvalue=="") elem.maximumvalue="900000000000000";
              elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    case "int":
              elem.type="Integer";
              if(elem.minimumvalue=="") elem.minimumvalue="-2147483648";
              if(elem.maximumvalue=="") elem.maximumvalue="2147483647";
              elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    case "smallint":
              elem.type="Integer";
              if(elem.minimumvalue=="") elem.minimumvalue="-32768";
              if(elem.maximumvalue=="") elem.maximumvalue="32767";
              elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    case "tinyint":
              elem.type="Integer";
              if(elem.minimumvalue=="") elem.minimumvalue="0";
              if(elem.maximumvalue=="") elem.maximumvalue="255";
              elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    case "bit":
              elem.type="Integer";
              if(elem.minimumvalue=="") elem.minimumvalue="0";
              if(elem.maximumvalue=="") elem.maximumvalue="1";
              elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
              if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
    default  :elem.evaluationfunction=customRangeValidatorEvaluateIsValid;
			  if(elem.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate; //controltovalidate.style.backgroundColor="#FFE4C4";
              break;
  }
}  //matchValidation()

function ChangeStyle(elm)
{
   //elm.style.backgroundColor="#FFE4C4";
  elm.insertAdjacentHTML("afterEnd","<font color=red>*</font>");
   //elm.outerHTML+="<font color=red>*</font>";
  
}//ChangeStyle

function customRequiredFieldValidatorEvaluateIsValid(val){
	if(val.nonEmpty=="yes"){
      if(!(ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))){
        val.errormessage=val.errortitle+"不能为空!";
        return false;
      }
    }

    var theStr=ValidatorTrim(ValidatorGetValue(val.controltovalidate));
    var valueLen=getTextValueLength(theStr);
    var MaxLength;
    //alert("MaxLength=document.all."+val.controltovalidate+".dataLength;"+","+MaxLength+","+valueLen+","+theStr);
    eval("MaxLength=document.all."+val.controltovalidate+".dataLength;");
    //alert("MaxLength=document.all."+val.controltovalidate+".dataLength;"+","+MaxLength+","+valueLen+","+theStr);
    if(valueLen>MaxLength){
        val.errormessage=val.errortitle+"超过最大长度("+MaxLength+"字节)!";
        return false;
    }
    return true;
}

function customRangeValidatorEvaluateIsValid(val) {
    if(val.nonEmpty=="yes"){
      if(!(ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))){
        val.errormessage=val.errortitle+"不能为空!";
        return false;
      }
    }
    val.errormessage=val.errortitle+"数据格式非法或数值越界\n(最小值:'"+val.minimumvalue+"',最大值:'"+val.maximumvalue+"')。";
    return RangeValidatorEvaluateIsValid(val);
}

function getTextValueLength(theStr){
  var charCode;
  var strLen=0;
  var str=new String(theStr);
  for(var i=0;i<str.length;i++){
    charCode=str.charCodeAt(i);
    if(charCode>255) strLen+=2; else strLen+=1;
  }
  return strLen;
}

function initMultiValidator(val){
  if(val.ValidateType.toLowerCase()=="fixedsum"){
    val.errormessage=val.errortitle+"的总和必须为"+val.sum+"!";
  }
  else if(val.ValidateType.toLowerCase()=="nonempty"){
    val.errormessage=val.errortitle+"中至少有一个不能为空!";
  }
  else if(val.ValidateType.toLowerCase()=="fixedfilledcount"){
    val.errormessage=val.errortitle+"中有且仅有"+val.NumToBeFilled+"个不能为空!";
  }

  val.evaluationfunction=MultiValidatorEvaluateIsValid;
}

function MultiValidatorEvaluateIsValid(val){
  var startIndex=val.ControlsToValidate.indexOf("{")+1;
  var endIndex  =val.ControlsToValidate.indexOf("}");
  var content=val.ControlsToValidate.substring(startIndex,endIndex);  //get rid of the {} pair
  var controls=content.split(';');

if(val.ValidateType==""){
    return true;
  }
  
  if(val.ValidateType.toLowerCase()=="nonempty"){
    var isEmpty=true;
    for(var i=0;isEmpty && i<controls.length;i++){
      if((ValidatorTrim(ValidatorGetValue(controls[i])) != ValidatorTrim(val.initialvalue))) isEmpty=false;
    }
  
    if(isEmpty) return false;
    else return true;
  }
  else if(val.ValidateType.toLowerCase()=="fixedsum"){
    var sum=0;
    for(var i=0;i<controls.length;i++){
      var j=ValidatorTrim(ValidatorGetValue(controls[i]));
      if(j==null||j=="") j=0;
      sum+=parseFloat(j)*1000000000;
    }
    sum/=1000000000;
     if(sum!=val.sum) return false;
    else return true;
  }
  if(val.ValidateType.toLowerCase()=="fixedfilledcount"){
    var isEmpty=true;
    for(var i=0;isEmpty && i<controls.length;i++){
      if((ValidatorTrim(ValidatorGetValue(controls[i])) != ValidatorTrim(val.initialvalue))) isEmpty=false;
    }
  
    if(isEmpty) return false;
    else return true;
  }
}

function initFileValidator(val){
  var controltovalidate;
  
  eval("controltovalidate=document.all."+val.controltovalidate);
  if(val.nonEmpty=="yes") ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate;
   controltovalidate.className="detailDisplay";
  //controltovalidate.style.backgroundColor="#FFE4C4";
  //controltovalidate.readOnly=true;
  
  //val.errormessage="请选择后缀名为"+val.validExts+"的文件!";
  val.evaluationfunction=FileExtValidatorEvaluateIsValid;
}

function FileExtValidatorEvaluateIsValid(val){  
  if(val.nonEmpty=="yes"){
      if(!(ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))){
        val.errormessage=val.errortitle+"不能为空!";
        return false;
      }
  }
  
  if(val.validExts=="") return true;
  
  var startIndex=val.validExts.indexOf("{")+1;
  var endIndex  =val.validExts.indexOf("}");
  var content=val.validExts.substring(startIndex,endIndex);  //get rid of the {} pair
  var exts=content.split(';');
  
  var filename=ValidatorTrim(ValidatorGetValue(val.controltovalidate));
  if(filename=="") return true;
  filename=filename.toLowerCase();
  for(var i=0;i<exts.length;i++){
    //check for each acceptable file exts
    var ext= exts[i];
    var pos=filename.search(ext.toLowerCase());
    
    if(pos>0) return true;
  }
  val.errormessage=val.errortitle+"请选择后缀名为"+val.validExts+"的文件!"; 
  return false;
}

function initFixedLengthValidator(val){
  var controltovalidate;
  
  //eval("controltovalidate=document.all."+val.controltovalidate);
  //ControlsToChangeStyle[ControlsToChangeStyle.length]=controltovalidate;
  //controltovalidate.className="detailDisplay";
  //controltovalidate.style.backgroundColor="#FFE4C4";
  //controltovalidate.readOnly=true;
  
  val.errormessage=val.errortitle+"输入文字字节数必须为"+val.SpecifiedLength+"(每个汉字为2字节)!";
  val.evaluationfunction=FixedLengthValidatorEvaluateIsValid;
}

function FixedLengthValidatorEvaluateIsValid(val){
  if((ValidatorTrim(ValidatorGetValue(val.controltovalidate)) == ValidatorTrim(val.initialvalue))) return true;  //未输入新值则不进行检查
  
  var userInput=ValidatorTrim(ValidatorGetValue(val.controltovalidate));
  
  //alert(userInput+","+getTextValueLength(userInput)+","+val.SpecifiedLength);
  
  if(getTextValueLength(userInput)==val.SpecifiedLength) return true;
  return false;
}
