/**
* Changes a dynamically generated list
* @param string The name of the list to change
* @param array A javascript array of list options in the form [key,value,text]
* @param string The key to display
* @param string The original key that was selected
* @param string The original item value that was selected
*/
function changeDynaList(listname, source, key, orig_key, orig_val) {
	var list = eval('document.adminForm.' + listname);

	// empty the list
	for (i in list.options.length)
		list.options[i] = null;

	i = 0;
	for (x in source) {
		if (source[x][0] == key) {
			opt = new Option();
			opt.value = source[x][1];
			opt.text = source[x][2];

			if ((orig_key == key && orig_val == opt.value) || i == 0)
				opt.selected = true;
			list.options[i++] = opt;
		}
	}
	list.length = i;
}

/**
 * @param object A form element
 * @param string The name of the element to find
 */
function getElementByName(frm, name) {
	if (frm.elements)
		for (i=0, n=frm.elements.length; i<n; i++)
			if (frm.elements[i].name == name) return frm.elements[i];
	return null;
}

function getSelectedRadio(frmName, srcGroupName) {
	var form = eval('document.' + frmName);
	var srcGroup = eval('form.' + srcGroupName);

	if (srcGroup[0]) {
		for (var i=0, n=srcGroup.length; i<n; i++)
			if (srcGroup[i].checked)
				return srcGroup[i].value;
	}else {
		if (srcGroup.checked)
			return srcGroup.value;
		// if the one button is checked, return zero
	}
	// if we get to this point, no radio button is selected
	return null;
}

function getSelectedValue(frmName, srcListName) {
	var form = eval('document.' + frmName);
	var srcList = eval('form.' + srcListName);

	i = srcList.selectedIndex;
	if (i != null && i > -1)
		return srcList.options[i].value;
	return null;
}

function getSelectedText(frmName, srcListName) {
	var form = eval('document.' + frmName);
	var srcList = eval('form.' + srcListName);

	i = srcList.selectedIndex;
	if (i != null && i > -1)
		return srcList.options[i].text;
	return null;
}

/**
 * Getting radio button that is selected.
 */
function getSelected(allbuttons) {
	for (i=0; i<allbuttons.length; i++)
		if (allbuttons[i].checked)
			return allbuttons[i].value;
}

// Form specific functions for editting content images
function previewImage(list, image, base_path) {
	form = document.adminForm;
	srcList = eval("form." + list);
	srcImage = eval("document." + image);
	var fileName = srcList.options[srcList.selectedIndex].text;
	var fileName2 = srcList.options[srcList.selectedIndex].value;
	if (fileName.length == 0 || fileName2.length == 0) {
		srcImage.src = '../images/blank.png';
	}else {
		srcImage.src = base_path + fileName2;
	}
}

/**
* 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.parseDate(el.value); // set it to a new date
	}
	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 center of the screen
* @version 2.2
* @author Nelson Matias
*/
function popWindow(url, name, width, height, scroll, resize) {
	if (document.all) var winWidth = screen.width, winHeight = screen.height;
	else if (document.layers || document.getElementById) var winWidth = window.outerWidth, winHeight = window.outerHeight;
	else var winWidth = 640, winHeight = 480;
	var winLeft = (winWidth - width)/2, winTop = (winHeight - height) / 2;
	winprops = 'width=' + width + ',height=' + height + ',top=' + winTop + ',left=' + winLeft + ',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 = '';
}
