﻿/*JS-VERSION:1.74.2.0*/
/*ROSH: global variable if calendar displayed on screen*/
var onScreen = false;
var actualDate = new Date();
var limitDate = actualDate;
/*ROSH: 28.03.07*/
function runToggleDIVCalendar(hide, ele){
	/*re-enable input fields*/ 
	if($('ctl00_CT_ContentPlaceHoder_inbound').value=='' && $('ctl00_CT_ContentPlaceHoder_depart').value!='')
	{
	    $('ctl00_CT_ContentPlaceHoder_depart').value= ''
	}
	if($('ctl00_CT_ContentPlaceHoder_inbound').value!='' && $('ctl00_CT_ContentPlaceHoder_depart').value=='')
	{
	    $('ctl00_CT_ContentPlaceHoder_inbound').value= ''
	}
	
	$('ctl00_CT_ContentPlaceHoder_inbound').disabled = false;
	$('ctl00_CT_ContentPlaceHoder_depart').disabled = false;
	
	if(hide){
		if(onScreen){
			$('calendarwrapper').style.visibility = "visible";
			onScreen = false;
		}
		if($('calendarwrapper').style.display=='none'){
			//var pos = Position.cumulativeOffset($('ctl00_PlaceHolderMain_SwissSearchBox1'));
			$('calendarwrapper').style.top = '63px';
			$('calendarwrapper').style.left = '480px';
			$('calendarwrapper').style.position = 'absolute';
			/*$('calendarwrapper').show();*/
			//$('calendarwrapper').style.zIndex = "50"; 
			$('calendarwrapper').style.visibility = "visible";
			$('calendarwrapper').style.display='';
			//$('calendar').style.zIndex = "50"; 
			onScreen = true;
			// ele.disabled = true;
		setDate(cal1, $('ctl00_CT_ContentPlaceHoder_depart').value);
		
		setDate(cal2, $('ctl00_CT_ContentPlaceHoder_inbound').value);
		}
	}
	else
	{
		$('calendarwrapper').style.display = 'none';
		$('calendarwrapper').style.visibility = "hidden";
		onScreen = false;
	}

	/*switch to identify the correct ref. text html element this is
	needed that the calendar writes selected date into html text marup element.*/
	if(ele)
	{
		switch(ele.id)
		{
			case 'ctl00_CT_ContentPlaceHoder_inbound':
			case 'ctl00_CT_ContentPlaceHoder_depart':
//			     $('title_depart').innerHTML = cal1.title;
//			     $('title_inbound').innerHTML = cal2.title;
//			     $('titleImage_depart').src = cal1.titleImage;
//			     $('titleImage_inbound').src = cal2.titleImage;
//				cal1.updateRefTextHtml('ctl00_CT_ContentPlaceHoder_depart');
//				cal2.updateRefTextHtml('ctl00_CT_ContentPlaceHoder_depart');
				break;
			
		}
		
	}
}
function runToggleDIVCalendar_ResultList(hide, ele){
	/*re-enable input fields*/ 
	if($('ctl00_CT_ContentPlaceHoder_inbound').value=='' && $('ctl00_CT_ContentPlaceHoder_depart').value!='')
	{
	$('ctl00_CT_ContentPlaceHoder_depart').value= ''
	}
	if($('ctl00_CT_ContentPlaceHoder_inbound').value!='' && $('ctl00_CT_ContentPlaceHoder_depart').value=='')
	{
	$('ctl00_CT_ContentPlaceHoder_inbound').value= ''
	}
	
	$('ctl00_CT_ContentPlaceHoder_inbound').disabled = false;
	$('ctl00_CT_ContentPlaceHoder_depart').disabled = false;
	
	if(hide){
		if(onScreen){
			$('calendarwrapper').style.visibility = "visible";
			onScreen = false;
		}
		if($('calendarwrapper').style.display=='none'){
			//var pos = Position.cumulativeOffset($('ctl00_PlaceHolderMain_SwissSearchBox1'));
			$('calendarwrapper').style.top = '463px';
			$('calendarwrapper').style.left = '657px';
			$('calendarwrapper').style.position = 'absolute';
			/*$('calendarwrapper').show();*/
			//$('calendarwrapper').style.zIndex = "50"; 
			$('calendarwrapper').style.visibility = "visible";
			$('calendarwrapper').style.display='';
			//$('calendar').style.zIndex = "50"; 
			onScreen = true;
			// ele.disabled = true;
		setDate(cal1, $('ctl00_CT_ContentPlaceHoder_depart').value);
		
		setDate(cal2, $('ctl00_CT_ContentPlaceHoder_inbound').value);
		}
	}
	else
	{
		$('calendarwrapper').style.display = 'none';
		$('calendarwrapper').style.visibility = "hidden";
		onScreen = false;
	}

	/*switch to identify the correct ref. text html element this is
	needed that the calendar writes selected date into html text marup element.*/
	if(ele)
	{
		switch(ele.id)
		{
			case 'ctl00_CT_ContentPlaceHoder_inbound':
			case 'ctl00_CT_ContentPlaceHoder_depart':
//			     $('title_depart').innerHTML = cal1.title;
//			     $('title_inbound').innerHTML = cal2.title;
//			     $('titleImage_depart').src = cal1.titleImage;
//			     $('titleImage_inbound').src = cal2.titleImage;
//				cal1.updateRefTextHtml('ctl00_CT_ContentPlaceHoder_depart');
//				cal2.updateRefTextHtml('ctl00_CT_ContentPlaceHoder_depart');
				break;
			
		}
		
	}
}
function runToggleDIVCalendar_Landing(hide, ele){

	/*re-enable input fields*/ 
	
	if($('ctl00_CT_ContentPlaceHoder_landingSearch_inbound_Landing').value=='' && $('ctl00_CT_ContentPlaceHoder_landingSearch_depart_Landing').value!='')
	{
	    $('ctl00_CT_ContentPlaceHoder_landingSearch_depart_Landing').value= ''
	}
	if($('ctl00_CT_ContentPlaceHoder_landingSearch_inbound_Landing').value!='' && $('ctl00_CT_ContentPlaceHoder_landingSearch_depart_Landing').value=='')
	{
	    $('ctl00_CT_ContentPlaceHoder_landingSearch_inbound_Landing').value= ''
	}
	
	$('ctl00_CT_ContentPlaceHoder_landingSearch_inbound_Landing').disabled = false;
	$('ctl00_CT_ContentPlaceHoder_landingSearch_depart_Landing').disabled = false;
	
	if(hide){
		if(onScreen){
			$('calendarwrapper').style.visibility = "visible";
			onScreen = false;
		}
		if($('calendarwrapper').style.display=='none'){
			//var pos = Position.cumulativeOffset($('ctl00_PlaceHolderMain_SwissSearchBox1'));
			$('calendarwrapper').style.top = '288px';
			$('calendarwrapper').style.left = '456px';
			$('calendarwrapper').style.position = 'absolute';
			/*$('calendarwrapper').show();*/
			//$('calendarwrapper').style.zIndex = "50"; 
			$('calendarwrapper').style.visibility = "visible";
			$('calendarwrapper').style.display='';
			//$('calendar').style.zIndex = "50"; 
			onScreen = true;
			// ele.disabled = true;
		setDate(cal1, $('ctl00_CT_ContentPlaceHoder_landingSearch_depart_Landing').value);
		
		setDate(cal2, $('ctl00_CT_ContentPlaceHoder_landingSearch_inbound_Landing').value);
		}
	}
	else
	{
		$('calendarwrapper').style.display = 'none';
		$('calendarwrapper').style.visibility = "hidden";
		onScreen = false;
	}

	/*switch to identify the correct ref. text html element this is
	needed that the calendar writes selected date into html text marup element.*/
	if(ele)
	{
		switch(ele.id)
		{
			case 'ctl00_CT_ContentPlaceHoder_landingSearch_inbound_Landing':
			case 'ctl00_CT_ContentPlaceHoder_landingSearch_depart_Landing':
//			     $('title_depart').innerHTML = cal1.title;
//			     $('title_inbound').innerHTML = cal2.title;
//			     $('titleImage_depart').src = cal1.titleImage;
//			     $('titleImage_inbound').src = cal2.titleImage;
//				cal1.updateRefTextHtml('ctl00_CT_ContentPlaceHoder_depart');
//				cal2.updateRefTextHtml('ctl00_CT_ContentPlaceHoder_depart');
				break;
			
		}
		
	}
}

