
YAHOO.util.Event.onDOMReady(function() {

    var inTxt = MyGetElementById("DepartDate"),
        outTxt = MyGetElementById("ReturnDate"),
        inDate, outDate, interval;

    var minDate =new Date();
    minDate.setDate(minDate.getDate()+1);
    var cal = new YAHOO.example.calendar.IntervalCalendar("cal1Container", {pages:2, mindate: minDate });

    cal.selectEvent.subscribe(function() {
        interval = this.getInterval();

	if (interval.length > 0) {
                inDate = interval[0];

                inTxt.value = dateToDMY(inDate);
                inTxt.style.color = 'black';

                if (interval.length == 2) {
                    outDate = interval[1];
                    outTxt.value = dateToDMY(outDate);
                    outTxt.style.color = 'black';
                } else {
                    outTxt.value = "Enter D/M/Y (Leave blank for one-way)";
                    outTxt.style.color = 'gray';
                }
            }

    }, cal, true);
    
    cal.render();
});


var InternationalSelected;
InternationalSelected = false;


function TabSelect(sTabName) {

var ctlTab = MyGetElementById('widgettab');

ctlTab.style.backgroundImage='url(pics/' + sTabName + 'selected.jpg)';

MyGetElementById('widgetframe_domestic').style.display='none';
MyGetElementById('widgetframe_international').style.display='none';
MyGetElementById('widgetframe_hotels').style.display='none';
MyGetElementById('widgetframe_carhire').style.display='none';
MyGetElementById('widgetframe_' + sTabName).style.display='';

if (sTabName=='international')
	{
	//if ($("*[id$='iwtfwidget_inpInternationalOrigin']").val()=='' && SelectedValue('strXXOrigin')!='-0')
	//	$("*[id$='iwtfwidget_inpInternationalOrigin']").val($("*[id$='strXXOrigin']").val() + ' (' + SelectedValue('strXXOrigin').replace('#','') + ')');

	if (InternationalSelected==false)
		{


		InternationalSelected = true;
        	 var int3 = $("select[id$='iwtfwidget_intXXAdults']").removeAttr("disabled").ufd({ skin: "sexy", addEmphasis: true, homeEndForCursor: true });
        	 var int4 = $("select[id$='iwtfwidget_intXXChildren']").removeAttr("disabled").ufd({ skin: "sexy", addEmphasis: true, homeEndForCursor: true });
        	 var int5 = $("select[id$='iwtfwidget_intXXinfants']").removeAttr("disabled").ufd({ skin: "sexy", addEmphasis: true, homeEndForCursor: true });
        	 var int6 = $("select[id$='iwtfwidget_strXXClass']").removeAttr("disabled").ufd({ skin: "sexy", addEmphasis: true, homeEndForCursor: true });
		}
	}
}

// Start Initialise values

$(document).ready(function () {

//Do not select the tabs if they are not on the page (eg - any page other than the homepage)
if (MyGetElementById('widgettab'))
	TabSelect('domestic');

if (MyGetElementById('DepartDate').value=='')
	{
	MyGetElementById('DepartDate').value="Enter D/M/Y or Click Calendar Below";
	MyGetElementById('DepartDate').style.color='gray';
	}	
if (MyGetElementById('ReturnDate').value=='')
	{
	MyGetElementById('ReturnDate').value="Enter D/M/Y (Leave blank for one-way)";
	MyGetElementById('ReturnDate').style.color='gray';
	}	


if (MyGetElementById('strXXOrigin').options[0].selected == true)
	LoadPreviousSearches();

	var s1 =$("select[id$='_strXXOrigin']").removeAttr("disabled").ufd({skin:"sexy", log: true, addEmphasis: true, homeEndForCursor: true});
	var s2 =$("select[id$='_strXXDestination']").removeAttr("disabled").ufd({skin:"sexy", log: true, addEmphasis: true, homeEndForCursor: true});
	var s3 =$("select[id$='_intXXAdults']").removeAttr("disabled").ufd({skin:"sexy", log: true, addEmphasis: true, homeEndForCursor: true});
	var s4 =$("select[id$='_intXXChildren']").removeAttr("disabled").ufd({skin:"sexy", log: true, addEmphasis: true, homeEndForCursor: true});
	var s5 =$("select[id$='_intXXInfants']").removeAttr("disabled").ufd({skin:"sexy", log: true, addEmphasis: true, homeEndForCursor: true});


});

