String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function Testar_Numeros(Obj)
{
	var re = /^(\d|-|\(|\)|\s)*$/;
	
	Obj.value = Obj.value.trim();
	
	if(Obj.value.length == 0)
		return true;
	else
	{
		if(re.exec(Obj.value) == null)
			return false;
		else
			return true;
	}	
}

function FormatCurrency(valor)
{
	var n, m, sAux, valor1, valor2;
	
	valor = valor.split(".");
	valor1 = valor[0].toString();
	
	if(valor.length > 1)
	{
		valor2 = valor[1].toString();
		if(valor2.length == 1)	
			valor2 = valor2 + "0";
	}
	else
		valor2 = "00";
	
	sAux = "";
    m = valor1.length;
    
    for(n = 1;n < m + 1; n++)
    {
        sAux = valor1.substr(m - n,1) + sAux;
        
        if((n % 3 == 0) && (n < m)) sAux = "." + sAux;
	}  
	return sAux + "," + valor2 + "€";
}


function GetNextDay(ActualDay)
{
	var data1, data2, result = "";
	
	ActualDay = ActualDay.split("/");
	data1 = new Date(ActualDay[2], ActualDay[1] - 1, ActualDay[0]);
	data2 = new Date(data1.getTime() + (24 * 60 * 60 * 1000));
	
	if(parseInt(data2.getDate()) < 10)
		result = "0" + data2.getDate() + "/";
	else
		result = data2.getDate() + "/";
		
		
	if(parseInt(data2.getMonth()) < 10)
		result = result + "0" + (data2.getMonth() + 1) + "/"	;
	else
		result = result + (data2.getMonth() + 1) + "/";		
	
	result = result + data2.getFullYear();
	
	return result;
}


function GetNextCincoDay(ActualDay)
{
	var data1, data2, result = "";
	
	ActualDay = ActualDay.split("/");
	data1 = new Date(ActualDay[2], ActualDay[1] - 1, ActualDay[0]);
	data2 = new Date(data1.getTime() + (120 * 60 * 60 * 1000));
	
	if(parseInt(data2.getDate()) < 10)
		result = "0" + data2.getDate() + "/";
	else
		result = data2.getDate() + "/";
		
		
	if(parseInt(data2.getMonth()) < 10)
		result = result + "0" + (data2.getMonth() + 1) + "/"	;
	else
		result = result + (data2.getMonth() + 1) + "/";		
	
	result = result + data2.getFullYear();
	
	return result;
}


function CalcularAlojamento()
{
	with(document.Form1)
	{
		var data1, data2, TotalDias, arrPrecos, ValorTotal;

		data1 = new Date(CheckIn.value.split("/")[2], CheckIn.value.split("/")[1] - 1, CheckIn.value.split("/")[0])
		data2 = new Date(CheckOut.value.split("/")[2], CheckOut.value.split("/")[1] - 1, CheckOut.value.split("/")[0])
		TotalDias = (data2 - data1)/1000/60/60/24;
					
		NrNoites.value = Math.round(TotalDias);	
		
		//alert(Math.round(TotalDias));
		//NrNoitesCell.innerText = Math.round(TotalDias);
	}
	
	return true;
}

function VerificaCheckIn()
{
	with(document.Form1)
	{
		CheckIn.value = CheckIn.value.trim();
		CheckOut.value = CheckOut.value.trim();
		if((CheckIn.value.length == 0) && (CheckOut.value.length == 0))
		{
			alert("Por favor, preencha o campo \"Check-in\".");
			CheckIn.focus();
			return false;
		}
	}
	return true;
}

function showCalCheckOut()
{
	if(VerificaCheckIn()) showCal('Calendar2');
}

