﻿
//---------------------------------------String------------------------------------
String.prototype.lTrim = function() { return this.replace(/^\s*/, ""); };
String.prototype.rTrim = function() { return this.replace(/\s*$/, ""); };
String.prototype.trim = function() { return this.rTrim().lTrim(); };
String.prototype.endsWith = function(sEnd) { return (this.substr(this.length - sEnd.length) == sEnd); };
String.prototype.startsWith = function(sStart) { return (this.substr(0, sStart.length) == sStart); };
String.prototype.isEmpty = function() { return this.length === 0; };
String.prototype.format = function() {
    var s = this;
    for (var i = 0; i < arguments.length; i++) {
        s = s.replace("{" + (i) + "}", arguments[i]);
    }
    return (s);
};
String.prototype.removeSpaces = function() {
    return this.replace(/ /gi, '');
};
String.prototype.removeExtraSpaces = function() {
    return (this.replace(String.prototype.removeExtraSpaces.re, " "));
};
String.prototype.removeExtraSpaces.re = new RegExp("\\s+", "g");
String.prototype.removeSpaceDelimitedString = function(r) {
    var s = " " + this.trim() + " ";
    return s.replace(" " + r, "").rTrim();
};
String.prototype.isEmail = function() {
    var emailReg = /^\w+([\-.]\w+)*@\w+([\-.]\w+)*\.\w+([\-.]\w+)*$/;
    return emailReg.test(this);
};
String.prototype.isURL = function() {
    var urlReg = /^http(s)?:\/\/([\w\-]+\.)+[\w\-]+(\/[\w- .\/?%&=]*)?$/;
    return urlReg.test(this);
};
String.prototype.isAlpha = function() {
    var aReg = /[a-zA-Z]/g;
    return aReg.test(this);
};
String.prototype.isAlphaNumeric = function() {
    var alphaReg = /[a-zA-Z0-9]/g;
    return alphaReg.test(this);
};
String.prototype.isPositiveNumber = function() {
    var pReg = /(\+)?\d+/g;
    return pReg.test(this);
};
String.prototype.isNegitiveNumber = function() {
    var nReg = /^\-\d+$/g;
    return nReg.test(this);
};
String.prototype.isNumeric = function() {
    return (this.isPositiveNumber() || this.isNegitiveNumber());
};
String.prototype.isLimitNumeric = function(minValue, maxValue) {
    minValue = minValue || Number.MIN_VALUE;
    maxValue = maxValue || Number.MAX_VALUE;
    if (this.isNumeric()) {
        var v = parseInt(this, 10);
        return v >= minValue && v <= maxValue;
    }
    return false;
};
String.prototype.isPositiveDecimal = function() {
    var pdReg = /^(\+)?((\d*\.\d+)|(\d+\.\d*))$/g;
    return (pdReg.test(this) || this.isPositiveNumber());
};
String.prototype.isNegitiveDecimal = function() {
    var ndReg = /^\-(\d+\.\d*)$/g;
    return (ndReg.test(this) || this.isNegitiveNumber());
};
String.prototype.isDecimal = function() {
    return (this.isPositiveDecimal() || this.isNegitiveDecimal());
};
String.prototype.IsLimitDecimal = function(minValue, maxValue) {
    minValue = minValue || Number.MIN_VALUE;
    maxValue = maxValue || Number.MAX_VALUE;
    if (this.isDecimal()) {
        var v = parseFloat(this);
        return v >= minValue && v <= maxValue;
    }
    return false;
};
String.prototype.encodeURI = function() {
    var returnString;
    returnString = escape(this);
    returnString = returnString.replace(/\+/g, "%2B");
    return returnString;
};
String.prototype.decodeURI = function() { return unescape(this); };
String.prototype.byteLength = function() {
    var len = this.length;
    var count = len;
    for (var i = 0; i < len; i++) {
        if (this.charCodeAt(i) > 255) {
            count++;
        }
    }
    return count;
};

