function altezzafinestra() {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function larghezzafinestra() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function mostrabox(larghezza,altezza,data) {
	
	var larghezzapx = larghezza + 'px';
	var altezzapx = altezza + 'px';
	var altopx =  $(window).scrollTop() + altezzafinestra()/2 - altezza/2 + 'px';
	var sinistrapx = $(window).scrollLeft() + larghezzafinestra() / 2 - larghezza /2 + 'px';
	
	
	$('#popup').html(data);
	$('#popup').css({width: larghezzapx, height: altezzapx, left: sinistrapx, top: altopx});
	 $('#popup').fadeIn(1000);
	$('#coprente').fadeIn(200);
}

function nascondipopup2(nome)
{
nome.fadeOut(1000);
$("#coprente").hide(1000);
}

function nascondipopup3()
{
$("#popup").hide(3000);
$("#coprente").hide(1000);
}

// chiude il popup clickando 2 volte fuori da esso
$(document).bind("dblclick", function (f) 
{
 if ( $(f.target).closest("#popup").attr('id') != 'popup' ) { 
 $("#popup").fadeOut(1000);
 $("#coprente").fadeOut(500);}
})

function seleziona(field)
	{field.focus(); field.blur(); field.select(); }

// chiude il suggerisci modello clickando fuori dai suggerimenti

$(document).bind("click", function (f) 
{
 if ( $(f.target).closest("#modellomostra").attr('id') != 'modellomostra' ) { $("#modellomostra").fadeOut(1000);}
})

function valid(moduloid)
	{
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}\s*$/i;
		modulo = document.getElementById(moduloid);
	if(modulo.nome.value.length < 2){alert("ERRORE:\nNome non valido"); seleziona(modulo.nome); return false}
if (modulo.email.value.search(validRegExp) == -1){alert ("ATTENZIONE\nNon hai scritto un indirizzo email valido"); return false;}
	if(modulo.telefono.value.length < 7){alert("ERRORE:\nNome hai scritto correttamente il numero di telefono"); seleziona(modulo.telefono); return false}
	if(modulo.messaggio.value.length <  50){alert("ERRORE:\nNon hai scritto con sufficente dettaglio quali sono le tue richieste. Devi usare almeno 50 caratteri per descrive con pił precisione che cosa desideri sapere."); seleziona(modulo.messaggio); return false}	
	return true;
	}


//funzione per preparare i dati da inviare con POST		
function preparadati(formid){
  stringa = "";
  var form = document.getElementById(formid);
  var numeroElementi = form.elements.length;
 
  for(var i = 0; i < numeroElementi; i++){
    if(i < numeroElementi-1){
		
		if( (form.elements[i].name == 'nome') || (form.elements[i].name == 'email') || (form.elements[i].name == 'telefono') )
			{
				setCookie(form.elements[i].name, form.elements[i].value, 90);	
			}
		
		
		
		if( ( (form.elements[i].type != 'radio') && (form.elements[i].type != 'checkbox') ) || form.elements[i].checked )
		{
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
		}
		
    }else{
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
    } 
  }
}

// imposta il cookie sNome = sValore
// per la durata di iGiorni
function setCookie(sNome, sValore, iGiorni) {
	
  var dtOggi = new Date()
  var dtExpires = new Date()
  dtExpires.setTime
    (dtOggi.getTime() + 24 * iGiorni * 3600000)
  document.cookie = sNome + "=" + escape(sValore) +
    "; expires=" + dtExpires.toGMTString() + 
	"; path = /" +
	"; domain = .scegliauto.com";}

// restituisce il valore del cookie sNome
function getCookie(sNome) {
	
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
	  
    // leggo singolo cookie "Nome = Valore"
    var asCookie = asCookies[iCnt].split("=");
	
    if (sNome == asCookie[0]) { 
      return (unescape(asCookie[1]));
    }
  }

  // SE non esiste il cookie richiesto
  return("");
}

function settacookie()
{
	
nomeval = document.getElementById('nome').value;
emailval = document.getElementById('email').value;
telefonoval = document.getElementById('telefono').value;
setCookie('nome', nomeval, 90);
setCookie('email', emailval, 90);
setCookie('telefono', telefonoval, 90);
}

function popola()
{
	 var nomex = getCookie('nome');
	document.getElementById('nome').value = getCookie('nome');
	document.getElementById('email').value = getCookie('email');
	document.getElementById('telefono').value = getCookie('telefono');
	
}

	
