/*******************************************************************\
						lib.js
					Javascript Library
	Version 2.0
	Last Updated: 9/4/2008
	Creator: Michael Riddle
	Contact: riddlemd(at)gmail.com
\*******************************************************************/

// Wrapper for document.getElementById()
function $(elm) {
	return document.getElementById(elm);
}

// Allows for use of extended methods
function $$(elm) {
	var elm = (elm.nodeName ? elm : document.getElementById(elm));
	return new ext_obj(elm);
}

function ext_obj(elm) {
	var elm = elm;	
	this.hide = function() {
		elm.style.display = "none";
	}
	this.show = function() {
		elm.style.display = (elm.nodeName == "DIV" | elm.nodeName == "TABLE" ? "block" : "inline");
	}
	this.setAlpha = function(value) {
		var value = (value < 0 ? 0 : (value > 100 ? 100 : value));
		elm.style.opacity = (value/100);
		elm.style.filter = "alpha(opacity='"+value+"');";
	}
	this.getAlpha = function() {
		return Math.round(elm.style.opacity * 100,2);
	}
}

/*
Ajax Class (Version 1.5)

Usage:
1. Create Object
$ajax = new Ajax();

2. Assign Variables (optional)
$ajax.variables = new Array("varName":value,"varName2":value2);

3. Set Handlers
$ajax.whileLoading = function() {  document.write("Loading..."); };
$ajax.onSuccess = function() { document.write(this.response); }
$ajax.onFail = function() {  document.write("Load Failed..."); }

4. Make request
$ajax.post("fileToLoadIntoPage.txt"); // Sends $ajax.variables via POST method

this.response = Response from server
this.responseXML = Response from server in XML
this.JSON = Response from server parsed through JSON
*/
function Ajax() {
	var parent = this;
	this.xmlHttp;
	this.response = null;
	this.responseXML = null;
	this.JSON = null;
	this.variables = new Array();
	this.onStart = function() {}
	this.whileLoading = function() {};
	this.onSuccess = function() {};
	this.onFail = function () {};
	this.post = function(url) {
		this.request(url,"POST");
	}
	// JSON parser
	this.parseJSON = function(string) {
		try {
			var object = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(string.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + string + ')');
			return object;
		} catch (e) {
			return false;
		}
	}
	// Required because php's urlencode and javascripts escape do work 100% the same...
	this.urlencode = function(str) {
		str = escape(str);
		str = str.replace('+', '%2B');
		str = str.replace('%20', '+');
		str = str.replace('*', '%2A');
		str = str.replace('/', '%2F');
		str = str.replace('@', '%40');
		return str;
	}
	this.request = function(url,type) {
		var type = (type ? type : "POST");
		var variables = "";
		this.onStart();
		try { // Firefox, Opera 8.0+, Safari
			this.xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer
			try {
				this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
	    }
		this.xmlHttp.onreadystatechange = function() {
			if(parent.xmlHttp.readyState == 1) {
				parent.whileLoading();
			}
			try {
				if(parent.xmlHttp.readyState == 4) {
					if(parent.xmlHttp.status == 200) {
						parent.response = parent.xmlHttp.responseText;
						parent.responseXML = parent.xmlHttp.responseXML;
						parent.JSON = parent.parseJSON(parent.response);
						parent.onSuccess();
					}
					else {
						parent.errorCode = parent.xmlHttp.status;
						parent.onFail();
					}
				}
			}
			catch (e) {
				parent.errorCode = 408;
				parent.onFail();
			}
		}
		this.xmlHttp.open(type,url,true);
		this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		for(key in this.variables) {
			variables += this.urlencode(key)+"="+this.urlencode(this.variables[key])+"&";
		}
		this.xmlHttp.send(variables);
	}
}

function isEmail(string) {
	var pattern = /^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\.[a-zA-Z0-9]+$/;
	return string.match(pattern);
}

function Browser() {}
Browser.getInnerWidth = function() {
	return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;
}
Browser.getInnerHeight = function() {
	return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;
}
Browser.isIE6 = function() {
	return /msie 6/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
Browser.isIE7 = function() {
	return /msie 7/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
Browser.isFirefox = function() {
	return /firefox/i.test(navigator.userAgent);
}
Browser.isSafari = function() {
	return /safari/i.test(navigator.userAgent) && !/chrome/i.test(navigator.userAgent);
}
Browser.isChrome = function() {
	return /chrome/i.test(navigator.userAgent);
}
Browser.loadJS = function(url) {
	var fileref = document.createElement("script");
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src",url);
	if (typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}
Browser.loadCSS = function(url) {
	var fileref = document.createElement("link");
	fileref.setAttribute("rel","stylesheet");
	fileref.setAttribute("type","text/css");
	fileref.setAttribute("href",url);
	if (typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}