﻿// JavaScript Document
/**
* Constants
***/

String.prototype.format=function(){
    var str=this;
    
    for(var i=0;i<arguments.length;i++){
		str=str.replaceAll(new RegExp("\\{" + i + "\\}"),arguments[i]);	        	
		}
    return str;
};
//replaceAll(/\{0\}/,rstr);		
String.prototype.replaceAll=function(search,rStr){
    var str=this;
    if(RegExp.prototype.isPrototypeOf(search))
        while(search.test(str))
            str=str.replace(search,rStr);
    else{
        var tArr=str.split(search);
        str=tArr.join(rStr);
        tArr.length=0;
        tArr=null;
    }
    return str;
};


Messages=new Array();

var CheckDomForms = new Array();
var TestIt = true;
var pseudoForm  ="_form";
var formCaption ="Form";
var ErrorColor = "#000";
var RequiredLabel= "*";
var DateRegEx = /(\d{1,2})[\.](\d{1,2})[\.](\d{1,4})/;
var DateDelimiter = ".";

var MonthDays = [0,31,28,31,30,31,30, 31, 31,30,31,30,31];

var AllowedList = new Array();
AllowedList['Nummer'] = '0123456789';
AllowedList['LowAlpha'] = 'abcdefghijklmnopqrstuvwxyz';
AllowedList['UpAlpha'] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
AllowedList['Umlauts'] = 'äöüÄÖÜß';
AllowedList['Alpha'] = ''+AllowedList['LowAlpha'] + AllowedList['UpAlpha'];
AllowedList['Email'] = '@.-_';
AllowedList['Telephon'] = ''+AllowedList['Nummer'];
AllowedList['Telephon2'] = '-/ ,.+()'+AllowedList['Telephon'];
AllowedList['Mathematic'] = '+/*-=%';
AllowedList['ExtraAlpha'] = ' :;.,&+*%/$-?!=_';
AllowedList['Betrag'] = '.,-'+AllowedList['Nummer'];
AllowedList['Comma'] = ',';
AllowedList['Blank'] = ' ';
AllowedList['Dot'] = '.';
AllowedList['And'] = '&';
AllowedList['Date'] = '.'+AllowedList['Nummer'];
AllowedList['User1'] = ' -.'+AllowedList['Alpha']+AllowedList['Nummer']+AllowedList['Umlauts'];  // for city, etc...
AllowedList['User2'] = ' /&.-'+AllowedList['Alpha']+AllowedList['Nummer']+AllowedList['Umlauts'];  // ie. for company
AllowedList['User3'] = '-'+AllowedList['Alpha']+AllowedList['Nummer'];  // ie. for hausnummer
AllowedList['User4'] = ','+AllowedList['User2'];  // ie. bankverbindung, kontoname
AllowedList['User5'] = ' .-'+AllowedList['Alpha']+AllowedList['Umlauts'];  // ie. nachname, vorname
AllowedList['User6'] = "'"+AllowedList['User1'];  // for streetnames

//** FUNCTIONS
function getLabel(fID,Parent){
	Parent=Parent ? Parent : document;
	var attrn=is.opera ? 'htmlFor' : 'for';
	var tLa=getElementsByAttributeValue(attrn,fID,'label',Parent,true);
	if(tLa && tLa[0]) return tLa[0];else return null;
}

function setRequiredLabel(fID,Req,Parent){
	//expects: <label for="fID">labeltext<span>*</span></label>
	var tLab=getLabel(fID,Parent);
	if(tLab){
		var tSpa=tLab.getElementsByTagName('span');
		if(tSpa && tSpa.item(0)){
			if(Req) tSpa.item(0).innerHTML=RequiredLabel;else tSpa.item(0).innerHTML='';
		}
	}
}

function setStatus(s){
  top.self.status=s;
  return true;
}

//Parameter printAll als flag, welches entscheidet, ob ein oder alle Fehler gleichzeitig ausgegeben wird bzw. werden
//printAll false ein Fehler wird ausgegeben (der erste gefundene)
//printAll true alle Fehler werden ausgegeben (unterschiedliche errorContainer vorausgesetzt sonst der letzte)

function submitForm(CHK_FormName, doCheck, command, popup, doSubmit, printAll){	
	if(doSubmit!=false) doSubmit=true;
	dbWrite('SUBMIT');
	var CHK_Form=CheckDomForms[CHK_FormName];
	var retvalue;
	if(CHK_Form){		
		if(CHK_Form.Ref.elements['command']) CHK_Form.Ref.elements['command'].value=command;	//set command before actual submit!		

		retvalue = CHK_Form.Check(doSubmit, doCheck, printAll);			// do submit!

		if(command=="reset"){
			 CHK_Form.Ref.reset();
			 return false;
		}else{
			if(retvalue && popup && popup!=null)  openWinMedium(popup,'winXtraInfo');
			return retvalue;
		}
	}
}


function VerifyList(PrintList){
  var j=0;
  dummy = new Array();
  for (var i in PrintList){
  	if (i%2==1)if (PrintList[i][0]!=""){
  			 dummy[j]=PrintList[i-1];
  			 dummy[j+1]=PrintList[i];
  			 j+=2;
  	}
  }
  return dummy;
}
/**
*  used for handling the errors
***/
function TErrorlist(ErrorObject){
  this.ErrorObject=ErrorObject;
  this.Errorlist = new Array(2);
  this.Errorlist[0]= new Array();
  this.Errorlist[1]= new Array();
}
TErrorlist.prototype.ClearErrorlist = function(){
  this.Errorlist[0].length=0;
  this.Errorlist[1].length=0;
}
TErrorlist.prototype.AddError = function(where,message){
  this.Errorlist[0][this.Errorlist[0].length]=where;
  this.Errorlist[1][this.Errorlist[1].length]=message;
}
/**
* have an Output for server-error-messages********
***/
function TPseudoForm(CheckFormRef){
  this.Name = pseudoForm;
  this.setFocus = false;
  this.ErrorX=0;
  this.ErrorY=0;
  this.ErrorW=0;
  this.ErrorH=0;
  this.Caption=formCaption;
  this.Errorlist= new TErrorlist(this);
  this.setFocus=false;
  this.CheckFormRef=CheckFormRef;
}
TPseudoForm.prototype.FindError = function(){ return false;}
TPseudoForm.prototype.lighten = function(a){}

