/*
Copyright Richard Bailey @ Enzyme

Do not use without express permission of Enzyme

v1.00
*/


//--------------Sandeep has added these function --------------
function updateCal(val,type) 
{
	 m = document.frmflightSearch.tbSelMonth
	 y = document.frmflightSearch.tbSelYear
	 
	 if (type == "calSelectedMonthFrom")
	 {
	 	m.selectedIndex = val;
	 } 
	 else if (type == "calSelectedYearFrom") 
	 {
	 	y.selectedIndex = val;
	 } 
	 else if (type == "calSelectedMonthTo") 
	 {
	 	m.selectedIndex = val;
	 } 
	 else if (type == "calSelectedYearTo") 
	 {
	 	y.selectedIndex = val;
	 } 
	 else if (type == "calSelectedDayTo") 
	 {
	 	//objPrevElement
	 } 
	 else if (type == "calSelectedDayFrom") 
	 {
	 	//objPrevElement
	 }
	 
	 fUpdateCal(y.options[y.selectedIndex].value, m.options[m.selectedIndex].value)

	setReturn()
}

function setReturn(){
	//update form to add 6 days to return flight

	f = document.frmflightSearch
	if(f.calSelectedDayTo){
		if(f.calSelectedDayTo.selectedIndex == 0 && f.calSelectedMonthTo.selectedIndex == 0 && f.calSelectedYearTo.selectedIndex == 0){

			if(f.calSelectedDayFrom.selectedIndex + 6 < f.calSelectedDayTo.options.length){
				f.calSelectedDayTo.selectedIndex = f.calSelectedDayFrom.selectedIndex + 6
				f.calSelectedMonthTo.selectedIndex = f.calSelectedMonthFrom.selectedIndex
				f.calSelectedYearTo.selectedIndex = f.calSelectedYearFrom.selectedIndex
			} else {
				f.calSelectedDayTo.selectedIndex = (f.calSelectedDayFrom.selectedIndex + 6) % f.calSelectedDayFrom.options.length
				if(f.calSelectedMonthFrom.selectedIndex + 1 < f.calSelectedMonthTo.options.length){
					f.calSelectedMonthTo.selectedIndex = f.calSelectedMonthFrom.selectedIndex + 1
					f.calSelectedYearTo.selectedIndex  = f.calSelectedYearFrom.selectedIndex
				} else {
					f.calSelectedMonthTo.selectedIndex = (f.calSelectedMonthFrom.selectedIndex + 1) % f.calSelectedMonthFrom.options.length
					if(f.calSelectedYearFrom.selectedIndex + 1 < f.calSelectedYearTo.options.length){
						f.calSelectedYearTo.selectedIndex  = f.calSelectedYearFrom.selectedIndex + 1
					} else {
						f.calSelectedYearTo.selectedIndex  = f.calSelectedYearFrom.selectedIndex
					}
				}
			}
		}
	}
}

function fUpdateCal(iYear, iMonth) 
{ 
	alert(iYear + ' / ' + iMonth)
	myMonth = fBuildCal(iYear, iMonth);
	
	i = 0
	for (w = 1; w < 7; w++) 
	{
		for (d = 0; d < 7; d++) 
		{
			if(document.getElementById)
			{ 
				objDateText = document.getElementById("calDateText" + i)
			} 
			else if(document.all)
			{
				objDateText = document.all["calDateText" + i]
			} 
			else 
			{
				objDateText = null
			}
			
			if (!isNaN(myMonth[w][d])) 
			{	
				//this old code relates to the when we were getting a collection but isn't netscape compatible
				//objDateText[((7*w)+d)-7].innerHTML = myMonth[w][d];
				//objDateText[((7*w)+d)-7].style.cursor = 'hand'
				//objDateText[((7*w)+d)-7].parentElement.style.backgroundColor = "#A9F3AB"
				
				objDateText.innerHTML = myMonth[w][d];
				objDateText.style.cursor = 'hand'
				if(objDateText.parentNode)
				{
					objDateText.parentNode.style.backgroundColor = "#A9F3AB"
				} 
				else 
				{
					objDateText.parentElement.style.backgroundColor = "#A9F3AB"
				}
			} 
			else 
			{
				objDateText.innerHTML = "&nbsp;";
				objDateText.style.cursor = ''
				if(objDateText.parentNode)
				{
					objDateText.parentNode.style.backgroundColor = "#24D429"
				} 
				else 
				{
					objDateText.parentElement.style.backgroundColor = "#24D429"
				}
			}
		i++;
		}
	}

	// change bgcolor of selected day 
	if(iYear == prevYear && iMonth == prevMonth)
	{
		objPrevElement.style.backgroundColor = "#ed9022";
	}
}

