/* Script for Calendar $Revision: 1.6 $ */
// JS for the open calendar
dojo.declare("parent.lh.app.Calendar", null, {
	tblId:null,
	sheetArr:null,
	actDay:null,
	actMonth:null,
	inpType:null,
	top:null,
	left:null,
	closeId:null,
	appId:null,
	minDay:null,
	hasFocus:null,
	firstWidth:0,
	doc:null,
	cid:null,
	constructor: function(params){
		dojo.mixin(this, params);
		if(dojo.isIE) {
			this.doc = frames["cal"].document;
		} else {
			//this.doc = document.defaultView.dCal.ifra.contentDocument;
			//this.doc = eval("document.defaultView."+this.instName+".ifra.contentDocument");
			this.doc = document.getElementById("cal").contentDocument;
		}
		this.sheetArr = this.doc.getElementById(this.tblId).getElementsByTagName("a");
		dojo.addOnLoad(this, "_setAnkerDay");
		dojo.addOnLoad(this, "_regClickArea");
		if (!dojo.isKhtml) {
			dojo.addOnLoad(this, "_sendSize");
		}
		dojo.connect(this.doc, "onmousedown", this, "_setClicked");
		dojo.connect(this.doc.getElementById(this.closeId), "onclick", this, "_sendClose");
	},
	/* Fills array with all <a>-tags for days and registers onclick-handler for all tags in the array */
	_regClickArea: function() {
		for (var i=0;i<this.sheetArr.length;i++) {
			if ((this.actMonth == 1 || this.actMonth > 1) && (this.sheetArr[i].id.charAt(0) != "x")) {
				dojo.connect(this.sheetArr[i], "onclick", this, "_sendDate");
				dojo.connect(this.sheetArr[i], "onclick", this, "_sendClose");
			}
		}
	},
	/* Adds a css-class for all days in the past styling them inactive and unclickable 
	 * Adds css-class for currently selected day highlighting it
	 */
	_setAnkerDay: function() {
		if (this.actDay > 0 && this.actDay < 10) {
			this.actDay = '0' + this.actDay;
		}
		for (var i=0; i<this.sheetArr.length; i++) {
			if (this.actMonth < 1) {
				dojo.addClass(this.sheetArr[i], "cal-inactive");
			}
			else if (this.actMonth == 1) {
				if (this.actDay > 0 && this.sheetArr[i].firstChild.nodeValue == this.actDay) {
					dojo.addClass(this.sheetArr[i], "cal-active");
				}
			}
			else {
				if (this.sheetArr[i].firstChild.nodeValue == this.actDay) {
					dojo.addClass(this.sheetArr[i], "cal-active");
				}
			}
			if (this.sheetArr[i].id.charAt(0) == "x") {
				dojo.addClass(this.sheetArr[i], "cal-inactive");
			}
		}
	},
	/* Sends the selected date and the requested field to parent window */
	_sendDate: function(object) {
		var date = object.target.id.split(",");
		if (parent && parent.dojo) {
			parent.dojo.publish("receiveDate", [{"date":date, "tDateField":this.inpType, "cid":this.cid}]);
		}
	},
	/* Sends a close request to parent window for closing IFrame */
	_sendClose: function() {
		if (parent && parent.dojo) {
			parent.dojo.publish("closeCal");
		}
	},
	/* Sends size to parent window for resizing IFrame */
	_sendSize: function() {
		var width = 9;
		var height = 167;
		if (dojo.isIE) {
			height += 6;
		}
		this.top = 1 * this.top;
		this.left = 1 * this.left;
		var tblBody = this.doc.getElementById(this.tblId).getElementsByTagName("tbody");
		this.rowsArr = tblBody[0].getElementsByTagName("tr");
		this.colsArr = tblBody[0].getElementsByTagName("td");
		if (this.rowsArr.length > 5) {
			height += 21;
		}
		else if (this.rowsArr.length == 5 && this.colsArr.length == 28) {
			height -= 20;
		}
		for (var i=0; i<7; i++) {
			width += this.colsArr[i].offsetWidth;
		}
		if (parent && parent.dojo) {
			parent.dojo.publish("resizeCal", [{"height":height, "width":width, "top":this.top, "left":this.left, "tDateField":this.inpType}]);
		}
	},
	/* Sets clicked true if calendar is clicked and publishes it to parent */
	_setClicked: function() {
		this.clicked = true;
		if (parent && parent.dojo) {
			parent.dojo.publish("setCalClicked", [{"calClicked":this.clicked}]);
		}
	}
});
// JS for the integration of links/icons in pages
dojo.declare("lh.app.CalendarHandler", null, {
	calOutboundLnkId:null,
	calReturnLnkId:null,
	outboundDateId:null,
	returnDateId:null,
	inpType:null,
	calUrl:null,
	ifra:null,
	df:null,
	validFrom:null,
	validTo:null,
	bo1From:null,
	bo1To:null,
	bo2From:null,
	bo2To:null,
	tzd:null,
	scope:null,
	mode:null,
	lang:null,
	plusX:null,
	portal:null,
	retUsed:false,
	calClicked:false,
	selLists:null,
	linkId:null,
	linkFlag:false,
	globalClick: null,
	cId: null,
	isSpecial: false,
	pos: 1,

	constructor: function(params) {
		dojo.mixin(this, params);
		if (this.calOutboundLnkId != null) {
			dojo.connect(dojo.byId(this.calOutboundLnkId), "onclick", this, "_openCal");
		}
		if (this.calReturnLnkId != null){
			dojo.connect(dojo.byId(this.calReturnLnkId), "onclick", this, "_openCal");
		}
		if (this.outboundDateId != null){
			dojo.connect(dojo.byId(this.outboundDateId), "onfocus", this, "_openCal");
			dojo.connect(dojo.byId(this.outboundDateId), "onclick", this, "_openCal");
			dojo.connect(dojo.byId(this.outboundDateId), "onkeypress", this, "_checkForKey");
			this.cId = this.outboundDateId;
		}
		if (this.returnDateId != null){
			dojo.connect(dojo.byId(this.returnDateId), "onfocus", this, "_openCal");
			dojo.connect(dojo.byId(this.returnDateId), "onclick", this, "_openCal");
			dojo.connect(dojo.byId(this.returnDateId), "onkeypress", this, "_checkForKey");
			if(this.cId==null)this.cId = this.returnDateId;
		}
		//dojo.connect(document, "onclick", this, "_clickDocument");
		if(dojo.isIE < 7) {
			dojo.connect(window, "onscroll", this, "_scrollRepaint");
		}
		dojo.subscribe("receiveDate", this, "_receiveDate");
		dojo.subscribe("resizeCal", this, "_sizeCal");
		dojo.subscribe("closeCal", this, "_closeCal");
		dojo.subscribe("setCalClicked", this, "_setClicked");
	},
	/* Gets the id of the requester, calls the createIFrame and the setIFrameVars methods */
	_openCal: function(object) {
		var curTrgt = object.currentTarget.id;
		var trgt = object.target.id;
		if (curTrgt == this.calOutboundLnkId || curTrgt == this.calReturnLnkId) {
			this.linkFlag = true;
		}
		if ((this.linkId == this.calOutboundLnkId && trgt == this.outboundDateId 
			|| this.linkId == this.calReturnLnkId && trgt == this.returnDateId)) {
			
		} else if (this.ifra != null && this.linkId != curTrgt) {
			this._closeCal();
		}
		if (this.ifra == null) {
			this.calClicked = false;
			var linkId = trgt;
			if (linkId == "") {
				linkId = curTrgt;
			}
			this.linkId = linkId;
			var fieldId;
			if (linkId == this.calOutboundLnkId || linkId == this.outboundDateId) {
				fieldId = this.outboundDateId;
				this.inpType = "dep";
			}
			else {
				fieldId = this.returnDateId;
				this.inpType = "ret";
			}
			this._createIFrame();
			this._setIFrameVars(linkId, fieldId);
		}
		this.globalClick = dojo.connect(document, "onclick", this, "_clickDocument");
		dojo.stopEvent(object);
	},
	/* Creates the IFrame for the calendar */
	_createIFrame: function() {
		if(dojo.isIE) {
			document.getElementsByTagName("body")[0].insertAdjacentHTML("beforeEnd","<iframe id='cal' src='' longdesc='no content' scrolling='no' frameborder='0' style='display:none;'></iframe>");
			this.ifra = document.getElementsByTagName("body")[0].lastChild;
			this.ifra.src="javascript:false";
		}
		else {
			this.ifra = document.createElement('iframe');
			this.ifra.src="javascript:false";
			this.ifra.id="cal";
			document.getElementsByTagName('body')[0].appendChild(this.ifra);
		}
	},
	/* Sets the IFrame attributes for the calendar */
	_setIFrameVars: function(linkId, fieldId) {
		// first some validation date logic for special in LH
		var validFrom = "";
		if((typeof this.validFrom)=="object"){
			if(fieldId==this.outboundDateId){
				validFrom = this.validFrom.out;
			}
			if(fieldId==this.returnDateId){
				validFrom = this.validFrom.ret;
			}
		}else validFrom = this.validFrom;
		if((typeof this.validTo)=="object"){
			if(fieldId==this.outboundDateId){
				validTo = this.validTo.out;
			}
			if(fieldId==this.returnDateId){
				validTo = this.validTo.ret;
			}
		}else validTo = this.validTo;
		// position + dimensions stuff
		this.ifra.scrolling = "no";
		this.ifra.frameborder = "0";
		if (!dojo.isKhtml) {
			this.ifra.width = "0px";
		}
		if (dojo.isIE) {
			this.ifra.width = "177px";
			this.ifra.height = "167px";
		}
		else {
			this.ifra.height = "0px";
		}
		// default for position 1 -> bottom left
		var coords = dojo.coords(dojo.byId(fieldId),true);
		coords.y += dojo.byId(fieldId).offsetHeight;
		this.ifra.style.top = coords.y + "px";
		this.ifra.style.left = coords.x + "px";
		this.ifra.style.display = "block";
		this.ifra.style.position = "absolute";
		if((!dojo.isIE)||(dojo.isIE<8)){
			this.ifra.style.visibility = "hidden"; //to avoid jumping calendars if position is different than default.
		}
		// build url
		this.ifra.src = this.calUrl + this._getSepForQuery(this.calUrl)+ 
						"dateVal=" + dojo.byId(fieldId).value + 
						"&inpType=" + this.inpType + 
						"&df=" + this.df +
						"&validFrom=" + validFrom + 
						"&validTo=" + validTo + 
						"&bo1From=" + this.bo1From + 
						"&bo1To=" + this.bo1To + 
						"&bo2From=" + this.bo2From + 
						"&bo2To=" + this.bo2To + 
						"&tzd=" + this.tzd + 
						"&scope=" + this.scope + 
						"&mode=" + this.mode + 
						"&lang=" + this.lang + 
						"&plusX=" + this.plusX + 
						"&portal=" + this.portal + 
						"&top=" + coords.y +
						"&left=" + coords.x +
						"&cid=" + this.cId +"";
		if((this.isSpecial)&&(fieldId==this.returnDateId)){
			if(dojo.byId(this.outboundDateId).value!=""){
				this.ifra.src += "&startDateOverride="+dojo.byId(this.outboundDateId).value;
			}
		}
	},
	/* Writes the selected calendar date to the input field
	 * In case the requester was the outbound field and the return field is still unused, the return date is also updated
	 */
	_receiveDate: function(pMessage) {
		if(pMessage.cid==this.cId){
			if (pMessage.tDateField == "ret") {
				this.retUsed = true;
				dojo.byId(this.returnDateId).value = pMessage.date[0];
			} else {
				dojo.byId(this.outboundDateId).value = pMessage.date[0];
			}
			if (this.returnDateId != null){
				if (pMessage.tDateField == "dep" && pMessage.date.length>1 && this.retUsed == false) {
					dojo.byId(this.returnDateId).value = pMessage.date[1];
				}
			}
		}
	},
	/* Checks if an iframe exists, the pressed key is the tab key and if the calendar hasn't been clicked before
	 * If all is true, the calendar is closed
	 */
	_checkForKey: function(event) {
		//if (this.ifra != null && this.calClicked == false && event.keyCode == dojo.keys.TAB) {
		if (this.ifra != null && this.calClicked == false) {
			this._closeCal();
		}
		//dojo.stopEvent(event);
	},
	/* Checks where the document has been clicked 
	 * If the clicked area is not the label of the input field or the input field itself, the calendar is closed
	 */
	_clickDocument: function(object) {
		
		if ((this.linkId == this.calOutboundLnkId && object.target.id == this.outboundDateId 
			|| this.linkId == this.calReturnLnkId && object.target.id == this.returnDateId)) {
		}
		else if (this.ifra != null && object.target.id != this.linkId && this.calClicked == false && this.linkFlag == false) {
			if (object.target.tagName != "LABEL") {
				this._closeCal();
			} else if (dojo.byId(object.target.htmlFor).id != this.linkId) {
				this._closeCal();
			}
		}
		this.linkFlag = false;
		//dojo.stopEvent(object);
	},
	/* Sets width and height of the IFrame according to the values given by the calendar
	 * Calls the setPosition-Method and the toggleSelLists-Method 
	 * --> Has to be called here because IFrame needs to be ready placed before hiding
	 */
	_sizeCal: function(pMessage) {
		if(this.ifra != null){
			this.ifra.width = pMessage.width + "px";
			if (dojo.isIE) {
				pMessage.height -= 6;
				this.ifra.height = pMessage.height + "px";
			}
			else {
				this.ifra.height = pMessage.height + "px";
			}
			this._setPosition(pMessage.width,pMessage.height,pMessage.top, pMessage.left,pMessage.tDateField);
		}
	},
	/* Set calender at the configured place relative to input element */
	_setPosition: function(width,height,top,left,tDateField){
		var pos;
		if(tDateField=="dep"){
			fieldDim = dojo.marginBox(this.outboundDateId);
			if((typeof this.pos)=="object"){
				pos = this.pos.out;
			}else pos = this.pos;
		}else if(tDateField=="ret"){
			fieldDim = dojo.marginBox(this.returnDateId);	
			if((typeof this.pos)=="object"){
				pos = this.pos.ret;
			}else pos = this.pos;
		}else pos = this.pos;
		// default: this.pos = 1 -> bottom, left aligned
		// conting in clockwise direction:
		if(pos==2){
			// left, top aligned
			top -= fieldDim.h;
			left -= width;
		}else if(pos==3){
			// top, left aligned
			top = top - fieldDim.h - height;
		}else if(pos==4){
			// top, right aligned
			top -= (fieldDim.h + height);
			left -= (width - fieldDim.w);
		}else if(pos==5){
			// right, top alligned
			top -= fieldDim.h;
			left +=	fieldDim.w;	
		}else if(pos==6){
			// bottom, right aligned
			left -= (width - fieldDim.w);
		}
		this.ifra.style.left = left + "px";
		this.ifra.style.top = top + "px";
		if((!dojo.isIE)||(dojo.isIE<8)){
			this.ifra.style.visibility = "visible"; //to avoid jumping calendars if position is different than default.
		}
	},
	/* Closes the IFrame if it exists */
	_closeCal: function() {
		if (this.ifra != null) {
			document.getElementsByTagName('body')[0].removeChild(this.ifra);
			this.ifra = null;
		}
		//dojo.disconnect(this.globalClick);
	},
	/* Registers when the calendar is clicked and stores it in calClicked */
	_setClicked: function(pMessage) {
		this.calClicked = pMessage.calClicked;
	},
	/* Sets IFrame hidden and visible again in order to get it in front of select list after scrolling */
	_scrollRepaint: function() {		
		if (this.ifra != null) {
			this.ifra.style.visibility="hidden";
			this.ifra.style.visibility="visible";
		}
	},
	_getSepForQuery: function(url) {
		if (url.indexOf("?") != -1) {
			return "&";
		}
		else {
			return "?";
		}
	}
});