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 VerificaEmail(obj) 
{
	var validChars, err01,err02, err03, err04, fieldValue, fieldLength;
	var cont = 0, cont2 = 0;
	var Limpar = false;
	
	validChars  = "abcdefghijklmnopqrstuvwxyz"; 
	validChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	validChars += "0123456789.@_-";
	fieldValue  = obj.value; 
	fieldLength = obj.value.length; 
	minLength   = 8; 
	maxLength   = 255; 

	err01   = "It was found an invalid character."; 
	err02   = "Please introduce at least " + minLength + " characters."; 
	err03   = "Please introduce less than " + maxLength + " characters."; 
 
	if ( fieldLength > 0)
	{
		if ( fieldLength < minLength ) 
		{ 
			alert( err02 ); 
			Limpar = true;
		}
		else if (( fieldLength > maxLength ) && ( maxLength > 0 )) 
		{ 
			alert( err03 ); 
			Limpar = true;
		}
		else if (( fieldValue.charAt( 0 ) == "@" ) || ( fieldValue.charAt( fieldLength - 1 ) == "@" )) 
		{
			alert("The Email is not valid!!!"); 
			Limpar = true;
		}
		else 
		{ 
			for( var i=0; i< fieldLength; i++ ) 
			{ 
				if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) 
				{ 
				    alert( err01 ); 
				    Limpar = true;
				    break; 
				}
				if (fieldValue.charAt( i ) == "@")
					cont++;
				if (fieldValue.charAt( i ) == ".")
					cont2++;
		    } 
			    
		    if (cont2 == 0 )
			{
				alert("The Email is not valid!!!"); 
				Limpar = true;
			}
			else if ( (cont == 0) || (cont > 1) )
		    {
				alert("OThe Email is not valid!!!"); 
				Limpar = true;		
			}
		} 
    } 
    
    if(Limpar) 
		obj.focus(); 
	else
	{
		with(document.Form1)
			SubmitCell.focus();
	}
}

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);	
		NrNoitesCell.innerText = Math.round(TotalDias);
		
	}
	
}

/*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;
		
		arrPrecos = ListaPrecosjs.split(";");
		if(arrPrecos.length > 1)
		{
			var DiasAux, i, j, Precosjs;
			
			DiasAux = TotalDias;
			ValorTotal = 0;
			
			while (DiasAux > 0)
			{
				i = 0;
			
				for(i = 0; i < arrPrecos.length; i++)
				{
					Precosjs = arrPrecos[i].split(",");
					
					if(Precosjs[0] == DiasAux)
					{
						ValorTotal += parseFloat(Precosjs[1]);
						DiasAux = 0;
						break;
					}
					else if(Precosjs[0] > DiasAux)
					{
						Precosjs = arrPrecos[i - 1].split(",");
						ValorTotal += parseFloat(Precosjs[1]);
						DiasAux -= parseInt(Precosjs[0]);
						break;
					}
				}
			}
		}
		else
			ValorTotal = TotalDias * arrPrecos[0].split(",")[1];
		
		NrNoites.value = TotalDias;	
		NrNoitesCell.innerText = TotalDias;
		TotalAlojamento = ValorTotal;
		ValorPropCell.innerHTML = FormatCurrency(ValorTotal.toString());
	}
	
	CalcularValorTotal();
}*/

function CalcularExtras()
{
	var objField, objFieldTimes, objValor;
	var i, valorTotal = 0;
	
	with(document.Form1)
	{
		if(NrTotalExtras.value > 0)
		{
			for(i = 0;i < NrTotalExtras.value;i++)
			{
				objField = eval("F" + i);
				objFieldTimes = eval("FNrVezes" + i);
				objValor = eval("FValor" + i);
				
				if (objField.checked)
					valorTotal += objValor.value * objFieldTimes.value;
			}
			TotalExtras = valorTotal;
			ValorExtrasCell.innerHTML = FormatCurrency(valorTotal.toString());
		}
		else
		{
			ValorExtrasCell.innerHTML = "0,00€";
			TotalExtras = 0;
		}
	}	
	CalcularValorTotal();
}

function CalcularRent()
{
	var valorTotal = 0;
	
	with(document.Form1)
	{
		valorTotal = CarroID.options[CarroID.selectedIndex].CarPrice;
		valorTotal = valorTotal * NrDiasRent.value;
		TotalRent = valorTotal;
		ValorRentCell.innerHTML = FormatCurrency(valorTotal.toString());
	}
	CalcularValorTotal();
}

function CalcularValorTotal()
{
	var valorTotal = 0;
	
	with(document.Form1)
	{
		ValorExtras.value = TotalExtras;
		ValorRent.value = TotalRent;
		
		valorTotal = Number(ValorAlojamento.value) + TotalExtras + TotalRent;
		TotalReserva.value = valorTotal;
		
		if(valorTotal > 0)
			ValorTotalCell.innerHTML = FormatCurrency(valorTotal.toString());
		else
			ValorTotalCell.innerHTML = "0,00€";
	}
}

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("At first, fill in the  \"Check-in\" date.");
			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,"/");
		
		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;
			ValorPropCell.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();
			else
				CalcularAlojamento();
		}
		else
		{
			CheckOut.value = "";
			NrNoites.value = 0;
			NrNoitesCell.innerText = 0;
			ValorPropCell.innerHTML = "0,00€";
			TotalAlojamento = 0;
		}
	}
}