function fBuildCal(iYear, iMonth) 
{
	var aMonth = new Array();

	aMonth[0] = new Array(7);
	aMonth[1] = new Array(7);
	aMonth[2] = new Array(7);
	aMonth[3] = new Array(7);
	aMonth[4] = new Array(7);
	aMonth[5] = new Array(7);
	aMonth[6] = new Array(7);

	var dCalDate = new Date(iYear, iMonth-1, 1);
	var iDayOfFirst = dCalDate.getDay();
	var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
	var iVarDate = 1;
	var i, d, w;

	aMonth[0][0] = "Su";
	aMonth[0][1] = "Mo";
	aMonth[0][2] = "Tu";
	aMonth[0][3] = "We";
	aMonth[0][4] = "Th";
	aMonth[0][5] = "Fr";
	aMonth[0][6] = "Sa";

	for (d = iDayOfFirst; d < 7; d++) 
	{
		aMonth[1][d] = iVarDate;
		iVarDate++;
	}
	for (w = 2; w < 7; w++) 
	{
		for (d = 0; d < 7; d++) 
		{
			if (iVarDate <= iDaysInMonth) 
			{
				aMonth[w][d] = iVarDate;
				iVarDate++;
			}
	   	}
	}
	return aMonth;
}

	
//--------------End Here --------------

function Calendar(id){

	var d = new Date

	this.date = d.getDate()
	this.month = d.getMonth() + 1
	this.year = d.getFullYear()

	this.title = ""
	this.Id = id

	this.previousMonthImageSrc = null
	this.nextMonthImageSrc = null	

	this.useDayImages = false
	this.weekday = ["Su","Mo","Tu","We","Th","Fr","Sa"]

	this.object = null

	this.isVisible = false
	
	this.monthArray = null
	
	this.closeHTML = "[close]"
	this.closePosition = "tr"
	
	this.hideAllSelect = true
	
	this.ignoreFirstOption = false
}



Calendar.prototype.write = CalendarWrite
Calendar.prototype.initialise = CalendarInitialise
Calendar.prototype.isDate = CalendarIsDate
Calendar.prototype.synchronise = CalendarSynchronise
Calendar.prototype.formSynchronise = CalendarFormSynchronise
Calendar.prototype.draw = CalendarDraw
Calendar.prototype.update = CalendarUpdate
Calendar.prototype.moveMonth = CalendarMoveMonth
Calendar.prototype.show = CalendarShow
Calendar.prototype.hide = CalendarHide
Calendar.prototype.switchVisibility = CalendarSwitchVisibility
Calendar.prototype.selectDay = CalendarSelectDay
Calendar.prototype.hideSelect = CalendarHideSelect
Calendar.prototype.showSelect = CalendarShowSelect

//Calendar.prototype.title = ""