function setDate(cal, dateString, ele)
{

	var dateArr = getDayMonthArray(dateString);

	if(dateArr && dateArr.length)
	{
		var day = parseInt(dateArr[0]);
	    var month = parseInt(dateArr[1]);
			    
	    if(ele)
        {		
            if(cal.id == 'cal2' && (ele.id == 'checkout' || ele.id == 'checkin'))
            {
                if(dateString && dateString.length == 10 && month && day)
                {
                    var year =  dateString.substr(dateString.length-4, 4);
                    var newDate = new Date(year, month-1, day);
				
                    newDate.setDate(day+1);
                    
                    day = newDate.getDate();
                    month = newDate.getMonth()+1;
                }
            }
        }        
		cal.calendar(null, month, day, cal.id);
		cal.showSpecificCal('select_'+cal.id, month);
		cal.setSelected($(cal.id+'_possibleSlelecteDay_'+day), day);
	}
}

function getDayMonthArray(dateString)
{
	var dateValue = dateString.split('.');
	if (dateValue &&
		(dateValue.length) &&
		(parseInt(dateValue[0]) >= 1) &&
		(parseInt(dateValue[0]) <= 31) &&
		(parseInt(dateValue[1]) >= 1) &&
		(parseInt(dateValue[1]) <= 12))
	{
		return dateValue;
		
		
	}
	return null;
}

/*ROSH: 28.03.07 Calender Object
	@id - need to be unique name
	@containerId - container id is a predefined div, this forces to write content into div
	@title - header title
	@refTextHtml - reference to text markup field.
	@preSelected - an offset, e.g. 3 will premark the today+3
	@titleImage - title image
	@prevImage - navigation image previous
	@nextImage - navigation image next
	@refId - reference id of different calendar

	@=====================================================
	usage example:

	#define instance

	var cal1 = new SwissCalendar('cal1','divCalendarOutbound','Hinflug','depart',1,
									'/Style%20Library/images/booking/icon_outward.gif',
									'/Style%20Library/images/booking/button_arrowleft.gif',
									
									'/Style%20Library/images/booking/button_arrowright.gif',
									null);
	var cal2 = new SwissCalendar('cal2','divCalendarInbound','Rückflug','inbound',9,
									'/Style%20Library/images/booking/icon_return.gif',
									'/Style%20Library/images/booking/button_arrowleft.gif',
									'/Style%20Library/images/booking/
									.gif',
									'cal1');


	#render content into specific place
	<script>cal1.Print();</script>
	<script>cal2.Print();</script>
*/

