/* INTERFACCIA JS PER GESTIRE LA RICHIESTA DEI SERVIZI ACQUISTATI */

// Variabili Globali
var url_xml_rpc;			// indirizzo per chiamare la call.php
var form_obj;				// form principale
var struct_param = new Array();		// array standard per contenere la struttura/xml
var iso_encoding;			// variabile che conterrà la codifica della struttura/xml

//FUNZIONE CHE RICEVE COME PARAMETRO L'ID DI UN ACQUISTO E LO FA VISUALIZZARE A VIDEO
function show_service(service){	
	document.getElementById('services').style.display = 'none';
	if(document.getElementById('packages'))
		document.getElementById('packages').style.display = 'none';
	document.getElementById('service_'+service).style.display = display_for_browser();
	document.getElementById('button_service').style.display = display_for_browser();
	refresh_box();
}

//FUNZIONE CHE RICEVE COME PARAMETRO L'ID DI UN PACCHETTO ACQUISTATO E FA VISUALIZZARE I DETTAGLI
function show_package(pack){
	document.getElementById('packages').style.display = 'none';
	document.getElementById('services').style.display = 'none';
	document.getElementById('package_'+pack).style.display = display_for_browser();
	document.getElementById('button_package').style.display = display_for_browser();
	refresh_box();
}

//FUNZIONE CHE, INSERITI EMAIL E CODICE ACQUISTO NEL FORM MOSTRA I DETTAGLI DEL PRODOTTO ACQUISTATO
function get_service(){
	
	//come prima cosa nascondo eventuali messaggi d'errore
	document.getElementById('tr_error').style.display = 'none';
	
	var where = document.getElementById('summary');
	
	// controlla che sia stata inserita un'email
	if(document.getElementById('no_registered_email').value == '') {
		document.getElementById('tr_error').style.display = display_for_browser();
		document.getElementById('div_error').innerHTML=js_dic_ERRORWRONGMAIL;
		return false;
	}
	// controlla che sia stati inserito il codice e che sia numerico
	if(document.getElementById('service_id').value == '' || isNaN(document.getElementById('service_id').value)) {
		document.getElementById('tr_error').style.display = display_for_browser();
		document.getElementById('div_error').innerHTML=js_dic_ERRORWRONGSERVICE;
		return false;
	}
	
	//non mostro il form per la ricerca e invece mostro il loading
	document.getElementById('fields').style.display = 'none';
	document.getElementById('loading').style.display = display_for_browser();
	refresh_box();
	
	//mi costruisco i parametri da passare alla call
	struct_param['query'] = new Array();
	struct_param['header']['product'] = 'services';
	struct_param['header']['type'] = 'search_service';
	struct_param['query']['filter_function'] = 'show_service_details';
	struct_param['query']['email'] = document.getElementById('no_registered_email').value;
	struct_param['query']['service_id'] = document.getElementById('service_id').value;
	
	//la callback_html_response non fa altro che risputare fuori l'html
	html_request(struct_param, url_xml_rpc, iso_encoding, callback_html_response, where);
}

//Funzione che mostra a video il risultato
function callback_html_response(response, where){
	if(response == 'failure'){
		document.getElementById('loading').style.display = 'none';
		print_error(js_dic_ERRORNOSERVICE);
		document.getElementById('tr_error').style.display = display_for_browser();
		document.getElementById('button_error').style.display = display_for_browser();
	}
	else{
		document.getElementById('loading').style.display = 'none';
		where.style.display = 'block';
		where.innerHTML = response;		
		refresh_box();
	}
}


