var currentLayer = "";

function ibsLayer(Name) {
	this.Name = Name;
	this.isActiv = false;
	this.width = 0;
	this.write = function (Code) {
		if (document.all) document.all[this.Name].innerHTML = Code;
		else {
			document[this.Name].document.open();
			document[this.Name].document.write(Code);
			document[this.Name].document.close();
		}
	}
	this.setActiv = function (isActiv) {
		this.isActiv = isActiv;
	}
	this.setVisible = function (isVisible) {
		if (document.all) {
			if (isVisible && this.isActiv) document.all[this.Name].style.visibility = 'visible';
			else if(!isVisible && !this.isActiv) document.all[this.Name].style.visibility = 'hidden';
		}
		else {
			if (isVisible && this.isActiv) document.getElementById(this.Name).style.visibility = 'visible';
			else if(!isVisible && !this.isActiv)  document.getElementById(this.Name).style.visibility = 'hidden';

//			if (isVisible && this.isActiv) document[this.Name].visibility = 'show';
//			else if(!isVisible && !this.isActiv) document[this.Name].visibility = 'hide';
		}

/***		
		if (isVisible) {
			if (currentLayer.Name && currentLayer != this) currentLayer.setVisible(false);
			currentLayer = this;
		}
***/

	}
	this.setPosition = function (event) {
		if (document.all) {
//			document.all[this.Name].style.top = event.clientY;
//			document.all[this.Name].style.left = event.clientX;
			document.all[this.Name].style.position = "absolute";
			document.all[this.Name].style.top = document.body.scrollTop + window.event.y;
			document.all[this.Name].style.left = window.event.x - this.width;
		}
		else {
//			document.getElementById(this.Name).style.position = 'relative';
//			document.getElementById(this.Name).style.border = '1px solid grey';
 			document.getElementById(this.Name).style.width = this.width + 10;
 			document.getElementById(this.Name).style.top = event.pageY;
 			document.getElementById(this.Name).style.left = event.pageX - this.width;
// 			document[this.Name].top = window.event.pageY;
//			document[this.Name].left = window.event.pageX - this.width;
		}
	}
}

function ibsEventManager() {
	EventHandlerList = new Array();
	
	this.handleEvent = function(ibsEvent) {
		FuncName = EventHandlerList[ibsEvent.name];
		EvalString = FuncName + "(ibsEvent);";
		eval(EvalString);
	}

	this.addEventHandler = function(EventName, FuncName) {
		EventHandlerList[EventName] = FuncName;
	}
}

function ibsEvent(name, listkey) {
	this.name = name;
	this.listkey = listkey;
	this.Link = "";
	this.FormaName = "";
	
	this.onSubmit = function () {
	}
}

function ibsFormManager() {
	this.ElementList = new Array();
	this.EventList = new Array();
	this.addElement = function(ibsElement) {
		if (!this.ElementList[ibsElement.FormName]) {
			this.ElementList[ibsElement.FormName] = new Array();
		}
		this.ElementList[ibsElement.FormName][ibsElement.ElementName] = ibsElement;
	}
	this.addEvent = function(ibsEvent) {
		this.EventList[ibsEvent.name] = ibsEvent;
	}
	this.handleEvent = function(ibsEvent) {
		document.forms[ibsEvent.FormName].action = ibsEvent.Link;
		
		var hlp = this.ElementList[ibsEvent.FormName];
		var error = 0;
		for (var ElementName in hlp) {
  			objElement = hlp[ElementName];
			error += objElement.onSubmit();
		}
		if (error == 0) {
			ibsEvent.onSubmit();
			document.forms[ibsEvent.FormName].submit();
		}
	}
	this.getElement = function(FormName, ElementName) {
		return this.ElementList[FormName][ElementName];
	}
}

