// JavaScript Document

function findPos(obj)
{
  var curleft = curtop = 0;
  if (obj.offsetParent)
  {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent)
    {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}

function decodifica_data(str)
{
	var giorno = str.substring(6,8)
	var mese = str.substring(4,6);
	var anno = str.substring(0,4);
	
	return giorno + "/" + mese + "/" + anno;
}

function ShowHide(checkboxName, id) {
//document.writeln(document.getElementById(checkboxName).checked == true);
//document.writeln(id);
//document.writeln(document.getElementsByTagName(checkboxName).checked == true)
	if (document.getElementById(checkboxName).checked == true) {
		document.getElementById(id).style.display = "block"
	} else {
		document.getElementById(id).style.display = "none"
	}
}

function changePhoto(sito, pathfoto, luogo, data, img_index)
{	
	document.getElementById("layer_foto_tdTesto").innerHTML = luogo.replace("|", "'") + " - " + decodifica_data(data)
	//document.getElementById("layer_foto_tdFoto").innerHTML = "<img id='foto' src='" + sito + pathfoto + "' class='foto'>";
	document.getElementById("layer_foto_tdFoto").innerHTML = "<img id='foto' src='" + pathfoto + "' class='foto'>";

  //alert(document.getElementById('img_index').value + ' + 1 % ' + img_path.length + ' = ' + (parseInt(document.getElementById('img_index').value) + 1));

  //document.getElementById('img_index').value = (parseInt(document.getElementById('img_index').value) + 1) % img_path.length;
  //document.getElementById('img_index').value = img_index;
  setImageIndexValue(sito, img_path, pathfoto);
}

function changeSnapshots(sito, pathfoto_next, pathfoto_prev)
{	
  //alert(pathfoto_prev + ' - ' + pathfoto_next);

	//document.getElementById("prevPhotoSnap").src = sito + pathfoto_prev;
	//document.getElementById("nextPhotoSnap").src = sito + pathfoto_next;
	document.getElementById("prevPhotoSnap").src = pathfoto_prev;
	document.getElementById("nextPhotoSnap").src = pathfoto_next;
}

function setImageIndexValue(sito, img_path, img_value)
{
  var img_index;
  
  img_index = parseInt(getArrayIndex(img_path, img_value));
  t1 = img_index + 1;
  t2 = img_index - 1;
  
  nextimg_index = t1 % img_path.length;
  if (t2 < 0) t2 = img_path.length + t2;
  previmg_index = t2 % img_path.length;

/*  
  tSnap1Next = img_index + 2;
  snap1Next_index = tSnap1Next % img_path.length;
  
  tSnap2Prev = img_index - 2;
  if (tSnap2Prev < 0) tSnap2Prev = img_path.length + tSnap2Prev;
  snap2Prev_index = tSnap2Prev % img_path.length;
*/  
  //alert(img_index - 1);
  //alert(img_index + ' - 1 % ' + img_path.length + ' = ' + previmg_index);
  
  //alert(img_index + ' - ' + nextimg_index + ' - ' + previmg_index);

  changeSnapshots(sito, img_path[nextimg_index], img_path[previmg_index]);
  
  //document.getElementById("nextPhotoAnchor").onclick = new Function("javascript:changePhoto('" + sito + "', img_path[" + nextimg_index + "], img_luogo[" + nextimg_index + "], img_data[" + nextimg_index + "], " + nextimg_index + "); changeSnapshots('" + sito + "', img_path[" + snap1Next_index + "], img_path[" + img_index + "]);");
  //document.getElementById("prevPhotoAnchor").onclick = new Function("javascript:changePhoto('" + sito + "', img_path[" + previmg_index + "], img_luogo[" + previmg_index + "], img_data[" + previmg_index + "], " + previmg_index + "); changeSnapshots('" + sito + "', img_path[" + img_index + "], img_path[" + snap2Prev_index + "]);");
  document.getElementById("nextPhotoAnchor").onclick = new Function("javascript:changePhoto('" + sito + "', img_path[" + nextimg_index + "], img_luogo[" + nextimg_index + "], img_data[" + nextimg_index + "], " + nextimg_index + ");");
  document.getElementById("prevPhotoAnchor").onclick = new Function("javascript:changePhoto('" + sito + "', img_path[" + previmg_index + "], img_luogo[" + previmg_index + "], img_data[" + previmg_index + "], " + previmg_index + ");");
  
  //alert("javascript:changePhoto('" + sito + "', img_path[" + nextimg_index + "], img_luogo[" + nextimg_index + "], img_data[" + nextimg_index + "], " + nextimg_index + "); changeSnapshots('" + sito + "', img_path[" + tSnap1Next + "], img_path[" + img_index + "]);");
  
  return 0;  
}

/*
function changePhoto(sito, pathfoto, luogo, data, img_index)
{	
	document.getElementById("layer_foto_tdTesto").innerHTML = luogo.replace("|", "'") + " - " + decodifica_data(data)
	document.getElementById("layer_foto_tdFoto").innerHTML = "<img id='foto' src='" + sito + pathfoto + "'' class='foto'>";

  //alert(document.getElementById('img_index').value + ' + 1 % ' + img_path.length + ' = ' + (parseInt(document.getElementById('img_index').value) + 1));

  //document.getElementById('img_index').value = (parseInt(document.getElementById('img_index').value) + 1) % img_path.length;
  //document.getElementById('img_index').value = img_index;
  setImageIndexValue(sito, img_path, pathfoto);
}

function setImageIndexValue(sito, img_path, img_value)
{
  var img_index;
  
  img_index = parseInt(getArrayIndex(img_path, img_value));
  t1 = img_index + 1;
  t2 = img_index - 1;
  nextimg_index = t1 % img_path.length;
  if (t2 < 0) t2 = img_path.length + t2;
  previmg_index = t2 % img_path.length;
  
  //alert(img_index - 1);
  //alert(img_index + ' - 1 % ' + img_path.length + ' = ' + previmg_index);

  document.getElementById("nextPhotoAnchor").onclick = new Function("javascript:changePhoto('" + sito + "', img_path[" + nextimg_index + "], img_luogo[" + nextimg_index + "], img_data[" + nextimg_index + "], " + nextimg_index + ")");
  document.getElementById("prevPhotoAnchor").onclick = new Function("javascript:changePhoto('" + sito + "', img_path[" + previmg_index + "], img_luogo[" + previmg_index + "], img_data[" + previmg_index + "], " + previmg_index + ")");
  
  return 0;  
}
*/

function getArrayIndex(x, value)
{
  //memorizzo l'indice dell'immagine su cui si č clickato
  var i=0;
  while (i<x.length && x[i] != value)
  {
    i++;
  }
  return i;
}

function AntispamEmail(email) {
	tokens = email.split('@');

	return tokens;
}

function addToSottosezioni(optionIndex, optionIndex2, idsottosezione, nomesottosezione)
{
	sottosezioni[optionIndex][optionIndex2] = new Object();
	sottosezioni[optionIndex][optionIndex2].value= idsottosezione;
	sottosezioni[optionIndex][optionIndex2].text= nomesottosezione;
}

function caricaSottosezioni(chooser) {
    var newElem;
    var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
    var cityChooser = chooser.form.elements["sottosezionepadre"];
    while (cityChooser.options.length) {
        cityChooser.remove(0);
    }
    var choice = chooser.selectedIndex;
    var db = sottosezioni[choice];
//document.write("Prova: " + sottosezioni[choice].text);
    newElem = document.createElement("option");
    newElem.text = "";
    newElem.value = 0;
    cityChooser.add(newElem, where);
	
//	document.write("Prova: " + choice);
    if (choice != null) {
//		document.write("Ciao")
        for (var i = 0; i < sottosezioni[choice].length; i++) {
            newElem = document.createElement("option");
            newElem.text = sottosezioni[choice][i].text;
            newElem.value = sottosezioni[choice][i].value;
            cityChooser.add(newElem, where);
        }
    }
	
//	cityChooser.selectedIndex = 0;
}

function stampaSottosezioni()
{
	document.write(sottosezioni[0][0].value);
	document.write(sottosezioni[0][0].text);
}

/*
</script></head>


<body>
<h1>Esempio - Select dinamiche</h1>
<hr> 
<form name="form">
Invia la richiesta a: 
<select name="continente" onchange="caricaSottosezioni(this)">
<option value="" selected="selected">Seleziona un continente:</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia/Oceania</option>
<option value="europa">Europa</option>
<option value="noamer">Nord America</option>
<option value="suamer">Sud America</option>
</select> 
<select name="cittā">
<option value="" selected="selected">Seleziona una cittā:</option>
</select>
</form>
*/

