/*utiliy*/
function Trim(strq)
{
	while(strq.indexOf(" ")==0)
	{
		if(strq.length > 0)
			strq = strq.replace(" ", "");
		else
			break;
	}
	
	while(strq.lastIndexOf(" ")==strq.length-1)
	{
		if(strq.length > 0)
			strq = strq.substring(0, strq.length-1);
		else
			break;
	}
		
	return strq;
}
/*utiliy FINE*/


$(document).ready(function(){

  
  $(".cat-container").each(function(){
    $(this).find("a.fotoProd").lightBox();
  });
  
  $(".home.content-sub .home-container a").click(function(e){
      e.preventDefault()
  })
  $(".home.content-sub .home-container").click(function(e){
    e.preventDefault()
    window.location.href=$(this).find("a:first").attr("href");
  });
  
  $(".catalogo.content-sub .cat-container a").click(function(e){
      e.preventDefault()
  })
  $(".catalogo.content-sub .cat-container").click(function(e){
    e.preventDefault()
    window.location.href=$(this).find("a:first").attr("href");
  });
  
  $('#gallery-home').cycle('fade');
  $('#message-box').cycle('scrollDown');
  
  $("input[class='area_ris']").focus(function(){
    if(Trim(String($(this).attr("value"))) == "Utente" && String($(this).attr("name"))== "nome"){$(this).attr("value", "")}
    if(Trim(String($(this).attr("value"))) == "$#Password$#" && String($(this).attr("name"))== "password"){$(this).attr("value", "");}
    $(this).css("color", "#505050")
  });
  
  $("input[class='area_ris']").blur(function(){
    if(String($(this).attr("name"))== "nome" && Trim(String($(this).attr("value"))) == ""){$(this).attr("value", "Utente");$(this).css("color", "#A0A0A0")}
    if(String($(this).attr("name"))== "password" && Trim(String($(this).attr("value"))) == ""){$(this).attr("value", "$#Password$#");$(this).css("color", "#A0A0A0");}
  });
  

  /*FORM ordine*/
  
  var objAtt = document.getElementById("ordine_altro")
  if(objAtt){
    if(objAtt.checked){
      $("#ordine_bar_div").hide();
      $("#ordine_tabaccheria_div").hide();
      $("#campo_ordine_altro").show();
      
      document.getElementById("ordine_bar").checked = false
      document.getElementById("ordine_tabaccheria").checked = false
    }
  }
  
  var myBoxArrL = $("input[class='add_ordine']")
  var myTotl = 0
  for(i=0;i<myBoxArrL.length;i++){
    if(!isNaN(parseInt(myBoxArrL[i].value))){
    myTotl = myTotl + (parseInt(myBoxArrL[i].value) * $("#"+myBoxArrL[i].id).attr("rel"))
    }
  }
  
  myTotl.toFixed(2);
  
  if(myTotl<495.50){
    $("#spese_spedizione").html("€ 12,00")
    $("#spedizione_hid").attr("value", "€ 12,00")
  }else{
    $("#spese_spedizione").html("€ 0,00")
    $("#spedizione_hid").attr("value", "€ 0,00")
  }
  
  
  if(myTotl!=0){
    $("#totale_ordine").html("€ " + String(myTotl.toFixed(2)).replace(".", ","));
    $("#tatale_ordine_hid").attr("value", "€ " + String(myTotl.toFixed(2)).replace(".", ","));
  }else{
    $("#totale_ordine").html("€ 0,00")
    $("#tatale_ordine_hid").attr("value", "€ 0,00")
  }
  
  
  $("#azzera_ordine").click(function(){
    $("input[type='text']").attr("value", "")
    $("#totale_ordine").html("€ 0,00")
    $("#spese_spedizione").html("€ 12,00")
    return false;
  });
  
  $("input[class='add_ordine']").blur(function(){
  
    if(isNaN(parseInt($(this).attr("value")))  && Trim(String($(this).attr("value"))).length > 0){
      alert("la quantita deve essere un numero");
      return false;
    }
    
    if(String($(this).attr("value")).indexOf(".") != -1 || String($(this).attr("value")).indexOf(",") != -1){
      alert("la quantità deve essere un numero intero");
      return false;
    }
    
    var myBoxArr = $("input[class='add_ordine']")
    var myTot = 0
    for(i=0;i<myBoxArr.length;i++){
      if(!isNaN(parseInt(myBoxArr[i].value))){
      myTot = myTot + (parseInt(myBoxArr[i].value) * $("#"+myBoxArr[i].id).attr("rel"))
      }
    }
    
    if(myTot<495.50){
      $("#spese_spedizione").html("€ 12,00") 
      $("#spedizione_hid").attr("value", "€ 12,00")      
    }else{
      $("#spese_spedizione").html("€ 0,00")
      $("#spedizione_hid").attr("value", "€ 0,00")
    }
    
    if(myTot!=0){
      $("#totale_ordine").html("€ " + String(myTot.toFixed(2)).replace(".", ","));
      $("#tatale_ordine_hid").attr("value", "€ " + String(myTot.toFixed(2)).replace(".", ","));
    }else{
      $("#totale_ordine").html("€ 0,00")
      $("#tatale_ordine_hid").attr("value", "€ 0,00")
    }
  });
  
  $("#ordine_altro").click(function(){
    var obj = document.getElementById("ordine_altro")
    if(obj.checked){
      $("#ordine_bar_div").hide();
      $("#ordine_tabaccheria_div").hide();
      $("#campo_ordine_altro").show();
      
      document.getElementById("ordine_bar").checked = false
      document.getElementById("ordine_tabaccheria").checked = false
    }else{
      $("#campo_ordine_altro").hide();
      $("#campo_ordine_altro").attr("value", "");
      $("#ordine_bar_div").show();
      $("#ordine_tabaccheria_div").show();
    }
  });
  
  $("#ordine_bar").click(function(){
    var obj = document.getElementById("ordine_bar")
    if(obj.checked){
      document.getElementById("ordine_tabaccheria").checked = false
    }
  });
  
  $("#ordine_tabaccheria").click(function(){
    var obj = document.getElementById("ordine_tabaccheria")
    if(obj.checked){
      document.getElementById("ordine_bar").checked = false
    }
  });
  
  $("#btn_conferma_ordine").click(function(){
    if(checkform(document.getElementById("formOrdine"))){
      document.getElementById("formOrdine").submit();
    }
    return false;
  });
  /*FORM ordine Fine*/
  
  
  $(document).scroll(function(){
    if(globalVideo){
      var vdiv = $("#videoDiv")
      vdiv.css("top", ($(window).height()-vdiv.height())/2+$(document).scrollTop() )
    }
  })
  
  $("#videoOverlay, #chiudiVideo").click(function(){
    closeVideo()
    return false;
  })
  $("#openVideo").click(function(){ openVideo(); return false;})
});


