window.onresize = SetThreeColHeight;


function CommonInit()
{

	SetThreeColHeight();
	if (window.screen.width <= 800)
	{
		document.getElementById("divLeftNav").style.display = "none";
	}

	//setup the treeview control if it exists
	if (document.getElementById("treeview"))
	{
		Setuptreeview("treeview");
		ExpandTree("public-home");
		ExpandTree("owners-home");

		//detect page and expand the tree to the correct item in the tree
		var currentPage = window.location.href;
		var arrPath = currentPage.split('/');
		if (arrPath.length > 0)
		{
			var currentFile = arrPath[arrPath.length - 1];
			currentFile = currentFile.replace(".aspx", "");

			if (document.getElementById(currentFile))
				ExpandTree(currentFile);
		}
	}
}

function SetThreeColHeight()
{
	var loColumn1 = document.getElementById("divLeftNav");
	var loColumn2 = document.getElementById("divContent");
	var loColumn3 = document.getElementById("divRightNav");
	

	if (loColumn1 != null)
	{
		var liCol1Height = loColumn1.offsetHeight;
		var liCol2Height = loColumn2.offsetHeight;
		var liCol3Height = loColumn3.offsetHeight;
		
		var liMaxHeight = loColumn2.offsetHeight;	//middle

		//determine column with the maximum height
		if (liCol1Height > liMaxHeight)
			liMaxHeight = loColumn1.offsetHeight;

		if (liCol3Height > liMaxHeight)
			liMaxHeight = loColumn3.offsetHeight;

		//apply maximum height to col1 and 3
		loColumn1.style.height = liMaxHeight + "px";
		loColumn3.style.height = liMaxHeight + "px";
	}
}


//GetElement and GetElements were copied from framecontent.js 
function GetElement(psId) 
{
	return document.getElementById(psId);
}

function GetElements(psId) 
{
	return document.getElementsByName(psId);
}
//  end of copied functions



function setStyle(poElement, psStyleClass)
{
	if(poElement)
		poElement.className = psStyleClass;
}

function MouseOverImage(poImg)
{
	var loImgCol = document.getElementsByName(poImg.name);

	if (loImgCol != null)
	{
		//set poImg[0] to image number 1 in collection
		try
		{
			loImgCol[0].src = loImgCol[1].src;
		}
		catch(e)
		{
			alert(poImg.name + ' does not contain three img tags');
		}
	}
}


function MouseOutImage(poImg)
{
	var loImgCol = document.getElementsByName(poImg.name);

	if (loImgCol != null)
	{
		//set poImg[0] to image number 2 in collection
		try
		{
			loImgCol[0].src = loImgCol[2].src;
		}
		catch(e)
		{
			alert(poImg.name + ' does not contain three img tags');
		}
	}
}


function OpenWindow( sURL, iHeight, iWidth, blnResizeable )
{
	//center window position
	var ScreenX = (screen.availWidth - iWidth)/2;
	var ScreenY = (screen.availHeight - iHeight)/2;

	var	objRet = window.open( sURL, '_blank', 'height=' + iHeight + ',width=' + iWidth + ',left=' + ScreenX + ',top=' + ScreenY + ',location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=' + (blnResizeable ? 'yes' : 'no'), true );
	objRet.focus();
	return objRet;
}

function OpenWindow2( sURL, iHeight, iWidth, blnResizeable, pbShowLocationBar )
{
	//center window position
	var ScreenX = (screen.availWidth - iWidth)/2;
	var ScreenY = (screen.availHeight - iHeight)/2;

	var	objRet = window.open( sURL, '_blank', 'height=' + iHeight + ',width=' + iWidth + ',left=' + ScreenX + ',top=' + ScreenY + ',location=' + (pbShowLocationBar ? 'yes' : 'no') + ',menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=' + (blnResizeable ? 'yes' : 'no'), true );
	objRet.focus();
	return objRet;
}



function OpenCalendarWindow()
{
	//center window position
	var lsUrl = '../calendar/calendar.htm';
	var liHeight = '205';
	var liWidth = '300';

	var ScreenX = (screen.availWidth - liWidth)/2;
	var ScreenY = (screen.availHeight - liHeight)/2;

	var objRet = OpenWindow2( lsUrl, liHeight, liWidth, true, true )
	//var	objRet = window.open( lsUrl, '_blank', 'height=' + liHeight + ',width=' + liWidth + ',left=' + ScreenX + ',top=' + ScreenY + ',location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=no', true );
	objRet.focus();
	return objRet;
}


function OpenAvailabilityWindow(psUrl)
{
	var liHeight = '250';
	var liWidth = '960';

	var ScreenX = (screen.availWidth - liWidth)/2;
	var ScreenY = (screen.availHeight - liHeight)/2;

	var	objRet = window.open( psUrl, '_blank', 'height=' + liHeight + ',width=' + liWidth + ',left=' + ScreenX + ',top=' + ScreenY + ',location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=no', true );
	objRet.focus();
}



