// JavaScript Document

function fn_waiting(){
	// alert("ciao");
	document.getElementById("id_wait").style.visibility = "visible";
}

function change_picture(img_src) {
	document['imgBig'].src = img_src;
}

// validazione amministratore / operatore
function fn_check_admin(){
	if (document.form.adm_nome.value==''){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.form.adm_username.value==''){
		alert("Il campo 'USERNAME' è obbligatorio.");
		return false;
	}
	return true;
}

// validazione form login
function fn_check_login(){
	// non fare domande a cui non puoi darti una risposta
	// 'form' non è riconosciuto invece 'frmLog' si ... mah!
	if (document.frmLog.usr.value==''){
		alert("Il campo 'USERNAME' è obbligatorio.");
		return false;
	}
	if (document.frmLog.pwd.value==''){
		alert("Il campo 'PASSWORD' è obbligatorio.");
		return false;
	}

	return true;
}

function fn_check_ricerca(){
	var ric_nrCamere = document.getElementById("ric_nrCamere");
	if (ric_nrCamere){
		// ricerca avanzata (il numero delle camere è presente)
		// deve essere numerico
		if (!is_numero(document.form.ric_nrCamere.value)){
			alert("Il campo 'NUMERO DI CAMERE' deve essere numerico.");
			return false;
		}
	}
	
	return true;
}

function fn_check_ricerca_data(){
	if ((document.form.data_da.value!='') && !is_date(document.form.data_da.value)){
		return false;
	}
	if ((document.form.data_a.value!='') && !is_date(document.form.data_a.value)){
		return false;
	}

	return true;
}

// validazione form richiesta
function fn_check_richiesta(){
	trovato = false;
		
	for (i=0; i<document.form["cmbTipologia[]"].length; i++){
		if(document.form["cmbTipologia[]"][i].checked){
			trovato = true;
			break;
		}
	}
	if (!trovato){
		alert("Il campo 'TIPOLOGIA' è obbligatorio.");
		return false;
	}
	if (document.getElementById('cmbRegione')){
		// la regione c'è se sono in modalità combo (e quindi anche le zone)
		/*
		trovato = false;
		for (i=0; i<document.form["cmbZona[]"].length; i++){
			if(document.form["cmbZona[]"][i].checked){
				trovato = true;
				break;
			}
		}
		if (!trovato){
			alert("Il campo 'ZONA' è obbligatorio.");
			return false;
		}
		*/
		if (document.getElementById("eleMacrozone").innerHTML==""){
			alert("Il campo 'ZONA' è obbligatorio.");
			return false;
		}
	}
	
	if (document.getElementById('cmbRegione') && document.form.cmbRegione.value==-1){
		// la regione c'è se sono in modalità combo
		alert("Il campo 'REGIONE' è obbligatorio.");
		return false;
	}
	if (document.getElementById('cmbProvincia') && document.form.cmbProvincia.value==-1){
		// la provincia c'è se sono in modalità combo
		alert("Il campo 'PROVINCIA' è obbligatorio.");
		return false;
	}
	if (document.getElementById('cmbMacrozona') && document.form.cmbMacrozona.value==-1){
		// la macrozona c'è se sono in modalità combo
		alert("Il campo 'MACROZONA' è obbligatorio.");
		return false;
	}
	if (!is_numero(document.form.ric_nrCamere.value)){
		alert("Il campo 'NUMERO DI CAMERE' deve essere numerico.");
		return false;
	}

	return true;
}

// validazione form immobile
function fn_check_immobile(){
	trovato = false;
	quante = 0;
		
	if (document.form.imm_rif.value==""){
		alert("Il campo 'RIFERIMENTO' è obbligatorio.");
		return false;
	}

	// controllo di aver scelto solo una tipologia
	for (i=0; i<document.form["cmbTipologia[]"].length; i++){
		if(document.form["cmbTipologia[]"][i].checked){
			trovato = true;
			quante++;
			//break;
		}
	}
	if (!trovato){
		alert("Il campo 'TIPOLOGIA' è obbligatorio.");
		return false;
	}
	if (quante>1){
		alert("E' possibile scegliere una sola 'TIPOLOGIA' di immobile.");
		return false;
	}

	// controllo di aver scelto solo una zona
	trovato = false;
	quante = 0;
	for (i=0; i<document.form["cmbZona[]"].length; i++){
		if(document.form["cmbZona[]"][i].checked){
			trovato = true;
			quante++;
			//break;
		}
	}
	if (!trovato){
		alert("Il campo 'ZONA' è obbligatorio.");
		return false;
	}
	if (quante>1){
		alert("E' possibile scegliere una sola 'ZONA' di immobile.");
		return false;
	}

	if (document.form.imm_codMq.value==""){
		alert("Il campo 'MQ' è obbligatorio.");
		return false;
	}
	if (!is_numero(document.form.imm_codMq.value)){
		alert("Il campo 'MQ' deve essere numerico.");
		return false;
	}
	if (document.form.imm_codPrezzo.value==""){
		alert("Il campo 'PREZZO' è obbligatorio.");
		return false;
	}
	if (!is_numero(document.form.imm_codPrezzo.value)){
		alert("Il campo 'PREZZO' deve essere numerico.");
		return false;
	}
	if (!is_numero(document.form.imm_nrCamere.value)){
		alert("Il campo 'NUMERO DI CAMERE' deve essere numerico.");
		return false;
	}
	if (document.form.imm_schedaIT.value==""){
		alert("Il campo 'SCHEDA IN ITALIANO' è obbligatorio.");
		return false;
	}

	return true;
}

