
//
//	Common Setup for IBE Booling Block
//
var BookingURL="https://www.vedaleon.com.au/strategicibe/onlinebooking.aspx"		//URL to VeRSE Booking Engine
var ReturnElement = "RadioButton"				// Use "RadioButton" or "CheckBox"


var	today =	new	Date();//We do this to remove reliance on the client date.
//var startdate = new Date("October 5, 2009");
//if (today <= startdate)	 today=startdate;	// Set start date of services after today

var dateFlag = "0";


function bbInit()
	{
	var triptype="";
		
	//Setup Booking Block Values
	datesSetup();
	setDestination();
	
	triptype= getTripType();
	if(triptype == "oneway")
		disableReturnDate();
		
	}	

//
// Set destination dropdown option values based on selected departure port
//

function setOptions(chosen) {
	var selbox = getObject('selDest');
	
	selbox.options.length = 0;
	if (chosen == "") {
	selbox.options[selbox.options.length] = new Option('To',' ');
	}

	if (chosen == "DCN") {
	selbox.options[selbox.options.length] = new Option('Perth','PER');
	}
	
	if (chosen == "DPS") {
	selbox.options[selbox.options.length] = new Option('Perth','PER');
	}

	if (chosen == "PER") {
	selbox.options[selbox.options.length] = new Option('Derby(Curtin)','DCN');
	selbox.options[selbox.options.length] = new Option('Denpasar(Bali)','DPS');
	}
}

function setDestination()
{
	setOptions(getObject('selDepart').options[getObject('selDepart').selectedIndex].value);
}


//
//	Main Booking Function
//
function bookNow()
	{
		var querystring = "";
		var triptype = "";
		var faretype = "";
		var depdate = "";
		var retdate = "";

		
		if (getObject('selDepart').value == "")
			{
			alert ("Please select a point of Departure");
			return;
			}
		
		if (getObject('selDest').value == "")
			{
			alert ("Please select a Destination");
			return;
			}

		if (getObject('selInfant').value > 0)
			{
			if(getObject('selAdult').value < getObject('selInfant').value)
				{
				alert ("Infants may only travel when accompanied by an adult passenger (one infant per adult passenger)");
				return;
				}
			}

		triptype = getTripType();
			
//
//	Construct Booking Query String
//					

		querystring = BookingURL+"?";
		querystring += "triptype=" + triptype;
		querystring += "&faretype=lowestavailable";
		querystring += "&sortorder=price";
		querystring += "&personadult=" + getObject('selAdult').value;
		querystring += "&personchild=" + getObject('selChild').value;
		querystring += "&personinfant=" + getObject('selInfant').value;
		querystring += "&departure=" + getObject('selDepart').value;
		querystring += "&destination=" + getObject('selDest').value;
		querystring += "&departuredate=" + getObject('selDepDay').value + getObject('selDepMonth').value.substr(0,3).toUpperCase() + getObject('selDepMonth').value.substr(4,4).toUpperCase();

		if (triptype == "return")
			{
		querystring += "&returndate="  + getObject('selRetDay').value + getObject('selRetMonth').value.substr(0,3).toUpperCase() + getObject('selRetMonth').value.substr(4,4).toUpperCase();
			}
		
//		alert(querystring);
		
		window.open(querystring,"BookingWindow","Location=0,Status=0,scrollbars=1,width=730,height=630");
		//window.location = querystring;
	}

	function getTripType()
	{
		var triptype="";
		
		if (ReturnElement == "CheckBox")
			{
		//
		//	If Checkbox used to indicate One-way/Return
		//
				if (getObject('chkReturn').checked)
					{
					triptype = "return";
					}
				else
					{
					triptype = "oneway";
					}
			}
			else
			{
			//
			//	If Radio Buttons used to indicate One-way/Return
			//
					if (getObject('rbRetOneWay').checked)
						{
						triptype = "oneway";
						}
					else
						{
						triptype = "return";
						}
			}
			return triptype;
	}
//
//	Booking Block Date Manipulation
//

function datesSetup()
{
	var doc = document.bookonline;
	
	setupDateControls(document.forms[0].selDepDay, document.forms[0].selDepMonth, document.forms[0].txtDepartureDate.value);
	setupDateControls(document.forms[0].selRetDay, document.forms[0].selRetMonth, document.forms[0].txtReturnDate.value);
}

function ChangeDate(value, dayctl, monctl)
{
	var dateSelected = new Date(value);
	var maxdate = new Date();
	value = value.toUpperCase()
	
	maxdate.setMonth(maxdate.getMonth()+11);

	if (dateSelected < today)
		return false;
	if (dateSelected > maxdate)	//11 months after today
	return false;
	
	var selDate = value;
	var selDay = value.substring(0,2);
	var selMonYear = value.substring(3,11);
//	alert(selDay);	
	monctl.value = selMonYear;
	updateDays(dayctl, monctl )
	dayctl.value = selDay;
}

function ToggleReturnDate(checkboxID) {
  var checkbox = getObject(checkboxID);
  updateToggle = checkbox.checked ? enableReturnDate() : disableReturnDate();
}

function disableReturnDate()
{
 	//var formObj = document.bookonline;
  	getObject('selRetDay').disabled = true;
  	getObject('selRetMonth').disabled = true;
  	getObject('selRetTime').disabled = true;
}

