/**
GlobalFunctions.ajax.fromForm(formObject,callback);
GlobalFunctions.ajax.get(site,callback);
GlobalFunctions.ajax.post(site,params,callback);
GlobalFunctions.date.getMonthFromIndex(index);
GlobalFunctions.dom.getClickedElement(event);
GlobalFunctions.dom.getElementsByClassName(className,div); //Div is the div to look in, by default it is document
GlobalFunctions.dom.addEventListener = function(element,listener,handler);
GlobalFunctions.dom.removeEventListener = function(element,listener,handler);
GlobalFunctions.object.addToSelect(selectObject,value,text); //Doesn't add if the value already exists
GlobalFunctions.object.clearDefaultValue(inputObject); //Clears an input if it's default value is still set
GlobalFunctions.object.addClassName(object,className);
GlobalFunctions.object.removeClassName(object,className);
GlobalFunctions.object.isPartOfClass(object,className);
GlobalFunctions.object.getBottomSide(inputObject);
GlobalFunctions.object.getLeftSide(inputObject);
GlobalFunctions.object.getRightSide(inputObject);
GlobalFunctions.object.getTopSide(inputObject);
GlobalFunctions.window.getClientHeight();
GlobalFunctions.window.getClientWidth();
GlobalFunctions.window.getScrollLeft();
GlobalFunctions.window.getScrollTop();
GlobalFunctions.window.getMouseX();
GlobalFunctions.window.getMouseY();
**/
function AjaxFunctions() {
	this.fromForm = function(formObject,callback) {
		if(!formObject) return false;
	
		//Build Parameters
		params = "";
		for(i=0; i<formObject.elements.length; i++) {
			var tmp = formObject.elements[i];
			//Check if it is a checkbox
			if(tmp.type=="checkbox" && tmp.checked!=true) {
				continue;
			}
			
			if(params!="") params += "&";
			params += escape(tmp.name) + "=" + escape(tmp.value);
		}
		
		var action = formObject.action;
		var method = formObject.method.toUpperCase();
		if(method=="POST") { //Post
			this.post(action,params,callback);
		} else { //Get
			this.get(action+"?"+params,callback);
		}
		return false;
	}
	
	this.get = function(site,callback) {
		var xmlhttp;
		if (window.XMLHttpRequest) {
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			alert("Your browser does not support XMLHTTP!");
		}
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState==4) {
				if(callback!=null) {
					//Deal With warnings
					var warning = xmlhttp.responseText.substring(0,1);
					var response = xmlhttp.responseText.substring(1);
					callback(response,warning);
				}
			}
		}
		xmlhttp.open("GET",site,true);
		xmlhttp.send(null);
	}
	
	this.post = function(site,params,callback) {
		var xmlhttp;
		if (window.XMLHttpRequest) {
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			alert("Your browser does not support XMLHTTP!");
		}
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState==4) {
				//Deal With warnings
				var warning = xmlhttp.responseText.substring(0,1);
				var response = xmlhttp.responseText.substring(1);
				callback(response,warning);
			}
		}
		xmlhttp.open("POST",site,true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(params);
	}
}

function DateFunctions() {
	this.getMonthFromIndex = function(index) {
		var month_name = new Array();
		month_name[0]="January";
		month_name[1]="February";
		month_name[2]="March";
		month_name[3]="April";
		month_name[4]="May";
		month_name[5]="June";
		month_name[6]="July";
		month_name[7]="August";
		month_name[8]="September";
		month_name[9]="October";
		month_name[10]="November";
		month_name[11]="December";
		return month_name[index];
	}
}

function DomFunctions() {
	
	this.getClickedElement = function(event) {
		if(!event.target) {
			return event.srcElement
		} else {
			return event.target;
		}
	}
	
	this.getElementsByClassName = function(className,div) {
		if(!div) {
			div = document;
		}
		var tags = div.getElementsByTagName('*');
		var returnVar = new Array();
		for(var i=0;i<tags.length;i++) {
			//There can be multiple classnames delimited by a space, so we need to go through each one
			var tmp = tags[i].className.split(" ");
			for(var j=0;j<tmp.length;j++) {
				if(tmp[j]==className) {
					returnVar[returnVar.length] = tags[i];
					break; //Only add a tag once
				}
			}
		}
		return returnVar;
	}
	
	this.addEventListener = function(element,listener,callback) {
		if(element.addEventListener) {
			element.addEventListener(listener,callback,true);
		} else {
			element.attachEvent("on"+listner,callback);
		}
	}
	
	this.removeEventListener = function(element,listener,callback) {
		if(element.removeEventListener) {
			element.removeEventListener(listener,callback,true);
		} else {
			element.detachEvent("on"+listner,callback);
		}
	}
}