//FUNZIONE CHE, INSERITI NOME UTENTE E PASSWORD NEL FORM, AUTENTICA L'UTENTE E MOSTRA TUTTI I PRODOTTI CHE HA ACQUISTATO
function get_all_services(){
	
	//come prima cosa nascondo eventuali messaggi d'errore
	document.getElementById('tr_error').style.display = 'none';
	
	// controlla che sia stato inserito l'email
	if(document.getElementById('registered_email').value == '') {
		document.getElementById('tr_error').style.display = display_for_browser();
		document.getElementById('div_error').innerHTML=js_dic_ERRORWRONGMAIL;
		return false;
	}
	// controlla che sia stata inserita la password
	if(document.getElementById('field_pwd').value == '') {
		document.getElementById('tr_error').style.display = display_for_browser();
		document.getElementById('div_error').innerHTML=js_dic_ERRORNOPWD;
		return false;
	}
	
	//non mostro il form per la ricerca e invece mostro il loading
	document.getElementById('fields').style.display = 'none';
	document.getElementById('loading').style.display = display_for_browser();
	refresh_box();
	
	//dopodiche devo loggare l'utente
	struct_param['query'] = new Array();
	struct_param['header']['product'] = 'authentication';
	struct_param['header']['type'] = 'login';
	struct_param['query']['email'] = document.getElementById('registered_email').value;
	struct_param['query']['pwd'] = document.getElementById('field_pwd').value;
	
	if(document.getElementById('box'))
		refresh_box();
	xml_request(struct_param, url_xml_rpc, iso_encoding, authentication_callback_xml_request);
	//controllo se ci sono stati errori nel login, ina caso affermativo lancio 
	//un errore, altrimenti continuo con il lavoro...ma questo lo faccio nella callback
	
	//una volta che l'utente � loggato devo farmi dare tutta la lista dei suoi 
	//prodotti, esattamente come viene fatto all'apertura della pagina
}

// Funzione per la gestione dei valori ritornati
function authentication_callback_xml_request(response, type) {
	if(response == 'failure'){
		print_error(js_dic_ERRORLOGIN);
		document.getElementById('loading').style.display = 'none';
		document.getElementById('tr_error').style.display = display_for_browser();
		document.getElementById('button_error').style.display = display_for_browser();
	}
	else{
		//se il login � andato a buon fine, riaggiorno la pagina, che 
		//caricadosi fa venire fuori la lista dei servizi acquistati
		location.reload();
	}
	return;
}

//funzione che serve per tornare indietro via javascript
function rollback_service(param){
	//param e il parametro che indica l'id del campo dove sono e che voglio non si mostri pi�

	if (param == 'summary'){
		document.getElementById(param).parentNode.parentNode.style.display = 'none';
		document.getElementById('fields').style.display = display_for_browser();
		refresh_box();
		return;
	}
	if (param == 'tr_error'){
		document.getElementById(param).style.display = 'none';
		document.getElementById('button_error').style.display = 'none';
		document.getElementById('fields').style.display = display_for_browser();
		refresh_box();
		return;
	}
	if (param.substr(0, 8) == 'service_'){		
		document.getElementById(param).style.display = 'none';
		document.getElementById('services').style.display = display_for_browser();
		if(document.getElementById('packages'))
			document.getElementById('packages').style.display = display_for_browser();
		refresh_box();
		return;
	}
	if (param.substr(0, 8) == 'package_'){
		document.getElementById(param).style.display = 'none';
		document.getElementById('services').style.display = display_for_browser();
		document.getElementById('packages').style.display = display_for_browser();
		refresh_box();
		return;
	}
	return;
	/*
	document.getElementById('tr_error').style.display = display_for_browser();
	document.getElementById('tr_error').style.display = display_for_browser();
	document.getElementById('tr_error').style.display = display_for_browser();
	*/
}


