/******************************************************************************************************************************
 * advsearch_left_form.source.js
 * 
 * Funzioni per il form di sinistra nella pagina dei risultati della ricerca avanzata
 * incluso in searchleft_adv_form.php
 *
 * richiede js/jquery.js
 *
 ******************************************************************************************************************************/

	// inizializzazione opzioni relative al tipo di camera
	show_room_options();
	
	$('#room_type').change( show_room_options );
	
	$('#searchleft').submit(function() {
		if (!validateAdvancedSearchForm()) {
			return false;
		}
		return true;
	});
	

function show_room_options() {
	var selectedRoomType = $('#room_type').val();	
	switch ( selectedRoomType ) {
		case '0':
			$('#shared_gender,#shared_beds').fadeOut();
			$('#kingbed').fadeOut();
			break;
		case '1':
			$('#shared_gender,#shared_beds').fadeOut('normal', function(){
				$('#kingbed').fadeIn();
			});
			break;
		case '2':
			$('#kingbed').fadeOut('normal', function(){
				$('#shared_gender,#shared_beds').fadeIn();
				if ( !( $('#mixed').attr('checked') || $('#male').attr('checked') || $('#female').attr('checked') ) ) {
					$('#mixed').attr('checked', 'true');
				}
			});
			break;
	} 
};

/**
 * Check form fields and returns true if all data is valid, otherwise shows the error messages and returns false
 */
function validateAdvancedSearchForm() {
	var error = false;
	var error_message = new String();
	
	location_field = $('#location:enabled');

	// Switch tra step015_advsearch.php e city_finder
	if ( $(location_field).length === 1 && $(location_field).val() !== '' ) {
	
		$('#searchleft').attr('action','http://' + servername + '/city_finder.php');
		return true;
		
	} else {
	
		$('#searchleft').attr('action','http://' + servername + '/step015_advsearch.php');
	
		// Controllo che sia stata selezionata una citt� e un paese
		if ( ( $('#country').val() == 0 ) || ( $('#city').val() == 0 ) ){
			error_message += cityerrormsg + '\n';
			error = true;
		}
		
		// Controllo che sia stata selezionata una valuta
		if ( $('#currency').val() == 0 ){
			error_message += currencyerrormsg + '\n';
			error = true;
		}
		
		// se � stata selezionata una camera shared verifico che almeno uno tra mixed, male e female sia selezionato
		if ( $('#room_type').val() == 2 ) {
			if ( ( $('#mixed').attr("checked") != true ) && ( $('#male').attr("checked") != true ) && ( $('#female').attr("checked") != true ) ) {
				error_message += sharederrormsg + '\n';
				error = true;
			}
		}
		
		// verifico che la data selezionata sia valida e non sia nel passato
		now = new Date();
		
		today = new Date( now.getFullYear(), now.getMonth(), now.getDate() );
		
		giorno = Number($('#giorno').val());
		mese = Number($('#mese').val()) - 1;
		anno = Number($('#anno').val());
		
		date = new Date( anno, mese, giorno );
		
		if ( !(date.getDate() === giorno && date.getFullYear() === anno && date.getMonth() === mese) || ( today.valueOf() > date.valueOf() ) ) {
			error_message += dateerrormsg + '\n';
			error = true;
		}
		
		if (error) {
			alert( error_message );
			return false;
		}
		
		var giorno = $('#giorno').attr('selectedIndex');
		var mese = $('#mese').attr('selectedIndex');
		var anno = $('#anno').attr('selectedIndex');
		var notti = $('#notti').attr('selectedIndex');
		var currency = $('#currency').attr('selectedIndex');
		var guests = $('#guests').attr('selectedIndex');
		
		if ( giorno === undefined ) {
			giorno = 0;
		}
		if ( mese === undefined ) {
			mese = 0;
		}
		if ( anno === undefined ) {
			anno = 0;
		}
		if ( notti === undefined ) {
			notti = 0;
		}
		if ( currency === undefined ) {
			currency = 0;
		}
		if ( guests === undefined ) {
			guests = 0;
		}
		
		// setto il cookie per la data, il numero di notti e le persone
		setCalCookie( giorno, mese, anno, notti, currency, guests );
		// cancello il cookie dela pagina settando la data nel passato
		document.cookie="asPage=0; expires=Fri, 02-Jan-1970 00:00:00 GMT";
		return true;
		
	}
};
