function checkemail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)) {
		testresults=true
	}
	else{
		testresults=false
	}
	return (testresults)
}

function controllaFormRicercaHome() {
	if (document.getElementById('regione').value == '') {
		alert ("Inserire almeno la regione per la ricerca");
		return false;
	}
}

function controllaFormIscrizione() {
	if (document.getElementById('nome').value == '') {
		alert ("Inserire il nome");
		return false;
	}
	if (document.getElementById('cognome').value == '') {
		alert ("Inserire il cognome");
		return false;
	}

	if (document.getElementById('email').value == '') {
		alert ("Inserire l'email");
		return false;
	}
	if (document.getElementById('password').value == '') {
		alert ("Inserire la password");
		return false;
	}
	if (!checkemail(document.getElementById('email').value)) {
		alert ("Inserire un indirizzo email valido.");
		return false;
	}
/*
	obj = document.getElementById('privacy');
	if (obj != null) {
		if (!obj.checked) {
			alert ("E' necessario accettare la policy privacy del sito per proseguire.");
			return false;
		}
	}	
	*/
	return true;	
}

function submitFormAnnuncio() {
	if (controllaFormAnnuncio() ) {	
	document.forms.form.submit();
	}
}

function submitFormRegistrazAg() {
	if (controllaFormDati("S") ) {
		//document.getElementById('form_ag').submit();
		//document.forms.form_ag.submit();
		document.forms.form_ag.submit();
	}
}

function controllaFormAnnuncio() {
	stringa = "regione,provincia,comune,prezzo,titolo,descrizione,tipo,tipologia,mq";

		
	var arrayFatturazione = stringa.split(",");
	i=0;
	while (i < arrayFatturazione.length-1) {
		if (document.getElementById(arrayFatturazione[i]).value == '') {
			alert ("Inserire tutti i dati obbligatori");
			//alert(arrayFatturazione[i]);
			return false;
		}
		i++;
	}  

	var prezzo =document.getElementById('prezzo').value;
	var re = new RegExp ('[.]', 'g') ;
	prezzo = prezzo.replace(re, '');
	re = new RegExp ('[,]', 'g') ;
	prezzo = prezzo.replace(re, '.');
	

	
		if (prezzo == '' || !IsNumeric(prezzo)) {
			alert ("Inserire un prezzo valido");
			return false;
		}
		if (prezzo <= 0) {
			alert ("Inserire un prezzo maggiore di zero");
			return false;
		}
		document.getElementById('prezzo').value = prezzo;

	// prendo la lat long da google e salvarla
	if (marker) {
	
		document.getElementById('map_lat').value = marker.getLatLng().y;
		document.getElementById('map_long').value = marker.getLatLng().x;
	}
	
	// controllo le immagini caricate via ajax
	
	for(var i = 1; i <= 8; i++) {
		ajax_img = document.getElementById('ajax_image_'+i).value;
		if (ajax_img != '') {
			document.getElementById('foto_'+i).value = ajax_img;
		}
		// controllo anche la rimozione
		// AV TODOajax_remove = document.getElementById('ajax_remove_foto_'+i).value;

	}	

	for(var i = 1; i <= 1; i++) {
		ajax_img = document.getElementById('ajax_video_'+i).value;
		if (ajax_img != '') {
			document.getElementById('video_'+i).value = ajax_img;
			alert (ajax_img);
		}
	}	
	
	return true;	
}

function controllaFormDati(tipo) {
	if (tipo =='P') {
		stringa = "password_privato,password2_privato,email_privato,regione_privato,provincia_privato,comune_privato";
	} else {
		stringa = "fatt_ragione_sociale_ag,password_ag,password2_ag,telefono_ag,cellulare_ag,email_ag,regione_ag,provincia_ag,comune_ag,fatt_via_ag,fatt_civico_ag,fatt_cap_ag,email_ag";
	}
	
	var arrayFatturazione = stringa.split(",");
	i=0;
	while (i < arrayFatturazione.length-1) {
		if (document.getElementById(arrayFatturazione[i]).value == '') {
			alert ("Inserire tutti i dati obbligatori");
			//alert(arrayFatturazione[i]);
			return false;
		}
		i++;
	}  
	if (tipo =='P') {
		if (document.getElementById("password_privato").value != document.getElementById("password2_privato").value) {
			alert ("Le password non coincidono");
			return false;
		}	
		if (!checkemail(document.getElementById('email_privato').value)) {
			alert ("Inserire un indirizzo email valido.");
			return false;
		}		
	} else {
		if (!checkemail(document.getElementById('email_ag').value)) {
			alert ("Inserire un indirizzo email valido.");
			return false;
		}			
		if (document.getElementById("password_ag").value != document.getElementById("password2_ag").value) {
			alert ("Le password non coincidono");
			return false;
		}	
	}

	ajax_img = document.getElementById('ajax_image_logo').value;
	if (ajax_img != '') {
		document.getElementById('foto_logo').value = ajax_img;
	}
	return true;	
}


