//

// Funzione di popup

//





var popup=null;



function viewImage(image, width, height, text) {



if (popup && popup.open) popup.close();



var msg = "Click to Close";

var poswx=(screen.width > width ? (screen.width - width)/2 : 0);

var poswy=(screen.height > height ? (screen.height - height)/3 : 0);



if (poswx==0 && poswy==0) {

   popup=window.open("","popup","width=" + (screen.width-10) + ",height=" + (screen.height-55) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=yes,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

} else if (poswx==0) {

   popup=window.open("","popup","width=" + (screen.width-10) + ",height=" + (height+1) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=yes,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

} else if (poswy==0) {

   popup=window.open("","popup","width=" + (width+1) + ",height=" + (screen.height-55) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=yes,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

} else {

   popup=window.open("","popup","width=" + (width+1) + ",height=" + (height+1) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=no,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

}



popup.document.write("<html><head><title>" + text + "</title>\n<meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'>\n");

//popup.document.write("<META http-equiv='expires' content='-1'>\n");

//popup.document.write("<META http-equiv='pragma' content='no-cache'>\n");

//popup.document.write("<META http-equiv='cache-control' content='no-cache'>\n");

popup.document.write("<body marginwidth='0' marginheight='0' topmargin='0' leftmargin='0'>\n");

popup.document.write("<img src='" + image + "' width='" + width + "' height='" + height + "' border='0' onclick='window.close()' title='" + msg + "'>\n");

popup.document.write("</body></html>");

popup.document.close();

popup.focus();

}



///////popup
 var stile = "top=10, left=10, width=600, height=500, status=no, menubar=no, toolbar=no, scrollbars=yes";
 function Popup(apri) {
	window.open(apri, "", stile);
 }
/////////////////


function xxviewMaps(gmaps_lat, gmaps_lon) {



if (popup && popup.open) popup.close();



var width = 720;

var height = 540;

var text = "Google Maps";



var msg = "Click to Close";

var poswx=(screen.width > width ? (screen.width - width)/2 : 0);

var poswy=(screen.height > height ? (screen.height - height)/3 : 0);



if (poswx==0 && poswy==0) {

   popup=window.open("","popup","width=" + (screen.width-10) + ",height=" + (screen.height-55) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=yes,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

} else if (poswx==0) {

   popup=window.open("","popup","width=" + (screen.width-10) + ",height=" + (height+1) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=yes,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

} else if (poswy==0) {

   popup=window.open("","popup","width=" + (width+1) + ",height=" + (screen.height-55) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=yes,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

} else {

   popup=window.open("","popup","width=" + (width+1) + ",height=" + (height+1) + ",status=no,hotkeys=no,menubar=no,toolbar=no,resizable=no,scrollbars=no,top=" + poswy + ",left=" + poswx + ",dependent=yes,alwaysRaised=yes");

}



popup.document.write("<html><head><title>" + text + "</title>\n<meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'>\n");

//popup.document.write("<META http-equiv='expires' content='-1'>\n");

//popup.document.write("<META http-equiv='pragma' content='no-cache'>\n");

//popup.document.write("<META http-equiv='cache-control' content='no-cache'>\n");

popup.document.write("<body marginwidth='0' marginheight='0' topmargin='0' leftmargin='0'>\n");

//popup.document.write("<img src='" + image + "' width='" + width + "' height='" + height + "' border='0' onclick='window.close()' title='" + msg + "'>\n");

popup.document.write(gmaps_lat + " - " + gmaps_lon);

popup.document.write("</body></html>");

popup.document.close();

popup.focus();

}







function viewMaps(catalog_id){



    if (popup && popup.open) popup.close();



	var w = 760;

	var h = 500;

	var l = Math.round((screen.width / 2) - (w / 2));

	var t = Math.round((screen.height / 2) - (h / 2));

	popup = window.open("gmaps.php?id="+ catalog_id ,"popup","toolbar=0,directories=0,menubar=0,resizable=0,width="+ w +",height="+ h +",left="+ l +",top="+ t);

	popup.focus();

}





//

// Funzioni Ajax per la gestione dinamica della select province

//



var myRequest = null;
var priceRequest = null;
var locaRequest = null;




// Oggetto AJAX che innesca chiamata server

function CreateXmlHttpReq(handler) {

  var xmlhttp = null;

  try {

    xmlhttp = new XMLHttpRequest();

  } catch(e) {

    try {

        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch(e) {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    }

  }

  xmlhttp.onreadystatechange = handler;
  return xmlhttp;

}



// Legge Province della regione selezionata, dal programma php lato server che interroga il db

function getProvince(myRegion) {

  myRequest = CreateXmlHttpReq(makeProvince);

  myRequest.open("GET","catalog.php?action=prov&id="+myRegion+"&rand="+Math.random());

  myRequest.send(null);
  
 

}



// Crea combo Province

function makeProvince()

{

  // se la risposta del server Ã¨ OK, carico array

  if (myRequest.readyState == 4 && myRequest.status == 200) {

     var retProv = myRequest.responseText.split(",");

  }



  // oggetto combo province

  objSelect = document.formcat.provincia;



  // azzera combo

  for (i = objSelect.length; i > 0; i--) {

      objSelect.options[i-1] = null;

  }



  // carica valori nel combo

  for (i = 0; i < retProv.length; i+=2) {

      if (retProv[i] != "") {

         objSelect.options[i/2] = new Option(retProv[i], retProv[i+1]);

      }

  }



  // seleziona indice iniziale combo

  objSelect.selectedIndex = 0; 
  
  
  //resetLoca();
  resetLoca();

}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Legge Località della provincia selezionata, dal programma php lato server che interroga il db

function getLoca(myProvince) {

  locaRequest = CreateXmlHttpReq(makeLoca);

var Cat = document.formcat.cat_id;
		
for (i=0;i<Cat.length;i++) {
	if (Cat[i].checked) {
		checked_cat = Cat[i].value;
	}
}
  locaRequest.open("GET","catalog.php?action=loc&c_id="+checked_cat+"&id="+myProvince+"&rand="+Math.random());

  locaRequest.send(null);

}



// Crea combo Localita

function makeLoca()

{

  // se la risposta del server è OK, carico array

  if (locaRequest.readyState == 4 && locaRequest.status == 200) {

     var retLoc = locaRequest.responseText.split(",");

  }



  // oggetto combo localita

  objSelect = document.formcat.localita;



  // azzera combo

  for (i = objSelect.length; i > 0; i--) {

      objSelect.options[i-1] = null;

  }



  // carica valori nel combo

  for (i = 0; i < retLoc.length; i+=2) {

      if (retLoc[i] != "") {

         objSelect.options[i/2] = new Option(retLoc[i], retLoc[i+1]);

      }

  }
  
  



  // seleziona indice iniziale combo

  objSelect.selectedIndex = 0;

}




// Legge prezzo in base alla categoria, dal programma php lato server che interroga il db

function getPrice() {

  priceRequest = CreateXmlHttpReq(makePrice);
	
var Cat = document.formcat.cat_id;
		
for (i=0;i<Cat.length;i++) {
	if (Cat[i].checked) {
		checked_cat = Cat[i].value;
	}
}
  priceRequest.open("GET","catalog.php?action=price&c_id="+checked_cat+"rand="+Math.random());
  priceRequest.send(null);

}



// Crea combo Province

function makePrice()

{

  // se la risposta del server è OK, carico array

  if (priceRequest.readyState == 4 && priceRequest.status == 200) {
	 
     var retPrice = priceRequest.responseText.split(",");

  }



  // oggetto combo localita

  objSelect = document.formcat.price;



  // azzera combo

  for (i = objSelect.length; i > 0; i--) {

      objSelect.options[i-1] = null;

  }



  // carica valori nel combo

  for (i = 0; i < retPrice.length; i+=2) {

      if (retPrice[i] != "") {

         objSelect.options[i/2] = new Option(retPrice[i+1], retPrice[i]);

      }

  }
 

  // seleziona indice iniziale combo

  objSelect.selectedIndex = 0;

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// Seleziona combo regione dalla cartina

function getRegion(regione)

{



// oggetto select regione

objSelect = document.formcat.regione;



// loop sulle select, quando trova la regione imposta index

for (var i=0; i<objSelect.length; i++){

  if (objSelect[i].value == regione ){

     objSelect.selectedIndex = i;

	 getProvince(regione);
	 
	 

  }

}



}


////////// funzioni di reset

function categoryReset()
{
	var obj = document.formcat.provincia[document.formcat.provincia.selectedIndex].value;
	

	
	getLoca(obj);
	getPrice();
	
	
	
}

function resetLoca()
{
	
	getLoca('',0);
	
}


/////validazione FORM richiesta


function isEmpty(field){
	
	if (field.value==null||field.value=="")
  	{return true;} else 
	{return false;}
	
}

function isValidEmail(strEmail){
	
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
	  setTimeout( function() {$('emailInput').clear();$('emailInput').focus();}, 10 );
      alert('Inserisci un indirizzo email valido');
      return false;
    } 
	
	$('verifyEmailResult').setStyle({
		  width:'150px',
		  height:'20px',
		  visibility:'inherit'
		});
	
	$('verifyEmailResult').update('<img src="images/icons/v.gif" />');
	
    return true; 
	
}


function validateJoin(form){
	
		if(!form.accetto[0].checked){alert("il consenso al trattamento dei dati personali é obbligatorio.");
		return false; } 
		
	
		if(isEmpty(form.nominativo)){
			
			alert('scrivi il tuo nome'); 
			form.nominativo.focus(); return false;
			
		} 	
		
		
		
		if(!isValidEmail(form.email.value)){
			
			return false;
		}
		

}



