var xmlHttp;
var stringa = "";
var div = {};
div.id = '';
var return_JSon = 0;

function GetXmlHttpObject() {
var xmlHttp=null;
	try {
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function stateChanged(){
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			if(return_JSon == 1) {
				var datiJson = eval("(" + xmlHttp.responseText + ")");
				if(datiJson.esito == 1) {
					var bgColor  = "#64E986";
					var txtColor = "#347235";
				} else {
					var bgColor  = "#E77471";
					var txtColor = "#C11B17";
				}
				var content = '';
				content += '<div id="save_result_123" style="padding:5px;margin-left:10px;float:left;color:'+txtColor+';background-color:'+bgColor+';border:1px solid '+txtColor+';width:300px;">'+datiJson.msg+'</div>';
				document.getElementById(div.id).innerHTML = content;
				switch (datiJson.tipo) {
					case "elImm" :
						if(datiJson.esito == 1) setTimeout("lista_immobili();", 1000);
					break;
					case "elCli" :
						if(datiJson.esito == 1) setTimeout("lista_clienti();", 1000);
					break;
					case "addUtente" :
						if(datiJson.esito == 1) setTimeout("change_page_menu('elenco_utenti.php','dmpages',true);", 1000);
					break;
					case "delUtente" :
						if(datiJson.esito == 1) setTimeout("lista_utenti();", 1000);
					break;
					case "addAgente" :
						if(datiJson.esito == 1) setTimeout("change_page_menu('elenco_agenti.php','dmpages',true);", 1000);
					break;
					case "delAgente" :
						if(datiJson.esito == 1) setTimeout("lista_agenti();", 1000);
					break;
					case "sal_note_appun" :
						if(datiJson.esito == 1) { 
							$('#save_result_123').fadeIn(1500);
							$('#save_result_123').fadeOut(1500);
							goTo_note_app(datiJson.idCl,datiJson.idImm,datiJson.sid,datiJson.type);
						}
					break;
					case "modImmobili" :
						if(datiJson.esito == 1) { 
							$('#save_result_123').fadeIn(1500);
							lista_immobili(); 
							$('#save_result_123').fadeOut(1500);
						}
					break;
					case "saveRichClienti" :
						document.getElementById('id').value = datiJson.id;
						if(datiJson.esito == 1) {  
							$('#save_result_123').fadeIn(1500);
							$('#save_result_123').fadeOut(1500);
						}
					break;
					case "saveRichSolo" :
						document.getElementById('id').value = datiJson.id;
						if(datiJson.esito == 1) {  
							$('#save_result_123').fadeIn(1500);
							$('#save_result_123').fadeOut(1500);
							call_ajax('richiesta_cliente.php','dmpages', '&sid='+datiJson.sid+'&id_cliente='+datiJson.id_val+'&id='+datiJson.id);
						}
					break;
					default:
						alert('caso non previsto per:'+datiJson.tipo);
					break;
				}
				//document.getElementById(div.id).innerHTML = xmlHttp.responseText;
			} else {
				switch (div.id) {
					case "res_sal_imm" :
						var datiJson = eval("(" + xmlHttp.responseText + ")");
						document.getElementById('sid').value=datiJson.sid;
						document.getElementById('id').value=datiJson.id;
						document.getElementById('rif').disabled = true;
						$('#'+div.id).fadeIn(2500);
						document.getElementById(div.id).innerHTML=datiJson.txt_msg;
						$('#'+div.id).fadeOut(2500);
						break;
					case "res_sal_clienti" :
						var datiJson = eval("(" + xmlHttp.responseText + ")");
						document.getElementById('sid').value=datiJson.sid;
						document.getElementById('id').value=datiJson.id;
						$('#'+div.id).fadeIn(2500);
						document.getElementById(div.id).innerHTML=datiJson.msg;
						$('#'+div.id).fadeOut(2500);
						break;
					case "res_rif" :
						var datiJson = eval("(" + xmlHttp.responseText + ")");
						document.getElementById(div.id).innerHTML=datiJson.txt_msg;
						document.getElementById('but_salva_anag').disabled = !datiJson.esito;
						break;
					case "controllo_cod_fisc" :
						var datiJson = eval("(" + xmlHttp.responseText + ")");
						if(datiJson.valido) {
							document.getElementById('sesso_'+datiJson.sesso).checked = true;
							document.getElementById('data_nascita').value = datiJson.data_nascita;
							seleziona(datiJson.idprov,document.getElementById('id_prov_nascita'));
							call_ajax('sel_comune.php','div_com_nascita',datiJson.idprov+'&idSelect=id_com_nascita&idcomune='+datiJson.idcomune);
						}
						document.getElementById('res_cod_fisc').innerHTML = datiJson.msg;
						break;
					case "tipol_imm_x_filtro_solo" :
						document.getElementById(div.id).innerHTML=xmlHttp.responseText;
						filtra_solo();
						break;
					case "sotto_tipol_imm_x_filtro_solo" :
						document.getElementById(div.id).innerHTML=xmlHttp.responseText;	
						filtra_solo();
						break;
					case "tipol_imm_x_filtro" :
						document.getElementById(div.id).innerHTML=xmlHttp.responseText;
						filtra_imm();
						break;
					case "sotto_tipol_imm_x_filtro" :
						document.getElementById(div.id).innerHTML=xmlHttp.responseText;	
						filtra_imm();
						break;
					case "tipol_imm_x_filtro_cl" :
						document.getElementById(div.id).innerHTML=xmlHttp.responseText;
						filtra_cl();
						break;
					case "sotto_tipol_imm_x_filtro_cl" :
						document.getElementById(div.id).innerHTML=xmlHttp.responseText;	
						filtra_cl();
						break;
					case "cancRichCl" :
						var datiJson = eval("(" + xmlHttp.responseText + ")");
						if(datiJson.esito) {
							var url = '&sid='+document.getElementById('sid').value;
							url += '&id='+document.getElementById('id_cliente').value;
							call_ajax('anagrafica_clienti.php','dmpages', url);
						} else {
							alert('errore cancellazione');	
						}
					break;
					default:
						document.getElementById(div.id).innerHTML=xmlHttp.responseText;
						break;
				}
			}
		}else{
			alert("Errore status: "+xmlHttp.status);
		}
	}
}

function onload_page(url) {
	if(url == 'elenco_immobili.php') {
		$('#data_da').datepicker();
		$('#data_a').datepicker();
		tip_this('tip');
	}
}

function call_ajax(url,idDiv,parametro_1) {
	return_JSon = 0;
	div.id = idDiv;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp===null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	url=url+"?par_1="+parametro_1;
	url=url+"&sid_rndm="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
	
function call_ajax_sincrono(url,idDiv,parametro_1) {
	return_JSon = 0;
	div.id = idDiv;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp===null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	url=url+"?par_1="+parametro_1;
	url=url+"&sid_rndm="+Math.random();
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200) {
			document.getElementById(idDiv).innerHTML=xmlHttp.responseText;
		}
	}
}

function PreparaDati(idForm) {
	var Laform = "";
	var Laform = document.getElementById(idForm);
	var numeroElementi = Laform.elements.length;
	stringa = "";
	for(var i = 0; i < numeroElementi; i++) {
		var objForm = Laform.elements[i];
		if((objForm.type != 'radio') || ((objForm.type == 'radio') && (objForm.checked))) {
			//if(objForm.name == 'is_imp_sport_1') alert(objForm.type + ' - ' + objForm.name+ ' - ' + objForm.checked);
			if((objForm.type == 'checkbox') && (!objForm.checked)) {
				valDt = '';
			} else {
				valDt = objForm.value;
			}
			stringa += objForm.name+"="+encodeURIComponent(valDt);
			if(i < numeroElementi-1) stringa += "&";
		}
	}
}

function execfunc(method,url,bool){
	xmlHttp.open(method,url,bool);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")
	xmlHttp.send(stringa);
	if(bool) {
		xmlHttp.onreadystatechange = stateChanged;
	} else {
		stateChanged();
		onload_page(url);
	}
}

function AJAXReq(url,idDiv,bool) {
	div.id = idDiv;
	if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	} else if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		if(!xmlHttp){
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	if(xmlHttp){
		execfunc('POST',url,bool);
	}else{
		alert("Impossibilitati ad usare AJAX");
	}
}

function InviaDati(url,idDiv,idForm,bool) {
  return_JSon = 0;
  if(typeof(idForm) != 'undefined') PreparaDati(idForm);
  AJAXReq(url,idDiv,bool);
}

function saveJson(url,idDiv,idForm) {
  return_JSon = 1;
  if(typeof(idForm) != 'undefined') PreparaDati(idForm);
  AJAXReq(url,idDiv,true);
}

function change_page_menu(url,idDiv,bool) {
  return_JSon = 0;
  stringa = 'sid='+document.getElementById('sid').value;
  AJAXReq(url,idDiv,bool);
}
/* *********************************************************** */

/*** funzioni generali controlli forms */
function ctrl_cmp_chg(objSel, descr) {
	var msgCmpChg = ""
	if((objSel.value == '') || (parseInt(objSel.value,10) == 0)) {
		objSel.style.backgroundColor = "#F99";
		msgCmpChg = "indicare "+descr;
	} else {
		objSel.style.backgroundColor = "#FFF";
	}
	document.getElementById('res_'+objSel.id).innerHTML = msgCmpChg;
	return msgCmpChg;
}

function ctrl_isNum_cmp_chg(objSel, descr, insOblig) {
	var msgCmpChg = ""
	if((objSel.value == '') && (parseInt(insOblig,10) == 1)) {
		objSel.style.backgroundColor = "#F99";
		msgCmpChg = "indicare "+descr;
	} else {
		if(!isNaN(objSel.value)) {
			objSel.style.backgroundColor = "#FFF";
		} else {
			objSel.style.backgroundColor = "#F99";
			msgCmpChg = descr+" deve contenere solo numeri";
		}
	}
	document.getElementById('res_'+objSel.id).innerHTML = msgCmpChg;
	return msgCmpChg;
}

function setValueCmp(idCmp,valCmp) {
	if(document.getElementById(idCmp) != null) document.getElementById(idCmp).value = valCmp;
}