function ibsElement(FormName, ElementName, ElementType) {
	this.FormName = FormName;
	this.ElementName = ElementName;
	this.ElementType = ElementType;
	this.CrtlName = "crtl_" + FormName + "_" + ElementName;
	this.ErrorCodes = new Array();
	
	this.setErrorCode = function (ErrorCode, ErrorText) {
		this.ErrorCodes[ErrorCode] = ErrorText;
	}
	this.getValue = function () {
		return document.forms[this.FormName].elements[this.ElementName].value;
	}
	this.setValue = function (val) {
		document.forms[this.FormName].elements[this.ElementName].value = val;
	}
	this.setFocus = function (){
		document.forms[this.FormName].elements[this.ElementName].focus();
	}
	this.setSelect = function (){
		document.forms[this.FormName].elements[this.ElementName].select();
	}
	this.setError = function (ErrorText){
		alert(ErrorText);			
		if(!document.all) this.setFocus();
		else return false;
	}
	this.setReturnEvent = function (ibsEvent){
		this.ReturnEvent = ibsEvent;
	}
	this.onBlur = function () {
	}
	this.onFocus = function () {
		this.setSelect();
	}
	this.onReturn = function (event) {
		if(document.all) {t = event.keyCode;}
 		else {t = event.which;}
		if (t == 13 && this.ReturnEvent) {
			FormManager.handleEvent(this.ReturnEvent);
		}
	}
	this.onChange = function () {
	}
	this.onKeydown = function (event) {
		if(document.all) {
			this.onReturn(event);
			valOK = this.validate(event.keyCode);
		}
		else {
			this.ibsObject.onReturn(event);
			valOK = this.ibsObject.validate(event.which);
		}
		return valOK;
	}
	this.validate = function (keyCode) {
		return true;
	}
	this.onSubmit = function () {
		return 0;
	}
	this.setEventHandler = function () {
		if(!document.all) {
			document.forms[this.FormName].elements[this.ElementName].ibsObject = this;
			document.forms[this.FormName].elements[this.ElementName].onkeydown = this.onKeydown;			
		}
	}
}

function ibsCharacter(FormName, ElementName) {
	this.base = ibsElement
	this.base(FormName, ElementName, 'character');
}

function ibsPassword(FormName, ElementName) {
	this.base = ibsElement
	this.base(FormName, ElementName, 'password');
}

function ibsOption(FormName, ElementName) {
	this.base = ibsElement
	this.base(FormName, ElementName, 'option');
}

function ibsSelect(FormName, ElementName) {
	this.base = ibsElement
	this.base(FormName, ElementName, 'select');
	this.setSelect = function (){
		selIndex = document.forms[this.FormName].elements[this.ElementName].selectedIndex;
		if (selIndex == -1) document.forms[this.FormName].elements[this.ElementName].selectedIndex = 0;
	}
	this.getValue = function () {
		selIndex = document.forms[this.FormName].elements[this.ElementName].selectedIndex;
		return document.forms[this.FormName].elements[this.ElementName].options[selIndex].value;
	}
}