function controllaFormRichiesta() {
		stringa = "form_name,form_cognome,form_email,regione,provincia";
		
		var arrayFatturazione = stringa.split(",");
		i=0;
		while (i < arrayFatturazione.length-1) {
			
			if (document.getElementById(arrayFatturazione[i]).value == '' || document.getElementById(arrayFatturazione[i]).value == 0) {
				alert ("Inserire tutti i dati obbligatori");
				//alert(arrayFatturazione[i]);
				return false;
			}
			i++;
		}  


		return true;	
	}

function controllaFormValutazione() {
	stringa = "form_name,form_cognome,form_email,regione,provincia,comune,form_mq";
	
	var arrayFatturazione = stringa.split(",");
	i=0;
	while (i < arrayFatturazione.length-1) {
		
		if (document.getElementById(arrayFatturazione[i]).value == '' || document.getElementById(arrayFatturazione[i]).value == 0) {
			alert ("Inserire tutti i dati obbligatori");
			//alert(arrayFatturazione[i]);
			return false;
		}
		i++;
	}  


	return true;	
}





function requestConfirm(url, msg) {

var re = new RegExp ('[+]', 'g') ;
var mymsg = msg.replace(re, ' ') ;

  var bOk = window.confirm(mymsg);
  if (bOk) {
	window.location=url;	
  }
}



function eseguiRichiestaTipi(baseurl) {

        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // Vedi note sotto
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
		
			tipoObj = document.getElementById("tipo");
         currentTipo = tipoObj.options[tipoObj.selectedIndex].value;
         tipologiaObj = document.getElementById("tipologia");
        var destObj, url;
			url = "listatipi.php?type="+currentTipo;
			destObj = tipologiaObj;
               
        if (!http_request) {
            alert('Giving up :( Non riesco a creare una istanza XMLHTTP');
            return false;
        }
        http_request.onreadystatechange = function() { 
		   riempiTipi(http_request,destObj);
		};
        http_request.open('GET', baseurl+url, true);
        http_request.send(null);

    }
    


function riempiTipi(http_request,dest) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			   	fineHandle=1; 
	   			stringa=http_request.responseText;
               //while (dest.options.length) {
               //     dest.options[dest.options.length-1] = null;
               //}
			   dest.options.length = 0;
					//alert(stringa);			
               var arrayS = stringa.split("#");
               var arrayText = arrayS[1].split("|");
               var arrayVals = arrayS[0].split("|");               
			   
			  //dest.options.length = arrayText.length;
				    for (var i=0; i<arrayText.length; i++) {
                        dest.options[i] = new Option(arrayText[i],arrayVals[i]);
                   }	   		
	   			
            } else {
               // alert('Si è verificato un problema con la comunicazione verso il server');
            }
        }
		
    }



function eseguiRichiesta(index, tipo, baseurl) {

        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // Vedi note sotto
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
		
			regioneObj = document.getElementById("regione"+index);
			provinciaObj = document.getElementById("provincia"+index);
			comuneObj = document.getElementById("comune"+index);
         currentregion = regioneObj.options[regioneObj.selectedIndex].value;
         currentprovincia = provinciaObj.options[provinciaObj.selectedIndex].value;
        var destObj, url;
		if (tipo == "R") {
			url = "listacomuni.php?type=R&regione="+currentregion;
			destObj = provinciaObj;
		} else {
			url = "listacomuni.php?type=P&provincia="+currentprovincia;
			destObj = comuneObj;
		}
               
        if (!http_request) {
            alert('Giving up :( Non riesco a creare una istanza XMLHTTP');
            return false;
        }
        http_request.onreadystatechange = function() { 
		   alertContents(http_request,destObj);
		};
        http_request.open('GET', baseurl+url, true);
        http_request.send(null);

    }
    