/**
* used for the form itself
***/
function CreateCheckForm(form,destWin){
  if(form){
	if(!destWin)destWin=self;
	if(getAttributeValue(form,"chkOn")=="true"){
		var FName	= form.name;
      	var CHKName	= getAttributeValue(form,"chkName");
		if(FName!='' && CHKName!=''){
			var NF=new TCheckForm(destWin,FName,CHKName);
			
			//alert('CREATED CHECKFORM'+CHKName);
				NF.Ref=form;
			NF.CheckFields[pseudoForm] = new TPseudoForm(NF);
						
			var SetFocus=NF.initDom();

			
			if(SetFocus){
				NF.win.focus();
				SetFocus.Ref.focus();
			}
			NF.Reminder=SetFocus;
			if(is.opera){
				var tL=getElementsByAttributeValue('type','radio','input',NF.Ref);
				var tLab=null;
				for(var j=0;j<tL.length;j++){
					tLab=getLabel(tL[j].id, NF.Ref);
					if(tLab){
						AddRefHandler('onclick',handle_opera_label_click,tL[j],tLab);
						AddRefHandler('onfocus',handle_opera_radio_focus,tL[j],tL[j]);
					}
				}
				tL=getElementsByAttributeValue('type','checkbox','input',NF.Ref);
				tLab=null;
				for(var j=0;j<tL.length;j++){
					tLab=getLabel(tL[j].id, NF.Ref);
					if(tLab) AddRefHandler('onclick',handle_opera_label_click,tL[j],tLab);  //(EventName,Handler,pointer,eventObj)
				}
			}
			
			for(var j in NF.CheckFields)
				if(j!=pseudoForm)
					if(NF.CheckFields[j].start) NF.CheckFields[j].start(); else delete( NF.CheckFields[j]);
			CheckDomForms[CHKName]=NF;
		}
    }
  }
}

function CreateCheckForms(destWin){
  if(!destWin) destWin=self;
  var formElems=destWin.document.getElementsByTagName("form");
  for(var i=0;i<formElems.length;i++){
	CreateCheckForm(formElems[i],destWin);   
  }
}

function handle_opera_radio_focus(a,b){
  if(!a.disabled && a.checked) a.click();
  return false;
}
function handle_opera_label_click(a,b,c){
  if(!a.disabled) a.click();
  a.focus();
  b.stopPropagation();
  return false;
}

function TCheckForm(win,FormName,instName){
  this.win=self;
  this.instName=instName;
  this.FormName=FormName;
  this.CheckFields = new Array();
  this.CheckSelects = new Array();
  this.WrongElements=new Array();
  this.ActuelError=0;
  this.ErrorCount=-1;
  this.Reminder="";
  this.CheckFocus=true;
  this.FirstCheck=false;

 //eval(instName+'=this;');
 // if(is.ns) win.document.captureEvents(Event.KEYPRESS);
}

TCheckForm.prototype.addField = function(Name,Type,Required,MinL,MaxL,MinValue,MaxValue,RegEx,RegExMsg,TstCode,KeyFlag,Caption,DefaultValue,ErrCntr,ExtraCheck,Ref,SuggestRequest,SuggestSelectSource,SuggestChild,CondRequired,CondRequiredCond){
  return this.CheckFields[Name]= new TCheckField(Name,Type,Required,MinL,MaxL,MinValue,MaxValue,RegEx,RegExMsg,TstCode,this,KeyFlag,Caption,DefaultValue,ErrCntr,ExtraCheck,Ref,SuggestRequest,SuggestSelectSource,SuggestChild,CondRequired,CondRequiredCond);

}

TCheckForm.prototype.SearchNewReminder = function(){
  if(!this.Reminder.activ){
    for(var i in this.CheckFields){
      if(this.CheckFields[i].activ) this.Reminder=this.CheckFields[i];
    }
  }
}

TCheckForm.prototype.initDom = function(){
  var inpElems=new Array();
  inpElems=this.Ref.elements;
  var DefaultField=null;
  for(var i=0;i<inpElems.length;i++){
    if(inpElems[i]){
      if(getAttributeValue(inpElems[i],"chkOn")=="true"){
      	var Name	= getAttributeValue(inpElems[i],"name");
      	if(Name=='') Name = getAttributeValue(inpElems[i],"id");
        var Required 	= getAttributeValue(inpElems[i],"chkRequired") 	? new Boolean(getAttributeValue(inpElems[i],"chkRequired")) 	: false;
        var CondRequired="";
        if(getAttributeValue(inpElems[i],"chkRequired")&&getAttributeValue(inpElems[i],"chkRequired")!="true"){
            CondRequired = getAttributeValue(inpElems[i],"chkRequired") 	? new String(getAttributeValue(inpElems[i],"chkRequired")) 	: "";        
            //alert(CondRequired);
            Required = false;
        }
      	var CondRequiredCond = getAttributeValue(inpElems[i],"chkRequiredCond") 	? new String(getAttributeValue(inpElems[i],"chkRequiredCond")) 	: "";;        
        var MinL 	= getAttributeValue(inpElems[i],"chkMinLength") 	? getAttributeValue(inpElems[i],"chkMinLength") 			: 0;
        var MaxL	= getAttributeValue(inpElems[i],"chkMaxLength") 	? getAttributeValue(inpElems[i],"chkMaxLength") 			: 1000000;
        var MinValue 	= getAttributeValue(inpElems[i],"chkMinValue") 	? getAttributeValue(inpElems[i],"chkMinValue") 			: null;
        var MaxValue	= getAttributeValue(inpElems[i],"chkMaxValue") 	? getAttributeValue(inpElems[i],"chkMaxValue") 			: null;
        var TstCode	= getAttributeValue(inpElems[i],"chkAllowedChars")	? eval(getAttributeValue(inpElems[i],"chkAllowedChars")) 		: new Array();
        var KeyFlag	= getAttributeValue(inpElems[i],"chkOnKeyCheck") 	? new Boolean(getAttributeValue(inpElems[i],"chkOnKeyCheck")) 	: false;
        var Caption	= getAttributeValue(inpElems[i],"chkCaption")	? new String(getAttributeValue(inpElems[i],"chkCaption")) 	: new String('');
        var RegEx	= getAttributeValue(inpElems[i],"chkRegEx")	? new String(getAttributeValue(inpElems[i],"chkRegEx")) 	: new String('');
        var RegExMsg	= getAttributeValue(inpElems[i],"chkRegExMsg")	? new String(getAttributeValue(inpElems[i],"chkRegExMsg")) 	: new String('');
	var DefaultValue	= getAttributeValue(inpElems[i],"chkDefaultValue")	? new String(getAttributeValue(inpElems[i],"chkDefaultValue")) 	: new String('');
        var ErrContainer	= getAttributeValue(inpElems[i],"chkErrContainer") 	? new String(getAttributeValue(inpElems[i],"chkErrContainer"))			: new String('');
        var ExtraCheck	= getAttributeValue(inpElems[i],"chkXtraCheck") 	? new String(getAttributeValue(inpElems[i],"chkXtraCheck")) 	: null;
	var Type=inpElems[i].type;
		var SuggestRequest=getAttributeValue(inpElems[i],"chkSuggestRequest") 	? new String(getAttributeValue(inpElems[i],"chkSuggestRequest")) 	: null;
		var SuggestSelectSource=getAttributeValue(inpElems[i],"chkSuggestSelectSource")	? new String(getAttributeValue(inpElems[i],"chkSuggestSelectSource")): null;
		var SuggestChild=getAttributeValue(inpElems[i],"chkSuggestChild")	? new String(getAttributeValue(inpElems[i],"chkSuggestChild")): null;
		var InitLightenFlag	= getAttributeValue(inpElems[i],"chkLighten") 	? new Boolean(getAttributeValue(inpElems[i],"chkLighten")) 	: false;
		//alert(SuggestSelectSource);

        var FLD = this.addField(Name,Type,Required,MinL,MaxL,MinValue,MaxValue,RegEx,RegExMsg,TstCode,KeyFlag,Caption,DefaultValue,ErrContainer,ExtraCheck,inpElems[i],SuggestRequest,SuggestSelectSource,SuggestChild, CondRequired, CondRequiredCond);
		FLD.InitLightenFlag=InitLightenFlag;
	//dbWrite('Found ',Name, 'Errorcontainer:',ErrContainer,inpElems[i].type);
        if(!DefaultField && getAttributeValue(inpElems[i],"chkDefaultField")=="true")
          DefaultField	= FLD;
      }
    }
  }
  return DefaultField;
}

