function OpenWindow(windowLink, windowName, windowFeatures)
{	
	window.open(String(windowLink), String(windowName), String(windowFeatures));
}

function OpenWindowCalendar(windowLink, windowName, windowFeatures)
{	

	var params = "?";
	var fieldsArr = new Array("formCheckIn_year", "formCheckIn_month", "formCheckIn_day", "formNights");
	var fieldsValuesArr = new Array(document.getElementsByName("formCheckIn_year")[0].value, document.getElementsByName("formCheckIn_month")[0].value, document.getElementsByName("formCheckIn_day")[0].value, document.getElementsByName("formNights")[0].value);
	
	for (var i = 0; i < fieldsValuesArr.length; i++)
		params += ((i == 0) ? "": "&") + fieldsArr[i] + "=" + fieldsValuesArr[i];
	
	window.open(String(windowLink) + params, String(windowName), String(windowFeatures));
}

function SetBgColor(idElem, newColor)
{	
	document.getElementsByName(idElem)[0].bgColor = newColor;
}
	
function SwapImage(idElem, newImageSrc)
{
	document.getElementsByName(idElem)[0].src = newImageSrc;
}
	
function ValidateSubmitTextAux(inForm, value)
{	
	return (value != "" && value != "undefined");
}
	
function ValidateSubmitText(id, inForm, value)
{	
	if (ValidateSubmitTextAux(inForm, value))
	{
		inForm.submit();
		return true;
	}
	alert('Wrong search value.');
	document.getElementsByName(id)[0].value = '';
	document.getElementsByName(id)[0].focus();
	return false;	
}
	
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 ValidateEmailAndSubmit(id, inForm, value)
{
	if (VerifyEmail(value) && ValidateSubmitTextAux(inForm, value))
	{
		inForm.submit();
		return true;
	}
		
	alert('Wrong Email address.');
	document.getElementsByName(id)[0].value = '';
	document.getElementsByName(id)[0].focus();
	return false;
}

function ValidateEmail(id, inForm, value)
{
	if (VerifyEmail(value) && ValidateSubmitTextAux(inForm, value))
		return true;
		
	alert('Wrong Email address.');
	document.getElementsByName(id)[0].value = '';
	document.getElementsByName(id)[0].focus();
	return false;
}
	
function SetNewClass(idElem, className)
{	
	if (String(className.indexOf("Blur")) > 0)
		document.getElementsByName(idElem)[0].className = className.substring(0, className.indexOf("Blur"));
	else
		document.getElementsByName(idElem)[0].className = String(className + "Blur");
}
	
function SetAltText(inText, inPadding, altSpan, altTD)
{
	document.getElementsByName(altSpan)[0].innerHTML = inText;
	document.getElementsByName(altTD)[0].style.paddingLeft = inPadding;
}	

function TestIfRegularYear(year)
{	
	return !(((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0);
}
					
function GetDaysInMonth(month)
{
	var minDaysInMonth = 30;
	var maxDaysInMonth = 31;
						
	if (month < 8)
		return (minDaysInMonth + (month % 2));
	return (maxDaysInMonth - (month % 2));
}

function CheckDateAux(day, month, year) // determina se a data é ou não válida
{
	var control = (TestIfRegularYear(year) ? 28 : 29);
						
	if (month == 2)
		return (day <= control);
	return (day <= GetDaysInMonth(month));
}
					
function CheckDate(day, month, year)
{
	if (!CheckDateAux(day, month, year))
	{
		alert("Invalid date.");
		return false;
	}
	return true;
}

function SetElemAction(elemID, newAction)
{
	document.getElementsByName(elemID)[0].action = newAction;
}

function SetElemValue(elemID, newValue)
{
	document.getElementsByName(elemID)[0].value = newValue;
}

function SetImageSrc(id, newSrc)
{
	document.getElementsByName(id)[0].src = newSrc;
}

function GetElem(id)
{	
	return document.getElementById(id);
}

function GetElemValue(id)
{	
	return document.getElementById(id).value;
}

function GetNights()
{	
	var dayTime = 86400000;
	var date1 = new Date(GetElemValue('formCheckIn_year'), GetElemValue('formCheckIn_month') - 1, GetElemValue('formCheckIn_day'));
	var date2 = new Date(GetElemValue('formCheckOut_year'), GetElemValue('formCheckOut_month') - 1, GetElemValue('formCheckOut_day'));
						
	var auxNights = Math.round((date2 - date1) / dayTime);
						
	return (auxNights > 0) ? auxNights : 0;
}


function GetDateInterval()
{	
	var fieldsValuesArr = new Array(document.getElementsByName("formCheckIn_year")[0].value, document.getElementsByName("formCheckIn_month")[0].value, document.getElementsByName("formCheckIn_day")[0].value);
	var str = "SetDateInterval(" + fieldsValuesArr.toString() + ")";
				
	opener.window.setInterval(str, 0);
}
			
function SetDateInterval()
{	
	var fieldsArr = new Array(GetElem("formCheckIn_year"), GetElem("formCheckIn_month"), GetElem("formCheckIn_day"));
		
	for (var i = 0; i < Number(SetDateInterval.arguments.length); i++)
		fieldsArr[i].value = SetDateInterval.arguments[i];	
}

// Validações para os mapas de pesquisa e reserva de hotelaria
function Validation()
{	
	if (document.getElementsByName('regionToSearch')[0].value == "")
	{
		alert("Please choose a region in order to perform a valid search.");
		document.getElementsByName('regionToSearch')[0].focus();
		return false;
	}
	if (document.getElementsByName('formCity')[0].value == "")
	{
		alert("Please choose a city in order to perform a valid search.");
		document.getElementsByName('formCity')[0].focus();
		return false;
	}
	if (CheckDate(document.getElementsByName('formCheckIn_day')[0].value, document.getElementsByName('formCheckIn_month')[0].value, document.getElementsByName('formCheckIn_year')[0].value))		
	{
		document.getElementsByName('MapSearchForm')[0].action = "../loading.asp";
		document.getElementsByName('MapSearchForm')[0].submit();
	}
	else
		document.getElementsByName('formCheckIn_day')[0].focus();
	return false;
}

function ValidateFormFields(formV)
{
	var control = "passenger_";
						
	for (var i = 0; i < document.getElementsByName(formV)[0].length; i++)
	{
		if (String(document.getElementsByName(formV)[0][i].name).indexOf(control) >= 0 && (document.getElementsByName(formV)[0][i].value == '' || document.getElementsByName(formV)[0][i].value == 'undefined'))
		{
			alert("Error.\n\rSome fields are missing.");
			document.getElementsByName(document.getElementsByName(formV)[0][i].name)[0].focus();
			return false;
		}
	}	
	document.getElementsByName(formV)[0].submit();
}