﻿// extending Array Class to contain indexOf funtion

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        
        
        var len = this.length,
            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;
    };
}

// Extend String Class prototype to include Format function
String.prototype.format = function () {
    
    var s = arguments[0], i, reg;

    for (i = 0; i < arguments.length - 1; i++) {
        reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }

    return s;
};


String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
};

String.prototype.startsWith = function (str)
{ return (this.match("^" + str) == str); };

String.prototype.endsWith = function (str)
{ return (this.match(str + "$") == str); };

function validateExpression(expresion, text) {

    var pattern = new RegExp(expresion);
    if (text.search(pattern) == -1) {
        return false;
    }
    else {
        return true;
    } 
}
function ReplaceString(inputData, pattren, newstring) {
    while (inputDate.search(pattren) > -1) {
        inputData = inputData.replace(pattren, newstring);
    }
    return inputData;
}
function IsDuplication(objectid, objectname, url) {
    var isduplicate = true;
    $.ajax({ url: url,
        type: "POST",
        data: "oid=" + objectid + "&oname=" + objectname,
        success: function (data) {
            if (data == "1") {
                isduplicate = true;
            } 
            else {
                isduplicate = false;
            } 
        },
        async: false
    });
    return isduplicate;
}

/////////// Document Ready
$(document).ready(function () {
    $("#master_MessageBox").dialog({ autoOpen: false,
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog('close');
            }
        }

    });
});
function ShowMessage(msg) {

    $("#master_MessageBox").html(msg);
    $("#master_MessageBox").dialog('open');

}
// this function show message text inside a selector
function DisplayMessage(selector, msg) {
    if (msg == undefined || msg == '') {
        $(selector).find(".widget-validation").remove(); 
    }
    else {
        $(selector).find(".widget-validation").remove(); 
        $("<div class=\"widget-validation\" ></div>").prependTo(selector);
        $(selector).find(".widget-validation").html(msg);
    }

}

function datastore(key, value) {
    if (value == undefined) {
        //read
        return $.data($("head")[0], key);
    }
    else {
        $.data($("head")[0], key, value);
    }

}