String.prototype.limit = function(min, max) {
    min = min || 0;
    max = max || Number.MAX_VALUE;
    return (this.length >= min && this.length <= max);
};
String.prototype.limitByte = function(min, max) {
    min = min || 0;
    max = max || Number.MAX_VALUE;
    var len = this.byteLength();
    return (len >= min && len <= max);
};
String.prototype.isZIP = function() {
    var zReg = /^\d{6}$/;
    return zReg.test(this);
};
String.prototype.isPhone = function() {
    var pReg = /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
    return pReg.test(this);
};
String.prototype.isMobile = function() {
    var mReg = /^((\(\d{2,3}\))|(\d{3}\-))?(13\d{9})|(15\d{9})$/;
    return mReg.test(this);
};
String.prototype.isInList = function(list) {
    var re = eval("/[" + list + "]/");
    return re.test(this);
};

String.prototype.isDate = function() {
	var redate = /((^((1[8-9]\d{2})|([2-9]\d{3}))([\-\/\._])(10|12|0?[13578])([\-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([\-\/\._])(11|0?[469])([\-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([\-\/\._])(0?2)([\-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([\-\/\._])(0?2)([\-\/\._])(29)$)|(^([3579][26]00)([\-\/\._])(0?2)([\-\/\._])(29)$)|(^([1][89][0][48])([\-\/\._])(0?2)([\-\/\._])(29)$)|(^([2-9][0-9][0][48])([\-\/\._])(0?2)([\-\/\._])(29)$)|(^([1][89][2468][048])([\-\/\._])(0?2)([\-\/\._])(29)$)|(^([2-9][0-9][2468][048])([\-\/\._])(0?2)([\-\/\._])(29)$)|(^([1][89][13579][26])([\-\/\._])(0?2)([\-\/\._])(29)$)|(^([2-9][0-9][13579][26])([\-\/\._])(0?2)([\-\/\._])(29)$))/;
    var p;
    var re1 = /(\d{4})[年\.\/\-](\d{1,2})[月\.\/\-](\d{1,2})[日]?$/;
    var re2 = /(\d{1,2})[月\.\/\-](\d{1,2})[日\.\/\-](\d{2})[年]?$/;
    var re3 = /(\d{1,2})[月\.\/\-](\d{1,2})[日\.\/\-](\d{4})[年]?$/;
    if (re1.test(this)) {
        p = re1.exec(this);
        return new Date(p[1], (p[2] - 1), p[3]);
    }
    if (re2.test(this)) {
        p = re2.exec(this);
        return new Date(p[3], (p[1] - 1), p[2]);
    }
    if (re3.test(this)) {
        p = re3.exec(this);
        return new Date(p[3], (p[1] - 1), p[2]);
    }
    return false;
};
String.prototype.isInChinese = function() {
    return (this.length != this.replace(/[^\x00-\xff]/g, "**").length);
};

String.prototype.sub = function(n) {
    var r = /[^\x00-\xff]/g;
    if (this.replace(r, "mm").length <= n) {
        return this;
    }
    n = n - 3;
    var m = Math.floor(n / 2);
    for (var i = m; i < this.length; i++) {
        if (this.substr(0, i).replace(r, "mm").length >= n) {
            return this.substr(0, i) + "...";
        }
    }
    return this;
};

String.prototype.getQueryString = function(name) {
    var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)"), r;
    r = this.match(reg);
    if (r.length > 1) {
        return unescape(r[2]);
    }
    return null;
};
var Request = {
    Url: {
        pathAndQuery: location.href,
        hostName: location.hostname,
        host: location.host,
        port: location.port,
        pathName: location.pathname,
        path: location.pathname,
        protocol: location.protocol,
        query: function() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = {};
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                var strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
    }
};
//---------------------------------------Date------------------------------------
Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};




//---------------------------------------Array------------------------------------
if (!Array.prototype.push) {
    Array.prototype.push = function() {
        var startLength = this.length;
        for (var i = 0; i < arguments.length; i++) {
            this[startLength + i] = arguments[i];
        }
        return this.length;
    };
}

Array.prototype.indexOf = function(p_var) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == p_var) {
            return (i);
        }
    }
    return (-1);
};
Array.prototype.exists = function(p_var) { return (this.indexOf(p_var) != -1); };
Array.prototype.queue = function(p_var) { this.push(p_var); };
Array.prototype.dequeue = function() { return (this.shift()); };
Array.prototype.removeAt = function(p_iIndex) { return this.splice(p_iIndex, 1); };
Array.prototype.remove = function(o) {
    var i = this.indexOf(o);
    if (i > -1){
        this.splice(i, 1);
    }
    return (i > -1);
};
Array.prototype.clear = function() {
    var iLength = this.length;
    for (var i = 0; i < iLength; i++) {
        this.shift();
    }
};
Array.prototype.addArray = function(p_a) {
    if (p_a) {
        for (var i = 0; i < p_a.length; i++) {
            this.push(p_a[i]);
        }
    }
};

//---------------------------------------prototype------------------------------------
/*
var Prototype = {
Version: '@@VERSION@@'
}

var Class = {
create: function() {
return function() { 
this.initialize.apply(this, arguments);
}
}
}

var Abstract = new Object();

Object.prototype.extend = function(object) {
for (property in object) {
this[property] = object[property];
}
return this;
}

if (!Function.prototype.apply) {
Function.prototype.apply = function(object, parameters) {
var parameterStrings = new Array();
if (!object)     object = window;
if (!parameters) parameters = new Array();
    
for (var i = 0; i < parameters.length; i++)
parameterStrings[i] = 'x[' + i + ']';
    
object.__apply__ = this;
var result = eval('obj.__apply__(' + 
parameterStrings[i].join(', ') + ')');
object.__apply__ = null;
    
return result;
}
}


Function.prototype.bind = function(object) {
var method = this;
return function() {
method.apply(object, arguments);
}
}

Function.prototype.bindAsEventListener = function(object) {
var method = this;
return function(event) {
method.call(object, event || window.event);
}
}

var Try = {
these: function() {
var returnValue;
    
for (var i = 0; i < arguments.length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) {}
}
    
return returnValue;
}
}
*/
/*--------------------------------------------------------------------------*/

//---------------------------------------Element未完成，不可用------------------------------------
var Elements ={};

Elements.scrollPosition = function() {
    var L, T, w = window, d = document, dd = d.documentElement;
    if (w.pageXOffset) {
        L = w.pageXOffset;
        T = w.pageYOffset;
    }
    else if (dd && dd.scrollLeft) {
        L = dd.scrollLeft;
        T = dd.scrollTop;
    }
    else if (d.body) {
        L = d.body.scrollLeft;
        T = d.body.scrollTop;
    }
    return { "left": L, "top": T };
};

Elements.realOffset = function(o) {
    var x =0, y = 0;
    do {
        x += o.scrollLeft || 0;
        y += o.scrollTop || 0;
        o = o.parentNode;
    } while (o);
    return { "x": x, "y": y };
};

Elements.cumulativeOffset = function(o) {
    var x =0, y = 0;
    do {
        x += o.offsetLeft || 0;
        y += o.offsetTop || 0;
        o = o.offsetParent;
    } while (o);
    return { "x": x, "y": y };
};

//---------------------------------------window.scrollTop------------------------------------
if (!window.scrollTop) {
    window.scrollTop = function() {
        var scrollPos = 0;
        if (typeof window.pageYOffset != 'undefined') {
            //ff
            scrollPos = window.pageYOffset;
        }
        else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
            //ie7
            scrollPos = document.documentElement.scrollTop;
        }
        else if (typeof document.body != 'undefined') {
            //ie6
            scrollPos = document.body.scrollTop;
        }
        return scrollPos;
    };
}
