﻿// ######################################## //
// ############### Template ############### //
var loginFailed;
var IsCompanyRewards;
var IsLoginPage;
//show or hide the search watermark  
function textboxWatermark(action, textbox)
{
   //get references to the search text box and the hidden field which has the "search flybmi.com" text from resource file   
   var textbox = document.getElementById(textbox);
   
   switch(action) 
   { 
       //adds the search flybmi.com watermark on the textbox if user clicks inside and no text has been entered
       case 'add':
            //if the textbox is empty, add the search flybmi.com text
            if (textbox.value == '') { textbox.value =  watermark; }
            break;         
       //clear the search flybmi.com watermark on the textbox if user clicks inside
       case 'remove':
            //if the textbox already has watermark text then clear it on click
            if (watermark == textbox.value) { textbox.value = ''; } 
            break;         
       //do nothing if not add or remove
       default:   
            break;
   }
}
// Change the CSS class of the given elements
function setCssClass() {
    var args=setCssClass.arguments;
    for(i=0; i<(args.length-1); i+=2)
    with (document) if (getElementById && ((obj=args[i])!=null)) { v=args[i+1];
    if (obj.className!=null) { obj.className=v;}}
}

//find relative x position of any object
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}
// Var to hold flyout open state
var languageFlyout = null;
var loginFlyout = null;
var accountFlyout = null;
// Shows/Hides the Flyout windows
function showHideFlyout(type, flyout, change, offset, login) {
    // ID of the relative position of a control where the animation starts from
    // this is needed since login/country language control are now in seperate controls
    var callingControlLinkID = null;

    switch (type) {
        // Show flyout 
        case 'show':
            switch (flyout) {
                case 'chg':
                    languageFlyout = true;
                    // If login flyout is open, hide it
                    if (loginFlyout) { showHideFlyout('hide', 'log'); }
                    if (accountFlyout) { showHideFlyout('hide', 'acc_det'); }
                    callingControlLinkID = masterPageID;
                    break;
                case 'log':
                    loginFlyout = true;
                    // If language flyout is open, hide it
                    if (languageFlyout) { showHideFlyout('hide', 'chg'); }
                    callingControlLinkID = loginUniqueID;
                    break;
                case 'acc_det':
                    accountFlyout = true;
                    if (languageFlyout) { showHideFlyout('hide', 'chg'); }
                    callingControlLinkID = 'acc_name';
                    // Change the arrow next to the name
                    toggleAccountArrow(true);
                    break;
            }
            // Get object "change" button link            
            var lnkChange = document.getElementById(callingControlLinkID + change);
            // If login flyout, modify logout link
            if (login && lnkChange.innerHTML == document.getElementById(loginUniqueID + 'HiddenLogOutText').value) {
                document.getElementById(loginUniqueID + 'hiddenLogOutClicked').value = true;
                document.forms[0].submit();
                return false;
            }
            // Find relative x position of change link and take away offset (~popup length)
            var relativePosition = findPosX(lnkChange) - offset;
            // Set position of the popup to the relative position 
            document.getElementById(flyout).style.left = relativePosition + 'px';
            // Use blind down effect script to show the popup
            //Effect.BlindDown(flyout, {duration: 0.5});    
            $("#" + flyout).slideDown("slow");

            break;
        // Hide flyout 
        case 'hide':
            //use blind up effect to hide the popup
            //Effect.BlindUp(flyout, {duration: 0.5}); 
            $("#" + flyout).slideUp("slow");
            switch (flyout) {
                case 'chg':
                    // Update as language flyout closed
                    languageFlyout = null;
                    break;
                case 'log':
                    // Update as login flyout closed
                    loginFlyout = null;
                    break;
                case 'acc_det':
                    // Update as account flyout closed
                    accountFlyout = null;
                    // Change the arrow next to the logged in name
                    toggleAccountArrow(false);
                    break;
            }
            break;
        default:
            languageFlyout = null;
            loginFlyout = null;
            break;
    }
}

// Checnge the orientation of the arrow next to the logged in as name
function toggleAccountArrow(open) {
    if (open)
        document.getElementById('acc_det_img').src = '/images.net/v2/global/template/flybmi/v7/Arrows/small_arrow_down.gif';
    else
        document.getElementById('acc_det_img').src = '/images.net/v2/global/template/flybmi/v7/Arrows/small_arrow_right.gif';
}


