// Example provided by Martin Honnen
// A function that sets cookie values properly
// The cookieName and cookieValue arguments are mandatory
// but all other arguments are optional.
// The maxAge argument is a maximum age to be set for cookie.
// The path defines the part of the document tree on the server
// that the cookie is valid for.
// The domain argument allows multiple server hosts to be used.
// The secure value is boolean and only applicable for use
// with HTTPS: connections.
function setCookieToCache(cookieName, cookieValue, maxAge, path, domain, secure)
{
	var cookieStr = escape(cookieName) + '=' + escape(cookieValue);
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		var today = new Date();
		var expires = today.getTime() + maxAge*1000;
		var expiresDate = new Date();
		expiresDate.setTime(expires);
		cookieStr += (maxAge ? '; expires=' + expiresDate.toGMTString() : '')
	}
	else
	{
		cookieStr += (maxAge ? '; max-age=' + maxAge : '');
	}
	cookieStr += (path ? '; path=' + path : '')
	+ (domain ? '; domain=' + domain : '')
	+ (secure ? '; secure' : '');

	document.cookie = cookieStr;
}

// A complementary function to unwrap a cookie.
function getCookieFromCache(cookieName)
{
	var cookieValue = null;
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1)
	{
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1)
		{
			cookieValue = unescape(document.cookie.substring(posValue, endPos));
		}
		else
		{
			cookieValue = unescape(document.cookie.substring(posValue));
		}
	}
	return cookieValue;
}

function displayRating(ratingText)
{
	var HTMLText = '';
	if(ratingText!='')
	{
		var ratings = ratingText.split('/');
		for(i=0;i<ratings.length;i++)
		{
			var rating = '';
			var ratingDisplay = ''
			if(ratings[i].indexOf('.') == -1)
			{
				rating += ratings[i];
				ratingDisplay += rating; 
			}
			else
			{
				rating += ratings[i].substring(0,ratings[i].indexOf('.')) + ratings[i].substr(ratings[i].indexOf('.') + 1)
				ratingDisplay += ratings[i].substring(0,ratings[i].indexOf('.')) + ' and a half';
			}
			HTMLText += '<p class="rating ourRate' + rating + '"><span>Our rating ' + ratingDisplay + '</span></p>';
		}
	}
	
	return HTMLText;
}

function getCountry(countryCode)
{
	for(var i=0; i<cList.length; i++) 
	{
		var cListMap = cList[i].split(",");
		if(cListMap[0]==countryCode)
		{
			return cListMap[2];
		}
	}
	return "";
}

function getDay(month, day, year) {

	var my_day=new Date(month + '/' + day + '/' + year)

	var day_name=new Array(7);
	day_name[0]="Sun"
	day_name[1]="Mon"
	day_name[2]="Tue"
	day_name[3]="Wed"
	day_name[4]="Thu"
	day_name[5]="Fri"
	day_name[6]="Sat"

	return day_name[my_day.getDay()];
}

function Map()
{
	this.entries = new Array();
	
	this.addEntry = function(key, value){
		// Get the values already mapped to the key.
		var values = this.getValues(key);
		
		for(var i=0;i<values.length;i++)
		{
			// If value already mapped to the key, do nothing.
			if(values[i]==value)
				return;
		}
		
		// New entry, add.
		this.entries.push({"key":key,"value":value})
	};
	
	this.getValues = function(key){
		var returnArray = new Array(); 
		for(var i=0;i<this.entries.length;i++){
			if(this.entries[i].key == key){
				returnArray.push(this.entries[i].value);
			}
		}
		return returnArray;
	};
}