TCheckForm.prototype.Print = function(PrintList){
  this.CheckFocus=false;
  PrintList = VerifyList(PrintList);
  if (PrintList.length>0){
  	this.ErrorCount = Math.floor(PrintList.length/2);
  	this.WrongElements.length = this.ErrorCount;
  	for (j=0; j<this.ErrorCount ;j++) this.WrongElements[j]=this.CheckFields[PrintList[(j*2)]];
  	for (i=0; i<this.ErrorCount ;i++){
  	  	this.WrongElements[i].Errorlist.ClearErrorlist();
    		var DummyList = PrintList[(i*2+1)];
    		for (k in DummyList) {
    			if (isNaN(DummyList[k])){
    				this.WrongElements[i].Errorlist.AddError(-1,DummyList[k]);
    			}else{
    				this.WrongElements[i].Errorlist.AddError(DummyList[k],'');
    			}
    		}
  	}
  	this.ActuelError=-1;
  	TestIt=false;
  	this.Next();
  }
}

TCheckForm.prototype.Next = function(){
  if(!TestIt) this.ActuelError++;
  if(this.ActuelError<this.ErrorCount){

    var RefField=this.WrongElements[this.ActuelError];
  /*  if(TestIt){
      if(!RefField.FindError()){
//	RefField.lighten(false);
        this.ActuelError++;
        if (this.ActuelError<this.ErrorCount)
          RefField=this.WrongElements[this.ActuelError];
      }
    }*/
    
	
	if(this.ActuelError<this.ErrorCount){

      if(RefField.setFocus){
      	 this.PrintError(2,RefField);
	 this.Reminder=RefField;
	 dbWrite('ERROR#', this.ActuelError,' FOR',RefField.Name,RefField.Ref,RefField.Ref.type);

      	 if(RefField.Type!='radio'){
      	 	if(RefField.Ref && !RefField.Ref.disabled) RefField.Ref.focus();
      	 }else{
      	 	if(RefField.Ref[0] && !RefField.Ref[0].disabled) RefField.Ref[0].focus();
      	 }
      	 //this.CheckFocus=true;
      }
    }else{
      this.PrintError(3,RefField);
    }
  }else{
   // this.CheckFocus=true;
    this.PrintError(3,this.WrongElements[(this.ErrorCount-1)]);
  }
}


TCheckForm.prototype.Check = function(doSubmit, doCheck, printAll){
  var GetASubmit=true;
  
	if(doCheck){

	  this.FirstCheck=true;
	  this.CheckFocus=false;
	  this.ErrorCount=0;
	
	  if(this.Reminder && this.Reminder.FindError()){
	//  	dbWrite('CHECK2:');
			this.WrongElements[this.ErrorCount]=this.Reminder;
			this.ErrorCount++;
	  }else{
		for(var q in this.CheckFields){
			  if (this.CheckFields[q].FindError()){
		//	    dbWrite('CHECK FOUND ERROR:',q);
				this.WrongElements[this.ErrorCount]=this.CheckFields[q];
				this.ErrorCount++;
				//break;
			  }//else this.CheckFields[q].lighten(false);
		}
	  }
	  			
	  // alert(this.WrongElements);
	  if (this.ErrorCount>0) {
		TestIt=true;
		this.ActuelError=0;
		this.Next();
		if(printAll&&printAll!=''){
			for(var j = 1;j<this.ErrorCount;j++){
				this.ActuelError=j;
				this.Next();
			}
		}
		GetASubmit=false;
	  }
	  this.CheckFocus=true;
	  
	}

	if (GetASubmit && doSubmit) this.Submit(doSubmit);	//changed 01.10.
	return GetASubmit;  // <-- neu  01.10.
}

