/** LOST PASSWORD **/
        $('document').ready(function () {  
            $('#dvLostPassword').jqm({toTop : true}); //Adding modal prevents the input box from receiving focus after closing and then re-opening
            //modal: true, toTop : true
        });
    function ShowLostPassword()
    {
        $('#dvLostPassword').jqmShow();
    }
    function SendLostPassword(){
        var email = document.getElementById('txtPasswordEmail').value;
        $.getJSON("JS_LostPassword.aspx?cachebust=" + new Date() + "&jsoncallback=?",
            {"email" : email},
        function(data) {
            if (data.status == "SUCCESS"){
                $('#dvLostPassword').attr("class", "StatelpSuccess");
            } else {
                $('#dvLostPassword').attr("class", "StatelpFailure");
            }
        });
        
    }

function HideCalendar(oCalendar){
    oCalendar.hide();
    oCalendar.get_element().blur();
}
    
/** SWAPOUT MANAGER **/
var SelectedElement;
var SwapoutManager;
var LoginBarCartElementID = "";

(function (){
    SwapoutManager = {};
    SwapoutManager.Items = new Array();
    SwapoutManager.VisibleItem = null;
    SwapoutManager.AddItem = function(obj){
        this.Items[this.Items.length] = obj;
    }
    SwapoutManager.ShowItem = function(id){
        for(var i=0; i<this.Items.length; i++){
            var obj = EL(this.Items[i].Control);
            if (obj != null){
                if (this.Items[i].ID == id){
                    obj.style.display = "block";
                } else {
                    obj.style.display = "none";
                }
            }
        }
    }
    SwapoutManager.ShowFirst = function(){
        if (this.Items.length > 0){
            this.ShowItem(this.Items[0].ID);
        }
    }
})();

/** SIZESWAPPER **/
(function(){
    SizeSwapper = {};
    SizeSwapper.Items = new Array();

    
    SizeSwapper.RegisterItem = function(obj){
        var FoundItem = this.GetItemByKey(obj.Key);
        //alert('Registering ' + obj.ElementID);
        if (FoundItem == null)
        { 
            FoundItem = {}; 
            FoundItem.Key = obj.Key; 
            FoundItem.IsImperial = true;
            FoundItem.Players = new Array(); 
            this.Items[this.Items.length] = FoundItem;
        }
        FoundItem.Players[FoundItem.Players.length] = obj; 
    }
    SizeSwapper.GetItemByKey = function(key){
        for(var i=0; i<this.Items.length; i++){
            var obj = this.Items[i];
            if (obj.Key == key) { return obj; }
        }
        return null;
    }
    SizeSwapper.Toggle = function(Key, Setting){
        var FoundItem = this.GetItemByKey(Key);
        if ((Setting == "IMPERIAL") && (FoundItem.IsImperial)) { return; }
        if ((Setting == "METRIC") && (!FoundItem.IsImperial)) { return; }
        
        FoundItem.IsImperial = !FoundItem.IsImperial;
        
        for (var i=0; i<FoundItem.Players.length; i++){
            var Item = FoundItem.Players[i];
            var ItemElement = EL(Item.ElementID);
            if ((Item.IsSelector) && (FoundItem.IsImperial)){
                if (Item.IsImperial) {
                    ItemElement.src = 'Common/Images/button-imperial-on.gif';
                    ItemElement.onmouseover = null;
                    ItemElement.onmouseout = null;
                } else {
                    ItemElement.src = 'Common/Images/button-metric.gif';
                    ItemElement.onmouseover = function() { this.src = 'Common/Images/button-metric-on.gif'; }
                    ItemElement.onmouseout = function() { this.src = 'Common/Images/button-metric.gif'; }
                }
            } 
            if ((Item.IsSelector) && (!FoundItem.IsImperial)){
                if (Item.IsImperial) {
                    ItemElement.src = 'Common/Images/button-imperial.gif';
                    ItemElement.onmouseover = function() { this.src = 'Common/Images/button-imperial-on.gif'; }
                    ItemElement.onmouseout = function() { this.src = 'Common/Images/button-imperial.gif'; }
                } else {
                    ItemElement.src = 'Common/Images/button-metric-on.gif';
                    ItemElement.onmouseover = null;
                    ItemElement.onmouseout = null;
                }
            }
            if (!Item.IsSelector){
                if (Item.IsImperial != FoundItem.IsImperial){
                    ItemElement.style.display = "none";
                } else {
                    ItemElement.style.display = "inline";
                }
            }
        }
    }
    
    
    
})();

/** PRODUCTGROUPSWAPPER **/
var SelectedProductGroupIndex = -1;
var ProductGroupSwapper;