function fn_check_agenzia(){
	// questi controlli valgono sia nel caso in cui l'agenzia sia stata inserita dall'admin
	// sia nel caso in cui l'agenzia stessa stia facendo delle modifiche
	if (document.form.age_ragSoc.value==""){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.form.age_indirizzo.value==""){
		alert("Il campo 'INDIRIZZO' è obbligatorio.");
		return false;
	}
	if (document.form.cmbRegione.value==-1){
		alert("Il campo 'REGIONE' è obbligatorio.");
		return false;
	}
	if (document.form.cmbProvincia.value==-1){
		alert("Il campo 'PROVINCIA' è obbligatorio.");
		return false;
	}
	if (document.form.age_citta.value==""){
		alert("Il campo 'CITTA' è obbligatorio.");
		return false;
	}
	if (document.form.age_cap.value==""){
		alert("Il campo 'CAP' è obbligatorio.");
		return false;
	}
	if (document.form.age_email.value==""){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (!is_email(document.form.age_email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	if (document.form.age_referente.value==""){
		alert("Il campo 'REFERENTE' è obbligatorio.");
		return false;
	}
	if (document.form.age_preInt.value==""){
		alert("Il campo 'PREFISSO INTERNAZIONALE' è obbligatorio.");
		return false;
	}
	if (document.form.age_tel.value==""){
		alert("Il campo 'TELEFONO' è obbligatorio.");
		return false;
	}
	/*
	Febbraio 2011: rilascio il vincolo sul cap perchè a volte Sabine deve inserire agenzie estere
	e in quel caso non è detto che il cap sia numerico
	if (!is_numero(document.form.age_cap.value)){
		alert("Il campo 'CAP' deve essere numerico.");
		return false;
	}*/
	if (!is_numero(document.form.age_tel.value)){
		alert("Il campo 'TELEFONO' deve essere numerico e senza spazi.");
		return false;
	}
	if (!is_numero(document.form.age_fax.value)){
		alert("Il campo 'FAX' deve essere numerico e senza spazi.");
		return false;
	}
	if (document.form.cmbDomanda){
		// qui siamo nel caso in cui l'agenzia stessa stia facendo delle modifiche
		if (document.form.cmbDomanda.value==-1){
			alert("Il campo 'DOMANDA' è obbligatorio.");
			return false;
		}
		if (document.form.age_risposta.value==""){
			alert("Il campo 'RISPOSTA' è obbligatorio.");
			return false;
		}
		if (document.form.age_new_password.value!=document.form.age_renew_password.value){
			alert("Il campo 'PASSWORD' e 'CONFERMA PASSWORD' devono essere uguali.");
			return false;
		}
	}else{
		// qui siamo nel caso in cui l'agenzia sia stata inserita dall'admin
		if ((document.form.age_dataEndProva.value!="")&&(!is_date(document.form.age_dataEndProva.value))){
			return false;
		}
	}
	
	return true;
}

function fn_check_agenzia_non_accreditata(){
	if (trim(document.form.age_ragSoc.value)==""){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.form.age_ragSoc.value.length>100){
		alert("Il campo 'NOME' deve essere al massimo 100 caratteri.");
		return false;
	}
	if (trim(document.form.age_indirizzo).value==""){
		alert("Il campo 'INDIRIZZO' è obbligatorio.");
		return false;
	}
	if ((document.form.age_email.value!="")&&!is_email(document.form.age_email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	if (!is_numero(document.form.age_cap.value)){
		alert("Il campo 'CAP' deve essere numerico.");
		return false;
	}
	
	return true;
}

/* form cli_contatti.php */
function fn_check_cli_contatti(){
	if (document.form.nome.value==""){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if ((document.form.email.value=="")&&(document.form.tel.value=="")){
		alert("Inserire almeno un riferimento per essere ricontattati: 'EMAIL' o 'TEL'.");
		return false;
	}
	if (document.form.richiesta.value==""){
		alert("Il campo 'RICHIESTA' è obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il campo 'PRIVACY' è obbligatorio.");
		return false;
	}
	return true;
}

/* form cli_contatti_immagnet.php */
function fn_check_contatti(){
	if (document.form.age_ragSoc.value==""){
		alert("Il campo 'AGENZIA' è obbligatorio.");
		return false;
	}
	if (document.form.age_referente.value==""){
		alert("Il campo 'REFERENTE' è obbligatorio.");
		return false;
	}
	if ((document.form.cmbNazione.value=="")||(document.form.cmbNazione.value==-1)){
		alert("Il campo 'NAZIONE' è obbligatorio.");
		return false;
	}
	if ((document.form.cmbRegione.value=="")||(document.form.cmbRegione.value==-1)){
		alert("Il campo 'REGIONE' è obbligatorio.");
		return false;
	}
	if ((document.form.cmbProvincia.value=="")||(document.form.cmbProvincia.value==-1)){
		alert("Il campo 'PROVINCIA' è obbligatorio.");
		return false;
	}
	if (document.form.age_email.value==""){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (!is_email(document.form.age_email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	if (document.form.note.value==""){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il campo 'PRIVACY' è obbligatorio.");
		return false;
	}
	
	return true;
}

/* form cli_iscrizione_immagnet.php */
function fn_check_richiesta_iscrizione(){
	if (document.form.age_ragSoc.value==""){
		alert("Il campo 'AGENZIA' è obbligatorio.");
		return false;
	}
	if (document.form.age_referente.value==""){
		alert("Il campo 'REFERENTE' è obbligatorio.");
		return false;
	}
	if ((document.form.cmbNazione.value=="")||(document.form.cmbNazione.value==-1)){
		alert("Il campo 'NAZIONE' è obbligatorio.");
		return false;
	}
	if ((document.form.cmbRegione.value=="")||(document.form.cmbRegione.value==-1)){
		alert("Il campo 'REGIONE' è obbligatorio.");
		return false;
	}
	if ((document.form.cmbProvincia.value=="")||(document.form.cmbProvincia.value==-1)){
		alert("Il campo 'PROVINCIA' è obbligatorio.");
		return false;
	}
	if (document.form.age_indirizzo.value==""){
		alert("Il campo 'INDIRIZZO' è obbligatorio.");
		return false;
	}
	if (document.form.age_cap.value==""){
		alert("Il campo 'CAP' è obbligatorio.");
		return false;
	}
	if (document.form.age_citta.value==""){
		alert("Il campo 'CITTA' è obbligatorio.");
		return false;
	}
	if (document.form.age_tel.value==""){
		alert("Il campo 'TELEFONO' è obbligatorio.");
		return false;
	}
	if (document.form.age_email.value==""){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (!is_email(document.form.age_email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	if (document.form.note.value==""){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il campo 'PRIVACY' è obbligatorio.");
		return false;
	}
	
	return true;
}

function fn_check_contatti_immagnet(){
	if (document.form.age_ragSoc.value==""){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.form.age_referente.value==""){
		alert("Il campo 'REFERENTE' è obbligatorio.");
		return false;
	}
	if (document.form.age_email.value==""){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (document.form.note.value==""){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il campo 'PRIVACY' è obbligatorio.");
		return false;
	}
	if (!is_email(document.form.age_email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	
	return true;
}

function fn_check_contatti_agenzia(){
	if (document.form.note.value==""){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il campo 'PRIVACY' è obbligatorio.");
		return false;
	}
	
	return true;
}

function fn_check_annotazione(){
	if (document.form.agenzia.value==""){
		alert("Il campo 'AGENZIA' è obbligatorio.");
		return false;
	}
	if (document.form.nota_oggetto.value==""){
		alert("Il campo 'OGGETTO' è obbligatorio.");
		return false;
	}
	if (document.form.nota_testo.value==""){
		alert("Il campo 'TESTO' è obbligatorio.");
		return false;
	}
	if ((document.form.nota_dataRIC.value!="")&&(!is_date(document.form.nota_dataRIC.value))){
		return false;
	}
	if (document.form.nota_dataRIC.value!=""){
		// controllo che la data sia successiva alla data corrente
		today = new Date();
		gg = today.getDate();
		if (gg < 10) gg = "0" + gg;
		mm = today.getMonth() + 1;
		if (mm < 10) mm = "0" + mm;
		aa = today.getYear();
		if (aa < 1000) aa += 1900;
		oggi = gg + "/" + mm + "/" + aa
		//alert(oggi);
		if (!fn_confronta_data(oggi, document.form.nota_dataRIC.value)){
			if (confirm("Il campo 'RICONTATTARE IL' è precedente alla data corrente, procedere ugualmente?")){
				return true;
			}else{
				return false;
			};
		}
	}
	/*if (document.form.nota_dataRIC.value==""){
		alert("Il campo 'RICONTATTARE IL' è obbligatorio.");
		return false;
	}*/
	
	return true;
}

function fn_check_appuntamento(){
	if (document.form.agenzia.value==""){
		alert("Il campo 'AGENZIA' è obbligatorio.");
		return false;
	}
	if ((document.form.cod_agenzia.value==-1)||(document.form.cod_agenzia.value=="")){
		alert("Il campo 'AGENZIA' non è stato valorizzato nel modo corretto.");
		return false;
	}
	if (document.form.cmbRivenditori.value==-1){
		alert("Il campo 'OPERATORE' è obbligatorio.");
		return false;
	}
	if (document.form.nota.value==""){
		alert("Il campo 'NOTA' è obbligatorio.");
		return false;
	}

	return true;
}

function fn_check_disponibilita(){
	if (document.form.cmbRivenditori.value==-1){
		alert("Il campo 'OPERATORE' è obbligatorio.");
		return false;
	}
	trovato = false;
	for (i=0; i<document.form["cmbGiorniSettimana[]"].length; i++){
		if(document.form["cmbGiorniSettimana[]"][i].checked){
			trovato = true;
			break;
		}
	}
	if (!trovato){
		alert("Il campo 'GIORNI' è obbligatorio.");
		return false;
	}
	if (document.form.oraStart.value==document.form.oraEnd.value){
		alert("Il campo 'ORARIO' è obbligatorio.");
		return false;
	}
	
	return true;
}

function fn_check_newsletter(){
	if (document.form.oggetto.value==""){
		alert("Il campo 'OGGETTO' è obbligatorio.");
		return false;
	}
	/*if (document.form.testo.value==""){
		alert("Il campo 'TESTO' è obbligatorio.");
		return false;
	}*/
	trovato = false;
	for (i=0; i<document.form["cmbNazione[]"].length; i++){
		if(document.form["cmbNazione[]"][i].checked){
			trovato = true;
			break;
		}
	}
	if (!trovato){
		alert("Il campo 'NAZIONE AGENZIA' è obbligatorio.");
		return false;
	}
	
	return true;
}

function fn_check_faq(){
	if (document.form.faq_domanda.value==""){
		alert("Il campo 'DOMANDA' è obbligatorio.");
		return false;
	}
	if (document.form.faq_risposta.value==""){
		alert("Il campo 'RISPOSTA' è obbligatorio.");
		return false;
	}
	return true;
}

function fn_check_all_agenzie(){
	trovato = false;
	for (i=0; i<document.form["chkAgenzia[]"].length; i++){
		if(document.form["chkAgenzia[]"][i].checked){
			trovato = true;
			break;
		}
	}
	if (!trovato){
		alert("Selezionare almeno una agenzia a cui inviare la richiesta.");
		return false;
	}
	
	return true;	
}

// conferma cancellazione
function fn_confirm(){
	if (confirm("Procedere con la cancellazione?")){
		return true;
	}else{
		return false;
	};
}
function fn_checkAll_agenzie(){
	for (i=0; i<document.getElementsByName("chkAgenzia[]").length; i++){
		chk = document.getElementsByName("chkAgenzia[]");
		chk[i].checked = document.form.chkAll.checked;
	}
}
function fn_checkAll_macrozone(){
	for (i=0; i<document.form["cmbMacrozona[]"].length; i++){
		document.form["cmbMacrozona[]"][i].checked = document.form.chkAll.checked;
	}
}
function fn_checkAll_zone(){
	for (i=0; i<document.form["cmbZona[]"].length; i++){
		document.form["cmbZona[]"][i].checked = document.form.chkAll.checked;
	}
}

// aree di interesse adv
// scelto un range a livello di provincia voglio lo stesso range per tutte le macrozone di quella provincia
// MA
// non è possibile impostare un prezzo per una macroarea ad esempio per l'affitto
// se è stato impostato il check per non ricevere affitti da quella provincia
function fn_checkAll_aree(selectobj, codContratto, codProvincia){
	// prima controllo di non aver spuntato il check
	if (codContratto==1){
		nome_chk = "chkNoAcq_"+codProvincia;
		msg = "Questa operazione non è consentita con il checkbox 'nessun contratto di acquisto' attivo.";
	}else{
		nome_chk = "chkNoAff_"+codProvincia;
		msg = "Questa operazione non è consentita con il checkbox 'nessun contratto di affitto' attivo.";
	}
	
	if (document.form[nome_chk].checked){
		alert(msg);
		selectobj.selectedIndex = 0;
		return false;
	}
	
	// poi cambio a cascata tutte le combo
	nome_select_macrozona = "cmbPrezzo_"+codContratto+"_"+codProvincia+"[]";
	for (i=0; i<document.form[nome_select_macrozona].length; i++){
		document.form[nome_select_macrozona][i].selectedIndex = selectobj.selectedIndex;
	}
}

// aree di interesse adv
// potrei non voler essere contattato per gli affitto su Pisa
// azzero e disabilito tutte le macrozone di quella provincia
function fn_checkAll_contratto(chk_checked, codContratto, codProvincia){
	nome_select_provincia = "cmbPrezzo_"+codContratto+"_"+codProvincia;
	document.form[nome_select_provincia].selectedIndex = 0;
	//document.form[nome_select_provincia].disabled = chk_checked;

	nome_select_macrozona = "cmbPrezzo_"+codContratto+"_"+codProvincia+"[]";
	for (i=0; i<document.form[nome_select_macrozona].length; i++){
		document.form[nome_select_macrozona][i].selectedIndex = 0;
		//document.form[nome_select_macrozona][i].disabled = chk_checked;
	}
}

// non è possibile impostare un prezzo per una macroarea ad esempio per l'affitto
// se è stato impostato il check per non ricevere affitti da quella provincia
function fn_check_disabled(combo, codContratto, codProvincia){
	if (codContratto==1){
		nome_chk = "chkNoAcq_"+codProvincia;
		msg = "Questa operazione non è consentita con il checkbox 'nessun contratto di acquisto' attivo.";
	}else{
		nome_chk = "chkNoAff_"+codProvincia;
		msg = "Questa operazione non è consentita con il checkbox 'nessun contratto di affitto' attivo.";
	}
	
	if (document.form[nome_chk].checked){
		alert(msg);
		combo.selectedIndex = 0;
	}
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0, n);
}

function trim(s){
	return s;
}

// data una stringa controlla che sia un valido indirizzo email 
function is_email(str) {
	if (window.RegExp) {
    	var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    	var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    	var regnv = new RegExp(nonvalido);
    	var regv = new RegExp(valido);
    	if (!regnv.test(str) && regv.test(str))
      		return true;
    	return false;
	} else {
    	if(str.indexOf("@") >= 0)
      		return true;
    	return false;
  	}
}

// data una stringa controlla che sia un numero
function is_numero(str){
	var nr="1234567890";
	
	// se la stringa è vuota va bene
	if (str=="")
		return true;
		
	for (i=0; i<=(str.length-1); i++)
		if (nr.indexOf(str.charAt(i))==(-1))
			return false;

	return true;		
}

function is_date(dateStr) {
	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("Il formato corretto per la data è gg/mm/aaaa.");
		return false;
	}
	
	// p@rse date into variables
	day 	= matchArray[1];
	month 	= matchArray[3]; 
	year 	= matchArray[5];
	
	if (day < 1 || day > 31) {
		alert("Il giorno deve essere compreso fra 1 e 31.");
		return false;
	}

	if (month < 1 || month > 12) { 
		alert("Il mese deve essere compreso fra 1 e 12..");
		return false;
	}
		
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("30 dì conta Novembre con April Giugno e Settembre, di 28 ce n'è uno tutti gli altri ne han 31!")
		return false;
	}
	
	// check for february 29th
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("Quest'anno non è bisestile.");
			return false;
		}
	}
	return true; // date is valid
}

// mi aspetto due date nel formato gg/mm/aaaa
function fn_confronta_data(data1, data2){
	// trasformo le date nel formato aaaammgg (es. 20081103)
	data1str = data1.substr(6)+data1.substr(3, 2)+data1.substr(0, 2);
	data2str = data2.substr(6)+data2.substr(3, 2)+data2.substr(0, 2);
	
	// controllo che la seconda data sia successiva alla prima
	// (quindi se sono uguali va bene)
	if (data2str-data1str<0)		
		return false;
	else
		return true;
}

// data una stringa controlla che sia uno username o una password corretta
// sono ammessi solo numeri lettere e _
function is_usr_pwd(str){
	var strValido =  /^([0-9a-zA-Z\\_\\])*$/;
	if (!strValido.test(str))
		return false;
	
	return true;
}

// validazione form richiedi password
function fn_check_forget(){
	if (document.form.age_email.value==""){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (!is_email(document.form.age_email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	if (document.form.cmbDomanda.value==-1){
		alert("Il campo 'DOMANDA' è obbligatorio.");
		return false;
	}
	if (document.form.age_risposta.value==""){
		alert("Il campo 'RISPOSTA' è obbligatorio.");
		return false;
	}
	return true;
}

// ---------- AJAX ----------
function createRequestObject() {   
    var ro;   
    var browser = navigator.appName;   
    if(browser == "Microsoft Internet Explorer"){   
        ro = new ActiveXObject("Microsoft.XMLHTTP");   
    }else{   
        ro = new XMLHttpRequest();   
    }   
    return ro;   
}   
var http = createRequestObject();   

// ----- PER LA GESTIONE DELLE AGENZIE IN BASE ALLA OPTION DEMO -----
function sndReqAgenzia(demo){
    http.open('get', 'ajax_cmbAgenzia.php?demo='+demo);   
    http.onreadystatechange = handleResponseAgenzia;   
    http.send(null);   
}
function handleResponseAgenzia() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('agenzia').innerHTML = http.responseText;   
        }   
    }   
}  

// ----- PER LA GESTIONE DELLA COMBO CATEGORIA/TIPOLOGIA IN BASE ALLA LINGUA -----
function sndReqFieldsetTipologia(lang, padre){
	if (padre=="mod_immobili")
		http.open('get', 'ajax_cmbFieldsetTipologia_modImmobili.php?lang='+lang+'&padre='+padre);   
	else
		http.open('get', 'ajax_cmbFieldsetTipologia.php?lang='+lang+'&padre='+padre);   
    http.onreadystatechange = handleResponseFieldsetTipologia;   
    http.send(null);   
}
function handleResponseFieldsetTipologia() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('fieldsetTipologia').innerHTML = http.responseText;   
        }   
    }   
}  

// ----- PER LA GESTIONE DELLA ZONA (a partire dalla nazione) IN BASE ALLA MODALITA (grafica o testuale) -----
function sndReqFieldsetZona(modalita, padre){	
    if (modalita=="T"){
		// combo
		http.open('get', 'ajax_cmbFieldsetZonaCombo.php?padre='+padre);   
	}else{
		// mappe
		http.open('get', 'ajax_cmbFieldsetZonaMappa.php?padre='+padre);      
	}
    http.onreadystatechange = handleResponseFieldsetZona;   
    http.send(null);   
}
function handleResponseFieldsetZona() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('fieldsetZona').innerHTML = http.responseText;   
        }   
    }   
}  

// ----- PER LA GESTIONE DELLA COMBO TIPOLOGIA IN BASE ALLA CATEGORIA -----
function sndReqTipologia(codCategoria) {
    http.open('get', 'ajax_cmbTipologia.php?codCategoria='+codCategoria);     
    http.onreadystatechange = handleResponseTipologia;   
    http.send(null);   
}   
function handleResponseTipologia() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('tipologia').innerHTML = http.responseText;   
        }   
    }   
}  

// ----- PER LA GESTIONE DELLA COMBO PREZZO IN BASE ALLA VENDITA O AFFITTO -----
function sndReqPrezzo(codAcquisto, required) {   
    http.open('get', 'ajax_cmbPrezzo.php?codAcquisto='+codAcquisto+'&required='+required);   
    http.onreadystatechange = handleResponsePrezzo;   
    http.send(null);   
}   
function handleResponsePrezzo() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');
  	    document.getElementById('prezzo').innerHTML = http.responseText;   
        }   
    }   
}  

