function iget(id) {
	return document.getElementById(id);
}

function aj_form2string(form) {
	var req = "";
	for( var i=0; i<form.length; i++) {
		if(form[i].type != 'button' && form[i].name !=undefined) {
			if(form[i].type == "checkbox" && !form[i].checked) {
			} else {
				req += form[i].name +"="+ encodeURIComponent(form[i].value)+"&";
			}
		}
	}
	req = req.substr(0,req.length-1);
	return req;
}

function aj_create_xmlhttp() {
	var http_request = false;
	try {
		http_request = new XMLHttpRequest();
	}
	catch (tryMS) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (otherMS) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(failed) {
				throw "Create XMLHTTP";
			}
		}
	}
	return http_request;
}

function mk_on_ready_state(http_request, callback) {
	return function() {
		if (http_request.readyState==4) {
			if ( http_request.status==200 ) {
				callback(http_request.responseText);
			} else {
				alert(http_request.status+'   '+http_request.readyState+'   Error!')
			}
		}
	}
}

function aj_post(url, body, callback) {
	var r = aj_create_xmlhttp();
	r.onreadystatechange = mk_on_ready_state(r, callback);
	r.open('post', url, true);
	r.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
	r.send(body);
}

function aj_get(url, callback) {
	var r = aj_create_xmlhttp();
	r.onreadystatechange = mk_on_ready_state(r, callback);
	r.open('get', url, true);
	r.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
	r.send(null);
}

function aj_pget(url, callback) {
	aj_post(url, '', callback);
}

function aj_postform(form_id, callback) {
	var form = iget(form_id);
	var url = form.action;
	var body = aj_form2string(form);
	aj_post(url, body, callback);
}

function aj_loadinto(url, div_id) {
	var cback = function(resp) {
		iget(div_id).innerHTML = resp;		
	}
	aj_get(url, cback);
}

function form_submit(form, replacement) {
	var cleanup = function(resp) {
		var wrapper = form.parentNode;
		if (replacement) {
			aj_loadinto(replacement, wrapper.id);
		} else {
			wrapper.innerHTML = "";
		}
	};
	aj_post_form(form, cleanup);
}

function form_cancel(form, replacement) {
	var wrapper = form.parentNode;
	if (replacement) {
		aj_loadinto(replacement, wrapper.id);
	} else {
		wrapper.innerHTML = "";
	}
}


function getLeft(ele) {
	if (ele.offsetParent)
		return ele.offsetLeft + getLeft(ele.offsetParent);
	else
		return ele.offsetLeft;
}

function getTop(ele) {
	if (ele.offsetParent)
		return (ele.offsetTop + getTop(ele.offsetParent));
	else
		return (ele.offsetTop);
}
													

var hint_timeout;
var hide_timeout;

function delay_hint(elem,user) {
	hint_timeout = setTimeout( function() { show_hint(elem,user) }, 600);
}

function get_window_width() {
	var width = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		width = window.innerWidth;
	} else if( document.documentElement && document.documentElement.clientWidth ) {
	//IE 6+ in 'standards compliant mode'
		width = document.documentElement.clientWidth;
	} else if( document.body && document.body.clientWidth ) {
	//IE 4 compatible
		width = document.body.clientWidth;
	}
	return width;
}

function get_window_height() {
	var height = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
	//Non-IE
		height = window.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
	} else if( document.body && document.body.clientHeight ) {
	//IE 4 compatible
		height = document.body.clientHeight;
	}
	return height;
}

function get_window_y_scroll() {
	var ys = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		ys = window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		ys = document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		ys = document.documentElement.scrollTop;
	}
	return ys;
}

function show_hint(elem,url) {
	var hint = $('hint');
	var cback = function(resp) {
		hint.innerHTML = resp;
		hint.style.left = (get_window_width() - hint.offsetWidth)/2;
		var toppos = get_window_y_scroll() + (get_window_height() - hint.offsetHeight) / 2;
		if (toppos < 0)
			hint.style.top = 0;
		else
			hint.style.top = toppos;
		hint.style.visibility = 'visible';
		hint.style.zIndex = '10';
		hint.style.height = hint.firstChild.clientHeight + 5;
	}
	aj_get(url, cback);
}

function show_hint_real(hint_id,elem,url) {
	var cback = function(resp) {
		var hint = $(hint_id);
		hint.style.left = getLeft(elem) + 30;
		hint.style.top = getTop(elem) + 30;
		hint.innerHTML = resp;
		hint.style.visibility = 'visible';
	}
	aj_get(url, cback);
}

