/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
* @param An alternative field name
*/
function checkAll(n, fieldname) {
	if (!fieldname) fieldname = 'cb';
	var frm = document.adminForm;
	var c = frm.toggle.checked;
	var count = 0;
	for (i=0; i<n; i++) {
		cb = eval('frm.' + fieldname + '' + i);
		if (cb) {
			cb.checked = c;
			count++;
		}
	}
	if (c) frm.boxchecked.value = count;
	else frm.boxchecked.value = 0;
}

function listItemTask(id, action) {
	var frm = document.adminForm;
	cb = eval('frm.' + id);
	if (cb) {
		for (i = 0; true; i++) {
			cbx = eval('frm.cb' + i);
			if (!cbx) break;
			cbx.checked = false;
		} // for
		cb.checked = true;
		frm.boxchecked.value = 1;
		submitButton(action);
	}
	return false;
}

function isChecked(isitchecked) {
	if (isitchecked == true) document.adminForm.boxchecked.value++;
	else document.adminForm.boxchecked.value--;
}

/**
*/
function hideMainMenu() {
	document.adminForm.hidemainmenu.value = 1;
}

/**
* Default function.  Usually would be overriden by the component
*/
function submitButton(pressbutton) {
	submitForm(pressbutton);
}

/**
* Submit the admin form
*/
function submitForm(pressbutton) {
	document.adminForm.action.value = pressbutton;
	try {
		document.adminForm.onsubmit();
	}catch (e) {}
	document.adminForm.submit();
}

// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

/**
* This function gets called when an end-user clicks on some date
*/
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

/**
* And this gets called when the end-user clicks on the _selected_ date,
* or clicks the "Close" (X) button.  It just hides the calendar without
* destroying it.
*/
function closeHandler(cal) {
	cal.hide();	// hide the calendar
	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

/**
* This gets called when the user presses a mouse button anywhere in the
* document, if the calendar is shown.  If the click was outside the open
* calendar this function closes it.
*/
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar. Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler();
		Calendar.stopEvent(ev);
	}
}

/**
* This function shows the calendar under the element having the given id.
* It takes care of catching "mousedown" signals on document and hiding the
* calendar if the click was outside.
*/
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide(); // hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	}else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;	// remember the calendar in the global
		cal.setRange(1900, 2070); // min/max year allowed
		calendar.create(); // create a popup calendar
	}
	calendar.sel = el; // inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field
	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}

/**
* MM_findObj
* @version 4.01
*/
function MM_findObj(n, d) {
	var p, i, x;
	if (!d) d = document;
	if ((p=n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
	if (!(x=d[n]) && d.all) x = d.all[n];
	for (i=0; !x && i<d.forms.length; i++) x = d.forms[i][n];
	for (i=0; !x && d.layers && i<d.layers.length; i++) x = MM_findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n);
	return x;
}

/**
* Reloads the window if Nav4 resized
* @version 3.0
*/
function MM_reloadPage(init) {
	if (init == true) with (navigator) {
		if (appName == "Netscape" && parseInt(appVersion) == 4) {
			document.MM_pgW = innerWidth;
			document.MM_pgH = innerHeight;
			onresize = MM_reloadPage;
		}
	}else if (innerWidth != document.MM_pgW || innerHeight != document.MM_pgH) location.reload();
}

/**
* Pops up a new window in the middle of the screen
* @version 2.1
* @author Nelson Matias
*/
function popWindow(url, name, w, h, scroll, resize) {
	if (document.all) var winlMax = screen.width, wintMax = screen.height;
	else if (document.layers) var winlMax = window.outerWidth, wintMax = window.outerHeight;
	else var wMax = 640, hMax = 480;
	var winl = (winlMax - w)/2, wint = (wintMax - h)/2;
	winprops = 'width='+w+',height='+h+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resize+',directories=no,location=no,titlebar=no,menubar=no,status=no,toolbar=no';
	win = window.open(url, name, winprops);
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}

/**
* MM_preloadImages
* Faz o preload de uma imagem
* <body onLoad="MM_preloadImages('img01','img02',...)">
* @version 3.0
*/
function MM_preloadImages() {
	var d = document;
	if (d.images) {
		if (!d.MM_p) d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		for (i=0; i<a.length; i++)
			if (a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
	}
}

/**
* MM_swapImgRestore
* @version 3.0
*/
function MM_swapImgRestore() {
	var i, x, a = document.MM_sr;
	for (i=0; a && i<a.length && (x=a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

/**
* MM_swapImage
* @version 3.0
*/
function MM_swapImage() {
	var i, j=0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for (i=0; i<(a.length-2); i+=3)
		if ((x=MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if (!x.oSrc) x.oSrc = x.src;
			x.src = a[i+2];
		}
}

/**
* Writes text into status bar
* @version 1.0
* @author Nelson Matias
*/
function writeStatus(txt, time) {
	window.status = txt;
	if (time > 0) window.setTimeout("clearStatus()", time);
}

/**
* Clear status bar
* @version 1.0
* @author Nelson Matias
*/
function clearStatus() {
	window.status = '';
}

function resizeOuterTo(w, h) {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName == "Netscape") {
			top.outerWidth = w;
			top.outerHeight = h;
		}
		else top.resizeTo(w,h);
	}
}
