// check if this person supports ajax requests
function can_ajax() {
	can_xml = (typeof window.XMLHttpRequest != 'undefined');
	can_acx = (typeof window.ActiveXObject != 'undefined');
	return (can_xml || can_acx);
}

// get the top position of an element
$.fn.postop = function() {
	var postop	= 0;
	var obj = this.get(0);
	while(obj && obj != null) {
		postop += obj.offsetTop; // - obj.scrollTop
		obj = obj.offsetParent;
	}
	return postop;
};

// get the left position of an element
$.fn.posleft = function() {
	var posleft = 0;
	var obj = this.get(0);
	if(obj) {
		posleft = obj.offsetLeft;
		while((obj = obj.offsetParent) != null) {
			posleft += obj.offsetLeft;
		}
	}
	return posleft;
};

$.fn.asMenu = function(){
	this.css('position','absolute').css('zIndex','99');
	return this;
};
$.fn.asLeftMenu = function(link) {
	show = this.css('display') == 'none';
	this.hide();
	if(show) {
		this.asMenu().css('left', link.posleft()+'px').css('top', (link.postop()+link.get(0).offsetHeight)+'px').slideDown("fast");
	}
	return this;
};
$.fn.asRightMenu = function(link) {
	show = this.css('display') == 'none';
	this.hide();
	if(show) {
		this.asMenu().css('left',(link.posleft()-(parseInt(this.css('width'))-parseInt(link.get(0).offsetWidth)))+'px').css('top',(parseInt(link.postop())+parseInt(link.get(0).offsetHeight)-1)+'px').slideDown("fast");
	}
	
	return this;
};

// expand the size of a textarea
function expand_txt(id) {
	var elm = $('#'+id).get(0);
	if(elm.rows >= 20) {
		elm.rows = 30;
	} else {
		elm.rows += 3;
	}
}

// reduce the size of a textarea
function reduce_txt(id) {
	var elm = $('#'+id).get(0);
	if(elm.rows <= 3) {
		elm.rows = 3;
	} else {
		elm.rows -= 3;
	}
}

// write the expand/reduce buttons for textareas
function txt_control_btns(id) {
	document.writeln('<div class="textarea_controls"><a href="javascript:;" onclick="expand_txt(\''+id+'\')">+</a><a href="javascript:;" onclick="reduce_txt(\''+id+'\')">-</a></div>');
}

// disable all submit and reset buttons on a page
function disable_form_buttons() {
	$("//input[@type='submit'],//input[@type='reset']").each(function(i){
		this.disabled = true;
	});
}

// put a loader on the page
var loaders = [];
function push_loader(text) {
	loader_id = "loader"+loaders.length;
	$(document.body).append("<div id=\""+loader_id+"\" class=\"loader\"><h3>"+text+"</h3></div>");
	$("#"+loader_id).center();
	loaders[loaders.length] = loader_id;
}

// take the current loader off the page
function pop_loader() {
	len = loaders.length;
	$("#"+loaders[len-1]).hide();
	document.body.removeChild(document.getElementById(loaders[len-1]));
	delete loaders[len-1];
}

function toggle_check(e,cid) {
	if(e.checked == true) {
		cid = '#'+cid;
		if(!$(cid).get(0).checked) {
			$(cid).get(0).checked = true;
		}
	}
}

function untoggle_checks(e) {
	if(e.checked == false) {
		for(i = 1; i < arguments.length; i++) {
			$('#'+arguments[i]).get(0).checked = false;
		}
	}
}

function mouse_off_menu(e,lid,mid) {
	var y = 0;
	if (self.pageYOffset) {
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		y = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		y = document.body.scrollTop;
	}
	var l = $(lid);
	var m = $(mid);
	
	var ecx = e.clientX;
	var ecy = e.clientY + y;
	var ret = false;
	var ll = l.posleft();
	var lt = l.postop();
	if(ecx < ll) { ret = true; }
	if(ecx > (ll + parseInt(l.css('width')))) { ret = true; }
	if(ecy < lt) { ret = true; }
	if(ecy > (m.postop() + parseInt(m.css('height')))) { ret = true; }
	if(e.target.id && (e.target.id == m.get('id') || e.target.id == l.get('id'))) { ret = false; }
	if(ret && e.target.nodeName.toLowerCase() == 'input') {	ret = false; }
	return ret;
}
