﻿/// <reference path="jquery-1.3.2.js" />

function findPosX(obj) {
    //TODO: Update documentation
    /// <summary>Find relative x position of any object</summary>
    /// <param name="obj"></param>
    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;
}

function showHideFlyout(type, flyout, change, offset) {
    //TODO: Update documentation
    /// <summary>Calls the show/hide function to change the visibility of the given div panel</summary>
    /// <param name="type"></param>
    /// <param name="flyout"></param>
    /// <param name="change"></param>
    /// <param name="flyout"></param>    

    // 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;
    }
}

function CountryChange(dropdown) {
    /// <summary>Changes the languages in the dropdown list depending on the language used</summary>
    /// <param name="dropdown">The country dropdown list control</param>
    try {
        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 unable 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";
    } catch (err) { }
    return false;
}

function SetExternalTargets() {
    /// <summary>Sets links with rel="nofollow" or rel="external" to open in a new window.</summary>

    // Find a tags with rel="nofollow"
    $("a[rel=nofollow]").click(function() {
        window.open(this.href); return false;
    });
    // Find a tags with rel="external"
    $("a[rel=external]").click(function() {
        window.open(this.href); return false;
    });
}

function PrintPage() {
    /// <summary>Send the current page to the printer if the browser supports it, otherwise show a message.</summary>
    window.print();

    return false;
}

function CheckNextStep() {
    // <summary>Determine whether the summary control is valid to determine whether the next step buttons should be enabled.</summary>
    if (typeof (summary) == "object" && summary != null && summary.IsValid != null) {
        summary.IsValid();
    }
}

function SetWatermarks() {

    watermarkValues = [];
    $(".watermark").each(
        function(i) {
            watermarkValues[i] = $(this).val();
            $(this).focus(
                            function() {
                             if ($(this).val() == watermarkValues[i]) 
                                {
                                    $(this).removeClass("normalText")
                                    $(this).toggleClass("watermark");
                                    $(this).val("")
                                }
                                else {
                                    $(this).removeClass("watermark")
                                    $(this).toggleClass("normalText")
                                }
                            }
                        ).blur(
                            function() {
                                if ($.trim($(this).val()) == "") {
                                    $(this).val(watermarkValues[i]);
                                    $(this).removeClass("normalText");
                                    $(this).addClass("watermark");
                                }
                            }
                        ).keyup(
                                    function() {
                                        if ($.trim($(this).val()) == "") {
                                            $(this).val(watermarkValues[i]);
                                            $(this).removeClass("normalText");
                                            $(this).addClass("watermark");
                                        }
                                    })
        });
}
$(document).ready(function() {
    /// <summary>Runs when the HTML has fully loaded.</summary>
    //external targets not required as called from template and homepage js
    //SetExternalTargets();
    CheckNextStep();
    SetWatermarks();
    $(".NumericOnly").keypress(function(e) {
        //if the letter is not digit then don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });
});