// Für Zend...
//<script>

// Funktionen für die Suchmaske

function ajax_suchmaske_init() {
	ajax_suchmaske_checkfields();
}

function ajax_suchmaske_checkfields() {
	ajax_loading(true);

	params = ajax_suchmaske_getfieldvalues();

	agent.call('/buchung/ajax_getdata.php?debug=1', 'suchmaske_checkfields', 'ajax_suchmaske_checkfields_callback', params);
}

function ajax_suchmaske_checkfields_callback (data) {
	fill_select("select_objektgruppe", 	data.objektgruppen, data.session.objektgruppe);
	fill_select("select_objektart", 	data.objektarten, 	data.session.objektart);
	fill_select("select_kategorie", 	data.kategorien,	data.session.kategorie);
	fill_select("select_betriebsart", 	data.betriebsarten,	data.session.betriebsart);
	fill_select("select_betrieb", 		data.betriebe, 		data.session.betrieb);
	
	// Die anderen Felder bleiben unverändert
	ajax_loading(false);
}

function ajax_get_anzahl_uebernachtungen () {
	ajax_loading(true);
	nr = get_select_value('select_buchungsart');
	gewaehlte_tage = document.getElementById('select_anzahl_tage').value;

	agent.call('/buchung/ajax_getdata.php','get_anzahl_uebernachtungen', 'ajax_get_anzahl_uebernachtungen_callback', nr, gewaehlte_tage);
}
function ajax_get_anzahl_uebernachtungen_callback (data) {
	if ( data == false ) {
		document.getElementById('buchen_suche_anmerkung').innerHTML = "Anzahl der Tage ist zu niedrig für die gewählte Buchungsart";
	} else {
		document.getElementById('buchen_suche_anmerkung').innerHTML = "";
	}
	ajax_loading(false);
}

function ajax_suchmaske_fetch_tibos_data () {
	ajax_loading(true);

	params = ajax_suchmaske_getfieldvalues();
	
	agent.call('/buchung/ajax_getdata.php', 'suchmaske_fetch_tibos_data', 'ajax_suchmaske_fetch_tibos_data_callback', params);
}

function ajax_suchmaske_fetch_tibos_data_callback (data) {
	if (typeof(data) == "string") {
		show_error(data);
	} else if (data.error) {
		if (typeof(data.errors) == "string") {
			ajax_suchmaske_process_errors(data.errors);
		} else if (typeof(data.errors) == "object") {
			// Hier macht der IE Probleme
			ajax_suchmaske_process_errors_from_tibos(data.errors);
		}
	} else if (data.link) {
		// Musste ich ausklammern, sonst gehts im IE nicht, weiß nicht
		// genau, für was AB das eingebaut hatte. 
		// ajax_suchmaske_process_errors("");
		document.location.href = data.link;
	}

	ajax_loading(false);
}

function ajax_suchmaske_process_errors (text) {
	//error_field = document.getElementById('fehler');
	if (document.getElementById('fehler')) {
		//error_field.innerHTML = text;
		document.getElementById('fehler').innerHTML = text;
	} else {
		alert(text);
	}
}

function ajax_suchmaske_process_errors_from_tibos (liste) {
	ret = ""

	for (i in liste) {
		error = liste[i];
		ret += "<li>"+error.text+"</li>";
	}

	if (ret) {
		ret = "<ul>"+ret+"</ul>";
		//alert(ret);
		// IE Probleme
		ajax_suchmaske_process_errors(ret);
	}
}

function ajax_suchmaske_getfieldvalues () {
	// Gibt keine Hashes in JS, deswegen Object => http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=20&t=006029
	params = new Object();
	
	// Daten zusammensammeln
	input_name = 'kdnr';
	input_id = 'select_'+input_name;
	input_value = get_input_value(input_id);
	params[input_name] = input_value;
	
	
	input_name = 'objektgruppe';
	input_id = 'select_'+input_name;
	input_value = get_select_value(input_id);
	params['input_name'] = input_value;

	input_name = 'objektart';
	input_id = 'select_'+input_name;
	input_value = get_select_value(input_id);
	params[input_name] = input_value;

	input_name = 'kategorie';
	input_id = 'select_'+input_name;
	input_value = get_select_value(input_id);
	params[input_name] = input_value;

	input_name = 'betriebsart';
	input_id = 'select_'+input_name;
	input_value = get_select_value(input_id);
	params[input_name] = input_value;

	input_name = 'betrieb';
	input_id = 'select_'+input_name;
	input_value = get_select_value(input_id);
	params[input_name] = input_value;
	
	// Buchungart
	input_name = 'buchungsart';
	input_id = 'select_'+input_name;
	input_value = get_select_value(input_id);
	params[input_name] = input_value;

	// Anreisetag
	input_name = 'datum_anreise';
	input_id = 'f-calendar-field-1';
	input_value = get_input_value(input_id);
	params[input_name] = input_value;

	// Abreisetag
	input_name = 'datum_abreise';
	input_id = 'f-calendar-field-2';
	input_value = get_input_value(input_id);
	params[input_name] = input_value;
	
	input_name = 'anzahl_tage';
	input_id = 'select_'+input_name;
	input_value = get_input_value(input_id);
	params[input_name] = input_value;
	
	for (var i = 1; i <= 30; i++) {
		// Personenkategorien
		input_name = 'anzahl_personen_' +i;
		input_id = 'select_'+input_name;
		input_value = get_input_value(input_id);
		params[input_name] = input_value;
	}

	input_name = 'kriterien';
	input_id = input_name;
	input_value = get_checkbox_values(input_id);
	params[input_name] = input_value;
	
	return params;
}