/*video callToAction*/

var globalVideo = false;
function openVideo(){
  globalVideo = true;
  var voverlay = $("#videoOverlay")
  var vdiv = $("#videoDiv")
  
  voverlay.css("opacity", 0.4)
  voverlay.width($(document).width())
  voverlay.height($(document).height())
  
  vdiv.css("left", ($(window).width()-vdiv.width())/2 )
  vdiv.css("top", ($(window).height()-vdiv.height())/2+$(document).scrollTop() )
  
  $('#videoDivContent').flash({
      src: 'http://www.voila-espositori.it/spot/progetto_definitivo_per_web.swf',
      width: 1024,
      height: 768
  });
  
  voverlay.fadeIn("fast")
  vdiv.fadeIn("fast")
  
}

function closeVideo(){
  globalVideo = false;
  var voverlay = $("#videoOverlay")
  var vdiv = $("#videoDiv")
  voverlay.fadeOut("fast")
  vdiv.fadeOut("fast")
  
  $('#videoDivContent').html("")
}
  
/*video callToAction FINE*/


var arrRequested = new Array();
arrRequested[0] = "ragione_sociale";
arrRequested[1] = "telefono";
arrRequested[2] = "via";
arrRequested[3] = "cap";
arrRequested[4] = "indirizzo";
arrRequested[5] = "localita";
arrRequested[6] = "provincia";
arrRequested[7] = "codice_fiscale";
arrRequested[8] = "partita_iva";
arrRequested[9] = "persona_riferimento";

