/*validate email*/
function isValidEmail(email) 
{
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/   
  return re.test(email);
}

/*validate FiscalCode*/
function isValidFiscalCode(cf) 
{
  var re = /^[A-Za-z]{6}[0-9]{2}[A-Za-z]{1}[0-9]{2}[A-Za-z]{1}[0-9]{3}[A-Za-z]{1}$/   
  return re.test(cf);
}

/*validate Date*/
function isDate(year,month,day)
{
	var d = day + "";
	var m = month + "";
	var y = year + "";

	if((d == "") || ( m == "") || ( y == ""))
		return false;

	if( isNaN(d) || isNaN(m) || isNaN(y) )
		return false;

	if( ((parseFloat(d) + "") != (parseInt(d) + "")) || ((parseFloat(m) + "") != (parseInt(m) + "")) || ((parseFloat(y) + "") != (parseInt(y) + "")) )
		return false;

	d = parseInt(d);
	m = parseInt(m);
	y = parseInt(y);

	if(d < 1 || m < 1 || y < 1)
		return false;

	if(m < 1 || m > 12)
		return false;

	if((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && (d > 31) )
		return false;
	if((m == 4 || m == 6 || m == 9 || m == 11) && (d > 30) )
		return false;
	if((m == 2) && (d > 29) )
		return false;
	if((m == 2) && (!isLeapYear(y)) && (d == 29))
		return false;

	return true;
}

// function isLeapYear(year)
function isLeapYear (year)
{
	return ((year % 4 == 0 && year % 100 != 0) || ((year % 4 == 0 && year % 100 == 0) && (year % 400 == 0)));
}


function validate(frm)
{
	// Nome
	if(frm.nome.value == "")
	{
		alert("Inserisci un valore valido per il campo Nome");
		frm.nome.focus();
		return false;
	}
	// Cognome
	if(frm.cognome.value == "")
	{
		alert("Inserisci un valore valido per il campo Cognome");
		frm.cognome.focus();
		return false;
	}
	// Email
	if(isValidEmail(frm.email.value) == "")
	{
		alert("Inserisci un valore valido per il campo E-mail");
		frm.email.focus();
		return false;
	}
	// Codice fiscale - Data di nascita
	if ( frm.richiesta[frm.richiesta.selectedIndex].value == "3.4 - Carta di Credito - Estratto Conto Carta di Credito" )
	{
		if(isValidFiscalCode(frm.cf.value) == "")
		{
			alert("Inserisci un valore valido per il campo Codice fiscale");
			frm.cf.focus();
			return false;
		}
		var day = document.frm.day.options[document.frm.day.selectedIndex].value;
		var month = document.frm.month.options[document.frm.month.selectedIndex].value;
		var year = document.frm.year.options[document.frm.year.selectedIndex].value;
	
		if ( day == "" || month == "" || year == "" )
		{ 
			alert("Selezionare una data");
			frm.day.focus();		
		   	return false;
		}
		else if(!(isDate(year,month,day)))
		{  
			alert("Inserire una data valida");				
	   		return false;
		}
	}

	
	//Privacy
	if(frm.privacy[0].checked == false)
	{
		alert("Devi consentire il trattamento dei tuoi dati personali");
		return false;
	}
	return true;
}

/*controlSelect*/
function controlSelect(selobj)
	{
		var id = selobj.options[selobj.selectedIndex].id;
		if ( id )
			doToggle( id + "sub" );
	}

/*doToggle*/
var _lasttoggledid = "";

function doToggle ( elementid )
{
	if ( findObj(elementid) )
	{
		if ( findObj(elementid).style.display == "block" )
		{
			findObj(elementid).style.display = "none";
			_lasttoggledid = ""
			return;
		}
	}
		
	if ( _lasttoggledid!="" )
		showHideObject ( _lasttoggledid, false );
	showHideObject ( elementid, true );
	_lasttoggledid = elementid;
}

// base functions
function findObj(n, d) { //v4.01
  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=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function 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=findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function 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 showHideLayers() {
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function showHideObject ( id, visible ) 
{
	var obj = findObj(id);
	if ( obj ) obj.style.display = visible ? "block" : "none";
}

$(document).ready(function(){

	$("#header .menu").animate({"opacity": 0.9 });
	
});
