var waitLayerName = "largeWait";
var VALID_EMAIL_REGEX = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

function checkEnter(event, func)
{ 	
	NS4 = (document.layers) ? true : false;
	var code = 0;
	
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13)
	{
		eval(func);  // call the function
	}
}


function disableControlKey(event, msg) 
{   

        NS4 = (document.layers) ? true : false;
        var code = 0;
        
        if (NS4)
            code = event.which;
        else
            code = event.keyCode;
                
        if (code==17)
        {
            alert(msg);
            window.event.returnValue = false;
        }
}   
function validEmails(primaryEmail, confirmEmail) {
    
    if ( VALID_EMAIL_REGEX.test(primaryEmail) == false ) {
        alert('Please enter correct email address format for Primary Contact Email');
        return false;
    }
    
    if ( VALID_EMAIL_REGEX.test(confirmEmail) == false ) {
        alert('Please enter correct email address format for Confirm Contact Email');
        return false;
    }    
    
    if ( primaryEmail != confirmEmail) {
        alert('Primary Contact Email and Confirm Contact Email must be the same');
        return false;
    }
    
    return true;
    
}
function validateLogin()
{
	var username = document.forms['loginForm'].username.value;
	if (username == null || username == '')
	{
		alert(document.forms['loginForm'].invalidUsername.value);
		document.forms['loginForm'].username.focus();
		return false;
	}
	
	var password = document.forms['loginForm'].password.value;
	if (password == null || password == '')
	{
		alert(document.forms['loginForm'].invalidPassword.value);
		document.forms['loginForm'].password.focus();
		return false;
	}
	
	var usernameRE = /[\s\,\'\"\`\~\!\#\$\%\^\&\*\(\)\{\}\[\]\|\\\+\=\?\/\<\>]+/;
	var loginRE = /[\s\.\@\,\'\"\`\~\!\#\$\%\^\&\*\(\)\{\}\[\]\|\\\+\=\?\/\<\>]+/;
	
	if (document.forms['loginForm'].username.value.search(usernameRE) > -1)
	{
		alert(document.forms['loginForm'].nameNonalpha.value);
		document.forms['loginForm'].username.focus();
		return false;
	}
			    
	if (document.forms['loginForm'].password.value.search(loginRE) > -1)
	{
		alert(document.forms['loginForm'].invalidPassword.value);
		document.forms['loginForm'].password.focus();
		return false;
	}
	
	showLargeWait();
	document.forms['loginForm'].submit();
}


function changedStartDate(startName, endName, addDays)
{
	if (document.forms[searchFormName].elements[startName] &&
		document.forms[searchFormName].elements[endName])
	{
		var startVal = document.forms[searchFormName].elements[startName].value;
		var startMonth = startVal.substring(startVal.indexOf('/')+1, startVal.lastIndexOf('/'));
		if (startMonth.substring(0,1) == '0')
		{
			startMonth = startMonth.substring(1,2);
		}
		var startDate = new Date(startVal.substring(startVal.lastIndexOf('/')+1), parseInt(startMonth) - 1, startVal.substring(0, startVal.indexOf('/')));
		
		var endVal = document.forms[searchFormName].elements[endName].value;
		var endMonth = endVal.substring(endVal.indexOf('/')+1, endVal.lastIndexOf('/'));
		if (endMonth.substring(0,1) == '0') { endMonth = endMonth.substring(1,2);}
		var endDate = new Date(endVal.substring(endVal.lastIndexOf('/')+1), parseInt(endMonth) - 1, endVal.substring(0, endVal.indexOf('/')));
		
		if (endDate.getFullYear() < startDate.getFullYear())
		{
			setFutureDate(endName, startDate, addDays);
		}
		else if(endDate.getFullYear() == startDate.getFullYear())
		{
			if (endDate.getMonth() < startDate.getMonth())
			{
				setFutureDate(endName, startDate, addDays);
			}
			else if (endDate.getMonth() == startDate.getMonth())
			{
				if (endDate.getDate() < startDate.getDate())
				{
					setFutureDate(endName, startDate, addDays);
				}
			}
		}
	}
}

function setFutureDate(name, dateObj, addDays)
{
	var newDate = new Date();
	//newDate.setTime(dateObj.getTime() + (86400000) * addDays);
	newDate.setTime(dateObj.getTime() + (86400000 * addDays));
	
	document.forms[searchFormName].elements[name].value = getFormattedDate(newDate.getFullYear(), (newDate.getMonth() + 1), newDate.getDate());
}

var tmpStart, tmpEnd, tmpDays;
tmpDays = 1;
function datePressed(year, month, day)
{
	document.forms[searchFormName].elements['startDate'].value = getFormattedDate(year, month, day);
	changedStartDate('startDate', 'endDate', tmpDays);
}

function getFormattedDate(year, month, day)
{
	var date = '';
	if (day < 10)
		date += '0';
	date += day + '/';
	if (month < 10)
		date += '0';
	date += month + '/' + year;
	
	return date;
}


//
//
// --- Vehicle javascripts ---

function validateVehicleDetails()
{
	if (document.forms[searchFormName].pickupLocation)
	{
		if (!document.forms[searchFormName].pickupLocation)
		{
			alert(document.forms[searchFormName].noPickupLocations.value);
			return false;
		}
		
		if (!document.forms[searchFormName].returnLocation)
		{
			alert(document.forms[searchFormName].noReturnLocations.value);
			return false;
		}
			
		// check the times are within the location's open hours
		var selectedLoc = 1;
		
		// see if we only have one location
		if (!document.forms[searchFormName].pickupLocation[1])
		{
			selectedLoc = document.forms[searchFormName].pickupLocation.value;
		}
		
		for (i=0; i< document.forms[searchFormName].pickupLocation.length; i++)
		{
			if (document.forms[searchFormName].pickupLocation[i].checked)
			{
				selectedLoc = document.forms[searchFormName].pickupLocation[i].value;
			}
		}
		
		var locOpenHour = document.forms[searchFormName].elements['pickupOpenTime' + selectedLoc].value.substring(0, 2);
		var locOpenMinute = document.forms[searchFormName].elements['pickupOpenTime' + selectedLoc].value.substring(3, 5);
		var locCloseHour = document.forms[searchFormName].elements['pickupCloseTime' + selectedLoc].value.substring(0, 2);
		var locCloseMinute = document.forms[searchFormName].elements['pickupCloseTime' + selectedLoc].value.substring(3, 5);
		
		// check to see if it's set to 00:00 and change to 24:00 if it is
		if (locOpenHour == '00') {locOpenHour = "24";}
		if (locCloseHour == '00') {locCloseHour = "24";}
		
		// account for things like open from 6am - 1am
		
		var pickupHour = document.forms[searchFormName].elements["pickupTime.hour"].options[document.forms[searchFormName].elements["pickupTime.hour"].selectedIndex].value;
		var pickupMinute = document.forms[searchFormName].elements["pickupTime.minute"].options[document.forms[searchFormName].elements["pickupTime.minute"].selectedIndex].value;
		
		if (locOpenHour < locCloseHour)
		{
			if (pickupHour < locOpenHour || (pickupHour == locOpenHour && pickupMinute < locOpenMinute)
				|| pickupHour > locCloseHour || (pickupHour == locCloseHour && pickupMinute > locCloseMinute))
			{
				alert(document.forms[searchFormName].invalidPickupHour.value);
				return false;
			}
		}
		else
		{
			if ((locCloseHour < pickupHour && pickupHour < locOpenHour) || (pickupHour == locOpenHour && pickupMinute < locOpenMinute)
				|| (pickupHour == locCloseHour && pickupMinute > locCloseMinute))
			{
				alert(document.forms[searchFormName].invalidPickupHour.value);
				return false;
			}
		}
		
		// check the return time
		if (!document.forms[searchFormName].returnLocation[1])
		{
			returnSelectedLoc = document.forms[searchFormName].returnLocation.value;
		}
		for (i=0; i< document.forms[searchFormName].returnLocation.length; i++)
		{
			if (document.forms[searchFormName].returnLocation[i].checked)
			{
				returnSelectedLoc = document.forms[searchFormName].returnLocation[i].value;
			}
		}
		if (returnSelectedLoc == -1)
		{
			alert('Please select a return location');
			return false;
		}
		var retLocOpenHour = document.forms[searchFormName].elements['returnOpenTime' + returnSelectedLoc].value.substring(0, 2);
		var retLocOpenMinute = document.forms[searchFormName].elements['returnOpenTime' + returnSelectedLoc].value.substring(3, 5);
		var retLocCloseHour = document.forms[searchFormName].elements['returnCloseTime' + returnSelectedLoc].value.substring(0, 2);
		var retLocCloseMinute = document.forms[searchFormName].elements['returnCloseTime' + returnSelectedLoc].value.substring(3, 5);
		
		// check to see if it's set to 00:00 and change to 24:00 if it is
		if (retLocOpenHour == '00') {retLocOpenHour = "24";}
		if (retLocCloseHour == '00') {retLocCloseHour = "24";}
		
		var returnHour = document.forms[searchFormName].elements["returnTime.hour"].options[document.forms[searchFormName].elements["returnTime.hour"].selectedIndex].value;
		var returnMinute = document.forms[searchFormName].elements["returnTime.minute"].options[document.forms[searchFormName].elements["returnTime.minute"].selectedIndex].value;
		
		var startDate = document.forms[searchFormName].startDate.value;
		var endDate = document.forms[searchFormName].endDate.value;

		if(startDate == endDate)
		{
			if (pickupHour > returnHour || ((pickupHour == returnHour) && (pickupMinute > returnMinute)))
			{
				alert(document.forms[searchFormName].startDateError.value);
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			if (retLocOpenHour < retLocCloseHour)
			{
				if (returnHour < retLocOpenHour || (returnHour == retLocOpenHour && returnMinute < retLocOpenMinute)
					|| returnHour > retLocCloseHour || (returnHour == retLocCloseHour && returnMinute > retLocCloseMinute))
				{
					alert(document.forms[searchFormName].invalidReturnHour.value);
					return false;
				}
			}
			else
			{
				if ((retLocCloseHour < returnHour && returnHour < retLocOpenHour) || (returnHour == retLocOpenHour && returnMinute < retLocOpenMinute)
					|| (returnHour == retLocCloseHour && returnMinute > retLocCloseMinute))
				{
					alert(document.forms[searchFormName].invalidReturnHour.value);
					return false;
				}
			}
			
			if (returnHour > pickupHour && !document.forms[searchFormName].sameDay)
			{
				if (!confirm(document.forms[searchFormName].msgExtraDay.value))
				{
					return false;
				}
				else
				{
					document.forms[searchFormName].research.value = 'yes';
				}
			}
		}
	}
	return true;
}

function validatedetailsForm()
{
	if (!validateVehicleDetails())
	{
		return false;
	}
	
	if (document.forms[searchFormName].agree) {
		if (!document.forms[searchFormName].agree.checked) {
			alert(document.forms[searchFormName].termsAgreeErr.value);
			document.forms[searchFormName].agree.focus();
			return false;
		}
	}
	
	showLargeWait("altLargeWait");
	document.forms[searchFormName].submit();
}


function setPaxFields()
{
	setPaxType('paxAdult');
	setPaxType('paxChild');
	setPaxType('paxInput');
}

function setPaxType(type)
{
	var i=1;
	while (document.forms[searchFormName].elements['related' + type + i])
	{
		var selPax = document.forms[searchFormName].elements['related' + type + i][document.forms[searchFormName].elements['related' + type + i].selectedIndex].value;
		if (selPax == 'Other')
		{
			document.forms[searchFormName].elements[type + i + 'Title'].disabled = false;
			document.forms[searchFormName].elements[type + i + 'FirstName'].disabled = false;
			document.forms[searchFormName].elements[type + i + 'Surname'].disabled = false;
			document.forms[searchFormName].elements[type + i + 'Title'].selectedIndex = 0;
			document.forms[searchFormName].elements[type + i + 'FirstName'].value = '';
			document.forms[searchFormName].elements[type + i + 'Surname'].value = '';
			document.forms[searchFormName].elements[type + i + 'ProfileId'].value = '';
			if (document.forms[searchFormName].elements['loyaltyNumber' + type.substring(3) + i])
			{
				document.forms[searchFormName].elements['loyaltyNumber' + type.substring(3) + i].value = '';
			}
		}
		else
		{
			document.forms[searchFormName].elements[type + i + 'Title'].disabled = true;
			document.forms[searchFormName].elements[type + i + 'FirstName'].disabled = true;
			document.forms[searchFormName].elements[type + i + 'Surname'].disabled = true;
			var title = document.forms[searchFormName].elements['pax' + selPax + 'Title'].value;
			var index = 0;
			for (j=0; j<document.forms[searchFormName].elements[type + i + 'Title'].length; j++)
			{
				if (document.forms[searchFormName].elements[type + i + 'Title'][j].value == title)
				{
					index = j;
				}
			}
			
			document.forms[searchFormName].elements[type + i + 'Title'].selectedIndex = index;
			document.forms[searchFormName].elements[type + i + 'FirstName'].value = document.forms[searchFormName].elements['pax' + selPax + 'FirstName'].value;
			document.forms[searchFormName].elements[type + i + 'Surname'].value = document.forms[searchFormName].elements['pax' + selPax + 'Surname'].value;
			document.forms[searchFormName].elements[type + i + 'ProfileId'].value = document.forms[searchFormName].elements['pax' + selPax + 'ProfileId'].value;
			if (document.forms[searchFormName].elements['loyaltyNumber' + type.substring(3) + i])
			{
				if (document.forms[searchFormName].elements['pax' + selPax + 'Loyalty'])
				{
					document.forms[searchFormName].elements['loyaltyNumber' + type.substring(3) + i].value = document.forms[searchFormName].elements['pax' + selPax + 'Loyalty'].value;
				}
				else
				{
					document.forms[searchFormName].elements['loyaltyNumber' + type.substring(3) + i].value = '';
				}
			}
		}
		i++;
	}
}


function validatebookingForm()
{
	enablePax('paxAdult');
	enablePax('paxChild');
	enablePax('paxInput');
	if (document.forms[searchFormName].cardExpiryDate)
	{
		enableCreditCardForm();
	}
	
	if (document.forms[searchFormName].pickupLocation)
	{
		if (!validateVehicleDetails())
		{
			return false;
		}
	}    
    if (validEmails(document.forms[searchFormName].elements['contactEmail'].value, document.forms[searchFormName].elements['confirmContactEmail'].value)) {
        showLargeWait(waitLayerName);   
        document.forms[searchFormName].submit();
        
    }

}

function validatepaymentForm()
{
	if (document.forms[searchFormName].cardExpiryDate)
	{
		enableCreditCardForm();
	}
	showLargeWait("altLargeWait");
	document.forms[searchFormName].submit();
}


function enablePax(type)
{
	var i=1;
	while (document.forms[searchFormName].elements['related' + type + i])
	{
		document.forms[searchFormName].elements[type + i + 'Title'].disabled = false;
		document.forms[searchFormName].elements[type + i + 'FirstName'].disabled = false;
		document.forms[searchFormName].elements[type + i + 'Surname'].disabled = false;
		i++;
	}
}

function setupCalendar()
{
cal1 = new CalendarPopup();
cal1.setDisplayType("month");
cal1.setReturnMonthFunction("monthReturn");
cal1.showYearNavigation();
}

var dateSelected;
function monthReturn(y,m) {
	var month = '' + m;
	if (m < 10)
		month = '0' + m;
	document.forms[searchFormName].elements[dateSelected].value=month+"/"+y;
}


function setCreditCardFields()
{
	var cardIndex = document.forms[searchFormName].cardSelect[document.forms[searchFormName].cardSelect.selectedIndex].value;
	if (cardIndex == 'newCard')
	{
		enableCreditCardForm();
		document.forms[searchFormName].cardHolderName.value = '';
		document.forms[searchFormName].cardNumber.value = '';
		document.forms[searchFormName].cardExpiryDate.value = 'MM/YYYY';
	}
	else
	{
		document.forms[searchFormName].creditCardCode.disabled = true;
		document.forms[searchFormName].cardHolderName.disabled = true;
		document.forms[searchFormName].cardNumber.disabled = true;
		document.forms[searchFormName].cardExpiryDate.disabled = true;
		for (i = 0; i<document.forms[searchFormName].creditCardCode.length; i++)
		{
			if (document.forms[searchFormName].creditCardCode[i].value == document.forms[searchFormName].elements['storedCardCode' + cardIndex].value)
			{
				document.forms[searchFormName].creditCardCode[i].selected = true;
			}
		}
		document.forms[searchFormName].cardHolderName.value = document.forms[searchFormName].elements['storedCardHolderName' + cardIndex].value;
		document.forms[searchFormName].cardNumber.value = document.forms[searchFormName].elements['storedCardNumber' + cardIndex].value;
		document.forms[searchFormName].cardExpiryDate.value = document.forms[searchFormName].elements['storedExpireDate' + cardIndex].value;
	}
}

function enableCreditCardForm()
{
	document.forms[searchFormName].creditCardCode.disabled = false;
	document.forms[searchFormName].cardHolderName.disabled = false;
	document.forms[searchFormName].cardNumber.disabled = false;
	document.forms[searchFormName].cardExpiryDate.disabled = false;
}

var step=0;
var slideShowImages = new Array();

/*function setupImages() {
	var images = new Array();

	if (document.forms[0].slideShowImages) {
		if (document.forms[0].slideShowImages.value.indexOf(";") <= -1) {
			document.forms[0].slideShowImages.value = document.forms[0].slideShowImages.value + ";";
		}
		
		if (document.forms[0].slideShowImages.value.indexOf(";") > -1) {
			images = document.forms[0].slideShowImages.value.split(";");
			
			for (i = 0; i < images.length; i++) {
				if (images[i] != "") {
					slideShowImages[i] = new Image();
					slideShowImages[i].src=supplierImageRoot + "/supplier_images/" + images[i];
				}
			}
		}
	}
}*/

/** handles images urls that start with http:// */
function setupImages() {
	var images = new Array();

	if (document.forms[0].slideShowImages) {
		if (document.forms[0].slideShowImages.value.indexOf(";") <= -1) {
			document.forms[0].slideShowImages.value = document.forms[0].slideShowImages.value + ";";
		}
		
		if (document.forms[0].slideShowImages.value.indexOf(";") > -1) {
			images = document.forms[0].slideShowImages.value.split(";");
			
			for (i = 0; i < images.length; i++) {
				if (images[i] != "") {
					slideShowImages[i] = new Image();
					if (images[i].indexOf("http") == -1) {
					slideShowImages[i].src=supplierImageRoot + "/supplier_images/" + images[i];
				} else 
				{
					slideShowImages[i].src=images[i];
				}
				}
			}
		}
	}
}

function slideit(){

//if browser does not support the image object, exit.
	if (!document.images)
		return
	
	if (document.forms[0].slideShowImages) {

		if (document.forms[0].slideShowImages.value.indexOf(";") > -1) {
		
			if (document.all) {
				document.all.Slide.filters.blendTrans.apply()
			}
			
			if (slideShowImages[step] != null)
			{
				document.images.Slide.src=eval("slideShowImages[" + step + "].src")
				
				if (document.all) {
					document.all.Slide.filters.blendTrans.play()
				}
			}
			
			if (step < slideShowImages.length) {
				step++;
			}
			if (step == slideShowImages.length) {
				step=0;
			}
		
			//call function "slideit()" every 2.5 seconds
			setTimeout("slideit()",5000)
		}
	}
}


function rulesPopup(msg, title, width, height)
{
	var popup = makePopup('', 'rulesPopup', width, height);
	
	var isNetscape = navigator && navigator.appName.substring(0,8) == 'Netscape';

	popup.document.clear();
	popup.document.writeln("<html><head><title>");
	popup.document.writeln(title);
	popup.document.writeln("</title>");

	if (isNetscape) {

		popup.document.writeln("</head>");
		popup.document.writeln("<body>");
		popup.document.writeln("<div align=\"right\" style=\"font-family:Verdana, Tahoma, Arial, Helvetica, sans-serif; font-size:10pt; font-weight:normal; align:right;\">");
		popup.document.writeln("<a href=\"#\" onClick=\"window.print(); return false;\">Print</a></div>");
		popup.document.writeln(msg);
		popup.document.writeln("<div align=\"right\" style=\"font-family:Verdana, Tahoma, Arial, Helvetica, sans-serif; font-size:10pt; font-weight:normal; align:right;\">");
		popup.document.writeln("<a href=\"#\" onclick=\"window.close();\">Close</a></div>");

	} else {
		
		popup.document.writeln("<script language=\"JavaScript\">");
		popup.document.writeln("function printWindow() {");
		popup.document.writeln("alert('Preparing to print Terms & Conditions');");
		popup.document.writeln("window.print();");
		popup.document.writeln("return false;");
		popup.document.writeln("}");
		popup.document.writeln("</script>");
		popup.document.writeln("</head>");
		popup.document.writeln("<body>");
		popup.document.writeln("<div align=\"right\" style=\"font-family:Verdana, Tahoma, Arial, Helvetica, sans-serif; font-size:10pt; font-weight:normal; align:right;\">");
		popup.document.writeln("<a href=\"#\" onClick=\"printWindow(); return false;\">Print</a></div>");
		popup.document.writeln(msg);
		popup.document.writeln("<div align=\"right\" style=\"font-family:Verdana, Tahoma, Arial, Helvetica, sans-serif; font-size:10pt; font-weight:normal; align:right;\">");
		popup.document.writeln("<a href=\"#\" onclick=\"window.close();\">Close</a></div>");
	}	
	popup.document.writeln("</body></html>");
	popup.document.close();
	popup.focus();
}

function makePopup(url, name, width, height)
{
	window.open(url, name, "width=" + width + ",height=" + height + ",scrollbars=yes,resizable=yes");
}

function makeEmptyPopup(windowName, width, height)
{
	var isNetscape = navigator && navigator.appName.substring(0,8) == 'Netscape';
	
	if (isNetscape)
	{
		window.open('', windowName, "width=" + width + ",height=" + height + ",scrollbars=yes,resizable=yes");
	}
	else
	{
		window.open('', windowName, "width=" + width + ",height=" + height + ",scrollbars=yes,resizable=yes");
	}
}

function updateFlightTotalPrice()
{
	var total = 0;
	var i=1;
	if (document.selectForm.outboundInstance)
	{
		if (document.selectForm.outboundInstance[1])
		{
			while (document.selectForm.elements['outboundPrice' + i])
			{
				if (document.selectForm.outboundInstance[i-1].checked)
				{
					total = document.selectForm.elements['outboundPrice' + i].value;
				}
				i++;
			}
		}
		else
		{
			if (document.selectForm.outboundInstance.checked)
			{
				total = Number(total) + Number(document.selectForm.elements['outboundPrice' + i].value);
			}
		}
	}
		
	i=1;
	if (document.selectForm.inboundInstance)
	{
		if (document.selectForm.inboundInstance[1])
		{
			while (document.selectForm.elements['inboundPrice' + i])
			{
				if (document.selectForm.inboundInstance[i-1].checked)
				{
					total = Number(total) + Number(document.selectForm.elements['inboundPrice' + i].value);
				}
				i++;
			}
		}
		else
		{
			if (document.selectForm.inboundInstance.checked)
			{
				total = Number(total) + Number(document.selectForm.elements['inboundPrice' + i].value);
			}
		}
	}
	
	document.selectForm.totalFare.value = document.selectForm.totalCurrency.value + " " + roundAmount(total);
}

function roundAmount(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + 
      s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}

function validateFlightselectForm() {
	
	var outboundIndex = -1;
	var inboundIndex = -1;
	i = 0;
	if (document.forms[searchFormName].outboundInstance[1])
	{
		while(document.forms[searchFormName].outboundInstance[i])
		{
			if (document.forms[searchFormName].outboundInstance[i].checked)
			{
				outboundIndex = i;
			}
			i++;
		}
	}
	else
	{
		if (document.forms[searchFormName].outboundInstance.checked)
		{
			outboundIndex = 0;
		}
	}
	if (document.forms[searchFormName].inboundInstance)
	{
		if (document.forms[searchFormName].inboundInstance[1])
		{
			i = 0;
			while(document.forms[searchFormName].inboundInstance[i])
			{
				if (document.forms[searchFormName].inboundInstance[i].checked)
				{
					inboundIndex = i;
				}
				i++;
			}
		}
		else
		{
			if (document.forms[searchFormName].inboundInstance.checked)
			{
				inboundIndex = 0;
			}
		}
	}
	
	if (outboundIndex == -1) {
		alert(document.forms[searchFormName].errorDeparting.value);
		return false;
	}
	
	if (document.forms[searchFormName].inboundInstance && inboundIndex == -1) {
		alert(document.forms[searchFormName].errorReturning.value);
		return false;
	}
	
	showLargeWait('altLargeWait');
	document.forms[searchFormName].submit();	
	
	return true;
}


var width;
var height;
if (navigator.userAgent.indexOf("Mozilla") > -1 && navigator.userAgent.indexOf("MSIE") == -1)
{
	width = window.innerWidth;
	height = window.innerHeight;
}


var lName;
function showLargeWait(layerName)
{
	var layer = layerName;
	if (layer == null || layer == '')
	{
		layer = "largeWait";
	}
	
	// Netscape 4+
	if (document.layers) {
		document.layers[layer].moveBy((width/2 - elementWidth/2),0);
		document.layers[layer].visibility = "visible";
	}
	// Internet Explorer 4
	else if (document.all & !document.getElementById) {
		document.all[layer].style.width = width;
		document.all[layer].style.height = height;
		document.all[layer].style.visibility = "visible";
	}
	// Netscape 6 and Internet Explorer 5
	else if (document.getElementById) {
		if (width == null)
		{
			document.getElementById(layer).style.width = document.body.clientWidth;
			document.getElementById(layer).style.height = document.body.clientHeight;
		}
		else
		{
			document.getElementById(layer).style.width = width;
			document.getElementById(layer).style.height = height;
		}
		document.getElementById(layer).style.visibility = "visible";
		window.scrollTo(0,0);
		if (document.forms[searchFormName])
		{
			for (i=0; i < document.forms[searchFormName].elements.length; i++)
			{
				document.forms[searchFormName].elements[i].style.visibility = "hidden";
			}
		}
	}
	if (document.images[layer])
	{
		lName = layer;
		setTimeout("swapImage()", 100);
	}
}

function hideImage(imageId) {
    document.getElementById(imageId).style.visibility = "hidden";
}

function swapImage()
{
	document.images[lName].src = document.images[lName].src;
}


function hideDropDowns(type)
{
	switch(type)
	{
		case "flights":
			document.forms[searchFormName].startCity.style.visibility='hidden';
			document.forms[searchFormName].endCity.style.visibility='hidden';
			document.forms[searchFormName].startTime.style.visibility='hidden';
			if (document.forms[searchFormName].endTime)
				document.forms[searchFormName].endTime.style.visibility='hidden';
			if (document.forms[searchFormName].flightClass)
				document.forms[searchFormName].flightClass.style.visibility='hidden';
			hidePaxFields();
			break;
		case "cars":
			document.forms[searchFormName].startCity.style.visibility='hidden';
			document.forms[searchFormName].endCity.style.visibility='hidden';
			if (document.forms[searchFormName].driverAge)
				document.forms[searchFormName].driverAge.style.visibility='hidden';
			if (document.forms[searchFormName].vehicleType)
				document.forms[searchFormName].vehicleType.style.visibility='hidden';
			break;
		case "hotels":
			document.forms[searchFormName].endCity.style.visibility='hidden';
			if (document.forms[searchFormName].roomType)
				document.forms[searchFormName].roomType.style.visibility='hidden';
			hidePaxFields();
			break;
		case "tours":
			document.forms[searchFormName].startCity.style.visibility='hidden';
			if (document.forms[searchFormName].duration)
				document.forms[searchFormName].duration.style.visibility='hidden';
			hidePaxFields();
			break;
		case "transfers":
			document.forms[searchFormName].startCity.style.visibility='hidden';
			hidePaxFields();
			break;
		case "packages":
			document.forms[searchFormName].packageType.style.visibility='hidden';
			document.forms[searchFormName].startCity.style.visibility='hidden';
			document.forms[searchFormName].endCity.style.visibility='hidden';
			document.forms[searchFormName].startTime.style.visibility='hidden';
			document.forms[searchFormName].endTime.style.visibility='hidden';
			document.forms[searchFormName].starRating.style.visibility='hidden';
			document.forms[searchFormName].carType.style.visibility='hidden';
			document.forms[searchFormName].airline.style.visibility='hidden';
			document.forms[searchFormName].tourType.style.visibility='hidden';
			hidePaxFields();
			break;
	}
}

function hidePaxFields()
{
	document.forms[searchFormName].numAdults.style.visibility='hidden';
	if (document.forms[searchFormName].childAge1)
	{
		document.forms[searchFormName].childAge1.style.visibility='hidden';
		document.forms[searchFormName].childAge2.style.visibility='hidden';
		document.forms[searchFormName].childAge3.style.visibility='hidden';
		document.forms[searchFormName].childAge4.style.visibility='hidden';
	}
	if (document.forms[searchFormName].numChildren)
	{
		document.forms[searchFormName].numChildren.style.visibility='hidden';
		document.forms[searchFormName].numInfants.style.visibility='hidden';
	}
}


function validateinsDetailsForm()
{
	showLargeWait('altLargeWait');
	document.forms['insDetailsForm'].submit();
}


function validateMailing()
{
	var email = document.forms['mailingForm'].email.value;
	if (email == null || email == '' || !checkEmail(email) || email == "your@email.com")
	{
		alert(document.forms['mailingForm'].invalidEmail.value);
		document.forms['mailingForm'].email.focus();
		return false;
	}
	
	showLargeWait();
	document.forms['mailingForm'].submit();
}

function checkEmail(email) {
	if (stringTrim(email) == '') return false;
	if (email.match(VALID_EMAIL_REGEX) == null) return false;
	return true;
}

function stringTrim(str) {
	var str2 = str;
	if (str2.length > 0)
		while (str2.indexOf(' ') == 0)
			str2 = str2.substring(1);
	
	if (str2.length > 0)
		while (str2.lastIndexOf(' ') == str2.length - 1)
			str2 = str2.substring(0, str2.length - 1);
	
	return str2;
}


function setSpecialStartDate(field, value)
{
	var today = new Date();
	var startDate = new Date();
	startDate.setYear(value.substring(6));
	startDate.setMonth(value.substring(3, 5) - 1);
	startDate.setDate(value.substring(0,2));
	
	if (today > startDate)
	{
		return field.value;
	}
	else
	{
		return value;
	}
}


function updatePackageTotal()
{
	// get the hotel price
	var hotel = 0;
	i=0;
	if (document.forms[0].hotelInstance[1])
	{
		while(document.forms[0].hotelInstance[i])
		{
			if (document.forms[0].hotelInstance[i].checked)
			{
				hotel = document.forms[0].elements['hotelPrice' + i].value;
			}
			i++;
		}
	}
	else
	{
		if (document.forms[0].hotelInstance.checked)
		{
			hotel = document.forms[0].hotelPrice0.value;
		}
	}
	
	// get the outbound flight price
	var outbound = 0;
	i=0;
	if (document.forms[0].outboundFlightInstance[1])
	{
		while(document.forms[0].outboundFlightInstance[i])
		{
			if (document.forms[0].outboundFlightInstance[i].checked)
			{
				outbound = document.forms[0].elements['outboundFlightPrice' + i].value;
			}
			i++;
		}
	}
	else
	{
		if (document.forms[0].outboundFlightInstance.checked)
		{
			outbound = document.forms[0].outboundFlightInstance0.value;
		}
	}
	
	// get the inbound flight price
	var inbound = 0;
	i=0;
	if (document.forms[0].inboundFlightInstance[1])
	{
		while(document.forms[0].inboundFlightInstance[i])
		{
			if (document.forms[0].inboundFlightInstance[i].checked)
			{
				inbound = document.forms[0].elements['inboundFlightPrice' + i].value;
			}
			i++;
		}
	}
	else
	{
		if (document.forms[0].inboundFlightInstance.checked)
		{
			inbound = document.forms[0].inboundFlightInstance0.value;
		}
	}
	
	// get the vehicle price
	var vehicle = 0;
	i=0;
	if (document.forms[0].vehicleInstance[1])
	{
		while(document.forms[0].vehicleInstance[i])
		{
			if (document.forms[0].vehicleInstance[i].checked)
			{
				vehicle = document.forms[0].elements['vehiclePrice' + i].value;
			}
			i++;
		}
	}
	else
	{
		if (document.forms[0].vehicleInstance.checked)
		{
			vehicle = document.forms[0].vehicleInstance0.value;
		}
	}
	
	// get the tour price
	var tour = 0;
	i=0;
	if (document.forms[0].tourInstance[1])
	{
		while(document.forms[0].tourInstance[i])
		{
			if (document.forms[0].tourInstance[i].checked)
			{
				tour = document.forms[0].elements['tourPrice' + i].value;
			}
			i++;
		}
	}
	else
	{
		if (document.forms[0].tourInstance.checked)
		{
			tour = document.forms[0].tourInstance0.value;
		}
	}
	
	// get the insurance price
	var insurance = 0;
	i=0;
	if (document.forms[0].insuranceInstance[1])
	{
		while(document.forms[0].insuranceInstance[i])
		{
			if (document.forms[0].insuranceInstance[i].checked)
			{
				insurance = document.forms[0].elements['insurancePrice' + i].value;
			}
			i++;
		}
	}
	else
	{
		if (document.forms[0].insuranceInstance.checked)
		{
			insurance = document.forms[0].insuranceInstance0.value;
		}
	}
	
	
	var total = roundAmount(eval(hotel) + eval(outbound) + eval(inbound) + eval(vehicle) + eval(tour) + eval(insurance));
	document.forms[0].average.value = roundAmount(total / eval(document.forms[0].numPax.value));
	document.forms[0].total.value = total;
}


function checkFrameStatus()
{
	//var src = top.frames["virginFrame"].document.innerHTML;
	//alert(src);
}


// ===================================
// VIRGIN FLIGHT BOOKING COUNTDOWN CODE

var totalTime = 600000;
var g_countdownField;

function setCountdownField(countdownField)
{
	g_countdownField = countdownField;
}

function showCountdown()
{
	if (g_countdownField)
	{
		countdown();
	}
}

function updateCountdown()
{
	var time = totalTime;
	var mins = (time - (time % 60000)) / 60000;
	var time = time - (mins * 60000);
	var secs = (time - (time % 1000)) / 1000;
	var out = "";
	if(mins < 10) out += "0";
	out += mins+":";
	if(secs < 10) out += "0";
	out += secs;
	g_countdownField.value = out;
}

function countdown()
{
	if (totalTime > 0)
	{
		updateCountdown();
		totalTime = totalTime - 1000;
		setTimeout("countdown()",1000);
	}
	else
	{
		updateCountdown();
		
		// the showCountdownError function is implemented
		// in the XSLT to take advantage of the I18N
		// functionality.
		showCountdownError();
	}
}

// ===================================
// Company Employees Functions
/**
 * EVENT HANDLER : Is Travel Arranger Checkbox
 * when travel arranger is checked, enables the email message field
 */
function travelArrangerChanged(form)
{
	// check if create profile is checked
	if (areEmployeeCheckboxesInvalid(form))
	{
		alert(getCreateTravelArrangerError());
		toggleTravelArranger(form);
		return;
	}
	updateTravelArrangerFields(form);
}

/**
 * EVENT HANDLER : Create Profile Checkbox
 * when create profile is checked, enables the username fields
 */
function createProfileChanged(form)
{
	// check if travel arranger is checked
	if (areEmployeeCheckboxesInvalid(form))
	{
		alert(getCreateTravelArrangerError());
		toggleCreateProfile(form);
		return;
	}
	updateCreateProfileFields(form);
}

function areEmployeeCheckboxesInvalid(form)
{
	return form.travelArranger.checked && form.createProfile.checked;
}

function toggleTravelArranger(form)
{
	form.travelArranger.checked = !form.travelArranger.checked;
	updateTravelArrangerFields(form);
}

function toggleCreateProfile(form)
{
	form.createProfile.checked = !form.createProfile.checked;
	updateCreateProfileFields(form);
}

/** update travel arranger fields worker */
function updateTravelArrangerFields(form)
{
	form.message.disabled = !form.travelArranger.checked;
}

/** update create profile fields worker */
function updateCreateProfileFields(form)
{
	form.username.disabled = !form.createProfile.checked;
	form.assignedTravelArranger.disabled = !form.createProfile.checked;
	form.mobile.disabled = !form.createProfile.checked;
	form.frequentFlyerNumber.disabled = !form.createProfile.checked;
	form.driversLicenceState.disabled = !form.createProfile.checked;
	form.driversLicence.disabled = !form.createProfile.checked;
}

// ===================================
// Supplier Guest List Functions
var numFields;
function toggleSelectFields()
{
	var i;
	var checked = document.forms[searchFormName].elements['selectAll'].checked;
	for (i=0; i < numFields; i++)
	{
		document.forms[searchFormName].elements['selected'+i].checked = checked;
		document.forms[searchFormName].elements['approvedBy'+i].disabled = !checked;
		document.forms[searchFormName].elements['confirmNumber'+i].disabled = !checked;
		document.forms[searchFormName].elements['notes'+i].disabled = !checked;
	}
}

/**
 * @param index is the index of the selected, approvedBy, confirmNumber and notes fields
 */
function selectFieldChanged(index)
{
	var checked = !document.forms[searchFormName].elements['selected'+index].checked;
	document.forms[searchFormName].elements['approvedBy'+index].disabled = checked;
	document.forms[searchFormName].elements['confirmNumber'+index].disabled = checked;
	document.forms[searchFormName].elements['notes'+index].disabled = checked;
}

function initialiseGuestListFields()
{
	var i;
	var checked;
	for (i=0; i < numFields; i++)
	{
		checked = !document.forms[searchFormName].elements['selected'+i].checked;
		document.forms[searchFormName].elements['approvedBy'+i].disabled = checked;
		document.forms[searchFormName].elements['confirmNumber'+i].disabled = checked;
		document.forms[searchFormName].elements['notes'+i].disabled = checked;
	}
}

function cancelSegment(instance)
{
	if (confirm(document.forms[searchFormName].cancelMessage.value))
	{
		document.forms[searchFormName].segmentInstance.value=instance;
		document.forms[searchFormName].operation.value='cancelSegment';
		
		document.forms[searchFormName].submit();
	}
}

function changeSegment(instance, showConfirm)
{
	if (showConfirm != 'yes' || (showConfirm == 'yes' && confirm(document.forms[searchFormName].changeMessage.value)))
	{
		document.forms[searchFormName].segmentInstance.value=instance;
		document.forms[searchFormName].operation.value='changeFlightSegment';
		document.forms[searchFormName].submit();
	}
}

function isValidDate(date)
{
	var dateRE = /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}/;
	
	var match = "" + date.match(dateRE);
	if (match != date) 
	{
		return false;
	}
	
	var pieces = date.split('/');
	if (eval(pieces[0]) > 31 || eval(pieces[1]) > 12)
	{
		return false;
	}
	
	return true;
}

function isValidName(name, nameRE)
{
	if (nameRE == null) {
		nameRE = /[a-zA-Z\'\s]+/;
	}
		
	var match = "" + name.match(nameRE);
	if (match != name) 
	{
		return false;
	}
	return true;
}

function isBlank(name)
{
	if (document.forms[searchFormName].elements[name].value == '')
		return true;
	
	return false;
}

/* Checks whether the email entered is "your@email.com" in the newsletter. 
 * If it is, it will set the field to nothing.
 */
function checkNewsletter(name)
{
	if (document.forms[searchFormName].elements[name])
	{
		if (document.forms[searchFormName].elements[name].value == 'your@email.com')
		{
			return document.forms[searchFormName].elements[name].value = '';
		}
		return document.forms[searchFormName].elements[name].value;
	}
	return false;
}

function trim(string) 
{
	// remove leading spaces

	while(string.substr(0,1)==" ")

		string = string.substring(1,string.length) ;

	// remove trailing spaces 

	while(string.substr(string.length-1,1)==" ")

		string = string.substring(0,string.length-2) ;
	return string;
}


function showdiv(id) 
{
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function hidediv(id) 
{
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function get_radio_value(rButton)
{
	for (var i=0; i < rButton.length; i++)
	{
   		if (rButton[i].checked)
      		{
      			return rButton[i].value;
      		}
   	}
}

