var win = null;

function openDownload(url,name) {
	server  = document.domain;
	server_url = 'http://' + server + url;
	w = 500;
	h = 30;
	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('', '_blank', settings);
	win.document.write('<html>'); 
	win.document.write('<meta http-equiv=refresh content="0; url=' + server_url + '">'); 
	win.document.write('<title>Download in progress: '+ name + '</title>'); 
	win.document.write('<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">');
	win.document.write('&nbsp;<p><center><img src=/images/standard/progressbar.gif width=467 height=17>'); 
	win.document.write('<br><a href="javascript:self.close()">click to close when download is completed</a></center>'); 
	win.document.write('</body></html>');
	win.document.close();
	win.focus();
}

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 == "[+]") {
		text.nodeValue = "[-]";
		return;
	}
	if (text.nodeValue == "[-]") {
		text.nodeValue = "[+]";
		return;
	}
	if (text.nodeValue == "[apri]") {
		text.nodeValue = "[chiudi]";
		return;
	}
	if (text.nodeValue == "[chiudi]") {
		text.nodeValue = "[apri]";
		return;
	}
	if (text.nodeValue == "[open]") {
		text.nodeValue = "[close]";
		return;
	}
	if (text.nodeValue == "[close]") {
		text.nodeValue = "[open]";
		return;
	}
  
}

/*
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++) {
			switch(document.validateArray[i].type) {
				case 'empty':
				break;
				case 'date':
					if ( ! isDate ( getValue(form.elements[document.validateArray[i].name]))  )  {
						errors += '- ' + document.validateArray[i].descr + '\n';
					}
				break; 
				case 'positive':
					if ( ! isPosInteger ( getValue(form.elements[document.validateArray[i].name]))  )  {
						errors += '- ' + document.validateArray[i].descr + '\n';
					}
				break; 
				case 'email':
					if ( ! isEmail ( getValue(form.elements[document.validateArray[i].name]) )  )  {
						errors += '- ' + document.validateArray[i].descr + '\n';
					}
				break; 
				case 'match':
					if ( ! matchValue ( form.elements[document.validateArray[i].name], document.validateArray[i].min )  )  {
						errors += '- Devi accettare: ' + document.validateArray[i].descr + '\n';
					}
				break;
				case 'length':
					if ( ! length ( form.elements[document.validateArray[i].name] ,document.validateArray[i].min,document.validateArray[i].max )  )  {
						errors += '- ' + document.validateArray[i].descr + '\n';
					}
				break; 
				default:
					if ( isEmpty ( form.elements[document.validateArray[i].name])  )  {
						errors += '- ' + document.validateArray[i].descr + '\n';
					}
				break; 
			}
			
		}
		if (errors != '') {
			alert('These fields are mandatory\n Completa i dati richiesti:\n'+errors);
		}
		else {
			alreadysubmitted = true;
		}
		return (errors=='');
	}


}

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 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 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 _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 setValue(obj, value)
{
	if ((typeof(obj)!='object')) {
		return;
	}
	
	var type = 'undefined';
	if (obj.length > 0) {
		for (i = 0; i < obj.length; i++) {
			if (obj[i]) {
				type = obj[i].type;
			}
		}
	}
	else {
		type = obj.type;
	}


	switch(type)
	{
		case 'undefined': return;
		case 'radio': obj.checked = value; break;
		case 'select-one': obj.selectedIndex = value; break;
		case 'checkbox' :
			value = value.split(',');
			for(var x=0; x < obj.length; x++) 
				obj[x].checked = value[x];
			break;

		case 'select-multiple':
			for(var x=0; x < obj.length; x++) 
				obj[x].selected = value[x];
			break;

		default: obj.value = value; break;
	}
}

function getValue(obj) 
{
	if ((typeof(obj)!='object')) {
		return false;
	}
	var value = '';
	var type = '';
	if (obj.length > 0) {
		for (i = 0; i < obj.length; i++) {
			if (obj[i]) {
				type = obj[i].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 matchValue (obj,value)
{
	if (isEmpty(obj))
       return false;
    s = getValue(obj);
    return (s == value);
}



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];}
}



