// **********************************************************
// Common routine functions - getting Object references etc *
// COMMENTED AS NOW OBSOLETE
// **********************************************************

// Function to get an object given its id

function getId(id) {    
	if (document.getElementById && document.getElementById(id)) {
		return document.getElementById(id); } 
	
	else if (document.all && document.all(id)) {
	  return document.all(id); } 
	
	else if (document.layers && document.layers[id]) {
		return document.layers[id]; } 
	
	else { return false; }
}

/* Description : Adds class name to documentElement for Javascript availability  */
if (document.documentElement && document.getElementById) {
	document.documentElement.setAttribute('class','jsEnabled');
	document.documentElement.className = 'jsEnabled'; //For IE
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Returns the value of a cookie. If the cookie value doesn't exist, 0 is returned
function readCookie(name){
	var nameEQ = name + "="; ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '0';
}

// Gets the value of a named parameter in the query string /
function readQueryString(param) {
 var loca = document.location.href;
 if(loca.indexOf('?' + param + '=')>-1 || loca.indexOf('&' + param + '=')>-1) {
  var qstring = loca.split('?');
  var keyVal = qstring[1].split('&');
  
	for (var i=0; i < keyVal.length; i++) {
   if(keyVal[i].indexOf(param + '=')==0) {
    var val = keyVal[i].split('=');
    return val[1]; } 
  }	
  
	return false; }
	
 else { return false; }

}


var randNum=new Date().getTime();
function showHomepagePopUp(windowsource,windowname,width,height,scrollbars,directories,location,menubar,status,toolbar,resizable,cookieLabel){
	var theLabel=cookieLabel+'=true';
  var date = new Date();
	date.setTime(date.getTime()+(24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
  var path = "; path=/";
  if (document.cookie.indexOf(cookieLabel)==-1){
      RenderPopup(windowsource,windowname,width,height,scrollbars,directories,location,menubar,status,toolbar,resizable);
      document.cookie=theLabel+expires+path;
  }
}

function RTeCollector(strParam) {
	intHeight = 460;
	intWidth  = 640;
	intTop=(screen.height/2)-(intHeight/2)
	intLeft=(screen.width/2)-(intWidth/2)
	strFeatures='toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,height='+intHeight+',width='+intWidth+',top='+intTop+',left='+intLeft;
	winEcollector=window.open('http://ecollector.responsetek.com/eCollector.asp?' + strParam, 'eCollector', strFeatures);
}

function sTime(){
	document.getElementById('holding_message').style.display='block';document.getElementById('master').style.display='none';	}
	
	
function eTime(){
	var elapsedTime = new Date(endTime-startTime);
	document.write('<span class=t1>'+elapsedTime.getSeconds()+'</span>');	
	document.getElementById('holding_message').style.display='none';document.getElementById('master').style.display='block';	}
	
function RenderPopup(windowsource,windowname,width,height,scrollbars,directories,location,menubar,status,toolbar,resizable) {
	var windowfeatures = 'width=' + width +
	',height=' + height + ',directories=' + directories +
	',location=' + location + ',menubar=' + menubar +
	',scrollbars=' + scrollbars + ',status=' + status +
	',toolbar=' + toolbar + ',resizable=' + resizable;
	window.open(windowsource, windowname, windowfeatures); }









//Broswer detection vars
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

var gSafeOnload = new Array();
function SafeAddOnload(f){
  if (IEmac && IE4)	{window.onload = SafeOnload;gSafeOnload[gSafeOnload.length] = f;}
	else if (window.onload){
		if (window.onload != SafeOnload){gSafeOnload[0] = window.onload;window.onload = SafeOnload;}		
		gSafeOnload[gSafeOnload.length] = f;
	}
	else window.onload = f; }
	
function SafeOnload(){for (var i=0;i<gSafeOnload.length;i++) gSafeOnload[i]();}

// shows or hides layer. layer in question is specified by <lyrName>. Shows layer if <show> is true, hides if false
function LMN_showhidelayer(lyrName, show) {
	if(document.layers)	document.layers[lyrName].display = show ? "block" : "none";
	else if(document.getElementById) {var obj = document.getElementById(lyrName);obj.style.display = show ? "block" : "none";}
	else if(document.all) document.all[lyrName].style.display = show ? "block" : "none"; }

// Pops open a little window for users that cannot view the
// Opening times and Call oost layers
function LMN_popuplayeralt(url,obj) {window.open(url,'mastheadpop','width=130,height=130');obj.href = '';return false;}

function isValidEmail(emailAddress)
{   
   var atIndex     = emailAddress.indexOf('@')
   var dotIndex    = emailAddress.indexOf('.',atIndex)
   // check 0 < atIndex < dotindex < length
   if( (0 < atIndex) && (atIndex < dotIndex) && (dotIndex < emailAddress.length-1) )
   { 
      // first character is a letter and all other are not whitespace
      var firstChar = emailAddress.charAt(0)
      
      if( (firstChar>='a' && firstChar<='z') ||  
          (firstChar>='A' && firstChar<='Z') ||
          (firstChar>='0' && firstChar<='9') )
      { 
         for(var i=1;i<emailAddress.length;++i){
            if(emailAddress.charCodeAt(i) <= 0x20){
               if(emailAddress.charAt(i) == ' ')
                  alert("There are no spaces allowed in your email address")
               else 
                  alert("The character \"" + emailAddress.charAt(i) + "\" is not allowed in your email address")
               return false;
            }    
         }
         return true;
          
      }
      alert("The first character in your email address, \"" + firstChar + "\" is not allowed")
   }
   alert("Please enter a full email address")
   return false;
}

// Function used on travel/flights/deals section pages to populate the flights search form
function prePopulate(dep,dest,airline) {
	if (document.getElementById) {
		var fieldsId = ['departurePoint','destination','airline'];
		var formField;
		
		for(var i=0; i < fieldsId.length; i++) {
			if(formField = getId(fieldsId[i])) {
				
				if(formField.type == 'select-one') {
					
					for (var l=0; l < formField.length; l++) {
						if(formField.options[l].text == dep || formField.options[l].text == airline)
							{ formField.selectedIndex = l;}
							
							if (airline=='') {
								formField.selectedIndex = 0; }
							}				
					} 
				
				if (formField.type == 'text') { 
					formField.value = dest;	}
			}
		}
	}
}

// Bookmarks the current page of the site
function bookmarkpage(){

  var url = document.location.href
  var title = document.title

  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }


}

/* LAB (Lightweight A-B Testing) */
var lab_numGroups=6;
function lab_getCookieVal(offset){var endstr=document.cookie.indexOf(";",offset);if(endstr==-1){endstr=document.cookie.length;}return unescape(document.cookie.substring(offset, endstr));}
function lab_getCookie(name){var arg=name + "=";var alen=arg.length;var clen=document.cookie.length;var i=0;while(i < clen){var j = i + alen;if (document.cookie.substring(i, j) == arg) return lab_getCookieVal (j);i = document.cookie.indexOf(" ", i) + 1;if (i == 0) break;}return null;}
labIndex=document.location.href.indexOf('LAB=');
if(labIndex>-1)
	{
	lab_group=document.location.href.slice(labIndex+4,labIndex+5);
        if(s_account=='lastmintravelocitycouk')
        	document.cookie="LLAB="+lab_group+";expires=Mon, 14-Jul-2008 00:00:00 GMT;path=/;domain=travelocity.co.uk";
        else
        	document.cookie="LLAB="+lab_group+";expires=Mon, 14-Jul-2008 00:00:00 GMT;path=/;domain=lastminute.com";
	}
else
	{	
	var lab_group = lab_getCookie('LLAB');
	if (lab_group==null) {
		lab_group=''+Math.floor(Math.random()*lab_numGroups);
      if(frontierSkin=='engb.travelocity.co.uk') { //Use skin to detect domain
         document.cookie="LLAB="+lab_group+";expires=Mon, 14-Jul-2008 00:00:00 GMT;path=/;domain=travelocity.co.uk";}
      else {
      	document.cookie="LLAB="+lab_group+";expires=Mon, 14-Jul-2008 00:00:00 GMT;path=/;domain=lastminute.com"; }
      }
	}



/*
* Author : Shiraz Solangi
* Date : xx/xx/2006
* Description : Touch Clarity - Read cookie and render TC content 
*/

function renderTCPromo(containerId,containerDefault) {
		
	var userCookie = readCookie("LLAB");
	
	if (userCookie == 0) {
		//Show normal content to control group
		document.write(containerDefault); }
	
	else {
		//Show TC targeted content for all others
		tc_optimise(containerId,containerDefault);	
		}	
}

// include lastminute library 
if( (typeof lm!='object') || (typeof lm == 'object' && !lm.loaded) ){
    var protocol = (document.location.href.indexOf('https')>-1)? 'https://' : 'http://';
    if(typeof frontierHTTPHost != 'undefined' && frontierHTTPHost!=''){
        document.write('<script type="text/javascript" src="' + protocol + frontierHTTPHost + frontierBaseUrl + '/lm_include.js?skin=' + frontierSkin + '"></script>');
    }else{
        document.write('<script type="text/javascript" src="' + protocol + 'www.lastminute.com/site/lm_include.js"></script>');
    }
}

// View product images thumbnail as larger image - on UI details pages
function thumbnailSwitch(targetId,imageSrc,caption){
	document.getElementById(targetId).src = imageSrc;
	
	if (document.getElementById(targetId + 'Caption')) {
		document.getElementById(targetId + 'Caption').innerHTML = caption;
	}
}

// Toggle the display of the bubble popup		
function togglePopup(obj,show){
	var offSetFromMouse = new Array()
	offSetFromMouse[0] = 231;
	offSetFromMouse[1] = 145;
	var divHeight=0;
	var selects = document.getElementById('page_content').getElementsByTagName('select');
	
	if (selects.length>0){
		if (show == true && selects[0].style.visibility != 'hidden'){
			for(i=0;i<selects.length;i++) selects[i].style.visibility = 'hidden';
		}
		
		else if (show == false) {
			for(i=0;i<selects.length;i++) selects[i].style.visibility = '';
		}
	}
	
    if (obj.href){
		divCont = obj.href.split('#')[1];
		divCont = document.getElementById(divCont);
		divCont.style.position='absolute';
		if (show){
			document.getElementById('master_center').style.zIndex='12';
			if (divCont.offsetHeight){
				divHeight=divCont.offsetHeight;
			} else if (divCont.style.pixelHeight){
				divHeight=divCont.style.pixelHeight;
			}	
			var curleft = curtop = 0;
			if (obj.offsetParent) {
				curleft = obj.offsetLeft
				curtop = obj.offsetTop
				while (obj = obj.offsetParent) {
					curleft += obj.offsetLeft
					curtop += obj.offsetTop
				}
			}		
			divCont.style.top=(curtop-offSetFromMouse[1]-divHeight)+'px';
			divCont.style.left=(curleft-offSetFromMouse[0])+'px';
		} else {
			document.getElementById('master_center').style.zIndex='2';
			divCont.style.left='-1000em';
			divCont.style.top='-1000em';			
		}
	} else {
		divCont = document.getElementById(obj);	
		divCont.style.position='absolute';
		divCont.style.left='-1000em';
		divCont.style.top='-1000em';
	}
}

/* Description - Price formatter for locales*/
function currencyFormat(amount,currency,locale) {
	var amount = parseFloat(amount).toFixed(2);
	if (currency == 'GBP') {
		 return '&pound;' + amount;
	}
	else if (currency == 'EUR') {
		if (locale == 'es_ES') {
			 return amount + '&euro;';
		}		 
		if (locale == 'fr_FR') {
			  return amount + ' &euro;'
		}
		if (locale in ['fr_BE', 'nl_BE', 'nl_NL', 'it_IT']) {
			  return '&euro;' + amount;
		}		 
		else {
			  return 'EUR ' + amount;
		}		
	}
	else {
		return amount + ' ' + currency;
	}
}

/*
* Author : Laurent Muchacho
* Date : 06/12/2006
* Description : calendar positioning hack
* Resource : http://devbgb0214:8100/frontier/js/calendar/hack.js
*/
var hijackShowCalendarTimer=-1;var calendarIframe=false;function hijackShowCalendar(){if(typeof showCalendar=="function"){clearTimeout(hijackShowCalendarTimer);showCalendar=function(e){var o=(typeof event!="undefined")?event.srcElement:e.target;var _3=lm.e("cIframe");if(!calendarIframe){_3.style.visibility="hidden";calendar.show(0,0);calendarIframe={left:lm.getLeft(_3),top:lm.getTop(_3)};_3.style.visibility="visible";}calendar.show((lm.getLeft(o)-calendarIframe.left)+o.width+5,lm.getTop(o)-calendarIframe.top);};}else{hijackShowCalendarTimer=setTimeout("hijackShowCalendar()",500);}}hijackShowCalendar();function stopHijackShowCalendarLoop(){if(typeof showCalendar!="function"&&hijackShowCalendarTimer!=-1){clearTimeout(hijackShowCalendarTimer);}}if(typeof calendar=="object"){var hijackOldOnload=window.onload;if(typeof hijackOldOnload=="function"){window.onload=function(){stopHijackShowCalendarLoop();hijackOldOnload();};}else{window.onload=stopHijackShowCalendarLoop;}}