TCheckForm.prototype.PrintError = function(art,FieldRef,fehler){
var ErrCntr=document.getElementById(FieldRef.ErrCntr);
var ErrCntrContent=document.getElementById(FieldRef.ErrCntr+'Content');
if(ErrCntr && ErrCntrContent){
  var text="";
  if(art==0){
    if (fehler){
      text=FieldRef.Errorlist.Errorlist[1][(FieldRef.Errorlist.Errorlist[1].length-1)];
      setStatus(text);
    }else setStatus('');
  }
//  dbWrite("Printing error ART",art);
  if((art==1)||(art==2)||(art==4)){
	  		  			 //   alert('huhu');
    for (var i in FieldRef.Errorlist.Errorlist[1]){
       if (FieldRef.Errorlist.Errorlist[0][i]==-1) text+=FieldRef.Errorlist.Errorlist[1][i]+" ";
    }
	ErrCntrContent.innerHTML=text;
	FieldRef.lighten(true);
    self.setTimeout('switchStyleClass(document.getElementById("'+FieldRef.ErrCntr+'"), "errContainerVisible");',180);
	
	//ErrCntr.setAttribute("class", "errContainerVisible");

  }
  if(art==3){
     // ErrCntrContent.innerHTML='';
     self.setTimeout('switchStyleClass(document.getElementById("'+FieldRef.ErrCntr+'"), "errContainerHidden");',180);

    FieldRef.lighten(false);
    this.checkIt=0;
    if (FieldRef.Name!=pseudoForm) FieldRef.result=FieldRef.Ref.value;
  }
}
}


TCheckForm.prototype.Submit = function(doSubmit){
  for(var q in this.CheckFields){
    var ii=this.CheckFields[q];
    if(ii.Ref && (ii.Ref.type=="text" || ii.Ref.type=="textarea") && ii.Ref.value==ii.DefaultValue) ii.Ref.value="";
  }
  if(doSubmit) this.Ref.submit();
}


/******************************************************************************************
******************** used for Input-text-,-password and textarea-fields **************
******************************************************************************************/
function TCheckField(Name,Type,Required,MinL,MaxL,MinValue,MaxValue,RegEx,RegExMsg,TstCode,CheckFormRef,KeyFlag,Caption,DefaultValue,ErrCntr,ExtraCheck,Ref,SuggestRequest, SuggestSelectSource,SuggestChild,CondRequired,CondRequiredCond){
  this.Name = Name;
  this.Required=Required;
  this.DefaultRequired=Required;
  this.MinL=MinL;	// Zeichen
  this.MaxL=MaxL;
  this.MinValue=MinValue;	// Werte, zb. datum
  this.MaxValue=MaxValue;
  this.Type=Type;
  this.RegEx=RegEx;
  this.RegExMsg=RegExMsg;
  this.TstCode=TstCode;
  this.ErrCntr=ErrCntr;
  this.Caption=Caption;
  this.DefaultValue=DefaultValue;
  this.ExtraCheck=ExtraCheck || '0';
  this.SuggestRequest=SuggestRequest;
  //alert(SuggestRequest);
  this.SuggestSelectSource=SuggestSelectSource;
  //alert(SuggestSelectSource);
  this.SuggestChild=SuggestChild;
  this.CharCondition=0;
  this.Errorlist=new TErrorlist(this);
  this.CheckFormRef=CheckFormRef;	// Ref auf CheckFormClasse
  this.Ref=Ref || CheckFormRef.Ref[Name];	// Ref auf HTML-Name des HTML-Objektes in der Form
  this.KeyFlag=KeyFlag;
  this.FocusFlag=false;
  if ((is.ie)&&(is.os=="mac")) this.KeyFlag=false;
  this.result=this.getCurrentValue();
  this.activ=true;
  this.setFocus = true;
	this.aOnKeyPressFunctions = new Array();	// zus. Events-Functions, die aus der jew. KlassenMethode aufgerufen werden, z.B. "obj.CheckFormRef.win.Test()";
	this.aOnFocusFunctions = new Array();
	this.aOnBlurFunctions = new Array();
 this.CondRequired= eval(CondRequired);
 //alert(this.CondRequired);
 this.CondRequiredCond= CondRequiredCond;
}

TCheckField.prototype.start = function(){
  if((this.Ref.type=="text" || this.Ref.type=="textarea") && this.Ref.value=="" && this.DefaultValue!=""){
	this.Ref.value=this.DefaultValue;
	this.Ref.defaultValue=this.DefaultValue;
  }
  if (this.KeyFlag) this.AddHandler("onkeypress",this.onKP);
  this.AddHandler("onblur",this.ResetStatus);
  this.AddHandler("onfocus",this.MadeACheckLight);
  if(this.Type=='radio') this.AddHandler("onchange",this.ResetStatus);
  if(this.SuggestRequest){
	var suggesthandler;
	if(this.Type=="text"){
		suggesthandler="onkeyup"; 
		this.AddHandler("onchange", suggest.resetSelectSuggest);
	}
	else{this.Type="select";suggesthandler="onfocus";}
	if(suggesthandler&&suggesthandler!="")this.AddHandler(suggesthandler, this.onSuggest);
  }
  this.lighten(false);
}

TCheckField.prototype.onSuggest=function(obj,e){
  if(is.ie) e=window.event;
	var ch=e.which?e.which:e.keyCode?e.keyCode:0;
	if(ch!=40&&ch!=38&&ch!=13&&ch!=9){
		var source=obj.SuggestSelectSource?document.getElementById(obj.SuggestSelectSource):document.getElementById(obj.Ref.id);
		if(source.value!=''){	
			if(!(obj.Type=="select"&&obj.Ref.value!=Messages["PlsChoose"])){
				eval(obj.SuggestRequest+'(suggest.HandlerCallbackEvent(obj.Type),obj.Ref.id,"'+source.value+'");');			
			}
		}
	}
	else if(obj.SuggestRequest&&obj.Type=="text"){
		//alert(ch);
		var elem=document.getElementById(obj.Ref.id);
		//alert(obj.Ref.id);
		if(ch==40&&elem.parentNode.childNodes.length>1){
			suggest.switchActiveListElement(obj.Ref.id,"down");
			//alert(suggest.getActiveListElem(obj.Ref.id));			
		}
		if(ch==38&&elem.parentNode.childNodes.length>1){
			suggest.switchActiveListElement(obj.Ref.id,"up");
			
		}
		if(ch==9&&elem.parentNode.childNodes.length>1){/*taste tab*/
			//alert(suggest.getActiveListElem(obj.Ref.id));
			suggest.take(suggest.getActiveListElemA(obj.Ref.id),obj.Ref.id);		
		}
		if(ch==13&&elem.parentNode.childNodes.length>1){ /*taste return*/
			//alert(suggest.getActiveListElem(obj.Ref.id));
			suggest.take(suggest.getActiveListElemA(obj.Ref.id),obj.Ref.id);
			//window.event=null;
		}
	
	}
}

