// Common.js
$.fn.exists = function () { return $(this).length !== 0; }

$.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

function submitForm() {
    var form = $('#form1');
    if (form.length > 0) form[0].submit();
    return true;
}

function creatNamespace() {
    if (typeof ($.apr) == "undefined") $.apr = {};
}

$(document).ready(function () {
    creatNamespace();

    $.apr.common = {
        basePageUrl: function (appendSlash) {
            var url = location.href;  // entire url including querystring - also: window.location.href;
            var baseUrl = url.substring(0, url.lastIndexOf('/'));
            if (appendSlash) return baseUrl + '/'
            else return baseUrl;
        },
        baseUrl: function (appendSlash) {
            var url = location.href;  // entire url including querystring - also: window.location.href;
            var baseUrl = url.substring(0, url.indexOf('/', 14));

            if (baseUrl.indexOf('http://localhost') != -1) {
                // Base Url for localhost
                var url = location.href;  // window.location.href;
                var pathname = location.pathname;  // window.location.pathname;
                var index1 = url.indexOf(pathname);
                var index2 = url.indexOf("/", index1 + 1);
                var baseLocalUrl = url.substr(0, index2);

                if (appendSlash) return baseLocalUrl + '/'
                else return baseLocalUrl;
            }
            else {

                // Root Url for domain name
                if (appendSlash) return baseUrl + '/'
                else return baseUrl;
            }
        },
        failedCallback: function (result, userContext, methodName) {
            switch (methodName) {
                default:
                    alert(result.get_message());
                    break;
            }
        },
        getLeftCenter: function (width, leftOffSet) {
            if (isNull(width)) width = 0;
            if (isNull(leftOffSet)) leftOffSet = 1;

            var left = Number(($(window).width() - width) / 2 + $(window).scrollLeft());
            return left * leftOffSet;
        },
        getTopCenter: function (height, topOffSet) {
            if (isNull(height)) height = 0;

            if (isNull(topOffSet)) topOffSet = 1;

            var top = Number(($(window).height() - height) / 2 + $(window).scrollTop());
            return top * topOffSet;
        },
        getUrl: function (page) {
            if (isNull(page)) page = '';
            return window.location.protocol + '//' + window.location.host + page;
        },
        htmlEscape: function (value) {
            if (isNull(value)) return '';
            return $('<div/>').text(value).html();
        },
        isNull: function (o) {
            return ("undefined" == typeof (o) || "unknown" == typeof (o) || null == o)
        }
    }
});


function isNull(o) 
{
    return ("undefined" == typeof (o) || "unknown" == typeof (o) || null == o)
}

function isSecure() {
    return window.location.protocol == 'https:';
}

function fillDropDown(dropDownID, items, fieldName, fieldValue, defaultValue) 
{
    var dropdown = document.getElementById(dropDownID);

    if (isNull(dropdown)) return;

    dropdown.options.length = 0;

    if (!isNull(items)) {
        var selectedIndex = 0;
        for (i = 0; i < items.length; i++) {
            var oOption = document.createElement("option");
            oOption.text = items[i][fieldName];
            oOption.value = items[i][fieldValue];
            if (!isNull(defaultValue) && oOption.value == defaultValue) selectedIndex = i;
            document.getElementById(dropDownID).add(oOption);
        }
        if (items.length > 0) dropdown.options[selectedIndex].selected = true;
    }
}

