var win = null;
function openWin(mypage,myname,w,h,aspect) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+ ',' + aspect;
	win = window.open(mypage, myname, settings);
	win.focus();
}
function openWindow(mypage,myname,w,h) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',directories=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no';
	win = window.open(mypage, myname, settings);
	win.focus();
}
function openResize(mypage,myname,w,h) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',directories=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
	win = window.open(mypage, myname, settings);
	win.focus();
}
function openSite(mypage,myname,w,h) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',directories=no,toolbar=yes,location=yes,status=no,menubar=yes,scrollbars=yes,resizable=yes';
	win = window.open(mypage, myname, settings);
	win.focus();
}
function openImage(mypage,myname,alt,w,h) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',directories=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no';
	win = window.open("", myname, settings);
	win.document.write('<html><title>'+alt+'</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">');
	win.document.write('<img src='+mypage+' width='+w+' height='+h+' alt='+alt+'>');
	win.document.write('</body></html>');
	win.document.close();
	win.focus();
}

function autoResize(w,h) {
	width = screen.availWidth;
	height = screen.availHeight;
	width = w;
	heigth = h;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	window.moveTo(LeftPosition, TopPosition);
	if (document.all) {
		top.window.resizeTo(width,height);
	}
	else if (document.layers||document.getElementById) {
			if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
					top.window.outerHeight = h;
					top.window.outerWidth = w;
			}
	}
}

/*
Show Hide layers
*/

function ShowHide(id)
{
  var itm = null;
  if (document.getElementById) {
	itm = document.getElementById(id);
  } else if (document.all){
	itm = document.all[id];
  } else if (document.layers){
	itm = document.layers[id];
  }

  if (!itm) {
   // do nothing
  }
  else if (itm.style) {
	if (itm.style.display == "none") {
	  itm.style.display = "";
	}
	else {
	  itm.style.display = "none";
	}
  }
  else {
	itm.visibility = "show";
  }
	var text = document.getElementById(id + "-switch").firstChild;
	if (text.nodeValue == "[apri]") {
		text.nodeValue = "[chiudi]";
	} else {
		text.nodeValue = "[apri]";
	}

}

/*
Text Link/Image Map Tooltip Script
*/

var event = null;
if (!document.layers&&!document.all&&!document.getElementById)
	event="test"

function showtip(current,e,text){
	if (document.all||document.getElementById){
		thetitle=text.split('<br>')
	if (thetitle.length>1){
		thetitles=''
		for (i=0;i<thetitle.length;i++)
			thetitles+=thetitle[i]
		current.title=thetitles
	}
	else
		current.title=text
	}
	else if (document.layers){
		document.tooltip.document.write('<layer bgcolor="#FFFFFF" style="border:1px solid black;font-size:12px;">'+text+'</layer>')
		document.tooltip.document.close()
		document.tooltip.left=e.pageX+5
		document.tooltip.top=e.pageY+5
		document.tooltip.visibility="show"
	}
}
function hidetip(){
	if (document.layers)
	document.tooltip.visibility="hidden"
}

function noSpam(user,domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}


var mOvrClass='';
function mOvr(src,clrOver) {
	if (clrOver == undefined) {var clrOver='mOvr'};
	mOvrClass = src.className;
	src.className = mOvrClass + ' ' + clrOver;
	src.className = clrOver;
}
function mOut(src) {
	src.className=mOvrClass;
}

var alreadysubmitted = false;

function validate( form ) {
    var errors = '';
	if (alreadysubmitted == true) {
		alert('You data are already submitted / I tuoi dati sono già stati inviati');
		return false;
	}
	if (document.validateArray)
	{
		for (var i = 0; i < document.validateArray.length; i++) {
			var has_error = false;
			switch(document.validateArray[i].type) {
				case 'empty':
				break;

				case 'range':
					if ( ! isIntegerInRange ( getValue(form.elements[document.validateArray[i].name]) , document.validateArray[i].min, document.validateArray[i].max )  )  {
						has_error = true;
					}
				break; 
				case 'date':
					if ( ! isDate ( getValue(form.elements[document.validateArray[i].name]))  )  {
						has_error = true;
					}
				break; 
				case 'positive':
					if ( ! isInteger ( getValue(form.elements[document.validateArray[i].name]))  )  {
						has_error = true;
					}
				break; 

				case 'email':
					if ( ! isEmail ( form.elements[document.validateArray[i].name])  )  {
						has_error = true;
					}
				break;
				case 'length':
					if ( ! length ( form.elements[document.validateArray[i].name] ,document.validateArray[i].min,document.validateArray[i].max )  )  {
						has_error = true;
					}
				break;
				case 'match':
					if ( ! matchValue ( form.elements[document.validateArray[i].name], document.validateArray[i].min )  )  {
						has_error = true;
					}
				break;

				default:
					if ( isEmpty ( form.elements[document.validateArray[i].name])  )  {
						has_error = true;
					}
				break;
			}
			if (has_error) {
				errors += '- ' + document.validateArray[i].descr + ' \n';
			}

		}
		if (errors != '') {
			alert('These fields are mandatory\n Completa i dati richiesti: \n'+errors);
		}
		else {
			alreadysubmitted = true;
		}
		return (errors=='');
	}


}