var suggest ={
	HandlerCallbackEvent:function(obj){
		if(obj){
			return obj=="text"?suggest.textCallbackEvent:obj=="select"?suggest.selectCallbackEvent:function(list,ctx){alert(list+ctx)};
		}
	},
	textCallbackEvent:function(list,objid){
		if(list&&list!=''){
			var obj=document.getElementById(objid);
				for(;obj.parentNode.childNodes.length>1;){			
					obj.parentNode.removeChild(obj.parentNode.lastChild);
				}
				var newBr=document.createElement("br");
				obj.parentNode.appendChild(newBr);
				var newUl=document.createElement("ul");
				newUl.setAttribute("tabindex","32767");
				newUl.id=objid+"SuggestListe";
				//newUl.addEventListener("onkeypress",suggest.handlekeypress,false);
				switchStyleClass(newUl,"SimilarLocators");
				for(var i=0;i<list.length;i++){
					var newLi=document.createElement("li");
					newLi.className="";
					var newA=document.createElement("a");
					newA.href="#";
					newA.target=""
					//newA.onclick="suggest.take('this,'"+objid+"');return false;");
					suggest.addHandler("onclick", suggest.take,objid, newA);
					newA.className="SimilarLocatorsColumn";
					var newAText=document.createTextNode(list[i]);
					
					newUl.appendChild(newLi);
					newLi.appendChild(newA);
					//newA.appendChild(newSpan);
					newA.appendChild(newAText);
				}
				obj.parentNode.appendChild(newUl);
				newUl.style.display="inline";
			}
	},
	addHandler: function(EventName,Handler,pointer,eventObj){
		eventObj[EventName] = function(){
			var res =Handler(eventObj,pointer);
			return res;
		}
	},

	take:function(listelem, objid){
		//alert(listelem.childNodes[0].nodeValue);
		if(listelem&&listelem!=""){
			//alert(listelem);
			var obj=document.getElementById(objid);
			obj.value=listelem.childNodes[0].nodeValue;
			var parentCell=listelem.parentNode.parentNode.parentNode;
			for(;parentCell.childNodes.length>1;){			
				parentCell.removeChild(parentCell.lastChild);
			}			
		//alert(obj);
			suggest.resetSelectSuggest(obj);
		}
		return false;
	},
	resetSelectSuggest:function(obj,e){
		var myObj = e?document.getElementById(obj.Ref.id):obj;
		if(myObj){
			if(getAttributeValue(myObj,"chkSuggestChild")){
				document.getElementById(getAttributeValue(myObj,"chkSuggestChild")).options[0]=new Option("Bitte auswählen","Bitte auswählen");
				document.getElementById(getAttributeValue(myObj,"chkSuggestChild")).selectedIndex=0;
				document.getElementById(getAttributeValue(myObj,"chkSuggestChild")).focus();
			}
		}
	},
	getActiveListElemA:function(objid){
		var obj=document.getElementById(objid);
		if(obj){
			if(obj.parentNode.childNodes.length>1){
				var liste=obj.parentNode.childNodes[2];
				for(var i=0;i<liste.childNodes.length;i++){
					//alert(liste.childNodes[i]);
					if(liste.childNodes[i].className=="SimilarLocatorsactive"){						
						return liste.childNodes[i].firstChild;
					}
				}
			}
		}
		return null;
	},
	selectCallbackEvent:function(list,objid){
		//alert(list);
		var obj=document.getElementById(objid);
		
		if(obj.options.length!=list.length+1){
			obj.options.length=0;
			if(list&&list!=''){
				var newOpt;
				if(list.length>0){
					newOpt= new Option(Messages["PlsChoose"],Messages["PlsChoose"]);//document.createElement("option");
					//newOpt.nodeValue="";
					//var newOptText =document.createTextNode("Bitte auswählen");					
					obj.options[obj.options.length]=newOpt;//newOpt.appendChild(newOptText);
					//obj.appendChild(newOpt);
				}
				for(var i=0;i<list.length;i++){
					newOpt= new Option(list[i],list[i]);//document.createElement("option");
					//newOpt.nodeValue=list[i];
					//newOptText =document.createTextNode(list[i]);					
					//newOpt.appendChild(newOptText);
					obj.options[obj.options.length]=newOpt;//obj.appendChild(newOpt);
				}
				obj.selectedIndex=0;
				//obj.value=obj.childNodes[0].childNodes[0].nodeValue;
			}	
		}
	},
	switchActiveListElement:function(objid,direction){
		if(objid&&objid!=""){
			var liste=getElementsByAttributeValue("id",objid+"SuggestListe","ul")[0];
			var direc;
			if(liste){
				if(direction&&direction=="down"){
					direc=1;			
				}
				else direc=liste.childNodes.length-1;
				var oldElem=-1;
				for(var i=0;i<liste.childNodes.length;i++){
					//alert(liste.childNodes[i]);
					if(liste.childNodes[i].className=="SimilarLocatorsactive"){						
						switchStyleClass(liste.childNodes[i],"");
						oldElem=i;
						i=liste.childNodes.length;
					}
				}				
				var newElem=(oldElem+direc)%liste.childNodes.length<0?(oldElem+direc)%liste.childNodes.length*-1:(oldElem+direc)%liste.childNodes.length;
				switchStyleClass(liste.childNodes[newElem],"SimilarLocatorsactive");
				if((newElem+1)*15>liste.scrollTop+90){
						liste.scrollTop=(newElem*15)-75;
				}else if(newElem*15<liste.scrollTop){
						liste.scrollTop=newElem*15;
				}			
//				alert(liste.childNodes[newElem].childNodes[0].childNodes[0].childNodes[0].nodeValue);
				document.getElementById(objid).value=liste.childNodes[newElem].childNodes[0].childNodes[0].nodeValue;
			}
		}
	}
}

