
/******** Array *********/
Array.prototype.append = function(obj, nodup) {  
    if(!(nodup && this.contains(obj))) {  
        this[this.length] = obj;  
    }  
}  
  
Array.prototype.contains = function(obj) {  
    return (this.indexOf(obj)>=0);  
}  
  
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.clear = function() {  
    this.length = 0;  
}  
  
Array.prototype.insertAt = function(index, obj) {  
    this.splice(index, 0, obj);  
}  
  
Array.prototype.removeAt = function(index) {  
    this.splice(index, 1);  
}
  
Array.prototype.remove = function(obj) {  
    var index = this.indexOf(obj);  
    if(index>=0)  
        this.removeAt(index);  
}
Array.prototype.unique=function(){
	var l=this.length;
	if(l<2) return this;
	var arr=[];
	var tmp={}
	for(var i=0;i<l;i++){
		if(!tmp[this[i]]){
			arr.push(this[i]);
			tmp[this[i]]=1;
		}
	}
	return arr;
}
/******** string *********/
/*是否为数字
String.prototype.isDigit  =  function()
{
	var patrn=/^[0-9]{1,20}$/; 
	if (!patrn.exec(this)) return false;
	return true;
}
*/
String.prototype.trim  =  function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.ltrim  =  function()
{
	return this.replace(/(^\s*)/g,"");
}

String.prototype.rtrim  =  function()
{
	return this.replace(/(\s*$)/g,"");
}

String.prototype.replaceAll  = function(s1,s2){    
    return this.replace(new RegExp(s1,"g"),s2);    
}

String.prototype.clearAllSpace  =  function()
{
	return this.replace(/\s/g, '');
}

/*
**将多个连续空格替换成一个
*/
String.prototype.many2one  =  function(){
	var blankBefore = false;
	var result = "";
	for(var i=0,j=this.length; i<j; i++){
		var chr = this.charAt(i);
		if(chr!=" "){
			result+=chr;
			blankBefore=false;
		}else{
			result+= blankBefore?"":" ";
			blankBefore=true;
		}
	}
	return result;
};
/*
String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}  
*/
String.prototype.endsWith = function(str)
{
    var lastIndex = this.lastIndexOf(str);
    return (lastIndex != -1) && (lastIndex + str.length == this.length);
}
String.prototype.startsWith = function(str){  
	return !this.indexOf(str);  
}

/*
**http://blog.csdn.net/kimsoft/archive/2007/08/29/1764261.aspx
**only for ie6
*/
function StringBuffer(){};

StringBuffer.prototype = {
    initialize : function (string) {
        this._strings = [];
        this.append(string);
    },
    
    append : function (string) {
        if (string != undefined) {
            this._strings.push(string);
        }
        return this;
    }, 

    toString : function (separator) {
           return this._strings.join(separator || "");
    }
};
/*
**HashTable
*	var hashtable = new HashTable();
*	for(var i = 0 ;i<100000;i++)
*	{
*		hashtable.Add(i*1.2,i);
*	}
*	var now = (new Date()).getTime();
*	var temp ="";
*	for(var i=0;i<1000;i++)
*	{
*		if(hashtable.Contains(1+i*2.8))
*		{  
*			hashtable.Remove(i*2.8);
*		}
*	}
*	hashtable.Clear();
*	$("output").innerHTML = " time pass:"+((new Date()).getTime()-now)+"ms";
*	alert(hashtable.Count());

*/
function HashTable(){};
HashTable.prototype=
{
    initialize:function()
    {
        this._content ={};
    },
    Count:function()
    {
        var count = 0;
        for(var i in this._content) count++;
        return count;
    },
    Items:function(key)
    {
        if(this.Contains(key))
        {
            return this._content[key];
        }
    },
    Add:function(key,value)
    {
        if(this._content.hasOwnProperty(key)) 
        {
            return false;
        }
        else
        {
            this._content[key] = value;
            return true;
        }
    },
    Clear:function()
    {
        this._content  = {};
    },
    Contains:function(key)
    {
        return this._content.hasOwnProperty(key);
    },
    Remove:function(key)
    {
        delete this._content[key];
    }
}

/*
**ArrayList
*/
function ArrayList(){   
 this.index = -1;   
 this.array = new Array();    
}   
ArrayList.prototype.add = function (obj){   
    this.index = this.index + 1;   
    this.array[eval(this.index)] = obj;   
}   
ArrayList.prototype.get = function (index){return this.array[eval(index)];}   
ArrayList.prototype.size = function (){return this.index+1;}   
ArrayList.prototype.remove = function (index){   
    var j = 0;   
    var arrThis = this.array;    
    var arrTemp = new Array();   
    for(w=0;w<arrThis.length;w++){   
        if (eval(index)!=eval(w)) {   
            arrTemp[j] = arrThis[w];       
            j++;   
        }      
    }      
    this.array = arrTemp;   
    this.index = eval(j-1);    
} 

function insertHtml(where, el, html){       
      
    where = where.toLowerCase();       
    if(el.insertAdjacentHTML){       
      
        switch(where){       
            case "beforebegin":       
                el.insertAdjacentHTML('BeforeBegin', html);       
                return el.previousSibling;       
            case "afterbegin":       
                el.insertAdjacentHTML('AfterBegin', html);       
                return el.firstChild;       
            case "beforeend":       
                el.insertAdjacentHTML('BeforeEnd', html);       
                return el.lastChild;       
            case "afterend":       
                el.insertAdjacentHTML('AfterEnd', html);       
                return el.nextSibling;       
        }       
        throw 'Illegal insertion point -> "' + where + '"';       
    }       
                     
    var range = el.ownerDocument.createRange();       
    var frag;       
    switch(where){       
         case "beforebegin":       
            range.setStartBefore(el);       
            frag = range.createContextualFragment(html);       
            el.parentNode.insertBefore(frag, el);       
            return el.previousSibling;       
         case "afterbegin":       
            if(el.firstChild){       
                range.setStartBefore(el.firstChild);       
                frag = range.createContextualFragment(html);       
                el.insertBefore(frag, el.firstChild);       
                return el.firstChild;       
             }else{       
                el.innerHTML = html;       
                return el.firstChild;       
             }       
        case "beforeend":       
            if(el.lastChild){       
                range.setStartAfter(el.lastChild);       
                frag = range.createContextualFragment(html);       
                el.appendChild(frag);       
                return el.lastChild;       
            }else{       
                el.innerHTML = html;       
                return el.lastChild;       
            }       
        case "afterend":       
            range.setStartAfter(el);       
            frag = range.createContextualFragment(html);       
            el.parentNode.insertBefore(frag, el.nextSibling);       
            return el.nextSibling;       
    }       
    throw 'Illegal insertion point -> "' + where + '"';       
}