// ----- PER LA GESTIONE DELLA COMBO REGIONE IN BASE ALLA NAZIONE -----
function sndReqRegione(codNazione) {   
    http.open('get', 'ajax_cmbRegione.php?codNazione='+codNazione);   
    http.onreadystatechange = handleResponseRegione;   
    http.send(null);   
	
	// non riesco a gestire con ajax le combo in cascata
	// quindi se cambio la prima devo azzerare le successive
	// e costringere l'utente a fare la sua scelta
	document.form.cmbProvincia.options.length = 0;	
	if (document.getElementById('cmbMacrozona'))
		document.getElementById('cmbMacrozona').options.length = 0;
	if (document.getElementById('idZona'))
		document.getElementById('idZona').options.length = 0;   	
	if (document.getElementById('zona'))
		document.getElementById('zona').style.visibility = 'hidden';  	
	if (document.getElementById('macrozona'))
		document.getElementById('macrozona').style.visibility = 'hidden';  	
}   
function handleResponseRegione() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('regione').innerHTML = http.responseText;   			
        }   
	}   
}  

// ----- PER LA GESTIONE DELLA COMBO PROVINCIA IN BASE ALLA REGIONE -----
function sndReqProvincia(codRegione) {  
    http.open('get', 'ajax_cmbProvincia.php?codRegione='+codRegione);   
    http.onreadystatechange = handleResponseProvincia;   
    http.send(null);   

	// non riesco a gestire con ajax le combo in cascata
	// quindi se cambio la prima devo azzerare le successive
	// e costringere l'utente a fare la sua scelta
	if (document.getElementById('cmbMacrozona'))
		document.getElementById('cmbMacrozona').options.length = 0;
	if (document.getElementById('idZona'))
		document.getElementById('idZona').options.length = 0;   	
	if (document.getElementById('zona'))
		document.getElementById('zona').style.visibility = 'hidden';  	
	if (document.getElementById('macrozona'))
		document.getElementById('macrozona').style.visibility = 'hidden';  	
}   
function handleResponseProvincia() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {  
            update = response.split('|');   
            document.getElementById('provincia').innerHTML = http.responseText;   
        }   
    }   
} 

