﻿String.prototype.SetStringLength = function(Length) {
    if (this.length > Length)
        return this.substring(0, Length) + '...';
    else
        return this;
};
var TextUtil = new Object();
TextUtil.isNotMax = function(oTextArea) {
    return oTextArea.value.length != oTextArea.getAttribute("MaxLength");
}
var EventUtil = new Object;
EventUtil.formatEvent = function(oEvent) {
    return oEvent;
}
TextUtil.blockChars = function(oTextbox, oEvent) {
    oEvent = EventUtil.formatEvent(oEvent);
    var sInvalidChars = oTextbox.getAttribute("InvalidChars");
    if (oEvent.charCode != null)
        var sChar = String.fromCharCode(oEvent.charCode);
    else
        var sChar = String.fromCharCode(oEvent.keyCode);
    var bIsValidChar = sInvalidChars.indexOf(sChar) == -1;
    return bIsValidChar || oEvent.ctrlKey;
};
TextUtil.allowChars = function(oTextbox, oEvent) {
    oEvent = EventUtil.formatEvent(oEvent);
    var sValidChars = oTextbox.getAttribute("ValidChars");
    if (oEvent.charCode != null)
        var sChar = String.fromCharCode(oEvent.charCode);
    else
        var sChar = String.fromCharCode(oEvent.keyCode);
    var bIsValidChar = sValidChars.indexOf(sChar) > -1;
    return bIsValidChar || oEvent.ctrlKey;
};
TextUtil.blurBlock = function(oTextBox) {
    var sInvalidChars = oTextBox.getAttribute("InvalidChars");
    var arrInvalidChars = sInvalidChars.split("");
    for (var i = 0; i < arrInvalidChars.length; i++) {
        if (oTextBox.value.indexOf(arrInvalidChars[i]) > -1) {
            alert("Character '" + arrInvalidChars[i] + "' not allowed.");
            oTextBox.focus();
            oTextBox.select();
            return;
        }
    }
};
String.prototype.replaceAll = function(replaceThis, withThis) {
    var re = new RegExp(RegExp.quote(replaceThis), "g");
    return this.replace(re, withThis);
};
RegExp.quote = function(str) {
    return str.replace(/([.?*+^$[\]\\(){}-])/g, "\\$1");
};
StringBuilder = function() {
    this._buffer = new Array();
}
StringBuilder.prototype = {
    append: function(text) {
        this._buffer[this._buffer.length] = text;
    },
    toString: function() {
        return this._buffer.join("");
    }
};
Array.prototype.contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element)
            return true;
    }
    return false;
};
Array.prototype.remove = function(s) {
    for (i = 0; i < this.length; i++) {
        if (s == this[i]) this.splice(i, 1);
    }
};
Array.prototype.replaceAt = function(element, strReplaceWith) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element)
            this.splice(i, 0, strReplaceWith);
    }
};
function ConvertDateBasedOnCulture(dateValue, cultureInfo, dateSeparator) {
    var dd, mm, yyyy;
    var dateString = new String(dateValue);
    var dateArray = dateValue.split(dateSeparator);
    if (dateArray.length < 3)
        return '';
    else {
        if (cultureInfo.toLowerCase() == "en-us") {
            mm = dateArray[0];
            dd = dateArray[1];
            yyyy = dateArray[2];
        }
        else
            if ((cultureInfo.toLowerCase() == "hu-hu") || (cultureInfo.toLowerCase() == "zh-cn") || (cultureInfo.toLowerCase() == "pl-pl")) {
            mm = dateArray[1];
            dd = dateArray[2];
            yyyy = dateArray[0];
        }
        else {
            dd = dateArray[0];
            mm = dateArray[1];
            yyyy = dateArray[2];
        }

        if (!IsValidDate(yyyy, mm, dd)) {
            return '';
        }
        else {
            var convertedDate = new Date();
            convertedDate.setFullYear(yyyy);
            convertedDate.setFullYear(yyyy, (mm - 1), dd);
            return convertedDate;
        }
    }
}
function IsValidDate(yy, mm, dd) {
    if (isNaN(yy))
        return false;
    if (isNaN(mm))
        return false;
    if (isNaN(dd))
        return false;
    var err = 0;
    if (dd < 1 || dd > 31) err = 1;
    if (mm < 1 || mm > 12) err = 1;
    if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
        if (dd == 31) err = 1;
    }
    if (mm == 2) {
        var g = parseInt(yy / 4)
        if (isNaN(g))
            err = 1
        if (dd > 29) { err = 1; }
        if (dd == 29 && ((yy / 4) != parseInt(yy / 4))) err = 1;
    }
    if (err == 1)
        return false;
    else
        return true;
}
function ConvertNumberBasedOnCulture(numberValue, decimalSeparator) {
    var returnValue = '';
    returnValue = numberValue.replace(DecimalSeparator, '.');
    if (isNaN(returnValue))
        return '';
    else
        return returnValue;
}
function IsValidEmail(value) {
    var exp = "\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    var rx = new RegExp(exp);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}
