var popupWin = null;
var popupWin2  = null;
function openWin(target, width, height)
{
	var popupTop = (screen.height - height)/2;
	var popupLeft = (screen.width - width)/2;

	if(popupWin == null || popupWin.closed)
	{
		popupWin = window.open(target,'window1','scrollbars=yes,resizable=yes,top=' + popupTop + ',left=' + popupLeft + ',status=no,width=' + width + ',height=' + height);
	}
	else
	{
		popupWin.location = target;
		setTimeout("popupWin.focus()",500)
	}
}
function openWin2(target, width, height)
{
	var popupTop = (screen.height - height)/2;
	var popupLeft = (screen.width - width)/2;

	if(popupWin2 == null || popupWin2.closed)
	{
		popupWin2 = window.open(target,'window2','scrollbars=yes,resizable=yes,top=' + popupTop + ',left=' + popupLeft + ',status=no,width=' + width + ',height=' + height);
	}
	else
	{
		popupWin2.location = target;
		setTimeout("popupWin2.focus()",500)
	}
}
function checkEmailAddress(field)
{

	// Note: The next expression must be all on one line...
	//       allow no spaces, linefeeds, or carriage returns!
	alert("checkEmailAddress");
        var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	alert(goodEmail);
        if (goodEmail==null)
	{

	   alert('Inserire un indirizzo valido');
	   field.focus();
	   field.select();
	}
	return goodEmail;
}


function validateFormRegistrazione(form,fields)
{

  campiValidi = validateForm(form,fields);
  if(campiValidi==true)
  {

    if(form["Password"].value != form["ConfermaPassword"].value)
    {
      alert("Verificare la password");
      return false;
    }
    if((form["Mail"].value!=(""))&&(checkEmailAddress(form["Mail"])))
    {

     form.submit();

    }else if(form["Mail"].value!=(""))
    {
     return false;
    }
    form.submit();
    return false;
  }
}

function validateForm(form,fields)
{

	if(fields == "") return true;
	fields = fields.split(',');

	for(var i = 0; i < fields.length; i++)
	{
		switch(form[fields[i]].type)
		{
			case 'select-one':      if(form[fields[i]].selectedIndex == 0) { alert("Please select a " + fields[i].toLowerCase() + "."); return false; }; break;
			case 'select-multiple': if(form[fields[i]].selectedIndex == 0) { alert("Please select a " + fields[i].toLowerCase() + "."); return false; }; break;
			case 'checkbox':        if(!form[fields[i]].checked) { alert("Please check the " + fields[i].toLowerCase() + "."); return false; }; break;
			default:                if(form[fields[i]].value == "") { alert("Il campo " + fields[i].toLowerCase() + " è obbligatorio."); return false; }
		}
	}
	return true;
}