function CalendarWrite(){

	var closeLink = '<a href="#" onclick="' + this.Id + '.hide();return false;" alt="Close Calendar" class="CalendarCloseLink">' + this.closeHTML + '</a>'

	with(document){
		write('<div id="'+ this.Id +'Calendar">')
		write('<form name="'+ this.Id +'CalendarForm">')
		write('<table border="0" cellpadding="0" cellspacing="0" class="CalendarTable">')
			if(this.title!=""){
				write('<tr class="CalendarTitleRow">')
					write('<td colspan="4" class="CalendarTitleCell">')
					write('<span class="CalendarTitleText">' + this.title + '</span>')
					if(this.closePosition == "tr"){
						write('<span class="CalendarClose">' + closeLink + '</span>')
					}
					write('</td>')
				write('</tr>')
			}
			write('<tr class="CalendarDateNavigationRow">')
				write('<td class="CalendarDateNavigationPreviousCell">')
					write('<a href="#" onclick="' + this.Id + '.moveMonth(parseInt(document.forms[\'' + this.Id + 'CalendarForm\'].CalendarMonth.options[document.forms[\'' + this.Id + 'CalendarForm\'].CalendarMonth.selectedIndex].value) - 1, parseInt(document.forms[\'' + this.Id + 'CalendarForm\'].CalendarYear.options[document.forms[\'' + this.Id + 'CalendarForm\'].CalendarYear.selectedIndex].value), -1);return false;" alt="Move back a Month">')
					if(this.previousMonthImageSrc!=null){
						write('<img src="'+this.previousMonthImageSrc+'" border="0">')
					} else {
						write('&nbsp;&lt;&nbsp;')
					}
					
					write('</a>')
				write('</td>')
				write('<td class="CalendarDateNavigationMonthCell">')
					write('<select name="CalendarMonth" class="CalendarMonthSelect" onchange="' + this.Id + '.update(this.options[this.selectedIndex].value,this.form.CalendarYear.options[this.form.CalendarYear.selectedIndex].value)">')
						
					write('</select>')
				write('</td>')
				write('<td class="CalendarDateNavigationYearCell">')
					write('<select name="CalendarYear" class="CalendarYearSelect" onchange="' + this.Id + '.update(this.form.CalendarMonth.options[this.form.CalendarMonth.selectedIndex].value,this.options[this.selectedIndex].value)">')
						
						
						
					write('</select>')
				write('</td>')
				write('<td class="CalendarDateNavigationNextCell">')
					write('<a href="#" onclick="' + this.Id + '.moveMonth(parseInt(document.forms[\'' + this.Id + 'CalendarForm\'].CalendarMonth.options[document.forms[\'' + this.Id + 'CalendarForm\'].CalendarMonth.selectedIndex].value) + 1, parseInt(document.forms[\'' + this.Id + 'CalendarForm\'].CalendarYear.options[document.forms[\'' + this.Id + 'CalendarForm\'].CalendarYear.selectedIndex].value), 1);return false;" alt="Move forward a Month">')
					if(this.nextMonthImageSrc!=null){
						write('<img src="'+this.nextMonthImageSrc+'" border="0">')
					} else {
						write('&nbsp;&gt;&nbsp;')
					}
					
					write('</a>')
				write('</td>')
			write('</tr>')

			write('<tr>')
				write('<td colspan="4" class="CalendarDaysContainerCell" align="center">')
				
				this.draw()

				write('</td>')
			write('</tr>')
			if(this.closePosition == "b"){
				write('<tr>')
					write('<td colspan="4" height="24" align="center">')
						write('<span class="CalendarClose">' + closeLink + '</span>')
					write('</td>')
				write('</tr>')
			}
		write('</table>')
		write('</form>')
		write('</div>')
	}
}

function CalendarInitialise(d,m,y){
	
	if(d && m && y){
	
		this.dateFormElement = d
		this.monthFormElement = m
		this.yearFormElement = y
		
		//if(!(this.ignoreFirstOption && d.selectedIndex==0))this.date = d.value
		//if(!(this.ignoreFirstOption && m.value==0))this.month = m.value
		//if(!(this.ignoreFirstOption && y.value==0))this.year = y.value
		
		this.date = d.value
		this.month = m.value
		this.year = y.value
	}
	
	var i
	this.monthElement = document.forms[this.Id +'CalendarForm'].CalendarMonth
	this.yearElement = document.forms[this.Id +'CalendarForm'].CalendarYear
	
	startOption = (this.ignoreFirstOption)?1:0;
	
	for(i=startOption;i<this.monthFormElement.options.length;i++){
		this.monthElement.options[i-startOption] = new Option(this.monthFormElement.options[i].text,this.monthFormElement.options[i].value);
	}
	
	for(i=startOption;i<this.yearFormElement.options.length;i++){
		this.yearElement.options[i-startOption] = new Option(this.yearFormElement.options[i].text,this.yearFormElement.options[i].value);
	}

	this.synchronise()

	this.object = CalendarGetObject(this.Id + 'Calendar')

}

function CalendarIsDate() {
	var dd = this.date
	var mm = this.month
	var yyyy = this.year
	var d = new Date(mm + "/" + dd + "/" + yyyy);
	//alert(d.getMonth() + 1 == mm)
	//alert(d.getDate() == dd)
	//alert(d.getFullYear() == yyyy)
	return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
	
}

function CalendarSynchronise(){

	startOption = (this.ignoreFirstOption)?1:0;
	
	this.monthElement.selectedIndex = (this.monthFormElement.selectedIndex - startOption < 0)?0:this.monthFormElement.selectedIndex - startOption;
	this.yearElement.selectedIndex = (this.yearFormElement.selectedIndex - startOption < 0)?0:this.yearFormElement.selectedIndex - startOption
	
	this.update(this.monthElement.value, this.yearElement.value)
}