function SearchFormCalendarDateChanged()
{
	//set date field value dd/mm/yyyy
	var loDateField = document.getElementById(window.__CalendarUpdateFieldName);
	loDateField.value = window.__CalendarDD + '/' + window.__CalendarMM + '/' + window.__CalendarYY;
	
	if (window.__CalendarUpdateFieldName == 'dtCheckInDate')
	{
		var CheckInDateField = document.getElementById("dtCheckInDate");
		var CheckOutDateField = document.getElementById("dtCheckOutDate");
		
		if (bIsValidDateDMY(CheckInDateField.value))
		{
			var checkInDate = ddMMyyyyToDate(CheckInDateField.value, '/');
			var checkOutDate = checkInDate.add('d', 1);
			
			CheckOutDateField.value = dateToString(checkOutDate);
		}
		
	}
	
	
}


function OpenNewRegistrationPopup()
{
	var lsURL = '../public/NewRegistrationPopup.aspx';
	var x = OpenWindow(lsURL, 320, 450, false);
}



function OpenReferAFriendPopup()
{
	var lsURL = '../public/ReferAFriend.aspx';
	var x = OpenWindow(lsURL, 320, 450, false);
}


function OpenOwnerTermsConditionsPopup()
{
	var lsURL = '../private/OwnerTermsConditionsPopup.aspx';
	var x = OpenWindow(lsURL, 320, 450, false);
}




function OpenEstablishmentRatesPopup(piEstablishmentId)
{
	var lsURL = '../public/EstablishmentRatesPopup.aspx?iEstablishmentId=' + piEstablishmentId.toString();
	var x = OpenWindow(lsURL, 320, 660, true);
}


function OpenForexCalculator()
{
	var lsURL = 'ForeignExchangePopup.aspx';
	var x = OpenWindow(lsURL, 580, 780, true);
}


function NoBudgetLimit_OnClick(pbChecked)
{
	var loMaxBudget = document.getElementById("iMaxBudget");
	
	if (pbChecked)
		loMaxBudget.value = "";
}


function MaxBudget_OnChange(poFrm)
{
	var loNoBudgetLimit = document.getElementById("vcNoBudgetLimit");
	loNoBudgetLimit.checked = false;
}



function ChangeSearchFormCriteria_OnClick()
{
	var loFrm = document.getElementById("frmCommonSearchForm");

	if (window.screen.width <= 800)	//shows search box on low res monitors
		window.scrollTo(window.screen.width, 1);
		
	loFrm.dtCheckInDate.select();
	loFrm.dtCheckInDate.focus();

}


function SearchForAllAreasInChosenArea()
{
	var loFrm = document.getElementById("frmCommonSearchForm");

	var loAccommodationTypeCheckboxes = document.getElementsByName("vcAccommodationTypeIds");
	for (var i in loAccommodationTypeCheckboxes)
	{
		loAccommodationTypeCheckboxes[i].checked = true;
	}

	var loNoBudgetLimitCheckbox = document.getElementById("vcNoBudgetLimit");
	loNoBudgetLimitCheckbox.checked = true;

	var loMaxBudgetAmt = document.getElementById("iMaxBudget");
	loMaxBudgetAmt.value = '';

	loFrm.submit();
}



var t;		// timer 