var CountryInfo = null;
// Country option changed, get languages
function CountryChanged()
{
    // Clear language list
    document.getElementById(changeLanguageID+'drpLanguage').options.length = 0;
    document.getElementById(changeLanguageID+'drpLanguage').options[0] = new Option('-','-');
    if(CountryInfo == null)
    {
        Bmi.Src.Asmx.WebMethods.GetWebsiteCountries(CountryChangedComplete,ChangeError);
    }
    else
    {
        PopulateLanguage();
    }
}
// Country changed CallBack
function CountryChangedComplete(result, userContext, methodName) 
{
    CountryInfo = result;
    PopulateLanguage();
}
// Populate the language box
function PopulateLanguage()
{
    var lang = document.getElementById(changeLanguageID+'drpLanguage');
    var count = 0;
    if(CountryInfo!=null)
    {
        for(i = 0; i < CountryInfo.length; i++)
        {
            if(document.getElementById(changeLanguageID+'drpCountry').value == CountryInfo[i].CountryCode)
            {
                lang.options[count] = new Option(CountryInfo[i].Language,CountryInfo[i].CultureCode);
                count++;
            }
        }
    }
    UpdateLanguageButtons();
}
// Update the continue button text and country flag
function UpdateLanguageButtons()
{  
    var lang = document.getElementById(changeLanguageID+'drpLanguage');
    var country = document.getElementById(changeLanguageID+'drpCountry');
    var button = document.getElementById(changeLanguageID+'btnContinue');
    var flag = document.getElementById(changeLanguageID+'divSelectedFlag');
    var contentLanguage = 'en';
    
    if(flag) { flag.style.background = 'url(/images.net/v2/global/template/flybmi/v6/flags/'+country.value+'.gif) left no-repeat';}
    
    if(CountryInfo != null)
    {
        for(i = 0; i < CountryInfo.length; i++)
        {
            if(CountryInfo[i].CultureCode == lang.value) { contentLanguage= CountryInfo[i].LanguageCode; }
        }
    }
    
    if(button)
    {
        switch(contentLanguage)
        {
            case 'fr': 
                button.innerHTML = "Continuer";
                break;
            case 'nl': 
                button.innerHTML = "Ga door";
                break;
            case 'es': 
                button.innerHTML = "Continuar";
                break;        
            default: 
                button.innerHTML = "Continue"; 
                break;
        }
    }    
}

function ChangeError(error, userContext, methodName) {
    //uncomment to view real error
    //alert(error.message);
}

function LanguageChanged()
{ 
    UpdateLanguageButtons();
}

// webapi3 linkbutton - adding click funcationality
function addClickFunction(id)
{
var b = document.getElementById(id);
if (b && typeof(b.click) == 'undefined') b.click = 
    function() 
    {
        var result = true; 
        if (b.onclick) result = b.onclick();
        if (typeof(result) == 'undefined' || result) 
        {
            eval(b.getAttribute('href')); 
        }

    };
}
            
// Logic used for the country language selector
function CountryChange(dropdown)
{   
    var oSelectedCountryLanguages = eval(dropdown[dropdown.selectedIndex].value)[0].split(',');
    var drpLanguage = document.getElementById(drpLanguageUniqueID + 'drpLanguage');    
    // Remove all options from list
    drpLanguage.length = 0;    
    // Repopulate with valid options
    for(var i = 0;i < oSelectedCountryLanguages.length;i++)
    {
        drpLanguage.options[i] = new Option(oSelectedCountryLanguages[i].split('#')[1],oSelectedCountryLanguages[i].split('#')[0]);
        try
        {
            // Select the right languge
            if(drpLanguage.options[i].value == window.location.href.split('/')[4].split('-')[0]) { drpLanguage.options[i].selected = true; }
        }
        catch(err)
        {// we have been unabble to 
        }
    }    
    // Make space for the Country flag and change it
    document.getElementById('chg_cty').style.paddingLeft = "30px";
    document.getElementById('chg_cty').style.background = "url(/images.net/v2/global/template/flybmi/v6/flags/" + dropdown[dropdown.selectedIndex].value + ".gif) no-repeat 0 23px";
    return false;
}

