/******************************************
@TITLE
YES AJAX LIST INPUT

@EXAMPLE
Firmenauswahl; in ein textfeld werden 2 oder mehr buchstaben eingegeben
und es wird durch ein php script die passenden firmennamen zur Auswahl gestellt.
Wenn man eine Firma aus der Auswahl getroffen hat, wird ein bisher leeres Select-Feld
mit dem entsprechenden Wert befüllt

@DESCRIPTION
Bei onkeyup im Formularfeld wird in includes/javascript/ajax-dynamic-list.js
die Funktion ajax_showOptions() aufgerufen.

Die Daten werden aus dem PHP Script ajax_list_getter.php geparsed.

@REQUIRES:
includes/prototype.js
includes/javascript/ajax-dynamic-list.js
includes/javascript/ajax.js
includes/javascript/yes_alertbox.js

@author
mario aspeleiter <m.aspeleiter@yes-websolutions.de>

@copyright
mario aspeleiter <m.aspeleiter@yes-websolutions.de>

******************************************/


function yes_ajax_list_input() {
	// FORMULARNAME IN DEM SICH DAS INPUT-FELD BEFINDET IN
	// DEM DER USER DIE SUCHE STARTET
	this.AJAXLISTForm = 'ajax_list_form';
	
	// DIV ID IN DEM SICH AJAXLISTForm befindet
	this.AJAXLISTFormDIV = 'AJAX_LIST_DIV';
	
	this.AJAXLISTFormTarget = '';
	// das SELECT form element der externen form was gefuellt werden soll
	this.AJAXLISTFormTargetElement = '';
	
	// DAS HIDDEN FIELD FUER DIE GEWAEHLTE ID IM AJAXLISTForm
	this.AJAXLISTFormHiddenElement = 'letters_hidden';
	this.AJAXLISTFormInputElement = 'letters';
	
	this.instanceName = ''; // INSTANZNAME YALI
	
	this.php_function = '';
	
	this.text_description = '';
	
	this.writeDIVhtml = write_div_html;
	this.setActualTarget = set_actual_target;
	this.show = show_div;
	this.hide = hide_div;
	this.setInput = set_input; 
}

function write_div_html(toppos,leftpos){

	div_string = '<div id="AJAX_LIST_DIV" style="display:none;top:'+toppos+'px;left:'+leftpos+'px;">' +
		'	<ul>'+
		'		<li><b>'+YES_JS_AJAX_LIST_SEARCH+this.text_description+'<\/b><br \/><i>'+YES_JS_AJAX_LIST_MINIMUM_LETTERS+'<\/i><\/li>'+
		'		<li class="close"><a href="javascript:'+this.instanceName+'.hide();"><img src="images\/cal_close_small.gif" border="0" \/><\/a><\/li>'+
		'	<\/ul>'+
		'	<form name="'+this.AJAXLISTForm+'" id="'+this.AJAXLISTForm+'" method="get">'+
		'	<input type="text" id="'+this.AJAXLISTFormInputElement+'" name="'+this.AJAXLISTFormInputElement+'" value="" onkeyup="ajax_showOptions(this,\''+this.php_function+'\',event)" class="iButton" style="width:250px;" \/>'+
		'	<input type="hidden" id="'+this.AJAXLISTFormHiddenElement+'" name="letters_id">'+
		'	<\/form>'+
		'<\/div>';
	$('AJAXLISTFORMDIV').innerHTML = div_string;
}

function hide_div(){
	div = this.AJAXLISTFormDIV;
	$(div).toggle();
	this.AJAXLISTFormTargetElement = '';
}

function show_div() {
	div = this.AJAXLISTFormDIV;
	$(div).toggle();
	// RESETS THE LAST VALUE
	document.forms[this.AJAXLISTForm].elements[this.AJAXLISTFormHiddenElement].value = '-1';
	document.forms[this.AJAXLISTForm].elements[this.AJAXLISTFormInputElement].focus();
}

//function choose_input(field_name,target_form){
function set_input(){
	id_value = document.forms[this.AJAXLISTForm].elements[this.AJAXLISTFormHiddenElement].value;
	field_name = this.AJAXLISTFormHiddenElement;
	text_value_element = document.forms[this.AJAXLISTForm].elements[this.AJAXLISTFormInputElement];
	text_value = text_value_element.value;
	dom = document.forms[this.AJAXLISTFormTarget];
	if(id_value != ''){
		target_field = this.AJAXLISTFormTargetElement;
		switch(target_field.type){
			case "select-one":
				target_field[0] = new Option(text_value,id_value,true);
				div = this.AJAXLISTFormDIV;
				$(div).toggle();
				text_value_element.value = '';
				if(typeof target_field.onchange == 'function')
					target_field.onchange();
				break;
			case "select-multiple":
				if(target_field.options[0].value == ''){
					target_field.options[0] = null;
				}
				target_field_len = target_field.length;
				target_field[target_field_len] = new Option(text_value,id_value,true);
				target_field.size = target_field_len+1;
				div = this.AJAXLISTFormDIV;
				$(div).toggle();
				text_value_element.value = '';
				break;
			default:
				alert('In der Javascript Funktion choose_input_field fuer das AJAX-Form Handling fehlt das Handling fuer Formelement-Typ '+target_field.type);
				break;
		}
		this.AJAXLISTFormTargetElement.focus();
	}else{
		show_yes_alertbox(YES_JS_NO_SEARCH_RESULTS,300,200);
	}
}

function set_ajax_list_form(yali_target_form_name,yali_php_function,yali_instance_name,text_description){
	yali = new yes_ajax_list_input();
	yali.AJAXLISTFormTarget = yali_target_form_name;
	yali.instanceName = yali_instance_name; 
	yali.php_function = yali_php_function;
	yali.text_description = text_description; 
	return yali;
}

function set_actual_target(target_form_element){
	this.AJAXLISTFormTargetElement = target_form_element;
}

function yali_start(element,yaliInstance){
	yaliInstance.setActualTarget(element);
	ajax_list_yali_instance = yaliInstance;
	// wir setzen das div dorthin wo geklickt wurde
	toppos = parseInt(element.offsetTop+25);
	leftpos = parseInt(element.offsetLeft+element.clientWidth);

	yaliInstance.writeDIVhtml(toppos,leftpos);
	yaliInstance.show();
	return true;
}

function yali_start_from_link(element,yaliInstance,positionelement){
	yaliInstance.setActualTarget(element);
	ajax_list_yali_instance = yaliInstance;
	// wir setzen das div dorthin wo geklickt wurde
	toppos = parseInt(positionelement.offsetTop+25);
	leftpos = parseInt(positionelement.offsetLeft+positionelement.clientWidth);

	yaliInstance.writeDIVhtml(toppos,leftpos);
	yaliInstance.show();
}
