function SearchResultsCacheManager()
{
	this.cookieName = 'searchResults';
	this.cookieValue = getCookieFromCache(this.cookieName);
	this.searchResultsCookie = null;
	this.MAX_SEARCHRESULTS = 3;
	
	if(this.cookieValue == null)
	{
		this.searchResultsCookie = new Array(0);
	}
	else
	{
		this.searchResultsCookie = eval(this.cookieValue);
	}
	
	this.addSearchResult = addSearchResult;
	this.render = renderSearchResults;
}

function addSearchResult(searchResult)
{
	for(var j=0;j<this.searchResultsCookie.length;j++)
	{
		if(searchResult.searchURL==this.searchResultsCookie[j].searchURL)
		{
			for(var k=j;k<this.searchResultsCookie.length - 1;k++)
			{
				this.searchResultsCookie[k] = this.searchResultsCookie[k+1];
			}
			
			this.searchResultsCookie[this.searchResultsCookie.length - 1] = searchResult;
			
			this.cookieValue = serializeSearchResultsCookie(this.searchResultsCookie);
			
			return;
		}
	}
	
	if(this.searchResultsCookie.length == this.MAX_SEARCHRESULTS)
	{
		this.searchResultsCookie.shift();
	}
	
	this.searchResultsCookie.length += 1;
	
	this.searchResultsCookie[this.searchResultsCookie.length - 1] = searchResult;
	
	this.cookieValue = serializeSearchResultsCookie(this.searchResultsCookie);
	
	//Temporary Fix for the case when new search criteria is not cached when cookie value is exhaustively long:
	var tempSearchResults = getCookieFromCache(this.cookieName);
	if(tempSearchResults!=this.cookieValue)
	{
		this.searchResultsCookie.length -= 1;
		this.searchResultsCookie.shift();
		this.addSearchResult(searchResult);
	}
}

function renderSearchResults()
{
	var HTMLText = '';
	for(var i=this.searchResultsCookie.length - 1;i>=0;i--)
	{
		HTMLText += this.searchResultsCookie[i].render();
	}
	
	return HTMLText;
}

/**
The SearchResult class is an encapuslation of all the data we would lke to store in a cookie so that the recent
searches can be stored be displayed to the user.
**/
function SearchResult(imgURL, searchURL, departureDate, departureAirport, countryID, countryName, resortID, resortName, accomID, accomName, duration, paxData)
{
	this.imgURL = imgURL;
	this.searchURL = searchURL;
	this.departureDate = departureDate;
	this.departureAirport = departureAirport;
	this.countryID = countryID;
	this.countryName = countryName;
	this.resortID = resortID;
	this.resortName = resortName;
	this.accomID = accomID;
	this.accomName = accomName;
	this.duration = duration;
	this.paxData = paxData;
	
	this.getJSONNotation = getSearchResultJSONNotation;
	this.render = renderSearchResult; 
}

function getSearchResultJSONNotation()
{
	var jsonText = '';
	jsonText += 'new SearchResult(';
	jsonText += '"' + this.imgURL + '",';
	jsonText += '"' + this.searchURL + '",';
	jsonText += '"' + this.departureDate + '",';
	jsonText += '"' + this.departureAirport + '",';
	jsonText += '"' + this.countryID + '",';
	jsonText += '"' + this.countryName + '",';
	jsonText += '"' + this.resortID + '",';
	jsonText += '"' + this.resortName + '",';
	jsonText += '"' + this.accomID + '",';
	jsonText += '"' + this.accomName + '",';
	jsonText += '"' + this.duration + '",';
	jsonText += 'new PAXData(' + this.paxData.noOfAdults + ',' + this.paxData.noOfChildren + ',' + this.paxData.noOfInfants + ')';
	jsonText += ')';
		/**
		jsonText += 'imgURL:' +  + ',';
		jsonText += 'departureDate:' + this.departureDate + ',';
		jsonText += 'departureAirport:' + this.departureAirport + ',';
		jsonText += 'country:' + this.country + ',';
		jsonText += 'resort:' + this.resort + ',';
		jsonText += 'duration:' + this.duration + ',';
		jsonText += 'paxData:' + this.paxData;
		**/
	
	return jsonText;
}

