//Property Selector Controller

var DEFAULT_MIN_VALUE = 1;

function countryManager(jvarValue)
{
	setCitySELECT('pf_City', jsCities, jvarValue);
	reservationManager('reservationForm', jsProperties, '-1', jsApartTypes);

	showHideForReservation(0);
	
	if(jvarValue == '-1')
	{
		setPropertySELECT('pf_Property', jsProperties, '-1');
	}
	else
	{
		setPropertySELECT('pf_Property', jsProperties, '0', jvarValue);		
	}
}
function cityManager(jvarValue)
{
	var countryIdSelected = document.getElementById('pf_Country').value;
	reservationManager('reservationForm', jsProperties, '-1', jsApartTypes);
	
	showHideForReservation(0);
	
	if(jvarValue == '-1')
	{
		if( countryIdSelected == '-1')
		{
			setCitySELECT('pf_City', jsCities, '-1');			
		}
		else
		{
			setCitySELECT('pf_City', jsCities, countryIdSelected);
			setSpecifyCountrySELECT('pf_Country', countryIdSelected);
			setPropertySELECT('pf_Property', jsProperties, '0', countryIdSelected);	
		}
	}
	else
	{
		setSpecifyCountrySELECT('pf_Country', jsCities.getCountryId(jvarValue));
		setCitySELECT('pf_City', jsCities, document.getElementById('pf_Country').value);		
		setSpecifyCitySELECT('pf_City', jvarValue);
		setPropertySELECT('pf_Property', jsProperties, jvarValue);
	}
}
function propertyManager(jvarValue, jvarLanguageAlias)
{
	
	var countryIdSelected = document.getElementById('pf_Country').value;
	var cityIdSelected = document.getElementById('pf_City').value;	
	
	// check propertyFinder.js, reservationManager() for the property status.
	var BOOKABLE 	               = 0; // citadines property that allow booking
	var NOT_BOOKABLE               = 1; // citadines property that don't allow booking
	var NON_CITADINES_NOT_BOOKABLE = 2; // non-citadines, does not allow booking
	var NON_CITADINES_BOOKABLE     = 3; // non-citadines, allow booking	
	
	if(jvarValue == '-1')
	{
		setPropertySELECT('pf_Property', jsProperties, cityIdSelected);
		reservationManager('reservationForm', jsProperties, jvarValue, jsApartTypes);
		
		showHideForReservation(0);
	}
	else
	{
		if(countryIdSelected == '-1' && cityIdSelected == '-1')
		{
			setSpecifyCountrySELECT('pf_Country', jsProperties.getCountryId(jvarValue));
			setCitySELECT('pf_City', jsCities, jsProperties.getCountryId(jvarValue));
			setSpecifyCitySELECT('pf_City', jsProperties.getCityId(jvarValue));
			setPropertySELECT('pf_Property', jsProperties, jsProperties.getCityId(jvarValue));
		}
		else
		{
			setSpecifyCitySELECT('pf_City', jsProperties.getCityId(jvarValue));
			setSpecifyCountrySELECT('pf_Country', jsProperties.getCountryId(jvarValue));
		}
		
		setSpecifyPropertySELECT('pf_Property', jvarValue);
		
		var intStatus = reservationManager('reservationForm', jsProperties, jvarValue, jsApartTypes);
				
		if(intStatus == BOOKABLE)// check propertyFinder.js, reservationManager() for the property status.
		{
			var selectedNights = document.getElementById('res_Nights').value;			
			setApartGroupSelect('res_ApartmentType', jsApartTypes, jsProperties.getApartTypeCode(jvarValue));
			document.getElementById('res_Property').value = jsProperties.getApartTypeCode(jvarValue);
			document.getElementById('reserveForm').action = "https://"+location.hostname+jsProperties.getURL(jvarValue);
			
			document.getElementById('PropertyCountryID').value = jsProperties.getCountryId(jvarValue)
			document.getElementById('PropertyCityID').value = jsProperties.getCityId(jvarValue);
			
			//marvin
			/*if(jsProperties.getMinStay(jvarValue) !=0){
			if(parseInt(document.getElementById('res_Nights').value) < parseInt(jsProperties.getMinStay(jvarValue)))
			document.getElementById('res_Nights').value = jsProperties.getMinStay(jvarValue);}
			else
			{
			if(!oldParams.res_Arrival_DD) // if the page load without user picking a date
			document.getElementById('res_Nights').value = DEFAULT_MIN_VALUE;}
			
			checkFieldsWithNights('reserveForm');
			
			document.getElementById('pptyClosestHBS').value=jsProperties.getClosestHBS(jvarValue);*/
			//end marvin
			
			
			//marvin
			if(jsProperties.getMinStay(jvarValue) !=0){
			if(parseInt(document.getElementById('res_Nights').value) < parseInt(jsProperties.getMinStay(jvarValue)))
			document.getElementById('res_Nights').value = jsProperties.getMinStay(jvarValue);}
			else
			{
			if(!oldParams.res_Arrival_DD) // if the page load without user picking a date
			document.getElementById('res_Nights').value = DEFAULT_MIN_VALUE;}
			
			checkFieldsWithNights('reserveForm');
			
			document.getElementById('pptyClosestHBS').value=jsProperties.getClosestHBS(jvarValue);
			//end marvin
			
			
			//anthony
			//if the current selected # of night is > default minimum date then selected # of night is used 
			/*
			if(jsProperties.getMinStay(jvarValue) !=0){
				if(selectedNights > jsProperties.getMinStay(jvarValue)) {
					document.getElementById('res_Nights').value = selectedNights;
				}
				else {
					document.getElementById('res_Nights').value = jsProperties.getMinStay(jvarValue);
					
				}
			}
			else {
				document.getElementById('res_Nights').value = DEFAULT_MIN_VALUE;	
			}
			
			
			
			checkFieldsWithNights('reserveForm');
			document.getElementById('pptyClosestHBS').value=jsProperties.getClosestHBS(jvarValue);	
			*/
			//end anthony
			
			document.getElementById('PropertyDocID').value = jvarValue;	
			showHideForReservation(1);
			
			var currentURL = document.getElementById('cancelLink').href;
			document.getElementById('cancelLink').href = currentURL +"&webResCode="+ jsProperties.getApartTypeCode(jvarValue);
			
			document.getElementById('res_Units').disabled           = false;
			document.getElementById('res_Adults').disabled          = false;
			document.getElementById('res_Children').disabled        = false;
			document.getElementById('res_ApartmentType').disabled   = false;			
						
			document.getElementById('cellApartType').style.display  = "";
			document.getElementById('cellGuestCount').style.display = "";			
		}
		else if(intStatus == NOT_BOOKABLE)// check propertyFinder.js, reservationManager() for the property status.
		{
			showHideForReservation(2, jvarValue);		
		}
		else if(intStatus == NON_CITADINES_NOT_BOOKABLE)// check propertyFinder.js, reservationManager() for the property status.
		{
			showHideForReservation(2, jvarValue, jvarLanguageAlias);		
		}
		else if(intStatus == NON_CITADINES_BOOKABLE)// check propertyFinder.js, reservationManager() for the property status.
		{
			setApartGroupSelect('res_ApartmentType', jsApartTypes, jsProperties.getApartTypeCode(jvarValue));
			document.getElementById('res_Property').value = jsProperties.getApartTypeCode(jvarValue);			
			
			var citadinesURL = jsProperties.getURL(jvarValue);
			var stringStart  = citadinesURL.indexOf("://") + 3;
			var stringEnd    = citadinesURL.length;
			var newURL       = "https://" + citadinesURL.substring(stringStart,stringEnd);			
			var selectedNights = document.getElementById('res_Nights').value;	
			
			document.reserveForm.action = newURL;//jsProperties.getURL(jvarValue);
			
			document.getElementById('PropertyCountryID').value      = jsProperties.getCountryId(jvarValue)
			document.getElementById('PropertyCityID').value         = jsProperties.getCityId(jvarValue);
			
			//marvin
			/* 
			if(jsProperties.getMinStay(jvarValue) !=0){
			if(parseInt(document.getElementById('res_Nights').value) < parseInt(jsProperties.getMinStay(jvarValue)))
			document.getElementById('res_Nights').value = jsProperties.getMinStay(jvarValue);}
			else
			{
			if(!oldParams.res_Arrival_DD) // if the page load without user picking a date
			document.getElementById('res_Nights').value = DEFAULT_MIN_VALUE;}
			
		
			checkFieldsWithNights('reserveForm');
			
			document.getElementById('pptyClosestHBS').value=jsProperties.getClosestHBS(jvarValue); 
			*/
			//end marvin
			
			//anthony
			//if the current selected # of night is > default minimum date then selected # of night is used 
			if(jsProperties.getMinStay(jvarValue) !=0){
				if(selectedNights > jsProperties.getMinStay(jvarValue)) {
					document.getElementById('res_Nights').value = selectedNights;
				}
				else {
					document.getElementById('res_Nights').value = jsProperties.getMinStay(jvarValue);
					
				}
			}
			else {
				document.getElementById('res_Nights').value = DEFAULT_MIN_VALUE;	
			}
			
			
			checkFieldsWithNights('reserveForm');
			document.getElementById('pptyClosestHBS').value=jsProperties.getClosestHBS(jvarValue);	
			//end anthony
			
			/*Changed values to TRUE - Anthony for ASCOTT & SOMERSET*/
			document.getElementById('PropertyDocID').value          = jvarValue;		
			document.getElementById('res_Units').disabled           = false;
			document.getElementById('res_Adults').disabled          = false;
			document.getElementById('res_Children').disabled        = false;
			document.getElementById('res_ApartmentType').disabled   = false;			
						
			document.getElementById('cellApartType').style.display  = "none";
			document.getElementById('cellGuestCount').style.display = "none";
			
			showHideForReservation(3);		
		}
	}
}


