﻿var __FormErrorCount=0;
var __FormChangeCount=0;

function __Replace(value,searchString,replaceString)
{
  if(searchString!=replaceString&&value!="")
  {
    while(value.indexOf(searchString)>-1)
    {
      value=value.replace(searchString,replaceString);
    }
  }
  return(value);
}

function __Compare(ctlId1, ctlId2)
{
  var isValid=false;
  var ctl1=document.getElementById(ctlId1);
  if(ctl1!=null)
  {
    var ctl2=document.getElementById(ctlId2);
    if(ctl2!=null)
    {
      if(ctl1.value==ctl2.value)
      {
        isValid=true;
      }
    }
  }
  return(isValid);
}

function __IsEmpty(ctlId)
{
  return(__GetValue(ctlId)=="");
}

function __GetValue(ctlId)
{
  var ctl=document.getElementById(ctlId);
  if(ctl!=null)
  {
    return(ctl.value.replace(/^\s+|\s+$/g, ''));
  }
  else
  {
    return("");
  }
}

function __GetURLValue(ctlId)
{
  var url=__GetValue(ctlId).toLowerCase();
  if((url.length>=6&&url.substr(0,7)=="http://")||(url.length>=8&&url.substr(0,8)=="https://")||(url.length>=6&&url.substr(0,6)=="ftp://"))
  {
    return(url);
  }
  else
  {
    if(url=="")
    {
      return("");
    }
    else
    {
      return("http://"+url);
    }
  }
}

function __GetCheckValue(ctlId)
{
  var ctl=document.getElementById(ctlId);
  if(ctl!=null)
  {
    return(ctl.checked);
  }
  else
  {
    return(false);
  }
}

function __GetBooleanValue(ctlId)
{
  var ctl=document.getElementById(ctlId);
  if(ctl!=null)
  {
    var val=ctl.value.replace(/^\s+|\s+$/g, '').toLowerCase();
    var boolVal=false;
    if(val=="true"||val=="yes")
    {
      boolVal=true;
    }
    return(boolVal);
  }
  else
  {
    return(false);
  }
}

function __GetNumericValue(ctlId)
{
  var ctl=document.getElementById(ctlId);
  if(ctl!=null)
  {
    var val=ctl.value.replace(/^\s+|\s+$/g, '').toLowerCase();
    if(isNaN(val))
    {
      return(0);
    }
    else
    {
      return(val*1);
    }
  }
  else
  {
    return(0);
  }
}

function __SetValue(ctlId, value)
{
  var ctl=document.getElementById(ctlId);
  if(ctl!=null)
  {
    ctl.value=value;
  }
}

function __ValidateNotEmpty(ctlId)
{
  var isEmpty=true;
  var ctl=document.getElementById(ctlId);
  if(ctl)
  {
    isEmpty=__IsEmpty(ctlId);
    __SetLabel(ctl,isEmpty);
  }
  __FormErrorCount=__FormErrorCount+(isEmpty==true?1:0);
  return(!isEmpty);
}

function __ValidateEmailAddress(ctlId)
{
  var isValid=false;
  var ctl=document.getElementById(ctlId);
  if(ctl)
  {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
    isValid=emailPattern.test(ctl.value.replace(/^\s+|\s+$/g, ''));
    __SetLabel(ctl,!isValid,"The e-mail address that you have entered is invalid");
  }  
  __FormErrorCount=__FormErrorCount+(isValid==true?0:1);
  return(isValid);
}

