function pen_enable_book()
{
}

function pen_disable_book()
{
}

function setDisplay(inElement, inValue)
{
    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (element != null)
	element.style.display = inValue;
}

function setDisabled(inElement, inValue)
{
    var elem = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    elem.disabled = inValue;
}

function ttAddEvent(inElement, inEventName, inFuncName)
{
    if (inElement == null || inEventName == null || inFuncName == null)
	return;

    if (inElement.ttAddEventListener)						
	inElement.ttAddEventListener(inEventName, inFuncName, true);
    else if (inElement.attachEvent)						
	inElement.attachEvent("on" + inEventName, inFuncName);
}

function ttRemoveEvent(inElement, inEventName, inFuncName)
{
    if (inElement == null || inEventName == null || inFuncName == null)
	return;

    if (inElement.ttRemoveEventListener)				
	inElement.ttRemoveEventListener(inEventName, inFuncName, true);
    else if (inElement.detachEvent)					
	inElement.detachEvent("on" + inEventName, inFuncName);
}

/**
 * Retrieve booking from CDP
 * 
 * @param {Object} inEmailElemId
 * @param {Object} inBkgNumberElemId
 */
function retrieveBooking(inEmailElemId, inBkgNumberElemId)
{
	var emailElem     = document.getElementById(inEmailElemId);
	var bkgNumberElem = document.getElementById(inBkgNumberElemId);
	
	if (emailElem == null || emailElem.value == null || emailElem.value.length == 0)
	{
		alert("Please enter you email address.");
		return false;		
	}
	
	if (bkgNumberElem == null || bkgNumberElem.value == null || bkgNumberElem.value.length == 0)
	{
		alert("Please enter the booking number.");
		return false;
	}
	
	var email     = emailElem.value;
	var bkgNumber = bkgNumberElem.value;
	
	var bkgNumArray = bkgNumber.split("/");
	
	if (bkgNumArray == null || bkgNumArray.length < 2)
	{
		alert("Please enter the booking number as it appears on your booking advice.");
		return false;
	}
	package.retrieveBooking(bkgNumArray[1], email);
}

/**
 * Toggle the hotel information section - hide / unhide.
 * <p>
 * @param {Integer} inOption, the option to toggle
 * @param {Integer} inHtlCount, the number of hotels on the page
 */
 function toggleHtlDisplay(inOption, inHtlCount)
{
	 var hide = false;
	 
	 if (document.getElementById("hlcontent" + inOption).style.display == "block")
	 	hide = true;
	 
	 for (var i = 1; i <= inHtlCount; i++) 
	 {
	 	 if (!document.getElementById("hlcontent" + i))
	 	 	continue;
	 	 	
		 document.getElementById("hlcontent" + i).style.display = "none";			
		 var htId = document.getElementById("hotelheader" + i);
		 htId.className = htId.className.replace(new RegExp(" tt_down\\b"), "");		
	 }
	 
	 // If we want to display the current option, show it now.
	 if (!hide)
	 {
	 	document.getElementById("hlcontent" + inOption).style.display = "block";
	 	document.getElementById("hotelheader" + inOption).className+=" tt_down";	
		loadImagesForDiv(inOption);	
	 }	 
}		

function ttDisplaySupLightBox(inDivId)
{
	lightBox(inDivId);
	loadImagesForDiv(inDivId);
}
 
/**
 * This function will load the images for this div including those in the hidden hotel description light box
 * @param {Object} inOption
 */
function loadImagesForDiv(inDivId)
{
	var contentElem = document.getElementById(inDivId, '_imageHolder');
	var imageElems = contentElem.getElementsByTagName("img");
		
	for (var i = 0; i < imageElems.length; i++)
	{
		// If src isnt the pixel image, we dont want to do anything 
		if (imageElems[i].src.indexOf('trans_px.gif') == -1)
			continue;


		var imageElemIdArray = imageElems[i].id.split('_');
		var postFix = imageElemIdArray[imageElemIdArray.length - 1];
		var urlElem = document.getElementById('htlImgUrl_' + postFix);
		imageElems[i].src = "/static/tsg/supub/S/images/H/" + urlElem.innerHTML.replace(' ', '');
			
		document.getElementById('htlImgLabel' + '_' + postFix).style.display = "block";
	}
}			

function killEvent(inEvent)
{
    var event = (!inEvent) ? window.event : inEvent;

    if (event.stopPropagation)
    {
	event.preventDefault();
	event.stopPropagation();
    }
    else
    {
	event.returnValue  = false;
	event.cancelBubble = true;
    }
}

