/**
*	对于内建对象的扩展
*	@Author danson
*/

/**************************对String 对象的扩展*********************************/
/**
 * 替换字符串中指定的字符串
 */
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;
}

/*******************对数组的扩展********************************/
/*
*附加另一个数组
*/
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.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;
}