function controllaCAP(capValue) {
	var re = /^\d{5}$/;
	if(!re.test(capValue)) {
		return "cap non valido";
	}
	return "";
}

function controllaUSERNAME(userValue) {
	var re = /^([a-zA-Z0-9\_\.])+$/; //'
	if(!re.test(userValue)) {
		return "inseriti caratteri non validi";
	}
	return "";	
}

function ctrl_cap (objSel) {
	var msgCap = controllaCAP(objSel.value);
	objSel.style.backgroundColor = (msgCap != "")? "#F99" : "#FFF";
	document.getElementById('res_'+objSel.id).innerHTML = msgCap;
	return msgCap;
}

function ctrl_user(objSel) {
	var msgUsr = controllaUSERNAME(objSel.value);
	objSel.style.backgroundColor = (msgUsr != "")? "#F99" : "#FFF";
	document.getElementById('res_'+objSel.id).innerHTML = msgUsr;
	return msgUsr;
}

function ctrl_cmp_mail(objSel,insOblig) {
	var msg = ""
	if((objSel.value == '') && (parseInt(insOblig,10) == 1)) {
		objSel.style.backgroundColor = "#F99";
		msg = "indicare l\'indirizzo e-mail";
	} else {
		var msg = (objSel.value != '')? controllaMAIL(objSel.value) : '';
		objSel.style.backgroundColor = (msg != "")? "#F99" : "#FFF";
	}
	document.getElementById('res_'+objSel.id).innerHTML = msg;
	return msg;
}

function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return "La lunghezza del codice fiscale non ט\n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\n";
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return "Il codice fiscale non ט corretto:\n"+
			"il codice di controllo non corrisponde.\n";
	return "";
}

function ControllaPIVA(pi)
{
	if( pi == '' )  return 'Indicare partita iva';
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non e\'\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return "La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return "La partita IVA non e\' valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return "";
}

function controllo_data(stringa) {	
	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;	
	if (!espressione.test(stringa))	{	    
		return 'formato data errato. formato corretto gg/mm/aaaa.';	
	}else{		
		anno = parseInt(stringa.substr(6),10);		
		mese = parseInt(stringa.substr(3, 2),10);		
		giorno = parseInt(stringa.substr(0, 2),10);
		var data=new Date(anno, mese-1, giorno);		
		if(data.getFullYear()==anno && data.getMonth()+1==mese && data.getDate()==giorno) {
			return '';
		}else{
			return 'formato data errato. formato corretto gg/mm/aaaa.';
		}	
	}
}

function controllaMAIL(mail){
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	if (!espressione.test(mail))	{
		return "La mail indicata non e\' valida!";
	}
	return '';
}

function controllaPWD(pwd){
	if(pwd.length < 8) {
		return "la password deve contenere almeno 8 caratteri alfanumerici.";
	} 
	var re = /^([a-zA-Z0-9])+$/;
	if(!re.test(pwd)) {
		return "inseriti caratteri non validi nel campo password.";
	} 
	var re = /[0-9]+/;
	if(!re.test(pwd)) {
		return "La password deve contenere lettere e caratteri numerici.";
	} 
	return '';
}

function ctrl_pwd(objSel) {
	if(objSel.value == '') {
		objSel.style.backgroundColor = "#F99";
		document.getElementById('res_'+objSel.id).innerHTML = "indicare la password";
		return "indicare la password";
	} else {	
		var msg = controllaPWD(objSel.value);
		objSel.style.backgroundColor = (msg != "")? "#F99" : "#FFF";
		document.getElementById('res_'+objSel.id).innerHTML = msg;
		return msg;
	}
}

function valida_rec_age() {
	var msg = '';
	msg += ctrl_cmp_chg(document.getElementById('nome_agente'),'il nome');
	msg += ctrl_cmp_chg(document.getElementById('cognome_agente'),'il cognome');
	msg += ctrl_isNum_cmp_chg(document.getElementById('cellulare'),'il cellulare', 0);
	msg += ctrl_cmp_mail(document.getElementById('email'),0);
	if(msg == '') {
		document.getElementById('salva_new').value = 1;
		saveJson('anagrafica_agenti.php','addNewAgente','new_agente',true);
	}
	return false;
}

