/* helper.js */

myGlobalHandlers = {
	
	
	
	onCreate: function() {
		Element.show('working');
	},

	onComplete: function() {
		if(Ajax.activeRequestCount === 0){
			Element.hide('working');
		}
	},
	
	onException: function(p1, p2) {
		//alert('Es ist ein Kommunikations-Fehler aufgetreten.');
		if(Ajax.activeRequestCount === 0){
			Element.hide('working');
		}
			
	},
	
	onInteractive: function(){
		Element.hide('working');
	},

	onError: function() {
		alert('Es ist ein Kommunikations-Fehler (2) aufgetreten.');
		if(Ajax.activeRequestCount === 0){
			Element.hide('working');
		}
		
	}
};
Ajax.Responders.register(myGlobalHandlers);

/* gedrueckte Taste holen */
function getKeyCode(e) {
	if (e.which) {
		if (e.which>0) {
			return e.which;
		} else if (e.keyCode) {
			return e.keyCode;
		} else if (e.modifiers !== undefined) {
			return e.modifiers;
		} else {
			return false;
		}
	} else if (e.keyCode) {
		return e.keyCode;
	} else {
		e = window.event;
		return e.keyCode;
	}
}
		
/* Liefert alle Kindelemente vom Typ DIV zurueck */
function getChildDivs(el) {
	var divs = new Array();
	el = el.firstChild;
	while (el !== null) {
		if (el.nodeName == "DIV") {
			divs.push(el);
		}
		el = el.nextSibling;
	}
	return divs;
}

function nextLi(el) {
	while (el = el.nextSibling) {
		if (el.nodeName == "LI") {
			break;
		}
	}
	return el;
}

function load(url, placeholder, js) {
	if (js === undefined) {
		js = true;
	}
	
	var myAjax = new Ajax.Updater( placeholder, url, { method: 'get', evalScripts: js });
}

function action(url, action, id, placeholder, js) {
	load(url + '?action=' + action + '&id=' + id + '&url=' + url + '&ph=' + placeholder, placeholder, js);
}

function saveForm(url, formName, placeholder, js) {	
	if (js === undefined) {
		js = true;
	}	
	
	var pars = Form.serialize(formName);
	var myAjax = new Ajax.Updater( placeholder, url, { method: 'post', parameters: pars, evalScripts: js });
}

function saveTinyMCEForm(url, formName, placeholder, js) {
	tinyMCE.activeEditor.save();
	if (js === undefined) {
		js = true;
	}	
	
	var pars = Form.serialize(formName);	
	var myAjax = new Ajax.Updater( placeholder, url, { method: 'post', parameters: pars, evalScripts: js });
}


function saveFormMute(url, formName, js) {
	if (js === undefined) {
		js = true;
	}
    var pars = Form.serialize(formName);
   new Ajax.Request(url, {
    	method: 'post',
        parameters: pars,
        evalScripts: js
    });
}

function saveFormGet(url, formName, placeholder, js) {
    if (js === undefined) {
        js = true;
    }
    var pars = Form.serialize(formName);
    var myAjax = new Ajax.Updater( placeholder, url, { method: 'get', parameters: pars, evalScripts: js });
}

function postPars(url, pars, placeholder, js) {
	if (js === undefined) {
		js = true;
	}
	var myAjax = new Ajax.Updater( placeholder, url, { method: 'post', parameters: pars, evalScripts: js });
}

function saveMultiForm(url, formNames, placeholder, js) {
	if (js === undefined) {
		js = true;
	}
	var tmp = new Array();
	var someForm;
	for (var i = formNames.length; --i >= 0; ) {
		someForm = $(formNames[i]);
		tmp.push(Form.serialize(someForm));
	}
	var pars = tmp.join('&');
	var myAjax = new Ajax.Updater( placeholder, url, { method: 'post', parameters: pars, evalScripts: js });
}

function getSelectedIndex(id) {
	return $(id).options.selectedIndex;
}

function decSelectedIndex(id) {

	id = $(id);
	var index = id.options.selectedIndex;
	if (index > 0) {
		id.options.selectedIndex = index - 1;
		id.onchange();
	}
}

function incSelectedIndex(id) {

	id = $(id);
	var index = id.options.selectedIndex;
	if (index < id.options.length - 1) {
		id.options.selectedIndex = index + 1;
		id.onchange();
	}
}

function performAjaxRequest(url, params, callback) {
    new Ajax.Request(url, {
    	method: 'get',
        parameters: params,
        onComplete:callback
    });
}

function updateWithAjaxRequest(url, params, div){

	new Ajax.Request(url, {
	    	method: 'get',
	        parameters: params,
	        onComplete: function(transport) {
			//alert(transport.responseText);
	        div.innerHTML = transport.responseText;
	        }
	    });
	return;
}

function go(url) {
	document.location.href = url;
}