function toggleSelectElemDisplay(inBrowseElem, inLeft, inTop)
{
    var selectElemArray = document.getElementsByTagName('select');

    if (selectElemArray.length == 0)
	return;

    var browse = (typeof inBrowseElem == 'object' ? inBrowseElem : document.getElementById(inBrowseElem));


    if (!isDisplayed(browse))
    {
	for (var i = 0; i < selectElemArray.length; i++)
	    selectElemArray[i].style.visibility = 'visible';

	return;
    }

    var bl = inLeft;			
    var br = bl + browse.offsetWidth;	
    var bt = inTop;		
    var bb = bt + browse.offsetHeight;		

    for (var i = 0; i < selectElemArray.length; i++)
    {
	var e = selectElemArray[i];

	if (e.style.visibility == 'hidden')
	    continue;

	var p = findAbsolutePos(e);

	var el = p.left;			
	var er = el + e.offsetWidth;
	var et = p.top;		
	var eb = et + e.offsetHeight;

	if (el <= br && er >= bl && et <= bb && eb >= bt)
	    e.style.visibility = 'hidden';
    }
}

function showWaitNotice()
{
    if (document.getElementById('tt_waitNotice') == null)
	return;

    if (mShowStartPkgWait == true && document.getElementById('startWaitNotice') != null)
    {
		setDisplay('startWaitNotice', '');
    }
    else
    {
		createbgDiv();//defined in lightbox.js
		setDisplay('tt_bgDiv', 'block');
		setDisplay('tt_ieFix', 'block');
		setDisplay('tt_waitNotice', 'block');
		positionWaitNotice();
		opacity();
		window.onscroll = positionWaitNotice;
    }
    
    ttAddEvent(document, "click",	   cancelWaitNotice);
    ttAddEvent(document, "keypress", cancelWaitNotice); 	
    ttAddEvent(document, "keydown",  cancelWaitNotice); 

    var inputElements = document.getElementsByTagName('input');

    for (var i = 0; i < inputElements.length; i++)
    {
	if (inputElements[i].type == 'button' || inputElements[i].type == 'submit')
	    setDisabled(inputElements[i], true);
    }
}

function positionWaitNotice()
{	
	
	var waitWindow = document.getElementById('tt_waitNotice');
	var topDiv = document.getElementsByTagName('div')[0]; //picks up the first div of the page
    topDiv.parentNode.insertBefore(waitWindow, topDiv);
	waitWindowTop = (waitWindow.offsetHeight)/2*-1;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		/*create hack to look like fixed position for ie6*/
		waitWindow.style.position = 'absolute';
		var scrollTop=document.documentElement.scrollTop;
		waitWindow.style.top = ((scrollTop + ((document.documentElement.clientHeight/2) + waitWindowTop))) + 'px';
	} else {
		waitWindow.style.position = 'fixed';
		waitWindow.style.marginTop = waitWindowTop + 'px';
	}
}

function scrollToElement(inElementId)
{
    var elem = document.getElementById(inElementId);

    var leftPos = 0;
    var topPos  = 0;

    //Find the position of the element 
    while (elem != null)
    {
	leftPos += elem.offsetLeft;
	topPos  += elem.offsetTop;
	elem    = elem.offsetParent;
    }
	
    window.scrollTo(leftPos, topPos);
}

function cancelWaitNotice(inEvent)
{
    var event = (!inEvent) ? window.event : inEvent;

    if (event.keyCode == 27)
    {
	hideWaitNotice();
	return(true);
    }

    killEvent(event);
    return(false);
}

function hideWaitNotice()
{
    setDisplay('tt_bgDiv', 'none');
	setDisplay('tt_ieFix', 'none');
	var waitWindow;
    
	waitWindow = document.getElementById('tt_waitNotice');  
	
    if (waitWindow == null)
	return;

    setDisplay(waitWindow, 'none');
	
    ttRemoveEvent(document, "click",    cancelWaitNotice);
    ttRemoveEvent(document, "keypress", cancelWaitNotice); 
    ttRemoveEvent(document, "keydown",  cancelWaitNotice); 
    
    var inputElements = document.getElementsByTagName('input');

    for (var i = 0; i < inputElements.length; i++)
    {
	if (inputElements[i].type == 'button' || inputElements[i].type == 'submit')
	    setDisabled(inputElements[i], false);
    }
    
    toggleSelectElemDisplay(waitWindow, 0, 0);
}