function checkform(form){
	var errore = 0
	var myerr = ""
	var boolReq;
	for(var i=0; i<form.elements.length; i++){
		boolReq = false;
		for(var j = 0; j < arrRequested.length; j++)
		{
			if(form.elements[i].name == arrRequested[j])
			boolReq = true;
		}
		
		if((form.elements[i].type!="submit" && form.elements[i].value=="" && boolReq))
		{
			alert("Attenzione! Non risulta compilato un campo richiesto");
			form.elements[i].focus();
			return false;
		}
		
		if(form.elements[i].type!="submit" && form.elements[i].name=="accetto"&& !form.elements[i].checked)
		{
			alert("Non si è data l'autorizzazione al trattamento dei dati personali");
			return false;
		}
		
		
    if(String(form.elements[i].name) == "telefono" && isNaN(Trim(String(form.elements[i].value))))
		{
      alert("Attenzione. Il campo telefono deve essere numerico");
      form.elements[i].focus();
      return false;
		}
    
    if(String(form.elements[i].name) == "cap" && isNaN(Trim(String(form.elements[i].value))))
		{
      alert("Attenzione. Il CAP deve essere numerico");
      form.elements[i].focus();
      return false;
		}
    
    if(String(form.elements[i].name) == "cap" && Trim(String(form.elements[i].value)).length != 5 )
		{
      alert("Attenzione. La lunghezza del CAP non è corretta");
      form.elements[i].focus();
      return false;
		}
    
    if(String(form.elements[i].name) == "codice_fiscale" && Trim(String(form.elements[i].value)).length != 16 )
		{
      alert("Attenzione. La lunghezza del codice fiscale non è corretta");
      form.elements[i].focus();
      return false;
		}
    
    
    if(String(form.elements[i].name) == "partita_iva" && isNaN(Trim(String(form.elements[i].value))))
		{
      alert("Attenzione. Il campo partita iva deve essere numerico");
      form.elements[i].focus();
      return false;
		}
    
    if(String(form.elements[i].name) == "partita_iva" && Trim(String(form.elements[i].value)).length != 11 )
		{
      alert("Attenzione. La lunghezza della partita iva non è corretta");
      form.elements[i].focus();
      return false;
		}
    
    if(String(form.elements[i].name) == "email" && String(form.elements[i].value).length > 0)
		{
			if(String(form.elements[i].value).indexOf("@") == -1)
			{
				alert("Attenzione indirizzo email non valido. Carattere @ richiesto");
				return false;
			}
			if(String(form.elements[i].value).indexOf(".") == -1)
			{
				alert("Attenzione indirizzo email non valido.");
				return false;
			}
		}
		
		if(form.elements[i].name.indexOf("data") == 0 && String(form.elements[i].value).length > 0)
		{
			if(!checkData(String(form.elements[i].value)))
				return false;
		}
	}
  
  
  if(document.getElementById("ordine_altro").checked == false && document.getElementById("ordine_bar").checked == false && document.getElementById("ordine_tabaccheria").checked == false)
  {
    alert("nessuna tipologia di attività selezionata")
    return false;
  }
  
  if(document.getElementById("ordine_altro").checked == true && Trim(String(document.getElementById("campo_ordine_altro").value)).length == 0)
  {
    alert("specificare la tipologia di attività");
    document.getElementById("campo_ordine_altro").focus();
    return false;
  }
  
  var myBoxArr = $("input[class='add_ordine']")
  var myTotCont = 0
  for(i=0;i<myBoxArr.length;i++){
    if(!isNaN(parseInt(myBoxArr[i].value))){
    myTotCont ++
    }else{
    myBoxArr[i].value = ""
    }
  } 
  
  if(myTotCont == 0){
    alert("specificare la quantità di almeno un tipo di prodotto per confermare l'ordine");
    return false;
  }
  
  
  
  var pagArr = $("input[name='pagamenti']")
  var pagCont = 0
  for(i=0;i<pagArr.length;i++){
    if(pagArr[i].checked)
      pagCont++;
  }
  if(pagCont==0){
    alert("specificare almeno un tipo di pagameto")
    return false;
  }
  
  
	return true
}

function apriSpot(){
  openVideo()
  //window.open("http://www.voila-espositori.it/spot/","spotWindow","width=1024,height=768,toolbar=no,location=no,directories=no,scrollbars=no,menubar=no,status=no,resizable=no")
}

function apriSpot2(){
  openVideo()
  //window.open("http://www.voila-espositori.it/spot/","spotWindow","width=1024,height=768,toolbar=no,location=no,directories=no,scrollbars=no,menubar=no,status=no,resizable=no")
}
