﻿function ValidateFields(ctl, szLabel, szdatatype, bisnullable, bDisplayTheError)
{ 

  var szrecheckdate = /^(?=\d)(?:(?!(?:(?:0?[5-9]|1[0-4])(?:\.|-|\/)10(?:\.|-|\/)(?:1582))|(?:(?:0?[3-9]|1[0-3])(?:\.|-|\/)0?9(?:\.|-|\/)(?:1752)))(31(?!(?:\.|-|\/)(?:0?[2469]|11))|30(?!(?:\.|-|\/)0?2)|(?:29(?:(?!(?:\.|-|\/)0?2(?:\.|-|\/))|(?=\D0?2\D(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|2[0-8]|1\d|0?[1-9])([-.\/])(1[012]|(?:0?[1-9]))\2((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?:$|(?=\x20\d)\x20)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;
  var szrecheckint = /^(\+|-)?\d+$/;
  var szrecchecknumber = /^((\d?)|(([-+]?\d+\.?\d*)|([-+]?\d*\.?\d+))|(([-+]?\d+\.?\d*\,\ ?)*([-+]?\d+\.?\d*))|(([-+]?\d*\.?\d+\,\ ?)*([-+]?\d*\.?\d+))|(([-+]?\d+\.?\d*\,\ ?)*([-+]?\d*\.?\d+))|(([-+]?\d*\.?\d+\,\ ?)*([-+]?\d+\.?\d*)))$/;    
  var szreccheckmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/;
  var bOK = false;
  var szerrmsg;
  var szvalue = ctl.value;
  lastszdatatype = szdatatype;
  lastbisnullable = bisnullable;
  var label = '';
  if(szLabel != '')
  {
    label = szLabel;   
  }
    

  if (szdatatype == 'System.String') {
    bOK = true;
  }

  if (szdatatype == 'System.Boolean') {
    bOK = true;      
  }
  
  if (szdatatype == 'Indeca.Mail') {     
    
    bOK = szreccheckmail.test(szvalue);
    szerrmsg = "Ihre Eingabe <val> hat kein gültiges E-Mail Format.<br /> E-Mail bitte im Format postfach@provider.de eingeben.";
  }
  
  if (szdatatype == 'System.DateTime') {
    bOK = szrecheckdate.test(szvalue)
    szerrmsg = "Ihre Eingabe <val> hat kein gültiges Datumsformat.<br /> Datum bitte im Format dd.mm.yyyy (z.B. 1.8.2007 oder 30.11.2006) eingeben."      
  }
  
  if (szdatatype == 'System.DateTime.Volljaehrigkeit') {
      bOK = szrecheckdate.test(szvalue)
      szerrmsg = "Ihre Eingabe <val> hat kein gültiges Datumsformat.<br /> Datum bitte im Format dd.mm.yyyy (z.B. 1.8.2007 oder 30.11.2006) eingeben."

      if (bOK == true)  {
          bOK = VolljaehrigkeitPruefen(szvalue)
          szerrmsg = "Es sind Fehler bei der Altersprüfung aufgetreten.<br />Bitte überprüfen Sie das eingegebene Datum."
      }
  }
  
  if (szdatatype == 'System.Int16' || szdatatype == 'System.Int32' || szdatatype == 'System.Int64') {
    bOK = szrecheckint.test(szvalue)
    szerrmsg = "Ihre Eingabe <val> hat kein gültiges Nummernformat.<br /> Wert bitte im Format nnn (z.B. 80 oder 827) eingeben."
  }

  if (szdatatype == 'System.Decimal' || szdatatype == 'System.Single' || szdatatype == 'System.Double') {
    bOK = szrecchecknumber.test(szvalue)
    szerrmsg = "Ihre Eingabe <val> hat kein gültiges Nummernformat.<br /> Wert bitte im Format nnn,nn (z.B. 100,39 oder 100) eingeben."
  }

  if (szvalue.length > 0) 
  {
    if (bOK) 
    {
      if(bDisplayTheError) {
          ctl.style.borderColor = "#999";
          //ctl.style.backgroundColor = "#fff";
          ctl.style.borderWidth = "0.1em";        
          ctl.style.height = "1.4em";
          ctl.style.paddingLeft = "0.2em";
          if (label != '') {
              document.getElementById(label).style.color = "#58585a";
          }        
      }
      objLastField = null;
    } 
    else
    {
      if(bDisplayTheError) {
          ctl.style.borderColor = "#AE0E1B";
//          ctl.style.backgroundColor = "#ff5555";
          ctl.style.borderWidth = "0.1em";  
          ctl.style.height = "1.4em";
          ctl.style.paddingLeft = "0.2em";
          if (label != '') {
              document.getElementById(label).style.color = "#AE0E1B";
          }
          //ShowErrorMsg(ctl, szerrmsg);        
        }
      objLastField = ctl;
      bAllOk = false;
    }
  } 
  else
  {
    if (bisnullable)
    {          
      if(bDisplayTheError) {      
          ctl.style.borderColor = "#999";
//          ctl.style.backgroundColor = "#fff";
          ctl.style.borderWidth = "0.1em";
          ctl.style.height = "1.4em";
          ctl.style.paddingLeft = "0.2em";
          if (label != '') {
              document.getElementById(label).style.color = "#58585a";
          }       
        }
      objLastField = null;
    }
    else
    {
      if(bDisplayTheError) {
          ctl.style.borderColor = "#AE0E1B";
//          ctl.style.backgroundColor = "#ff5555";
          ctl.style.borderWidth = "0.1em";   
          ctl.style.height = "1.4em";
          ctl.style.paddingLeft = "0.2em";
          if (label != '') {
              document.getElementById(label).style.color = "#AE0E1B";
          }
          //ShowErrorMsg(ctl, "Dieses Feld ist ein Mussfeld, bitte geben Sie einen Wert ein!");        
          }
      objLastField = ctl;
      bAllOk = false;
    }
  }
  
}