function getBrowserGeometry()
{
    var geometry = new Array(4);

    geometry['top']    = window.scrollY	    ? window.scrollY	 : document.body.scrollTop;
    geometry['left']   = window.scrollX	    ? window.scrollY	 : document.body.scrollLeft;
    geometry['width']  = window.innerWidth  ? window.innerWidth	 : document.body.clientWidth;
    geometry['height'] = window.innerHeight ? window.innerHeight : document.body.clientHeight;

    return(geometry);
}

function findAbsolutePos(inElement)
{
    var elem = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (elem == null)
	return(false);

    var left = 0;
    var top  = 0;

    for (; elem.offsetParent; elem = elem.offsetParent)
    {
	left += elem.offsetLeft;
	top  += elem.offsetTop;
    }

    return(new coordinate(left, top));
}

function coordinate(inLeft, inTop)
{
    this.left = inLeft;
    this.top  = inTop;
}

function isDisplayed(inElement)
{
    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (element != null)
    {
	if (element.style.display != 'none')
	    return(true);
    }

    return(false);
}

/*
 * This function will be called when a flight is selected to disable all radio buttons that allow selection of the other carrier, in
 * the other direction.
 * This function is written with the assumption that a radio button can never be unselected.
 */ 
function toggleShownFlightOptions(inActiveCarrier, inDirection)
{
    var rowElements = document.getElementsByTagName('tr');

    for (var i = 0; i < rowElements.length; i++)
    {
	/* The row element id is in the form <Carrier>_<Direction>_<number> */ 
	var elemIdArr = rowElements[i].id.split('_');

	if (elemIdArr.length < 3)
	    continue;
	
	var carrier   = elemIdArr[0];
        var direction = elemIdArr[1];
	
	// If we are in the same direction as the active radio button dont do anything
	if (direction == inDirection)
	    continue;
	
	// If we are in the opposite direction, with the same carrier, ensure we are visible, otherwise ensure we are hidden.
	if (carrier == inActiveCarrier)
	{
	    setDisplay(rowElements[i], '');
	}
	else
	{
	    setDisplay(rowElements[i], 'none');
	    setDisplay(document.getElementById('showallButton'), '');
	}
    }

    // Ensure nothing that is hidden is selected, if it is, unselect it

    var radioElements = document.getElementsByTagName('input');

    for (var j = 0; j < radioElements.length; j++)
    {
	if (radioElements[j].type != 'radio' || !radioElements[j].checked)
	    continue;
	
	/* The row element id is in the form radio_<Carrier>_<Direction>_<number> */ 
	var elemIdArr = radioElements[j].id.split('_');

	if (elemIdArr.length < 4)
	    continue;
	
	var carrier   = elemIdArr[1];
        var direction = elemIdArr[2];
	
	// If we are in the same direction as the active radio button dont do anything
	if (direction == inDirection)
	    continue;
	
	// If we are in the opposite direction, with the same carrier, ensure we are visible, otherwise ensure we are hidden.
	if (carrier != inActiveCarrier)
	    radioElements[j].checked = false;
    }
}

function showAllFlightOptions()
{
    var rowElements = document.getElementsByTagName('tr');

    for (var i = 0; i < rowElements.length; i++)
    {
	/* The row element id is in the form <Carrier>_<Direction>_<number> */ 
	var elemIdArr = rowElements[i].id.split('_');

	if (elemIdArr.length < 3)
	    continue;
	
	setDisplay(rowElements[i], '');
    }

    // Ensure nothing that is hidden is selected, if it is, unselect it

    var radioElements = document.getElementsByTagName('input');

    for (var j = 0; j < radioElements.length; j++)
    {
	if (radioElements[j].type != 'radio' || !radioElements[j].checked)
	    continue;
	
	/* The row element id is in the form radio_<Carrier>_<Direction>_<number> */ 
	var elemIdArr = radioElements[j].id.split('_');

	if (elemIdArr.length < 4)
	    continue;
	
	radioElements[j].checked = false;
    }
    
}

function editComp(){
	scrollToElement('componentWorkingSection');
	if (document.getElementById("hotelError")){
		document.getElementById("hotelError").style.display="none";
	
	}
}

function updateComponentAndBookWrapper(inComponentId, inRequirementFieldId, inRequirementValue)
{
	updateMsg("updateBooking");
	package.updateComponentAndBook(inComponentId, inRequirementFieldId, inRequirementValue);
	scrollToElement('dynamicPackage');
}