// ----- PER LA GESTIONE DELLA COMBO MACROZONA IN BASE ALLA PROVINCIA -----
function sndReqMacrozona(codProvincia) {   
	var pagina = "";
	if (document.getElementById('idPagina'))
		pagina = document.form.pagina.value;
    http.open('get', 'ajax_cmbMacrozona.php?codProvincia='+codProvincia+'&pagina='+pagina);   
    http.onreadystatechange = handleResponseMacrozona;   
    http.send(null);   

	//if (document.getElementById('idZona'))
		//document.getElementById('idZona').options.length = 0;   	
	if (document.getElementById('zona'))
		document.getElementById('zona').style.visibility = 'hidden';  	
}   
function handleResponseMacrozona() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
			if (document.getElementById('macrozona')){
	            document.getElementById('macrozona').innerHTML = http.responseText;   
				document.getElementById('macrozona').style.visibility = 'visible'; 
			}
        }   
    }   
}  

// ----- PER LA GESTIONE DELLA COMBO ZONA IN BASE ALLA MACROZONA -----
function sndReqZona(codMacrozona) {   
    http.open('get', 'ajax_cmbZona.php?codMacrozona='+codMacrozona);   
    http.onreadystatechange = handleResponseZona;   
    http.send(null);   
}   
function handleResponseZona() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('zona').innerHTML = http.responseText;  
			document.getElementById('zona').style.visibility = 'visible'; 
        }   
    }   
}  