function showHideForReservation(intStatus, propId, strLangAlias)
{	
	var DISABLE_BOOKING            = 0; // Form disabled.
	var OPEN_BOOKING 	           = 1; // Citadines property that is open for booking.
	var NOT_FOR_BOOKING            = 2; // Property not open for booking yet regardless brand.
	var OPEN_BOOKING_NON_CITADINES = 3; // Non-Citadines property open for booking.
	
	var reservationFormDIV = document.getElementById('reservationForm');
	var viewCancelDIV      = document.getElementById('viewCancel'); //For citadines
	var amendCancelDIV     = document.getElementById('amendCancel'); // For Ascott/Somerset
	var notForBookingDIV   = document.getElementById('notForBooking');
	var ApartTypeCELL      = document.getElementById('cellApartType');
	var guestCountCELL     = document.getElementById('cellGuestCount');
	
	var propNameSPAN = document.getElementById('propName');
	var goToURL = document.getElementById("propertyFinderForm").action;
	
	var nonCitadinesDIV = document.getElementById("nonCitadines");
	var ncBrand =  document.getElementById("ncBrand");
	var ncPropName = document.getElementById("ncPropName");
	var ncURL = document.getElementById("ncURL");
	
	if(intStatus == DISABLE_BOOKING)
	{
		toggleDIVs("block", "block", "none", "none", "none", "", ""); // "" is the same as Block, so it's compatible with IE and firefox for rendering dynamic TR			
	}
	else if(intStatus == OPEN_BOOKING)
	{
		toggleDIVs("block", "block", "none", "none", "none", "none", "none");
	}
	else if(intStatus == NOT_FOR_BOOKING)
	{			
		toggleDIVs("none", "none", "block", "none", "none", "none", "none");
		
		propNameSPAN.innerHTML = "<a href='#' onclick=\""+ goToURL +"\">"+ jsProperties.getPropertyName(propId);
		propNameSPAN.innerHTML += "</a>";
	}
	else if(intStatus == OPEN_BOOKING_NON_CITADINES)
	{
		toggleDIVs("block", "none", "none", "none", "block", "block", "block");
	}

	
	function toggleDIVs(divReservationForm, divViewCancel, divNotForBooking, divNonCitadines, divAmendCancel, cellApartType, cellGuestCount)
	{
		try
		{		
			reservationFormDIV.style.display = divReservationForm;
			viewCancelDIV.style.display 	 = divViewCancel; //requested link to be permanent
			notForBookingDIV.style.display	 = divNotForBooking;
			nonCitadinesDIV.style.display	 = divNonCitadines;
			amendCancelDIV.style.display 	 = divAmendCancel;
			ApartTypeCELL.style.display 	 = cellApartType;
			guestCountCELL.style.display 	 = cellGuestCount;
		}
		catch(e){}
	}
}
function buildTag()
{
	var aryTagList = new Array();
	
	this.add = add;
	this.build = build;
	this.getFirstTagID = getFirstTagID;
	
	function add(jvarSortOrder, jvarTagName, jvarTagID, jvarOnClick, jvarURL)
	{
		if(jvarSortOrder > 0)
		{
			aryTagList.push(new Array(jvarSortOrder, jvarTagName, jvarTagID, jvarOnClick, jvarURL));
		}
	}
	
	function build()
	{
		aryTagList = bubbleSort(aryTagList);
		
		var strLI = "";		
		
		for(var i = 0; i < aryTagList.length; i++)
		{		
			var strHead = "<li class=\"";
			var strMiddle = "";
			var strEnd = "\"><a href=\""+ aryTagList[i][4] +"\" onclick=\""+ aryTagList[i][3]  +"\" id=\""+ aryTagList[i][2] +"\"><table class=\"tabTable\"><tr><td>"+ aryTagList[i][1] +"</td></tr></table></a></li>\n";
			
			if(i == 0)
			{
				strMiddle = "first";
			}
			else if(i == aryTagList.length)
			{
				strMiddle = "last";
			}			
			strLI = strLI + strHead + strMiddle + strEnd;
		}
		
		return strLI;
	}
	function getFirstTagID()
	{
		return aryTagList[0][2];
	}	
}