function showPackageWrapper()
{
	updateMsg("returnToSearch");
	package.showPackage();
	scrollToElement('dynamicPackage');
}

function updateMsg(inMsgId)
{
	var waitMsg = document.getElementById("waitMsg");
  
	var waitNotices = new Array();
	waitNotices["initialWaitNotice"] = "We're checking hotel availability and finding the best prices ...";
	waitNotices["updateBooking"] = "Please wait while we confirm your booking ...";
	waitNotices["returnToSearch"] = "Please wait while we return you to the selection screen";
	waitNotices["finaliseBooking"] = "We are currently processing your confirmation and payment.<br/>" +
	 							     "Please do not press the Back button or Refresh your screen.<br/>" + 
									 "Thank you for your booking.";

  
	if (waitMsg)
	{
		var msg =  waitNotices[inMsgId] ? waitNotices[inMsgId] : "Please wait ...";
	    waitMsg.innerHTML = waitNotices[inMsgId];
	}
}

/*
 * Function ensures the conditions are acknowledged before the user is allowed to submit.
 */ 
function validateBookingForm(inErrorDiv)
{
	var isMissingField = false;
    
	document.getElementById('javascriptError').style.display = "none";
	document.getElementById('javascriptError').innerHTML = "";
	
	var errorDiv = document.getElementById(inErrorDiv);
	if (errorDiv)
	    document.getElementById(inErrorDiv).style.display = "none";	
	
	if(!fieldSpecifiedCorrectly('state', 'State')) 
		isMissingField = true;
	
    if(!fieldSpecifiedCorrectly('town', ' Town')) 
		isMissingField = true;
		
	if(!fieldSpecifiedCorrectly('postcode', 'Postcode')) 
		isMissingField = true;
    
    if(!fieldSpecifiedCorrectly('firstAddressLine', 'First Street Address Line')) 
		isMissingField = true;
	
	if (errorDiv) 
	{	
		if ((document.getElementById('accept1') && !document.getElementById('accept1').checked) ||
			(document.getElementById('accept2') && !document.getElementById('accept2').checked)) {
			document.getElementById(inErrorDiv).style.display = 'none';
			document.getElementById(inErrorDiv).style.display = '';
            scrollToElement('fareRulesErrorMsg');
			return;
		}
	}	

	if (isMissingField) return;	
	
	concatFields();
	scrollToElement('dynamicPackage');	
	updateMsg("finaliseBooking");
    package.completeBooking('passengerDetailsForm');
}

function concatFields()
{	
	var fields = '';
	fields += document.getElementById('town').value + ',';
	fields += document.getElementById('state').value + ',';
	fields += document.getElementById('postcode').value;
	
	document.getElementById("hiddenAddressLine").value = fields;
} 

function fieldSpecifiedCorrectly(inId, inDescription){
	var field = document.getElementById(inId);

	if (field && field.value.length == 0)
	{
		document.getElementById('javascriptError').innerHTML += "Must enter the " + inDescription + ".<br/>";
		document.getElementById('javascriptError').style.display = "block";
		return(false);
	}

	if (field.value.indexOf(',') >= 0)
	{
		document.getElementById('javascriptError').innerHTML += "Cannot enter a comma (,) in the field " + inDescription + ". <br/>";
		document.getElementById('javascriptError').style.display = "block";
		return(false);
	}
	
	return(true);
}

function displayChildAgeBoxes(numChildFld, divName, totNum)
{
    var numChild = document.getElementById(numChildFld).value;
    document.getElementById(divName).style.display = numChild > 0 ? "" : "none";
    for (i=1; i<=numChild; i++)
        document.getElementById(divName + i).style.display = "";
    for (; i<=totNum; i++)
        document.getElementById(divName + i).style.display = "none";
}

function getRoomMixes()
{
    var roomMixes = "";
	var adultCount = document.getElementById("numberAdults").value;
	var childCount = document.getElementById("numberChildren").value;
	
	if (adultCount == 0) return null;
	
	roomMixes = adultCount + "A";
	
	if (childCount > 0)
	{
		roomMixes += childCount + "C";
		
		for (i=1; i<= childCount; i++)
		{
			roomMixes += "-" + document.getElementById("childAge" +  i).value ;
		}
	}

	return(roomMixes);
}

