
//
//	Common Setup for IBE Booking Block
//
var BookingURL="https://www.vedaleon.com.au/cobhamibe/onlinebooking.aspx?"			//URL to VeRSE Booking Engine
var ReturnElement = "RadioButton"													// Use "RadioButton" or "CheckBox"
var BlockType = "Matrix"															// Use "Basic" or "Matrix"
var MultiCarrier = 0																//Flag for Multi-Carrier Booking Block 0=Single otherwise value equals carrier count


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 = document.bookonline.selDest;
	var IATA = ""
	
	if(MultiCarrier != 0) and (selbox.options.lengh !=0)
		IATA = chosen.substr(0,2).toUpperCase();
	
	selbox.options.length = 0;
	if (chosen == "") {
	selbox.options[selbox.options.length] = new Option('To',' ');

	setInfantText(0);
	}
	if (chosen == IATA+"KDB") {
	selbox.options[selbox.options.length] = new Option('Perth (PER)','PER');
	setInfantText(0);
	}
	if (chosen == IATA+"PER") {
	selbox.options[selbox.options.length] = new Option('Kambalda (KDB)','KDB');
	setInfantText(0);
	}
}

function setDestination()
{
	setOptions(document.bookonline.selDepart.options[document.bookonline.selDepart.selectedIndex].value);
}

function setInfantText(flag)
	{
		if(flag == "0")
			{			
			 	document.getElementById('chtext').innerHTML  = "&nbsp;&nbsp;(2-11 years)";
				document.getElementById('inftext').innerHTML  = "&nbsp;&nbsp;(0-23 months)";
				return;
			}
		if(flag == "1")	//International
			{
			 	document.getElementById('chtext').innerHTML  = "&nbsp;&nbsp;(2-11 years)";
				document.getElementById('inftext').innerHTML  = "&nbsp;&nbsp;(0-23 months)";
				return;
			}
		if(flag == "2")	//Domestic PER-DCN
			{
			 	document.getElementById('chtext').innerHTML  = "&nbsp;&nbsp;(2-11 years)";
				document.getElementById('inftext').innerHTML  = "&nbsp;&nbsp;(0-23 months)";
				return;
			}
	}


//
//	Main Booking Function
//
function bookNow()
	{
		var querystring = "";
		var triptype = "";
		var faretype = "";
		var depdate = "";
		var retdate = "";
		var IATA = "";
		
		if (document.bookonline.selDepart.value == "")
			{
			alert ("Please select a point of Departure");
			return;
			}
		
		if (document.bookonline.selDest.value == "")
			{
			alert ("Please select a Destination");
			return;
			}

		if (document.bookonline.selInfant.value > 0)
			{
			if(document.bookonline.selAdult.value < document.bookonline.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
//					

		if (MultiCarrier == 0)
			{		
			querystring = BookingURL;
			}
			else
			{
			IATA = document.bookonline.selDepart.value.substr(0,2).toUpperCase();	
			
			if (IATA == "NC")
				querystring = "https://www.vedaleon.com.au/cobhamibe/onlinebooking.aspx?";
				
			if (IATA == "QF")
				querystring = "https://www.vedaleon.com.au/cobhamibe/onlinebooking.aspx?";
			}
			
//		querystring = BookingURL+"?";

		querystring += "triptype=" + triptype;
		if (BlockType == "Basic")
		{
		querystring += "&faretype="  + document.bookonline.selTktType.value;
		}
		else
		{
		querystring += "&faretype=LA";
		}
		querystring += "&sortorder=price";
		querystring += "&personadult=" + document.bookonline.selAdult.value;
		querystring += "&personchild=" + document.bookonline.selChild.value;
		querystring += "&personinfant=" + document.bookonline.selInfant.value;
		querystring += "&departure=" + document.bookonline.selDepart.value.substr(2);
		querystring += "&destination=" + document.bookonline.selDest.value;
		querystring += "&departuredate=" + document.bookonline.selDepDay.value + document.bookonline.selDepMonth.value.substr(0,3).toUpperCase() + document.bookonline.selDepMonth.value.substr(4,4).toUpperCase();

		if (triptype == "return")
			{
		querystring += "&returndate="  + document.bookonline.selRetDay.value + document.bookonline.selRetMonth.value.substr(0,3).toUpperCase() + document.bookonline.selRetMonth.value.substr(4,4).toUpperCase();
			}
		
//		alert(querystring);
		
		window.location = querystring;
	}

	function getTripType()
	{
		var triptype="";
		
		if (ReturnElement == "CheckBox")
			{
		//
		//	If Checkbox used to indicate One-way/Return
		//
				if (document.bookonline.chkReturn.checked)
					{
					triptype = "return";
					}
				else
					{
					triptype = "oneway";
					}
			}
			else
			{
			//
			//	If Radio Buttons used to indicate One-way/Return
			//

			for (i = 0; i<2; i++)
				{
				if (document.bookonline.rbRetOneWay[i].checked)
					{
					triptype = document.bookonline.rbRetOneWay[i].value;
					}
				}
			}
			
			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);
	
	monctl.value = selMonYear;
	updateDays(dayctl, monctl )
	dayctl.value = selDay;
}

function ToggleReturnDate(checkboxID) {
  var checkbox = document.getElementById(checkboxID);
  updateToggle = checkbox.checked ? enableReturnDate() : disableReturnDate();
}

function disableReturnDate()
{
 	var formObj = document.bookonline;
  	formObj.elements["selRetDay"].disabled = true;
  	formObj.elements["selRetMonth"].disabled = true;
  	formObj.elements["selRetTime"].disabled = true;
}

function enableReturnDate()
{
	var formObj = document.bookonline;
	formObj.elements["selRetDay"].disabled = false;
	formObj.elements["selRetMonth"].disabled = false;
	formObj.elements["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;
	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;		
    }   
	var selectedDayIndex = parseInt(ctlDays.value) - 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));
	}
}


