function TestIfRegularYear(year)
{
	return !(((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0);
}                                                           

function GetDaysInMonth(month)
{
    var minDaysInMonth = 30;
    var maxDaysInMonth = 31;                                    

    return (month < 8) ? (minDaysInMonth + (month % 2)) : (maxDaysInMonth - (month % 2));
}

function CheckDateAux(day, month, year) // determina se a data é ou não válida
{
	var control = (TestIfRegularYear(year) ? 28 : 29);	
	
	return (month == 2) ? (day <= control) : (day <= GetDaysInMonth(month));
}  

function LoginSite()
{
	//EMail
	if ( !VerifyEmail(document.getElementById('fieldEMail').value) ) 
	{
		alert(decodeURIComponent("Por%20favor%20digite%20um%20endere%C3%A7o%20de%20E-Mail%20v%C3%A1lido"));
		document.getElementById('fieldEMail').focus();
		return;
	}	
	
	//Password
	if ( !VerifyTextField(document.getElementById('fieldPass').value) ) 
	{
		alert(decodeURIComponent("Por%20favor%20digite%20uma%20Password%20v%C3%A1lida"));
		document.getElementById('fieldPass').focus();
		return;
	}
	
	document.getElementById("fieldLogin").value = "true";

	document.getElementById('login').submit();
	

}

function showRegisto()
{

	window.location.href="/Registration/Registration.aspx?RegSave=true";
	
	/*
	document.getElementById('HeaderDiv').style.display='none';
	document.getElementById('EngineRegistoDiv').style.display='block';
	document.getElementById('ContentsRegistoDiv').style.display='block';
	document.getElementById('EngineLoginDiv').style.display='none';
	document.getElementById('EngineRightDiv').style.display='none';
	document.getElementById('SearchAviaoDiv').style.display='none';
	document.getElementById('SearchHotelDiv').style.display='none';
	*/
}                                                         

function CheckDate(day, month, year)
{
	return CheckDateAux(day, month, year) && day > 0 &&  month > 0 && year > 0;
}

function VerifyNumber(valueN)
{
	return !isNaN(valueN);
}

function VerifyEmail(email) {        

	var chk = /^(([^<>()[\]\\.,;:\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 (chk.test(email));
}

function VerifyTextField(str)
{
	var strAux = str;
	return strAux != null && strAux != "";
}

function TestIfRegularYear(year)
{
	return !(((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0);
}

function GetDaysInMonth(month)
{
	var minDaysInMonth = 30;
	var maxDaysInMonth = 31;
	
	return (month < 8) ? minDaysInMonth + (month % 2) : maxDaysInMonth - (month % 2);	
}

function CheckDateAux(day, month, year) // determina se a data é ou não válida
{
	var control = (TestIfRegularYear(year) ? 28 : 29);
	
	return (month == 2) ? day <= control : day <= GetDaysInMonth(month);
}

function CheckDate(day, month, year)
{	
	return (!CheckDateAux(day, month, year)) ? false : true;
}

function InvalidDateParts(day, month, year)
{
	var dateCounter = Number(day) * Number(month) * Number(year);
	return dateCounter > 0;
}

//Validação do Registo de Users
	function validateFields_(formID)
	{
	
		//Tipo Cartão
		if ( !document.getElementById('TipoCartao1').checked && !document.getElementById('TipoCartao2').checked && !document.getElementById('TipoCartao3').checked && !document.getElementById('TipoCartao4').checked )
		{
			alert(decodeURIComponent("Por%20favor%20escolha%20um%20cart%C3%A3o"));
			document.getElementById('TipoCartao1').focus();
			return false;			
		}
	
		//EMail			
		if ( !VerifyEmail(document.getElementById('email').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20um%20endere%C3%A7o%20de%20E-Mail%20v%C3%A1lido"));
			document.getElementById('email').focus();
			return false;
		}			
		//Password
		if ( !VerifyTextField(document.getElementById('password').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20uma%20Password%20v%C3%A1lida"));
			document.getElementById('password').focus();
			return false;
		}
		//ConfirmPassword
		if ( !VerifyTextField(document.getElementById('confirmPassword').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20uma%20Password%20de%20Confirma%C3%A7%C3%A3o%20v%C3%A1lida"));
			document.getElementById('confirmPassword').focus();
			return false;
		}
		//Valida Password == PasswordConfirmacao
		if ( document.getElementById('password').value != document.getElementById('confirmPassword').value )
		{
			alert(decodeURIComponent("A%20Password%20n%C3%A3o%20corresponde%20%C3%A0%20Password%20de%20Confirma%C3%A7%C3%A3o%20digitada"));
			document.getElementById('confirmPassword').focus();
			return false;	
		}
		
		//Nome
		if ( !VerifyTextField(document.getElementById('name').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20um%20Nome%20v%C3%A1lido"));
			document.getElementById('name').focus();
			return false;
		}			
		
		//DataNascimento
		/*
		if ( !CheckDate(document.getElementById('DataNascimentoDia').value, document.getElementById('DataNascimentoMes').value, document.getElementById('DataNascimentoAno').value) || !InvalidDateParts(document.getElementById('DataNascimentoDia').value, document.getElementById('DataNascimentoMes').value, document.getElementById('DataNascimentoAno').value)) 
		{
			alert(decodeURIComponent("Data%20de%20Nascimento%20inv%C3%A1lida"));
			document.getElementById('DataNascimentoDia').focus();
			return false;
		}
		*/
		
		//Morada
		if ( !VerifyTextField(document.getElementById('morada').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20uma%20Morada%20v%C3%A1lida"));
			document.getElementById('morada').focus();
			return false;
		}
		
		//CodigoPostal4
		if ( document.getElementById('codPostal4').value.length != 4 || document.getElementById('codPostal4').value == "" || !VerifyNumber(document.getElementById('codPostal4').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20um%20C%C3%B3digo%20Postal%20v%C3%A1lido"));
			document.getElementById('codPostal4').focus();
			return false;
		}
		
		//CodigoPostal3
		if ( document.getElementById('codPostal3').value.length != 3 || document.getElementById('codPostal3').value == "" || !VerifyNumber(document.getElementById('codPostal3').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20um%20C%C3%B3digo%20Postal%20v%C3%A1lido"));
			document.getElementById('codPostal3').focus();
			return false;
		}
				
		//Localidade
		if ( !VerifyTextField(document.getElementById('localidade').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20uma%20Localidade%20v%C3%A1lida"));
			document.getElementById('localidade').focus();
			return false;
		}
		
		//Telefone / Telemovel
		if ( !VerifyTextField(document.getElementById('phone').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20um%20Telefone%20v%C3%A1lido"));
			document.getElementById('phone').focus();
			return false;
		}
		
		//Habilitacoes
		/*
		if ( !VerifyTextField(document.getElementById('habilitacoes').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20uma%20Habilita%C3%A7%C3%A3o%20v%C3%A1lida"));
			document.getElementById('habilitacoes').focus();
			return false;
		}
		
		//AreaProfissional
		if ( !VerifyTextField(document.getElementById('AreaProfissional').value) ) 
		{
			alert(decodeURIComponent("Por%20favor%20digite%20uma%20%C3%81rea%20Profissional%20v%C3%A1lida"));
			document.getElementById('AreaProfissional').focus();
			return false;
		}
		*/
		
		//Privacidade
		if ( !document.getElementById('checkPrivacidade').checked )
		{
			alert(decodeURIComponent("Para%20se%20registar%20ter%C3%A1%20de%20aceitar%20as%20condi%C3%A7%C3%B5es%20de%20utiliza%C3%A7%C3%A3o"));
			document.getElementById('checkPrivacidade').focus();
			return false;			
		}		
		
		//Privacidade
		if ( !document.getElementById('checkProteccaoDados').checked )
		{
			alert(decodeURIComponent("Para%20se%20registar%20ter%C3%A1%20de%20aceitar%20a%20politica%20de%20protec%C3%A7%C3%A3o%20de%20dados"));
			document.getElementById('checkProteccaoDados').focus();
			return false;			
		}		
		
		//document.getElementById(formID).submit();

	}

	// For the Login DIV
	function SubmitOnEnter(event,ourform) {
		if (event && event.keyCode == 13)
		{
			LoginSite();
		}
		else
		{
			return true;
		}
	}
	
	// Recuperar Password
	function SubmitOnEnterRecuperarPassword(event,ourform) {
		if (event && event.keyCode == 13)
		{
			VerificaEMail();
		}
		else
		{
			return true;
		}
	}
	
	// Recuperar Email Registo
	function SubmitOnEnterRecuperarEmailRegisto(event,ourform) {
		if (event && event.keyCode == 13)
		{
			VerificaEMailRegisto();
		}
		else
		{
			return true;
		}
	}
