//
// global vars
//
var itemNextpos = 1;
var itemBox = new Array();


// 
// shortcut functon for selecting an element by id
//
function s(name) {
	return document.getElementById(name);
}

//
// function displays the first and hides the second element by name
//
function toggle(toClose, toOpen) {
	s(toClose).style.display = 'none';
	s(toOpen).style.display = 'block';
}

//
// function that does nothing (in case there is a simplex ajax call)
//
function doNothing() {}

//
// function changes the action atribut of a html form
//
function formAction(formID, location) {
	var form = s(formID);
	form.action = location;
	form.submit();
}

//
// function removes a default label-text from an input field and changes the 
// css class of that field
//
function formLabelFocus(field, labeltext, newClass, isPassword) {
	if (field.value == labeltext) {
		field.value = "";
		field.className = newClass;
		if (isPassword) {
			field.type = "password";
		}
	}
}

//
// function puts the default label back in, if the field is empty on blur
//
function formLabelBlur(field, labeltext, newClass, isPassword) {
	if (field.value == "") {
		field.value = labeltext;
		field.className = newClass;
		if (isPassword) {
			field.type = "text";
		}
	}
}

//
// function aborts a dialog action and displays the main content again
//
function abortdialog() {
	toggle("cont", "selectDialog");
}

//
// function expand a textarea thats two nodes above the button
//
function expandTextarea(name, amount) {
	var area = s(name);
	if (area.style.height == '') {
		area.style.height = "300px";
	}
	slideHeight(name, parseInt(area.style.height.replace(/px/, '')) + amount, 5);
}
// this function saves the input text of a textarea, so it doesnt get lost when moving a item
function taSave(me) {
	me.firstChild.data = me.value;
}
function taInit(me) {
	me.value = me.firstChild.data.replace(/ +$/, '');
}

//
// function changes height of a block object width smooth sliding
//
function slideHeight(name, size, speed) {
	var oldHeight = parseInt(s(name).style.height.replace(/px$/, ""));
	if (oldHeight != size) {
		if (!speed) {
			speed = 5;
		}
		for (var i = 0; i <= 10; i ++) {
			var newHeight = oldHeight - ((oldHeight - size) / 10) * i;
			if (newHeight > 0) {
				setTimeout("s('" + name + "').style.height = '" + newHeight + "' + 'px';", i * speed * 5);
			}
		}
	}
}

//
// the next three functions load a given page into the dialog-part of the webmin
//
function mainLoadDialog(page) {
	toggle("cont", "loadingScreen");
	ajaxObj.onreadystatechange = mainDisplayDialog;
	ajaxObj.open("GET", SERVER + page, true);
	ajaxObj.send(null);
}
function mainDisplayDialog() {
	if (ajaxObj.readyState == 4 && ajaxObj.status == 200) {
		s("selectDialog").innerHTML = ajaxObj.responseText;
		toggle("loadingScreen", "selectDialog");
	}
}
function mainAbort() {
	toggle("selectDialog", "cont");
}

//
// the following function are for managin conetent items
//
function itemGet(action, itembox) {
	if (!ajaxObjSec) {
		ajaxObjSec = createAjaxObj();
	}
	ajaxObjSec.onreadystatechange = itemInsert;
	ajaxObjSec.open("GET", SERVER + action + '/new' + itemNextpos, true);
	itemBox[itemNextpos] = itembox;
	itemNextpos = itemNextpos + 1;
	ajaxObjSec.send(null);
	return 'new' + (itemNextpos - 1);
}

function itemInsert() {
	if (ajaxObjSec.readyState == 4 && ajaxObjSec.status == 200) {
		var div = document.createElement('div');
		div.innerHTML = ajaxObjSec.responseText;
		s(itemBox[div.firstChild.id.replace(/.+_new/, '')]).appendChild(div);
	}
}

function itemMoveUp(itemid) {
	var me = s(itemid).parentNode;
	var mum = me.parentNode;
	if (me != mum.firstChild) {
		mum.insertBefore(me.cloneNode(true), me.previousSibling);
		mum.removeChild(me);
	}
}

function itemMoveDown(itemid) {
	var me = s(itemid).parentNode;
	var mum = me.parentNode;
	if (me != mum.lastChild) {
		mum.insertBefore(me.nextSibling.cloneNode(true), me);
		mum.removeChild(me.nextSibling);
	}
}

function itemRemove(itemid, idfield, collectfield) {
	if (idfield && collectfield) {
		s(collectfield).value = s(collectfield).value + s(idfield).value + ',';
		s(itemid).parentNode.parentNode.removeChild(s(itemid).parentNode);
	}
}