// ----- PER LA GESTIONE DELLA COLLOCAZIONE GEOGRAFICA DELLA RICHIESTA -----
// in base alla collocazione geografica (IT o DE) cambiano le categorie,
// le tipologie, le macrozone e le zone
function sndReqTerritorio(lang, ric_cod, padre){
    http.open('get', 'ajax_territorio_combo.php?lang='+lang+'&ric_cod='+ric_cod+'&padre='+padre);   
    http.onreadystatechange = handleResponseTerritorio;   
    http.send(null);   
}
function handleResponseTerritorio() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('territorio').innerHTML = http.responseText;   
        }   
    }   
} 

// ----- PER LA GESTIONE DELLA MOLTEPLICITA' DELLE MACROAREE -----
function sndReqElencoMacrozone(){
	var str = "";
	var macrozona_cod = document.form.cmbMacrozona.value;
	var zona_cod = "";

	//alert(document.form["cmbZona[]"].length);
	//alert(document.getElementsByName("cmbZona[]").length);
	
	str = "macrozona_cod="+macrozona_cod; 
	trovato = false;
	/*
	// questo oggetto document.form["cmbZona[]"] non vede la zona
	// quando la zona è composta da una sola zona
	for (i=0; i<document.form["cmbZona[]"].length; i++){
		if(document.form["cmbZona[]"][i].checked){
			trovato = true;
			zona_cod = zona_cod + document.form["cmbZona[]"][i].value + "|";
		}
	}
	*/
	
	// questo oggetto document.getElementsByName("cmbZona[]")
	// risolve il problema precedente
	for (i=0; i<document.getElementsByName("cmbZona[]").length; i++){
		if(document.getElementsByName("cmbZona[]")[i].checked){
			trovato = true;
			zona_cod = zona_cod + document.getElementsByName("cmbZona[]")[i].value + "|";
		}
	}
	
	if (trovato){
		// a fronte di una macrozona ho N zone
		// voglio aggiungere o modificare le zone di una macrozona
		zona_cod = Left(zona_cod, String(zona_cod).length-1);
		str = str + "&zona_cod="+zona_cod; 
	}else{
		// a fronte di una macrozona non ho alcuna zona
		// voglio eliminare tutte le zone di una macrozona
		// quindi voglio eliminare quella macrozona
		// quindi come parametro lascio solo la macrozona
	}
	http.open('get', 'ajax_multipleZone.php?'+str);   
	http.onreadystatechange = handleResponseElencoMacrozone;   
	http.send(null);   
}
function handleResponseElencoMacrozone() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('eleMacrozone').innerHTML = http.responseText;   
        }   
    }   
} 

