// JavaScript Document
function JQuery_initialization() {
	jQuery(document).ready(function() { 
									
		jQuery("a.tab_candidati").bind("click", function() {
				//jQuery("div#tab_aziende").hide();
				//jQuery("div#tab_candidati").show();

				jQuery("div#login_aziende").hide();
				jQuery("div#login_candidati").show();
			}
		);

		jQuery("a.tab_aziende").bind("click", function() {
				//jQuery("div#tab_aziende").hide();
				//jQuery("div#tab_candidati").show();

				jQuery("div#login_candidati").hide();
				jQuery("div#login_aziende").show();
			}
		);

	}); 
	
}

function addslashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}
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 doSubmit(form_name) {
	form = document.getElementById(form_name);
	if (form) {
		form.submit();
	}
}

function cancellaVoce(elemento) {
	var user = document.getElementById('email').value ;
	var pass = document.getElementById('password').value ; 
	if(elemento.value  == "email"){
		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  = "email" ;  
		 // document.getElementById('username').style.backgroundColor = "cccccc"; 
		}           
	} 
}

function cancellaVoceAzienda(elemento) {
	if (elemento.value  == "email") {
		document.getElementById('username').value  = "" ;
		if(document.getElementById('password_az').value  == "") document.getElementById('password_az').value  = "password" ;  
	}
	if (elemento.value  == "password") {
		document.getElementById('password_az').value  = "" ;  
		if(document.getElementById('username').value  == "") document.getElementById('username').value  = "email" ;  
	} 
}

function cambiaPagina(pag){
	document.ricercaForm.paginaScelta.value=pag;
	document.ricercaForm.submit();
}

function cambiaPerPagina(n){
	document.ricercaForm.n_per_pagina.value=n;
	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 textarea = document.getElementById("area_segnala");
		url_params += "&area_segnala=" + textarea.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);
        document.getElementById("segnalaForm").style.display = "block";
        document.getElementById("email_amico").value = "";
        document.getElementById("email_personale").value = "";
        document.getElementById("area_segnala").value = "";
	}
}

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 check_fm_invia_candidatura(alert_cv, alert_img, alert_lp) {
	var result = false;
        /*
	var selCV = document.forms["fm_invia_candidatura"].sel_cv;
	var selImg = document.forms["fm_invia_candidatura"].sel_img;
	var selLP = document.forms["fm_invia_candidatura"].sel_lp;
        */
        var selCV  = document.getElementsByName("sel_cv");
	var selImg = document.getElementsByName("sel_img");
	var selLP  = document.getElementsByName("sel_lp");
	
	cv_checked = false;
	for (i=0;i<selCV.length;i++) {
		cv_checked = cv_checked || selCV[i].checked; 
	}

	img_checked = false;
	for (i=0;i<selImg.length;i++) {
		img_checked = img_checked || selImg[i].checked; 
	}

	lp_checked = false;
	for (i=0;i<selLP.length;i++) {
		lp_checked = lp_checked || selLP[i].checked; 
	}
	
	if (!cv_checked) alert(alert_cv);
	else if (!img_checked) alert(alert_img);
	else if (!lp_checked) alert(alert_lp);
	
	result = cv_checked && img_checked && lp_checked;
	
	return result;
}

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;
}

function aggiorna(id){
	new Ajax.Request('/include/azioni_ajax.php' , {
	   	method: 'get', parameters: {
	   	aggiornaClickDettaglio: "s",
	   	id_an : id
		} ,
		asynchronous: false,onSuccess: function(transport) {
		}
	});
}

function aggiorna_lr(id){
	new Ajax.Request('/include/azioni_ajax.php' , {
	   	method: 'get', parameters: {
	   	aggiornaClickRedirect: "s",
	   	id_an : id
		} ,
		asynchronous: false,onSuccess: function(transport) {
		}
	});
}

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

