/* ********************************************************** */
/* ***	FILE ID:		atom_jpl.js										* */
/* ***	FILE TITLE:	Javascript Prototype Library				* */
/* ***	DESCRIPTION:	Contains extended properties and		* */
/* ***		methods of intrinsic javascript object				* */
/* ***		prototypes													* */
/* ***	COMPATABILITY:	IE6+											* */
/* ***	VERSION:			2.0											* */
/* ********************************************************** */
/* *** Index:																* */
/* *** ---------------------------------------------------- * */
/* *** line#:Object:method:description								* */
/* *** ---------------------------------------------------- * */
/* *** 20:String:begsin: test if string begins with X			* */
/* *** 37:String:endsin: test if string ends with X			* */
/* ********************************************************** */
/* ******************************************************* */
/* ***	GLOBAL DECLARATIONS		 								  */
/* ******************************************************* */
//act_scrpt.push("atom_jpl.js");//add this file to active scripts array
/* ******************************************************* */
/* ***	PROTOTYPE:	String.begsin								* */
/* ***	DESCRIPTION:	Returns 0 if string begins with  * */
/* ***						search string, -1 if not			* */
/* ***	PARAMETERS:													* */
/* ***		s	=	search string									* */
/* ******************************************************* */
String.prototype.begsin=function(s){
	if((s=="")||(s=="null")){
		return 0;
	}
	var x1=s.length;
	if(this.indexOf(s)==0){
		return 1;
	}
	return 0;
}
String.prototype.capitalize=function(){
	return this.charAt(0).toUpperCase()+this.slice(1);
}
String.prototype.contains=function(s){
	var rg=new RegExp(s);
	//var rslt=this.match(rg);
	var rslt=rg.test(this);
	return rslt;
}
/* ******************************************************* */
/* ***	PROTOTYPE:	String.endsin								* */
/* ***	DESCRIPTION:	Returns 0 to n if string begins  * */
/* ***						with search string, -1 if not		* */
/* ***	PARAMETERS:													* */
/* ***		s	=	search string									* */
/* ******************************************************* */
String.prototype.endsin = function(s) {
	if ((s=="")||(s=="null")) {
		return -1;
	}
	var x1 = s.length;
	var x2 = this.length - x1;
	if(this.lastIndexOf(s)==x2) {
		return x2;
	}
	return -1;
}
String.prototype.supplant=function(o){
	return this.replace(/{([^{}]*)}/g,function(a,b){var r=o[b];return typeof r==='string'?r:a;});	
	/* *** EXAMPLE USAGE :
	var template='<table border="{border}"><tr><th>Last</th><td>{last}</td></tr>'+'<tr><th>First</th><td>{first}</td></tr></table>';
	var data={
		 "first": "Carl",
		 "last": "Hollywood",
		 "border": 2 
	};	
	mydiv.innerHTML = template.supplant(data); */
}
Array.prototype.addUnique=function(v){
	if(!this.contains(v)){
		this.push(v);
	}
}
Array.prototype.contains=function(obj){
	//handles ie8 strings and objects
	var i=this.length;
	while (i--){
		if(this[i]===obj){
			return true;
		}
	}
	return false;
}
Array.prototype.insert=function(v,p){
	var s1=this.slice(0,p);
	var s2=this.slice(p);
	return new Array.concat(s1,v,s2);
}
/* **********************************************
*** PROTOTTYPE:	Array.mergeUnique(array)		*
*** Description:	returns a merged array with	*
***		only unique values							*
************************************************/
Array.prototype.mergeUnique=function(ary){
	var mx=this.length;
	var rslt=new Array();
	while(mx--){
		rslt.push(this[mx]);
	}
	ary=ary.reverse();
	mx=ary.length;
	while(mx--){
		if(!rslt.contains(ary[mx])){
			rslt.push(ary[mx]);
		}
	}
	return rslt;
}
/* ******************************************************* */
/* ***	PROTOTYPE:	Array.remove								* */
/* ***	DESCRIPTION:	Returns the array with the		   * */
/* ***						item removed - works on objects	* */
/* ***	PARAMETERS:													* */
/* ***		v	=	the value tested against					* */
/* ******************************************************* */
Array.prototype.remove=function(v){
	var tmp=new Array();
	for(var x=0;x<this.length;x++){
		var has=this[x];
		//alert('has:'+this[x]+' searching for:'+v);
		
		if(!has===v){
			tmp.push(has);
			//alert('adding: '+has+' searching for:'+v);
		}
	}
	return tmp;
}
Array.prototype.removeIndex=function(i){
	var tmp=new Array();
	for (var x=0;x<this.length;x++) {
		if(x!=i){
			tmp.push(this[x]);
		}
	}
	//tmp.show();
	return tmp;
}
/* ********************************************* */
/* ***   PROTOTYPE:	Array.swap						 */
/* ***	DESCRIPTION:	SWAPS 2 ARRAY ELEMENTS	 */
/* ***		Parameters: Array of 2 items			 */
/* ***		Returns:	Array of 2 items				 */
/* ********************************************* */
Array.prototype.swap = function(a,b) {
	var a_typ = (a)?typeof(a):null;
	var b_typ = (b)?typeof(b):null;
	if (((typeof(a))=='Number')&&((typeof(b))=='Number')) {
		var x = this[a];
		this[a] = this[b];
		this[b] = x;
	}
}
Array.prototype.show = function() {
	var NumArgs=arguments.length;
	var tit="";
	if(NumArgs>0){
		tit=arguments[0];
	}
	var msg = "*****************************************\nArray Elements:"+tit+"\n*****************************************\n";
	for (var x=0;x<this.length;x++) {
		msg+= "["+x+"]:"+this[x]+"\n";
	}
	msg+="*****************************************";
	alert(msg);
}

/* ADD METHODS TO FIREFOX CHROME ET AL FOR insertAdjacentElement, insertAdjacentHTML, & insertAdjacentText
src: http://forums.mozillazine.org/viewtopic.php?t=445587 */
try{
	if((typeof(HTMLElement!="undefined"))&&(!HTMLElement.prototype.insertAdjacentElement)){ 
		HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){
			switch (where){ 
				case 'beforeBegin':{
					this.parentNode.insertBefore(parsedNode,this) 
					break;
				}
				case 'afterBegin':{
					this.insertBefore(parsedNode,this.firstChild); 
					break;
				}
				case 'beforeEnd':{
					this.appendChild(parsedNode);
					break;
				}
				case 'afterEnd':{
					if(this.nextSibling){
						this.parentNode.insertBefore(parsedNode,this.nextSibling);
					}else{
						this.parentNode.appendChild(parsedNode);
					}
					break;
				}
			}
		}
		HTMLElement.prototype.insertAdjacentHTML=function(where,htmlStr){
			var r = this.ownerDocument.createRange();
			r.setStartBefore(this);
			var parsedHTML = r.createContextualFragment(htmlStr);
			this.insertAdjacentElement(where,parsedHTML);
		}
		HTMLElement.prototype.insertAdjacentText=function(where,txtStr){
			var parsedText = document.createTextNode(txtStr);
			this.insertAdjacentElement(where,parsedText);
		}
	}
}catch(e){

}