function VerificaDataCheckIn()
{
	with(document.Form1)
	{
		CheckIn.value = CheckIn.value.trim();
		CheckOut.value = CheckOut.value.trim();
		CheckIn.value = CheckIn.value.replace(/-/g,"/");
		CheckIn.value = CheckIn.value.replace(/\./g,"/");
		//alert(data_serv);
		if(CheckIn.value.length > 0)
		{
			if(!verificaData3(CheckIn.value, data_serv))
			{
				CheckIn.focus();
				
				
			}	
			else if(CheckOut.value.length == 0) 
			{
				CheckOut.value = GetNextDay(CheckIn.value);
				CheckOut.focus();
				CalcularAlojamento();
			}
			else if(!verificaData4(CheckIn.value, CheckOut.value))
			{
				CheckOut.value = GetNextDay(CheckIn.value);
				CheckOut.focus();
				CalcularAlojamento();
			}	
			else
			{
				CalcularAlojamento();
				
			}	
		}
		else
		{
			CheckOut.value = "";
			NrNoites.value = 0;
			//NrNoitesCell.innerText = 0;
			//ValorTotalCell.innerHTML = "0,00€";
			//TotalAlojamento = 0;
		}
	}
}

function VerificaDataCheckOut()
{
	with(document.Form1)
	{
		CheckOut.value = CheckOut.value.trim();
		CheckOut.value = CheckOut.value.replace(/-/g,"/");
		CheckOut.value = CheckOut.value.replace(/\./g,"/");
		
		if(CheckOut.value.length > 0)
		{
			if(!verificaData3(CheckOut.value, GetNextDay(CheckIn.value)))
			{
				//CheckOut.focus();
				CheckOut.value = GetNextDay(CheckIn.value);
				CheckOut.focus();
			}	
			else
			{
				CalcularAlojamento();
				
				if(NrNoites.value < 1)
				{
					alert("A reserva deve ser de no mínimo 1 dia.");
					CheckOut.focus();
					return false;
				}
			}		
		}
		else
		{
		
			alert("Por favor, preencha o campo \"Check-Out\".");
			CheckIn.focus();
			return false;
		
			//CheckOut.value = "";
			//NrNoites.value = 0;
			//NrNoitesCell.innerText = 0;
			//ValorTotalCell.innerHTML = "0,00€";
			//TotalAlojamento = 0;
		}
	}
	
	return true;
}

function ChangeTipo()
{
	var capacidade = 0, SelIndex = 0, i = 0;
	
	with(document.Form1)
	{
		capacidade = TipoApartamento.options[TipoApartamento.selectedIndex].capacidade;
		
		SelIndex = num_adultos.selectedIndex;
		if(SelIndex == -1) SelIndex = 0;
		num_adultos.length = capacidade;
		
		for(i = 0; i < num_adultos.length; i++)
		{
			num_adultos.options[i].text = i + 1;
			num_adultos.options[i].value = i + 1;
		}
		
		if(SelIndex < num_adultos.length)
			num_adultos.selectedIndex = SelIndex;
		else
			num_adultos.selectedIndex = num_adultos.length - 1;
	}
	
	CheckCriancas();
	CheckBercos();
}


function CheckCriancas()
{
	var i = 0, SelIndex = 0, capacidade = 0;
	with(document.Form1)
	{
		capacidade = TipoApartamento.options[TipoApartamento.selectedIndex].capacidade;
	
		SelIndex = num_criancas.selectedIndex;
		if(SelIndex == -1) SelIndex = 0;
		num_criancas.length = capacidade - num_adultos.value + 1;
		
		for(i = 0; i < num_criancas.length; i++)
		{
			num_criancas.options[i].text = i;
			num_criancas.options[i].value = i;
		}
		
		if(SelIndex < num_criancas.length)
			num_criancas.selectedIndex = SelIndex;
		else
			num_criancas.selectedIndex = num_criancas.length - 1;
	}
}

function CheckBercos()
{
	var i = 0, SelIndex = 0, bercos = 0;
	with(document.Form1)
	{
		bercos = TipoApartamento.options[TipoApartamento.selectedIndex].bercos;
	
		SelIndex = num_bercos.selectedIndex;
		if(SelIndex == -1) SelIndex = 0;
		num_bercos.length = parseInt(bercos) + 1;
		
		for(i = 0; i < num_bercos.length; i++)
		{
			num_bercos.options[i].text = i;
			num_bercos.options[i].value = i;
		}
		
		if(SelIndex < num_bercos.length)
			num_bercos.selectedIndex = SelIndex;
		else
			num_bercos.selectedIndex = num_bercos.length - 1;
	}
}