// ----- PER LA GESTIONE DELLA RICERCA DELLA MACROZONA / ZONA IN FASE DI INSERIMENTO DI UNA RICHIESTA -----
function sndReqCercaMacrozonaZona(){
	var campo_ricerca = document.form.campo_ricerca.value;
	
	if (campo_ricerca==""){
		alert("Il campo 'MACROZONA / ZONA' è obbligatorio.");
		return false;
	}

	http.open('get', 'ajax_cercaMacrozonaZona.php?campo_ricerca='+campo_ricerca);   
	http.onreadystatechange = handleResponseCercaMacrozonaZona;   
	http.send(null);   
}
function handleResponseCercaMacrozonaZona() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('risultato_ricerca').innerHTML = http.responseText;   
        }   
    }   
} 

// ----- PER LA GESTIONE DELLA MOLTEPLICITA' DEI REFERENTI -----
function sndReqReferente(){
    http.open('get', 'ajax_referente.php');   
    http.onreadystatechange = handleResponseReferente;   
    http.send(null);   
}
function handleResponseReferente() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('addReferente').innerHTML += http.responseText;   
        }   
    }   
} 

// ----- PER LA GESTIONE DELLA DISPONIBILITA' DEL CALENDARIO IN BASE ALL'OPERATORE SCELTO -----
function sndReqCalendario(cod_admin, dia, mes, ano){
    http.open('get', 'ajax_calendario.php?cod_admin='+cod_admin+'&dia='+dia+'&mes='+mes+'&ano='+ano);   
    http.onreadystatechange = handleResponseCalendario;   
    http.send(null);   
}
function handleResponseCalendario() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('calendario').innerHTML = http.responseText;   
        }   
    }   
} 