function constructLink(inLinkElem, inBaseValue)
{
	var startDate = document.getElementById("startDate").value;
	var duration = document.getElementById("duration").value;
	var includeCot = document.getElementById("includeCot");
	var roomMixes = getRoomMixes();
	var brand = document.getElementById("brand").value;
	var link = "cdp_fit.html?" + inBaseValue;
	
	if (!startDate || !duration || !includeCot || !roomMixes || !brand) {
		return (false);
	}
		
	link = link + "&brand=" + brand + "&CDP_startDate=" + startDate + "&CDP_duration=" + duration + "&CDP_includeCot=" + includeCot.checked + "&CDP_roomMixes=" + roomMixes;

	inLinkElem.href = link;
	
	return(true);

}

/**
* This function will call the 'loadbranding' javascript function if one exsits.
* The 'loadbranding' javascript function is the function implemented by our customers if they wish to add behaviour to the pages
* 'onload' event when using dcdp branding.
*/
function onBodyLoadLoadExternalBranding()
{
    if (typeof loadbranding == 'undefined')
      return;
      
    loadbranding();
}

//This function separates the logic of registering event hooks from the code that is run
//every time a search request is made for a new package (i.e. run()). Otherwise the events will be
//registered multiple times.

function initialiseCPackageAndEventHooks(inCdpLocation)
{
	package = new CPackage(inCdpLocation);
	package.registerEventHook("pageAltered", createToolTips);
	package.registerEventHook("packageBooked", trackingCall.createCdpTrackingCall);
 
	return(package);
}

function createToolTips(inEventId, inData)
{
  $j("a.tipz").tooltip({ 
	  	showURL: false, 
	  	bodyHandler: function() { 
        	return $j("#" + $j(this).attr("id").split('_')[0] + "_div").html(); 
      	}
  });
} 

function openEnqWindow(inUrl)
{
    window.open(inUrl,'_blank', 'width=750,height=600,status=0,toolbar=0,location=0,menubar=0,resizeable=1,scrollbars=1');
    return false;
}

function toggleExtraRooms(inRowClassToToggle, inShowButtonId)
{
	var roomRows = $j("tr." + inRowClassToToggle);
	var display = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) ) ? 'block' : 'table-row';
	for ( var i=0; i < roomRows.length; i++ )
	{
		roomRows[i].style.display = (roomRows[i].style.display != 'none' ? 'none' : display);
	}
	
	var showButton = document.getElementById(inShowButtonId);
	if (showButton.className != 'tt_hide')
	{
		showButton.className = 'tt_hide';
	} 
	else
	{
		showButton.className = '';
	}
}

function changeNextPage(inNextButtonId, inPrevButtonId)
{
	document.getElementById(inNextButtonId).className = 'tt_button_nextdays_hide';

	var page1_td_values = $j("td.tt_price_day");
	var page1_th_values = $j("th.tt_price_day");
	var page2_td_values = $j("td.tt_price_day_hide");
	var page2_th_values = $j("th.tt_price_day_hide");

	for ( var i=0; i < page1_td_values.length; i++ )
	{
		page1_td_values[i].className = 'tt_price_day_hide';
	}
	for ( var j=0; j < page1_th_values.length; j++ )
	{
		page1_th_values[j].className = 'tt_price_day_hide';
	}
	
	for ( var k=0; k < page2_td_values.length; k++ )
	{
		page2_td_values[k].className = 'tt_price_day';
	}
	for ( var m=0; m < page2_th_values.length; m++ )
	{
		page2_th_values[m].className = 'tt_price_day';
	}
	
	document.getElementById(inPrevButtonId).className = 'tt_button_prevdays';
}

function changePrevPage(inPrevButtonId, inNextButtonId)
{
	document.getElementById(inPrevButtonId).className = 'tt_button_prevdays_hide';
	
	var page1_td_values = $j("td.tt_price_day_hide");
	var page1_th_values = $j("th.tt_price_day_hide");
	var page2_td_values = $j("td.tt_price_day");
	var page2_th_values = $j("th.tt_price_day");
	
	for ( var i=0; i < page1_td_values.length; i++ )
	{
		page1_td_values[i].className = 'tt_price_day';
	}
	for ( var j=0; j < page1_th_values.length; j++ )
	{
		page1_th_values[j].className = 'tt_price_day';
	}
	
	for ( var k=0; k < page2_td_values.length; k++ )
	{
		page2_td_values[k].className = 'tt_price_day_hide';
	}
	for ( var m=0; m < page2_th_values.length; m++ )
	{
		page2_th_values[m].className = 'tt_price_day_hide';
	}
	
	document.getElementById(inNextButtonId).className = 'tt_button_nextdays';
}