// Accepts the language selection change
function SelectLanguage()
{
    var drpCountries = document.getElementById(drpLanguageUniqueID + 'drpCountry');
    var drpLanguage = document.getElementById(drpLanguageUniqueID + 'drpLanguage');
    // Redirect to required site
    window.location.href = '/bmi/' + drpLanguage[drpLanguage.selectedIndex].value + '-' + drpCountries[drpCountries.selectedIndex].value + '/index.aspx';
}            

// Sets links that need to open in a new window to do so
function SetExternalTargets()
{
    $("a[rel*=nofollow]").click(function() {
        window.open(this.href);return false;
    });
    $("a[rel*=external]").click(function() {
        window.open(this.href);return false;
    });
}



// Runs with window.onload
$(document).ready(function() {   

/* OCKI DISABLE EMAIL  */
$("img[src$='EmailBoardingPass_Blue_Small.gif'], img[src$='EmailBoardingPassLong_Blue_Small.gif'],img[src$='EmailEticketReceipt_White.gif']", "div#divOlckiContainer div#CONF").css("display", "none");
$("div#CONF","div#divOlckiContainer").before("<div class='MessageContainer'>Service temporarily unavailable: Unfortunately your boarding pass or e-ticket cannot be emailed at this time. Please print your boarding pass below.</div>");
/* OCKI DISABLE EMAIL  */

if(loginFailed == 'True' || IsLoginPage == 'True')
{
     $("div.LoginSpotContent").show();
     $("#LoginSpotHeader").addClass("LoginSpotMainHeaderOpen");
     $("#LoginSpotHeader").removeClass("LoginSpotMainHeaderClosed");
    
}
else
{
     $("div.LoginSpotContent").hide();
     $("#LoginSpotHeader").addClass("LoginSpotMainHeaderClosed");
     $("#LoginSpotHeader").removeClass("LoginSpotMainHeaderOpen");

}




     //toggle funtionality for logged out loginspot control
   $("#LoginSpotHeader").toggle (
      function() {
        $("div.LoginSpotContent").slideDown("slow");
        $(this).addClass("LoginSpotMainHeaderOpen");
        $(this).removeClass("LoginSpotMainHeaderClosed");
       
       
     },
     function() {
       $("div.LoginSpotContent").slideUp("slow");
            $(this).removeClass("LoginSpotMainHeaderOpen");
            $(this).addClass("LoginSpotMainHeaderClosed");
           
     });
  
   
   //Close DaimondClub, Company Rewards, Your next flight Controls initially
   $("#DivDiamondClubHeader").addClass("LoginSpotHeaderLabelClosed");
   $("#DivDiamondClubHeader").removeClass("LoginSpotHeaderLabelOpen");
   $("#DivDiamondClubContent").hide();
   
   $("#DivCompanyRewardsHeader").addClass("LoginSpotHeaderLabelClosed");
   $("#DivCompanyRewardsHeader").removeClass("LoginSpotHeaderLabelOpen");
   $("#DivCompanyRewardsContent").hide();  
 
   $("#DivYourNextFlightHeader").addClass("LoginSpotHeaderLabelClosed");
   $("#DivYourNextFlightHeader").removeClass("LoginSpotHeaderLabelOpen");
   $("#DivYourNextFlightContent").hide();  
   
   
   //If page is company rewards open control CompanyRewards
   if(IsCompanyRewards == 'True')
    {
        $("#DivCompanyRewardsHeader").addClass("LoginSpotHeaderLabelOpen");
        $("#DivCompanyRewardsHeader").removeClass("LoginSpotHeaderLabelClosed");
        $("#DivCompanyRewardsContent").show();
    }
   
     
   
   //toggle funtionality for logged in loginspot control main Acordion
   $("#DivLoginSpotHeaderMain").click ( function() {
        if($("#DivLoginSpotContentMain").css("display") == "none") {
            $(this).addClass("LoginSpotMainHeaderOpen");
            $(this).removeClass("LoginSpotMainHeaderClosed");
            $("#DivLoginSpotContentMain").slideDown("slow");
        }
        else
        {
            $(this).addClass("LoginSpotMainHeaderClosed");
            $(this).removeClass("LoginSpotMainHeaderOpen");
            $("#DivLoginSpotContentMain").slideUp("slow");
        }
   });
   
  //toggle funtionality for Diamond club control
  $("#DivDiamondClubHeader").click ( function() {
    if($("#DivDiamondClubContent").css("display") == 'none') {
        $(this).addClass("LoginSpotHeaderLabelOpen");
        $(this).removeClass("LoginSpotHeaderLabelClosed");
        $("#DivDiamondClubContent").slideDown("slow");
        
        $("#DivCompanyRewardsHeader").addClass("LoginSpotHeaderLabelClosed");
        $("#DivCompanyRewardsHeader").removeClass("LoginSpotHeaderLabelOpen");
        $("#DivCompanyRewardsContent").slideUp("slow"); 
        
        $("#DivYourNextFlightHeader").addClass("LoginSpotHeaderLabelClosed");
        $("#DivYourNextFlightHeader").removeClass("LoginSpotHeaderLabelOpen");
        $("#DivYourNextFlightContent").slideUp("slow"); 
     }
     else
     {
        $(this).addClass("LoginSpotHeaderLabelClosed");
        $(this).removeClass("LoginSpotHeaderLabelOpen");
        $("#DivDiamondClubContent").slideUp("slow");
     }
     
     
  });
      
      //toggle funtionality for Company Rewards control
  $("#DivCompanyRewardsHeader").click( function() {
    if($("#DivCompanyRewardsContent").css("display") == 'none') {
        $("#DivDiamondClubHeader").addClass("LoginSpotHeaderLabelClosed");
        $("#DivDiamondClubHeader").removeClass("LoginSpotHeaderLabelOpen");
        $("#DivDiamondClubContent").slideUp("slow");
        
        $(this).addClass("LoginSpotHeaderLabelOpen");
        $(this).removeClass("LoginSpotHeaderLabelClosed");
        $("#DivCompanyRewardsContent").slideDown("slow");
        
        $("#DivYourNextFlightHeader").addClass("LoginSpotHeaderLabelClosed");
        $("#DivYourNextFlightHeader").removeClass("LoginSpotHeaderLabelOpen");
        $("#DivYourNextFlightContent").slideUp("slow"); 
    } 
    else
    {
        $(this).addClass("LoginSpotHeaderLabelClosed");
        $(this).removeClass("LoginSpotHeaderLabelOpen");
        $("#DivCompanyRewardsContent").slideUp("slow");
    }   
  });
  
  
   //toggle fintionality for Your Next Flight control
   $("#DivYourNextFlightHeader").click( function() {
    if($("#DivYourNextFlightContent").css("display") == 'none') {
        $("#DivDiamondClubHeader").addClass("LoginSpotHeaderLabelClosed");        
        $("#DivDiamondClubHeader").removeClass("LoginSpotHeaderLabelOpen");
        $("#DivDiamondClubContent").slideUp("slow");
        
        $("#DivCompanyRewardsHeader").addClass("LoginSpotHeaderLabelClosed");        
        $("#DivCompanyRewardsHeader").removeClass("LoginSpotHeaderLabelOpen");
        $("#DivCompanyRewardsContent").slideUp("slow"); 
        
        $(this).addClass("LoginSpotHeaderLabelOpen");
        $(this).removeClass("LoginSpotHeaderLabelClosed");
        $("#DivYourNextFlightContent").slideDown("slow");
    }
    else
    {
         $(this).addClass("LoginSpotHeaderLabelClosed");
        $(this).removeClass("LoginSpotHeaderLabelOpen");
        $("#DivYourNextFlightContent").slideUp("slow");
    }
   });  

      
      
      
      
      function LoginInError()
      {
        alert("functionCalled");
      }
  
     
    SetExternalTargets();
    
    jQuery.each(jQuery.browser, function(i) {        
        //Underline text on hover for input of class button 
        $("input.button").hover(
           function() {
            $(this).css("text-decoration", "underline")
            if($.browser.mozilla) $(this).css("display", "table-cell");  
           },
           function() {
             $(this).css("text-decoration", "none")
           }        
        );          
    });
});