function CalendarFormSynchronise(){

	startOption = (this.ignoreFirstOption)?1:0;
	
	this.dateFormElement.selectedIndex = this.date-1 + startOption
	this.monthFormElement.selectedIndex = this.month-1 + startOption
	this.yearFormElement.selectedIndex = this.yearElement.selectedIndex + startOption
	CalendarOnChange()
}

function CalendarGetDaysInMonth(iMonth, iYear) {
	var dPrevDate = new Date(iYear, iMonth, 0);
	return dPrevDate.getDate();
}

function CalendarBuild(iYear, iMonth) {
	var aMonth = new Array();
	
	aMonth[0] = new Array(7);
	aMonth[1] = new Array(7);
	aMonth[2] = new Array(7);
	aMonth[3] = new Array(7);
	aMonth[4] = new Array(7);
	aMonth[5] = new Array(7);
	aMonth[6] = new Array(7);

	var dCalDate = new Date(iYear, iMonth-1, 1);
	var iDayOfFirst = dCalDate.getDay();
	var iDaysInMonth = CalendarGetDaysInMonth(iMonth, iYear);
	var iVarDate = 1;
	var i, d, w;

	aMonth[0][0] = "Su";
	aMonth[0][1] = "Mo";
	aMonth[0][2] = "Tu";
	aMonth[0][3] = "We";
	aMonth[0][4] = "Th";
	aMonth[0][5] = "Fr";
	aMonth[0][6] = "Sa";

	for (d = iDayOfFirst; d < 7; d++) {
		aMonth[1][d] = iVarDate;
		iVarDate++;
	}
	for (w = 2; w < 7; w++) {
		for (d = 0; d < 7; d++) {
			if (iVarDate <= iDaysInMonth) {
				aMonth[w][d] = iVarDate;
				iVarDate++;
			}
	   	}
	}
	return aMonth;
}

function CalendarWeekday(value, bUseDayImages){
	if(bUseDayImages){
		return '<img src="' + value + '" border="0"  />'
	} else {
		return value
	}
}

function CalendarDraw() { 
	var cssClass = ""
	this.monthArray = CalendarBuild(this.year, this.month);
	
	document.write('<table border="0" cellpadding="0" cellspacing="0" class="CalendarDaysTable">')
	document.write('<tr class="CalendarWeekDayRow">');

	document.write('<td class="CalendarWeekDayCell" title="sunday">' + CalendarWeekday(this.weekday[0],this.useDayImages) + '</td>');
	document.write('<td class="CalendarWeekDayCell" title="monday">' + CalendarWeekday(this.weekday[1],this.useDayImages) + '</td>');
	document.write('<td class="CalendarWeekDayCell" title="tueday">' + CalendarWeekday(this.weekday[2],this.useDayImages) + '</td>');
	document.write('<td class="CalendarWeekDayCell" title="wednesday">' + CalendarWeekday(this.weekday[3],this.useDayImages) + '</td>');
	document.write('<td class="CalendarWeekDayCell" title="thursday">' + CalendarWeekday(this.weekday[4],this.useDayImages) + '</td>');
	document.write('<td class="CalendarWeekDayCell" title="friday">' + CalendarWeekday(this.weekday[5],this.useDayImages) + '</td>');
	document.write('<td class="CalendarWeekDayCell" title="saturday">' + CalendarWeekday(this.weekday[6],this.useDayImages) + '</td>');

	document.write('</tr>');
	
	
	
	i = 0
	for (w = 1; w < 7; w++) {
		document.write('<tr class="CalendarDayRow">')
			for (d = 0; d < 7; d++) {

				if (!isNaN(this.monthArray[w][d])) {
				
					cssClass="CalendarDayCell"
					
					document.write('<td class="' + cssClass + '" id="'+ this.Id +'CalendarDayCell' + i + '" onclick="' + this.Id + '.selectDay('+w+','+d+')">');
					document.write('<span id="' + this.Id + 'CalendarDayText' + i + '">' + this.monthArray[w][d] + '</span>');
					document.write('</td>')
				} else {
					//empty cell
					cssClass="CalendarDayEmptyCell"
					document.write('<td class="' + cssClass + '" id="'+ this.Id +'CalendarDayCell' + i + '" onclick="' + this.Id + '.selectDay('+w+','+d+')">');
					document.write('<span id="' + this.Id + 'CalendarDayText' + i + '">&nbsp;</span>');
					document.write('</td>')
				}
				i++;
			}
		document.write('</tr>');
	}
	
	document.write('</table>')
}

