/* -----------------------------------------------------------------------------
Filename:                                                                       
    utils.js                                                                   
                                                                                
Purpose:                                                                        
    javaScript utility functions                                   
                                                                                
History:                                                                        
Ver         Inits   Date        Comments                                        
1.00.00     ???     ??/??/??    Created by someone other than me.
1.00.01     JWL     09/25/06    Added the GetQueryVariable function.     
                                         
--------------------------------------------------------------------------------
*/

// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
//USAGE: addEvent(window, "load", externalLinks);
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}

//USAGE: removeEvent(window, "load", externalLinks);
function removeEvent(obj, evType, fn){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, true);
    return true;
  } else if (obj.detachEvent){
	var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }	
}

//allows rel=external to open new windows beacuase "_blank" is deprecated inXHTML
addEvent(window, "load", externalLinks);
function externalLinks() {
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName("a");
		for (var i=0, j=anchors.length; i<j; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") &&
				anchor.getAttribute("rel") == "external") {
				anchor.onclick = function () {window.open(this.href); return false;};
			}
		}
	}
}


/*controls css by adding or removing classes see http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
	usage:  jscss('swap','Element','classToFind','classToUse');
			jscss('add','Element','classToAdd');
			jscss('remove','Element','classToRemove'); OR jscss('remove','divID'); <-- this will remove the class attribute completly
			jscss('check','Element','classToCheck');
*/

function jscss(a,o,c1,c2){
	//a = "action"
	//o = "object being passed"
	//c1 = "class 1"
	//c2 = "class 2"
	switch (a){
		case 'swap':
			o.className=!jscss('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
		case 'add':
			if(!jscss('check',o,c1)){
				o.className+=o.className?' '+c1:c1;
			}
			break;
		case 'remove':
			if(c1 == undefined){
				removeElAttribute(o,"class","There is no class assigned to the selected HTML element");
			}else{
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
				if(o.className == ""){
					removeElAttribute(o,"class","There is no class assigned to the selected HTML element");
				}
			}
			break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(o.className);
		break;
	}
}

function removeElAttribute(element,attToRemove,error){
	var atNode = element.getAttributeNode(attToRemove);
	if(atNode != null){
		element.removeAttributeNode(atNode);
	}else if(error != undefined){
		alert(error);
	}
}

//removes elements. The element's parent and tagName are used
function removeEls(elToRemove,parentEl,scope){
  if(scope == "wholeParent"){
	  var childEls = parentEl.getElementsByTagName(elToRemove);
	  for(i=0;i<childEls.length;i++){
		childEls[i].parentNode.removeChild(childEls[i]);
	  }
  }else{
	  elToRemove.parentNode.removeChild(elToRemove);
  }
}

function printMe() {
	if (window.print){
		window.print();
	}else{
		alert("Sorry, this browser does not support this feature.  To print this page, please click on File at the top of your screen, and then choose Print.");
	}
}

/*	
allows javascript to understand coldfusion string functions
	Suported Function are: left(), right(),	rtrim(), ltrim(), trim(), ucase(), lcase()
*/

// required for right() function
function reverse(string){
	returnString = '';
	for (i = string.length; i >= 0; i--)
	returnString += string.charAt(i);
	return returnString;
}

// left is just a rename of substring()
function left(string, count){
	return string.substring(0, count);
}

// right actually uses reverse() and left()
function right(string, count){
	return reverse(left(reverse(string), count));
}

/* the rtrim() and ltrim() are left and right trim, respectively
they are used more specifically by trim(), but function on
their own. rtrim() removes whitespace on the right, and
ltrim() does the same for the left */
function rtrim(string) {
	return string.replace(/\s+$/,'');
}
function ltrim(string){
	return string.replace(/^\s+/,'');
}
function trim(string){
	return ltrim(rtrim(string));
}
// add support for ColdFusion's uppercase and lowercase
// functions
function ucase(string){
	return string.toUpperCase();
}
function lcase(string){
	return string.toLowerCase();
}
/* end coldfusion string functions*/


//	GetQueryVariable:
//
//	Pass in the name of the query string variable you want and this function
//	will return the value of that variable.
//
function GetQueryVariable(varName) 
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) 
	{
		var pair = vars[i].split("=");
		if(pair[0] == varName)
			return pair[1];
	} 
	return ""; 	
  
} // GetQueryVariable

/*
Make a text unout a numerical input allowing onl;y numbers and decimals
usage: 
onkeyup="validateNM(this);" onchange="validateNMChange(this);"
*/
function validateNM(inputObject){
	var corVal = inputObject.value;
	var checkForNum = isNaN(corVal);
	if(checkForNum == true || corVal==""){
		inputObject.value = corVal.slice(0,corVal.length-1);
	}
}

function validateNMChange(inputObject){
	var corVal = inputObject.value;
	var checkForNum = isNaN(corVal);
	if(checkForNum == true || corVal==""){
		alert("Numbers and decimal points only please");
		inputObject.value = "";
	}
}