﻿function SetupBookmarks() {
    jQuery("a.jQueryBookmark").click(function (e) {
        e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
        var bookmarkUrl = this.href;
        var bookmarkTitle = this.title;
        if (bookmarkUrl.match(/#$/) != null) {
            bookmarkUrl = window.location.href;
            bookmarkTitle = document.title;
        }
        if (window.sidebar) { // For Mozilla Firefox Bookmark
            window.sidebar.addPanel(bookmarkTitle, bookmarkUrl, "");
        } else if (window.external || document.all) { // For IE Favourite
            window.external.AddFavorite(bookmarkUrl, bookmarkTitle);
        } else if (window.opera) { // For Opera Browsers
            $("a.jQueryBookmark").attr("href", bookmarkUrl);
            $("a.jQueryBookmark").attr("title", bookmarkTitle);
            $("a.jQueryBookmark").attr("rel", "sidebar");
        } else { // for other browsers which does not support
            alert('Your browser does not support this bookmark action');
            return false;
        }
    });
}


function PopUpLink(Url, Title, Width, Height) {
    window.open(Url, Title, 'menubar=0,resizeable=1,scrollbars=1,width=' + Width + ',height=' + Height);
}


function Trim(Value) {
    return Value.replace(/^\s+|\s+$/g, "");
}


function GetParameterByName(strName) {

    var urlParts = document.URL.split('?');
    if (urlParts.length < 2) {
        return "";
    }

    var params = urlParts[1].split('&');

    for (var i = 0; i < params.length; i++) {
        if (params[i].split('=')[0] == strName) {
            params = params[i].split('=')[1];
            break;
        }
    }

    return params;
}


function DaysDifference(date1, date2) {
    if (!date1 || !date2) { return 0; }
    var t1 = date1.getTime();
    var t2 = date2.getTime();
    var ms = t2 - t1;
    var s = ms / 1000;
    var h = s / 3600;
    var d = h / 24;
    if (d < 0) { d = 0; }
    d = Math.round(d);
    return d;
}


/*  Form pre-selection */

function SelectRadioButtonList(inputName, value) {
    if (!value) {
        value = GetParameterByName(inputName);
    }
    if (!value || value.length == 0) {
        $('input[name="' + inputName + '"]:eq(0)').attr("checked", true);
    } else {
        $('input[name="' + inputName + '"]').filter("[value=" + value + "]").attr("checked", true);
    }
}
function SelectDropDown(inputName, value) {
    if (!value) {
        value = GetParameterByName(inputName);
    }
    if (value && value.length > 0) {
        $('select[name="' + inputName + '"]').val(value.toString().replace(/%20/g, " "));
    }
    else {
        $('select[name="' + inputName + '"]').val("");
    }
}
function SelectCheckBox(inputName, value) {
    if (!value) {
        value = GetParameterByName(inputName);
    }
    if (value && value.length > 0) {
        $('input[name="' + inputName + '"]').attr("checked", true);
    }
}
function SelectTextBox(inputName, value) {
    if (!value) {
        value = GetParameterByName(inputName);
    }
    if (value && value.length > 0) {
        $('input[name="' + inputName + '"]').val(value);
    }
    
}

//use attribute name for query string name
function PrefilValuesFromUrlFactory(ParentFilterPnlId) {
    var url = window.location.href;
    var hasUrlQuery = url.indexOf("?") > 1;
    if (hasUrlQuery) {
        var splitUrl = url.split("?");
        var rawUrl = splitUrl[0];
        var queryStrings = splitUrl[1].split("&");
        $("input, select", $("#" + ParentFilterPnlId)).each(function () {

            for (var i = 0; i < queryStrings.length; i++) {
                if (queryStrings[i].indexOf($(this)[0].name + "=") >= 0) {
                    var pair = queryStrings[i].split("=");
                    if ($(this).is("input:checkbox")) {
                        SelectCheckBox($(this)[0].name, pair[1]);
                    }
                    else if ($(this).is("input:radio")) {
                        SelectRadioButtonList($(this)[0].name, pair[1]);
                    }
                    else if ($(this).is("select")) {
                        SelectDropDown($(this)[0].name, pair[1]);
                    }
                    else {
                        $(this).val(decodeURIComponent(pair[1]));
                    }
                }
            }
        });
    }
}


function clearText(field) {
    if (field.defaultValue == field.value) {
        field.value = '';
        document.getElementById(field.id).className = "txtChangeColor";
    } else if (field.value == '') {
        field.value = field.defaultValue;
        document.getElementById(field.id).className = "txtRightColumn";
    }
}


function clearTextMemberSearch(field) {
    if (field.defaultValue == field.value) {
        field.value = '';
        document.getElementById(field.id).className = "txtLarge";
    } else if (field.value == '') {
        field.value = field.defaultValue;
        document.getElementById(field.id).className = "txtLarge";
    }
}

/*
sfHover = function () {
    var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function () {
            this.className += " sfhover";
        }
        sfEls[i].onmouseout = function () {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }

    var sfEls = document.getElementById("topNav").getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function () {
            this.className += " sfhover";
        }
        sfEls[i].onmouseout = function () {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }

}
if (window.attachEvent) window.attachEvent("onload", sfHover);*/