TCheckField.prototype.AddHandler = function(EventName,Handler){
  var TCheck = this;
	if(TCheck.Type=='radio'){
	  for(var i=0;i<TCheck.Ref.length;i++){
	  	TCheck.Ref[i][EventName] = function(Ev){
	  		return Handler(TCheck,Ev);
  	  	}
  	  }
  	}else{
  		TCheck.Ref[EventName] = function(Ev){
    	  		return Handler(TCheck,Ev);
  	  	}
  	}
}

//benötigt global.js switchStyleClass(obj, className)
TCheckField.prototype.lighten = function(err){  
	//alert(this.Ref.style);
	if(is.dom)
    	if(err){
    		 switchStyleClass(this.Ref,'inputfieldError');
/*			 this.Ref.style.color=ErrorColor;
			 this.Ref.style.background="#F3D39A";
			 this.Ref.style.border ="1px solid #e07e00";			 */
	    }else{
    		 switchStyleClass(this.Ref,'inputfield');
    		/* this.Ref.style.color="#000";
			 this.Ref.style.background="#fff";
			 this.Ref.style.border ="1px solid #000";	*/
	    }
}

TCheckField.prototype.IsItEmail = function(){
	if(this.Ref.value.length>0){
		var email=this.Ref.value.split("@");
		if (email.length==2){
			if (email[0].match(/^([\w\-]+[\w\-\.])*[\w\-]+$/)==null){
				 this.Errorlist.AddError(-1,Messages["ValidEmail"]);
			}
			var host=new Array();
			host[0]=email[1].substring(0,email[1].lastIndexOf('.'));
			host[1]=email[1].substring(email[1].lastIndexOf('.')+1,email[1].length);
			if(host[0]=='' ||  host[0].length<2 || host[0].match(/^([\w\-]+[\w\-\.])*[\w\-]+$/)==null)
				this.Errorlist.AddError(-1,Messages["ValidEmailDomain"]);
			else
				if(host[1]=='' || host[1].match(/^([a-zA-Z]{2,5})$/)==null) this.Errorlist.AddError(-1,Messages["ValidEmailDomain"]);
		}else{
			if(email.length<2) var message=Messages["ValidEmail"]; else var message=Messages["ValidEmail"];
			this.Errorlist.AddError(-1,message);
		}
	}
}


function getDateFormat(v){
	var arrDate=v.match(DateRegEx);
	var res=null;
	var ErrMsg='';
	if(!arrDate)
		ErrMsg=Messages["WrongDate"];
	else{
		var d=parseInt(arrDate[1],10);
		var m=parseInt(arrDate[2],10);
		var y=parseInt(arrDate[3],10);
		if(y || y==0){
			if(y>=10 && y<=99) y+=1900;
			if(y<10) y+=2000;
		}
		var maxd=MonthDays[m];
		if(m==2){
			if(y%4==0 && (y%100!=0 || (y%400==0)))  maxd=29;
			else maxd=28;
		}
		if(!d || d<1 || d>maxd) ErrMsg=Messages["WrongDay"];
		if(!m || m<1 || m>12) ErrMsg=Messages["WrongMonth"];
		if(!y || y<1900) ErrMsg=Messages["WrongYear"];
		if(ErrMsg=='') res=new Date(y,m-1,d);
	}
	dbWrite('getDateFormat',v,arrDate,res,ErrMsg);
	return [res,ErrMsg];
}

function getDateString(date){
	var td=(date.getDate()<10)?'0':'';
	td+=date.getDate()+DateDelimiter;
	td+=(date.getMonth()+1<10)?'0':'';
	td+=(date.getMonth()+1)+DateDelimiter+date.getFullYear();
	return td;
}

TCheckField.prototype.checkDateFormat = function(){
	var tD=getDateFormat(this.Ref.value);
	if(!tD[0] && tD[1]!=''){
		this.Errorlist.AddError(-1,this.Caption+' '+tD[1]);
	}
	return tD[0];
}

TCheckField.prototype.IsItDate = function(){
	if(this.Ref.value.length>0){
		var date=this.checkDateFormat();
		if(date!=null){
			if(this.ExtraCheck=='Date'){
				var refer=null;
				if(this.MinValue && this.MinValue!=''){
					if(this.MinValue=='Now()') refer=new Date();else refer=getDateFormat(this.MinValue)[0];
					if(refer && date < refer) this.Errorlist.AddError(-1,Messages["MaxDate"].format(this.Caption,getDateString(refer)));
				}
				if(this.MaxValue && this.MaxValue!=''){
					if(this.MaxValue=='Now()') refer=new Date();else refer=getDateFormat(this.MaxValue)[0];
					if(refer && date > refer) this.Errorlist.AddError(-1,Messages["MinDate"].format(this.Caption,getDateString(refer)));
				}
			}
			if(this.ExtraCheck=='Age'){
				if(this.MinValue && this.MinValue!=''){
					var refer=new Date();
					var arefer=new Date(refer.getFullYear()-this.MinValue,refer.getMonth(),refer.getDate());
					if(date > arefer)
						 this.Errorlist.AddError(-1,Messages["WrongAge"].format(this.Caption,this.MinValue));
					else
					if(date > refer)
						 this.Errorlist.AddError(-1,this.Caption + Messages["NotAllowedDate"]);
				}
			}
			if(this.Errorlist.Errorlist[0].length==0) this.Ref.value=getDateString(date);
		}
	}
}

TCheckField.prototype.RegExCheck = function(){
	var aVal=new String(this.Ref.value);
	var test=null;
	if(aVal!='' && this.RegEx!=''){
		var d = this.RegEx.substring(0,1);
		if(d=="!"){
			d = this.RegEx.slice(1);
			test=eval('String(aVal).match('+d+')');
	        	if(test==null) this.Errorlist.AddError(-1,this.Caption+' '+this.RegExMsg+'.');
		}
		if(d=="/"){
			test=eval('String(aVal).match('+this.RegEx+')');
			if(test) this.Errorlist.AddError(-1,this.Caption+' '+this.RegExMsg+'.');
          	}
	}
}