function ValidateCommonSearchForm(poFrm)
{
	//double click protection
	if (document.getElementById("timeOutVar").value == "x")
	{
		return false;
	}

	Screen.Form_error = false;
	Screen.Form_errorString = "";

	
	var ldtCheckInDate = new Date();
	var ldtCheckOutDate = new Date();
	var ldtCurrentDate = new Date();
	ldtCurrentDate.setHours(0,0,0,0);

	var loMaxBudget = document.getElementById("iMaxBudget");
	var loNoBudgetLimit = document.getElementById("vcNoBudgetLimit");
	var loEstablishmentName = document.getElementById("asbEstablishment");
	var loLocationName = document.getElementById("asbLocation");


	loEstablishmentName.value = loEstablishmentName.value.Trim();
	loLocationName.value = loLocationName.value.Trim();


	if ( (loEstablishmentName.value == '') && (loLocationName.value == '') )
	{
		alertFieldError(GetElement("asbLocation"), "This search will return more than a 1000 establishments, please enter an area/establishment name to reduce the result");
	}



	if (loNoBudgetLimit.checked)
	{
		loMaxBudget.value = "";
	}

	if (GetElement("dtCheckInDate"))
		verifyMandatoryDateField("dtCheckInDate", "check-in date");

	if ( GetElement("dtCheckOutDate") )
		verifyMandatoryDateField("dtCheckOutDate", "check-out date");


	// checkin date >= current date
	if ( bIsValidDateDMY(GetElement("dtCheckInDate").value) )
	{
		ldtCheckInDate = stringToDate(GetElement("dtCheckInDate").value);

		if (ldtCheckInDate < ldtCurrentDate)
			alertFieldError(GetElement("dtCheckInDate"), "The check-in date must be greater than today's date");
	}


	//check if checkout date > checkin date
	if ( bIsValidDateDMY(GetElement("dtCheckInDate").value) && bIsValidDateDMY(GetElement("dtCheckOutDate").value) )
	{
		ldtCheckInDate = stringToDate(GetElement("dtCheckInDate").value);
		ldtCheckOutDate =  stringToDate(GetElement("dtCheckOutDate").value);

		if (ldtCheckInDate > ldtCheckOutDate)
			alertFieldError(GetElement("dtCheckInDate"), "The check-in date cannot be greater than the check-out date");

		if (ldtCheckInDate.valueOf() == ldtCheckOutDate.valueOf() )
			alertFieldError(GetElement("dtCheckInDate"), "The check-in and check-out dates may not be the same day");
	}

	verifyMandatoryIntOver("iNoOfAdults", "number of adults", 0);
	//verifyInt("iNoOfChildren", "number of children");
	if (GetElement("iNoOfChildren"))
		verifyMandatoryIntOver("iNoOfChildren", "number of children", -1);


	var loNoChildren = document.getElementById("iNoOfChildren");
	if (isInt(loNoChildren.value))
	{
		var noChildren = parseInt(document.getElementById("iNoOfChildren").value);
		
		if (noChildren > 0)
		{
			//validate ages
			var loChildrenAges = document.getElementById("vcChildrenAges");
			var arrAges = loChildrenAges.value.split(',');
			
			if (arrAges.length != noChildren)
			{
				alertFieldError(loChildrenAges, "The number of children entered and the number of ages specified does not match");
			}
			else
			{
				//step through the array to check whether valid data has been entered
				var bValidAges = true;
				for (var i=0; i < arrAges.length; i++)
				{
					if (!isInt(arrAges[i].Trim()))
						bValidAges = false;
				}

				if (!bValidAges)
					alertFieldError(loChildrenAges, "Please enter the ages described in years, eg  1, 5  \nPlease convert months to years if you are entering details about a baby");
			}
		}
	}


	if (GetElement("iMaxBudget"))
		verifyMoney("iMaxBudget", "maximum budget amount");


	if (Screen.Form_error == true)
	{
		alert(Screen.Form_errorString);
		return false;
	}
	else
	{
		var loEstablishmentNameInput = document.getElementById("asbEstablishment");
		var loEstablishmentIdHidden = document.getElementById("asbEstablishment_SelectedValue");
		var loLocationNameInput = document.getElementById("asbLocation");
		var loLocationIdHidden = document.getElementById("asbLocation_SelectedValue");

		//clear the field values
		if (loEstablishmentNameInput.value.Trim() == "")
			loEstablishmentIdHidden.value = '';

		if (loLocationNameInput.value.Trim() == "")
			loLocationIdHidden.value = '';


		setCookie("CheckInDate", GetElement("dtCheckInDate").value)
		setCookie("CheckOutDate", GetElement("dtCheckOutDate").value)


		document.getElementById("timeOutVar").value = "x";
		window.setTimeout("ResetTimeout()", 2000);

		return true;	//true
	}
}


function ResetTimeout()
{
	window.clearTimeout(t);
	document.getElementById("timeOutVar").value = "";
}


function UncheckAllCheckBoxes(poCheckBoxes)
{
	for (var i=0; i < poCheckBoxes.length; i++)
		poCheckBoxes[i].checked = false;
}


if (typeof HTMLElement != "undefined" && typeof HTMLElement.prototype.__defineGetter__ != "undefined") 
{
	//emulate innertext property in non-ie browsers
	HTMLElement.prototype.__defineGetter__("innerText", function () 
		{
			var r = this.ownerDocument.createRange();
			r.selectNodeContents(this);

			var resultString = r.toString();
			resultString = resultString.replace(/\t/g, "");	//horizontal tab
			resultString = resultString.replace(/\n/g, "");	//newline
			resultString = resultString.replace(/\r/g, "");	//carriage return
			resultString = resultString.replace(/\f/g, "");	//form feed
			resultString = resultString.replace(/\b/g, "");	//backspace
			resultString = resultString.replace(/\v/g, "");	//vertical tab

			return resultString;
		}
	);

	HTMLElement.prototype.__defineSetter__("innerText", function (sText) 
		{
		this.innerHTML = sText.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
		}
	);
}



function OpenCertDetails()
{
	thewindow =window.open('https://www.thawte.com/cgi/server/certdetails.exe?code=ZASABOX1', 'anew',config='height=400,width=450,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=yes');
}



function ToggleCommonSearchAdvancedOptions()
{
	var divAdvOptionContainer = document.getElementById("divAdvancedOptionContainer");
	var divAdvOptionToggle = document.getElementById("divAdvOptionsToggle");
	
	if (divAdvOptionContainer.className == 'Hidden')
	{
		divAdvOptionContainer.className = 'Visible';
		divAdvOptionToggle.innerText = 'Hide advanced options';
		setCookie("AdvOptionContainerClassName", 'Visible')
	}
	else
	{
		divAdvOptionContainer.className = 'Hidden';
		divAdvOptionToggle.innerText = 'Show advanced options';
		setCookie("AdvOptionContainerClassName", 'Hidden')
	}
}
