﻿if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(elt /*, from*/) {
        var len = this.length >>> 0;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this &&
          this[from] === elt)
                return from;
        }
        return -1;
    };
}

function $$(id, context) {
    var el = $("#" + id, context);
    if (el.length < 1)
        el = $("[id$=_" + id + "]", context);
    return el;
}

function LinkDelete(e,Link,ConfirmationMessage, SuccessMessage,url,data,PostSuccessFunction) {

        e.preventDefault(); // don't follow the link

        if (Link.data("clicked")) {
            alert('This is currently being done. Please wait...');
            return false;
        }

        if (confirm(ConfirmationMessage)) {
                Link.data("clicked", true);
                DALC(url, data, function(output) {
                    Link.data("clicked", false);
                    if (output === "OK") {
                        if (SuccessMessage != null) { alert(SuccessMessage); }
                        if (PostSuccessFunction != null) { PostSuccessFunction(); }
                    }
                    else {
                        alert(output);
                    }
                });
            }
}

function TableRowDelete(ConfirmationMessage, SuccessMessage) {
    $('table.Ajaxify a.Delete').click(function(e) {

        if (ConfirmationMessage == null) { ConfirmationMessage = "Are you sure you want to delete this row?"; }
        if (SuccessMessage == null) { SuccessMessage = "Row deleted successfully."; }

        var LINK = $(this);
        var TR = LINK.closest('tr');
        var TABLE = TR.closest('table');

        var UniqueId = TR.attr("UniqueId").split("/");
        var UniqueIdFieldName = TABLE.attr("UniqueIdFieldName").split("/");
        var URL = "/wsEuroplayers.asmx/"+TABLE.attr("DeleteWebMethod");

        if (UniqueId.length != UniqueIdFieldName.length) {
            alert("UniqueId and UniqueIdFieldName don't have the same length.");
        }
        else {

            var data = new Array(UniqueId.length);

            for (var i = 0; i < UniqueId.length; i++) {
                data[i] = "'" + UniqueIdFieldName[i] + "':'" + UniqueId[i] + "'";
            }

            LinkDelete(e,LINK, ConfirmationMessage, SuccessMessage, URL, "{" + data.join(",") + "}",function() {
                        TR.remove();
                    });
          }
    });
            
}

function LaunchPopupWindow(URL, Width, Height) {
    newwindow = window.open(URL, 'name', 'width=' + Width + 'px,height=' + Height + 'px,left=' + ((screen.width - Width) / 2) + ',top=' + (screen.height - Height) / 2 + 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
    if (window.focus) {  //check if the browser supports the focus() method, if so place the focus on the new window.
        newwindow.focus();
    }
    
    return false;
}

function SetPopupLink() {
    $('a.popup').click(function(e) {
        e.preventDefault(); // don't follow the link
        LaunchPopupWindow(this.href, $(this).attr("popupwidth"), $(this).attr("popupheight"));
    });
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}


function htmlEncode(value) {
    return $('<div/>').text(value).html();
}
function htmlDecode(value) {
    return $('<div/>').html(value).text();
}