// JavaScript Document
// controlla email
function controllaEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);var res=1;if (str.indexOf(at)==-1){
		res=0;
	}
	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		res=0;
	}
	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		res=0;
	}
	else if (str.indexOf(at,(lat+1))!=-1){
		res=0;
	}
	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		res=0;
	}
	else if (str.indexOf(dot,(lat+2))==-1){
		res=0;
	}
	else if (str.indexOf(" ")!=-1){
		res=0;
	}
	if(res==1) {
		return true;
	}
	else {
		return false;
	}					
}


function quoteReplace(fields) {
	var elenco=fields.split(",");
	var tmp;
	for(i in elenco) {
		var tmp=document.getElementById(elenco[i]).value;
		document.getElementById(elenco[i]).value=tmp.replace(/\'/g, "&#39;");
		document.getElementById(elenco[i]).value=tmp.replace(/\"/g, "&#34;");
		/*
		document.getElementById(elenco[i]).value=tmp.replace(/à/g, "&agrave;");
		document.getElementById(elenco[i]).value=tmp.replace(/è/g, "&egrave;");
		document.getElementById(elenco[i]).value=tmp.replace(/ì/g, "&igrave;");
		document.getElementById(elenco[i]).value=tmp.replace(/ò/g, "&ograve;");
		document.getElementById(elenco[i]).value=tmp.replace(/ù/g, "&ugrave;");
		*/
	}
}

function contenuto(quale) {
  var nome = 'contenutotab';
  var indice = 1;
  var giraancora = true;
  while (giraancora && indice<10) {
    if(document.getElementById('contenutotab'+indice).style.display=='') {
    	document.getElementById('contenutotab'+indice).style.display='none';
    	document.getElementById('tab'+indice).style.backgroundColor='black';
      giraancora = false;
    }
    indice++;
  }
	new Effect.Appear(document.getElementById('contenutotab'+quale));
	//document.getElementById('contenutotab'+quale).style.display='';
	document.getElementById('tab'+quale).style.backgroundColor='#584c39';
}

function cambiaColore(colore) {
  document.getElementById('areastrisce').style.display='none';
  document.getElementById('areastrisce').innerHTML='';
  new Ajax.Updater(
    'areastrisce',
    'materiali_elenco.php',
    {
      method: 'get',
      parameters: {colore: colore}
    }
  );
  new Effect.Appear('areastrisce');
}

function offertaDettaglio(quale) {
	$('#offertadettaglio'+quale).slideToggle();
	/*
  var stato = document.getElementById('offertadettaglio'+quale).style.display;
  if(stato=="") new Effect.SlideUp('offertadettaglio'+quale);
  else new Effect.SlideDown('offertadettaglio'+quale);
  */
}

function showAllDettagli(booleano,quanti) {
  var stato = "";
  if(booleano=="no") stato = "none";
  for(i=1;i<=quanti;i++) {
    document.getElementById('offertadettaglio'+i).style.display=stato;
  }
}

function coloraCampo(campo) {
  campo.style.backgroundColor='#ffdd44';
  return true;
}

function checkContatti() {
  var rag_soc = document.getElementById('rag_soc');
  var email = document.getElementById('email');
  var telefono = document.getElementById('telefono');
  var libero = document.getElementById('libero');
  var contmessaggio = document.getElementById('contmessaggio');

  if(rag_soc.value.length==0) {
    coloraCampo(rag_soc);
    rag_soc.focus();
    document.getElementById('contmessaggio1').style.display='';
    return false;  
  }
  else if(email.value.length==0 && telefono.value.length==0) {
    coloraCampo(email);
    coloraCampo(telefono);
    email.focus();
    document.getElementById('contmessaggio2').style.display='';
    return false;
  }
  else if(email.value.length>0 && controllaEmail(email.value) == false ) {
    coloraCampo(email);
    email.focus();
    document.getElementById('contmessaggio4').style.display='';
    return false;
  }
  else if(libero.value.length==0) {
    coloraCampo(libero);
    libero.focus();
    document.getElementById('contmessaggio3').style.display='';
    return false;
  }
  else {
    return true;
  }
}

function removeColor() {
  document.getElementById('rag_soc').style.backgroundColor='';
  document.getElementById('email').style.backgroundColor='';
  document.getElementById('telefono').style.backgroundColor='';
  document.getElementById('libero').style.backgroundColor='';
  document.getElementById('contmessaggio1').style.display='none';
  document.getElementById('contmessaggio2').style.display='none';
  document.getElementById('contmessaggio3').style.display='none';
  document.getElementById('contmessaggio4').style.display='none';
  return true();
}

function colonialFoto(link, dida) {
	document.getElementById('colonialsingelefoto').src=link;
	document.getElementById('galleria_didascalia').innerHTML=dida;
	return false;
}

function galleryFoto(link, dida) {
	document.getElementById('galleria_foto').src='imageresample.php?src='+link+'&typ=b&siz=350,350&pars=';
	document.getElementById('galleria_didascalia').innerHTML=dida;
	return false;
}

function cavafreccia (id) {
	id = id + '';	
	if(id.length<2) id = '0' + id;
	document.getElementById('freccia'+id).src='img/blank.gif';
}

function mettifreccia (id) {
	id = id + '';	
	if(id.length<2) id = '0' + id;
	document.getElementById('freccia'+id).src='img/freccia.jpg';
}
