/* Array class extension */
Array.prototype.moveUp=function(key){
	if(key==0) return this;
	if(key>=(this.length)) return this;
	if(key>1) var newArr=this.slice(0, key -1);
	else var newArr=new Array;
	newArr[newArr.length]=this[key];
	newArr[newArr.length]=this[key-1];
	var endArr=this.slice(key+1, this.length);
	return newArr.concat(endArr);
}

Array.prototype.moveDown=function(key){
	if(key>=(this.length-1)) return this;
	if(key>0) var newArr=this.slice(0, key);
	else var newArr=new Array;
	newArr[newArr.length]=this[key +1];
	newArr[newArr.length]=this[key];
	if(this.length>(key+2)){
		var endArr=this.slice(key+2, this.length);
		return newArr.concat(endArr);
	}
	return newArr;
}

Array.prototype.moveToTop=function(key){
	if(key==0) return this;
	if(key>=(this.length)) return this;
	var startArr=new Array(this[key]);
	var middleArr=this.slice(0, key);
	var endArr=this.slice(key+1, this.length);
	return startArr.concat(middleArr, endArr);
}

Array.prototype.moveToBottom=function(key){
	if(key>=(this.length-1)) return this;
	if(key>0) var startArr=this.slice(0, key);
	else var startArr=new Array;
	var middleArr=this.slice(key+1, this.length);
	var endArr=new Array(this[key]);
	return startArr.concat(middleArr, endArr);
}

Array.prototype.indexOf=function(str){
	for(var i=0;i<this.length;i++){
		if (this[i] instanceof Array && this[i].equals(str)) return i;
		else if(this[i]==str) return i;
	}
	return -1;
}
Array.prototype.equals=function(o){
	if (o==null) return false;
	if (!(o instanceof Array)) return false;
	if (this.length != o.length) return false;
	for(var i=0;i<this.length;i++){
		if(this[i] instanceof Array
			&& !this[i].equals(o[i])) return false;
		else if(this[i] != o[i]) return false;
	}
	return true;
}

Array.prototype.has=function(str){
	return (this.indexOf(str)>=0);
}

Array.prototype.deleteItem=function(i){
	if(i<0||i>(this.length-1)) return false;
	if(i==(this.length-1)){
		this.length--;
		return true;
	}
	for(var i=(i+1);i<this.length;i++){
		this[i-1]=this[i];
	}
	this.length--;
	return true;
}

Array.prototype.deleteItemHash=function(key){
	var ret=new Array;
	for(var k in this){
		if(k!=key) ret[k]=this[k];
	}
	return ret;
}

/* String class extension */
String.prototype.trimLeading=function(){
	return this.replace(/^\s*/g,"");
}

String.prototype.trimTailing=function(){
	return this.replace(/\s*$/g,"");
}

String.prototype.trim=function(){
	return this.replace(/^\s*|\s*$/g,"");
}

String.prototype.stripTags=function(){
	return this.replace(/<\/?[^>]+>/gi, '');
}

String.prototype.escHtml=function(){//cp from prototype
	var div = document.createElement('div');
	var text = document.createTextNode(this);
	div.appendChild(text);
	return div.innerHTML;
}

String.prototype.unescHtml=function(){
	var div = document.createElement('div');
	div.innerHTML = this.stripTags();
	return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
}

String.prototype.escJs=function(){
	return this.replace(/"/g,'\\"').replace(/'/g,"\\'");
}

String.prototype.isBlank=function(){
	return this.replace(/^\s*|\s*$/g,"")=="";
}

String.prototype.isDigit=function(){
	return !isNaN(this);
}

String.prototype.isInteger=function(){
	if(this.isBlank()) return false;
	for(var i=0;i<this.length;i++){
		if(!this.charAt(i).isDigit()) return false;
	}
	return true;
}

String.prototype.isNumeric=function(){
	return parseFloat(this,10)==(this*1);
}
    
String.prototype.isValidEmail=function(){
	return /^[\w\.=-]+@[\w\.-]+\.[\w]{2,6}$/.test(this);
}

String.prototype.testMinLen=function(len){
	return !this.isBlank()&&this.length>=len;
}

String.prototype.testMaxLen=function(len){
	return this.isBlank()||this.length<=len;
}
String.prototype.endWith=function(suffix){
	var pattern =new RegExp(suffix + "$");
	return this.match(pattern) != null;
}
String.prototype.startWith=function(suffix){
	var pattern =new RegExp("^" + suffix);
	return this.match(pattern) != null;
}