// ----- PER L'AUTOCOMPLETAMENTO DELLE AGENZIE NON ACCREDITATE -----
// in ajax_cmbAutocompleteAgenzia_01.php il tag <select> richiama fn_MatchSelected per cui seleziono
// una agenzia e il suo codice
var global_objTxtAgenzia = ""; 
var global_objTxtAgenziaHidden = ""; 
/*
function sndReqAutocompleteAgenzia_01(objTxtAgenzia, objTxtAgenziaHidden){
	global_objTxtAgenzia = objTxtAgenzia;
	global_objTxtAgenziaHidden = objTxtAgenziaHidden;
    http.open('get', 'ajax_cmbAutocompleteAgenzia_01.php?likeAgenzia='+objTxtAgenzia.value);   
    http.onreadystatechange = handleResponseAutocompleteAgenzia_01;   
    http.send(null); 
}
function handleResponseAutocompleteAgenzia_01() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('matchAgenzia').innerHTML = http.responseText;   
        }   
    }   
} 
function fn_MatchSelected(objSelect){
	var cod_agenzia = objSelect.value;
	var nome_agenzia = objSelect.options[objSelect.selectedIndex].text;
	global_objTxtAgenzia.value = nome_agenzia; 
	global_objTxtAgenziaHidden.value = cod_agenzia;
}
*/
// ----- PER L'AUTOCOMPLETAMENTO DELLE AGENZIE NON ACCREDITATE -----
// in adm_cal_new_agenzia : devo fare aprire il popup con la scheda dell'agenzia
// in ajax_cmbAutocompleteAgenzia.php il tag <p> richiama fn_SchedaAgenzia per cui seleziono
// la scheda di una agenzia (si basa sul nome dell'agenzia)
function sndReqAutocompleteAgenzia(objTxtAgenzia, form_action, dia, mes, ano){
	global_objTxtAgenzia = objTxtAgenzia;
	// in generale non c'è bisogno della data
	// ma nel caso di adm_cal_calendario è necessaria anche la data
	// per tornare al giorno scelto
	//alert('ajax_cmbAutocompleteAgenzia.php?likeAgenzia='+objTxtAgenzia.value+'&form_action='+form_action+'&data='+data);
	http.open('get', 'ajax_cmbAutocompleteAgenzia.php?likeAgenzia='+objTxtAgenzia.value+'&form_action='+form_action+'&dia='+dia+'&mes='+mes+'&ano='+ano);   
    http.onreadystatechange = handleResponseAutocompleteAgenzia;   
    http.send(null); 
}
function handleResponseAutocompleteAgenzia() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('matchAgenzia').innerHTML = http.responseText;   
        }   
    }   
} 
// ----- PER L'AUTOCOMPLETAMENTO DELLE AGENZIE NON ACCREDITATE -----
// in adm_cal_new_agenzia : devo fare aprire il popup con la scheda dell'agenzia
// in ajax_cmbAutocompleteAgenzia.php il tag <p> richiama fn_SchedaAgenzia per cui seleziono
// la scheda di una agenzia (si basa sull'indirizzo dell'agenzia)
function sndReqAutocompleteAgenziaIndirizzo(objTxtAgenzia, form_action){
	global_objTxtAgenzia = objTxtAgenzia;
    http.open('get', 'ajax_cmbAutocompleteAgenziaIndirizzo.php?likeIndirizzo='+objTxtAgenzia.value+'&form_action='+form_action);   
    http.onreadystatechange = handleResponseAutocompleteAgenziaIndirizzo;   
    http.send(null); 
}
function handleResponseAutocompleteAgenziaIndirizzo() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('matchAgenziaIndirizzo').innerHTML = http.responseText;   
        }   
    }   
} 
// le seguenti funzioni valgono per sndReqAutocompleteAgenzia e sndReqAutocompleteAgenziaIndirizzo
function fn_SchedaAgenzia(cod_agenzia, form_action, dia, mes, ano){
	// in generale non c'è bisogno della data
	// ma nel caso di adm_cal_calendario è necessaria anche la data
	// per tornare al giorno scelto
	http.open('get', 'ajax_schedaAgenzia.php?cod_agenzia='+cod_agenzia+'&form_action='+form_action+'&dia='+dia+'&mes='+mes+'&ano='+ano);   
    http.onreadystatechange = handleResponseSchedaAgenzia;   
    http.send(null); 
}
function handleResponseSchedaAgenzia() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('scheda_agenzia').innerHTML = http.responseText; 
			document.getElementById('scheda_agenzia').style.visibility = "visible";   
        }   
    }   
} 
// voglio chiudere la finestra della scheda agenzia
function fn_winClose(){
	document.getElementById('scheda_agenzia').innerHTML = "";   
	document.getElementById('scheda_agenzia').style.visibility = "hidden";   
	return false;
}
function fn_showTooltip(nota){
	http.open('get', 'ajax_tooltip.php?nota='+nota);   
    http.onreadystatechange = handleResponseTooltip;   
    http.send(null); 
}
function handleResponseTooltip() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('scheda_agenzia').innerHTML = http.responseText; 
			document.getElementById('scheda_agenzia').style.visibility = "visible";   
        }   
    }   
} 