// Gestisce le date di inizio e fine soggiorno e la durata che intercorre
function dates_manager(param) {
	if(!param)
		param='';
	reset_errors();
	
	var from = document.getElementById(param+'syear').value+'-'+document.getElementById(param+'smonth').value+'-'+document.getElementById(param+'sday').value;
	var to = document.getElementById(param+'eyear').value+'-'+document.getElementById(param+'emonth').value+'-'+document.getElementById(param+'eday').value;
	
	// controlla se le date inserite sono valide
	if(!check_date(document.getElementById(param+'syear').value, document.getElementById(param+'smonth').value, document.getElementById(param+'sday').value) || !check_date(document.getElementById(param+'eyear').value, document.getElementById(param+'emonth').value, document.getElementById(param+'eday').value)) {
		document.getElementById(param+'duration').className = 'red';
		document.getElementById(param+'duration').innerHTML = js_dic_ERRORWORNGDATES;
		return false;
	}
	
	// controlla se tra le due date intercorrono più di 45 giorni e li stampa
	global_duration = compute_days(from, to);
	if(global_duration <= 0)
		document.getElementById(param+'duration').className = 'red';
	else
		document.getElementById(param+'duration').className = 'green';
	document.getElementById(param+'duration').innerHTML = js_dic_DURATION+': '+global_duration+' '+js_dic_DAYS;
}




//FUNZIONE CHE ABILITA E DISABILITA I CAMPI A SECONDA CHE SIA O MENO CLICCATA UNA CHECKBOX
function enable_disable(param){
	if(document.getElementById(param).checked == true){
		document.getElementById(param+'_sday').disabled = false;
		document.getElementById(param+'_smonth').disabled = false;
		document.getElementById(param+'_syear').disabled = false;
		document.getElementById(param+'_eday').disabled = false;
		document.getElementById(param+'_emonth').disabled = false;
		document.getElementById(param+'_eyear').disabled = false;
	}
	else{
		document.getElementById(param+'_sday').disabled = true;
		document.getElementById(param+'_smonth').disabled = true;
		document.getElementById(param+'_syear').disabled = true;
		document.getElementById(param+'_eday').disabled = true;
		document.getElementById(param+'_emonth').disabled = true;
		document.getElementById(param+'_eyear').disabled = true;
	}
}


