/**
*	对于内建对象的扩展
*	@Author danson
*/

/**************************对String 对象的扩展*********************************/
String.EMPTY = "";
/**
 * 替换字符串中指定的字符串
 */
String.prototype.relaceAll = function(oldstr, newstr){
	if (oldstr == null || newstr == null || typeof oldstr == "undefined" || typeof newstr == "undefined")
		throw new Error();
	
	// 使用正则表达式
	// return this.replace(new RegExp(oldstr, "ig"), newstr);
	var index = 0;
	var tmp = this;
	var sl = newstr.length - oldstr.length;
	while(tmp.indexOf(oldstr, index) != -1){
		tmp = tmp.replace(oldstr, newstr);
		index = tmp.indexOf(oldstr, index) + sl;
	}
	return tmp;
}

/*
*去除两端空格
*/
String.prototype.trim = function(){
	return this.replace(/(^\s+)|(\s+$)/g, "");
}

/*
*忽略大小写的比较, 不忽略大小写的比较用==
*/
String.prototype.equalsIgnoreCase = function(str){
	return this.toLowerCase() == str.toLowerCase();
}

/*
*判断字符串是否以指定的字符串开始
*/
String.prototype.startsWith = function(str) {
    return this.substr(0, str.length) == str;
}

/*
*判断字符串是否以指定的字符串结束
*/
String.prototype.endsWith = function(str) {
    return this.substr(this.length - str.length) == str;
}

/**
 * 判断是否包含指定字符
 * @param {Object} str
 * @param {Object} sepr
 */
String.prototype.contains = function(str, sper) {
	return this.indexOf(str) > -1;
}

String.prototype.isEmpty = function() {
	if (this == void 0) {throw new Error("Illegal argument error.");}
	return this == null || this.length == 0;
}

String.prototype.substringBefore = function(separator) {
	if (this.isEmpty() || separator == null) {
        return this;
    }
    if (separator.length == 0) {
        return String.EMPTY;
    }
    var pos = this.indexOf(separator);
    if (pos == -1) {
        return this;
    }
    return this.substring(0, pos);
}

/**
*	很好很和谐, add by danson
*/
String.prototype.substringAfterLast = function(separator) {
	if (this.isEmpty()) {
		return this;
	}
	if (separator.isEmpty()) {
		return String.EMPTY;
	}
	var pos = this.lastIndexOf(separator);
	if (pos == -1 || pos == (this.length - separator.length)) {
		return String.EMPTY;
	}
	return this.substring(pos + separator.length);
}

String.prototype.capitalize2 = function () {
    if (this.isEmpty()) {
        return this;
    }
    return this.charAt(0).toUpperCase() + this.substring(1);
}

String.prototype.uncapitalize2 = function() {
    if (this.isEmpty()) {
        return this;
    }
    return this.charAt(0).toLowerCase() + this.substring(1);
}


/******************对Date对象的扩展*****************************/
/*
*IE和mozilla年的差量
*/
Date.DISPERSION = 1900;

/*
*试图解析参数字符串给定的日期,separator为日期分割符,分割符不能为:
*/
Date.parseText = function(str, separator) {
	var temp = str.replace("T", " ");
	var sp = "-";
	if(separator != null)
		sp = separator;
	temp = temp.relaceAll(sp, "/");
	var time = Date.parse(temp);
	var o = new Date();
	o.setTime(time);
	return o;
}

/*
*验证日期格式, 注意分割符是-
*/
Date.validateDate = function(str){
	var pattern = /^(19[0-9]{2}|[2-9][0-9]{3})-((0(1|3|5|7|8)|10|12)-(0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0(4|6|9)|11)-(0[1-9]|1[0-9]|2[0-9]|30)|(02)-(0[1-9]|1[0-9]|2[0-9]))$/;
	var res = str.match(pattern);
	return res ? true : false;
}

/*
*验证时间格式
*/
Date.validateTime = function(str){
	var pattern = /^(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){1,2}$/;
	var res = str.match(pattern);
	return res ? true : false; 
}

/**
*验证时间戳(yyyy-MM-dd HH:mm:ss)
*/
Date.validateTimeStamp = function(str){
	if (str == null || typeof str == "undefined") {
		throw new Error("Illegal argument.");
	}
	var fragments = str.trim().split(' ');
	alert(fragments.length);
	if (fragments.length != 2) return false;
	return Date.validateDate(fragments[0]) && Date.validateTime(fragments[1]);
}

/*
*格式化日期
*/
Date.prototype.formatDate = function(separator){
	var sp = "-"
	if(separator != null)
		sp = separator;
	return (window.ActiveXObject?this.getYear() : Date.DISPERSION + this.getYear())
		+ sp + ((this.getMonth() + 1) > 9 ? (this.getMonth() + 1) : "0" + (this.getMonth() + 1))
		+ sp + (this.getDate() > 9 ? this.getDate() : "0" + this.getDate());
}

/*
*格式化日期和时间
*/
Date.prototype.formatDateTime = function(dateSeparator, dateTimeSeparator){
	return this.formatDate(dateSeparator) 
		+ (dateTimeSeparator == null ? " " : dateTimeSeparator) 
		+ (this.getHours() > 9 ? this.getHours() : "0" + this.getHours())  
		+ ":" + (this.getMinutes() > 9? this.getMinutes() : "0" + this.getMinutes())+ ":00"	
}

/*
*计算日期间隔
*/
Date.getDuration = function(daystr1, daystr2) {
	var day1 = Date.parseText(daystr1);
	var day2 = Date.parseText(daystr2);
	return parseInt(Math.abs(day1 - day2) / 1000 / 60 / 60 /24);
}


/*******************对数组的扩展********************************/
/*
*附加另一个数组
*/
Array.prototype.appendArray = function(arr){
	var tempArr = this;
	for(var i=0; i<arr.length; i++){
		tempArr = tempArr.concat(arr[i]);
	}
	return tempArr;
}

Array.prototype.indexOf = function(obj) {
	var result = -1;
	for(var i = 0; i < this.length; i++) {
		if(this[i] == obj) {
			result = i;
			break;
		}
	}
	return result;
}	

Array.prototype.contains = function(obj) {
	return this.indexOf(obj) > -1;
}

Array.prototype.append = function(obj,nodup) {
	if(!(this.contains(obj) && nodup)) {
		this[this.length] = obj;
	}
}

Array.prototype.remove = function(obj) {
	if(this.contains(obj)) {
		var index = this.indexOf(obj);
		for(var i = index; i < this.length - 1; i++) {
			this[i] = this[i + 1];
		}
		this.length--;
	}
}

Array.prototype.clear = function() {
	var length = this.length;
	for (var i = 0; i < length; i++) {
		this.pop();
	}
}


/************************用js实现的一个Map***********************/

function Map(){
	this.container = new Object();
}

Map.prototype.put = function(key, value){
	this.container[key] = value;
}

Map.prototype.get = function(key){
	return this.container[key];
}

Map.prototype.contains = function(key) {
	return this.get(key) != undefined;
}

Map.prototype.keySet = function() {
	var keyset = new Array();
	var count = 0;
	for (var key in this.container) {
		// 跳过object的extend函数
		if (key == 'extend')
			continue;
			
		keyset[count] = key;
		count++;
	}
	
	return keyset;
}

Map.prototype.size = function() {
	var count = 0;
	for (var key in this.container) {
		// 跳过object的extend函数
		if (key == 'extend')
			continue;
			
		count++;
	}
	
	return count;
}

Map.prototype.toString = function(){
	var str = "";
	for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
		str = str + keys[i] + "=" + this.container[keys[i]] + ";\n";
	}
	return str;
}
