var current_menu=null;
document.onclick=hide_menu;
//document.onkeydown=handle_keydown;

function anzsco_reset(){
	var myform = new getObj('anzsco_form');
	var sort = new getObj('anzsco_sort');
	sort.obj.value = "`ANZSCO` ASC";
	myform.obj.submit();
}

function anzsco_sort(field){
	var myform = new getObj('anzsco_form');
	var sort = new getObj('anzsco_sort');
//	alert(sort.obj.value);

	if(myform && myform.obj && sort && sort.obj){
		var temp = new Array();
		var orient = "DESC";
		if(sort.obj.value){
			var x = sort.obj.value.split(", ");
			for(var i=0;i<x.length;i++){
				var info = x[i].split("` ");
				var myfield = info[0].substr(1);
				if(myfield == field){
					orient = (info[1] == "ASC") ? "DESC" : "ASC";
				} else {
					temp.push(x[i]);
				}
			}
		}
		temp = temp.join(", ");
		temp = (temp.length > 0) ? ", " + temp : "";
		temp = "`" + field + "` " + orient + temp;
//		alert(temp);
		sort.obj.value = temp;
		myform.obj.submit();

	}
}

function textcounter(x) {
	if(!x.maxlimit){
		max = new getObj("max_" + x.id);
		if(max) x.maxlimit = 1*max.obj.innerHTML;
	}
	if(!x.count_obj) x.count_obj = new getObj("count_" + x.id);
	if(x.value.length >= x.maxlimit) alert ("You've reached the maximum text limit for this field.");
    if (x.value.length > x.maxlimit) {
      x.value = x.value.substring(0, x.maxlimit-1);
     }
    x.count_obj.obj.innerHTML = (x.maxlimit - x.value.length);
 }



function top_menu_setup(){
	var top_menu = new getObj('topbar');
	if(top_menu){
//		alert(top_menu.obj.innerHTML);
		var cells = top_menu.obj.getElementsByTagName('td');
		for(var x in cells){
			if(cells[x].id) top_menu_cell_setup(cells[x]);
		}
	}
}

function top_menu_cell_setup(x){
	var myid = x.id;
//	alert(x.id);
	var mydisp = new getObj(myid);
	x.display = mydisp;
	var mymenu = new getObj(myid + "-menu");
	if(mymenu){
//		alert(myid + "-menu");
		x.menu = mymenu;
		x.menu.style.display="none";
	}
	x.onmouseover = show_menu;
}

function set_pos(m,p){
	m.style.left = getPosX(p.obj) + "px";
	m.style.top = (getPosY(p.obj) + p.obj.offsetHeight) + "px";
}

function show_menu(e){
	set_pos(this.menu,this.display); 
	if(this.menu.style.display=='none'){
//		alert('showing menu '+ this.menu.style.display);
		hide_menu(e);
		this.menu.style.display='';
		current_menu=this;
	} 
	cancel_bubble(e);
}

function hide_menu(e){
	if(current_menu){
		current_menu.menu.style.display='none';
	}
	current_menu=false;
}