//////////////
// BASIC JS // helper javascript functions to make life easier
//////////////

//create element
function $(ge_id) { return document.getElementById(ge_id); }

//change a style property of an element
function st(st_id,st_p,st_v) { eval('$(\''+st_id+'\').style.'+st_p+'=\''+st_v+'\''); }

//append an element to a parent
function ap(ap_id,ap_elm) { $(ap_id).appendChild(ap_elm); }

//change innerHTML
function html(html_id,html_val) {
	if(!$(html_id)) return;
	$(html_id).innerHTML=html_val; 
}

//create an element
function cr(cr_t,cr_id,cr_c,cr_p) {
	var cr_elm = document.createElement(cr_t);
	if(cr_id) cr_elm.setAttribute("id",cr_id);
	if(cr_c) cr_elm.setAttribute("class",cr_c);
	if(cr_p) ap(cr_p,cr_elm);
	return cr_elm;
}

function gc(gc_o,gc_n) {
	if($(gc_o)) return $(gc_o).getElementsByTagName(gc_n);
	else return gc_o.getElementsByTagName(gc_n);
}

/* evalScripts */

//params: id - id of element

//description: evaluates all script tags within given element

function evalScripts(id) {
	var scripts = $(id).getElementsByTagName('script');
	for(i=0;i<scripts.length;i++) eval(scripts[i].innerHTML);	
}

function showme(which,not) {
	$(which).style.display="";
	$(not).style.display="none";
	$(which+"_link").className="select";
	$(not+"_link").className="";
}

/* AjaxIt */

//params:
//url - url to request
//id - (optional) where to place responseText
//postfunc - (optional) function to call after request is complete

//description: sends an AJAX call to specified URL and processes response

function AjaxIt(params,id,postfunc,base) {
	var myAjax = new AjaxMe();
	if(!base) base="ajax.php";
	myAjax.open("POST",base,true);
	myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	myAjax.setRequestHeader("Content-length", params.length);
	myAjax.setRequestHeader("Connection", "close");
	myAjax.onreadystatechange=function() {
		if(myAjax.readyState==4 || myAjax.readyState=="complete") {
			if($('popup') && $('popupload')) {
				$('popup').removeChild($('popupload'));
			}
			if(id) {
				$(id).innerHTML=myAjax.responseText;
				evalScripts(id);
			}
			if(postfunc) eval(postfunc+"(myAjax)");
		}
	}
	myAjax.send(params);
}

function AjaxGet(url,id,postfunc) {
	var myAjax = new AjaxMe();
	myAjax.open("GET",url,true);
	myAjax.onreadystatechange=function() {
		if(myAjax.readyState==4 || myAjax.readyState=="complete") {
			if(id) {
				$(id).innerHTML=myAjax.responseText;
				evalScripts(id);
			}
			if(postfunc) eval(postfunc+"(myAjax)");
		}
	}
	myAjax.send(null);
}

/* AjaxMe */

//description: creates new AJAX object

function AjaxMe() {
	var xmlHttp=null;
	try {
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e) {
	 try {
	 		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
 	}
	return xmlHttp;
}