var MASTHEAD_COOKIE_NAME = "masthead";
var MASTHEAD_TIL	= "til";
var MASTHEAD_ES 	= "es";

function resetStars() {
	for (var i=1; i <= 5; i++) {
		doStar(i, 'http://i.thisislondon.co.uk/i/std/siteimages/stars/starOff.gif');
	}
}
function resetAll() {
	resetStars();
}

function lightStars(rating) {
	resetStars();
	for (var i=1; i<= rating; i++) {
		doStar(i, 'http://i.thisislondon.co.uk/i/std/siteimages/stars/starOn.gif');
		document.forms.addRating.rating.value='' + i;
	}
}

function doStar(elId, image) {
	elId = 's' + elId; 
	document.getElementById(elId).src='' + image;
}

function submitRatingForm() {
	document.forms.addRating.submit();
}

function setItemRating(itemType,itemId) {
	var cookie = getCookie(itemType+"UScores");
	userRating = 0;
	if (cookie!=null) {
		var split = cookie.split("~");
		for (var i=1;i<split.length;i++){
			var text = split[i];
			var id = text.substring(0,text.indexOf("/"));
			var rating = text.substring(text.indexOf("/")+1);
			if (id==itemId) {
				userRating=rating;
				break;
			}
		}
	}
	document.getElementById('itemRating').value = userRating ;
}

function showDiv(boxid){ 
   document.getElementById(boxid).style.display="block"; 
} 

function hideDiv(boxid){ 
   document.getElementById(boxid).style.display="none"; 
}
function toggleDiv(show, hide) {
	showBox = document.getElementById(show);
	hideBox = document.getElementById(hide);
	
	if (hideBox.style.display == "block") {
		showDiv(show);
		hideDiv(hide);
	}
	else {
		showDiv(hide);
		hideDiv(show);
	}
}

function showBut() {
	document.getElementById('hide').style.display='none';
    document.getElementById('show').style.display='inline';
}
function hideBut() {
	document.getElementById('hide').style.display='inline';
    document.getElementById('show').style.display='none';
}

function functionFalse() {
	
}

function setCookie(name,value,expires,path,domain,secure){
	var curCookie=name+"="+escape(value)+
		((expires)?"; expires="+expires.toGMTString():"")+
		((path)?"; path="+path:"")+
		((domain)?"; domain="+domain:"")+
		((secure)?"; secure":"");
	document.cookie=curCookie;
}

function getCookie(name){
	var dc=document.cookie;
	var prefix=name+"=";
	var begin=dc.indexOf("; "+prefix);
	if(begin==-1){
		begin=dc.indexOf(prefix);
		if(begin!=0)return null;
	}
	else
		begin+=2;
	var end=document.cookie.indexOf(";",begin);
	if(end==-1)
		end=dc.length;
	return unescape(dc.substring(begin+prefix.length,end));
}

function deleteCookie(name,path,domain){
	if(getCookie(name)){
		document.cookie=name+"="+
		((path)?"; path="+path:"")+
		((domain)?"; domain="+domain:"")+
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function addCookieEnabledTest(name,value,path,domain){
	if(!getCookie(name)) {
		var curCookie=name+"="+escape(value)+
			((path)?"; path="+path:"")+
			((domain)?"; domain="+domain:"");
		document.cookie=curCookie;
	}
}

/* Only creates cookie if it doesn't already exist. 
 * @param masthead value should be "es" or "til"
 */
function initMastheadCookie(masthead, domain) {
	var existingCookie = getCookie(MASTHEAD_COOKIE_NAME);
	if (existingCookie == null) {
		/* name,value,expires,path,domain,secure */
		setCookie(MASTHEAD_COOKIE_NAME, masthead, null, "/", domain);
	}
}

/* Toggle div if required */
function handleEsMasthead(esDivId, tilDivId) {
	if (getCookie(MASTHEAD_COOKIE_NAME) == MASTHEAD_TIL) {
		hideDiv(esDivId);
		showDiv(tilDivId);		
	}
}

// Popup function -->
function PopUp(win_name,URL,width,height,scrollbars,menubar){
  if (scrollbars==null){
     scrollbars=0;
  }
  if (menubar==null){
     menubar="no"
  }
    open( URL,win_name, "width="+width+",height="+height+",status=0,toolbar=0,menubar="+menubar+",resizable=no,location=0,scrollbars="+scrollbars+"");
}


// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this notice.
// SETUPS:
// ===============================
// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows
var AutoClose = true;
// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width=500,height=500,left=0,top=0'; // Set these value approximately 20 pixels greater than the size of the largest image to be used (needed for Netscape)
var optIE='scrollbars=no,width=150,height=100,left=0,top=0';
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE,isSafari,width,height;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');
writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');
writeln('if (isIE){');
writeln('window.resizeTo(300,300);');
writeln('width=300-(document.body.clientWidth-document.images[0].width);');
writeln('height=300-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);');
writeln('}');
writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;');
writeln('width = window.outerWidth;');
writeln('height = window.outerHeight;');
writeln('}');
writeln('window.moveTo((screen.width-width)/2,(screen.height-height)/2);');
writeln('}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus();" onblur="self.close();">');
writeln('<img name="George" src="'+imageURL+'" style="display:block" onclick="self.close();"></body></html>');
close();		
}}


