webMatchHost = window;

// These run in the context of the web match IFrame
function launchAudio(url)
{
    window.open(url);
}
//Called from WebMatch in Search Results
function launchMoreInfo(url)
{
    var validUrl = url;
    
    if (url.indexOf("[MDF]") > -1)
    {
        validUrl= validUrl.replace("[MDF]","");
        oMatrixTop = document.getElementById('mi_top_searchcontrols');
		
		if (oMatrixTop != undefined) 
		{ 
            showMDFTab('GRID');//20/2/2008
        }
    }
   
    if (validUrl.length > 3)
    {
	    if (validUrl.indexOf("|") > -1)
	    {
		    var href = validUrl.substr(0,url.indexOf("|"));
		    var args = validUrl.substr(url.indexOf("|")+1);
		    window.open(href, '_blank', args);
	    }
	    else
	    {
		    window.open(validUrl, '_blank','scrollbars=yes,width=640,height=480,resizable=yes');
	    }
	}
}

function selectCarriers(carriers)
{
    if (window.webMatchHost.selectCarriersInHost)
    {
        webMatchHost.selectCarriersInHost(carriers);
    }
    //alert('Selected carriers: ' + carriers);
}

function hideWebMatch()
{
    //alert(webMatchHost.ControllerForm.closeWebMatch);
    if (webMatchHost.closeWebMatch)
    {
        webMatchHost.closeWebMatch();
    }
}

var trans=0;
var mainImage;
var mainImageFadeOpacity;
var mozillaTimerHandle;
function fadeInImage()
{
    if (parent.window.ShowWebMatch)
    {
        parent.window.ShowWebMatch();
    }


    mainImage = document.getElementById("imgAdvert");
    if (mainImage != null)
    {
        if (mainImage.filters)
        {
            mainImage.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=10)";
            mainImage.filters[0].Apply();
            mainImage.style.visibility = "visible";
            mainImage.filters[0].Play();
        }
        else
        {
            // Mozilla
            mainImageFadeOpacity=0;
            mainImage.style.opacity = 0;
            mainImage.style.visibility = "visible";
            mozillaTimerHandle = window.setInterval(mozillaFadeIn, 100);
        }
    }
}

function mozillaFadeIn()
{
    mainImageFadeOpacity += 1;
    mainImage.style.opacity = mainImageFadeOpacity / 100;
    if (mainImageFadeOpacity == 100)
    {
        window.clearInterval(mozillaTimerHandle);
    }
}


///////////////////////////////////////////////////////////////////////////////
// This runs in the context of the host page
///////////////////////////////////////////////////////////////////////////////
var webMatch_ctrl; // Set in hosting page
function completeSubmit()
{
    if (webMatch_ctrl.oldOldClick)
    {
        webMatch_ctrl.onclick = webMatch_ctrl.oldOldClick;
    }
    else
    {
        webMatch_ctrl.onclick = null;
    }
    webMatch_ctrl.click();
}

var wmFrame;
function webMatch_onButtonClick()
{
    wmFrame = document.all["ctlWebMatchFrame_Search"];
    if (wmFrame == null || !document.all)
    {
        // Don't perform WebMatch on search for Mozilla
        if (webMatch_ctrl.oldOnClick)
        {
            return webMatch_ctrl.oldOnClick();
        }
        else
        {
            return true;
        }
    }
    
    var searchDayField = document.getElementById("ContentView_PageLayout_FlightFinderTab_ctlDepartureDate_day");
    var searchMonthField = document.getElementById("ContentView_PageLayout_FlightFinderTab_ctlDepartureDate_month");
    var toCity =  document.getElementById("ContentView_PageLayout_FlightFinderTab_ctlDestinationFinder_city");
    var toCityCode = document.getElementById("ContentView_PageLayout_FlightFinderTab_ctlDestinationFinder_cityCode");
    // fromCity is drop down, toCity is text
    var fromCity = document.getElementById("ContentView_PageLayout_FlightFinderTab_ctlDeparting");
    
    window.setTimeout(completeSubmit, 100);

    if (!searchDayField || !searchMonthField || !toCity || !fromCity || !toCityCode
    || !searchDayField.value || !searchMonthField.value || !toCity.value || !fromCity.value)
    {
        return false;
    }
    
    var webmatchURL = "WebMatch/WebMatch.aspx?searchDate=" + searchMonthField.value.replace("/","-") + "-" + searchDayField.value + "&dep=" + fromCity.value + "&arr=" + escape(toCity.value) + "&arrCity=" + toCityCode.value + "&type=search";
    wmFrame.style.display = "block";
    wmFrame.src = webmatchURL;
    return false;
}

function ShowWebMatch()
{
    if (wmFrame)
    {
        wmFrame.style.visibility = 'visible';
    }
}

function closeWebMatch()
{
    var iFrame = document.getElementById("ContentView_PageLayout_FlightFinderTab_ctlWebMatchFrame_Results");
    if (iFrame)
        iFrame.style.display='none';

    
    // expand flight list
    var divFlightList = document.getElementById('ContentView_PageLayout_FlightFinderTab_ctlFare');
    if (divFlightList)
    {
        if (divFlightList.style.pixelWidth)
        {
            divFlightList.style.width = new String(divFlightList.style.pixelWidth + 110) + "px";
        }
        else
        {
            divFlightList.style.width = "630px"; 
        }
    }
    
}

function selectCarriersInHost(carriers)
{   
    if (!carriers)
        return;
        
    var carrierList = carriers.split(',');
    var aImages = document.getElementsByTagName("input");
    for (var i=0; i < aImages.length; i++)
    {
        var imageEl = aImages[i];
        // Find image buttons with /<carrier>.gif in the name
        if (imageEl.type == "image")
        {
            for (var k=0; k < carrierList.length; k++)
            {
                var fileName = "/" + carrierList[k].toUpperCase() + ".GIF";
                if (imageEl.src.toUpperCase().indexOf(fileName) > -1)
                {
                    // Make sure its not already selected
                    // see if the background colour of its gradparents are transparent...
                    var parent = imageEl.parentNode;
                    while (parent.tagName && parent.tagName.toUpperCase() != "DIV")
                    {
                        if (parent.bgColor)
                            return;
                            
                        parent = parent.parentNode;
                    }
                    
                    // No background color, so not selected.
                    imageEl.click();
                    return;
                }
            }
        }
    }
}