TCheckField.prototype.TestNumber = function(){
  var digit=new Number(this.Ref.value);
  if (this.ExtraCheck.indexOf('Age')!=-1 && this.MinValue && this.MinValue!=''){ 
      if(digit > ((new Date().getFullYear())-this.MinValue))
         this.Errorlist.AddError(-1,Messages["WrongAge"].format(this.Caption,this.MinValue));
  }else{
     
     if (this.MaxValue!=null) if (digit>this.MaxValue) this.Errorlist.AddError(-1,this.Caption+Messages["TooHigh"]);
     if (this.MinValue!=null) if (digit<this.MinValue) this.Errorlist.AddError(-1,this.Caption+Messages["TooLow"]);
  }
}
TCheckField.prototype.CountChar = function(CharSet){
  var count=0;
  for (var j=0;j<this.Ref.value.length;j++) if (AllowedList[CharSet].indexOf(this.Ref.value.charAt(j))>-1) count++;
  return count;
}

TCheckField.prototype.ExtraInterpret = function(){
  if(this.ExtraCheck=='Date' || this.ExtraCheck=='Age') this.IsItDate();
  for (var i=0;i<this.ExtraCheck.length;i++){
    if (this.ExtraCheck.charAt(i)=='1') this.IsItEmail();
    if (this.ExtraCheck.charAt(i)=='2') this.TestNumber();
  }
}

TCheckField.prototype.getCurrentValue = function(){
  var Res='';
  if(this.Type=="radio"){
  	var allDis=true;
  	for(var i=0;i<this.Ref.length;i++){
  		if(!this.Ref[i].disabled){
  			allDis=false;
  			if(this.Ref[i].checked) Res=this.Ref[i].value;
  		}
  	}
  	if(allDis) Res=null;
  	return Res;
  }
  if(this.Ref.type=="text" || this.Ref.type=="textarea" || this.Ref.type=="password" || this.Ref.type=="file") Res=this.Ref.value;
  if(this.Ref.type=="select-one") Res=this.Ref.selectedIndex;
  if(this.Ref.type=="checkbox" && this.Ref.checked) Res=this.Ref.value;
  return Res
}

TCheckField.prototype.setCurrentValue = function(valu){
  if(this.Type=="radio"){
  	var allDis=true;
  	for(var i=0;i<this.Ref.length;i++){
  		if(!this.Ref[i].disabled){
  			allDis=false;
  			if(this.Ref[i].value==valu) this.Ref[i].checked=true;
  		}
  	}
  	if(allDis) Res=null;
  	return null;
  }
  if(this.Ref.type=="checkbox"){
  	if(valu==null || valu=='' || valu==false || valu=='false' || valu!=this.Ref.value) this.Ref.checked=false;
	else this.Ref.checked=true;
  }
  if(this.Ref.type=="text" || this.Ref.type=="textarea" || this.Ref.type=="password") this.Ref.value=valu;
  if(this.Ref.type=="select-one") if(valu==null || valu=='') this.Ref.selectedIndex=0;
  else this.Ref.selectedIndex=valu;
  return true;
}


TCheckField.prototype.FindError = function(){
  this.Errorlist.ClearErrorlist();
  var value=this.getCurrentValue();
   // alert(this.CondRequired);
/*+++++++++++++++++++++ Beginn Erweiterung für RequiredConditionals +++++++++++++++++++++++++++*/
if(this.CondRequired&&this.CondRequired!=""){
  
    var CondObj = document.getElementById(this.CondRequired) | document.getElementById(this.CondRequired.replaceAll("$","_"));
    if(CondObj){
        this.Required=CondObj.type=='checkbox'?CondObj.checked:CondObj.type=='radio'?CondObj.checked:CondObj.value==this.CondRequiredCond;
    }
}
/*+++++++++++++++++++++ Ende Erweiterung für RequiredConditionals +++++++++++++++++++++++++++*/
  if(this.Type=="radio"){
  	if(this.Required && value!=null && value==''){
  	  this.Errorlist.AddError(-1,this.Caption+Messages["ChooseRequired"]);
  	  return true;
  	}else return false;
  }else{
	  if(!this.Ref.disabled){
		if(this.Ref.type=="checkbox"){
			if (this.Required && this.Ref.checked!=true){
				this.Errorlist.AddError(-1,this.Caption+Messages["AcceptRequired"]);
				return true;
			}else return false;
		}


 		if(value!="" && value!=this.DefaultValue){
  			if(this.ExtraCheck && this.ExtraCheck!='0') this.ExtraInterpret();
  			if(this.RegEx!='') this.RegExCheck();
   			if(value.length<this.MinL){
				this.Errorlist.AddError(-1,this.Caption+Messages["TooShort"]);
    			}else if(value.length>this.MaxL) this.Errorlist.AddError(-1,Messages["TooLong"].format(this.Caption,this.MaxL));
			for(var i=0;i<value.length;i++) if(!this.CheckChar(value.charAt(i))){
				this.Errorlist.AddError(-1,Messages["NotValidChar"].format(this.Caption,value.charAt(i)));
				this.CheckFormRef.CheckFocus=false;
				break;
			}
    		}else if(this.Required) this.Errorlist.AddError(-1,this.Caption+Messages["FillRequired"]);
	//   dbWrite("TCheckField.FindError:",this.Name,this.Caption,'#',this.Errorlist.Errorlist[0].length);
 	 }
  }
//  dbWrite('FUNCTION FindError',this.Errorlist.Errorlist[0].length);
  if(this.Errorlist.Errorlist[0].length>0) return true; else return false;
}

TCheckField.prototype.Check = function(obj){
//  dbWrite("CHECK:",this.Name);
  var res=this.FindError();
  if (res){
    this.CheckFormRef.PrintError(1,this);
   // if (is.ns6) self.setTimeout('document.forms.'+this.CheckFormRef.FormName+'["'+this.Ref.name+'"].focus();',10); else
    this.Ref.focus();
  }else{
    this.CheckFormRef.Reminder=obj;
    this.CheckFormRef.PrintError(3,this);
  }
  return res;
}