(function(){
    ProductGroupSwapper = {};
    ProductGroupSwapper.Items = new Array();
    
    ProductGroupSwapper.RegisterItem = function(obj){
        var FoundItem = this.GetItemByKey(obj.Key);
        if (FoundItem == null)
        { 
            FoundItem = {}; 
            FoundItem.Key = obj.Key; 
            FoundItem.Players = new Array(); 
            this.Items[this.Items.length] = FoundItem;
        }
        FoundItem.Players[FoundItem.Players.length] = obj; 
    }
    ProductGroupSwapper.GetItemByKey = function(key){
        for(var i=0; i<this.Items.length; i++){
            var obj = this.Items[i];
            if (obj.Key == key) { return obj; }
        }
        return null;
    }
    ProductGroupSwapper.GetItemIndex = function(Key){
        for(var i=0; i<this.Items.length; i++){
            var obj = this.Items[i];
            if (obj.Key == Key) { return i; }
        }
        return -1;
    }

    ProductGroupSwapper.GetSelectedItem = function(){
        if (SelectedProductGroupIndex < 0) { return null; }
        
        return this.Items[SelectedProductGroupIndex];
    }
    ProductGroupSwapper.SelectNextItem = function(){
        var NewIndex = SelectedProductGroupIndex + 1;
        if (NewIndex >= this.Items.length) { NewIndex = 0; }
        this.SelectItem(this.Items[NewIndex].Key);
    }
    ProductGroupSwapper.SelectPreviousItem = function(){
        var NewIndex = SelectedProductGroupIndex - 1;
        if (NewIndex < 0 ) { NewIndex = this.Items.length - 1; }
        this.SelectItem(this.Items[NewIndex].Key);
    }
    ProductGroupSwapper.SelectItem = function(Key){
       EL("RangeImage").style.display = "none";
       var OldSelectedItem = this.GetSelectedItem();
       if (OldSelectedItem != null){
           for (var i=0; i<OldSelectedItem.Players.length; i++){
                this.UnSelectPlayer(OldSelectedItem.Players[i]);
           }
       }
       var NewSelectedItem = this.GetItemByKey(Key);
       if (NewSelectedItem != null){
            for(var j=0; j<NewSelectedItem.Players.length; j++){
                this.SelectPlayer(NewSelectedItem.Players[j]);
            }
       }
       SelectedProductGroupIndex = this.GetItemIndex(Key);
    }
    ProductGroupSwapper.SelectItemByIndex = function(Index){
        if (Index < this.Items.length){
        this.SelectItem(this.Items[Index].Key);
        }
    }
    ProductGroupSwapper.SelectPlayer = function(Player){
        var Element = EL(Player.ElementID);
        Element.className = Player.SelectedClass;
        if (Player.ToggleVisibility) { Element.style.display = "block"; }
        if (Player.SetFocus) { if (Element.focus) { Element.focus(); } }
        
    }
    ProductGroupSwapper.UnSelectPlayer = function(Player){
        var Element = EL(Player.ElementID);
        Element.className = Player.UnselectedClass;
        if (Player.ToggleVisibility) { Element.style.display = "none"; }
        if (Player.SetFocus) { if (Element.focus) { Element.focus(); } }
    }
   
    
})();

function EL(id){
    return document.getElementById(id);
}

function SetClass(ary){
    for(var i=0; i<ary.length; i++){
        var element = EL(ary[i].ID);
        if (element != SelectedElement){
            element.className = ary[i].CssClass;
        }
    }
}

function ScriptCall2(el, url){
var qty;
$(el).find('.qty').each(function() { qty = this.value; });


    var dte = new Date();
    var newurl = url + "&qty=" + escape(qty) + "&time=" + escape(dte) + escape(dte.getHours()) + escape(dte.getMinutes()) + escape(dte.getSeconds()) + escape(dte.getMilliseconds());

    var scr = document.createElement("script");   
        scr.type="text/javascript";   
        scr.src = newurl;
        document.body.appendChild(scr);
}
function ScriptCall(el, url){
    var qty;
    $(el).find('.qty').each(function() { qty = this.value; });
    //try adding and check response
    $.getJSON(url + "&cachebust=" + new Date() + "&format=json&jsoncallback=?",
            {"force" : "false", "qty" : qty},
        function(data) {
                    if (data.toomany == 'true'){
                    alert("There are only " + data.qty + " available. Please select a lower quantity.");
                    return;
                }
            if (data.alreadyAdded == 'true'){
                if (confirm("You already have " + data.qty + " " + data.title + " in your list, would you like to add " + qty + " more?")){
                    $.getJSON(url + "&cachebust=" + new Date() + "&format=json&jsoncallback=?",
                    {"force" : "true", "qty" : qty},
                    function(newdata){
                        ShowCartTotal(newdata.totalQuantity);
                    });
                }
            } else {
                ShowCartTotal(data.totalQuantity);
            }
        });
}
function ShowCartTotal(qty){
    $('#LoginBarCartElementID').html('My Basket [' + qty + ']');
}



function SetSelectedElement(ElementID, SelectedClass, UnSelectedClass){

    if (SelectedElement != null) { SelectedElement.className = UnSelectedClass; }
    SelectedElement = EL(ElementID);
    SelectedElement.className = SelectedClass;
    
}