function alertContents(http_request,dest) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			   	fineHandle=1; 
	   			stringa=http_request.responseText;
               //while (dest.options.length) {
               //     dest.options[dest.options.length-1] = null;
               //}
			   dest.options.length = 0;
					//alert(stringa);			
               var arrayS = stringa.split("#");
               var arrayText = arrayS[1].split("|");
               var arrayVals = arrayS[0].split("|");               
               dest[0] = new Option();
               dest[0].value = '';
               dest[0].text = "Seleziona "+dest.name;
			   
			  //dest.options.length = arrayText.length;
				    for (var i=0; i<arrayText.length; i++) {
                        dest.options[i+1] = new Option(arrayText[i],arrayVals[i]);
						//dest[i+1]=new Option();
                        //dest[i+1].value = arrayVals[i];
                        //dest[i+1].text = arrayText[i];
                       // if (currentlea == arrayVals[i])
                       //     dest[i+1].selected = true;
                   }	   		
	   			
            } else {
                //alert('Si è verificato un problema con la comunicazione verso il server');
            }
        }
		
    }

    
    
function IsNumeric(sText)
{
   var ValidChars = "0123456789,.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
	   Char = sText.charAt(i); 
	   if (ValidChars.indexOf(Char) == -1) {
	      IsNumber = false;
      }
   }
   return IsNumber;
   
}
    


function cambiaRicerca(tipo) {
	document.getElementById('tipo_cliente').value=tipo;
	//setCheckedValue(obj,tipo);
	obj= document.getElementById('form_ricerca');
	//obj.submit();
	//document.form_ricerca.submit();
	document.getElementById('hsubmit').value='1';
	document.forms.form_ricerca.submit();
}
function cambiaRicercaPrezzi(min,max) {
	document.getElementById('prezzo_min').value=min;
	document.getElementById('prezzo_max').value=max;
	obj= document.getElementById('form_ricerca');
	document.getElementById('hsubmit').value='1';
	document.forms.form_ricerca.submit();
}
function cambiaRicercaMQ(min,max) {
	document.getElementById('mq_min').value=min;
	document.getElementById('mq_max').value=max;
	obj= document.getElementById('form_ricerca');
	document.getElementById('hsubmit').value='1';
	document.forms.form_ricerca.submit();
}


//set the radio button with the given value as being checked
//do nothing if there are no radio buttons
//if the given value does not exist, all the radio buttons
//are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function selezionaZona(valore) {
	var mylocal = valore;
	var selectBox = document.getElementById('zona');
	if (selectBox.type == 'select-one') {
		for (var i = 0; i < selectBox.options.length; i++) {
			
			if (selectBox.options[i].text == mylocal) {
				selectBox.options[i].selected = true;
			}
		}
	}
    
}




function eseguiRichiestaZone(baseurl,soloTavole, valore) {
//alert("richiesta zone");
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // Vedi note sotto
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
		
			comuneObj = document.getElementById("comune");
         currentComune = comuneObj.options[comuneObj.selectedIndex].value;
        var destObj, url;
			url = "edit_annuncio_zone.html.php?id="+currentComune;
             if (soloTavole) 
     			url += "&ricerca=1";

            	 if (!http_request) {
            alert('Giving up :( Non riesco a creare una istanza XMLHTTP');
            return false;
        }
        http_request.onreadystatechange = function() { 
		   riempiZone(http_request,valore);
		};
        http_request.open('GET', baseurl+url, true);
        http_request.send(null);

    }
    


function riempiZone(http_request,valore) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			   	fineHandle=1; 
	   			stringa=http_request.responseText;
	   			destObj = document.getElementById("zona_ajax");
	   			destObj.innerHTML=stringa;
	   			selezionaZona(valore);
            } else {
                //alert('Si è verificato un problema con la comunicazione verso il server');
            }
        }
		
    }




