// JavaScript Document
function trim(sString) {
	/*
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
	*/
	return sString.replace(/\s+$|^\s+/g,"");
}
function cancellaVoce(elemento){
	var user = document.getElementById('email').value ;
	var pass = document.getElementById('password').value ; 
	if(elemento.value  == "username"){
		document.getElementById('email').value  = "" ;
		//   document.getElementById('username').style.backgroundColor = "white"; 
		if(document.getElementById('password').value  == "")
		{   document.getElementById('password').value  = "password" ;  
			//  document.getElementById('password').style.backgroundColor = "cccccc"; 
		}     
	}
	if(elemento.value  == "password") {
		document.getElementById('password').value  = "" ;  
		//  document.getElementById('password').style.backgroundColor = "white"; 
		if(document.getElementById('email').value  == "") {   
			document.getElementById('email').value  = "username" ;  
		 // document.getElementById('username').style.backgroundColor = "cccccc"; 
		}           
	} 
}

function cambiaPagina(pag){
	document.ricercaForm.paginaScelta.value=pag;
	document.ricercaForm.submit();
}

function dettaglioAnnuncio(id_an){
	document.ricercaForm.action='dettaglio_annuncio.php';
	document.ricercaForm.id_an.value=id_an;
	document.ricercaForm.submit();
}

function clean(){
	if(document.getElementById('descrizioneJA').value == 'Avvisami quando viene inserito un annuncio'){
		document.getElementById('descrizioneJA').value = '';
	}
}

function check_form_segnala() {
	// prendo i riferimenti ai campi
	var id_an = document.getElementById("id_an");
	
	var field_required = new Array('email_amico', 'email_personale');
	
	for (i=0;i<field_required.length;i++) {
		var field_handle = document.getElementById(field_required[i]);
		var field_label_handle = document.getElementById(field_required[i] + "_lbl");

		if (field_handle.type != 'checkbox' && field_handle.type != 'radio')
			ok_required = field_handle && field_handle.value=='';
		else
			ok_required = field_handle && field_handle.checked == false;
			
		if (ok_required)	{
			if (field_label_handle) {
				// la label puņ contenere html
				var field_label_value = field_label_handle.innerHTML;
				tag_start = field_label_value.indexOf('<');
				if (tag_start >= 0) 
					field_label_value = field_label_value.substr(0, tag_start);
				
				alert(field_label_value + ' č un campo obbligatorio');
			}
			field_handle.focus();
			break;
		}
	}
	
	if (!ok_required) {
		url_params = "?id_an=" + escape(trim(id_an.value)) + "&segnalaForm_submit=true";
		for (i=0;i<field_required.length;i++) {
			var field_handle = document.getElementById(field_required[i]);
			url_params += "&" + field_required[i] + "=" + escape(trim(field_handle.value));
		}
		
		var url = "ajax_call/segnala.php" + url_params;
		http.open("POST", url, true); 
		http.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
		http.onreadystatechange = handleHttpResponse_form_segnala; 
		http.send(null);
	}
}

function handleHttpResponse_form_segnala() { 
	try {
		var div_output_id = "fm_segnala_form_messages";
		if (http.readyState == 4) { 
			if (http.status == 200) {
				results = http.responseText; 
				var div_output = document.getElementById(div_output_id); 
				if (div_output) { div_output.innerHTML = results; }
			}
		} 
		else {
			var div_output = document.getElementById(div_output_id); 
			if (div_output) { 
				div_output.style.display = "block";
				div_output.innerHTML = ajax_sending_text; 
			}
		}
	}
	catch(e) {
		var div_output = document.getElementById(div_output_id); 
		if (div_output) { div_output.innerHTML = ajax_error_text; }
		alert(generic_server_error);
    }	
}

