//-----------------------------------------------------------//
//                                                           //
// Version: 2.00, Author: Karol Kolodziej, Date: 29Jan, 2007 //
//                                                           //
//-----------------------------------------------------------//


//----------------------//
//                      //
// VALIDATION FUNCTIONS //
//                      //
//----------------------//

function initValidateOnBlur(formID, vldList, targetCss, targetCssOnValid)
{
  var form = document.getElementById(formID);
  if (typeof(targetCss) == "undefined") targetCss = "";
  
  if (targetCss == "")
    for (var incr = 0; incr < form.elements.length; incr++)
    {
      var elem = form.elements[incr]; 
      if ((elem.type != "button") && (elem.type != "submit")) continue;
      
      var jsFunc = getElementAttributeValue(elem, "onClick").toLowerCase();
    
      if ((jsFunc.indexOf("submitvalidform") > -1) || (jsFunc.indexOf("validateform") > -1))
      {
        var aParams = jsFunc.split(",");
        targetCss = aParams[2].replace(/'/g,"").replace(/ /g,"");
        targetCssOnValid = aParams[3].replace(/'/g,"").replace(/ /g,"");
        break;
      }
    }
    
  //if (targetCss == "")
  //{
  //  alert("On-the-fly validation setup failed.\nReason: 'targetCss' needs to be specified.");
  //  return;
  //}
  
  var aFld = createMultiArray(countObjectProperties(vldList), 2);
  incr = -1;
  for (var key in vldList)
  {
    var thisField = form.elements[extractFieldName(key)];
    //if ("text,textarea,password,file,select-one,select-multiple,checkbox".indexOf(thisField.type) == -1) continue;
    if (getSimpleArrayValue(aFld, thisField.name) == "done") continue;
    
    var onblur = getElementAttributeValue(thisField, "onblur");
    if ((onblur != "") && (onblur.substr(onblur.length - 1,1) != ";"))
      onblur += ";";
    
    eval("thisField.onblur = function() { " + onblur + "validateForm(form, vldList, targetCss, targetCssOnValid, false, true, this.name); }");
    
    incr++;
    aFld[incr][0] = thisField.name;
    aFld[incr][1] = "done";
  }
}


function submitValidForm(formID, vldList, targetCss, targetCssOnValid, boolShowPopup)
{
  var form = document.getElementById(formID);
  if (validateForm(form, vldList, targetCss, targetCssOnValid, boolShowPopup)) 
  {  
      form.submit(); return true;
  }
  else
      return false;
}


function validateForm(form, vldList, targetCss, targetCssOnValid, boolShowPopup, boolDisableFocus, fldNameInclude)
{
  var formMsg = "";
  var vldMsg  = "";
  var fldName = "";
  var aVld  = createMultiArray(countObjectProperties(vldList), 2);
  var incr  = -1;
  var incr2 = -1;
  for (var key in vldList)
  {
     var strKey = key;
     
     if (typeof(fldNameInclude) != "undefined")
     {  
       if ((strKey.indexOf(fldNameInclude) != 0) &&
           (getValidatorType(strKey) != "group"))
         continue;  
       
       if (getValidatorType(strKey) == "group")
         if ((strKey.indexOf("&&" + fldNameInclude) == -1) &&
             (strKey.indexOf("||" + fldNameInclude) == -1) &&
             (strKey.indexOf(fldNameInclude) != 0))
           continue;
     }
     
     if (getParentRequiredValidatorKey(strKey, vldList) == "")
     {
       fldName = extractFieldName(strKey);
       if (getSimpleArrayValue(aVld, fldName) == "invalid") continue;
       
       var boolFocus = false;
       if (typeof(boolDisableFocus) == "undefined" || !boolDisableFocus)
           boolFocus = formMsg == "";
       else
           boolFocus = !boolDisableFocus;
           
       vldMsg = validateField(form, strKey, vldList, targetCss, targetCssOnValid, boolFocus, false); 
       if (vldMsg != "")
       {
         formMsg += vldMsg + "\n";
       }
       incr++;
       aVld[incr][0] = fldName;
       aVld[incr][1] = (vldMsg == "" || getValidatorType(strKey) == "group") ? "valid" : "invalid";
     }
  }
  
  if (boolShowPopup && (formMsg != "")) alert(formMsg);
  
  return formMsg == ""; 
}


function validateField(form, key, vldList, targetCss, targetCssOnValid, boolFocus, boolSuppressCss, boolForceValid, aTargetElemExcept)
{
    if(typeof(vldList[key]) == 'undefined') return "";
    
    var thisField = null;
    var comparedField = null;
    var comparedFieldValue = "";
    var comparisonSign = "";
    var aTargetElem = null;
    var aItem = null;
    var aChildrenRequiredValidatorKey = null;
    var strChildrenRequiredValidatorType = "OR";
    var boolChildrenValid = false;
    var boolWakeUpParentRequiredValidator = false;
    var aParams = null;
    var aTargetCss = null;
    var aTargetCssOnValid = null;
    
    var validatorName = "";
    var allowedFileExtensions = "";
    var fieldValue = "";
    var defaultValue = "";
    var aVld = null;
    var vldMsg = "";
    var strTmp = "";
    var incr = 0;
    var incr2 = 0;
    var boolSkip = false;
    var boolInvalid = false
    
    var strKey = key;
    
    if (strKey.indexOf("?") > -1)
    {  
        aParams = convertParamsToSimpleArray(strKey.substr(strKey.indexOf("?") + 1), "&");
        strKey = strKey.substr(0, strKey.indexOf("?"));
    }
    
    validatorName = getSimpleArrayValue(aParams, "validator_name"); 
    if (validatorName == "")
        validatorName = getSimpleArrayValue(aParams, "validatorname"); 
    if (validatorName == "")
        validatorName = getSimpleArrayValue(aParams, "validator"); 
    
    strTmp = getSimpleArrayValue(aParams, "target_id");
    if (strTmp == "")
        strTmp = getSimpleArrayValue(aParams, "targetid");
    if (strTmp != "")
    {
        aItem = strTmp.split("#"); aTargetElem = new Array(aItem.length);
        for (incr = 0; incr <= aItem.length - 1; incr++)
          aTargetElem[incr] = document.getElementById(aItem[incr]); 
    }
    
    allowedFileExtensions = getSimpleArrayValue(aParams, "allowed_file_extensions"); 
    if (allowedFileExtensions == "")
        allowedFileExtensions = getSimpleArrayValue(aParams, "file_extensions"); 
    strTmp = getSimpleArrayValue(aParams, "target_css");
    if (strTmp == "")
        strTmp = getSimpleArrayValue(aParams, "targetcss");
    if (strTmp != "")
    {    
        strTmp = strTmp.replace(/@#/g,"@<");
        aTargetCss = strTmp.split("#");  
    }
    else if (targetCss != "")
    {    
        targetCss = targetCss.replace(/@#/g,"@<");
        aTargetCss = targetCss.split("#");  
    }
    if (aTargetCss != null)
      for(incr = 0; incr <= aTargetCss.length - 1; incr++) 
      {  
        aTargetCss[incr] = allTrim(aTargetCss[incr].replace(/'/g,""));
        aTargetCss[incr] = aTargetCss[incr].replace(/@</g,"#");
      }
    
    strTmp = getSimpleArrayValue(aParams, "target_css_onvalid");
    if (strTmp == "")
        strTmp = getSimpleArrayValue(aParams, "targetcss_onvalid");
    if (strTmp == "")
        strTmp = getSimpleArrayValue(aParams, "target_css_on_valid");
    if (strTmp == "")
        strTmp = getSimpleArrayValue(aParams, "targetcss_on_valid");
    if (strTmp != "")
    {    
        strTmp = strTmp.replace(/@#/g,"@<");
        aTargetCssOnValid = strTmp.split("#");
    }
    else if ((typeof(targetCssOnValid) != "undefined") && (targetCssOnValid != "")) 
    {    
        targetCssOnValid = targetCssOnValid.replace(/@#/g,"@<");
        aTargetCssOnValid = targetCssOnValid.split("#")
    }
    else if (aTargetCss != null)
    {    
        aTargetCssOnValid = new Array(aTargetCss.length);
        for (incr = 0; incr <= aTargetCss.length - 1; incr++)
          aTargetCssOnValid[incr] = clearCssProperties(aTargetCss[incr]); 
    }
    if (aTargetCssOnValid != null)
      for(incr = 0; incr <= aTargetCssOnValid.length - 1; incr++) 
        aTargetCssOnValid[incr] = allTrim(aTargetCssOnValid[incr].replace(/'/g,"")); 
    if (aTargetCssOnValid != null)
      for(incr = 0; incr <= aTargetCssOnValid.length - 1; incr++) 
        aTargetCssOnValid[incr] = aTargetCssOnValid[incr].replace(/@</g,"#");
    
    defaultValue = getSimpleArrayValue(aParams, "default_value");
    if (defaultValue == "")
        defaultValue = getSimpleArrayValue(aParams, "defaultvalue");
    if (defaultValue == "")
        defaultValue = getSimpleArrayValue(aParams, "default");
    defaultValue = defaultValue.replace(/'/g,"");
    
    
    if (strKey.indexOf("==") > 0)
      comparisonSign = "==";
    else if (strKey.indexOf("=") > 0)
      comparisonSign = "=";
    else if (strKey.indexOf(">") > 0)
      comparisonSign = ">";
    else if (strKey.indexOf("<") > 0)
      comparisonSign = "<";
    else if (strKey.indexOf("<>") > 0) 
      comparisonSign = "<>";
    else if (strKey.indexOf("!=") > 0) 
      comparisonSign = "!=";
    else if (strKey.indexOf("<=") > 0) 
      comparisonSign = "<=";
    else if (strKey.indexOf("=<") > 0) 
      comparisonSign = "=<";
    else if (strKey.indexOf(">=") > 0) 
      comparisonSign = ">=";
    else if (strKey.indexOf("=>") > 0) 
      comparisonSign = "=>";
         
    if (comparisonSign != "")
    {  
      comparedField = form.elements[strKey.substr(strKey.indexOf(comparisonSign) + comparisonSign.length)];
      strKey = strKey.substr(0, strKey.indexOf(comparisonSign));
    
      if (comparisonSign == "<>")
        comparisonSign = "!=";
      else if (comparisonSign == "=<")
        comparisonSign = "<=";
      else if (comparisonSign == "=>")  
        comparisonSign = ">=";
      else if (comparisonSign == "=")  
        comparisonSign = "==";
    }
    
    if (strKey.indexOf("||") > -1)
    {
       aChildrenRequiredValidatorKey = (strKey.substr(strKey.indexOf("||") + 2)).split("||"); 
       strKey = strKey.substr(0, strKey.indexOf("||"));
       strChildrenRequiredValidatorType = "OR";
    }
    else if (strKey.indexOf("&&") > -1)
    {  
       aChildrenRequiredValidatorKey = (strKey.substr(strKey.indexOf("&&") + 2)).split("&&"); 
       strKey = strKey.substr(0, strKey.indexOf("&&"));
       strChildrenRequiredValidatorType = "AND";
    }
    if (strChildrenRequiredValidatorType == "AND")
      if (("true,yes,on".indexOf(getSimpleArrayValue(aParams, "wakeup", true).toLowerCase()) > -1) ||
          ("true,yes,on".indexOf(getSimpleArrayValue(aParams, "wake_up", true).toLowerCase()) > -1))
      {
        boolWakeUpParentRequiredValidator = true;
      }
    
    thisField = form.elements[strKey];  
    if(typeof(thisField) == 'undefined') return "";
    
    if ((aTargetElem == null) && (thisField.parentNode != null)) 
      aTargetElem = new Array(thisField.parentNode);    
    
    fieldValue = getFieldValue(thisField);   
    
    if (aChildrenRequiredValidatorKey != null)
    {
      boolChildrenValid = getChildrenRequiredValidatorStatus(form, vldList, aChildrenRequiredValidatorKey, strChildrenRequiredValidatorType, targetCss, targetCssOnValid);
    }
    
    if ((typeof(boolForceValid) == 'undefined') || (boolForceValid == false))
      if (comparedField != null)
      {
      	comparedFieldValue = comparedField.value;
      	boolSkip = false;
      	if (isNaN(comparedFieldValue) || isNaN(fieldValue))
      	  if ((comparisonSign.indexOf(">") > -1) || (comparisonSign.indexOf("<") > -1))
      	  {
      	     boolSkip = true;
      	  }
      	if (boolSkip == false)
      	{
      	  var compareExpr = true;
      	  if (isNaN(comparedFieldValue) || isNaN(fieldValue))
      	    compareExpr = eval("'" + fieldValue + "' " + comparisonSign + " '" + comparedFieldValue + "'");
      	  else
      	    compareExpr = eval((fieldValue == "" ? "0" : fieldValue) + " " + 
      	                       comparisonSign + " " + 
      	                       (comparedFieldValue == "" ? "0" : comparedFieldValue));
      	  if (compareExpr == false)
          {
      	    vldMsg = vldList[key];
            if (vldMsg == "") vldMsg = "<undefined validator message>";
          }
        }
      }
      else if ((thisField.type == "file") && (allowedFileExtensions != ""))
      {      	
	if (fieldValue != "")
        {
	  var aFileExtensions = allowedFileExtensions.split("#");
	  var boolFnd = false;
	  var fileExt = getFileExtension(getFileNameFromPath(fieldValue))	
	  for(incr = 0; incr <= aFileExtensions.length - 1; incr++)
	  {
	    boolFnd = boolFnd || (fileExt.toUpperCase() == aFileExtensions[incr].toUpperCase())
	  }
	  if (!boolFnd)
	  {
	    vldMsg = vldList[key];
	    if (vldMsg == "") 
	      vldMsg = fileExt.toUpperCase() + " extension is not allowed for '" + strKey + "' field";
	  }	
        }
      }
      else if (validatorName != "")
      { 
        if ((fieldValue != "") || (thisField.type.indexOf("select") > -1 && (thisField.selectedIndex > -1)))
        {  
          var vldOutput = eval(validatorName + "(thisField, aParams)");
          if (typeof(vldOutput) == 'string')
            vldMsg = vldOutput;
          else if ((typeof(vldOutput) == 'boolean') && (vldOutput == false))
          {  
            vldMsg = vldList[key];
            if (vldMsg == "") vldMsg = "<undefined validator message>";
          }
       }
      }			
      else if ((fieldValue == "") || (fieldValue == defaultValue))
      {
          if (vldList[key].indexOf("@") == 0) // literal message
            vldMsg = vldList[key].substring(1);
          else if (aChildrenRequiredValidatorKey != null)
            vldMsg = vldList[key];
          else
          {
            vldMsg = "Please enter a value for '" + vldList[key] + "' field.";
  	    //if (thisField.type == "file")
  	    //  vldMsg = "Please load a file for '" + vldList[key] + "' field.";
  	  }
      }
      
    if (aChildrenRequiredValidatorKey != null)
    {
      switch (strChildrenRequiredValidatorType)
      {
         case "OR" :
           if (boolChildrenValid && (vldMsg != ""))
             vldMsg = "";
           break;
      
         case "AND" :
           if (boolChildrenValid == false)
           {
             boolInvalid = true;
             if (boolWakeUpParentRequiredValidator == true)
             {
                boolInvalid = vldMsg == "";
                var aChildrenRequiredValidatorStatus = getChildrenRequiredValidatorStatusAsArray(form, vldList, aChildrenRequiredValidatorKey, strChildrenRequiredValidatorType, targetCss, targetCssOnValid);
                for (var elem in aChildrenRequiredValidatorStatus)
                {
                  if (aChildrenRequiredValidatorStatus[elem][1] == "valid")
                  { boolInvalid = true; break; }
                } 
                if (boolInvalid == false) vldMsg = "";
             }
             
             if (boolInvalid == true)
             {
               if (vldMsg == "") vldMsg = vldList[key];
             }
           }
           break;
      }
    }
    
    if (vldMsg != "")
    {
    	clearFieldRemainingValidators(key, aTargetElem, form, vldList, targetCss, targetCssOnValid);
    	if ((boolSuppressCss == false) && (aTargetElem != null))
    	{
    	  if (aTargetCss != null)
    	    for (incr = 0; incr <= aTargetElem.length - 1; incr++)
	    {  
	      _setElementCss(aTargetElem[incr], (aTargetCss.length > incr ? aTargetCss[incr] : ""));
	    }
        }  
        if (boolFocus)
        {  
          if (typeof(thisField.length) != "undefined")
          {	
              if ((aTargetElem != null) && (aTargetElem[0] != null)) aTargetElem[0].focus(); 
          }
          else
              thisField.focus();
        }
    }
    else
    {
        if ((boolSuppressCss == false) && (aTargetElem != null))
        {  
          for (incr = 0; incr <= aTargetElem.length - 1; incr++)
          {  
	    boolSkip = false;
	    if (typeof(aTargetElemExcept) != "undefined") 
	    {  
	       for (incr2 = 0; incr2 <= aTargetElemExcept.length - 1; incr2++)
	       {  
	         if (typeof(aTargetElemExcept[incr]) != "undefined") 
	           if (aTargetElemExcept[incr].id == aTargetElem[incr].id)
	           { boolSkip = true; break; }
	       }
	    }  
	    if ((boolSkip == false) && (aTargetCssOnValid != null))
	      _setElementCss(aTargetElem[incr], (aTargetCssOnValid.length > incr ? aTargetCssOnValid[incr] : ""));
	  }
        }
    }
    
    return vldMsg;
}


function clearFieldRemainingValidators(keyExcept, aTargetElemExcept, form, vldList, targetCss, targetCssOnValid)
{
   var fldName = extractFieldName(keyExcept);
   var key2;
   for (key2 in vldList)
   {
       if ((key2.indexOf(fldName) == 0) && (key2 != keyExcept)) 
       {
         validateField(form, key2, vldList, targetCss, targetCssOnValid, false, false, true, aTargetElemExcept)
       }
   }
}


function getChildrenRequiredValidatorStatus(form, vldList, aChildrenRequiredValidatorKey, strChildrenRequiredValidatorType, targetCss, targetCssOnValid)
{
    if (aChildrenRequiredValidatorKey == null) return true;
    
    var boolChildrenValid = false;
    var vldMsg = "";
    for (incr = 0; incr <= aChildrenRequiredValidatorKey.length - 1; incr++)
    {
      for (key2 in vldList)
      {
        if (key2.indexOf(aChildrenRequiredValidatorKey[incr]) == 0) 
        { 
          if (getValidatorType(key2) != "required") continue;
            
          vldMsg = validateField(form, key2, vldList, targetCss, targetCssOnValid, false, true); 
          if (vldMsg == "")
          { 
               boolChildrenValid = true; break; 
          }
          else if (strChildrenRequiredValidatorType == "AND")
          {
               boolChildrenValid = false; break;
          }
        } 
      }
      if ((strChildrenRequiredValidatorType == "OR") && (boolChildrenValid == true))
          break;
      else if ((strChildrenRequiredValidatorType == "AND") && (boolChildrenValid == false))
          break;
    }
    return boolChildrenValid;
}


function getChildrenRequiredValidatorStatusAsArray(form, vldList, aChildrenRequiredValidatorKey, strChildrenRequiredValidatorType, targetCss, targetCssOnValid)
{
    if (aChildrenRequiredValidatorKey == null) return null;
    
    var arrOut = createMultiArray(aChildrenRequiredValidatorKey.length, 2);
    
    var vldMsg = "";
    for (incr = 0; incr <= aChildrenRequiredValidatorKey.length - 1; incr++)
    {
      for (key2 in vldList)
      {
        if (key2.indexOf(aChildrenRequiredValidatorKey[incr]) == 0) 
        { 
          if (getValidatorType(key2) != "required") continue;
            
          vldMsg = validateField(form, key2, vldList, targetCss, targetCssOnValid, false, true); 
          arrOut[incr][0] = key2;
          arrOut[incr][1] = (vldMsg == "" ? "valid" : "invalid");
        } 
      }
    }
    return arrOut;
}


function getParentRequiredValidatorKey(childKey, vldList)
{
  if (getValidatorType(childKey) != "required") return "";
  
  for (var key in vldList)
  {
    var aStrSign = new Array("||","&&");
    for (var sign in aStrSign)
      if (key.indexOf(aStrSign[sign]) > -1)
      {
        var strKey = key; 
        if (strKey.indexOf("?") > -1)
        {   
          strKey = strKey.substr(0, strKey.indexOf("?"));
        }
        var aStrKey = strKey.split(aStrSign[sign]); 
        if (aStrKey.length > 1)
          for (incr = 0; incr <= aStrKey.length - 1; incr++)
          {
            if (childKey.indexOf(aStrKey[incr]) == 0) return key;
          }
      }
  }
  return "";	
}


function getValidatorType(key)
{
  var vldType = "required";
  if ((key.indexOf("validator_name=") > -1) || (key.indexOf("validator=") > -1))
  {
    vldType = "custom";    
  }    
  else if ((key.indexOf("||") > -1) || (key.indexOf("&&") > -1))
  {
    vldType = "group";
  }
  return vldType;
}


function extractFieldName(key)
{
    var fldName = key;
    if (fldName.indexOf("?") > -1)
      fldName = fldName.substr(0, fldName.indexOf("?"));
    
    var comparisonSign = "";  
    if (fldName.indexOf("==") > 0)
      comparisonSign = "==";
    else if (fldName.indexOf("=") > 0)
      comparisonSign = "=";
    else if (fldName.indexOf(">") > 0)
      comparisonSign = ">";
    else if (fldName.indexOf("<") > 0)
      comparisonSign = "<";
    else if (fldName.indexOf("<>") > 0) 
      comparisonSign = "<>";
    else if (fldName.indexOf("!=") > 0) 
      comparisonSign = "!=";
    else if (fldName.indexOf("<=") > 0) 
      comparisonSign = "<=";
    else if (fldName.indexOf("=<") > 0) 
      comparisonSign = "=<";
    else if (fldName.indexOf(">=") > 0) 
      comparisonSign = ">=";
    else if (fldName.indexOf("=>") > 0) 
      comparisonSign = "=>";
    
    if (comparisonSign != "")
      fldName = fldName.substr(0, fldName.indexOf(comparisonSign));
    
    if (fldName.indexOf("||") > -1)
      fldName = fldName.substr(0, fldName.indexOf("||"));
    else if (fldName.indexOf("&&") > -1)
      fldName = fldName.substr(0, fldName.indexOf("&&"));
      
    return fldName;
}



//----------------------//
//                      //
// UNIVERSAL VALIDATORS //
//                      //
//----------------------//

function validateEmail(field, aParams)
{
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(field.value.replace(/ /g,""));
}


function validateWebsite(field, aParams)
{
  var strWebsite = allTrim(field.value); 
  if ((strWebsite.toLowerCase().indexOf("https://") == -1) &&
      (strWebsite.toLowerCase().indexOf("http://") == -1))
      strWebsite = "http://" + strWebsite;
  field.value = strWebsite; 
  
  var regex = /^(((http(s?))|(ftp))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$/;
  return regex.test(strWebsite);
}


function validatePostalCodeOrZipCode(field, aParams)
{
  return validateCanadianPostalCode(field, aParams) || validateUsaZipCode(field, aParams);
}


function validateCanadianPostalCode(field, aParams)
{
  var strPcode = field.value.replace(/ /g,"").toUpperCase();
  if ((strPcode.length != 6) || !isNaN(strPcode)) return false;
  strPcode = strPcode.substr(0,3) + " " + strPcode.substr(3);
  
  var regex = /[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d/;
  var boolResult = regex.test(strPcode);
  if (boolResult == true)
    field.value = strPcode; 
  return boolResult;
}


function validateUsaZipCode(field, aParams)
{
  var strZipCode = field.value.replace(/ /g,"");
  if (isNaN(strZipCode)) return false;
  if (strZipCode.length != 5) return false;
  field.value = strZipCode; 
  
  var regex = /[0-9][0-9]{4}/;
  return regex.test(strZipCode);
}


function validatePhoneNumber(field, aParams)
{
    var strNumber = parseOutNumber(field.value.replace(/ /g,""));
    var strNumberFormatted = "";
    
    if (strNumber.length < 7) return false;
    
    var intAreaCodeEndIndex = -1;
    if (strNumber.length >= 10)
    {
       intAreaCodeEndIndex = strNumber.length >= 11 ? 3 : 2;
    }
    
    if (strNumber.length >= 10)
      strNumberFormatted = "(" + strNumber.substr(0, intAreaCodeEndIndex + 1) + ") ";
    strNumberFormatted += strNumber.substr(intAreaCodeEndIndex + 1,3);
    strNumberFormatted += "-" + strNumber.substr(intAreaCodeEndIndex + 4); 
    field.value = strNumberFormatted;
    return true;
}


function validateNumber(field, aParams)
{
    return !isNaN(field.value.replace(/ /g,""));
}


function validateInteger(field, aParams)
{
    var strNumber = field.value.replace(/ /g,"");
    
    if (isNaN(strNumber)) return false;
    
    var numOfDecimals = 0;
    if (strNumber.indexOf(".") > -1)
      numOfDecimals = strNumber.substr(strNumber.indexOf(".")).length - 1;
    
    return numOfDecimals == 0;
}


function validateNoNumericCharacters(field, aParams)
{
    return parseOutNumber(field.value.replace(/ /g,"")) == "";
}


function validateNumberRange(field, aParams)
{
    var strNumber = field.value.replace(/ /g,"");
    if (isNaN(strNumber)) return false;
    
    rangeFr = getSimpleArrayValue(aParams, "range_from"); 
    if (rangeFr == "")
      rangeFr = getSimpleArrayValue(aParams, "min_range"); 
    
    rangeTo = getSimpleArrayValue(aParams, "range_to"); 
    if (rangeTo == "")
      rangeTo = getSimpleArrayValue(aParams, "max_range"); 
    
    var boolResult = (rangeFr == "") ? true : eval(strNumber + " >= " + rangeFr);
    boolResult = boolResult && ((rangeTo == "") ? true : eval(strNumber + " <= " + rangeTo));

    return boolResult;
}


function validateNumberOfDecimalPlaces(field, aParams)
{
    var strNumber = field.value.replace(/ /g,"");
    if (isNaN(strNumber)) return false;
    
    var minDecimals = getSimpleArrayValue(aParams, "min_decimals"); 
    if (parseInt(minDecimals) == 0) minDecimals = "";
    
    var maxDecimals = getSimpleArrayValue(aParams, "max_decimals"); 
    if (parseInt(maxDecimals) == 0) maxDecimals = "";
    
    var numOfDecimals = 0;
    if (strNumber.indexOf(".") > -1)
      numOfDecimals = strNumber.substr(strNumber.indexOf(".")).length - 1;
    
    var boolResult = (minDecimals == "") ? true : (numOfDecimals >= minDecimals);
    boolResult = boolResult && ((maxDecimals == "") ? true : (numOfDecimals <= maxDecimals));

    var exactDecimals = getSimpleArrayValue(aParams, "decimals"); 
    if (exactDecimals != "")
    {
      boolResult = numOfDecimals == exactDecimals;
    }
    
    return boolResult;
}


function validateTextLength(field, aParams)
{
    var strText = field.value.replace(/ /g,"");
    var strTextLength = strText.length; 
    
    var minLength = getSimpleArrayValue(aParams, "min_length"); 

    var maxLength = getSimpleArrayValue(aParams, "max_length"); 

    var boolResult = (minLength == "") ? true : eval(strTextLength + " >= " + minLength);
    boolResult = boolResult && ((maxLength == "") ? true : eval(strTextLength + " <= " + maxLength));

    var exactLength = getSimpleArrayValue(aParams, "length"); 
    if (exactLength != "")
    {
      boolResult = strTextLength == parseInt(exactLength);
    }
    
    return boolResult;
}