function renderSearchResult()
{
	var destination = '';
	if(this.accomName != '')
	{
		destination += this.accomName + ', in ' + this.resortName + ', ' + this.countryName; 
	}
	else if(this.resortName != ''){
		destination += this.resortName + ' in ' + this.countryName;
	}
	else{
		destination += 'any resort in ' + this.countryName;
	}
	if(this.duration=='Up to 11 nights')
	{
		this.duration='Up to 11 nights'
	}
	else
	{
	this.duration+=' nights'
	}
	
	var HTMLText = '';
	HTMLText += '<li>';
	HTMLText +=		'<div class="insideRecentSearch">';
	HTMLText +=			'<div class="image">';
	HTMLText +=				'<a href="' + encodeURI(this.searchURL) + '">';
	HTMLText +=					'<img alt="' + decodeURIComponent(this.accomName) + '" src="/dbimages' + this.imgURL + '" />';
	HTMLText +=				'</a>';
	HTMLText +=			'</div>';				
	HTMLText +=			'<div class="text">';
	HTMLText +=				'<p class="date">' + this.departureDate + '</p>';
	HTMLText +=				'<p class="route">';
	HTMLText +=					'<a href="' + encodeURI(this.searchURL) + '">';
	HTMLText +=						'<span class="flyingFrom">' + this.departureAirport + '</span> to ';
	HTMLText +=						'<span class="goingTo">' + decodeURIComponent(destination) + '</span>';
	HTMLText +=					'</a>';
	HTMLText +=				'</p>';
	HTMLText +=				'<p class="duration">' + this.duration + '</p>';
	HTMLText +=			'</div>';
	HTMLText +=		'</div>';
	HTMLText +=	'</li>';				

	return HTMLText;
}

function PAXData(noOfAdults, noOfChildren, noOfInfants)
{
	this.noOfAdults = noOfAdults;
	this.noOfChildren = noOfChildren;
	this.noOfInfants = noOfInfants;
}

function serializeSearchResultsCookie(searchResultsCookie)
{
	var cookieValue = '[' + searchResultsCookie[0].getJSONNotation();
	
	for(i=1;i<searchResultsCookie.length;i++)
	{
		cookieValue += ',';
		cookieValue += searchResultsCookie[i].getJSONNotation();
	}
	
	cookieValue += ']';
	
	setCookieToCache('searchResults',cookieValue,'','/');
	
	return cookieValue;
}

function setShowHideCookie(showHideElementId)
{
	if(document.getElementById(showHideElementId).className == "CollapsiblePanelOpen")
	{
		setCookieToCache(showHideElementId,"hide",'','/');
	}
	else
	{
		setCookieToCache(showHideElementId,"show",'','/');
	}
}

function showHideSearchPanel()
{
	if(new SearchResultsCacheManager().cookieValue==null)
	{
		document.getElementById("previousSearches").style.display = "none";
	}
	else
	{
		var showHideSearch = getCookieFromCache("showHideSearches"); 
		if(showHideSearch ==null || showHideSearch == "show")
		{
			document.getElementById("showHideSearches").className = "CollapsiblePanelOpen";
			document.getElementById("recentSearches").style.display = "block";
		}
		else
		{
			document.getElementById("showHideSearches").className = "CollapsiblePanelClosed";
			document.getElementById("recentSearches").style.display = "none";
		}
	}
}

function showHidePanels(id1, id2) {

	if (id1 != null && id2 != null) {

		document.getElementById(id1).style.display = "block";
		document.getElementById(id2).style.display = "none";
	}
}

function encodeParams()
{
	document.forms['frmSearch'].elements['resortName'].value = encodeURIComponent(decodeURIComponent(document.forms['frmSearch'].elements['resortName'].value));
	document.forms['frmSearch'].elements['accommodationName'].value = encodeURIComponent(decodeURIComponent(document.forms['frmSearch'].elements['accommodationName'].value));
}