function MM_findObj(n, d) 
{
	var p,i,x;

	// La ricorsione per la ricerca inizia a livello del documento
	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];
		
	// Ricorsione per tutti i layer. Ci si interrompe se si trova l'oggetto
	for (i=0; !x && d.layers && i < d.layers.length; i++)
			x = MM_findObj(n, d.layers[i].document);
		
	// Se infine non si è ancora trovato l'oggetto si usa il metodo GetElementById 
	if (!x && d.getElementById) 
		x = d.getElementById(n);
		
	return x;
}

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_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_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 EnterPressed(evt)
{
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) 
	{
		var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
		if (charCode == 13) return true;
	}
	
	return false;
}
		
function CheckPrice(theField) {
	if (isNaN(theField.value)) {
		alert("Inserted number not valid!");
		
		return;
	}
}

/* Controlla che l'input sia un numero decimale */
function ValidateDecimal(evt) {
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
		if ((charCode < 48 || charCode > 57) && charCode != 8 && charCode != 46 && charCode != 44 && charCode != 9)
			return false;
	}
	return true;
}

function ValidateDecimalWithSign(evt) {
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
		if ((charCode < 48 || charCode > 57) && charCode != 8 && charCode != 46 && charCode != 45 && charCode != 44 && charCode != 9)
			return false;
	}
	return true;
}

/* Verifica se il numero è un decimale valido altrimento lo converte */
function CheckDecimal(theField) {
	var newstr = theField.value.replace(',', '.') ;
	if (isNaN(newstr)) {
		alert('Inserted number not valid!');
		theField.value = '';
	} else {
		newstr = theField.value.replace('.', ',') ;
		theField.value = newstr;
	}
}

/* Controlla che l'input sia un numero intero */
function ValidateInt(evt) 
{
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) 
	{
		var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
		if ((charCode < 48 || charCode > 57) && charCode != 8 && charCode != 9)
			return false;
	}
	return true;
}

/* Controlla che l'input sia un numero intero */
function ValidateIntWithSign(evt) 
{
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) 
	{
		var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
		if ((charCode < 48 || charCode > 57) && charCode != 8 && charCode != 9 && charCode != 45)
			return false;
	}
	return true;
}

/* Verifica se il numero è un intero valido altrimento lo converte */
function CheckInt(theField) 
{
	if (isNaN(theField.value)) 
	{
		alert('Inserted number not valid!');
		theField.value = '';
	}
}

function ChackMaxValue(theField, maxValue) 
{
	if (theField.value.replace(',','.') > maxValue)
	{
		theField.value = maxValue;
		alert('Value cannot be greater than ' + maxValue);
	}
}

function Redirect(url)
{
	var version = navigator.appVersion;
	if (version.indexOf("MSIE") >= -1)
	{ window.location.href=url; }
	else
	{ window.open(url, target="_self");	}
}

// Effettua il fill della lista PGiorno, dati i valori delle liste PMese e PAnno
// EmptyRow se nell'elenco dei giorni ci sono righe vuote
function vediGiorni(Pgiorno, Pmese, Panno, emptyRow) 
{ 
	if (Pgiorno == null || Pmese == null || Panno == null) return;
	
  // Indice del mese scelto
  var i = Pmese.selectedIndex;
  
  // Se è Febbraio
  if (Pmese.options[i].value == "2") 
  {
    Pgiorno.options[30 + emptyRow] = null;
    Pgiorno.options[29 + emptyRow] = null;
    var j = Panno.selectedIndex;
    var year = eval(Panno.options[j].value);
    if 
    ( ((year%400)==0) || (((year%100)!=0) && ((year%4)==0)) ) {
      if (Pgiorno.options[28 + emptyRow] == null) {
        Pgiorno.options[28 + emptyRow] = new Option("29");
        Pgiorno.options[28 + emptyRow].value = "29";
      }
    } 
    else Pgiorno.options[28 + emptyRow] = null;
  }
  
  // Mesi con 31 giorni
  if(Pmese.options[i].value == ""   ||
	 Pmese.options[i].value == "1"  ||
     Pmese.options[i].value == "3"  ||
     Pmese.options[i].value == "5"  ||
     Pmese.options[i].value == "7"  ||
     Pmese.options[i].value == "8"  ||
     Pmese.options[i].value == "10" ||
     Pmese.options[i].value == "12")
  {
    if (Pgiorno.options[28 + emptyRow] == null) {
      Pgiorno.options[28 + emptyRow] = new Option("29");
      Pgiorno.options[28 + emptyRow].value = "29";
    }
    if (Pgiorno.options[29 + emptyRow] == null) {
      Pgiorno.options[29 + emptyRow] = new Option("30");
      Pgiorno.options[29 + emptyRow].value = "30";
    }
    if (Pgiorno.options[30 + emptyRow] == null) {
      Pgiorno.options[30 + emptyRow] = new Option("31");
      Pgiorno.options[30 + emptyRow].value = "31";
    }
  }
  
  // Mesi con 30 giorni
  if(Pmese.options[i].value == "4" ||
     Pmese.options[i].value == "6" ||
     Pmese.options[i].value == "9" ||
     Pmese.options[i].value == "11") 
  {
    if (Pgiorno.options[28 + emptyRow] == null) {
      Pgiorno.options[28 + emptyRow] = new Option("29");
      Pgiorno.options[28 + emptyRow].value = "29";
    }
    if (Pgiorno.options[29 + emptyRow] == null) {
      Pgiorno.options[29 + emptyRow] = new Option("30");
      Pgiorno.options[29 + emptyRow].value = "30";
    }
    Pgiorno.options[30 + emptyRow] = null;
  }
  if (Pgiorno.selectedIndex == -1)
    Pgiorno.selectedIndex = 0;
}

// Get di un cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

// Get della chiave di un cookie
function getCookieKey(c_name, k_name)
{
	cookie = getCookie(c_name)

	if (cookie.length>0)
	{
		c_start=cookie.indexOf(k_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + k_name.length+1;
			c_end=cookie.indexOf("&",c_start);
			if (c_end==-1) c_end=cookie.length;
			return unescape(cookie.substring(c_start,c_end));
		}
	}
	return "";
}