function apriPopUp(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {
  var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
  var int_windowTop = (screen.height - a_int_windowHeight) / 2;
  var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
  var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
    if (parseInt(navigator.appVersion) >= 4) {
      obj_window.window.focus();
    }
}

function open_tb_filtro(url,tipo,sid,exists) {
	document.getElementById('tipo').value=tipo;
	document.getElementById('launch_fn').value=1;
	document.getElementById('sid').value=sid;
	document.getElementById('exists').value=exists;
	document.getElementById('rows').value="";
	document.getElementById('page').value="";
	document.getElementById('sidx').value="";
	document.getElementById('sord').value="";
	InviaDati(url,'griglia_res','form_incroci',true);
} 

function ric_filtro_imm(url,tipo,sidx,launch) {
	if(typeof(launch) == 'undefined') launch = 1;
	document.getElementById('tipo').value=tipo;
	document.getElementById('exists').value=1;
	document.getElementById('launch_fn').value=launch;
	if (sidx != '') {
		if (document.getElementById('sidx').value == sidx) {
			document.getElementById('sord').value = (document.getElementById('sord').value == 'ASC')? 'DESC' : 'ASC';
		} else {
			document.getElementById('sord').value =	'ASC';
		}
		document.getElementById('sidx').value=sidx;
	}
	InviaDati(url,'res_ric','form_incroci',true);
}

function filtra_cl() {
	//ric_filtro_imm('filtra_clienti_imm.php',document.getElementById('tipo').value,'',0);	
	ric_filtro_imm('filtra_clienti_imm.php',document.getElementById('tipo').value,'',0);	
} 

function filtra_imm() {
	//ric_filtro_imm('filtra_imm_clienti.php',document.getElementById('tipo').value,document.getElementById('sidx').value,0);
	ric_filtro_imm('filtra_imm_clienti.php',document.getElementById('tipo').value,'',0);
}

function filtra_solo() {
	//ric_filtro_imm('incroci_solo.php',document.getElementById('tipo').value,document.getElementById('sidx').value,0);
	ric_filtro_imm('incroci_solo.php',document.getElementById('tipo').value,'',0);
}

function ordGr(page,cmpOrd) {
	ric_filtro_imm(page,document.getElementById('tipo').value,cmpOrd);
}

function opt_solo_tipo(objCmp) {
	if(objCmp.value != 1) {
		document.getElementById('div_affitto_da').style.display = 'block';
		document.getElementById('div_affitto_a').style.display = 'block';
	} else {
		document.getElementById('div_affitto_da').style.display = 'none';
		document.getElementById('div_affitto_a').style.display = 'none';
	}
}

function filtra_to_clienti(id_imm) {
	document.getElementById('rows').value="";
	document.getElementById('page').value="";
	document.getElementById('sidx').value="";
	document.getElementById('sord').value="";	
	document.getElementById('id_immobile').value=id_imm;
	document.getElementById('exists').value=0;
	document.getElementById('sidx').value='';
	InviaDati('filtra_clienti_imm.php','dmpages','form_incroci',true);
}

function goTo_incrocio_cl(id_cliente,idRichiesta,sid,tipo) {
	stringa = "exists=0&launch_fn=0&idRichiesta="+idRichiesta+"&idcliente="+id_cliente+"&tipo="+tipo+"&sid="+sid;
	return_JSon = 0;
	AJAXReq('filtra_imm_clienti.php','dmpages',true);
}

function filtra_to_immobili(id_cliente,idRichiesta) {
	document.getElementById('rows').value="";
	document.getElementById('page').value="";
	document.getElementById('sidx').value="";
	document.getElementById('sord').value="";
	document.getElementById('launch_fn').value = 0;
	document.getElementById('id_cliente').value=id_cliente;
	document.getElementById('idRichiesta').value=idRichiesta;
	document.getElementById('exists').value=0;
	document.getElementById('sidx').value='';
	InviaDati('filtra_imm_clienti.php','dmpages','form_incroci',true);
}

function goTo_incrocio_imm(id_immobile,tipo,sid) {
	stringa = "exists=0&launch_fn=0&id_immobile="+id_immobile+"&tipo="+tipo+"&sid="+sid;
	return_JSon = 0;
	AJAXReq('filtra_clienti_imm.php','dmpages',true);
}

function open_rich_cliente(idDiv,idForm,idRichiesta) {
	showHide(idDiv);
	document.getElementById('id_richiesta').value = idRichiesta;
	document.getElementById('launch_fn').value = 1;	
	if(document.getElementById(idDiv).style.display == 'block') InviaDati('riep_rich_cliente.php',idDiv,idForm,true);
}

function ga(id) {
	document.getElementById('id_agente').value=id;
	InviaDati('anagrafica_agenti.php','content_page','anagrafica_agenti',true);
}

function ea(id) {
	if(confirm("Cancellare l'agente?")) {
		document.getElementById('id_agente').value=id;
		saveJson('elimina_anagrafiche.php','tr_'+id,'anagrafica_agenti');
	}
}
//go utenti
function eu(id) {
	if(confirm("Cancellare l'utente?")) {
		document.getElementById('id_ute').value=id;
		saveJson('elimina_anagrafiche.php','tr_'+id,'anagrafica_utenti');
	}
}

function gim(id) {
	document.getElementById('id').value=id;
	InviaDati('anagrafica_immobili.php','dmpages','anagrafica_immobili',true);
}

function eim(id) {
	if(confirm('Eliminare l\'anagrafica immobile?')) {
		document.getElementById('id').value=id;
		saveJson('elimina_anagr_immobile.php','res_el_imm','anagrafica_immobili');
	}
} 

function ecl(id) {
	if(confirm('Eliminare l\'anagrafica cliente \ne tutte le richieste salvate?')) {
		document.getElementById('id').value=id;
		saveJson('elimina_anagr_cliente.php','tr_'+id,'anagrafica_clienti');
	}
} 

function pubbAnn(elem,idAnn) {
	/*var msg = (!elem.checked)? 'Vuoi togliere dalla pubblicazione questo annuncio?' : 'Vuoi pubblicare questo annuncio?';
	if(confirm(msg)) {*/
		document.getElementById('id').value=idAnn;
		document.getElementById('pubblica').value= (!elem.checked)? 0 : 1;
		InviaDati('pubblica_annuncio_immobile.php','tot_ann_pubbl','anagrafica_immobili',true);
	/*} else {
		elem.checked = (!elem.checked);
	}*/
} 

function gcl(id) {
	document.getElementById('id').value=id;
	InviaDati('anagrafica_clienti.php','dmpages','anagrafica_clienti',true);
}

function goTo_note_app(id_cliente,id_imm,sid,type) {
	stringa = "id_cliente="+id_cliente+"&id_imm="+id_imm+"&type="+type+"&sid="+sid;
	return_JSon = 0;
	AJAXReq('note_appuntamenti.php','dmpages',true);
}

function goTo_sp_note_app(id,id_cliente,id_imm,sid,type,idDivReturn,cancella) {
	if(id=='') {
		if(document.getElementById('crea_new').style.display=='none') {
			stringa = "id="+id+"&id_cliente="+id_cliente+"&id_imm="+id_imm+"&type="+type+"&cancella="+cancella+"&sid="+sid;
			return_JSon = 0;
			AJAXReq('specchio_note_appuntamenti.php',idDivReturn,true);
			document.getElementById('crea_new').style.display='block';
		} else {
			document.getElementById('crea_new').style.display='none';
		}
	} else {
		stringa = "id="+id+"&id_cliente="+id_cliente+"&id_imm="+id_imm+"&type="+type+"&cancella="+cancella+"&sid="+sid;
		return_JSon = 0;
		AJAXReq('specchio_note_appuntamenti.php',idDivReturn,true);	
	}
}

function goTo_ana_imm(id_imm,sid) {
	stringa = "id="+id_imm+"&sid="+sid;
	return_JSon = 0;
	AJAXReq('anagrafica_immobili.php','dmpages',true);
}

function goTo_ana_cli(id_cli,sid) {
	stringa = "id="+id_cli+"&sid="+sid;
	return_JSon = 0;
	AJAXReq('anagrafica_clienti.php','dmpages',true);
}

/*** funzioni inserimento dati agenzia ***/
function ctrl_cmp_r_mail_dtAge() {
	var msgMails
	msgMails = (document.getElementById('email').value == document.getElementById('r_email').value)? "" : "L\'indirizzo email indicato non coincide con quello di controllo"; 
	document.getElementById('r_email').style.backgroundColor = (msgMails != "")? "#F99" : "#FFF";
	document.getElementById('res_r_email').innerHTML = msgMails;
	return msgMails;
};

function valida_rec_agenzia() {
	var msg = '';
	msg += ctrl_cmp_chg(document.getElementById('nome_agenzia'),'il nome agenzia');
	msg += ctrl_cmp_chg(document.getElementById('provincia'),'la provincia');
	msg += ctrl_cmp_chg(document.getElementById('comune'),'il comune');
	msg += ctrl_cmp_chg(document.getElementById('indirizzo'),'l\'indirizzo');
	msg += ctrl_cap(document.getElementById('cap'));
	msg += ctrl_isNum_cmp_chg(document.getElementById('telefono'),'il telefono principale', 1);
	msg += ctrl_isNum_cmp_chg(document.getElementById('fax'),'il fax', 0);
	msg += ctrl_cmp_mail(document.getElementById('email'),1);
	msg += ctrl_cmp_r_mail_dtAge();
	if(msg == '') InviaDati('anagrafica_agenzie_for_admin.php','dmpages','anagrafica_agenzie',true);
	setTimeout('document.getElementById("res_sal_imm").style.display="none"',2000);
}

function valida_rec_adm() {
	var msg = '';
	msg += ctrl_cmp_chg(document.getElementById('nome_agenzia'),'il nome agenzia');
	msg += ControllaPIVA(document.getElementById('partita_iva').value);
		document.getElementById('res_partita_iva').innerHTML = ControllaPIVA(document.getElementById('partita_iva').value);
	msg += ctrl_cmp_chg(document.getElementById('provincia'),'la provincia');
	msg += ctrl_cmp_chg(document.getElementById('comune'),'il comune');
	msg += ctrl_cmp_chg(document.getElementById('indirizzo'),'l\'indirizzo');
	msg += ctrl_cmp_chg(document.getElementById('nome_referente'),'il nome del referente');
	msg += ctrl_cmp_chg(document.getElementById('cognome_referente'),'il cognome del referente');
	msg += ctrl_isNum_cmp_chg(document.getElementById('telefono'),'il telefono principale', 1);
	msg += ctrl_isNum_cmp_chg(document.getElementById('fax'),'il fax', 0);
	msg += ctrl_cmp_mail(document.getElementById('email'),1);
	if(document.getElementById('privacy').checked == false) {
		msg += "per validare la registrazione e\' necessario autorizzare il trattamento dei dati.";
		document.getElementById('res_privacy').innerHTML = "per validare la registrazione e\' necessario autorizzare il trattamento dei dati.";
	} else {
		document.getElementById('res_privacy').innerHTML = "";
	}
	msg += ctrl_cmp_r_mail_dtAge();
	if(msg == '') InviaDati('anagrafica_agenzie.php','content_page','anagrafica_agenzie',true);
}

function valida_rec() {
	var msg = '';
	msg += ctrl_cmp_chg(document.getElementById('nome_agenzia'),'il nome agenzia');
	msg += ControllaPIVA(document.getElementById('partita_iva').value);
		document.getElementById('res_partita_iva').innerHTML = ControllaPIVA(document.getElementById('partita_iva').value);
	msg += ctrl_cmp_chg(document.getElementById('provincia'),'la provincia');
	msg += ctrl_cmp_chg(document.getElementById('comune'),'il comune');
	msg += ctrl_cmp_chg(document.getElementById('indirizzo'),'l\'indirizzo');
	msg += ctrl_cmp_chg(document.getElementById('nome_referente'),'il nome del referente');
	msg += ctrl_cmp_chg(document.getElementById('cognome_referente'),'il cognome del referente');
	msg += ctrl_isNum_cmp_chg(document.getElementById('telefono'),'il telefono principale', 1);
	msg += ctrl_isNum_cmp_chg(document.getElementById('fax'),'il fax', 0);
	msg += ctrl_cmp_mail(document.getElementById('email'),1);
	if(document.getElementById('privacy').checked == false) {
		msg += "per validare la registrazione e\' necessario autorizzare il trattamento dei dati.";
		document.getElementById('res_privacy').innerHTML = "per validare la registrazione e\' necessario autorizzare il trattamento dei dati.";
	} else {
		document.getElementById('res_privacy').innerHTML = "";
	}
	msg += ctrl_cmp_r_mail_dtAge();
	if(msg == '') InviaDati('../salva_registrazione.php','container','salva_registrazione',true);
}

function mod_age(idRow) {
	document.getElementById('id_agente').value = idRow;
	InviaDati('row_elenco_agenti.php','tr_'+idRow,'anagrafica_agenti',true);
}

function upd_agente(idRow) {
	var msg = '';
	msg += ctrl_cmp_chg(document.getElementById('nome_agente_'+idRow),'il nome');
	msg += ctrl_cmp_chg(document.getElementById('cognome_agente_'+idRow),'il cognome');
	msg += ctrl_isNum_cmp_chg(document.getElementById('cellulare_'+idRow),'il cellulare', 0);
	msg += ctrl_cmp_mail(document.getElementById('email_'+idRow),0);
	if(msg == '') InviaDati('row_elenco_agenti.php','tr_'+idRow,'mod_agente_'+idRow,true);
	return false;
}
/*** funzioni relative al cambiamento dati utente */
function ctrl_cmp_chgUte(objSel, descr) {
	if(objSel.value == '') {
		objSel.style.backgroundColor = "#F99";
		document.getElementById('res_'+objSel.id).innerHTML = "indicare "+descr;
		return "indicare "+descr;
	} else {
		objSel.style.backgroundColor = "#FFF";
		if(objSel.id=='r_password') {
			if(document.getElementById('password').value != objSel.value) {
				objSel.style.backgroundColor = "#F99";
				document.getElementById('res_'+objSel.id).innerHTML = "le due password non coincidono";
			} else {
				objSel.style.backgroundColor = "#FFF";
				document.getElementById('res_'+objSel.id).innerHTML = "";
				return "";
			}
		} else {
			var re = (objSel.id == 'utente')? /^([a-zA-Z0-9\_\.])+$/ : /^([a-zA-Z0-9 'אטילעשח])+$/; //'
			if(!re.test(objSel.value)) {
				document.getElementById('res_'+objSel.id).innerHTML = "inseriti caratteri non validi.";
				objSel.style.backgroundColor = "#F99";
				return "inseriti caratteri non validi.";
			} else {
				document.getElementById('res_'+objSel.id).innerHTML = "";
				objSel.style.backgroundColor = "#FFF";
				return "";
			}
		}
	}
}

function chg_pwd_ute(objChk) {
	document.getElementById('password').disabled = !objChk.checked;
	document.getElementById('r_password').disabled = !objChk.checked;
	if(!objChk.checked) {
		document.getElementById('password').style.backgroundColor = "#CCC";
		document.getElementById('r_password').style.backgroundColor = "#CCC";
		document.getElementById('password').value = "";
		document.getElementById('r_password').value = "";
		document.getElementById('res_password').innerHTML = "";
		document.getElementById('res_r_password').innerHTML = "";
	} else {
		document.getElementById('password').style.backgroundColor = "#FFF";
		document.getElementById('r_password').style.backgroundColor = "#FFF";	
	}
}

function ctrl_campi_obbl_camb_utente() {
	var msg = '';
	msg += ctrl_cmp_chgUte(document.getElementById('nome'), 'il nome');
	msg += ctrl_cmp_chgUte(document.getElementById('cognome'), 'il cognome');
	msg += ctrl_cmp_chgUte(document.getElementById('utente'), 'l&acute;username');
	msg += ctrl_cmp_mail(document.getElementById('email'),1);
	msg += ctrl_cmp_chgUte(document.getElementById('old_password'), 'la password per variare i dati');
	if(document.getElementById('chk_chg_pwd').checked) {
		msg += ctrl_pwd(document.getElementById('password'));
		msg += ctrl_cmp_chgUte(document.getElementById('r_password'), 'la password di controllo');
	}
	if(msg == '') InviaDati('change_pwd.php','dmpages','change_dt_utenti',true);
}

/*** fine funzioni relative al cambiamento dati utenti ***/
function addNewUte() {
	var objDivAdd = document.getElementById('addNewUtente');
	objDivAdd.style.display = (objDivAdd.style.display  == 'none')? 'block' : 'none'; 
	if (objDivAdd.style.display  == 'block') {
		document.getElementById('new').value = 1;
		InviaDati('anagrafica_utenti.php','addNewUtente','anagrafica_utenti',true);
	}
}

function addNewAge() {
	var objDivAdd = document.getElementById('addNewAgente');
	objDivAdd.style.display = (objDivAdd.style.display  == 'none')? 'block' : 'none'; 
	if (objDivAdd.style.display  == 'block') {
		document.getElementById('new').value = 1;
		InviaDati('anagrafica_agenti.php','addNewAgente','anagrafica_agenti',true);
	}
}

function ctrl_cmp_r_pwd_dtUte() {
	var msg
	msg = (document.getElementById('password').value == document.getElementById('r_password').value)? "" : "La password indicata non coincide con quella di controllo";
	document.getElementById('r_password').style.backgroundColor = (msg != "")? "#F99" : "#FFF";
	document.getElementById('res_r_password').innerHTML = msg;
	return msg;
}

function valida_rec_utente() {
	var msg = '';
	msg += ctrl_cmp_chg(document.getElementById('nome_utente'),'il nome');
	msg += ctrl_cmp_chg(document.getElementById('cognome_utente'),'il cognome');
	msg += ctrl_user(document.getElementById('utente'));
	msg += ctrl_cmp_mail(document.getElementById('email'),1);
	msg += ctrl_pwd(document.getElementById('password'));	
	msg += ctrl_cmp_r_pwd_dtUte();
	if(msg == '') saveJson('anagrafica_utenti.php','res_add_ute','new_utente');
}

/*** fine funzioni relative al cambiamento dati dell'utente ***/
function lista_utenti(sidx) {
	var oldSidx = document.getElementById('sidx').value;
	if(typeof(sidx) != 'undefined') {
		var objSord = document.getElementById('sord');
		if(oldSidx == sidx) {
			document.getElementById('sord').value = (objSord.value == 'asc')? 'desc' : 'asc';
		} else {
			document.getElementById('sord').value = 'asc';
		}
		document.getElementById('sidx').value = sidx;		
	}
	
	InviaDati('tb_elenco_utenti.php','res_ric','anagrafica_utenti',true);
}

function lista_agenti(sidx) {
	var oldSidx = document.getElementById('sidx').value;
	if(typeof(sidx) != 'undefined') {
		var objSord = document.getElementById('sord');
		if(oldSidx == sidx) {
			document.getElementById('sord').value = (objSord.value == 'asc')? 'desc' : 'asc';
		} else {
			document.getElementById('sord').value = 'asc';
		}
		document.getElementById('sidx').value = sidx;		
	}
	InviaDati('tb_elenco_agenti.php','res_ric','anagrafica_agenti',true);
}

function lista_mu(sidx) {
	var oldSidx = document.getElementById('sidx').value;
	if(typeof(sidx) != 'undefined') {
		var objSord = document.getElementById('sord');
		if(oldSidx == sidx) {
			document.getElementById('sord').value = (objSord.value == 'asc')? 'desc' : 'asc';
		} else {
			document.getElementById('sord').value = 'asc';
		}
		document.getElementById('sidx').value = sidx;		
	}
	document.getElementById('reload').value = 1;
	InviaDati('multiupload_list.php','res_ric','anagrafica_immobili',true);
}

function lista_clienti(sidx) {
	var oldSidx = document.getElementById('sidx').value;
	if(typeof(sidx) != 'undefined') {
		var objSord = document.getElementById('sord');
		if(oldSidx == sidx) {
			document.getElementById('sord').value = (objSord.value == 'asc')? 'desc' : 'asc';
		} else {
			document.getElementById('sord').value = 'asc';
		}
		document.getElementById('sidx').value = sidx;		
	}
	document.getElementById('ricalcola').value = 1;
	InviaDati('elenco_clienti.php','res_ric','anagrafica_clienti',true);	
}

function lista_pop_clienti(sidx) {
	var oldSidx = document.getElementById('sidx').value;
	if(typeof(sidx) != 'undefined') {
		var objSord = document.getElementById('sord');
		if(oldSidx == sidx) {
			document.getElementById('sord').value = (objSord.value == 'asc')? 'desc' : 'asc';
		} else {
			document.getElementById('sord').value = 'asc';
		}
		document.getElementById('sidx').value = sidx;		
	}
	InviaDati('popup_clienti.php','res_ric','popup_clienti',true);	
}

/*** funzioni relative all inserimento immobili */
function ric_data_gr_imm(id) {
	if(document.getElementById(id).value != '') {
		if((res = controllo_data(document.getElementById(id).value)) == '') { 
			document.getElementById('page').value = 1;
			lista_immobili();
		} else { 
			alert(res);
			document.getElementById(id).value = '';
		}
	} else {
		lista_immobili();
	}
}

function pul_grid_imm() {
	document.getElementById('comune').value = "";	
	document.getElementById('data_a').value = "";
	document.getElementById('data_da').value = "";
	seleziona(0,document.getElementById('id_tipologia_immobile'));
	document.getElementById('indirizzo').value = "";
	seleziona(0,document.getElementById('mq_a'));
	seleziona(0,document.getElementById('mq_da'));
	seleziona(0,document.getElementById('nr_locali'));
	seleziona(10,document.getElementById('nr_rows'));
	document.getElementById('page').value = 1;
	document.getElementById('prezzo_a').value = 0;	
	document.getElementById('prezzo_da').value = 0;	
	document.getElementById('proprietario').value = "";	
	document.getElementById('rif').value = "";	
	document.getElementById('LiveSearch').value = "";
	seleziona(0,document.getElementById('sel_tipo'));
	lista_immobili();
}

function pul_grid_cl() {
	document.getElementById('tutti_ute').checked = true;
	document.getElementById('is_proprietario').checked = false;
	document.getElementById('is_acquirente').checked = false;
	document.getElementById('is_collaboratore').checked = false;
	document.getElementById('is_inserzionista').checked = false;
	document.getElementById('is_proprietario').value = 0;
	document.getElementById('is_acquirente').value = 0;
	document.getElementById('is_collaboratore').value = 0;
	document.getElementById('is_inserzionista').value = 0;
	document.getElementById('cognome').value = "";
	document.getElementById('nome').value = "";
	document.getElementById('tel_abitazione').value = "";
	document.getElementById('cellulare').value = "";
	document.getElementById('email').value = "";
	document.getElementById('LiveSearch').value = "";
	lista_clienti();
}

function pul_grid_elenco_clienti_incroci() {
	document.getElementById('sidx').value = "";
	document.getElementById('sord').value = "";
	document.getElementById('rows').value = "10";
	document.getElementById('page').value = "1";
	document.getElementById('cognome').value = "";
	document.getElementById('nome').value = "";
	document.getElementById('tel_abitazione').value = "";
	document.getElementById('cellulare').value = "";
	document.getElementById('email').value = "";
	document.getElementById('LiveSearch').value = "";
	ric_filtro_imm('fn_mk_elenco_clienti_incroci.php',document.getElementById('tipo').value,document.getElementById('sidx').value);
}

function pul_grid_elenco_imm_incroci() {
	document.getElementById('sidx').value = "";
	document.getElementById('sord').value = "";
	document.getElementById('rows').value = "10";
	document.getElementById('page').value = "1";
	document.getElementById('rif').value = "";
	document.getElementById('indirizzo').value = "";
	document.getElementById('comune').value = "";
	document.getElementById('tipologia').value = "";
	document.getElementById('prezzo').value = "";
	document.getElementById('mq').value = "";
	document.getElementById('LiveSearch').value = "";
	ric_filtro_imm('fn_mk_elenco_imm_incroci.php',document.getElementById('tipo').value,document.getElementById('sidx').value);
}

function chk_tipo_ute(from) {
	if(from == 'tutti_ute') {
		document.getElementById('is_proprietario').checked = false;
		document.getElementById('is_acquirente').checked = false;
		document.getElementById('is_collaboratore').checked = false;
		document.getElementById('is_inserzionista').checked = false;	
	} else {
		if((document.getElementById('is_proprietario').checked == false) &&
		   (document.getElementById('is_acquirente').checked == false) &&
		   (document.getElementById('is_collaboratore').checked == false) &&
		   (document.getElementById('is_inserzionista').checked == false)) {
			document.getElementById('tutti_ute').checked = true;
		} else {
			document.getElementById('tutti_ute').checked = false;
		}
	}
	
	document.getElementById('page').value = 1;
	lista_clienti();
}

function exp_grid_imm() {
	document.getElementById('anagrafica_immobili').target = "_blank";
	document.getElementById('anagrafica_immobili').action = "exp_csv_grid_imm.php";
	document.getElementById('anagrafica_immobili').submit();
}

function lista_immobili(sidx) {
	var oldSidx = document.getElementById('sidx').value;
	if(typeof(sidx) != 'undefined') {
		var objSord = document.getElementById('sord');
		if(oldSidx == sidx) {
			document.getElementById('sord').value = (objSord.value == 'asc')? 'desc' : 'asc';
		} else {
			document.getElementById('sord').value = 'asc';
		}
		document.getElementById('sidx').value = sidx;		
	}
	document.getElementById('reload').value = 1;
	InviaDati('elenco_immobili.php','res_ric','anagrafica_immobili',false);
}

function lista_pop_immobili(sidx) {
	var oldSidx = document.getElementById('sidx').value;
	if(typeof(sidx) != 'undefined') {
		var objSord = document.getElementById('sord');
		if(oldSidx == sidx) {
			document.getElementById('sord').value = (objSord.value == 'asc')? 'desc' : 'asc';
		} else {
			document.getElementById('sord').value = 'asc';
		}
		document.getElementById('sidx').value = sidx;		
	}
	InviaDati('popup_immobili.php','res_ric','popup_immobili',true);	
}

function edit_affitto(edit) {
	if(edit) {
		document.getElementById('dt_affitto').style.display = 'block';		
		document.getElementById('affitto_da').style.backgroundColor = '#FFF';
		document.getElementById('affitto_da').readOnly = false;
		document.getElementById('affitto_a').style.backgroundColor = '#FFF';
		document.getElementById('affitto_a').readOnly = false;
	} else {		
		document.getElementById('affitto_da').value = '';
		document.getElementById('affitto_da').style.backgroundColor = '#CCC';
		document.getElementById('affitto_da').readOnly = true;
		document.getElementById('affitto_a').value = '';
		document.getElementById('affitto_a').style.backgroundColor = '#CCC';
		document.getElementById('affitto_a').readOnly = true;
		document.getElementById('dt_affitto').style.display = 'none';
	}
}

function cntr_tipologia() {
	if(!document.getElementById('tipo_contratto_1').checked && !document.getElementById('tipo_contratto_2').checked && !document.getElementById('tipo_contratto_3').checked) {
		document.getElementById('res_tipo_contratto').innerHTML = "indicare il tipo contratto";
		return "indicare il tipo contratto; ";
	}
	document.getElementById('res_tipo_contratto').innerHTML = "";
	return "";
}

function ctrl_cat_imm(objSel) {
	if(objSel.value == 0) {
		objSel.style.backgroundColor = "#F99";
		document.getElementById('res_cat_immobile').innerHTML = "indicare la categoria immobile";
		return "indicare la categoria immobile; ";	
	} else {
		objSel.style.backgroundColor = "#FFF";
		document.getElementById('res_cat_immobile').innerHTML = "";
		return "";		
	}
}

function ctrl_tipol_imm(objSel) {
	if(objSel.value == 0) {
		objSel.style.backgroundColor = "#F99";
		document.getElementById('res_tipol_imm').innerHTML = "indicare la tipologia immobile";
		return "indicare la tipologia immobile; ";	
	} else {
		objSel.style.backgroundColor = "#FFF";
		document.getElementById('res_tipol_imm').innerHTML = "";
		return "";		
	}
}

function ctrl_provincia(objSel,idDivRes) {
	if(typeof(idDivRes) == 'undefined') idDivRes = 'res_provincia';
	if(objSel.value == 0) {
		objSel.style.backgroundColor = "#F99";
		document.getElementById(idDivRes).innerHTML = "indicare la provincia";
		return "indicare la provincia; ";	
	} else {
		objSel.style.backgroundColor = "#FFF";
		document.getElementById(idDivRes).innerHTML = "";
		return "";		
	}
}

function ctrl_comune(objSel,idDivRes) {
	if(typeof(idDivRes) == 'undefined') idDivRes = 'res_comune';
	if(objSel.value == 0) {
		objSel.style.backgroundColor = "#F99";
		document.getElementById(idDivRes).innerHTML = "indicare il comune";
		return "indicare il comune; ";	
	} else {
		objSel.style.backgroundColor = "#FFF";
		document.getElementById(idDivRes).innerHTML = "";
		return "";
	}
}

function ctrl_campi_obbl_form_imm() {
	var msg = '';
	msg += cntr_tipologia();
	msg += ctrl_cat_imm(document.getElementById('id_cat_immobile'));
	msg += ctrl_tipol_imm(document.getElementById('id_tipologia_immobile'));
	msg += ctrl_provincia(document.getElementById('id_provincia'));
	msg += ctrl_comune(document.getElementById('id_comune'),'res_id_comune');
	msg += ctr_data(document.getElementById('data_inserzione') ,'res_inserzione' ,0);
	if (document.getElementById('tipo_contratto_2').checked == true || document.getElementById('tipo_contratto_3').checked == true) {
		msg += ctr_data(document.getElementById('affitto_da'),'res_affitto_da',0);
		msg += ctr_data(document.getElementById('affitto_a') ,'res_affitto_a' ,0);	
	}
	return msg;
}

function ctr_cf_cliente(objCmp) {
	if(objCmp.value != '') {
		objCmp.value=objCmp.value.toUpperCase();
		call_ajax('../risorse/ctr_cod_fisc.php','controllo_cod_fisc', '&cod_fisc='+objCmp.value);
	} else {
		document.getElementById('res_cod_fisc').innerHTML = '';
	}
}

function ctr_piva_cliente(objCmp) {
	if(objCmp.value != '') {
		document.getElementById('res_partita_iva').innerHTML = ControllaPIVA(objCmp.value);
	} else {
		document.getElementById('res_partita_iva').innerHTML = '';
	}
}

function ctr_data(objCmp,idCmpRes,obbligatorio) {
	if (((objCmp.value != '') && (objCmp.value != '00/00/0000')) || (obbligatorio == 1)) {
		msg = controllo_data(objCmp.value);
		if(idCmpRes != '') {
			if(msg != '') document.getElementById(idCmpRes).innerHTML = msg;
		} else {
			if(msg != '') alert(msg);
		}
		return msg;
	} else {
		if(idCmpRes != '') document.getElementById(idCmpRes).innerHTML = '';
		return '';
	}
}

function rel_dt_note_app(objCmp) {
	if(ctr_data(objCmp,'',0) != '') {
		document.getElementById(objCmp.id).value = '';
	} else {
		InviaDati('note_appuntamenti.php','dmpages','note_apps',true);
	}
}

function ctr_tipo_cliente() {
	msg = '';
	if((document.getElementById('is_proprietario').checked == false) &&
	   (document.getElementById('is_acquirente').checked == false) &&
	   (document.getElementById('is_collaboratore').checked == false) &&
	   (document.getElementById('is_inserzionista').checked == false)) {
		msg = "selezionare il tipo cliente";
	}
	document.getElementById('res_tipo_cliente').innerHTML = msg;
	return msg;
}

function save_new_utente() {
	if((document.getElementById('is_proprietario').checked == false) &&
	   (document.getElementById('is_acquirente').checked == false) &&
	   (document.getElementById('is_collaboratore').checked == false) &&
	   (document.getElementById('is_inserzionista').checked == false)) {
		alert("selezionare il tipo cliente\ndati non salvati");
	} else {
		document.getElementById('modifica').value=1; 
		InviaDati('popup_new_cliente.php','content_popup_clienti','modifica_cliente',true);
	}
}

function save_clienti() {
	document.getElementById('res_errs_page').innerHTML = '';
	msg = '';
	msg = ctr_tipo_cliente();
	if(document.getElementById('cod_fisc').value != '')     msg += ControllaCF(document.getElementById('cod_fisc').value);
	if(document.getElementById('partita_iva').value != '')  msg += ControllaPIVA(document.getElementById('partita_iva').value);
	if((document.getElementById('data_nascita').value != '') && (document.getElementById('data_nascita').value != '00/00/0000')) msg += controllo_data(document.getElementById('data_nascita').value);
	if(document.getElementById('email').value != '')    msg += controllaMAIL(document.getElementById('email').value);
	if(document.getElementById('email_ac').value != '') msg += controllaMAIL(document.getElementById('email_ac').value);
	if(document.getElementById('cognome').value == '') {
		msg += 'Inserire il cognome';
		document.getElementById('res_cognome').innerHTML = 'Inserire il cognome';
	}
	if(msg != '') {
		document.getElementById('res_errs_page').style.display = 'block';
		document.getElementById('res_errs_page').innerHTML = "correggere prima gli errori in pagina";
	} else {
		document.getElementById('salva').value = 1;
		InviaDati('salva_anagr_cliente.php','res_sal_clienti','anagrafica_clienti',true);
	}
}

function salva_note_app(idDivRit,idForm,valIdImm,valIdCli) {
	if(valIdImm == '') {
		alert('indicare l\'immobile');
	} else {
		if(valIdCli == '') {
			alert('indicare il cliente');
		} else {	
			saveJson('salva_note_appuntamenti.php',idDivRit,idForm);
		}
	}
}

function salva_rich_cliente(idForm) {
	var msg = '';	
	if (document.getElementById('tipo_contratto_2').checked == true || document.getElementById('tipo_contratto_3').checked == true) {
		var objAffDa = document.getElementById('affitto_da');
		var objAffA = document.getElementById('affitto_a');
		msg += (objAffDa.value != '')? ctr_data(objAffDa,'res_affitto_da',0) : '';
		msg += (objAffA.value != '')?  ctr_data(objAffA,'res_affitto_a' ,0) : '';
	}
	if(msg == '') {
		saveJson('salva_richiesta_cliente.php','res_sal_clienti',idForm);
		document.getElementById('res_errs_page').innerHTML = "";
	} else {
		document.getElementById('res_errs_page').innerHTML = "- Per procedere al salvataggio correggere gli errori in pagina";		
	}
}

function save_imm() {
	var msg = '';	
	msg = ctrl_campi_obbl_form_imm();
	if(msg == '') {
		InviaDati('salva_anagr_immobile.php','res_sal_imm','anagrafica_immobili',true);
		document.getElementById('res_errs_page').innerHTML = "";
	} else {
		document.getElementById('res_errs_page').innerHTML = "- Per procedere al salvataggio correggere gli errori in pagina";		
	}
}

function imp_conf_multi_all(conftype) {
	var formChks = document.getElementById('conf_multi');
	var numeroChks = formChks.elements.length;
	arrChks = "";
	for(var i = 0; i < numeroChks; i++) {
		objForm = formChks.elements[i];
		if(objForm.type == 'radio') {
			switch (conftype) {
				case 1 :
					   if(objForm.id.indexOf('_1') > 0) document.getElementById(objForm.id).checked = true;
				   break;
				case 2 :
					   if(objForm.id.indexOf('p_2') > 0)  document.getElementById(objForm.id).checked = true;
					   if(objForm.id.indexOf('gr_1') > 0) document.getElementById(objForm.id).checked = true;
				   break;
				case 0 :
					   if(objForm.id.indexOf('_2') > 0) document.getElementById(objForm.id).checked = true;
				   break;
				default :
					   if(objForm.id.indexOf('_2') > 0) document.getElementById(objForm.id).checked = true;
				   break;
			}
		} 
	}
}

function save_conf_multi() {
	document.getElementById('salva_conf_multi').value = 1;
	setTimeout("document.getElementById('res_sal_conf').style.display = 'none'",4000);
	InviaDati('conf_multiupload.php','dmpages','conf_multi',true);
}

function sel_multiup(objChiamante, ids_check) {
	var formChks = document.getElementById('form_chks');
	var numeroChks = formChks.elements.length;
	arrChks = "";
	for(var i = 0; i < numeroChks; i++) {
		objForm = formChks.elements[i];
		if((objForm.type == 'checkbox')&&(objForm.name == 'chk')) {
			if(!(objForm.value.indexOf(ids_check) < 0)) document.getElementById(objForm.id).checked = objChiamante.checked;
		} 
	}
}

function save_multiupload() {
	var formChks = document.getElementById('form_chks');
	var numeroChks = formChks.elements.length;
	arrChks = "";
	for(var i = 0; i < numeroChks; i++) {
		objForm = formChks.elements[i];
		if((objForm.type == 'checkbox')&&(objForm.name == 'chk')) {
			arrChks += (objForm.checked)? objForm.value+'_1;': objForm.value+'_0;';
		} 
	}
	document.getElementById('value_chks').value = arrChks;
	document.getElementById('reload').value = 1;
	document.getElementById('salva_multi_list').value = 1;
	setTimeout("document.getElementById('res_sal_conf').style.display = 'none'",4000);
	InviaDati('multiupload_list.php','dmpages','anagrafica_immobili',true);
}

var lastIdTab = '';
function showTab(width) {
	$("#boxBody").animate({ marginLeft: width}, 300 );
	//e.preventDefault();
}

function seleziona(valore,objSel){
	for(var k=0;k<objSel.options.length;k++){
		if(objSel.options[k].value==valore){
			objSel.selectedIndex=k;
			break;
		}
	}
}

function openListClienti(sid,id_rit_val,id_rit_descr,typeTab) { 
	ajaxwin=dhtmlwindow.open("ajaxbox", "ajax", "popup_clienti.php?sid="+sid+"&id_rit_val="+id_rit_val+"&id_rit_descr="+id_rit_descr+"&typeTab="+typeTab, "lista clienti", "width=550px,height=400px,left=300px,top=100px,resize=1,scrolling=1");
}

function openListImmobili(sid,id_rit_val,id_rit_descr,typeTab) { 
	ajaxwin=dhtmlwindow.open("ajaxbox", "ajax", "popup_immobili.php?sid="+sid+"&id_rit_val="+id_rit_val+"&id_rit_descr="+id_rit_descr+"&typeTab="+typeTab, "lista immobili", "width=550px,height=400px,left=300px,top=100px,resize=1,scrolling=1");
}

function retValuePopup(json) {
	var datiJson = eval("(" + json + ")");
	id_rit_val = datiJson.id_rit_val
	id_val     = datiJson.id_val
	id_rit_descr = datiJson.id_rit_descr
	typeTab = datiJson.typeTab
	switch (typeTab) {
		case "ANA_IMM" :			
			val_descr   = "<div><div style=\"float:left; padding-right:10px; border-right:1px solid #999;\">";
			val_descr  += datiJson.cognome+" "+datiJson.nome+"<br>tel.: "+datiJson.tel+"<br>mobile: "+datiJson.mobile+"<br>mail: <a href=\"mailto:"+datiJson.mail+"\">"+datiJson.mail+"</a><br>office: "+datiJson.office+"<br>fax: "+datiJson.fax;
			val_descr  += "</div><div style=\"float:left; padding-left:10px;\">";
			val_descr  += "<b>altro referente: </b><br />"+datiJson.cognome_ac+" "+datiJson.nome_ac+"<br>tel.: "+datiJson.tel_ac+"<br>mobile: "+datiJson.mobile_ac+"<br>mail: <a href=\"mailto:"+datiJson.mail_ac+"\">"+datiJson.mail_ac+"</a><br>office: "+datiJson.office_ac;
			val_descr  += "</div><div style=\"clear:both;\"></div></div>";
			document.getElementById(id_rit_val).value=id_val;
			document.getElementById(id_rit_descr).innerHTML=val_descr;
			break;
		case "RICH_SOLO" :
			val_descr   = datiJson.cognome + ' ' + datiJson.nome + '<br>tel.: ' + datiJson.tel + '<br>mobile: ' + datiJson.mobile + '<br>mail: <a href="mailto:' + datiJson.mail + '">'+datiJson.mail+'</a><br>office: '+ datiJson.office+'<br>fax: '+datiJson.fax; 
			document.getElementById(id_rit_val).value=id_val;
			salva_rich_cliente('form_incroci');
			break;
		case "NOTE" :
			val_descr   = datiJson.cognome + ' ' + datiJson.nome + ' &nbsp; tel.: ' + datiJson.tel + ' &nbsp; mobile: ' + datiJson.mobile + ' &nbsp; mail: <a href="mailto:' + datiJson.mail + '">'+datiJson.mail+'</a> &nbsp; office: '+ datiJson.office+' &nbsp; fax: '+datiJson.fax; 
			document.getElementById(id_rit_val).value=id_val;
			document.getElementById(id_rit_descr).innerHTML=val_descr;
			break;
		case "NOTE_IMM" :
			val_descr   = datiJson.rif + ' &nbsp; tipo.: ' + datiJson.tipo_imm + ' &nbsp; ind: ' + datiJson.ind; 
			document.getElementById(id_rit_val).value=id_val;
			document.getElementById(id_rit_descr).innerHTML=val_descr;
			break;
		default :
			alert('utilizzatore non configurato');
			break;
	}
	ajaxwin.close();
}

function mk_riep_age(strArrValue) {
	arrVal = strArrValue.split(";");
	document.getElementById('descr_agente').innerHTML= "mobile: "+arrVal[1]+"<br>mail:<a href='mailto: "+arrVal[2]+"'>"+arrVal[2]+"</a><br>zona: "+arrVal[3];	
}

function typeRdButton(objChk, idAltroChk) {
	if(objChk.checked) { document.getElementById(idAltroChk).checked = false; }
}

function ContaCaratteri()
{
    document.crea_locandina.conta.value = massimo - document.crea_locandina.txt_annuncio.value.length;
    if (document.crea_locandina.txt_annuncio.value.length > massimo)
    {
        document.crea_locandina.txt_annuncio.value = document.crea_locandina.txt_annuncio.value.substr(0, massimo);
        document.crea_locandina.conta.value = 0;
        alert("Limite Caratteri raggiunto! (" + massimo + "caratteri)");
    }

}

function M_imm(rif,id) {
	ajaxwin=dhtmlwindow.open("ajaxbox", "ajax", "modifica_immobile.php?sid="+document.getElementById('sid').value+"&id="+id, "modifica immobile - ( rif.: "+rif+")", "width=650px,height=420px,left=10px,top=10px,resize=1,scrolling=1");
}
function formcasait() {
	formcasaitwin=dhtmlwindow.open("casaitbox", "casait", "formcasait.html", "Attivazione CASA.it", "width=650px,height=420px,left=300px,top=200px,resize=1,scrolling=1");
}

function open_gmaps_age() {
	iframewin=dhtmlwindow.open("googlebox", "iframe", "add_gmap.php?from=1&sid="+document.getElementById('sid').value, "Centra nella mappa", "width=730px,height=500px,resize=1,scrolling=1,center=1", "recal");
}

function visRubr(idprop) {
	dhtmlwindow.open("rubricabox", "iframe", "container_gen.php?pageToLoad=anagrafica_clienti.php&id="+idprop+"&sid="+document.getElementById('sid').value, "...", "height=490px,width=990px,resize=1,scrolling=1,center=1", "recal");
}

function showHide(id) {
	document.getElementById(id).style.display = (document.getElementById(id).style.display == 'none')? 'block' : 'none'; 	
}

function Mk_loc_v(id,sid) {
	dhtmlwindow.open("locbox_v", "iframe", "crea_locandina_v.php?id="+id+"&sid="+sid, "Crea locandina", "width=950px,height=550px,left=10px,top=10px,resize=1,scrolling=1", "recal");
}

function Mk_loc_o(id,sid) {
	dhtmlwindow.open("locbox_o", "iframe", "crea_locandina_o.php?id="+id+"&sid="+sid, "Crea locandina", "width=950px,height=550px,left=10px,top=10px,resize=1,scrolling=1", "recal");
}

/*function Mk_prj(id,sid) {
	dhtmlwindow.open("locprj", "iframe", "prjcontent.php?id="+id+"&sid="+sid, "Proiettore", "width=980px,height=700px,left=0px,top=0px,resize=1,scrolling=1", "recal");
}*/

function Mk_Ver_Imm(id,sid) {
	dhtmlwindow.open("locbox", "iframe", "stampa_verbale_immobile.php?id="+id+"&sid="+sid, "Stampa verbale immobile", "width=950px,height=550px,left=10px,top=10px,resize=1,scrolling=1", "recal");
}

function Mk_Ver_Rich(id,id_cliente,sid) {
	dhtmlwindow.open("locbox", "iframe", "stampa_verbale_rich.php?id="+id+"&id_cliente="+id_cliente+"&sid="+sid, "Stampa verbale immobile", "width=950px,height=550px,left=10px,top=10px,resize=1,scrolling=1", "recal");
}

function slideTop(id,pxs) {
	$("#"+id).animate({marginTop: pxs}, 500 );
}

function slideMenuModImm(id) {
	pxs = (document.getElementById(id).style.marginTop == '0px')? -120 : 0; 
	pxs = (0 + pxs).toString()+"px";
	slideTop(id,pxs);
}

function change_tb_page(thePage, totRecord, typeTab) {
	switch (thePage) {
		case 'first' :
			document.getElementById('page').value = 1;
			break;
		case 'last' :
			var totPages = 0;
			totPages = (totRecord / document.getElementById('nr_rows').value);
			var intPage = parseInt(totPages,10);
			document.getElementById('page').value = (intPage < totPages)? (intPage+1) : intPage;
			break;
		default :
			document.getElementById('page').value = eval(document.getElementById('page').value + thePage);
			break;
	}
	switch (typeTab) {
		case "LI" :
			lista_immobili();
		break;
		case "MU" :
			lista_mu();
		break;		
		case "LC" :
			lista_clienti();
		break;
		case "PC" :
			lista_pop_clienti();
		break;
		case "PI" :
			lista_pop_immobili();
		break;
		case "RFI" :
			ric_filtro_imm('fn_mk_elenco_imm_incroci.php',document.getElementById('tipo').value,'');
		break;
		case "RFC" :
			ric_filtro_imm('fn_mk_elenco_clienti_incroci.php',document.getElementById('tipo').value,'');
		break;
		case "RCI" :
			ric_filtro_imm('filtra_clienti_imm.php',document.getElementById('tipo').value,'');
		break;
		case "RIC" :
			ric_filtro_imm('filtra_imm_clienti.php',document.getElementById('tipo').value,'');
		break;
		case "SOLO" :
			ric_filtro_imm('incroci_solo.php',document.getElementById('tipo').value,'');
		break;
		default :
			alert('tipo elenco non configurato!');
		break;
	}
}

function dis_ric_incroci(objChk,label,campo,type,valcmp) {
	if (valcmp == 'undefined') valcmp = 0;
	document.getElementById(label).style.color = '#000';
	if(!objChk.checked) {
		document.getElementById(campo).value = valcmp;
		document.getElementById(label).style.color = '#ccc';
	}
	document.getElementById(campo).disabled = (!objChk.checked);
	if(type == 1) filtra_imm();
	if(type == 2) filtra_cl();
}

function dis_sel2(objChk,label,sel_1,sel_2,campo,type) {
	document.getElementById(label).style.color = '#000';
	if(!objChk.checked) {
		document.getElementById(sel_1).value = 0;
		document.getElementById(sel_2).options[0].selected = true;
		document.getElementById(label).style.color = '#ccc';
	}
	document.getElementById(sel_1).disabled = (!objChk.checked);
	document.getElementById(sel_2).disabled = (!objChk.checked);
	document.getElementById(campo).disabled = (!objChk.checked);
	if(type == 1) filtra_imm();
	if(type == 2) filtra_cl();
}

function numeralsOnly(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
	if (charCode> 31 && (charCode < 48 || charCode> 57)) {
		alert("inserire solo numeri");
		return false;
	}
	return true;
}

function change_page(segno, quanto) {
	document.getElementById('oldOrderBy').value = '';
	if(segno == 'first') {
		document.getElementById('nr_page').value = 1;
		document.getElementById('nr_rows').value = 10;	
	} else {
		if(segno == 'last') {
			var nr_last_page_orig = (parseInt(document.getElementById('nr_rec_max').value) / parseInt(document.getElementById('nr_rows').value,10));
			nr_last_page = parseInt(nr_last_page_orig, 10);
			if(nr_last_page < nr_last_page_orig) nr_last_page++;
			document.getElementById('nr_page').value = nr_last_page;
			document.getElementById('nr_rows').value = 10;	
		} else {
			var nr_this_page  = parseInt(document.getElementById('nr_page').value,10);
			var valMin = (segno == '+')? (nr_this_page+1) : (nr_this_page-1);
			var valMax = 10;
			document.getElementById('nr_page').value = valMin;
			document.getElementById('nr_rows').value = valMax;
		}
	}
	document.getElementById('ric_immobile').submit();
}

function orderBy(elem) {
	document.getElementById('orderBy').value = elem;
	document.getElementById('ric_immobile').submit();
}

function goAnn_age(idAnn) {
	document.location.href = 'details_per_agenzia.php?id_annuncio='+idAnn;
}

function goAnnuncio(idAnn) {
	document.location.href = 'dettaglio_immobile.php?id_annuncio='+idAnn;
}

function goAgenzia(idAge) {
	document.location.href = 'details_ag.php?id_age='+idAge;
}

function reg_miPiace(idImm) {
	tb_show("registrazione a <i>Mi piace!</i>", "risorse/propertylist/reg_mipiace.php?height=460&width=550&id_immobile="+idImm, null);	
}

function call_mi_piace(idImm,email,salva,chiudiLight) {
	var goodMail = controllaMAIL(email);
	if(goodMail == '') {
		call_ajax_sincrono('mi_piace.php','mipiace','&email='+email+'&id_immobile='+idImm+'&cancella=0&salva='+salva);
		if(chiudiLight) tb_remove();
		return true;
	} else {
		alert(goodMail);
		return false;
	}
}

function addMiPiace(idImm,salva) {
	var email = document.getElementById('email_4_mipiace').value;
	if ((email == '') || (email == 'undefined')) {
		reg_miPiace(idImm);
	} else {
		call_mi_piace(idImm,email,salva,0)
	}
}

function delete_ann_mipiace(idDiv, mail,idImm) {
	objDiv = document.getElementById(idDiv);
	objDiv.style.border = "2px solid #c00";
	if(confirm('Cancellare l\'annuncio selezionato?')) {
		call_ajax('mi_piace.php','mipiace','&email='+mail+'&id_immobile='+idImm+'&salva=2');
	} else {
		objDiv.style.border = "2px solid #fff";
	}
}

function invia_mail_contatti() {
	var msg = '';
	if (document.getElementById('Nome').value == '') msg = 'indicare nome\n';
	if (document.getElementById('Cognome').value == '') msg += 'indicare cognome\n';
	if ((document.getElementById('Email').value == '') && (document.getElementById('txtbxPhone').value == '')) msg += 'indica la mail o il telefono\n';
	if (document.getElementById('Email').value != '') msg += controllaMAIL(document.getElementById('Email').value);
	if (!document.getElementById('Privacy').checked) msg += '\nper inviare la mail bisogna acconsentire al trattamento dei dati personali';
	
	if(msg == '') {
		InviaDati('http://www.domusmondo.com/it/risorse/propertylist/form_submit.php','content_formcontatti','contatti',true);
	} else {
		alert(msg);	
	}
}

function search_imm() {
	document.getElementById('oldOrderBy').value = "";
	document.getElementById('nr_page').value = 1;	
	document.getElementById('nr_rows').value = 10;	
	document.getElementById('ric_immobile').submit();	
}

function fullwin(par) { window.open(par,"","fullscreen,scrollbars"); }

function addComuRichCl() {
	var idProv = document.getElementById('id_provincia').value;
	if((idProv != '') && (idProv != 0)) {
		var idComu = document.getElementById('id_comune').value;
		var valQuartiere = document.getElementById('quartiere').value; 
		var valLocalita = document.getElementById('localita').value;
		var id_rich = document.getElementById('id').value;
	
		url  = "&salva=1";
		url += "&canc=0";
		url += "&launch_fn=1";
		url += "&id_rich="+id_rich;
		url += "&id_prov="+idProv;
		url += "&id_comu="+idComu;
		url += "&quartiere="+valQuartiere;
		url += "&localita="+valLocalita;

		call_ajax('mk_comu_rich_cl.php','comuni_ric',url);
	} else {
		alert('indicare almeno la provincia');
	}
}

function addTipolRichCl() {
	var id_tipologia_immobile = document.getElementById('id_tipologia_immobile').value;
	if((id_tipologia_immobile != '') && (id_tipologia_immobile != 0)) {
		var id_sotto_tipologie_immobili = document.getElementById('id_sotto_tipologie_immobili').value;
		var id_rich = document.getElementById('id').value;
	
		url  = "&salva=1";
		url += "&canc=0";
		url += "&launch_fn=1";
		url += "&id_rich="+id_rich;
		url += "&id_tipologia_immobile="+id_tipologia_immobile;
		url += "&id_sotto_tipologie_immobili="+id_sotto_tipologie_immobili;

		call_ajax('mk_tipol_rich_cl.php','tipo_imm_ric',url);
	} else {
		alert('indicare la tipologia immobile');
	}
}

function addStrComu() {
	if(document.getElementById('id_provincia').value != 0) {
		num_option=document.getElementById('id_provincia').options.length; 
		indice_selezionato = document.getElementById('id_provincia').selectedIndex;
		var idProv = document.getElementById('id_provincia').options[indice_selezionato].value;
		var dis_Prov = document.getElementById('id_provincia').options[indice_selezionato].innerHTML;
		
		if(document.getElementById('id_comune').value != 0) {
			num_option=document.getElementById('id_comune').options.length; 
			indice_selezionato = document.getElementById('id_comune').selectedIndex;
			var idComu = document.getElementById('id_comune').options[indice_selezionato].value;
			var dis_Comu = document.getElementById('id_comune').options[indice_selezionato].innerHTML;
		} else {
			var idComu = '';
			var dis_Comu = '';
		}
		
		var valQuartiere = document.getElementById('quartiere').value; 
		var valLocalita = document.getElementById('localita').value;
	
		var value_this_str_comu = idProv+";"+idComu+";"+valQuartiere+";"+valLocalita+"|";
		var dis_this_str_comu = dis_Prov+" "+dis_Comu+" "+valQuartiere+" "+valLocalita;
		
		var esiste = document.getElementById('str_comus').value.indexOf(value_this_str_comu);
		if(esiste<0) {
			document.getElementById('str_comus').value += value_this_str_comu;
			var id_div = Math.floor(Math.random()*99999+1);
			document.getElementById('comuni_ric').innerHTML += "<div id=\"c_"+id_div+"\"> &nbsp; <img src=\"../images/typ/cancel.png\" style=\"cursor:hand;cursor:pointer;\" onclick=\"delStrComuRich('"+value_this_str_comu+"','c_"+id_div+"','str_comus');\" /> &nbsp; "+dis_this_str_comu;
		}
	}
}

function addStrRich() {
	if((document.getElementById('id_cat_immobile').value != 0) && (document.getElementById('id_tipologia_immobile').value != 0)) {
		num_option=document.getElementById('id_cat_immobile').options.length; 
		indice_selezionato = document.getElementById('id_cat_immobile').selectedIndex;
		var idImm = document.getElementById('id_cat_immobile').options[indice_selezionato].value;
		
		num_option=document.getElementById('id_tipologia_immobile').options.length; 
		indice_selezionato = document.getElementById('id_tipologia_immobile').selectedIndex;
		var idTipImm = document.getElementById('id_tipologia_immobile').options[indice_selezionato].value;
		var dis_idTipImm = document.getElementById('id_tipologia_immobile').options[indice_selezionato].innerHTML;

		if(document.getElementById('id_sotto_tipologie_immobili').value != 0) {
			num_option=document.getElementById('id_sotto_tipologie_immobili').options.length; 
			indice_selezionato = document.getElementById('id_sotto_tipologie_immobili').selectedIndex;
			var idSottoImm = document.getElementById('id_sotto_tipologie_immobili').options[indice_selezionato].value;
			var dis_SottoImm = document.getElementById('id_sotto_tipologie_immobili').options[indice_selezionato].innerHTML;
		} else {
			var idSottoImm = '';
			var dis_SottoImm = '';
		}
	
		var value_this_str_imm = idImm+";"+idTipImm+";"+idSottoImm+"|";
		var dis_this_str_imm = dis_idTipImm+" "+dis_SottoImm;
		
		var esiste = document.getElementById('str_imms').value.indexOf(value_this_str_imm);
		if(esiste<0) {
			document.getElementById('str_imms').value += value_this_str_imm;
			var id_div = Math.floor(Math.random()*99999+1);
			document.getElementById('tipo_imm_ric').innerHTML += "<div id=\"i_"+id_div+"\"> &nbsp; <img src=\"../images/typ/cancel.png\" style=\"cursor:hand;cursor:pointer;\" onclick=\"delStrComuRich('"+value_this_str_imm+"','i_"+id_div+"','str_imms');\" /> &nbsp; "+dis_this_str_imm;
		}
	}
}

function delStrComuRich(str,idDiv,idHidden) {
	document.getElementById(idHidden).value = document.getElementById(idHidden).value.replace(str,"");
	document.getElementById(idDiv).style.display = 'none';
}

function canc_rich_cl(idRich) {
	call_ajax('canc_rich_cliente.php','cancRichCl','&idRich='+idRich);
}

function mod_cli(id) {
	ajaxwin=dhtmlwindow.open("ajaxbox", "ajax", "row_elenco_clienti.php?sid="+document.getElementById('sid').value+"&id="+id, "modifica rubrica", "width=500px,height=250px,left=10px,top=10px,resize=1,scrolling=1");
}

function salva_row_cl(idRow) {
	var msg = '';
	msg += ctrl_cmp_chg(document.getElementById('cognome_'+idRow),'il cognome\n');
	msg += ctrl_cmp_chg(document.getElementById('nome_'+idRow),'il nome\n');
	msg += ctrl_cmp_mail(document.getElementById('email_'+idRow),0);
	
	if(msg == '') {
		InviaDati('row_elenco_clienti.php','ret_sal_'+idRow,'mod_cli_'+idRow,true);
	} else {
		alert(msg);	
	}
	setTimeout("document.getElementById('save_result_123').style.display = 'none';", 1900);
	setTimeout("lista_clienti();", 2000);
}

function ctr_data_filtro(idCmp,tipo) {
	var cmpDt = document.getElementById(idCmp);
	var resCtr = (cmpDt.value != '')? controllo_data(cmpDt.value) : '';
	
	if( resCtr== '') { 
		if(tipo == 1) filtra_imm(); 
		if(tipo == 2) filtra_cl();
	} else {
		alert(resCtr); 
		cmpDt.value='';
	}
}

function ric_fitro_comu(objCmp,tipo) {
	if(objCmp.checked) {
		document.getElementById('str_ric_comu').value = document.getElementById('str_ric_comu').value.replace(objCmp.value+"|","");
	} else {
		document.getElementById('str_ric_comu').value += objCmp.value+"|";
	}
	if(tipo == 1) {
		filtra_imm();
	}
}

function ric_fitro_ti(objCmp,tipo) {
	if(objCmp.checked) {
		document.getElementById('str_ric_ti').value = document.getElementById('str_ric_ti').value.replace(objCmp.value+"|","");
	} else {
		document.getElementById('str_ric_ti').value += objCmp.value+"|";
	}
	if(tipo == 1) {
		filtra_imm();
	}
}

function m_over_des(obj, lang) {
	document.getElementById(obj.id).style.backgroundImage = "url(../images/flag/"+lang+"_on.png)";
}

function m_out_des(obj, lang) {
	if(document.getElementById("des_tit_"+lang).style.display == 'none') document.getElementById(obj.id).style.backgroundImage = "url(../images/flag/"+lang+"_off.png)";
}

function sel_des(lang) {
	document.getElementById('des_flag_IT').style.backgroundImage = "url(../images/flag/IT_off.png)";
	document.getElementById('des_flag_EN').style.backgroundImage = "url(../images/flag/EN_off.png)";
	document.getElementById('des_flag_FR').style.backgroundImage = "url(../images/flag/FR_off.png)";
	document.getElementById('des_flag_ES').style.backgroundImage = "url(../images/flag/ES_off.png)";
	document.getElementById('des_flag_DE').style.backgroundImage = "url(../images/flag/DE_off.png)";
	document.getElementById('des_tit_IT').style.display = "none";
	document.getElementById('des_tit_EN').style.display = "none";
	document.getElementById('des_tit_FR').style.display = "none";
	document.getElementById('des_tit_ES').style.display = "none";
	document.getElementById('des_tit_DE').style.display = "none";
	document.getElementById('des_flag_'+lang).style.backgroundImage = "url(../images/flag/"+lang+"_on.png)";
	document.getElementById('des_tit_'+lang).style.display = "block";
}

function goTo_lista_incr(tipoLista) {
	document.getElementById('launch_fn').value = 0;
	document.getElementById('tipo_list').value = tipoLista;
	document.getElementById('rows').value=10;
	document.getElementById('page').value=1;
	document.getElementById('sidx').value="";
	document.getElementById('sord').value="ASC";
	InviaDati('container_incroci.php','dmpages','form_incroci',true);
}

function change_tab_solo(div_show,div_hide) {
	if(div_show == 'tab_risultati') {
		if(document.getElementById('tipo').value == '') {
			alert('indicare il tipo di contratto');
		} else {
			document.getElementById('rows').value=10;
			document.getElementById('page').value=1;
			document.getElementById('sidx').value="";
			document.getElementById('sord').value="ASC";
			document.getElementById('LiveSearch').value="";
			InviaDati('incroci_solo.php','res_ric','form_incroci',true);
			document.getElementById('span_param').style.color='#000000';
			document.getElementById('span_param').style.fontSize='12px';
			document.getElementById('span_ris').style.color='#d71f49';
			document.getElementById('span_ris').style.fontSize='14px';
			document.getElementById(div_show).style.display='block';	
			document.getElementById(div_hide).style.display='none';
		}
	} else {
			document.getElementById('span_param').style.color='#d71f49';
			document.getElementById('span_param').style.fontSize='14px';
			document.getElementById('span_ris').style.color='#000000';
			document.getElementById('span_ris').style.fontSize='12px';
			document.getElementById(div_show).style.display='block';	
			document.getElementById(div_hide).style.display='none';
	}
}

function appendOptionLast(num,text,idSel) {
	var elOptNew = document.createElement('option');
	elOptNew.text = text;
	elOptNew.value = num;
	var elSel = document.getElementById(idSel);
	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}
}

function remSelOpt(type, idSel) {  
	var sel = document.getElementById(idSel);
	for (i = sel.options.length - 1; i>=0; i--) {
		valSel = parseInt(sel.options[i].value,10);
		if((valSel > 0) && (valSel <500001)) {
			sel.remove(i);
		}
	}
	if(type == 'aff') {
		appendOptionLast("250","250",sel.id);
		appendOptionLast("500","500",sel.id);
		appendOptionLast("750","750",sel.id);
		appendOptionLast("1000","1.000",sel.id);
		appendOptionLast("1250","1.250",sel.id);
		appendOptionLast("1500","1.500",sel.id);
	} else {
		appendOptionLast("50000","50.000",sel.id);
		appendOptionLast("100000","100.000",sel.id);
		appendOptionLast("150000","150.000",sel.id);
		appendOptionLast("200000","200.000",sel.id);
		appendOptionLast("250000","250.000",sel.id);
		appendOptionLast("300000","300.000",sel.id);
		appendOptionLast("350000","350.000",sel.id);
		appendOptionLast("400000","400.000",sel.id);
		appendOptionLast("450000","450.000",sel.id);
		appendOptionLast("500000","500.000",sel.id);	
	}
} 

function apri_chiudi() {
	if(document.getElementById('left').style.left  == '0px') {
		document.getElementById('left').style.left = '-165px';
		document.getElementById('dmpages').style.marginLeft = '32px';
		document.getElementById('icon_apri_chiudi').src = 'img/icon_foward_16x16.png';
	} else {
		document.getElementById('left').style.left = '0px';
		document.getElementById('dmpages').style.marginLeft =  '195px';
		document.getElementById('icon_apri_chiudi').src = 'img/icon_back_16x16.png';
	}
}

/****************** TIPS ********************/
function tip_this(name_class) {
    $('button.'+name_class).one('click', openMenu);
    function openMenu(e) {
        var button = $(this).addClass('active');
        var menu = $('#' + button.attr('name'));
        var offset = button.offset();
        var h = (button.outerHeight) ? button.outerHeight() : button.height();
        menu.addClass('active').css({
            // 'top': offset.top + h, 'left': offset.left
            'top': offset.top + 0, 'left': offset.left + 25
        }).click(function(e) { e.stopPropagation(); }).show(200, function() {
            $(document).one('click', {button: button, menu: menu}, closeMenu);
        });
    }
    function closeMenu(e) {
        e.data.menu.removeClass('active').hide(100, function() {
            e.data.button.removeClass('active');
        });
        e.data.button.one('click', openMenu);
    }
}
/**************** END TIPS ******************/