function enableReturnDate()
{
	//var formObj = document.bookonline;
	getObject('selRetDay').disabled = false;
	getObject('selRetMonth').disabled = false;
	getObject('selRetTime').disabled = false;
}


var	ie=document.all
var	dom=document.getElementById
var	ns4=document.layers

var startAt = 0			// 0 - sunday ; 1 - monday

if (startAt==0)
{
	dayName = new Array	("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
}
else
{
	dayName = new Array	("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
}

var monthName = new Array;
monthName[0]=new Option("JAN",31);
monthName[1]=new Option("FEB",28);
monthName[2]=new Option("MAR",31);
monthName[3]=new Option("APR",30);
monthName[4]=new Option("MAY",31);
monthName[5]=new Option("JUN",30);
monthName[6]=new Option("JUL",31);
monthName[7]=new Option("AUG",31);
monthName[8]=new Option("SEP",30);
monthName[9]=new Option("OCT",31);
monthName[10]=new Option("NOV",30);
monthName[11]=new Option("DEC",31);

var firstDayOfMonth = new Array;

//var	today =	new	Date(document.form1.txtServerDate.value)
var	dateNow	 = today.getDate()
var	monthNow = today.getMonth()
var	yearNow	 = today.getYear()
if (yearNow < 2000) { yearNow += 1900	}

thisMonth = monthNow;
thisYear = yearNow;
	
for (var i = 0; i < 13; i++)
{
	firstDayOfMonth[i] = new Date(thisYear, thisMonth, 1).getDay();

	thisMonth = thisMonth + 1;
	if (thisMonth > 12)
	{
		thisMonth = 1;
		thisYear = thisYear + 1;
	}
}

var nMonth, nYear;
var displayMonths = new Array;

nYear = yearNow;

for	(i=0; i<13;	i++)
{
	nMonth = i + monthNow
	
	if (nMonth > 11)
	{
		nMonth = nMonth-12
		nYear = yearNow + 1
	}
	displayMonths[i] =	monthName[nMonth].text + " " + nYear;

}

function updateReturnDate(ctlDepMonth, ctlDepDay, ctlRetMonth, ctlRetDay)
{
	var depDate = new Date(ctlDepMonth.value + "/" + ctlDepDay.value);
	var retDate = new Date(ctlRetMonth.value + "/" + ctlRetDay.value);
	
	if (retDate < depDate)
	{
		ctlRetMonth.value = ctlDepMonth.value;
		updateDays(ctlRetDay, ctlRetMonth);
		ctlRetDay.value = ctlDepDay.value;
	}
}


function updateMonths(ctlMonth)
{
	for (var i = 0; i < 13; i++) 
	{
		ctlMonth.options[i] = new Option(displayMonths[i], displayMonths[i] );
	}

}

function updateDays(ctlDays, ctlMonth)
{
//debugger;
	var daysInMonth;
	var SelectedDayIndex = 0;
	
	for (var i = 0; i < 12; i++) 
	{
		if (monthName[i].text == ctlMonth.value.substr(0,3))
		{
			daysInMonth = parseInt(monthName[i].value);
		}
	}

    if(daysInMonth == 28)
    {
        year=parseInt(ctlMonth.value.substr(4,4));
        if (isLeapYear(year))
			daysInMonth = daysInMonth + 1;		
    }   
	selectedDayIndex = parseInt(ctlDays.value.substr(0,2)) - 1;
	ctlDays.options.length = 0;
	var dayOffset = 1;
	var monthIndex = ctlMonth.selectedIndex;
	if (monthIndex == 0)
	{	
		dayOffset=dateNow;
		if (selectedDayIndex < dayOffset - 1)
			selectedDayIndex = 0;
		else
			selectedDayIndex = selectedDayIndex - dayOffset + 1;
	}
	populateDateList(dayOffset, daysInMonth, ctlDays, monthIndex);
	if (selectedDayIndex > daysInMonth - 1)
		selectedDayIndex = daysInMonth - 1;
	if (selectedDayIndex > -1)
		ctlDays.options[selectedDayIndex].selected = true;
}

function populateDateList(beginDate, endDate, ctlDays, monthIndex)
{
	daysLeft = (endDate - beginDate) + 1;
	var date;
	for (var i = 0; i < daysLeft; i++) 
	{
		date = i+beginDate;
		var index = (firstDayOfMonth[monthIndex] + date - 1 - startAt) % 7;
		if(date < 10)
		{			
		ctlDays.options[i] = new Option(dayName[index] + " " + date, "0" + date);
		}
		else
		{
		ctlDays.options[i] = new Option(dayName[index] + " " + date, date);
		}
	}
}

function isLeapYear(year)
{
	if (year % 4 == 0  && ( !(year % 100 == 0 && year % 400 != 0)))
		return true;
	else
		return false;
}

//txtDefaultDate must be in "dd MMM yyyy" format.
function setupDateControls(ctlDays, ctlMonth, txtDefaultDate )
{
	if (txtDefaultDate == "") 
	{
		updateMonths(ctlMonth);
		updateDays(ctlDays, ctlMonth);
	}
	else
	{
		updateMonths(ctlMonth);
		ctlMonth.value = txtDefaultDate.substr(3,8);
		updateDays(ctlDays, ctlMonth);
		ctlDays.value = parseInt(txtDefaultDate.substr(0,2));
	}
}

function getObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId);
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject


	

