var l_strFldFocus="";

//----------------------------------------------------------------------
function SetStyle(fldObj,p_mode) { 
//----------------------------------------------------------------------
    if (p_mode==1) {  //onfocus
     fldObj.style.backgroundColor = '#FEE679';
     if (fldObj.type=='text' || fldObj.type=='textarea') {
           //fldObj.style.color = '#336699';
           //fldObj.style.fontWeight = 700;
         }
   } else {  //onblur
     fldObj.style.backgroundColor = '#FFFFFF';
     if (fldObj.type=='text' || fldObj.type=='textarea') {
           //fldObj.style.color = '#000000';
           //fldObj.style.fontWeight = 400;
     }
   }
 } 
 
// Konstruktor für Felder erstellen ---------------------------------------
  function arrFld(yName,yDescription,yStyle,yMinLength,yErrMsg,yDefaultValue) {
  //------------------------------------------------------------------------- 
    this.xName = yName;
    this.xDescription = yDescription;
    this.xStyle = yStyle;
    this.xMinLength = yMinLength;
    this.xErrMsg = yErrMsg;
    this.xDefaultValue = yDefaultValue;
  }
  //---------------------------------------
  function Start() {
  //---------------------------------------
    if (l_strFldFocus!="") {
      SetFieldFocus(l_strFldFocus);
    } else {
      SetFieldFocus();
    }


  }

