﻿Function.ValidateParameters = function(parameters) {
    for (var name in parameters) {
        if (name == "notempty") {
            if (parameters[name].trim() == "") {
                return false;
            }
        }
        else if (name == 'invalidwords') {
            var reg = /[\,,\{,\},\',\"]/;
            return reg.test(parameters[name]);
        }
    }
    return true;
}
String.prototype.startsWith = function(prefix) {
    return (this.substr(0, prefix.length) === prefix);
}
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
}
String.prototype.trimEnd = function() {
    return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function() {
    return this.replace(/^\s+/, '');
}
Array.add = function(array, item) {
    array[array.length] = item;
}
Array.clone = function(array) {
    if (array.length === 1) {
        return [array[0]];
    }
    else {
        return Array.apply(null, array);
    }
}
Array.indexOf = function(array, item, start) {
    if (typeof (item) === "undefined") return -1;
    var length = array.length;
    if (length !== 0) {
        start = start - 0;
        if (isNaN(start)) {
            start = 0;
        }
        else {
            if (isFinite(start)) {
                start = start - (start % 1);
            }
            if (start < 0) {
                start = Math.max(0, length + start);
            }
        }
        for (var i = start; i < length; i++) {
            if ((typeof (array[i]) !== "undefined") && (array[i] === item)) {
                return i;
            }
        }
    }
    return -1;
}
Array.remove = function(array, item) {
    var index = Array.indexOf(array, item);
    if (index >= 0) {
        array.splice(index, 1);
    }
    return (index >= 0);
}

jQuery.identify = 1;

jQuery.extend({
    getNewID: function() {
        return jQuery.identify++;
    },
    isEmpty: function(v, allowBlank) {
        return v === null || v === undefined || ((jQuery.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
    },
    isObject: function(v) {
        return v && typeof v == "object";
    },
    isNumber: function(v) {
        return typeof v === 'number' && isFinite(v);
    },
    isString: function(v) {
        return typeof v === 'string';
    },
    isBoolean: function(v) {
        return typeof v === 'boolean';
    },
    isPrimitive: function(v) {
        return jQuery.isString(v) || jQuery.isNumber(v) || jQuery.isBoolean(v);
    },
    isDefined: function(v) {
        return typeof v !== 'undefined';
    },
    urlDecode: function(string, overwrite) {
        var obj = {},
                pairs = string.split('&'),
                d = decodeURIComponent,
                name,
                value;
        oo.each(pairs, function(pair) {
            pair = pair.split('=');
            name = d(pair[0]);
            value = d(pair[1]);
            obj[name] = overwrite || !obj[name] ? value :
                            [].concat(obj[name]).concat(value);
        });
        return obj;
    },
    urlEncode: function(o, pre) {
        var undef, buf = [], key, e = encodeURIComponent;

        for (key in o) {
            undef = !$.isDefined(o[key]);
            oo.each(undef ? key : o[key], function(val, i) {
                buf.push("&", e(key), "=", (val != key || !undef) ? e(val) : "");
            });
        }
        if (!pre) {
            buf.shift();
            pre = "";
        }
        return pre + buf.join('');
    },
    invokeWebMethod: function(s) {
        function success(data, status) {
            if (s.success) {
                if (data && data.d) {
                    s.success(data.d, status, s.userContext);
                }
                else if (s.failed) {
                    s.failed(null, status);
                }
            }
        }
        function complete(xhr, status) {
            if (status != 'success') {
                if (s.failed) {
                    s.failed(xhr, status);
                }
            }
        }
        var async = true;
        if (s.async === false) async = false;
        var data = s.data ? json.encode(s.data) : '{}';
        var url = s.servicePath + "/" + encodeURIComponent(s.methodName);
        return $.ajax({
            type: "POST",
            url: url,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            data: data,
            timeout: s.timeout,
            complete: complete,
            success: success,
            async: async
        });
    },
    redirect: function(url) {
        url = url.replace("~", "");
        location.href = url;
    },
    checkResponse: function(data) {
        if (data.status == -500 || data.status == -400) {
            if (data.status == -400) {
                $.redirect(data.url);
            }
            else if (data.status = -500) {
                alert(data.message);
            }
            return false;
        }
        return true;
    },
    getInfoMsg: function() {
        if (typeof (infoMessage) == 'undefined') {
            return false;
        }
        return infoMessage;
    },
    formatNumber: function(s, n) {
        var index = s.indexOf('.');
        if (index >= 0) {
            var prefix = s.length - index - 1;
            if (prefix > options) {
                s = s.substring(0, index + options + 1);
            }
        }
        n = n > 0 && n <= 20 ? n : 2;
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
        var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
        t = "";
        for (i = 0; i < l.length; i++) {
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
        }
        return t.split("").reverse().join("") + "." + r;
    },
    getDateString: function() { //获取yyyy-MM-dd格式的当前日期 add by shangdu.lin 20100729    	
        var now = new Date();
        var year, month, date;
        year = now.getFullYear();
        month = now.getMonth() + 1;
        date = now.getDate();
        month = ((month < 10) ? "0" : "") + month;
        date = ((date < 10) ? "0" : "") + date;

        var dateNow = year + "-" + month + "-" + date + " ";
        return dateNow;
    }
});

jQuery.extend({
    JSON: new (function() {
        var useHasOwn = !!{}.hasOwnProperty,
              isNative = function() {
                  var useNative = null;
                  return function() {
                      if (useNative === null) {
                          useNative = window.JSON && JSON.toString() == '[object JSON]';
                      }

                      return useNative;
                  };
              } (),
              pad = function(n) {
                  return n < 10 ? "0" + n : n;
              },
              doDecode = function(json) {
                  return eval("(" + json + ')');
              },
              doEncode = function(o) {
                  if (typeof o == "undefined" || o === null) {
                      return "null";
                  } else if (jQuery.isArray(o)) {
                      return encodeArray(o);
                  } else if (Object.prototype.toString.apply(o) === '[object Date]') {
                      return jQuery.JSON.encodeDate(o);
                  } else if (typeof o == "string") {
                      return encodeString(o);
                  } else if (typeof o == "number") {
                      return isFinite(o) ? String(o) : "null";
                  } else if (typeof o == "boolean") {
                      return String(o);
                  } else {
                      var a = ["{"], b, i, v;
                      for (i in o) {
                          if (!useHasOwn || o.hasOwnProperty(i)) {
                              v = o[i];
                              switch (typeof v) {
                                  case "undefined":
                                  case "function":
                                  case "unknown":
                                      break;
                                  default:
                                      if (b) {
                                          a.push(',');
                                      }
                                      a.push(doEncode(i), ":",
                                        v === null ? "null" : doEncode(v));
                                      b = true;
                              }
                          }
                      }
                      a.push("}");
                      return a.join("");
                  }
              },
              m = {
                  "\b": '\\b',
                  "\t": '\\t',
                  "\n": '\\n',
                  "\f": '\\f',
                  "\r": '\\r',
                  '"': '\\"',
                  "\\": '\\\\'
              },
            encodeString = function(s) {
                if (/["\\\x00-\x1f]/.test(s)) {
                    return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return "\\u00" +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    }) + '"';
                }
                return '"' + s + '"';
            },
            encodeArray = function(o) {
                var a = ["["], b, i, l = o.length, v;
                for (i = 0; i < l; i += 1) {
                    v = o[i];
                    switch (typeof v) {
                        case "undefined":
                        case "function":
                        case "unknown":
                            break;
                        default:
                            if (b) {
                                a.push(',');
                            }
                            a.push(v === null ? "null" : jQuery.JSON.encode(v));
                            b = true;
                    }
                }
                a.push("]");
                return a.join("");
            };
        this.encodeDate = function(o) {
            return '"' + o.getFullYear() + "-" +
                        pad(o.getMonth() + 1) + "-" +
                        pad(o.getDate()) + "T" +
                        pad(o.getHours()) + ":" +
                        pad(o.getMinutes()) + ":" +
                        pad(o.getSeconds()) + '"';
        };
        this.encode = function() {
            var ec;
            return function(o) {
                if (!o) return "";
                if (!ec) {
                    // setup encoding function on first access
                    ec = isNative() ? JSON.stringify : doEncode;
                }
                return ec(o);
            };
        } ();
        this.decode = function() {
            var dc;
            return function(json) {
                if (json == '') return null;
                if (!dc) {
                    // setup decoding function on first access
                    dc = isNative() ? JSON.parse : doDecode;
                }
                return dc(json);
            };
        } ();
    })()
});

jQuery.extend({
    xQuant: {
        version: '1.0',
        apply: function(o, c, defaults) {
            if (defaults) {
                jQuery.xQuant.apply(o, defaults);
            }
            if (o && c && typeof c == 'object') {
                for (var p in c) {
                    o[p] = c[p];
                }
            }
            return o;
        },
        toString: function() {
            return "jQuery.xQuant";
        }
    }
});
(function() {
    isIterable = function(v) {
        if (jQuery.isArray(v) || v.callee) {
            return true;
        }
        if (/NodeList|HTMLCollection/.test(toString.call(v))) {
            return true;
        }
        return ((v.nextNode || v.item) && jQuery.isNumber(v.length));
    },
        ua = navigator.userAgent.toLowerCase(),
        check = function(r) {
            return r.test(ua);
        },
        isIE = check(/msie/),
        jQuery.xQuant.apply(jQuery.xQuant, {
            extend: function() {
                var io = function(o) {
                    for (var m in o) {
                        this[m] = o[m];
                    }
                };
                var oc = Object.prototype.constructor;
                return function(sb, sp, overrides) {
                    if (jQuery.isObject(sp)) {
                        overrides = sp;
                        sp = sb;
                        sb = overrides.constructor != oc ? overrides.constructor : function() { sp.apply(this, arguments); };
                    }
                    var F = function() { },
                    sbp,
                    spp = sp.prototype;

                    F.prototype = spp;
                    sbp = sb.prototype = new F();
                    sbp.constructor = sb;
                    sb.superclass = spp;
                    if (spp.constructor == oc) {
                        spp.constructor = sp;
                    }
                    sb.override = function(o) {
                        Query.xQuant.override(sb, o);
                    };
                    sbp.superclass = sbp.supr = (function() {
                        return spp;
                    });
                    sbp.override = io;
                    jQuery.xQuant.override(sb, overrides);
                    sb.extend = function(o) { jQuery.xQuant.extend(sb, o); };
                    return sb;
                };
            } (),
            override: function(origclass, overrides) {
                if (overrides) {
                    var p = origclass.prototype;
                    jQuery.xQuant.apply(p, overrides);
                    if (jQuery.xQuant.isIE && overrides.toString != origclass.toString) {
                        p.toString = overrides.toString;
                    }
                }
            },
            each: function(array, fn, scope) {
                if (jQuery.isEmpty(array, true)) {
                    return;
                }
                if (!isIterable(array) || jQuery.isPrimitive(array)) {
                    array = [array];
                }
                for (var i = 0, len = array.length; i < len; i++) {
                    if (fn.call(scope || array[i], array[i], i, array) === false) {
                        return i;
                    };
                }
            },
            namespace: function() {
                var o, d;
                jQuery.xQuant.each(arguments, function(v) {
                    d = v.split(".");
                    o = window[d[0]] = window[d[0]] || {};
                    jQuery.xQuant.each(d.slice(1), function(v2) {
                        o = o[v2] = o[v2] || {};
                    });
                });
                return o;
            },
            isIE: isIE,
            createDelegate: function(scope, method) {
                return function() {
                    return method.apply(scope, arguments);
                }
            }
        });
})();
$.fn.numeralFormat = function(options) {
    this.blur(function() {
        if ($(this).val() != '') {
            var n = options;
            var s = $(this).val();
            var index = s.indexOf('.');
            if (index >= 0) {
                var prefix = s.length - index - 1;
                if (prefix > options) {
                    s = s.substring(0,index + options+1);
                }
            }
            n = n > 0 && n <= 20 ? n : 2;
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
            var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
            t = "";
            for (i = 0; i < l.length; i++) {
                t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
            }
            $(this).val(t.split("").reverse().join("") + "." + r);
        }
    });
    this.focus(function() {
        if ($(this).val() != '') {
            $(this).val(Number($(this).val().replace(/,/g, "")));
            var rng = $(this).get(0).createTextRange();
            rng.collapse(false);
            rng.select();
        }
    });
    return this;
}
$.fn.numeral = function() {
    $(this).css("ime-mode", "disabled");
    this.bind("keypress", function() {
        if (event.keyCode == 46) {
            if (this.value.indexOf(".") != -1) {
                return false;
            }
        } else {
            return event.keyCode >= 46 && event.keyCode <= 57;
        }
    });
    this.bind("blur", function() {
        if (this.value.lastIndexOf(".") == (this.value.length - 1)) {
            this.value = this.value.substr(0, this.value.length - 1);
        }
    });
    this.bind("paste", function() {
//        var s = clipboardData.getData('text');
//        if (/\D/.test(s));
//        value = s.replace(/^0*/, '');
//        return false;
    });
    this.bind("dragenter", function() {
        return false;
    });
    return this;
};

oo = xQuant = jQuery.xQuant;
json = xQuant.JSON = jQuery.JSON;
xQuant.Component = function() {
    this.__list = {};
};
xQuant.Component.prototype = {
    __getEvent: function(id) {
        if (!this.__list[id]) {
            this.__list[id] = [];
        }
        return this.__list[id];
    },
    addHandler: function(id, handler) {
        Array.add(this.__getEvent(id), handler);
    },
    removeHandler: function(id, handler) {
        var evt = this.__getEvent(id);
        if (!evt) return;
        Array.remove(evt, handler);
    },
    getHandler: function(id) {
        var evt = this.__getEvent(id);
        if (!evt || (evt.length === 0)) return null;
        evt = Array.clone(evt);
        return function(source, args) {
            for (var i = 0, l = evt.length; i < l; i++) {
                evt[i](source, args);
            }
        }
    },
    trace: function(message) {
        var node = document.getElementById("divTrace");
        if (node) {
            node.innerHTML += "\r\n" + message;
        }
    }
};