SwissCalendar = function (id, containerId, title, refTextHtml, preSelected, titleImage, prevImage, nextImage, refId)
{
	/*calendar unique id*/
	this.id = id;
	/*calendar container unique id - thats the place were its render content*/
	this.containerId = containerId;
	/*title e.g. to / back*/
	this.title = title;
	
	/*refer text input type*/
	this.refTextHtml = refTextHtml;
	/*months select html element which rendered automatically*/
	this.currentSelected;
	/*current selected day within the calendar*/
	this.currentSelectElement;
	/*optional preselection*/
	if(!preSelected)
		preSelected = -1;
	this.preSelected = preSelected;
	/*calendars title text, e.g. Outbound / Inbound*/
	this.titleImage = titleImage;
	/*right click image*/
	this.prevImage = prevImage;
	this.prevImageDis = replace(prevImage, ".gif", "_disabled.gif");
	/*left click image*/
	this.nextImage = nextImage;
	this.nextImageDis = replace(nextImage, ".gif", "_disabled.gif");

	/* ref. / parent / controlling parameters */
	this.arrayParentControls = new Array();
	/*observer ref id.*/
	this.refId = refId;
	if(refId)
	{
		this.refObject = eval(refId);
		this.refObject.registerReferences(this.id);
	}
	/*current slected day*/
	this.day = 0;
	/*current slected month*/
	this.month = 0;
	if(!this.id)
	{
		alert('config problem with id!');
	}
	if(!this.containerId)
	{
		alert('config problem with containerId!');
	}
	if(!this.title)
	{
		alert('config problem with title!');
	}
	if(!this.refTextHtml)
	{
		alert('config problem with refTextHtml!');
	}
}

