// JavaScript Document
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function hideLayer(layerName)
{
	var DHTML = (document.getElementById || document.all || document.layers);
	if (!DHTML)  return;
	var obj=document.getElementById(layerName);
	obj.style.visibility="hidden";
}
function showLayer(layerName)
{
	var DHTML = (document.getElementById || document.all || document.layers);
	if (!DHTML)  return;
	var obj=document.getElementById(layerName);
	obj.style.visibility="visible";
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function openCenterWin(url,theWidth,theHeight){
	var theTop=(screen.height/2)-(theHeight/2);
	var theLeft=(screen.width/2)-(theWidth/2);
	var features='height='+theHeight+',width='+theWidth+',top='+theTop+',left='+theLeft+",menubar=yes,scrollbars=yes,resizable=yes";
	theWin=window.open(url,'',features);
}
function setNumImage(numImages){
	var source = "../postcards/foto/";
	var DHTML = (document.getElementById || document.all || document.layers);
	if (!DHTML)  return;
	var obj=document.getElementById("photo");
	obj.src = source + numImages + ".jpg";
	document.frmPostcard.numImage.value = numImages;
}
function changeImage(element,path){
	var DHTML = (document.getElementById || document.all || document.layers);
	if (!DHTML)  return;
	var obj=document.getElementById(element);
	obj.src = path;
}
function restoreExcursion(){
	var DHTML = (document.getElementById || document.all || document.layers);
	if (!DHTML)  return;
	switch(excursion){
		case 1:
			var obj=document.getElementById('Image12');
			obj.src = '../images/mare_over.jpg';
			break;
		case 2:
			var obj=document.getElementById('Image11');
			obj.src = '../images/cultura_over.jpg';
			break;
		case 3:
			var obj=document.getElementById('Image13');
			obj.src = '../images/avventura_over.jpg';
			break;
	}
}
function popupform(myform, windowname)
{
if (! window.focus)return true;
window.open('', windowname, 'height=420,width=780,scrollbars=yes');
myform.target=windowname;
return true;
}

function setBeachImage(numImages){
	var source = "../images/spiagge/";
	var DHTML = (document.getElementById || document.all || document.layers);
	if (!DHTML)  return;
	var obj=document.getElementById("photo");
	obj.src = source + numImages + ".jpg";
}
function go_structure(f){
	if(f.structure.value != '*'){
		f.action = 'village.php'; 
		f.submit();
	}
	else
	{
		alert("Selezionare una struttura");	
	}
}

function isblank(e)
{
  if (e.value == null || e.value == "")
    return true;

  for(var i = 0; i < e.value.length; i++)
  {
     var c = e.value.charAt(i);
     if ((c != ' ') &&
         (c != '\n') &&
         (c != '\t'))
        return false;
  }
  return true;
}

// Checks if an optional field is blank

function checkblank(e)
{
  if (isblank(e))
  {
    alert("Il campo " + e.description + " deve essere riempito");
    return false;
  }
  return true;
}

// Checks if a field is numeric.
// If the optional min property is set, it checks it is greater than
// its value
// If the optional max property is set, it checks it is less than
// its value

function checknumber(e)
{
  var v = parseFloat(e.value);
  if (isNaN(v))
  {
    alert("Il campo " + e.description + " deve essere un numero");
    return false;
  }
  if ((e.minNumber != null) && (v < e.minNumber))
  {
    alert("The field " + e.description +
          " must be greater than or equal to " + e.minNumber);
    return false;
  }
  if (e.maxNumber != null && v > e.maxNumber)
  {
    alert("The field " + e.description +
          " must be less than or equal to " + e.maxNumber);
    return false;
  }
  return true;
}

// Checks if a field looks like a date in the 99/99/9999 format

function checkdate(e)
{
  var slashCount = 0;
  if (e.value.length > 10 && e.value.length < 9)
  {
    alert(" Il campo " + e.description +
          " deve avere il formato 99/99/9999" +
          " e lungo 10 caratteri");
    return false;
  }
  for(var j = 0; j < e.value.length; j++)
  {
    var c = e.value.charAt(j);
    if ((c == '/'))
       slashCount++;
    if (c != '/' && (c < '0' || c > '9'))
    {
      alert(" Il campo " + e.description +
            " puņ contenere solo numeri separati da slash");
      return false;
    }
  }
  
  if (slashCount != 2)
  {
    alert(" Il campo" + e.description +
          " deve avere il formato 99/99/9999");
    return false;
  }
  return true;
}

// Checks if a field contains any whitespace

function checkwhitespace(e)
{
  var seenAt = false;
  for(var j = 0; j < e.value.length; j++)
  {
     var c = e.value.charAt(j);
     if ((c == ' ') || (c == '\n') || (c == '\t'))
     {
       alert("Il campo " + e.description +
             " non deve contenere spazi vuoti");
       return false;
     }
  }
  return true;
}

// Now check for fields that are supposed to be emails.
// Only checks that there's one @ symbol and no whitespace

function checkemail(e)
{
  var seenAt = false;
  for(var j = 0; j < e.value.length; j++)
  {
    var c = e.value.charAt(j);
    if ((c == ' ') || (c == '\n') || (c == '\t'))
    {
      alert("Il campo " + e.description + 
            " non deve contenere spazi vuoti");
      return false;
    }
    if ((c == '@') && (seenAt == true))
    {
      alert("Il campo " + e.description + " deve contenere solo una @");
      return false;
    }
    if ((c == '@'))
      seenAt = true;
  }
  if (seenAt == false)
  {
    alert("Il campo " + e.description + " deve contenere una @");
    return false;
  }
  return true;
}

function checkRadioButton(f){ 
       if (f.rdbPrivacy.checked && f.rdbPrivacy.value == 'YES'){ 
		   return true; 
       } 
	   else
	   {
	   	   alert("Attenzione: Per inviare il modulo,\r\nselezionare il consenso al trattamento dei dati personali") 
	   	   return false;
	   }
} 

// This is the function that performs <form> validation.
// It is invoked from the onSubmit( ) event handler.
// The handler should return whatever value this function
// returns.
function verify(f)
{
  // Loop through the elements of the form, looking for all
  // text and textarea elements. Report errors using a post validation,
  // field-by-field approach
  for(var i = 0; i < f.length; i++)
  {
     var e = f.elements[i];
		 if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")))
		 {
			// first check if the field is empty and shouldn't be
			if (!e.isOptional && !checkblank(e))
			  return false;
			  
			// Now check for fields that are supposed to be numeric.
	
			if (!isblank(e) && e.isNumeric && !checknumber(e))
			  return false;
	
			// Now check for fields that are supposed to be dates
	
			if (!isblank(e) && e.isDate && !checkdate(e))
			  return false;
			// Now check for fields that are supposed to be emails
	
			if (!isblank(e) && e.isEmail && !checkemail(e))
			  return false;
	
			// Now check for fields that are supposed
			// not to have whitespace
	
			if (!isblank(e) && e.hasNospaces && !checkwhitespace(e))
			 return false;
	
		 } // if (type is text or textarea)
		 if(e.type == "radio"){
		 	if(!checkRadioButton(f)) return false;
		 }
	  } // for each character in field
  // There were no errors if we got this far
  return true;
}