function __ValidateURL(ctlId)
{
  var isValid=false;
  var ctl=document.getElementById(ctlId);
  if(ctl)
  {
    isValid=true;
    var urlPattern = /^(((ht|f)tp(s?))\:\/\/)?(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\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$/;
    isValid=urlPattern.test(ctl.value.toLowerCase().replace(/^\s+|\s+$/g, ''));
    __SetLabel(ctl,!isValid,"The URL that you have entered is invalid");    
  }  
  __FormErrorCount=__FormErrorCount+(isValid==true?0:1);
  return(isValid);
}

function __ValidateCompare(ctlId1, ctlId2)
{
  isValid=__Compare(ctlId1,ctlId2);
  __FormErrorCount=__FormErrorCount+(isValid==true?0:1);
  return(isValid);
}

function __ValidateIsNumeric(ctlId)
{
  var isValid=false;
  var ctl=document.getElementById(ctlId);
  if(ctl)
  {
    isValid=!isNaN(ctl.value.replace(/^\s+|\s+$/g, ''));
    __SetLabel(ctl,!isValid);
  }  
  __FormErrorCount=__FormErrorCount+(isValid==true?0:1);
  return(isValid);
}

function __ValidateRange(ctlId,min,max)
{
  var isValid=false;
  var ctl=document.getElementById(ctlId);
  if(ctl)
  {
    var message="Please enter a numeric value";
    var value=ctl.value.replace(/^\s+|\s+$/g, '');
    if(!isNaN(value))
    {
      value=value*1;
      isValid=true;
      if(min!=null&&value<min)
      {
        isValid=false;
        message="Amount must be greater than "+min;
      }      
      else
      {
        if(max!=null&&value>max)
        {
          isValid=false;
          message="Amount must be less than "+max;
        }
      }
    }
    __SetLabel(ctl,!isValid,message);
  }  
  __FormErrorCount=__FormErrorCount+(isValid==true?0:1);
  return(isValid);
}

function __ValidateMinLen(ctlId,minLen)
{
  var isValid=false;
  var ctl=document.getElementById(ctlId);
  if(ctl)
  {
    isValid=ctl.value.replace(/^\s+|\s+$/g, '').length>=minLen;
    __SetLabel(ctl,!isValid,"Please enter a minimum of "+minLen+" characters");
  }  
  __FormErrorCount=__FormErrorCount+(isValid==true?0:1);
  return(isValid);
}

function __ValidateMaxLen(ctlId,maxLen)
{
  var isValid=false;
  var ctl=document.getElementById(ctlId);
  if(ctl)
  {
    isValid=ctl.value.replace(/^\s+|\s+$/g, '').length<=maxLen;
    __SetLabel(ctl,!isValid,"Please enter no more than "+maxLen+" characters");
  }  
  __FormErrorCount=__FormErrorCount+(isValid==true?0:1);
  return(isValid);
}

function __SetLabel(ctl,isError,message)
{
  var parent=ctl.parentNode;
  if(parent)
  {
    var label=null;
    var labels=parent.getElementsByTagName("label");
    if(labels.length>0)
    {
      label=labels[0];
    }
    if(label!=null)
    {
      label.className=(isError==true?"NormalRed":"");
    }   
  }
  __SetErrorMessage(ctl,isError,message);
}

function __SetErrorMessage(ctl,isError,message)
{
  var parent=ctl.parentNode;
  if(parent)
  {
    var spans=parent.getElementsByTagName("span");
    for(var i=spans.length-1;i>=0;i--)
    {
      if(spans[i].className=="NormalRed")
      {
        parent.removeChild(spans[i]);
      }      
    }
    if(message!=null&&isError)
    {
      errorSpan=document.createElement("span");
      errorSpan.innerHTML=message;
      errorSpan.className="NormalRed";
      parent.appendChild(errorSpan);
    } 
  }
}

function __AcceptNumericOnly(e)
{
  var result=true;
  var unicode=e.charCode?e.charCode:e.keyCode;
  if(unicode!=8&&unicode!=9)
  {
    if(unicode<48||unicode>57)
    {
      result=false;
    }
  }
  return(result);
}

function __AcceptDecimalOnly(e,obj)
{
  var result=true;
  var unicode=e.charCode?e.charCode:e.keyCode;
  if(unicode!=8&&unicode!=9&&unicode!=46)
  {
    if(unicode<48||unicode>57)
    {
      result=false;
    }
  }
  else
  {
    if(obj!=null&&unicode==46)
    {
      if(obj.value.indexOf(".")>-1)
      {
        result=false;
      }
    }
  }
  return(result);
}

function __AcceptNumericAndSpace(e)
{
  var result=true;
  var unicode=e.charCode?e.charCode:e.keyCode;
  if(unicode!=8&&unicode!=9&&unicode!=32)
  {
    if(unicode<48||unicode>57)
    {
      result=false;
    }
  }
  return(result);
}

function __AcceptNonSpace(e)
{
  var result=true;
  var code=e.charCode? e.charCode : e.keyCode;
  if(code==" "){
    result=false;
  }
  return(result);
}
function __FormatCurrency(ctlId)
{
  __FormatDecimal(ctlId,2);
}
function __FormatDecimal(ctlId,decimals)
{
  if(decimals<=10)
  {
    var obj=document.getElementById(ctlId);
    if(obj)
    {
      var multiplier=("1"+"0000000000".substr(0,decimals))*1;
      obj.value=(Math.round(__GetNumericValue(ctlId)*multiplier)/multiplier).toFixed(decimals);
    } 
  }
}