// End Initialise values

function SelectSelectedIndexSet(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}

function LoadPreviousSearches()
{

ControlValueFromCookieSet("dtDateDepartureFrom", 'DepartDate')
ControlValueFromCookieSet("dtDateReturnFrom", 'ReturnDate')

ControlValueFromCookieSet("sOriginLocCode", 'strXXOrigin');
ControlValueFromCookieSet("sDestLocCode", 'strXXDestination');
ControlValueFromCookieSet("iAdults", 'intXXAdults');
ControlValueFromCookieSet("iChildren", 'intXXChildren');
ControlValueFromCookieSet("iInfants", 'intXXInfants');
ControlValueFromCookieSet("eOutputFormat", 'hdnOutputFormat');

//International tab values
if ($("*[id$='iwtfwidget_OtherAirlineZuji']").length>0)
	{
	ControlValueFromCookieSet("iwtfwidget_OtherAirlineZuji", 'iwtfwidget_OtherAirlineZuji')
	ControlValueFromCookieSet("iwtfwidget_OtherAirlineExpedia", 'iwtfwidget_OtherAirlineExpedia')
	$("*[id$='iwtfwidget_inpInternationalOrigin']").val(GetCookie('LastSearch_iwtfwidget_inpInternationalOrigin'));
	$("*[id$='iwtfwidget_inpInternationalDestination']").val(GetCookie('LastSearch_iwtfwidget_inpInternationalDestination'));
	//$("*[id$='iwtfwidget_DepartDate']").val(GetCookie('LastSearch_iwtfwidget_DepartDate'));
	//$("*[id$='iwtfwidget_ReturnDate']").val(GetCookie('LastSearch_iwtfwidget_ReturnDate'));
	ControlValueFromCookieSet("iwtfwidget_intXXAdults", 'iwtfwidget_intXXAdults')
	ControlValueFromCookieSet("iwtfwidget_intXXChildren", 'iwtfwidget_intXXChildren')
	ControlValueFromCookieSet("iwtfwidget_intXXInfants", 'iwtfwidget_intXXInfants')
	ControlValueFromCookieSet("iwtfwidget_strXXClass", 'iwtfwidget_strXXClass')
	}

}

function ControlValueFromCookieSet(sCookieName, sControlName)
{
var sValue;
var oControl = MyGetElementById(sControlName);

sCookieName = 'LastSearch_' + sCookieName;

sValue=GetCookie(sCookieName)

if (sValue==null || sValue=='')
	return;

if (sControlName.indexOf("Date")>-1)
	{
	var myDate = new Date(sValue);
	oControl.value = dateToDMY(myDate);
	oControl.style.color='black';
	}
else if (sControlName.indexOf("hdnOutputFormat")>-1)
	{
	$('[name="hdnOutputFormat" ][value="' + sValue + '"]').attr('checked', true);
	}
else if (sControlName.indexOf("iwtfwidget_OtherAirline")>-1)
	{
	oControl.checked = (sValue === 'true');
	}
else
	{
	SelectSelectedIndexSet(oControl,sValue);
	}
}



function SearchForFlightsClicked()
{

	if (MyGetElementById("chkShowEventsInCalendar").checked==true)
		{
		CheckFBLoggedIn(); 
		return false;
		}
	else
		{
		MySubmitForm(); return true;
		}
}

function MySubmitForm()
{
$('#aSubmit').replaceWith("<div id='aSubmit' style='color:gray'>Searching....</div>");

var myForm = document.forms['aspnetForm'];
if (!myForm) 
	{    myForm = document.aspnetForm;}
myForm.submit();
}