function search_group_services(){
	var where = document.getElementById('services_list');


	//controllo che sia checkata almeno una tra le check box dei pacchetti e quella dei prodotti singoli
	if(document.getElementById('packages').checked == false && document.getElementById('singleproducts').checked == false){
		print_error(js_dic_ERRORNOPACKAGEPRODUCT);
		return false;
	}
	
	// controlla che le date selezionate siano valide
	if(document.getElementById('buydate_duration').className == 'red' || document.getElementById('startdate_duration').className == 'red') {
		print_error(js_dic_ERRORWORNGDATES);
		return false;
	}
	
	//adesso per ogni campo controllo se � stato inserito qualcosa ed in caso affermativo lo memorizzo
		
	//mi costruisco i parametri da passare all'html
	struct_param['query'] = new Array();
	struct_param['header']['product'] = 'services';
	struct_param['header']['type'] = 'search_group_services';
	struct_param['query']['filter_function'] = 'show_manager_services_list';
	
	//cominciamo con la tipologia
	//se la tipologia � uguale a tutti, allora non serve che lo inserisca 
	//come parametro, e neanche il prodotto di conseguenza
	if(document.getElementById('typology').value != 'all'){
		if(document.getElementById('product').value != '***'){
			struct_param['query']['product'] = document.getElementById('product').value;
		}
		else{
			//se invece non � stato scelto un prodotto inserisco una tipologia
			struct_param['query']['typology'] = document.getElementById('typology').value;
		}
	}
		
	//adesso vado a farmi dare la data in cui � stata comprata
	if(document.getElementById('buydate').checked == true){
		struct_param['query']['buydate_startdate'] = document.getElementById('buydate_syear').value+'-'+document.getElementById('buydate_smonth').value+'-'+document.getElementById('buydate_sday').value;
		struct_param['query']['buydate_enddate'] = document.getElementById('buydate_eyear').value+'-'+document.getElementById('buydate_emonth').value+'-'+document.getElementById('buydate_eday').value;
	}
	
	//e la data di inizio polizza
	//adesso vado a farmi dare la data in cui � stata comprata
	if(document.getElementById('startdate').checked == true){
		struct_param['query']['startdate_startdate'] = document.getElementById('startdate_syear').value+'-'+document.getElementById('startdate_smonth').value+'-'+document.getElementById('startdate_sday').value;
		struct_param['query']['startdate_enddate'] = document.getElementById('startdate_eyear').value+'-'+document.getElementById('startdate_emonth').value+'-'+document.getElementById('startdate_eday').value;
	}
	
	
	//mi faccio dare distributori
	if(document.getElementById('distributor')){
		struct_param['query']['distributor'] = document.getElementById('distributor').value;
	}

	//i clenti
	struct_param['query']['client'] = document.getElementById('client').value;
	//suppliers
	if(document.getElementById('supplier') != null){
		struct_param['query']['supplier'] = document.getElementById('supplier').value;
	} else {
		struct_param['query']['supplier'] = 'none';
	}
	// groups
	struct_param['query']['group'] = document.getElementById('group').value;

	//le destinazioni
	if(document.getElementById('destination').value != 'all'){
		struct_param['query']['destination'] = document.getElementById('destination').value;
	}
	// get type manager or distributor
	if(document.getElementById('type')){
		struct_param['query']['type'] = document.getElementById('type').value;
	}
	
	
	//adesso mi faccio dare se vuole i prodotti songoli o solo i pacchetti o entrambi
	if(document.getElementById('packages').checked == true){
		struct_param['query']['packages'] = true;
	}
	if(document.getElementById('singleproducts').checked == true){
		struct_param['query']['singleproducts'] = true;
	}
	
	//a questo punto ho tutto, posso fare la html_request
	//non mostro il form per la ricerca e invece mostro il loading
	document.getElementById('search').style.display = 'none';
	document.getElementById('loading').style.display = 'block';
	refresh_box();
	//la callback_html_response non fa altro che risputare fuori l'html
	html_request(struct_param, url_xml_rpc, iso_encoding, callback_html_response, where);

}



