// MISC
Array.prototype.exists = function(search){
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return true;
		
  return false;
} 

//Get cookie routine by Shelley Powers 
function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = false; //"";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) { 
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		} //no cookie w/ format= [Name]=[Value]
	} // no js cookie
	return returnvalue;
}

// EVENT HANDLING
function addEventHandler(oNode, sEvt, fFunc, bCaptures) {
	if(oNode.addEventListener){
		oNode.addEventListener(sEvt, fFunc, bCaptures);
	} else if(oNode.attachEvent) {
		oNode.attachEvent("on" + sEvt, fFunc);
	}
}

function removeEventHandler(oNode, sEvt, fFunc, bCaptures) {
	if(oNode.removeEventListener){
		oNode.removeEventListener(sEvt, fFunc, bCaptures);
	} else if(oNode.dettachEvent) {
		oNode.detachEvent("on" + sEvt, fFunc);
	}
}

//returns the node on which the event originated
function getEventTarget(e) {
	/*if(oNode.addEventListener){
		oNode.addEventListener(sEvt, fFunc, bCaptures);
	} else if(oNode.attachEvent) {
		oNode.attachEvent("on" + sEvt, fFunc);
	}*/
	if (window.event != null) {
		return window.event.srcElement;
	} else {
		return e.target;
	}
}

function noDefault(e) {
	if(e.preventDefault) e.preventDefault(); 
	else e.returnValue = false;
	//alert(window.event);
	/*if (window.event != null) {
		//alert("noDefault: IE");
		window.event.returnValue = false;
	} else {
		//alert("noDefault: FF/w3c");
		e.preventDefault();
	}*/
	
	return false;
}

//CLASS MANIPULATION
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

//XMLHttpRequest - AJAX
//queryString example - Concatenate GET variables (optional)
/*for(var i=0; i<allPhotos.length) {
	var age = document.getElementById('age').value;
	var wpm = document.getElementById('wpm').value;
	var sex = document.getElementById('sex').value;
	var queryString = "?age="+age+"&wpm="+wpm+"&sex="+sex;
}*/
function getResponseText(useResponse, file, queryString) {
	if (typeof queryString == 'undefined' ) queryString = '';
	var xhr = false;
	var output = "";
	
	//Browser Normalization
	try{
		// Opera 8.0+, Firefox, Safari
		xhr = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
			}
		}
	}
	
	//XMLHttpRequest
	if(xhr) {
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4){
				if(xhr.status == 200) {
					// SUCCESSFUL XHR - Do Something With the results
					output = xhr.responseText;
				} else {
					output = "There was a problem "+xhr.status;
					output += "<br/>url: "+file;
				}
				if(useResponse(output)) {
					return true;
				} else { return false; }
				//display_tooltip(output);
			}
		} 
	} 
	
	xhr.open("GET", file+queryString, true);
	xhr.send(null);
}
