/**
 * helper function for volvox_admin
 * @author	Jo Schulze
 * @remarks
 * jo20070427	separated to external file
 * 		fixed (de)selection of NULL values
 * jo20070503	M$IE suxx on object handling; workaround
 * jo20071218	added escape() to windowOpenPreview()
 * @todo
 * - better API naming conventions
 */

// Returns true if the passed value is found in the
// array.  Returns false if it is not.
Array.prototype.inArray = function (value)
{
	for (var i = 0; i < this.length; i++) {
		if (this[i] == value) {
			return true;
		}
	}
	return false;
};

function form_check(e, flag)
{
	for(var i = 0; i < e.length; i++) {
		e[i].checked = flag;
	}
}

function form_invert(e)
{
	for(var i = 0; i < e.length; i++) {
		e[i].checked = !e[i].checked;
	}
}

function form_select(e, flag)
{
	for(var i = 0; i < e.length; i++) {
		if(e[i].value == 'NULL') {
			e[i].selected = !flag;
		} else {
			e[i].selected = flag;
		}
	}
}

function form_flip(e)
{
	for(var i = 0; i < e.length; i++) {
		if(e[i].value == 'NULL') {
			e[i].selected = false;
		} else {
			e[i].selected = !e[i].selected;
		}
	}
}

function elements_enable(e, childs, flag)
{
d = new String();
	for(var i = 0; i < childs.length; i++) {
		var c = childs[i];
		for(var j = 0; j < e.length; j++) {
			if(e[j].value == c) {
				e[j].disabled = !flag;
				d += c + ": ";
				d += (flag) ? "on" : "off";
				d += "\n";
			}
		}
	}
// alert(d);
}

/**
 * disables all elements except those with value in lst
 */
function elements_disable(e, lst)
{
	for(var i = 0; i < e.length; i++) {
		if(!lst.inArray(e[i].value)) {
			e[i].disabled = true;
		}
	}
}

function elements_flip(e, others, flag)
{
	if(flag) {
		for(var i = 0; i < others.length; i++) {
			var c = others[i];
			for(var j = 0; j < e.length; j++) {
				if(e[j].value == c && e[j].checked) {
					e[j].checked = false;
				}
			}
		}
	}
}

/**
 * Checks the number of selected elements in checkbox/select multiple.
 * If the # exceeds max, an alert is issued and the change is ignored.
 * volvox adds a call to the onclick handler if maxLength is set.
 * @param string	form	name of the form
 * @param string	element	name of the form element
 * @param int		max	max # of checked options
 * @return
 */
function element_checkLength(form, element, max)
{
	var a = 0;
	var rc = true;

	var opts = getPhpArray(form, element.name);

	for(var i = 0; i < opts.length; i++) {
		if(opts[i].checked) {
			a++;
			if(a > max) {
				alert("Bitte max. " + max + " Einträge auswählen.");
				opts[i].checked = false;
				rc = false;
				break;
			}
		}
	}

	return(rc);
}

/**
 * Passes an array named by PHP specs ([]) to JS functions
 * This is neccessary due to braindead M$IE design
 * @param string	strForm	the form name
 * @param string	strElem	the element _array_ name in PHP notation
 * @return object[]	the spcified JS array
 */
function getPhpArray(strForm, strElem)
{
	var theForm = document.forms[strForm];
	var rc = new Array();

	for(var i = 0; i < theForm.elements[strElem].length; i++) {
		rc[i] = theForm.elements[strElem][i];
	}
 	return(rc);
}

/**
 * open a new window for preview of form element settings using JS
 * @param string	uri	the URI to open
 * @param string	name	name of opened window
 * @param string	opts	flags for opening the window
 * @param mixed[]	bbcode	value to display
 * @return	object		the opened window
 */
function windowOpenPreview(uri, name, opts, bbcode)
{
	win = window.open("", name, 'width=640,height=400,top=100,left=100');
	if(win) {
		win.location.href = uri + '&bbcode=' + escape(bbcode);
		win.focus();
	}
	return(win);
}