//FUNZIONE CHE RICERCA TUTTI I SERVIZI ACQUISTATI E LI MOSTRA A VIDEO, E SECONDA DI ALCUNI CAMPI RIEMPITI
function search_services(){
	var where = document.getElementById('services_list');


	//controllo che sia checkata almeno una tra le check box dei pacchetti e quella dei prodotti singoli
	if(document.getElementById('packages').checked == false && document.getElementById('singleproducts').checked == false){
		print_error(js_dic_ERRORNOPACKAGEPRODUCT);
		return false;
	}
	
	// controlla che le date selezionate siano valide
	if(document.getElementById('buydate_duration').className == 'red' || document.getElementById('startdate_duration').className == 'red') {
		print_error(js_dic_ERRORWORNGDATES);
		return false;
	}
	
	//adesso per ogni campo controllo se � stato inserito qualcosa ed in caso affermativo lo memorizzo
		
	//mi costruisco i parametri da passare all'html
	struct_param['query'] = new Array();
	struct_param['header']['product'] = 'services';
	struct_param['header']['type'] = 'search_manager_services';
	struct_param['query']['filter_function'] = 'show_manager_services_list';
	
	//cominciamo con la tipologia
	//se la tipologia � uguale a tutti, allora non serve che lo inserisca 
	//come parametro, e neanche il prodotto di conseguenza
	if(document.getElementById('typology').value != 'all'){
		if(document.getElementById('product').value != '***'){
			struct_param['query']['product'] = document.getElementById('product').value;
		}
		else{
			//se invece non � stato scelto un prodotto inserisco una tipologia
			struct_param['query']['typology'] = document.getElementById('typology').value;
		}
	}
		
	//adesso vado a farmi dare la data in cui � stata comprata
	if(document.getElementById('buydate').checked == true){
		struct_param['query']['buydate_startdate'] = document.getElementById('buydate_syear').value+'-'+document.getElementById('buydate_smonth').value+'-'+document.getElementById('buydate_sday').value;
		struct_param['query']['buydate_enddate'] = document.getElementById('buydate_eyear').value+'-'+document.getElementById('buydate_emonth').value+'-'+document.getElementById('buydate_eday').value;
	}
	
	//e la data di inizio polizza
	//adesso vado a farmi dare la data in cui � stata comprata
	if(document.getElementById('startdate').checked == true){
		struct_param['query']['startdate_startdate'] = document.getElementById('startdate_syear').value+'-'+document.getElementById('startdate_smonth').value+'-'+document.getElementById('startdate_sday').value;
		struct_param['query']['startdate_enddate'] = document.getElementById('startdate_eyear').value+'-'+document.getElementById('startdate_emonth').value+'-'+document.getElementById('startdate_eday').value;
	}
	
	
	//mi faccio dare distributori
	if(document.getElementById('distributor')){
		struct_param['query']['distributor'] = document.getElementById('distributor').value;
	}

	//i clenti
	struct_param['query']['client'] = document.getElementById('client').value;
	//suppliers
	if(document.getElementById('supplier') != null){
		struct_param['query']['supplier'] = document.getElementById('supplier').value;
	} else {
		struct_param['query']['supplier'] = 'none';
	}
	// groups
	struct_param['query']['group'] = document.getElementById('group').value;

	//le destinazioni
	if(document.getElementById('destination').value != 'all'){
		struct_param['query']['destination'] = document.getElementById('destination').value;
	}
	// get type manager or distributor
	if(document.getElementById('type')){
		struct_param['query']['type'] = document.getElementById('type').value;
	}
	
	
	//adesso mi faccio dare se vuole i prodotti songoli o solo i pacchetti o entrambi
	if(document.getElementById('packages').checked == true){
		struct_param['query']['packages'] = true;
	}
	if(document.getElementById('singleproducts').checked == true){
		struct_param['query']['singleproducts'] = true;
	}
	
	//a questo punto ho tutto, posso fare la html_request
	//non mostro il form per la ricerca e invece mostro il loading
	document.getElementById('search').style.display = 'none';
	document.getElementById('loading').style.display = 'block';
	refresh_box();
	//la callback_html_response non fa altro che risputare fuori l'html
	html_request(struct_param, url_xml_rpc, iso_encoding, callback_html_response, where);

}

//Trova tutti i prodotti di una determinata tipologia
function products_of_typology(typology) {
	struct_param['query'] = new Array();
	struct_param['header']['product'] = 'availability';
	struct_param['header']['type'] = 'products_of_typology';
	struct_param['query']['typology'] = typology;
	
	document.getElementById('typology').value = typology;
	document.getElementById('products').innerHTML = js_dic_LOADING+'...';
	
	xml_request(struct_param, url_xml_rpc, iso_encoding, print_products_of_typology);
}


//Crea la select con tutti i prodotti di una determinata tipologia
function print_products_of_typology(response, type) {
	var html = js_dic_PRODUCTS+' &nbsp;'
	         + '<select class="font_10" name="product" id="product">'
		 + '<option value="***">'+js_dic_ALL+'</option>';
	
	var count = 0;
	for(var i in response) {
		html+= '<option value="'+response[i]['id']+'">'+response[i]['name']+'</option>';
		count++;
	}
	html+= '</select>';
	
	// se ho trovato almeno un prodotto mostro il bottone per fare la ricerca e la select coi prodotti
	if(count > 0) {
		document.getElementById('products').innerHTML = html;
		document.getElementById('button').style.display = 'inline';
	}
	// ...altrimenti nascondo il bottone e mostro un avviso
	else {
		document.getElementById('products').innerHTML = js_dic_NOPRODUCTS;
		document.getElementById('button').style.display = 'none';
	}
	
	refresh_box();
}

