/** 
 * Utils.js
 * Original Author: Chris Robinson
 * Last Updated: Unknown
 *************************
 * Change Log:
 * 10/25/2006 - started Change log
 *************************
 */

var $INCLUDED_JS_FILES = new Array();
var $INCLUDED_CSS_STYLES = new Array();

// must pass a file to javascript
function include_js( fileName ) { 
	if( ! in_array( fileName, $INCLUDED_JS_FILES ) ) {
		$INCLUDED_JS_FILES.push( fileName );
		/*
		var scriptTag = document.createElement("script");
		scriptTag.setAttribute( "type", "text/javascript" );
		scriptTag.src = fileName;
		gET("head")[0].appendChild( scriptTag );
		*/
		document.write( '<script type="text/javascript" src="' + fileName + '"></script>' );
	}
}

// can pass "@import url( /path/to/css.css )" or raw styles "div { color: blue; }"
function include_css( styles ) {
	if( ! in_array( styles, $INCLUDED_CSS_STYLES ) ) {
		$INCLUDED_CSS_STYLES.push( styles );		
		var styleTag = document.createElement("style");
		styleTag.setAttribute( "type", "text/css" );
		if( styleTag.styleSheet ) { // IE only
			styleTag.styleSheet.cssText = styles;
		} else { // w3c standard
			var stylesNode = document.createTextNode( styles );
			styleTag.appendChild( stylesNode );
		}
		document.getElementsByTagName("head")[0].appendChild( styleTag );
		
	}
}
 
function hasIndexOf(theString, theIndex) {
	if(theString.indexOf(theIndex) == -1) {
		return false;
	}
	return true;
} 
function addClass(item, classToAdd) {
	if(!hasClass(item, classToAdd)) {
		item.className += (" " + classToAdd);
	}
}

function removeClass(item, classToRemove) {
	if(hasIndexOf(item.className, classToRemove)) {
		var theClass = item.className;
		var classes = theClass.split(" ");
		for(var i = 0; i < classes.length; i++) {
			if(classes[i] == classToRemove || classes[i] == " ") {
				classes.splice(i, 1);
			}
		}
		item.className = classes.join(" ");
	}
}
function hasClass(item, classToSearch) {
	if(hasIndexOf(item.className, classToSearch)) {
		var theClass = item.className;
		var classes = theClass.split(" ");
		for(var i = 0; i < classes.length; i++) {
			if(classes[i] == classToSearch) {
				return true;
			}
		}
	}
	return false;
}
function getFileName(theString) {
	if(hasIndexOf(theString, "/")) {
		var firstpos = theString.lastIndexOf("/") + 1;
		var lastpos = theString.length;
		return theString.substring(firstpos, lastpos);	
	}
}
function getQSValue(getThisVar) {
	var toReturn = "";
	//kick out if theres no Query String
	if(!hasIndexOf(document.URL, "?")) {return false;}
	var QS = document.URL.substring(document.URL.indexOf("?") + 1);
	var variables = QS.split("&");
	for(var i = 0; i < variables.length; i++) {
		if(hasIndexOf(variables[i], getThisVar)) {
			//if we find it the return its value
			toReturn = variables[i].split("=")[1];
			return toReturn;
		}
	}
	//if all else fails return false
	return false;
}

function in_array( findThisVar, theArray ) {
    for( var i = 0; i < theArray.length; i++ ) {
        if( theArray[i] == findThisVar ) {
            return true;
        }
    }
    return false;
}