/*SwissCalendar prototype extensions*/
SwissCalendar.prototype = {    
	/*core funtion which renders calendar details*/
	Print : function()
	{
		/*calendar navigation back / forward - select html element to choose the month*/
		this.currentSelectElement = 'select_'+this.id;
		var buffer = '';

		//buffer += '<img id="titleImage_' + this.refTextHtml +'" src="'+this.titleImage+'" alt="'+this.title+'" /> <span class="data" id="title_' + this.refTextHtml + '">'+this.title+'</span>';

		if(this.id == 'cal1')
		{
			//buffer += '<span style="position:absolute;display:block;left:265px;top:0px;"><a href="#" onclick="javascript:runToggleDIVCalendar(false);return false;"><img src="PitsCalendar_Imgs/button_close.gif" /></a></span>';
		}
		buffer += '<div class="line months" style="text-align:center;">';
		if(this.id == 'cal1')
		{   buffer += '<font style="font-size:13px;"><b>Fr&uuml;heste Hinreise</b></font><br/> '; }
		else if (this.id == 'cal2')
		{   buffer+= '<hr><font style="font-size:13px;"><b>Sp&auml;teste R&uuml;ckreise</b></font> <br/>';  }
		buffer += '	<div style="padding-top:5px; text-align:center; width:100%; vertical-align:middle;"><span><a href="#" onclick=\''+this.id+'.showPrevCal("select_'+this.id+'");void(0);return false;\'>';
		buffer += '		<img border="0" class="arrowleft" id="'+this.id+'_arrowleft" src="'+this.prevImage+'" alt="" />';
		buffer += '	</a>&nbsp;&nbsp;&nbsp;</span>';		
	    buffer += '	<span><select id=\'select_'+this.id+'\' onchange=\''+this.id+'.onNavigationMonthChange(this);\'></select>';
		buffer += '	&nbsp;&nbsp;&nbsp;</span><span><a href="#" onclick=\''+this.id+'.showNextCal("select_'+this.id+'");void(0);return false;\'>';
		buffer += '		<img border="0" class="arrowright" id="'+this.id+'_arrowright"  src="'+this.nextImage+'" alt="" />';
		buffer += '	</a></span></div>';
		buffer += '</div>';	
		
		document.write(buffer);

		/* update the select html element */
		this.fillNavigationMonths('select_'+this.id);

		var requestDate = new Date();
		requestDate = new Date(requestDate.getFullYear(), requestDate.getMonth(), requestDate.getDate());

		/*set preselected dates*/
		if(this.preSelected>0)
		{		    
			requestDate = new Date(requestDate.getFullYear(), requestDate.getMonth(), requestDate.getDate() + this.preSelected);
			this.calendar(requestDate.getFullYear(), requestDate.getMonth(), requestDate.getDate(), this.id, null);
			//this.calendar(requestDate.getFullYear(), requestDate.getMonth()+1, requestDate.getDate(), this.id, null);
			var toSelect = this.id+'_possibleSlelecteDay_'+ requestDate.getDate();
			
			this.setSelected($(toSelect), requestDate.getDate());

			/* update the dropdown box with correct preselected month */
			//setDropDownValue( $('select_'+this.id), requestDate.getMonth());
			setDropDownValue( $('select_'+this.id), requestDate.getMonth()+1 );
			
		
		}
		else
		{
			/* no preselection */
			requestDate = new Date(requestDate.getFullYear(), requestDate.getMonth(), requestDate.getDate());
			//******************************Added by rejith for fixing the second calendar should be 1 month greated than first one initialy************************************
			if (this.id == 'cal2')
			{
			this.calendar(requestDate.getFullYear(), requestDate.getMonth()+2, requestDate.getDate(), this.id, null);
			}
			else
			{
			this.calendar(requestDate.getFullYear(), requestDate.getMonth()+1, requestDate.getDate(), this.id, null);
			}
			//***********************************************************************************
			var toSelect = this.id+'_possibleSlelecteDay_'+ requestDate.getDate();			
			this.setSelected($(toSelect), requestDate.getDate());
			/* update the dropdown box with correct preselected month */
			setDropDownValue( $('select_'+this.id), requestDate.getMonth());
		}

//		this.month = requestDate.getMonth()+1;        Commented by rejith 
//		this.day = requestDate.getDate();             Commented by rejith 
		this.initRefernceTextbox();

		/*bugfix:disabled button upon startup and backup link*/
		if(this.month <= (new Date().getMonth()+1))
		{
			$(this.id+'_arrowleft').src = this.prevImageDis;
			this.backupHref($(this.id+'_arrowleft').parentNode);
		}
	},
	
	backupHref : function(ele)
	{
		/*ensure element and that this runs just on a markup elements*/
		if(ele && ele.tagName == "A")
		{
			var href = ele.getAttribute("href");
			var onclick = ele.getAttribute("onclick");

			if(href && href != "" && href != null){
				ele.setAttribute('href_bak', href);
			}
			if(onclick != null && onclick != "void(0);"){
				ele.setAttribute('onclick_bak', onclick);
				ele.setAttribute('onclick', "void(0);");
			}
		}
	},
	restoreHref : function(ele)
	{
		/*ensure element and that this runs just on a markup elements*/
		if(ele && ele.tagName == "A")
		{
			if(ele.attributes['onclick_bak']!=null)
			{
				ele.setAttribute('onclick', ele.attributes['onclick_bak'].nodeValue);
			}

			if(ele.attributes['href_bak']!=null)
			{
				ele.setAttribute('href', ele.attributes['href_bak'].nodeValue);
			}
		}
	},
	/*register if other controls have a referneces - observer*/
	registerReferences : function(obj)
	{
		if(obj)
			this.arrayParentControls.push(obj);
	},
	/*handling selection of a new date*/
	updateDateSelection : function()
	{
		if(this.arrayParentControls.length >0)
		{
			/* update parent controls */
			for(var t=0;t<this.arrayParentControls.length;++t)
			{
				if(this.arrayParentControls[t])
				{
					var parentObject = eval(this.arrayParentControls[t]);
					if(parentObject)
					{
						parentObject.updateDateSelection();
					}
				}
			}
		}
		/*
		disable fields in reference object.
		e.g. cal1. has a ref. to cal2;
		*/
		if(this.refObject)
		{

			/*first restore all links and prepare them as possible seleactable day's*/
		    for(var x=1;x<=31;++x)
		    {
			    if($(this.id+'_possibleSlelecteDay_'+x))
			    {
				    this.enableDay(x);
			    }
				
		    }
			var textData = $(this.refTextHtml).value.split('.');
			

			var loc_parentDay = parseInt(this.refObject.day);
		
			if(this.refTextHtml == 'checkin' || this.refTextHtml == 'checkout')
			{
			    loc_parentDay = parseInt(this.refObject.day)+1;
			}
			
			var loc_parentMonth = parseInt(this.refObject.month);
			var loc_textDay,loc_textMonth;
			if(textData.length > 2)
			{
				loc_textDay = parseInt(textData[0]);
				loc_textMonth = parseInt(textData[1]);
			}

			/*mark first the selected day*/
			if($(this.id+'_possibleSlelecteDay_'+this.day))
			{	
				this.markDay( $(this.id+'_possibleSlelecteDay_'+this.day) );
			}

			if(loc_parentMonth == this.month && parseInt(this.day) <= loc_parentDay)
			{
			    this.unmarkDay( $(this.id+'_possibleSlelecteDay_'+this.day) );
			}

			var loc_tmpDay = parseInt(this.day) <= loc_parentDay ? loc_parentDay : parseInt(this.day);

            //Calc limit Date
			if(loc_parentMonth < (actualDate.getMonth()+1))
		    {
		        limitDate = new Date(actualDate.getFullYear()+2, loc_parentMonth-1, loc_parentDay+maxNumberOfBookableNightsKuoni);
		    }else
		    {
		        limitDate = new Date(actualDate.getFullYear(), loc_parentMonth-1,loc_parentDay+maxNumberOfBookableNightsKuoni);
		    }
					
			/*disble*/
			for(var d=1;d<=31;++d)
			{
				if($(this.id+'_possibleSlelecteDay_'+d))
				{					
					if(loc_parentMonth == this.month)
					{
						if(loc_parentDay >= d && (this.refTextHtml == 'checkin' || this.refTextHtml == 'checkout'))
						{
							/* if the day is not unmarked, a css problem appears.*/
							this.unmarkDay( $(this.id+'_possibleSlelecteDay_'+loc_tmpDay) );
							this.day = loc_parentDay;
							//Do not disable the first possible selectable day
							if(d != loc_parentDay)
							{
							    this.disableDay(d);
							}
						}else if(loc_parentDay > d)
						{
							/* if the day is not unmarked, a css problem appears.*/
							this.unmarkDay( $(this.id+'_possibleSlelecteDay_'+loc_tmpDay) );
							this.day = loc_parentDay;
							this.disableDay(d);
						}
						/*ensure previous selected fields are unmarked*/
						else if(this.refTextHtml == 'checkin' || this.refTextHtml == 'checkout')
						{
							this.unmarkDay( $(this.id+'_possibleSlelecteDay_'+(d-1)));
						}
						else
						{
							this.unmarkDay( $(this.id+'_possibleSlelecteDay_'+d) );
						}
						/*mark the new selected date, this.day get every loop updated */
					    if(d == loc_tmpDay)
					    {					    
					        this.markDay($(this.id+'_possibleSlelecteDay_'+loc_tmpDay));
							
						    this.printFormattedTextToReference(loc_tmpDay);
					    }else if(((d-1)== loc_tmpDay ) && (this.refTextHtml == 'checkin' || this.refTextHtml == 'checkout'))
					    {
					        this.markDay('-1');
							
						    this.printFormattedTextToReference((loc_tmpDay));
					    }
					}
				}
			} 
		}
	},
	disableDay : function(d)
	{
//        if($(this.id+'_possibleSlelecteDay_'+d))
//		{
//		    var href = $(this.id+'_possibleSlelecteDay_'+d).getAttribute("href");
//		    var onclick = $(this.id+'_possibleSlelecteDay_'+d).getAttribute("onclick");

//		    if(href && href != "" && href != null){
//			    $(this.id+'_possibleSlelecteDay_'+d).setAttribute('href_bak', href);
//		    }
//		    if(onclick != null){
//			    $(this.id+'_possibleSlelecteDay_'+d).setAttribute('onclick_bak', onclick);
//			    $(this.id+'_possibleSlelecteDay_'+d).setAttribute('onclick', "void(0);");
//		    }
//    		
//	
//		    $(this.id+'_possibleSlelecteDay_'+d).removeAttribute('href');
//		    $(this.id+'_possibleSlelecteDay_'+d).className='';
//		    $(this.id+'_possibleSlelecteDay_'+d).parentNode.className='';
//		    $(this.id+'_possibleSlelecteDay_'+d).style.textDecoration = 'none';
//		    $(this.id+'_possibleSlelecteDay_'+d).parentNode.className="";
//		    $(this.id+'_possibleSlelecteDay_'+d).parentNode.disable = 1;
//		}
	},
	enableDay : function(x)
	{
		this.unmarkDay($(this.id+'_possibleSlelecteDay_'+x));
		$(this.id+'_possibleSlelecteDay_'+x).parentNode.className = "possible";

		if($(this.id+'_possibleSlelecteDay_'+x).attributes['onclick_bak']!=null)
			$(this.id+'_possibleSlelecteDay_'+x).setAttribute('onclick', $(this.id+'_possibleSlelecteDay_'+x).attributes['onclick_bak'].nodeValue);

		if($(this.id+'_possibleSlelecteDay_'+x).attributes['href_bak']!=null)
			$(this.id+'_possibleSlelecteDay_'+x).setAttribute('href', $(this.id+'_possibleSlelecteDay_'+x).attributes['href_bak'].nodeValue);

		$(this.id+'_possibleSlelecteDay_'+x).style.textDecoration = 'underline';
		$(this.id+'_possibleSlelecteDay_'+x).style.fontStyle = '';
	},
	/*sets a specific day and set it marked*/
	setSelected : function(link, day)
	{
		if(this.arrayParentControls.length >0)
		{
			// update parent controls
			for(var t=0;t<this.arrayParentControls.length;++t)
			{
				if(this.arrayParentControls[t])
				{
					var parentObject = eval(this.arrayParentControls[t]);
					if(parentObject)
					{
						parentObject.unmarkAll();
					}
				}
			}
		}
		
		this.unmarkAll();

		if($(this.currentSelected)){
		
		
			$(this.currentSelected).parentNode.className="possible";
		}
		if(link)
		{
			this.currentSelected = link.id;
		}
		this.printFormattedTextToReference(day);
		if(link)
		{		
			this.markDay(link);
		}

		this.day = day;

		/*update links*/
		this.updateDateSelection();

		return false;
	},
	/*mark a specific day*/
	markDay : function(ele)
	{	    
		ele.parentNode.className="selected";
		ele.style.color="#fff";
		ele.style.textDecoration = 'none';
	},
	/*unmark a specific day*/
	unmarkDay : function(ele)
	{
		if(ele)
		{
			ele.parentNode.className="possible";
			ele.className="possible";
			ele.style.color="#656565";
			ele.style.textDecoration = 'underline';
		}
	},
	unmarkAll : function()
	{
		for(var x=1;x<=31;++x)
		{
			if($(this.id+'_possibleSlelecteDay_'+x))
			{
				this.unmarkDay($(this.id+'_possibleSlelecteDay_'+x));
			}
		}
	},
	
	/*Validate month if greater than 12*/
	ValidateMonth : function(intMonth){
		while (intMonth> 12)
		{
		    intMonth = intMonth - 12;
		}
		return intMonth;
	},
	
	/*prints the formatted text to ref. text box*/
	printFormattedTextToReference : function(day)
	{
		var requestDate = new Date();

		if(requestDate.getMonth()+1 <= this.month){
			requestDate = new	Date(requestDate.getFullYear(), this.month -1, 1);
		}
		else{
			requestDate = new	Date(requestDate.getFullYear()+1, this.month -1, 1);
		}
		
//		if($(this.refTextHtml).value=="start")
//		{
//		$(this.refTextHtml).value="1/1/1900";
//		}
//		if($(this.refTextHtml).value=="end")
//		{
//		$(this.refTextHtml).value="1/1/2000";
//		}

		if(this.refTextHtml && $(this.currentSelectElement))
			$(this.refTextHtml).value = this.printFormatter(day) + '.' + this.printFormatter(this.ValidateMonth(this.month)) + '.' + requestDate.getFullYear();
			
	},
	/*
	rosh: 30.05.2007: changes 1-9 to 01 - 09
	*/
	printFormatter : function (val)
	{
		if(val)
		{
			var tmp = parseInt(val);

			if(tmp >9)
				return tmp;
			else
				return '0'+tmp;
		}
	},
	/*init ref. textbox.*/
	initRefernceTextbox : function()
	{
		var day, month;
		var textData = $(this.refTextHtml).value.split('.');
		if(textData.length > 2)
		{
			day = parseInt(textData[0],10);
			month = parseInt(textData[1],10);
		}
		if(!isNaN(day) && !isNaN(month))
		{
//			if(this.month == parseInt(month))  Commented by rejith
//			{
				if(day == 0)
					day = this.day;

				this.setSelected($(this.id+'_possibleSlelecteDay_'+day), day);
				setDropDownValue( $('select_'+this.id), month );
					
//			}
		}
	},
	/*display month*/
	printMonth : function(ele, month){
		ele = $(ele);
		var requestDate = new Date();

		if(requestDate.getMonth()+1 <= month){
			requestDate = new	Date(requestDate.getFullYear(), month -1, 1);
		}
		else{
			requestDate = new	Date(requestDate.getFullYear()+1, month -1, 1);
		}
		
		this.calendar(requestDate.getFullYear(), requestDate.getMonth()+1, requestDate.getDate(), this.id, null);
	},
	/*
	toggles the images to navigate between months
	@dir = true[next] / false[previous]
	@val = true[enable] / false[disable]
	*/
	toggleMonthSelection : function(dir,val){
		/*next month*/
		if(dir){
			/*enable*/
			if(val){
				$(this.id+'_arrowright').src = this.nextImage;
				this.restoreHref($(this.id+'_arrowright').parentNode);
			}
			/*disable*/
			else{
				$(this.id+'_arrowright').src = this.nextImageDis;
				this.backupHref($(this.id+'_arrowright').parentNode);
			}
		}
		/*previous month*/
		else{
			/*enable*/
			if(val){
				$(this.id+'_arrowleft').src = this.prevImage;
				this.restoreHref($(this.id+'_arrowleft').parentNode);
			}
			/*disable*/
			else{
				$(this.id+'_arrowleft').src = this.prevImageDis;
				this.backupHref($(this.id+'_arrowleft').parentNode);
			}
		}
		var toSelect = this.id+'_possibleSlelecteDay_'+ new Date().getDate();
		this.setSelected($(toSelect), new Date().getDate());
	},
	/*
		enables / disable the buttons to navigate to next / previous month
		@ele - element
	*/
	activateMonthSelection : function()
	{
		/*validate just forward is enable*/
		//jan to dec is fine
		if(parseInt( $('select_'+this.id).selectedIndex ) == 0)
		{
		
			/*previous month, disable*/
			this.toggleMonthSelection(false, false);
			/*next month, enable*/
			this.toggleMonthSelection(true, true);
		}
		/*validate just backwards is enable*/
		
		else if(parseInt( $('select_'+this.id).selectedIndex ) == monthCount)
		{
		
		
			/*next month, disable*/
			this.toggleMonthSelection(true, false);
			/*previous month, enable*/
			this.toggleMonthSelection(false, true);
		}
		/*validate both directions are enabled*/
		else
		{
			/*next month, enable*/
			this.toggleMonthSelection(true, true);
			/*previous month, enable*/
			this.toggleMonthSelection(false, true);
		}
	},
	/*randomize month, so it works as a circle*/
	checkMonthStep : function(val)
	{
		/*if(val == 0)
		{
			val = 12;
		}
		else if ( val == 13)
		{
			val = 1;
		}*/
		return val;
	},
	/*display previous month*/
	showPrevCal : function(ele)
	{
		debugger;
		setDropDownValue($(ele), this.checkMonthStep( parseInt($(ele).options[$(ele).selectedIndex].value) - 1));
			
				
			
		this.onNavigationMonthChange($(ele));
	},
	/*display next month*/
	showNextCal : function(ele)
	{
		setDropDownValue($(ele), this.checkMonthStep( parseInt($(ele).options[$(ele).selectedIndex].value) + 1));
			
		
			
		this.onNavigationMonthChange($(ele));
	},
	/*display specific month*/
	showSpecificCal : function(ele, month)
	{
		setDropDownValue($(ele), month);
			
	    this.onNavigationMonthChange($(ele));
	},
	/*ROSH: 29.03.07: event handler for day overview within month*/
	onNavigationMonthChange : function(ele)
	{
		var setMark = false;

		if(this.month == $(ele).options[$(ele).selectedIndex].value)
		{
			setMark = true;
		}

		this.printMonth(ele, $(ele).options[$(ele).selectedIndex].value);
		this.month = $(ele).options[$(ele).selectedIndex].value;
		
		if(this.arrayParentControls.length >0)
		{
			// update parent controls
			for(var t=0;t<this.arrayParentControls.length;++t)
			{
				if(this.arrayParentControls[t])
				{
					var parentObject = eval(this.arrayParentControls[t]);
					if(parentObject)
					{
						parentObject.showSpecificCal('select_'+parentObject.id, this.month);
					}
				}
			}
		}

		/*BUGFIX:ROSH:28.05.07: ensure the correct arrows are enabled / disabled*/
		this.activateMonthSelection();

		if(setMark)
		{		
			this.markDay( $(this.id+'_possibleSlelecteDay_'+this.day) );
		}

		/* add here the date dif fix of 340 day's - everything which is behind this date, has to be deactivated*/
		if(this.refTextHtml == 'checkin' || this.refTextHtml == 'checkout')
		{
		    //Deactiveate all Days after a specified period
		    if(this.refTextHtml == 'checkout')
		    {
                if(actualDate != limitDate)
                {
		            if((limitDate.getMonth()+1) >= (actualDate.getMonth()+1))
		            {
                        if(Number(this.month) > (limitDate.getMonth()+1))
                        {
                            try
                            {
                                for(var i=1;i<32;++i)
                                {
                                    this.disableDay( i );
                                }
                            }catch(e)
                           {
                           }      
                        }else if(Number(this.month) < (limitDate.getMonth()+1))
                        {
                           if(Number(this.month) < actualDate.getMonth()+1)
                           {
                                try
                                {
                                    for(var i=1;i<32;++i)
                                    {
                                        this.disableDay( i );
                                    }
                                }catch(e)
                               {
                               } 
                           }
                        }else
                        {
                            try
                            {
                                for(var i=limitDate.getDate()+1;i<32;++i)
                                {
                                    this.disableDay( i );
                                }
                           }catch(e)
                           {
                           }  
                        }
		            }
		            else
		            {
                        if(Number(this.month) < limitDate.getMonth()+1)
                        {
                            //do not deactivate anything
                        }
                        else if(Number(this.month) == limitDate.getMonth()+1)
                        {
                            try
                            {
                                for(var i=limitDate.getDate()+1;i<32;++i)
                                {
                                    this.disableDay( i );
                                }
                            }catch(e)
                           {
                           }  
                        }
                        else if(actualDate.getMonth()+1 <= Number(this.month))
		                {
		                    //do not deactivate anything
		                }
                        else if(Number(this.month) > limitDate.getMonth()+1)
                        {
                            try
                            {
                                for(var i=1;i<32;++i)
                                {
                                    this.disableDay( i );
                                }
                            }catch(e)
                           {
                           }  
                        }else
                        {
                            //unhandled case
                        }
		            }                 
                }
                //Error
                else
                {
                    //Error: could not calculate limitDate!
                }
            }
		    
            //Last possible Month
	        if($(ele).selectedIndex == (maxCheckInNumberOfMonthKuoni-1))
	        {
	            var calcDate = new Date();
                var starttime = new Date(calcDate.getFullYear(),calcDate.getMonth(),calcDate.getDate());
                
                for(var i=starttime.getDate();i<32;++i)
                {
                    this.disableDay( i );
                }
            //Anything else after the  last possible month
            }else if($(ele).selectedIndex > (maxCheckInNumberOfMonthKuoni-1))
            {	           
               try{
                    for(var i=1;i<32;++i)
                    {
                        this.disableDay( i );
                    }
               }catch(e)
               {
               }      
            }    
		}
		
		else if($(ele).selectedIndex == 11)
		{
		    var calcDate = new Date();
			var starttime = new Date(calcDate.getFullYear(),calcDate.getMonth(),calcDate.getDate());
			var dateSpan = 339;
			starttime = new Date(starttime.valueOf()+(dateSpan*86400000));
			for(var i=starttime.getDate();i<32;++i)
			{
				this.disableDay( i );
			}
		}		
	},
	/*ROSH: 29.03.07: fill up next 12 months to select markup html element*/
	fillNavigationMonths : function(ele)
	{
		var today = new Date();
		today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
        var twoDiggitYear = today.getFullYear().toString();
		var maxMonth = monthCount;
		var currMonthSRS = today.getMonth()+1;
	     for(var i=0;i<=maxMonth;++i){
		     //Edited on 26.05.10
		    $(ele).options[i] = new Option(calendarData.short_month[today.getMonth()] + ' ' + twoDiggitYear.substring(2,4),currMonthSRS);
			currMonthSRS++;
		    today = new Date(today.getFullYear(), today.getMonth()+1, 1);
			twoDiggitYear = today.getFullYear().toString();
			
	    }
	},
	/*prints the calendar into container*/
	calendar : function(year, month, day, ContainerId)
	{	
	
		this.month = month;
		

		/* If no parameter is passed use the current date. */
		var calDate = new Date();	
		var tmpToday = calDate.getDate();
		var tmpMonth = calDate.getMonth();
		var tmpYear = calDate.getFullYear();
		var calYear = (year == null) ? calDate.getFullYear() : year;
		var calMte = (month == null) ? calDate.getMonth() : month - 1;
		while(calMte < 0)
		{
				calMte += 12;
				calYear--;
		}
		while(calMte >= 12)
		{
			calMte -= 12;
			calYear++;
		}

		var calDay = (day == null) ? 0 : day;
		calDate = new Date(calYear, calMte, 1);
		NextMonth = new Date(calYear, calMte + 1, 1);

		var firstDayOfMonth = new Date(calDate.getFullYear(),calDate.getMonth(),1);
		var weekDaysArray = new Array(6,0,1,2,3,4,5);

		// WeekStart = calDate.getDay();
		WeekStart = weekDaysArray[firstDayOfMonth.getDay()];

		/* Get the number of days in current month */
		MonthDays = Math.round((NextMonth.getTime() - calDate.getTime()) / 86400000) + 1;

		/* Check whether the Container Id is null */
		if(ContainerId != null)
		{		
			ContainerId = ContainerId;
			Container = document.getElementById(ContainerId);
			/* If an element doesnot exists with the given ContainerId then create it */
		//return;
			if(!Container)
				document.write('<div  id="' + ContainerId + '">&nbsp;</div>');
				
		}
		else
		{
			// Loop until a unique id is obtained for the container
			do
			{
				ContainerId = 'tblCalendar' + Math.round(Math.random() * 1000);
			}
			while(document.getElementById(ContainerId));
			// create an element with the new id
			document.write('<div id="' + ContainerId + '">&nbsp;</div>');
		}
		Container = document.getElementById(ContainerId);
		HTML = '<table>';
		// Title bar
		// HTML += '<tr><td class="Nav"><a href="javascript:void(0)" onMouseDown="Calendar(' + calYear + ', ' + calMte + ', ' + calDay+', \''+ContainerId+'\');">&laquo;</a></td><td colspan="5" class="Title">' + monthNames[calMte] + ' ' + calYear + '</td><td class="Nav"><a href="javascript:void(0)" onMouseDown="Calendar(' + calYear + ', ' + (calMte + 2) + ', ' + calDay+', \''+ContainerId+'\');">&raquo;</a></td></tr>';

		// Week Names
		HTML += '<tr>';
		HTML += '	<th>'+calendarData.short_days[1]+'</th>';
		HTML += '	<th>'+calendarData.short_days[2]+'</th>';
		HTML += '	<th>'+calendarData.short_days[3]+'</th>';
		HTML += '	<th>'+calendarData.short_days[4]+'</th>';
		HTML += '	<th>'+calendarData.short_days[5]+'</th>';
		HTML += '	<th>'+calendarData.short_days[6]+'</th>';
		HTML += '	<th>'+calendarData.short_days[0]+'</th>';
		HTML += '</tr>';
		HTML += '<tr>';

		// Fill the previous month days with space
		for(DayCounter = 0; DayCounter < WeekStart; DayCounter++)
		{
			HTML += '<td>&nbsp;</td>';
		}

		// Populate current month
		for(DayCounter = 1; DayCounter < MonthDays; DayCounter++)
		{
			if((DayCounter + WeekStart) % 7 == 1)
				HTML += '<tr>';

			/*non-optional days*/
			if( tmpMonth == calMte && (DayCounter < tmpToday)&& (tmpYear == calYear))
			{
				HTML += '<td class="" style="	color: #656565;font-weight: bold;">' + DayCounter + '</td>';
			}
			/*selected day*/
			else if(DayCounter == calDay)
			{
				HTML += '<td class="possible"><a href="#" id="'+this.id+'_possibleSlelecteDay_'+DayCounter+'" onclick="'+this.id+'.setSelected(this, \''+DayCounter+'\');return false;">' + DayCounter + '</a></td>';
			}
			/*optional days*/
			else
			{
				HTML += '<td class="possible"><a href="#" id="'+this.id+'_possibleSlelecteDay_'+DayCounter+'" onclick="'+this.id+'.setSelected(this, \''+DayCounter+'\');return false;">' + DayCounter + '</a></td>';
			}

			if((DayCounter + WeekStart) % 7 == 0)
				HTML += '</tr>';
		}

		// Fill the next month days with space
		for(j = (42 - (MonthDays + WeekStart)), DayCounter = 0; DayCounter <= j; DayCounter++)
		{
			HTML += '<td>&nbsp;</td>';
			if((j - DayCounter) % 7 == 0) HTML += '</tr>';
		}
		HTML += '</table>';
		
		//alert(HTML);
		//document.getElementById("divNote").style.top=posy + "px";
            //document.getElementById("divNote").style.left=posx  + "px"; 
            
//        Container.position = "relative";
//		Container.style.left="1000px";
//		Container.style.top="500px";
		Container.innerHTML = HTML;
		

		// Returns Id of the element containing the calendar
		return ContainerId;
	},
	updateRefTextHtml : function(elementName)
	{
		this.refTextHtml = elementName;
	}
	
} /*end class*/