//called onblur:
TCheckField.prototype.ResetStatus = function(obj,Erg){
if(!Erg) Erg=window.event;
//  dbWrite('Resetting Status for '+obj.Name+',REMINDER: '+obj.CheckFormRef.Reminder,obj.CheckFormRef.CheckFocus,Erg.type);
  self.setStatus('');
  if(obj.CheckFormRef.FirstCheck){
  	if(obj.FindError())
  		obj.CheckFormRef.PrintError(1,obj);
    	 else
    	 	obj.CheckFormRef.PrintError(3,obj);
		//self.setTimeout('CheckDomForms["'+obj.CheckFormRef.instName+'"].PrintError(3,CheckDomForms["'+obj.CheckFormRef.instName+'"].CheckFields["'+obj.Name+'"]);',180);
   }
  //Resetting Caption:
  if((obj.Ref.type=="text" || obj.Ref.type=="textarea")){
  	 if(obj.Ref.value==""){
  	   obj.Ref.value=obj.DefaultValue;
  	 }
  }
  return true;
//execObj(obj.aOnBlurFunctions);	// run user defined functions
	if(obj.SuggestRequest){suggest.take(suggest.getActiveListElemA(obj.Ref.Id), obj.Ref.Id)}
}

//called onfocus;
TCheckField.prototype.MadeACheck = function(obj,Erg){ //obj has the new focus
  var reminder=obj.CheckFormRef.Reminder;
  if (obj.activ){
    //dbWrite("MadeACheck 1: Reminder="+reminder.Name+"; Obj="+obj.Name);
    if (reminder && (reminder!=obj)&&(obj.CheckFormRef.CheckFocus)){
     //   dbWrite("MadeACheck 2: Reminder="+reminder.Name+"; Obj="+obj.Name);
        reminder.Check(obj); //no error in reminder, go ahead!
   //     dbWrite("MadeACheck 3a: Reminder="+reminder.Name+"; Obj="+obj.Name);
     }else
        if(!obj.CheckFormRef.Reminder) obj.CheckFormRef.Reminder=obj;
     if(reminder && (reminder.Ref.type=="text" || reminder.Ref.type=="textarea") && reminder.Ref.value==reminder.DefaultValue){
	reminder.Ref.value="";
//	dbWrite("MadeACheck 4b:"+obj.Name+' SET TO EMPTY, Reminder='+reminder.Name+"; Obj="+obj.Name);
       // eval(reminder.Ref.focus());
     }

  }else reminder.Ref.focus();
//  execObj(obj.aOnFocusFunctions);	// run user defined functions
}

//called onfocus;
TCheckField.prototype.MadeACheckLight = function(obj,Erg){ //obj has the new focus
//  if (obj.activ){
     var reminder=obj.CheckFormRef.Reminder;
     if(obj && obj.Ref && (obj.Ref.type=="text" || obj.Ref.type=="textarea") && obj.Ref.value==obj.DefaultValue){
	obj.Ref.value="";
     }
//  }
	var elems= getElementsByAttributeValue("chkSuggestRequest",null,"input");
	if(elems){
		for(var i=0;i<elems.length;i++){
			if(obj.id!=elems[i].id+"SuggestList")
				for(;elems[i].parentNode.childNodes.length>1;){			
					elems[i].parentNode.removeChild(elems[i].parentNode.lastChild);
				}
			
		}
	}
}

TCheckField.prototype.CheckChar = function(zeichen){
  if(this.TstCode.length>0){
    for(var i in this.TstCode){
        if(zeichen==this.TstCode[i]) return true; 
        else if(AllowedList[this.TstCode[i]] && AllowedList[this.TstCode[i]].indexOf(zeichen)>-1) return true;
    }
    return false;
  }else return true;
}

function getCharCode(Erg){
	var result=(Erg.charCode || Erg.charCode==0) 
		? Erg.charCode
		: Erg.keyCode 
			? Erg.keyCode 
			: Erg.which 
				? Erg.which 
				: void 0;
	return result;
}


TCheckField.prototype.onKP = function(obj,Erg){
  var res=false;
  if(is.ie) Erg=window.event;//obj.CheckFormRef.win.event;
  var ch = getCharCode(Erg);
  //alert(obj.Type);
  //alert(Erg);
  var zeichen=String.fromCharCode(ch);
  //dbWrite('keypressed',this,obj,Erg,Erg.cancelable,ch,zeichen,Erg.isChar, Erg.keyCode,  Erg.charCode  );
  if(ch!=8 && ch!=0 && ch!=13){
    if( ch!=180 && ch!=94) if(!obj.CheckChar(zeichen)) obj.Errorlist.AddError(obj.Ref.value.length,Messages["CharNotValid"].format(zeichen)); else res=true;
  }else res=true;
  obj.CheckFormRef.PrintError(0,obj,!res);
  execObj(obj.aOnKeyPressFunctions);  // run user defined functions
  if(!res){
  	if(window.event && window.event.preventDefault && !window.opera) window.event.preventDefault();
  	if(Erg.preventDefault) Erg.preventDefault();
  	dbWrite('preventing');
  }
  return res;
}

TCheckField.prototype.setActive = function(activ,store){
	  if(activ){
		if(!this.activ){
			this.Ref.disabled=false;
			if(store==true) this.setCurrentValue(this.result);
			this.setRequired(this.DefaultRequired);
		}
	  }else{

	  	if(this.activ){
			this.result=this.getCurrentValue();
			if(store==true) this.setCurrentValue("");
			this.Ref.disabled=true;
			this.setRequired(false);
		}
	  }
	this.activ=activ;
}

TCheckField.prototype.setActiveRadioG = function(activ,store){
  if(activ){
  	if(!this.activ){
  		if(store==true && this.result) this.result.checked;
  		for(var i=0;i<this.Ref.length;i++){
  			if(store==true && this.result) this.result.checked=true;
	 		this.Ref[i].disabled=false;
		}
		this.setRequired(this.DefaultRequired);
	}
  }else{
    	if(this.activ){
    		this.result=null;
    		for(var i=0;i<this.Ref.length;i++){
  			if(store==true && this.Ref[i].checked){
  				 this.result=this.Ref[i];
  				 this.Ref[i].checked=false;
  			}
	 		this.Ref[i].disabled=true;
		}
		this.setRequired(false);
	}
  }
  this.activ=activ;
}

TCheckField.prototype.setRequired = function(Req){
    this.Required=Req;
    setRequiredLabel(this.Ref.id,Req,this.CheckFormRef.Ref);
    if(this.Errorlist.Errorlist[0].length>0) this.ResetStatus(this);
}

onLoadObj[onLoadObj.length]="CreateCheckForms(self);";