function ibsDate(FormName, ElementName, LayerName) {
	this.base = ibsElement
	this.base(FormName, ElementName, 'date');
	this.isActive = false;

	this.CalendarLayer = new ibsLayer(LayerName);
			
	this.validate = function (keyCode) {
		var ACC = new Array(); // AllowedCharCodes
		ACC[190] = true;  // '.'
		ACC[8] = true;  // BackSpace-Taste
		ACC[37] = true;  // Pfeil-Links
		ACC[39] = true;  // Pfeil-Rechts
		ACC[9] = true;  // Tab
		ACC[27] = true;  // esc
		ACC[46] = true;  // entfernen

 		regexp = "[0-9]";
		a=String.fromCharCode(keyCode);
		if (ACC[keyCode] || a.match(regexp)) return true;
		else return false;
	}
	this.onBlur = function () {
		var curval = this.getValue();
		if(curval.length==0) return true;

		Tag = '01';
		Monat = '01';
		Jahr = '2001';

		d=curval.split('.');

		var error = 0;
		if (d.length > 3) error = 1;
		if (d.length == 3) {
			if (d[0].length > 2 || d[1].length > 2 || d[2].length > 4)  error = 1;		
			Tag = getTag(d[0]);
			Monat = getMonat(d[1]);
			Jahr = getJahr(d[2]);
		}
		if (d.length == 2) {
			if (d[0].length > 2 || d[1].length > 4)  error = 1;
			if (d[1].length < 2) {
				Tag = getTag(d[0]);
				Monat = getMonat(d[1]);
				Jahr = '2000';
			}
			else {
				Tag = '01';
				Monat = getMonat(d[0]);
				Jahr = getJahr(d[1]);
			}	
		}
		if (d.length == 1) {
			if (d[0].length > 4)  error = 1;
			else {
				Tag = '01';
				Monat = '01';
				Jahr = getJahr(d[0]);
			}
		}

		if (error == 0) {
			this.setValue(Tag + "." + Monat + "." + Jahr);
			return true;
		}
		else {
			alert(this.ErrorCodes[error]);
			this.setFocus();
			return false;
		}
	}
	this.onSubmit = function () {
		var curval = this.getValue();
		var d=curval.split('.');
		var Tag = d[0];
		var Monat = d[1];
		var Jahr = d[2];
		var error = 0;

		if((Monat==4 ||Monat==6 || Monat==9 || Monat==11) && (Tag > 30)) error = 2;
		if(Monat==2) {
			if((Jahr%4==0) && (Jahr%400==0)) maxTag = 29;
			else maxTag = 28;
			
			if (Tag > maxTag) error = 2;
		}
		if (error) {
			alert(this.ErrorCodes[error]);
			this.setFocus();
		}
		return error;
	}
	this.closeCalendar = function () {
		this.CalendarLayer.setActiv(false);
		this.CalendarLayer.setVisible(false);
		this.isActive = false;
	}
	this.selectDate = function (Jahr, Monat, Tag) {
		Tag = getTag(String(Tag));
		Monat = getMonat(String(Monat));
		Jahr = getJahr(String(Jahr));		
		this.setValue(Tag + "." + Monat + "." + Jahr);
		
		this.closeCalendar();
	}
	this.setPosition = function (event) {
		this.CalendarLayer.setPosition(event);
	} 
	this.setActiv = function (isActiv) {
		this.CalendarLayer.setActiv(isActiv);
	} 
	this.setVisible = function (isVisible) {
		this.CalendarLayer.setVisible(isVisible);
	} 
	this.openCalendar = function () {
		hlp = this.getValue();
		if (hlp) {
			d = hlp.split(".");
			var Monat = Number(d[1]);
			var Jahr = Number(d[2]);
		}
		else {
			var jetzt = new Date();
			var Monat = jetzt.getMonth() + 1;
			var Jahr = jetzt.getFullYear();
		}
	
//		this.setSelect();
		this.showCalendar(Jahr, Monat);
	}
	this.showCalendar = function (Jahr, Monat) {
		Monatsname = new Array ("Januar","Februar","M&auml;rz","April","Mai","Juni","Juli",
			"August","September","Oktober","November","Dezember");
		Tag = new Array ("Mo","Di","Mi","Do","Fr","Sa","So");

		var jetzt = new Date();
		var DieserMonat = jetzt.getMonth() + 1;
		var DiesesJahr = jetzt.getFullYear();
		if(DiesesJahr < 999) DiesesJahr+=1900;
		var DieserTag = jetzt.getDate();

		hlp = this.getValue();
		d = hlp.split(".");
		var SelTag = Number(d[0]);
		var SelMonat = Number(d[1]);
		var SelJahr = Number(d[2]);

		var Zeit = new Date(Jahr,Monat-1,1);
		var Start = Zeit.getDay();
		if(Start > 0) Start--;
		else Start = 6;
		var Stop = 31;
		if(Monat==4 ||Monat==6 || Monat==9 || Monat==11 ) --Stop;
		if(Monat==2) {
			Stop = Stop - 3;
			if(Jahr%4==0) Stop++;
			if(Jahr%100==0) Stop--;
			if(Jahr%400==0) Stop++;
		}

		nextJahr = Jahr;
		prevJahr = Jahr;
		nextMonat = Monat + 1;
		prevMonat = Monat - 1;
		if (Monat==12) {
			nextMonat = 1;
			nextJahr = Jahr + 1;
		}
		else if (Monat==1) {
			prevMonat = 12;
			prevJahr = Jahr - 1;
		}
				
		var Tageszahl = 1;
		var retCode = "";
		retCode += "<table border=1 cellpadding=1 cellspacing=1 bgcolor=#FFFFFF>";
		retCode += "<tr>";
		retCode += "<td align=center colspan=7 valign=middle>";
		retCode += "<a href=javascript:" + this.CrtlName + ".showCalendar(" + prevJahr + "," + prevMonat + "); >&lt;&lt;</a>&nbsp;";
		retCode += Monatsname[Monat-1] + ", " + Jahr;
		retCode += "&nbsp;<a href=javascript:" + this.CrtlName + ".showCalendar(" + nextJahr + "," + nextMonat + "); >&gt;&gt;</a>";
		retCode += "</td></tr>";
		retCode += "<tr>";
		for(var i=0;i<=6;i++) {
			retCode += "<td>" + Tag[i] + "</td>";
		}
		retCode += "</tr>";
					
		for(var i=0;i<=5;i++) {
			retCode += "<tr>";
			for(var j=0;j<=6;j++) {
				if((i==0)&&(j < Start))	retCode += "<td>&nbsp;</td>";
				else {
					if(Tageszahl > Stop) {
						retCode += "<td>&nbsp;</td>";
						i=6;
					}
					else {
						TZ_Str = "<a href=javascript:" + this.CrtlName + ".selectDate(" + Jahr + "," + Monat + "," + Tageszahl + "); >";
						TZ_Str += Tageszahl + "</a>";
						if((Jahr==DiesesJahr)&&(Monat==DieserMonat)&&(Tageszahl==DieserTag))
							retCode += "<td bgcolor=red>" + TZ_Str + "</td>";
						else 
							if((Jahr==SelJahr)&&(Monat==SelMonat)&&(Tageszahl==SelTag))
								retCode += "<td bgcolor=gray>" + TZ_Str + "</td>";
							else
								retCode += "<td>" + TZ_Str + "</td>";
					}
					Tageszahl++;
				}
			}
			retCode += "</tr>";
		}
		retCode += "</table>";

		this.CalendarLayer.write(retCode);
	}
}

