﻿// ######################################## //
// ############### Template ############### //

//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;
// 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');}
                    callingControlLinkID = masterPageID;
                    break;
                case 'log':
                    loginFlyout = true;
                    // If language flyout is open, hide it
                    if(languageFlyout) { showHideFlyout('hide','chg');}
                    callingControlLinkID = loginUniqueID;
                    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;
            }        
            break;  
        default:
            languageFlyout = null;
            loginFlyout = null;
            break;   
    }
}

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() {
    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")
           }        
        );          
    });
});