function check_form_jobalert() {

	var field_required = new Array('descrizioneJA');
	var field_multi_required = new Array('regioni', 'province', 'funzioni', 'settori');

	// prendo i riferimenti ai campi
	for (i=0;i<field_required.length;i++) {
		var field_handle = document.getElementById(field_required[i]);
		var field_label_handle = document.getElementById(field_required[i] + "_lbl");

		if (field_handle.type != 'checkbox' && field_handle.type != 'radio')
			ok_required = field_handle && field_handle.value=='';
		else
			ok_required = field_handle && field_handle.checked == false;
			
		if (ok_required)	{
			if (field_label_handle) {
				// la label puņ contenere html
				var field_label_value = field_label_handle.innerHTML;
				tag_start = field_label_value.indexOf('<');
				if (tag_start >= 0) 
					field_label_value = field_label_value.substr(0, tag_start);
				
				alert(field_label_value + ' č un campo obbligatorio');
			}
			field_handle.focus();
			break;
		}
	}
	
	if (!ok_required) {

		// prendo i riferimenti ai campi
		for (i=0;i<field_multi_required.length;i++) {
			var field_handle = document.getElementsByName(field_multi_required[i] + '[]');
			var field_label_handle = document.getElementById(field_multi_required[i] + "_lbl");
			
			ok_required = true;
			for (j=0;j<field_handle.length;j++) {
				//alert(field_handle[j].id);
				if (field_handle[j].type != 'checkbox' && field_handle[j].type != 'radio')
					ok_required = ok_required && (field_handle[j] && field_handle[j].value=='');
				else
					ok_required = ok_required && (field_handle[j] && field_handle[j].checked == false);
					
				if (!ok_required) break;
					
			}
			if (ok_required) {
				if (field_label_handle) {
					// la label puņ contenere html
					var field_label_value = field_label_handle.innerHTML;
					tag_start = field_label_value.indexOf('<');
					if (tag_start >= 0) 
						field_label_value = field_label_value.substr(0, tag_start);
					
					alert(field_label_value + ' č un campo obbligatorio');
				}
				field_handle[0].focus();
				break;
			}
		}
	}

	return !ok_required;
}

function hide_show_element(element_id)  {
	var div_element = document.getElementById(element_id);
	if (div_element) {
		if (div_element.style.display == 'none' || div_element.style.display == '') {
			div_element.style.display = 'block';
		}
		else {
			div_element.style.display = 'none';
		}
	}
}

function hide_element(element_id) {
	var div_element = document.getElementById(element_id);
	if (div_element) {
		div_element.style.display = 'none';
	}
}
function show_block_element(element_id) {
	var div_element = document.getElementById(element_id);
	if (div_element) {
		div_element.style.display = 'block';
	}
}
function show_element(element_id) {
	var div_element = document.getElementById(element_id);
	if (div_element) {
		div_element.style.display = '';
	}
}
function show_inline_element(element_id) {
	var div_element = document.getElementById(element_id);
	if (div_element) {
		div_element.style.display = 'inline';
	}
}


function getHTTPObject() {
	var xmlhttp;
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {     // Object of the current windows
    	try {
			xmlhttp = new XMLHttpRequest();     // Firefox, Safari, ...
			//xmlhttp.overrideMimeType('text/xml');
        } catch(e) {
			xmlhttp = false;
        }
	} 
	else if (window.ActiveXObject) {
		try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} 
		catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = false;
        	}
		}
	}
	return xmlhttp;
}

var http = "";
function chiamataAsincrona(url,parametri,async){
            var browser = navigator.appName;

    if (browser=="Microsoft Internet Explorer"){ // sniff browser
                    http=new ActiveXObject("Microsoft.XMLHTTP");
            } else {
        http=new XMLHttpRequest();
    }

            var page = url +"?"+ parametri;
            http.open('get', page , async);
    http.send(null)

            return http.responseText;
}

function aggiorna_dettaglio_click(id_an){
    chiamataAsincrona("/azioni_ajax.php","aggiorna_click_dettaglio=1&id_an="+id_an,false);
    //location.href=url;
}

var generic_server_error = "Richiesta dati dal server fallita, riprovare";


var ajax_loading_text = "<p>in caricamento...</p>";
var ajax_sending_text = "<p>sto inviando...</p>";
var ajax_error_text = "<p><span class=\"testo_errore\">[" + generic_server_error + "]</span></p>";
var http = getHTTPObject(); // HTTP Object
