/**
* JavaScript FormLib
* For handling dynamic Changes
* in select firlds of a form
* @copy Mayflower GmbH 2005
* @author Gregor Streng <streng@mayflower.de>
* @version $Id: formlib.js,v 1.1 2005/08/30 15:05:42 gregor Exp $
*/


/**
* Get an element by name or id
* @param string name the name or id of the element
* @param object doc the document to search in
* 
* @return object the DOM Element
*/
function getelement(name, doc) { //v4.01
	var p,i,x;  
	if(!doc) doc=document; 
	if((p=name.indexOf("?"))>0&&parent.frames.length) {
		doc=parent.frames[n.substring(p+1)].document; name=name.substring(0,p);
	}
	if(!(x=doc[name])&&doc.all) x=doc.all[name]; 
	for (i=0;!x&&i<doc.forms.length;i++) x=doc.forms[i][name];
	for(i=0;!x&&doc.layers&&i<doc.layers.length;i++) x=MM_findObjSelmover(n,doc.layers[i].document);
	if(!x && doc.getElementById) x=doc.getElementById(name); 
	return x;
}


/**
* Removes all options from a select
* @param string id the name opr id of the select<BR>
*
* @return void
*/
function emptyselect(id) {
	var myfield=getelement(id);
	if(myfield) {
		var size=myfield.options.length-1;
		var count=size;
		for(var i=0;i<=size;i++) {
			myfield.options[count]=null;
			count=count-1;
		}
	}
}

/**
* Fills a selectfield
* @param string id the name or id of the field
* @param array data the array containing the new options/values for the select
*
* @return void
*/
function fillselect(id, data) {
	emptyselect(id);
	var myfield=getelement(id);
	if(myfield) {
		for(var name in data) {
			var newtext=name;
			var newvalue=data[name];
			var size=myfield.options.length;
			var newoption = document.createElement("option");
			newoption.text = newtext;
			newoption.value = newvalue;
			myfield.options[size]=newoption; 			
		}
	}
}

/**
* Processes the xmlhttp answer
* @param string the response from the xmlhttp request
*
* @return void
*/
function dofillselect(response) {
	eval(response);
	fillselect(field, data);	
}


/**
* Refills a SelectField with Data from a xmlhtto request
* @param int newsid the newsid of the form
* @param string source the name of the calling selectfield
* @param string target the name of the selectfield which should be changed 
* @param string name the name of the where statement that should be used in the query
* @param string value the value of the where statement to be compared with
*
* @return void
*/
function refillselect(newsid, source, target, name, value) {
	refilltargets[source]=target;
	unsetTargets(target);
	var request = new MF_xmlRequest('/form/getfieldvalues.php?NewsID='+newsid+'&field='+target+'&'+target+'['+name+']='+escape(value));
	request.doRequest('dofillselect');
}

/**
* Recursivly unsets the selectfields if dependecies exist
* @param name the name of the target to be unsetted
*
* @return void
*/
function unsetTargets(name) {
	emptyselect('mayform__'+name);
	if(refilltargets[name]) {
		unsetTargets(refilltargets[name]);
	}
}


function fillelem(id,data){
	var myfield = getelement(id);
	myfield.innerHTML = data;
}

function dofillelem(response){
	eval(response);
	fillelem(field, data);
}

/**
 *
 *
 *
 */
function refillelem(source, target, condition, value){
	refilltargets[source]=target;

	var url = '/form/'+source+'.php?target='+target+'&cname='+condition+'&cval='+escape(value);

	var request = new MF_xmlRequest(url);
	var result = request.doRequest('dofillelem');
}

/**
* defines the variable of the dependencies
* @var array refilltargets
*/
var refilltargets = new Array();