//lucky seven functions

function luckySeven(ti, te, im, ur) {
	var title = ti;
	var bookingUrl = ur;
	var text = te;	
	var image = im;
	
	this.DisplayAdvert = DisplayAdvert;
	this.DisplayHolidayAdvert = DisplayHolidayAdvert;
	
	function DisplayAdvert(i) {
		document.write('<div id=\"lucky'+i+'\"></div>');
		var str = '<span class=\"luckyTitle\">'+ title.replace('\\\'','\'') +'</span>';
		str += '<img src=\"'+ image +'\" width=\"107\" height=\"62\" border=\"0\" alt=\"'+ title +'\" class=\"lucky7\" />';
		str += '<div class=\"luckyMore\">';
		if(bookingUrl!='#') {
			str += '<a href=\"'+ bookingUrl +'\">';
		}
		str += '<img onmouseover="this.T_TITLE=\''+ title +'\';return escape(\''+ text +'\')\" src=\"http://i.thisislondon.co.uk/i/std/siteimages/lucky7/more_info.gif\" width=\"40\" height=\"62\" border=\"0\" alt=\"\" />';
		if(bookingUrl!='#') {
			str += '</a>';
		}
		str += '</div>';
		str += '<div class=\"clear\">&nbsp;</div>';
		str += '<span class=\"dotted luckyLink\">';
		if(bookingUrl!='#') {
			str+='<a href=\"'+ bookingUrl +'\" class=\"green\" style=\"font-size:10px;\">Bookings / reviews</a>';
		} else {
			str+='&nbsp';
		}
		str+='</span>';
		document.getElementById('lucky'+i).innerHTML = str;
	}
	
	function DisplayHolidayAdvert(i) {
		var str = '<li><a href=\"' + bookingUrl +'\"><img src=\"'+ image +'\" border=\"0\" width=\"151px\" alt=\"' + title + '\" title=\"' + title + '\" /></a></li>';
		//alert(str);
		document.write(str);
	}
}

function writeAds() {
	for(var i = 0; i < totalAds; i++) {
		ads[i].DisplayAdvert(i);
	}
}

function writeHolidayAds() {
	var str = '<div id=\"travel_ads\"><img src=\"http://i.thisislondon.co.uk/i/std/cep/travel_watch.gif\" alt=\"Travel Watch\" /><ul>';
	//alert(str);
	document.write(str);
	for(var i = 0; i <= totalAds; i++) {
		ads[i].DisplayHolidayAdvert(i);
	}
	str = '</ul><img src=\"http://i.thisislondon.co.uk/i/std/cep/travel_ES_logo.gif\" alt=\"from the Evening Standard\" /></div><br clear=\"all\"/>';
	//alert(str);
	document.write(str);
}


function randOrd(){
	return (Math.round(Math.random())-0.5); 
}

var haveAds=false;

function refresh(milliseconds) {
	document.location.reload(true);
	self.setTimeout('refresh('+milliseconds+')', milliseconds); 
}

function utcDate(year,month,day) {
	var date = new Date();
	if (year!=null&&month!=null&&day!=null) {
		date.setUTCFullYear(year);
		date.setUTCMonth(month-1);
		date.setUTCDate(day);
	}
	date.setUTCHours(0);
	date.setUTCMinutes(0);
	date.setUTCSeconds(0);
	date.setUTCMilliseconds(0);
	return date;
}
function dayRemain(year,month,day) {
	var tRef = utcDate(year,month,day);
	var tNow = utcDate();
	var diff = (tRef - tNow)/86400000;
	return (diff<0)?0:diff;
}
function formatDigitRecursive(n, format, digitCount) {
	var strN=n.toString();
	if (digitCount!=null && digitCount>strN.length) strN=(n+Math.pow(10,digitCount)).toString().substr(1);
	var out = '';
	for (var i=0; i<strN.length; i++) {
		out+=format.replace(/\*/g,strN.charAt(i));
	}
	return out;
}