function ObjectFunctions() {
	this.addClassName = function(object,className) {
		var tmp = object.className.split(" ");
		for(var i=0;i<tmp.length;i++) {
			if(tmp[i]==className) {
				return false;
			}
		}
		object.className = object.className + " " + className;
	}
	
	this.isPartOfClass = function(object,className) {
		var tmp = object.className.split(" ");
		for(var i=0;i<tmp.length;i++) {
			if(tmp[i]==className) {
				return true;
			}
		}
		return false;
	}
	
	this.removeClassName = function(object,className) {
		var found = false;
		var tmp = object.className.split(" ");
		var newClassName = "";
		for(var i=0;i<tmp.length;i++) {
			if(tmp[i]!=className) {
				if(newClassName!="") {
					newClassName += " ";
				}
				newClassName += tmp[i];
			} else {
				found = true;
			}
		}
		object.className = newClassName;
		return found;
	}
	
	this.addToSelect = function(selectObject,value,text) {
		//First check if it already exists
		for(var i=0;i<selectObject.length;i++) {
			var tmp = selectObject.options[i];
			if(tmp.value==value) {
				return false;
			}
		}
		var tmp = document.createElement('option');
		tmp.text = text;
		tmp.value = value;
		try {
			selectObject.add(tmp,null); //Doesn't work with IE
		} catch (ex) {
			selectObject.add(tmp);
		}
	}
	
	this.clearDefaultValue = function(inputObj) {
		var tmp = inputObj.getAttribute("defaultValueChanged");
		if(tmp) {
			//Default value already changed
			return false;
		} else {
			inputObj.setAttribute("defaultValueChanged",true);
			return true;
		}
	}
	
	this.getBottomSide = function(inputObj) {
		var top = this.getTopSide(inputObj);
		var height = inputObj.offsetHeight;
		return (top + height);
	}
	
	this.getLeftSide = function(inputObj) {
		var returnValue = inputObj.offsetLeft;
		while((inputObj = inputObj.offsetParent) != null){
		    if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
		}
		return returnValue;
	}
	
	this.getRightSide = function(inputObj) {
		var left = this.getLeftSide(inputObj);
		var width = inputObj.offsetWidth;
		return (left + width);
	}
	
	this.getTopSide = function(inputObj) {
		var returnValue = inputObj.offsetTop;
		while((inputObj = inputObj.offsetParent) != null){
		    if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
		}
		return returnValue;
	}
}

function WindowFunctions() {
	
	var mouseX;
	var mouseY;
	
	this.getClientHeight = function() {
		return filterResults(
			window.innerHeight ? window.innerHeight : 0,
			document.documentElement ? document.documentElement.clientHeight : 0,
			document.body ? document.body.clientHeight : 0
		);
	}
	
	this.getClientWidth = function() {
		return filterResults(
			window.innerWidth ? window.innerWidth : 0,
			document.documentElement ? document.documentElement.clientWidth : 0,
			document.body ? document.body.clientWidth : 0
		);
	}
	
	
	
	this.getMouseX = function() {
		return mouseX;
	}
	
	this.getMouseY = function() {
		return mouseY;
	}
	
	this.getScrollLeft = function() {
		return filterResults(
			window.pageXOffset ? window.pageXOffset : 0,
			document.documentElement ? document.documentElement.scrollLeft : 0,
			document.body ? document.body.scrollLeft : 0
		);
	}
	
	this.getScrollTop = function() {
		return filterResults(
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}
	
	filterResults = function(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}
	
	setMouseCoordinates = function(e) {
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			mouseX = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			mouseY = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
	}
	if(document.addEventListener) {
		document.addEventListener('mousemove',setMouseCoordinates,true);
	} else {
		document.attachEvent('onmousemove',setMouseCoordinates);
	}
}

function GlobalFunctions() {
	this.ajax = new AjaxFunctions();
	this.date = new DateFunctions();
	this.dom = new DomFunctions();
	this.object = new ObjectFunctions();
	this.window = new WindowFunctions();
}
var GlobalFunctions = new GlobalFunctions();