function hide_hint_real(hint_id) {
	var hint = $(hint_id);
	hint.style.visibility = 'hidden';
}


function hide_delay(elem) {
	clearTimeout(hint_timeout);
}

function delay_hide(elem) {
	hide_timeout = setTimeout( function() { hide_hint(elem) }, 1000);
}

function cancel_hide() {
	clearTimeout(hide_timeout);
}

function hide_hint(elem) {
	var hint = document.getElementById('hint');
	hint.innerHTML = '';
	hint.style.visibility = 'hidden';
}

function blank() {
}

function post_form_into(id, target) {
	var cleanup = function(resp) {
		var wrapper = iget(target);
		wrapper.innerHTML = resp;
	};
	aj_postform(id, cleanup);
}

function clean_into(target) {
	var wrapper = iget(target);
	wrapper.innerHTML = "";
}

// REDESIGN

function aj_get_into(url, elem, callback) {
	var cb = function(resp) {
		elem.innerHTML = resp;		
		if (callback)
			(callback)();
	}
	aj_get(url, cb);
}

function aj_post_into(url, body, elem, callback) {
	var cb = function(resp) {
		elem.innerHTML = resp;		
		if (callback)
			(callback)();
	}
	aj_post(url, body, cb);
}

function aj_post_form_into(form_id, elem, callback) {
	var form = iget(form_id);
	var url = form.action;
	var body = aj_form2string(form);
	aj_post_into(url, body, elem, callback);
}

function aj_clear(elem) {
	elem.innerHTML = "";
}

function $(id) {
	return document.getElementById(id);
}

function aj_post_form(form, callback) {
	var url = form.action;
	var body = aj_form2string(form);
	aj_post(url, body, callback);
}

function aj_post_form_to(form, url, callback) {
	var body = aj_form2string(form);
	aj_post(url, body, callback);
}

// SPECIFIC

function sjoin() {
	var temp = new Array;
	for(var i=0; i<arguments.length; i++)
		temp[i] = arguments[i];
	return temp.join('_');
}

function pjoin() {
	var temp = new Array;
	for(var i=0; i<arguments.length; i++)
		temp[i] = arguments[i];
	return temp.join('/');
}

function aj_fclear(elem_id) {
	return function() {
		$(elem_id).innerHTML = "";
	}
}

function aj_fremove(elem_id) {
	return function() {
		elem = $(elem_id);
		elem.parentNode.removeChild(elem);
	}
}

function aj_remove(elem_id) {
	elem = $(elem_id);
	elem.parentNode.removeChild(elem);
}


function aj_fremove_elem(elem) {
	return function() {
		elem.parentNode.removeChild(elem);
	}
}

function aj_fclear(elem_id) {
	return function() {
		$(elem_id).innerHTML = "";
	}
}

function aj_fremove(elem_id) {
	return function() {
		elem = $(elem_id);
		elem.parentNode.removeChild(elem);
	}
}

function aj_fremove_elem(elem) {
	return function() {
		elem.parentNode.removeChild(elem);
	}
}

// REDESIGN 2

function form_cancel_2(oid, replacement) {
	var wrapper = iget(oid);
	if (replacement) {
		aj_loadinto(replacement, wrapper.id);
	} else {
		wrapper.innerHTML = "";
	}
	hide_hint();
}

function form_submit_2(form, oid, replacement) {
	var cleanup = function(resp) {
		var wrapper = iget(oid);
		if (replacement) {
			aj_loadinto(replacement, wrapper.id);
		} else {
			wrapper.innerHTML = "";
		}
		hide_hint();
	};
	aj_post_form(form, cleanup);
}


function aj_select_page(page, page_class) {
	var divs = document.getElementsByTagName("div");
	var selected;
	for (var i=0, max=divs.length; i<max; i++) {
		if (divs[i].className == page_class) {
			if (divs[i].id == page) {
				divs[i].style.display = 'block';
				selected = divs[i];
			} else {
				divs[i].style.display = 'none';
			}
		}
	}
	aj_set_hint_size($("hint"), selected);
}

function aj_set_hint_size(hint, subpage) {
//	hint.style.width = subpage.offsetWidth + 20;
	hint.style.height = subpage.offsetHeight + 60;
}

function aj_set_hint_position(hint) {
	hint.style.left = (document.documentElement.clientWidth - hint.offsetWidth)/2;
	var toppos = (document.documentElement.clientHeight - hint.offsetHeight) / 2;
	if (toppos < 0)
		hint.style.top = 0;
	else
		hint.style.top = toppos;
}


function highlight(f, color) {
	f.style.backgroundColor=color;
}


