function checkForm(form)
{var i;
 var arr;
 var e;
 var x;

 for (i=0;i<form.elements.length;i++)
 {if (form.elements[i].name.substr(0,4) == "chk_")
  {
   e = GetFieldValueByName(form, form.elements[i].name.substr(4));
   if (e.value != null)
   {e.value = Trim(e.value);
    arr = form.elements[i].value.split("|");      
    switch(arr[0])
    {
    case "combo":

       //check mandatory field              
      if ((e.value.length == 0) && (arr[1] == "true"))
      {
        alert("El campo '" + arr[2] + "' debe ser ingresado");
        e.focus();
        return false;
      }
      break;

    case "e.value":

       //check mandatory field              
      if ((e.value.length == 0) && (arr[3] == "true"))
      {
        alert("El campo '" + arr[4] + "' debe ser ingresado");
        e.select();
        e.focus();
        return false;
      }
              
      //check min length  
      if (e.value.length < parseInt(arr[1]))
      {
        alert("El valor ingresado en el campo '" + arr[4] + "' es demasiado pequeño");
        e.select();
        e.focus();
        return false;
      }
              
      //check max length
      if (e.value.length > parseInt(arr[2]))
      {
        alert("El valor ingresado en el campo '" + arr[4] + "' es demasiado grande");
        e.select();
        e.focus();
        return false;
      }
      break;

    case "int":
                                
      //check mandatory field
      if ((e.value.length == 0) && (arr[3] == "true"))
      {
        alert("El campo '" + arr[4] + "' debe ser ingresado");
        e.select();
        e.focus();
        return false;
      }
            
      //check sanity
      if(!(IsNumeric(e.value)))
      {
        alert("El valor ingresado en el campo '" + arr[4] + "' debe ser solo con numero");
        e.select();
        e.focus();
        return false;
      }
                        
      //check min value
      if (arr[1] != "x")
      {
        if (parseInt(e.value) < parseInt(arr[1]))
        {
         alert("El valor ingresado en el campo '" + arr[4] + "' es muy pequeño");
        e.select();
        e.focus();
         return false;
        }
      }
            
      //check max length
      if (arr[2] != "x")
      {
        if (parseInt(e.value) > parseInt(arr[2]))
        {
          alert("El valor ingresado en el campo '" + arr[4] + "' es muy grande");
          e.select();
          e.focus();
          return false;
        }
      }
            
      break;
          
    case "float":
                                    
      //check mandatory field
      if ((e.value.length == 0) && (arr[3] == "true"))
      {
        alert("El campo '" + arr[4] + "' debe ser ingresado");
        e.select();
        e.focus();
        return false;
      }
            
      //check sanity
      if(!(IsNumeric(e.value)))
      {
        alert("El valor ingresado en el campo '" + arr[4] + "' debe ser solo con numero");
        e.select();
        e.focus();
        return false;
      }
                        
      //check min value
      if (arr[1] != "x")
      {
        if (parseFloat(e.value) < parseFloat(arr[1]))
        {
          alert("El valor ingresado en el campo '" + arr[4] + "' es muy pequeño");
          e.select();
          e.focus();
         return false;
        }
      }
            
      //check max length
      if (arr[2] != "x")
      {
        if (parseFloat(e.value) > parseFloat(arr[2]))
        {
          alert("El valor ingresado en el campo '" + arr[4] + "' es muy grande");
          e.select();
          e.focus();
          return false;
        }
      }
            
      break;
                    
    case "date":
            
      //check mandatory field
      if ((e.value.length == 0) && (arr[3] == "true"))
      {
        alert("El campo '" + arr[4] + "' debe ser ingresado");
        e.select();
        e.focus();
        return false;
      }
            
      //check sanity
      if(!(IsDate(e.value)))
      {
        alert("El valor ingresado en el campo '" + arr[4] + "' no es una fecha valida (DD/MM/YYYY)");
        e.select();
        e.focus();
        return false;
      }
            
      //check min value
      if (arr[1] != "x")
      {
        if (GetDate(e.value) < GetDate(arr[1]))
        {
          alert("La fecha ingresada en el campo '" + arr[4] + "' es menor que la minima fecha permitida");
          e.select();
          e.focus();
          return false;
        }
      }
            
      //check max length
      if (arr[2] != "x")
      {
        if (GetDate(e.value) > GetDate(arr[2]))
        {
          alert("El valor ingresado en el campo '" + arr[4] + "' es mayor que la maxima fecha permitida");
          e.select();
          e.focus();
          return false;
        }
      }
            
      break;
        }
      }
    }
  }
  
return true;
}

//============================================================================================

