function newWindow(mypage,myname,w,h,features) {
  if(screen.width){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  }else{winl = 0;wint =0;}
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}

function get_radio_value(){
	for(var i=0; i < document.jump.var1.length; i++)
	{
		if(document.jump.var1[i].checked)
		{
			var rad_val = document.jump.var1[i].value;
		}
	}
	return(rad_val);
}

function  jump_radio(){
	var link=document.jump.jslink;
	var choice1=get_radio_value();
	self.location.href=link.value+"&var1="+choice1+"#a1";
}

function  jump1(){
	var link=document.form1.link;	/* forms[0] */
	var choice1=document.form1.var1;
	self.location.href=link.value+"&IDvar1="+choice1.options[choice1.selectedIndex].value+"#a1";
}

function  jump2(){
	var link=document.form1.link;
	var choice1=document.form1.var1;
	var choice2=document.form1.var2;
	self.location.href=link.value+"&IDvar1="+choice1.options[choice1.selectedIndex].value+"&IDvar2="+choice2.options[choice2.selectedIndex].value+"#a1";
}

function  jump3(){
	var link = document.form1.link;
	var choice1 = document.form1.var1;
	var choice2 = document.form1.var2;
	var choice3 = document.form1.var3;
	self.location.href=link.value+"&IDvar1="+choice1.options[choice1.selectedIndex].value+"&IDvar2="+choice2.options[choice2.selectedIndex].value+"&IDvar3="+choice3.options[choice3.selectedIndex].value+"#a1";
}

// aggiornamento select lingua
function  jump_language(){
	var sl=document.form_lng.lng;
	self.location.href="index.php?lngId="+sl.options[sl.selectedIndex].value;
}

function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
	var anchor = anchors[i];
	if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
		anchor.target = "_blank";
		if (anchor.title) anchor.title += " (Il link apre una nuova finestra)";
		if (!anchor.title) anchor.title = "Il link apre una nuova finestra";
	}
}
}
window.onload = externalLinks;

// Form

/*
window.onload=function(){
if(!document.getElementById || !document.createElement) return;
document.forms[0].onsubmit=function(){
	if(Validate())
		document.forms[0].submit();
	else{
		alert("attenzione: alcuni campi non sono stati inseriti o l\' indirizzo email fornito non è valido");
		return(false);
		}
	}
}
*/

// onsubmit=\"return confirmSubmit();\"
function confirmSubmit()
{
	var agree=confirm("Are you sure you wish to continue?");
	if (agree)
	return true ;
	else
	return false ;
}

// Funzioni di class.form.php

function validateForm(oForm)
{
	oForm.onsubmit = function() // attach the function to onsubmit event
	{
		if(!ValidateTermCondition())
		{
			alert("Attenzione: accettare i termini e le condizioni d'uso per proseguire nella registrazione.");
			return(false);
		}
		
		if(Validate())
			document.forms[oForm].submit();
		else
		{
			alert("Attenzione: alcuni campi non sono stati inseriti o l\' indirizzo email fornito non è valido");
			return(false);
		}
	}
}

function ValidateTermCondition(){
	var labels=document.getElementsByTagName("label");
	var validationOK=true;
	for(var i=0;i<labels.length;i++)
	{
		var lab=labels[i];
		if(lab.className=="req2") lab.className="req";
		if(lab.className=="req")
		{
			var inp=lab.getElementsByTagName("input");
			if(inp[0].type=="checkbox" && inp[0].checked==false && inp[0].id=="terms")
			{
				lab.className="req2";
				validationOK=false;
			}
		}
	}
	return(validationOK);
}

function Validate(){
	var labels=document.getElementsByTagName("label");
	var validationOK=true;
	for(var i=0;i<labels.length;i++)
	{
		var lab=labels[i];
		if(lab.className=="req2") lab.className="req";
		if(lab.className=="req")
		{
			var inp=lab.getElementsByTagName("input");
			if(inp.length>0 && inp[0].value=="")
			{
				lab.className="req2";   //input vuoto
				validationOK=false;
			}
			if(inp.length>0 && inp[0].id=="email")
			{
				if(!isEmail(inp[0].value))
				{
					lab.className="req2";   //email non valida
					validationOK=false;
				}
			}
			if(inp.length>0 && inp[0].type=="checkbox" && inp[0].checked==false)
			{
				lab.className="req2";   //checkbox non selezionato
				validationOK=false;
			}
			var sel=lab.getElementsByTagName("select");
			if(sel.length>0 && sel[0].selectedIndex==0)
			{
				lab.className="req2";  //select con indice a zero
				validationOK=false;
			}
			var txt=lab.getElementsByTagName("textarea");
			if(txt.length>0 && txt[0].value=="")
			{
				lab.className="req2";   //input vuoto
				validationOK=false;
			}
		}
	}
	return(validationOK);
}

function isEmail(str){
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}