// -------------------------------------------
function gbvalidate() {
// -------------------------------------------
var dgbname='data[tt_guest][NEW][cr_name]'


if(document.getElementsByName("data[tt_guest][NEW][cr_name]").value == "")
{
  alert("Bitte geben Sie Ihren Namen ein");
  document.getElementsByName("data[tt_guest][NEW][cr_name]").focus();
  return false;
}


}
  
  
  
  //--------------------------------------------------
  function SetFieldFocus() { 
  //--------------------------------------------------
    var d,s,i,args,fld,hitField=false;
    if (document.forms.length>0){
      for (i=0;i<document.forms.length;i++) {
        if (document.forms[i].name!='_msg') {
          d=document.forms[i];
          break;
        }
      }
      if (!d) d=document.forms[0];
      s=d.elements;
      args=SetFieldFocus.arguments
      if (s && s.length>0){
        if (args.length>0) {
          for (i=0;i<d.elements.length;i++) {
            fld=eval("d."+args[0]);
            if (fld==d.elements[i] && !d.elements[i].disabled && d.elements[i].type!='hidden' && d.elements[i].value!='Ihr Suchbegriff') {
              eval("d."+args[0]+".focus()");hitField=true;break;
            }
          }
          if (!hitField) {SetFieldFocus();} //Feldname gesetzt, aber falsch
        } else {  // ohne Angaben des Feldnamens
           if (IE4) {
              for (i=0;i<d.elements.length;i++) {
                if (!d.elements[i].disabled && d.elements[i].type!='hidden' && d.elements[i].value!='Ihr Suchbegriff') {
                  d.elements[i].focus();
                  break;
                }
              }
           } else {
              d.elements[0].focus();
           }
        }
      }
    }
  }

 //-----------------------------------------------------------
  function ValidateKomFields(frm) {
  //-----------------------------------------------------------
    var validate = true,i,effFields='';
    var l_cancel,l_bcheck=true;
    var l_FieldFocus="",l_ErrMsg="",l_requiredFields="";
    l_cancel = frm.action.indexOf("rq_SenderId=-1")

    if (l_cancel != '-1' ) {
      l_bcheck=false;
    }

    if (frm.elements.length>0 && l_bcheck) {
      for (i=0;i<frm.elements.length;i++) {
        effFields=effFields+frm.elements[i].name;
      }

      //- alle Felder durchlaufen, action, wenn Feld gefunden
      for (i=0;i<arrFld.length;i++) {
        if (effFields.indexOf(arrFld[i].xName,0)!='-1') { //Feld im Form gefunden
          if (arrFld[i].xStyle == 'text' ) {
              if (eval('frm.'+arrFld[i].xName+'.value.length')<arrFld[i].xMinLength ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle=='text_optional' || arrFld[i].xStyle=='tel_optional') {
              if ( eval('frm.'+arrFld[i].xName+'.value.length')>0 && eval('frm.'+arrFld[i].xName+'.value.length')<arrFld[i].xMinLength) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'ort_optional') {
              if ( eval('frm.' + arrFld[i].xName + '.value')>0 && !validate_ort(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'mail') {
              if (!validate_email(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'url') {
              if (!validate_url(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'plz') {
              if (!validate_plz(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'tel') {
              if (!validate_tel(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'httpurl_optional') {
              if (eval('frm.'+arrFld[i].xName+'.value.length')>0 && !validate_httpurl(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'httpurl') {
              if (!validate_httpurl(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'bannertimeout') {
              if ( eval('frm.'+arrFld[i].xName+'.value')!=0 && (eval('frm.'+arrFld[i].xName+'.value') < arrFld[i].xMinLength) || (eval('frm.'+arrFld[i].xName+'.value') > arrFld[i].xDefaultValue) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'DropDown') {
              if (eval('frm.'+arrFld[i].xName+'[frm.'+arrFld[i].xName+'.selectedIndex].value')==arrFld[i].xMinLength || eval('frm.'+arrFld[i].xName+'[frm.'+arrFld[i].xName+'.selectedIndex].value')==-1) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'articleno') {
              if (eval('frm.'+arrFld[i].xName+'.value')!= 0 && eval('frm.'+arrFld[i].xName+'.value.length')> 0 ) {
                if (l_ErrMsg=="" && !validate_articleno(eval('frm.' + arrFld[i].xName + '.value' )) ) { // Alles andere ok
                  validate = false;
                  l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                  l_FieldFocus=eval('frm.'+arrFld[i].xName);
                  l_ErrMsg=arrFld[i].xErrMsg;
                }
              }
          } else if (arrFld[i].xStyle == 'ip') {
              if (!validate_ip(eval('frm.' + arrFld[i].xName + '.value' )) ) {
                validate = false;
                if (l_FieldFocus=="") {l_FieldFocus=eval('frm.'+arrFld[i].xName) };
                l_requiredFields += "\t- " + arrFld[i].xDescription + "\n"
                if (l_ErrMsg=="")l_ErrMsg=arrFld[i].xErrMsg;
              }
          } else if (arrFld[i].xStyle == 'ResetDefault') {
              if (eval('frm.'+arrFld[i].xName+'.value.length')<arrFld[i].xMinLength ) {
                eval('frm.'+arrFld[i].xName+'.value="'+arrFld[i].xDefaultValue+'";');
              }
          }
        }
      }
      if (l_requiredFields!="") {
        alert("Pflichtfelder:\n========================\t\n" + l_requiredFields +"\n"+l_ErrMsg);
        l_FieldFocus.focus();
      }
    }
    return validate;
  }
  
  //------------------------------------------------------------------------- 
  function validate_email(item) {
  //------------------------------------------------------------------------- 
   var validate = false;
   var regexp_email = /^([_a-zA-Z0-9-.])+\@([_a-zA-Z0-9-.])+\.([a-z]{2,6})$/ ;
    if (item.length != 0) {
      validate = regexp_email.test(item);
    }
   return validate;
  } // end function validate_articleno

  //------------------------------------------------------------------------- 
  function validate_ip(item) {
  //------------------------------------------------------------------------- 
   var validate = false;
   var regexp_ip = /^([0-9])+\.([0-9])+\.([0-9])+\.([0-9]{1,3})$/ ;
    if (item.length != 0) {
      validate = regexp_ip.test(item);
    }
   return validate;
  } // end function validate_articleno
  //------------------------------------------------------------------------- 
  function validate_httpurl(item) {
  //------------------------------------------------------------------------- 
   var validate = false;
   var regexp_httpurl = /^(http:\/\/)+([_a-zA-Z0-9-]{3,15})+\.([_a-zA-Z0-9-]{3,30})+\.([_a-zA-Z0-9-]{2,6})$/ ;
   var regexp_httpurl2 = /^(http:\/\/)+([_a-zA-Z0-9-]{3,15})+\.([_a-zA-Z0-9-]{3,30})+\.([_a-zA-Z0-9-]{2,6})+\/([_a-zA-Z0-9-\/?&,|.=]{1,300})$/ ;
   var regexp_httpsurl = /^(https:\/\/)+([_a-zA-Z0-9-]{3,15})+\.([_a-zA-Z0-9-]{3,30})+\.([_a-zA-Z0-9-]{2,6})$/ ;
   var regexp_httpsurl2 = /^(https:\/\/)+([_a-zA-Z0-9-]{3,15})+\.([_a-zA-Z0-9-]{3,30})+\.([_a-zA-Z0-9-]{2,6})+\/([_a-zA-Z0-9-\/?&,|.=]{1,300})$/ ;
    if (item.length != 0) {
      validate = regexp_httpurl.test(item) || regexp_httpsurl.test(item) || regexp_httpurl2.test(item) || regexp_httpsurl2.test(item);
    }
   return validate;
  } // end function validate_httpurl
  //------------------------------------------------------------------------- 
  function validate_url(item) {
  //------------------------------------------------------------------------- 
   var validate = false;
   var regexp_url = /^([0-9]{5})+\.([0-9]{6})$/ ;
    if (item.length != 0) {
      validate = regexp_url.test(item);
    }
   return validate;
  } // end function validate_url
  //------------------------------------------------------------------------- 
  function validate_plz(item) {
  //------------------------------------------------------------------------- 
   var validate = false;
   var regexp_plz = /^([0-9]{5})$/ ;
    if (item.length != 0) {
      validate = regexp_plz.test(item);
    }
   return validate;
  } // end function validate_url
  //------------------------------------------------------------------------- 
  function validate_tel(item) {
  //------------------------------------------------------------------------- 
   var validate = false;
   var regexp_tel = /^([0-9]){6,15}$/ ;
    if (item.length != 0) {
      validate = regexp_tel.test(item);
    }
   return validate;
  } // end function validate_url


//****************************************
// Initialize function
// Browser Detection
//Language: German locale
//****************************************
NS4=(document.layers);
IE4=(document.all);
W3C=(document.documentElement && !document.all);
MAXINT  = Math.pow(2,31);



//****************************************
// Initialize function
// Event capturing for Netscape
//****************************************
if (NS4) {
  document.captureEvents(Event.KEYPRESS);
  document.onkeypress = processKey;
}

if (W3C) {
  document.addEventListener("keypress",processKey,true);
}

function FillLeading(strIn) {
  var strInput = new String(strIn);
  if(strInput.length > 1)
    return strInput;
  else
    return '0' + strInput;
}



  var whitespace = " \t\n\r";

//****************************************
// Date Helper function
//****************************************
function isDay(s) {
      return isIntegerInRange(s, 1, 31);
}

//****************************************
// Check for integrity
//****************************************
function isInteger(s) {
    var i;
      
      for ( i = 1; i < s.length; i++ )
      {   
          var c = s.charAt(i);
          if ( ! isDigit(c)) return false;
      }
      return true;
  }
  
//****************************************
// Check for letters
//****************************************
function isLetter(s) {
    var i;
      
      for ( i = 1; i < s.length; i++ )
      {   
          var c = s.charAt(i);
          if ( isDigit(c)) return false;
		  //if ( ! isSingleLetter(c)) return false;
      }
      return true;
  }


//****************************************
// Helper function
//****************************************
function isIntegerInRange(s, a, b) {
      if ( ! isInteger(s) ) return false;
      var num = parseInt (s);
      return ( ( num >= a ) && ( num <= b ) );
}



//****************************************
// makeArrey
//****************************************

function makeArray(n) {
     for ( var i = 1; i <= n; i++ ) {
        this[i] = 0
     } 
     return this
}

//****************************************
// Trim whitespaces
//****************************************
function trim(s) {
    return lTrim(rTrim(s));
}
  
function lTrim(s)
{
  return s.replace(/^ +/g, "");
}

function rTrim(s)
{
  return s.replace(/ +$/g, "");
}

function Trim(s)
{
  return s.replace(/^ +| +$/g, "");
}

//****************************************
// canInput
//****************************************
function canInput(str, cKey,iMaxCount){
  var iCount = 0;
  for (i = 0; i < str.length; i++) {
    if (str.charAt(i) == cKey)
      iCount++;
  }
  return (iCount < iMaxCount) ? true:false;
}

//****************************************
// General
//****************************************
function isDigit(c) {
  return ( ( c >= "0" ) && ( c <= "9" ) )
}

function isSingleLetter(c) {
  //return ( ( c >= "a" ) && ( c <= "z" ) || ( c >= "A" ) && ( c <= "Z" ))
  return ( ( c >= "a" ))
}

//****************************************
// Trim leading Zero
//****************************************
function trimLeadingZero(s) {
    
     if(s.length > 0 && s.charAt(0) == '0')
      s = s.substring(1,s.length);
     return s;
}

//****************************************
// Trim all leading Zeros
//****************************************
function trimAllLeadingZero(s) {
 while (s.charAt(0) == '0') 
    {
    if(s.length > 0 && s.charAt(0) == '0')
     s = s.substring(1,s.length);
    }
    return s;
}

//*************************
// invoked by 'onBlur()'
// Param: object
// ************************
function ValidateInteger(obInt)
{
  strC = obInt.value; //original 
  //strC = trimAllLeadingZero(obInt.value); //alle führenden Nullen entfernen
  //strC = trimLeadingZero(obInt.value); //erste Null entfernen
  obInt.value = (isInteger(strC)) ? strC : 'Ungültige Eingabe';

}

//*************************
// invoked by 'onBlur()'
// Param: object
// ************************
function ValidateLetter(obInt)
{
  strC = obInt.value; //original 
  //strC = trimAllLeadingZero(obInt.value); //alle führenden Nullen entfernen
  //strC = trimLeadingZero(obInt.value); //erste Null entfernen
  obInt.value = (isLetter(strC)) ? strC : 'Ungültige Eingabe';
}


function IsINTInRange(strC)
{
  
  l_lIntValue = parseInt(strC);
  if(isNaN(l_lIntValue))
    return false;
  l_lIntValue = Math.abs(l_lIntValue);
  
  if(l_lIntValue > MAXINT)
    return false;
  
  return true;
}


//***********************************
// Input Handler for Letters Control
// invoked by 'onkeypress'
// Accepts: '.' but only two of them
// Numbers
// ':'
//***********************************
function validateLettersOnlyInput(strItemValue, cKey){
  
  switch(cKey)
  {
    case(' '):
      return canInput(strItemValue,' ',4);
      break;
    case('@'):
      return canInput(strItemValue,'@',2);
      break;
    //case('a'):
    //  return canInput(strItemValue,'a',0);
    //  break;
    case('/'):
      return canInput(strItemValue,'/',1);
      break;
       	default:
      return isSingleLetter(cKey);
  }
  return true;
}



//***********************************
// Input Handler for IntegerOnly Control
// invoked by 'onkeypress'
// Accepts nothing but numbers
//***********************************
function validateIntegerOnlyInput(strItemValue, cKey){
0
  switch(cKey)
  {
    default:
      return isDigit(cKey);
  }
  return true;
}

//***********************************
// Input Handler for Integer Control
// invoked by 'onkeypress'
// Accepts: '-' 
// Numbers
//***********************************
function validateIntegerInput(strItemValue, cKey, lUnSigned){

  //wird nicht mehr benötigt, da keine '-' mehr zulässig durch processkey
  //switch(cKey)
  //{
  //  case('-'):
  //    if (!lUnSigned)
  //    {
  //      return canInput(strItemValue, '-', 1);
  //    }
  //    
  //  default:
      if (isDigit(cKey))
      {
        NewVal =   strItemValue + cKey;
        return IsINTInRange(NewVal);
      }
  //}

  return false;
}

//***********************************
// Input Handler Control
// invoked by 'onkeypress'
//***********************************
function processValidation(strItemValue, cKey,lControl){
  
  var bValidated;

  switch(lControl)
  {
    //wird nicht eintreten, da in processkey auskommentiert
    //case(1):
    //  bValidated = validateCurrencyInput(strItemValue, cKey);
    //  break;
    case(2):
      bValidated = validateDateInput(strItemValue, cKey);
      break;
    case(3):
      bValidated = validateIntegerOnlyInput(strItemValue, cKey);
      break;
    //wird nicht eintreten, da in processkey auskommentiert
    //case(4):
    //  bValidated = validateIntegerInput(strItemValue, cKey,0);
    //  break;
    case(5):
      bValidated = validateIntegerInput(strItemValue, cKey,1);
      break;
    //wird nicht eintreten, da in processkey auskommentiert
    //case(6):
    //  bValidated = validateFloatInput(strItemValue, cKey);
    //  break;
    case(7):
      bValidated = validateLettersOnlyInput(strItemValue, cKey);
      
      break;
  }
  return bValidated;
}

//***********************************
// Input Handler Control
// invoked by 'onkeypress'
//***********************************
function processKey(e) {
  var lControl;
  if (NS4 || W3C)
  {
    oItemObj = e.target;
  }
  else
  {
    oItemObj = e;
  }
  
  switch(oItemObj.UPtype)
  {
    case("UPDate"):
      lControl = 2;
      break;
    case("UPIntegerOnly"):
      lControl = 3;
      break;
    case("UPUnsignedInteger"):
      lControl = 5;
      break;
    case("UPLettersOnly"):
      lControl = 7;
      break;
    default:
      lControl = 0;
      break;
  }
  if(lControl)
  {
    if (NS4 || W3C)
    {
      lWhichASC = e.which;
    }
    else
    {
      lWhichASC = event.keyCode;
    }
    
    if(lWhichASC == 0)
      return true;

    if(lWhichASC == 8)
      return true;
    cWhichKey = String.fromCharCode(lWhichASC).toLowerCase();
    strItemName = (NS4 || W3C) ? e.target.name : e.name;
    strItemValue = (NS4 || W3C) ? e.target.value : e.value;
    bValidated = processValidation(strItemValue,cWhichKey,lControl);
    //return (NS4) ? bValidated : event.returnValue= lWhichASC;
  
    if(NS4 || W3C)
    {
       return bValidated;
    }
    else
    {
      if(bValidated)
        return event.returnValue= lWhichASC;
      else
        return event.returnValue=false;
    }
   }
   else
   {
    //return   (NS4) ? true : event.returnValue=true;
    return true;
   }
  
}