function CheckExtra(ExtID)
{
	var objField, objFieldTimes;
	
	with(document.Form1)
	{
		objField = eval("F" + ExtID);
		objFieldTimes = eval("FNrVezes" + ExtID);
		
		if (objField.checked)
		{
			objFieldTimes.value = objFieldTimes.value.trim();
			
			if(objFieldTimes.value.length == 0)
				objFieldTimes.value = 1;
			else 
			{
				if(Testar_Numeros(objFieldTimes))
				{
					if(objFieldTimes.value < 1) objFieldTimes.value = 1;
				}
				else
					objFieldTimes.value = 1;
			}
		}		
		else
			objFieldTimes.value = "";
	}
	CalcularExtras();
}

function CheckRent()
{
	with(document.Form1)
	{
		NrDiasRent.value = NrDiasRent.value.trim();
		if(CarroID.value != "") 
		{
			if(NrDiasRent.value.length == 0)
				NrDiasRent.value = 1;
			else 
			{
				if(Testar_Numeros(NrDiasRent))
				{
					if(NrDiasRent.value < 1) NrDiasRent.value = 1;
				}
				else
					NrDiasRent.value = 1;
			}
			CalcularRent();
		}
		else
		{
			NrDiasRent.value = "";		
			ValorRentCell.innerHTML = "0,00€";
			TotalRent = 0;
		}
	}
}

function EscolheGrupo()
{
	var msel = document.Form1.GrupoID;
	var arrClasses, arrGrupos, arrDetalhes;
	var n,i,p;
	
	msel.options.length = 1;
	p = 1;

	msel.options.length = p;
	msel.options[p-1].value = "";
	msel.options[p-1].text = "--- Nenhum ---";
	msel.options[p-1].style.color = "#C0C0C0";
	p++;

	with(document.Form1)
	{
		if (ClasseID.value != "")
		{
			arrClasses = GroupListjs.split("|");
			for (i = 0;i <= arrClasses.length - 1;i++)
			{
				arrGrupos = arrClasses[i].split(";")
				for (n = 0; n < arrGrupos.length;n++)
				{
					arrDetalhes = arrGrupos[n].split(",")
					if (arrDetalhes[0] == ClasseID.value)
					{
						msel.options.length = p;
						msel.options[p-1].value = arrDetalhes[1];
						msel.options[p-1].text = arrDetalhes[2];
						p++;
					}
				}
			}
		}
	}
	EscolheCarro();
}

function EscolheCarro()
{
	var msel = document.Form1.CarroID;
	var arrGrupos, arrCarros, arrDetalhes;
	var n,i,p;
	
	msel.options.length = 1;
	p = 1;

	msel.options.length = p;
	msel.options[p-1].value = "";
	msel.options[p-1].text = "--- Nenhum ---";
	msel.options[p-1].style.color = "#C0C0C0";
	p++;

	with(document.Form1)
	{
		if (GrupoID.value != "")
		{
			arrGrupos = CarListjs.split("|");
			for (i = 0;i <= arrGrupos.length - 1;i++)
			{
				arrCarros = arrGrupos[i].split(";")
				for (n = 0; n < arrCarros.length;n++)
				{
					arrDetalhes = arrCarros[n].split(",")
					if (arrDetalhes[0] == GrupoID.value)
					{
						msel.options.length = p;
						msel.options[p-1].value = arrDetalhes[1];
						msel.options[p-1].text = arrDetalhes[2];
						msel.options[p-1].CarPrice = arrDetalhes[3];
						p++;
					}
				}
			}
		}
	}
	CheckRent();
}





function EscolheGrupoUK()
{
	var msel = document.Form1.GrupoID;
	var arrClasses, arrGrupos, arrDetalhes;
	var n,i,p;
	
	msel.options.length = 1;
	p = 1;

	msel.options.length = p;
	msel.options[p-1].value = "";
	msel.options[p-1].text = "--- None ---";
	msel.options[p-1].style.color = "#C0C0C0";
	p++;

	with(document.Form1)
	{
		if (ClasseID.value != "")
		{
			arrClasses = GroupListjs.split("|");
			for (i = 0;i <= arrClasses.length - 1;i++)
			{
				arrGrupos = arrClasses[i].split(";")
				for (n = 0; n < arrGrupos.length;n++)
				{
					arrDetalhes = arrGrupos[n].split(",")
					if (arrDetalhes[0] == ClasseID.value)
					{
						msel.options.length = p;
						msel.options[p-1].value = arrDetalhes[1];
						msel.options[p-1].text = arrDetalhes[2];
						p++;
					}
				}
			}
		}
	}
	EscolheCarroUK();
}

function EscolheCarroUK()
{
	var msel = document.Form1.CarroID;
	var arrGrupos, arrCarros, arrDetalhes;
	var n,i,p;
	
	msel.options.length = 1;
	p = 1;

	msel.options.length = p;
	msel.options[p-1].value = "";
	msel.options[p-1].text = "--- None ---";
	msel.options[p-1].style.color = "#C0C0C0";
	p++;

	with(document.Form1)
	{
		if (GrupoID.value != "")
		{
			arrGrupos = CarListjs.split("|");
			for (i = 0;i <= arrGrupos.length - 1;i++)
			{
				arrCarros = arrGrupos[i].split(";")
				for (n = 0; n < arrCarros.length;n++)
				{
					arrDetalhes = arrCarros[n].split(",")
					if (arrDetalhes[0] == GrupoID.value)
					{
						msel.options.length = p;
						msel.options[p-1].value = arrDetalhes[1];
						msel.options[p-1].text = arrDetalhes[2];
						msel.options[p-1].CarPrice = arrDetalhes[3];
						p++;
					}
				}
			}
		}
	}
	CheckRent();
}