﻿/*
*/
// ------------------------------------------------------------------------
// Select List helper
//
(function($) {
    $.fn.emptySelect = function() {
        return this.each(function() {
            if (this.tagName == 'SELECT') this.options.length = 0;
        });
    }
    // Fill select list with elements
    $.fn.loadSelect = function(elements, defaultName) {
        if (typeof defaultName != 'undefined') {
            var d = { Name: defaultName };
            var narray = new Array(d);
            elements = narray.concat(elements);
        }
        return this.emptySelect().each(function() {
            if (this.tagName == 'SELECT') {
                var selectElement = this;
                $.each(elements, function(index, element) {
                    // fill name-id
                    var option = new Option(element.Name, element.Id);
                    // fill name, name
                    //var option = new Option(element.Name, element.Name);
                    if ($.browser.msie) {
                        selectElement.add(option);
                    } else {
                        selectElement.add(option, null);
                    }
                });
            }
        });
    }

    // ------------------------------------------------------------------------
    // Global Helper

    // Display html content in a full browser window with toolbars, scrollbars 
    $.BrowserWindow = function(url, hWind, nWidth, nHeight) {
        var cToolBar = "toolbar=1,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=" + nWidth + ",height=" + nHeight
        var popupwin = window.open(url, hWind, cToolBar);
        if (popupwin == null) {
            alert('Please enable Popup Windows for this site!');
        }
    }

    // Display html content in a popup window with window.open
    $.PopUpWindow = function(url, hWind, nWidth, nHeight, nScroll) {
        var cToolBar = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=" + nScroll + ",resizable=1,width=" + nWidth + ",height=" + nHeight
        var popupwin = window.open(url, hWind, cToolBar);
        if (popupwin == null) {
            alert('Please enable Popup Windows for this site!');
        }
    }

    // Load text into a matching element
    $.LoadFile = function(pattern, view, name) {
        var url = "/Load/TextFile/" + view + "/" + name;
        $(pattern).load(url);
    }

    // Load text into a matching element
    $.LoadText = function(pattern, category, name, appName) {
        if (typeof appName == 'undefined') {
            appName = "AutoInsuranceQuote";
        }
        var url = "/Load/Text/" + appName + "/" + category + "/" + name;
        $(pattern).load(url);
    }

    // Load text into a matching element
    $.LoadPartialView = function(pattern, category, name) {
        //var url = "/Info/Load/" + "/" + category + "/" + name;
        //var url = "/Info/Load/" + name;
        var url = "/" + category + "/Load/" + name;
        $(pattern).load(url);
    }


    // Load a list and add -select- as the first option
    $.LoadList = function(pattern, listId) {
        var listUrl = "/Load/Lists/" + listId;
        $.getJSON(listUrl, null, function(data) {
            $(pattern).loadSelect(data, '-select-');
        });
    }
})(jQuery);