function Replace(string,replacechar,replacewith) 
{
  var temp = "";
  var i;
  
  string = '' + string;
  splitstring = string.split(replacechar);
  for(i = 0; i < splitstring.length; i++)
  {
    if(i < (splitstring.length -1))
      temp += splitstring[i] + replacewith;
    else
      temp += splitstring[i];
  }
  return temp;
}

//============================================================================================

function GetFieldValueByName(form,name)
{
  var i;
  
  for (i=0;i<form.elements.length;i++)
  {
    if(form.elements[i].name == name)
      return (form.elements[i]);
  }
  return null;
}

/*

======================= STANDARD HELPER FUNCTIONS BELOW =======================================

*/
function IsNumeric(str)
{
  var i;
  for(i=0;i<str.length;i++)
  {
    if(!(  ((str.charAt(i) <= '9') && (str.charAt(i) >= '0')) ||
        (str.charAt(i) == ' ') || (str.charAt(i) == '.') || (str.charAt(i) == '-')))
      return(false);
  }
  return(true);
}

//============================================================================================

function IsAlphaNumeric(str)
{
  var i;
  for(i=0;i<str.length;i++)
  {
    if(!(  ((str.charAt(i) <= '9') && (str.charAt(i) >= '0')) ||
        ((str.charAt(i) <= 'z') && (str.charAt(i) >= 'a')) ||
        (str.charAt(i) == ' ') || (str.charAt(i) == '-') ||
        (str.charAt(i) == 'ü') || (str.charAt(i) == 'ä') ||
        (str.charAt(i) == 'ö') || (str.charAt(i) == 'Ü') ||
        (str.charAt(i) == 'Ä') || (str.charAt(i) == 'Ö') ||
        (str.charAt(i) == 'é') || (str.charAt(i) == 'ß') ||
        ((str.charAt(i) <= 'Z') && (str.charAt(i) >= 'A')) ))
      return(false);
  }
  return(true);
}

//============================================================================================

function IsAlpha(str)
{
  var i;
  
  for(i=0;i<str.length;i++)
  {
    if(!(  ((str.charAt(i) <= 'z') && (str.charAt(i) >= 'a')) ||
        (str.charAt(i) == ' ') || (str.charAt(i) == '-') ||
        (str.charAt(i) == 'ü') || (str.charAt(i) == 'ä') ||
        (str.charAt(i) == 'ö') || (str.charAt(i) == 'Ü') ||
        (str.charAt(i) == 'Ä') || (str.charAt(i) == 'Ö') ||
        (str.charAt(i) == 'é') || (str.charAt(i) == 'ß') ||
        ((str.charAt(i) <= 'Z') && (str.charAt(i) >= 'A')) ))
      return(false);
  }
  return(true);
}

//============================================================================================

function Trim(str)
{
  //trim leding spaces
  while(true)
  {
    if(str.charAt(0) == ' ')
      str = str.substr(1);
    else
      break;
  }
  
  //trim trailing spaces
  while(true)
  {
    if(str.charAt(str.length-1) == ' ')
      str = str.substr(0,str.length-1);
    else
      break;
  }
  return(str);  
}

//============================================================================================

function IsDate(argDate)
{
  var date_split;
  var i;
  var tdate, tmonth, tyear;
  
  date_split = argDate.split('/');
  
  //check for date parts
  if(date_split.length != 3)
    return(false);
  
  //check for zero values
  //for(i=0;i<date_split.length;i++)
  //{
  //  if (parseInt(date_split[i]) == 0){
  //    return(false);
  //  }
  //}
  
  //check for 4-digit year
  if(date_split[2].length != 4)
    return(false);
    
  //check for valid date, e.g. 29/02/1997
  tdate = parseInt(date_split[0]);
  tmonth = parseInt(date_split[1]);
  tyear = parseInt(date_split[2]);
  
  var date = new Date(parseInt(date_split[2]),parseInt(date_split[1])-1,parseInt(date_split[0]));
  
  
  if(date.getDate() != tdate)
    return(false);
  
  if(date.getMonth() != (tmonth-1))
    return(false);
  
  if(date.getFullYear() != tyear)
    return(false);
    
  return(true);
}


function GetDate(argDate)
{
  //use IsDate() first !!!!
  
  var date_split;  
  var tdate, tmonth, tyear;
  
  date_split = argDate.split('/');
  
  tdate = parseInt(date_split[0]);
  tmonth = parseInt(date_split[1]);
  tyear = parseInt(date_split[2]);
  
  return date = new Date(parseInt(date_split[2]),parseInt(date_split[1])-1,parseInt(date_split[0])-1);
    
}