function removeFromValidateArray(name) {
	if (!document.validateArray) {
		return false;
	}

	for (var i = 0; i < document.validateArray.length; i++) {
		if (document.validateArray[i].name == name) {
			field = new Object();
			field.name = '';
			field.descr = '';
			field.type = 'empty';
			document.validateArray[i] = field;
			return true;
		}
	}

}

function addToValidateArray(name, descr, type, min, max) {
	if (!document.validateArray) {
		document.validateArray = new Array();
	}
	field = new Object();
	field.name = name;
	field.descr = descr;
	field.type = type;
	field.min = min;
	field.max = max;
	document.validateArray[document.validateArray.length] = field;
}


function matchValue (obj,value)
{
	if (isEmpty(obj))
       return false;
    s = getValue(obj);
    return (s == value);
}

function length (obj, min, max)
{
	if (isEmpty(obj))
       return false;
    s = obj.value;
    var sLength = s.length;
    if (min == max) {
		return (sLength >= min);
    }
    else {
		return ((sLength >= min)&&(sLength <= max));
    }

}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isDate(dtStr){
	if (isEmpty(dtStr)) {
		return false;
	}
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
	return true
}

function isEmail (obj)
{   
	if (isEmpty(obj)) 
       return false;
    
    s = obj.value;
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function isIntegerInRange (value, min, max)
{   

    if (!isInteger(value)) return false;
    var num = parseInt (value);
    return ((num >= min) && (num <= max));
}


function isInteger (s)
{   
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    if (i==0) return false;
    // All characters are numbers.
    var num = parseInt (s);
    return (num > 0);
}


function getValue(obj) {
	var value = '';
	var type = '';
	if (obj.length > 0)
		type = obj[0].type;
	else
		type = obj.type;

	switch (type) {
		case "select-one" :
			return (obj.options[obj.selectedIndex].value);
		case "select-multiple" :
			for (i = 0; i < obj.length; i++) {
				if (obj.options[i].selected) {
					return obj.options[i].value;
				}
			}
			return value;
		case "checkbox" :
			for (i = 0; i < obj.length; i++) {
				if (obj[i].checked) {
					return  obj[i].value;
				}
			}
			if (obj.checked) {
				return obj.value;
			}
		case "radio" :
			for (i = 0; i < obj.length; i++) {
				if (obj[i].checked) {
					return obj[i].value;
				}
			}
			return "";
		default :
			return (obj.value);
	}
}




function isEmpty(obj) {
    s = trim(getValue(obj));
    return ((s == null) || (s.length == 0));
}

function trim(theString) {
	theString = "" + theString;
	var i, firstNonWhite;
	if (stripChars(" \n\r\t", theString).length == 0)
		return "";
	i = -1;
	while (true) {
		i++;
		if (theString.charAt(i) != " ")
			break;
	}
	firstNonWhite = i;
	i = theString.length;
	while (1) {
		i--;
		if (theString.charAt(i) != " ")
			break;
	}
	return theString.substring(firstNonWhite, i + 1);
}
function stripChars(theFilter, theString) {
	var strOut, i, curChar;
	strOut = "";
	for (i = 0; i < theString.length; i++) {
		curChar = theString.charAt(i);
		if (theFilter.indexOf(curChar) < 0)
			strOut += curChar;
	}
	return strOut;
}

function createForm(formAction, formName, formTarget){
  var formMethod = "post";
  if (formTarget == null) {
  	formTarget = '_top';
  }
  if (document.getElementById) {
    var form = document.createElement('form');
      if (document.all) {
        form.method = formMethod;
        form.name = formName;
        form.action = formAction;
        form.target = formTarget;
      }
      else if (document.getElementById) {
        form.setAttribute('method', formMethod);
        form.setAttribute('name', formName);
        form.setAttribute('action', formAction);
        form.setAttribute('target', formTarget);
      }
    document.body.appendChild(form);
  }
  return form;
}

function addField(form, fieldType, fieldName, fieldValue) {
  if (document.getElementById) {
    var input = document.createElement('input');
      if (document.all) {
        input.type = fieldType;
        input.name = fieldName;
        input.value = fieldValue;
      }
      else if (document.getElementById) {
        input.setAttribute('type', fieldType);
        input.setAttribute('name', fieldName);
        input.setAttribute('value', fieldValue);
      }
    form.appendChild(input);
  }
}

function MM_swapImgRestore()
{
	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_preloadImages()
{
	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)
{
	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); return x;
}
function MM_swapImage()
{
	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];}
}


