﻿String.prototype.ReplaceAll = function(searchArray, replaceArray) {
    var replaced = this;

    for (var i = 0; i < searchArray.length; i++) {
        replaced = replaced.replace(searchArray[i], replaceArray[i]);
    }

    return replaced;
}

String.prototype.StartsWith = function(value) {
    return (this.substr(0, value.length) == value);
}

// Extends the String object, creating a "EndsWith" method on it.
String.prototype.EndsWith = function(value, ignoreCase) {
    var L1 = this.length;
    var L2 = value.length;

    if (L2 > L1)
        return false;

    if (ignoreCase) {
        var oRegex = new RegExp(value + '$', 'i');
        return oRegex.test(this);
    }
    else
        return (L2 == 0 || this.substr(L1 - L2, L2) == value);
}

String.prototype.Remove = function(start, length) {
    var s = '';

    if (start > 0)
        s = this.substring(0, start);

    if (start + length < this.length)
        s += this.substring(start + length, this.length);

    return s;
}

String.prototype.Trim = function() {
    // We are not using \s because we don't want "non-breaking spaces to be caught".
    return this.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '');
}

String.prototype.LTrim = function() {
    // We are not using \s because we don't want "non-breaking spaces to be caught".
    return this.replace(/^[ \t\n\r]*/g, '');
}

String.prototype.RTrim = function() {
    // We are not using \s because we don't want "non-breaking spaces to be caught".
    return this.replace(/[ \t\n\r]*$/g, '');
}

String.prototype.ReplaceNewLineChars = function(replacement) {
    return this.replace(/\n/g, replacement);
}

String.prototype.Replace = function(regExp, replacement, thisObj) {
    if (typeof replacement == 'function') {
        return this.replace(regExp,
			function() {
			    return replacement.apply(thisObj || this, arguments);
			});
    }
    else
        return this.replace(regExp, replacement);
}

function onloadimage(ImgD, iwidth, iheight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        //ImgD.alt = "原图尺寸 宽 " + image.width + " × 高 " + image.height;
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
        else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }
}