EventManager = new ibsEventManager();
FormManager = new ibsFormManager();

///////////////////////////////////////////////

function getJahr(d) {
	if (d.length == 0) Jahr = '2000';
	if (d.length == 1) Jahr = '200' + d;
	if (d.length == 2) {
		if (d<20) Jahr = '20' + d;
		else  Jahr = '19' + d;
	}
	if (d.length == 3) Jahr = '0' + d;
	if (d.length == 4) Jahr = d;
	return Jahr;	
}

function getMonat(d) {
	if (d.length == 0) Monat = '01';
	if (d.length == 1) Monat = '0' + d;
	if (d.length == 2) Monat = d;

	if (Monat <= '00') Monat = '01';
	if (Monat > '12') Monat = '12';

	return Monat;
}

function getTag(d) {
	if (d.length == 0) Tag = '01';
	if (d.length == 1) Tag = '0' + d;
	if (d.length == 2) Tag = d;
	
	if (Tag <= '00') Tag = '01';
	if (Tag > '31') Tag = '31';
	
	return Tag;
}

//////

function noAction() {
	return;
}

activTagOldColor = "";
activTagID = "";
function changeTagColor(TagID) {
	if (document.all) {
		if (document.all[activTagID]) {
			document.all[activTagID].style.backgroundColor = activTagOldColor;
		}
		activTagOldColor = document.all[TagID].style.backgroundColor;
		activTagID = TagID;
		document.all[TagID].style.backgroundColor = 'red';
	}
	else {
		if (document[activTagID]) {
			document[activTagID].backgroundColor = activTagOldColor;
		}
		activTagOldColor = document[TagID].backgroundColor;
		activTagID = TagID;
		document[TagID].backgroundColor = 'red';
	}
}

///////////////
///////////////

function ibsPopup (URL) {
	this.width = 300;
	this.height = 300;
	this.posX = 100;
	this.posY = 100;
	this.URL = URL;
	this.Target = 'POPUP';
	this.open = function() {
		Parameter = 'width=' + this.width + ',height=' + this.height + ',left=' + this.posX + ',top=' + this.posY;
		this.window = window.open(this.URL,this.Target,Parameter);
	}
	this.close = function() {
		this.window.close();
	}
}