function CalendarUpdate(iMonth, iYear){
	
	this.monthArray = CalendarBuild(iYear, iMonth);

	var objDayCell
	var objDayText
	i = 0
	for (w = 1; w < 7; w++) {
		for (d = 0; d < 7; d++) {
			
			objDayCell = CalendarGetObject(this.Id +'CalendarDayCell' + i)
			objDayText = CalendarGetObject(this.Id +'CalendarDayText' + i)
			if (!isNaN(this.monthArray[w][d])) {
				objDayText.innerHTML = this.monthArray[w][d];
				if(this.monthArray[w][d] == this.date && this.monthElement.options[this.monthElement.selectedIndex].value == this.month && this.yearElement.options[this.yearElement.selectedIndex].value == this.year){
					cssClass="CalendarDaySelectedCell"
				} else {
					cssClass="CalendarDayCell"
				}
			
				objDayCell.className = cssClass
			} else {
				objDayText.innerHTML = "&nbsp;"
				cssClass="CalendarDayEmptyCell"
				objDayCell.className = cssClass
			}
			
			i++;
		}
	}
	
}

function CalendarMoveMonth(iMonth,iYear,dir){

	if(iMonth==0){
		if(this.yearElement.selectedIndex-1>=0){
			iMonth = 12
			iYear = iYear - 1
			
			this.monthElement.selectedIndex = 11
			this.yearElement.selectedIndex--
			
			this.update(iMonth, iYear)
		}
	} else if(iMonth==13){
		if(this.yearElement.selectedIndex+1 < this.yearElement.options.length){
		
			iMonth = 1
			iYear = iYear + 1
			
			this.monthElement.selectedIndex = 0
			this.yearElement.selectedIndex++
			
			this.update(iMonth, iYear)
		}
	} else {
		if(dir<0){
			this.monthElement.selectedIndex--
		} else {
			this.monthElement.selectedIndex++
		}
		this.update(iMonth, iYear)
	}

}

function CalendarSelectDay(w,d)
{
	if (!isNaN(this.monthArray[w][d])) 
	{
		//set the date
		this.date = this.monthArray[w][d]
		this.month = this.monthElement.options[this.monthElement.selectedIndex].value
		this.year = this.yearElement.options[this.yearElement.selectedIndex].value

		//synchronise the form
		this.formSynchronise(this.date)

		this.hide()
	}
}

function CalendarShow(){
	this.synchronise()
	if(this.object){
		if(this.hideAllSelect){
			this.hideSelect()
		}
		this.object.style.visibility = "visible"
		this.isVisible = true
	}
}

function CalendarHide(){
	if(this.object){
		if(this.hideAllSelect){
			this.showSelect()
		}
		this.object.style.visibility = "hidden"
		this.isVisible = false
	}
}

function CalendarSwitchVisibility(){
	if(this.object){
		if(this.isVisible){
			this.hide()
		} else {
			this.show()
		}
		
	}
}

function CalendarGetObject(id){
	if(document.getElementById){ 
		obj = document.getElementById(id)
	} else if(document.all){
		obj = document.all[id]
	} else {
		obj = null
	}
	return obj
}

function CalendarHideSelect(){
	var selNodes = document.getElementsByTagName('select')
	var i=0;
	if(!selNodes.item(0)){
		return false;
	} else {
		do{
			if(!(selNodes.item(i).className=="CalendarMonthSelect" || selNodes.item(i).className=="CalendarYearSelect")){
				selNodes.item(i).style.visibility = 'hidden';
			}
		}
		while(++i < selNodes.length);
	}
}

function CalendarShowSelect(){
	var selNodes = document.getElementsByTagName('select')
	var i=0;
	if(!selNodes.item(0)){
		return false;
	} else {
		do{
			if(!(selNodes.item(i).className=="CalendarMonthSelect" || selNodes.item(i).className=="CalendarYearSelect")){
				selNodes.item(i).style.visibility = 'visible';
			}
		}
		while(++i < selNodes.length);
	}
}

function CalendarOnChange(){
	
}

