// Browser sniffer
var ns4 = (document.layers); 
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var ie = document.all;

// Attaches objects dependent on browser
function object_attach(id)
{
	var obj;
	if(ns4) obj = document.layers[id];
	else if(ie4) obj = document.all[id];
	else if(ie5 || ns6) obj = document.getElementById(id);
	return obj;
}
// Shows/hides div objects
function div_switch(id, bShow, bBlock)
{
	temp_Obj = object_attach(id);
	if(ns4)
	{
		if(bShow==true)
		{ 
			temp_Obj.display = "block";
			temp_Obj.visibility = "show";
		}
		else
		{ 
			temp_Obj.display = "none";
			temp_Obj.visibility = "hide";
		}
	}
	else
	{
		if(bShow==true)
		{ 
			temp_Obj.style.display = "block";
			temp_Obj.style.visibility = "visible";
		}
		else
		{ 
			temp_Obj.style.display = "none";
			temp_Obj.style.visibility = "hidden";
		}
	}
}

function clearField(id,field_value){
	if (document.getElementById(id).value == field_value){
		document.getElementById(id).value = "";
	}
}

var popUpWin=0;
function popUpWindow_bak(URLStr, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  
  var left = (screen.width-width)/2;
  var top = (screen.height-height)/2;
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

// Opens a popup window
function openPopupWin(popUrl, popWid, popHgt, popScroll, popResize)
{
	popupWin=window.open(popUrl,"popupWin","width=" + popWid + ",height=" + popHgt + ",left=" + ((screen.width-popWid)/2) + ",top=" + ((screen.height-popHgt)/2) + ",status=yes,toolbar=no,menubar=no,scrollbars=" + popScroll + ",resizable=" + popResize);
	if(!popupWin.opener) popupWin.opener=self;
}

function popUpWindow(URLStr, width, height, sScroll)
{
  if(sScroll != 'yes'){
	  var sScroll = 'no';
  }
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  
  var left = (screen.width-width)/2;
  var top = (screen.height-height)/2;
  popUpWin = window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+sScroll+',resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}


//#################################################################################################################
// BEGIN COUNTDOWN STUFF FOR BOOKING FLOW
function cdtime(container, targetdate){
	if (!document.getElementById || !document.getElementById(container)) return
	this.container=document.getElementById(container)
	this.currentTime=new Date()
	this.targetdate=new Date(targetdate)
	this.timesup=false
	this.updateTime()
}

cdtime.prototype.updateTime=function(){
	var thisobj=this
	this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
	setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

cdtime.prototype.displaycountdown=function(baseunit, functionref){
	this.baseunit=baseunit
	this.formatresults=functionref
	this.showresults()
}

cdtime.prototype.showresults=function(){
	var thisobj=this
	var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
	if (timediff<0){ //if time is up
		this.timesup=true
		this.container.innerHTML=this.formatresults()
		return
	} else if (timediff<600){ 
		document.getElementById('countdowncontainer').style.background='#f02831';
	} else if (timediff<1200){ 
		document.getElementById('countdowncontainer').style.background='#db741f';
	}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
	hourfield=dayfield*24+hourfield
	dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
	minutefield=dayfield*24*60+hourfield*60+minutefield
	dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
	var secondfield=timediff
	dayfield=hourfield=minutefield="n/a"
}
	this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
	setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}

function eraseCookie(name) {
	 document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}

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=/";
}

function formatresults(){
	if (this.timesup==false){//if target date/time not yet met
		var displaystring="You have "+arguments[2]+" minutes "+arguments[3]+" seconds left to complete this booking."
	}
	else{ //else if target date/time met
		//alert('EXPIRED');
		setTimeout("document.getElementById('countdowncontainer').style.background='#f02831';",1000);
		if(document.getElementById('editbooking')) {setTimeout("document.getElementById('editbooking').style.display='none';",2000);}
		if(document.getElementById('editbooking')) {setTimeout("document.getElementById('submitbooking').style.display='none';",2000);}
		var displaystring="This booking has taken too long to process. The seats can only be held in the system for 30 minutes before being released. If you wish to proceed with a booking can you please start again and attempt to complete the booking process in under 30 minutes. Thank you."
		eraseCookie('holdingtime');
		setTimeout("document.location.href='/'", 20000)
		
		//alert('Timeup');
		//document.location.href='/';
	}
	return displaystring;
}
// END COUNTDOWN STUFF FOR BOOKING FLOW

// Checks if field contains valid characters
// 1 - Numeric Integer, 2 - Email, 3 - Numeric Decimals
function isValidField(sText, iType){
	if(iType==1)
	{ ValidChars = "0123456789"; }
	else if(iType==2)
	{ ValidChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-@"; }
	else if(iType==3)
	{ ValidChars = "0123456789."; }
	else if(iType==4)
	{ ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "; }
	var isValid=true;
	var Char;
	for (i = 0; i < sText.length && isValid == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{ isValid = false; }
	}
	return isValid;
}
function echeck(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
		return (true)
	}
	return (false)
}

var SelectedSearchTab = 'search_tab1';
function SwitchTabs(id, on) {
	if(SelectedSearchTab!=id) {
		sExtra = '';
		if(on==1){
			if(document.getElementById(id).className.indexOf('Over')<0){
				document.getElementById(id).className = 'searchtabOver'
			}else{
				SelectedSearchTab = id;
			}
		}else{
			if(document.getElementById(id).className.indexOf('Over') > -1) {
				document.getElementById(id).className = 'searchtab'
			}
		}
	}
}
function SwitchSearch(id) {
	/* HIDE PREDICTIVE TEXT OPTIONS IF SHOWING */
	for(x=1;x<8;x++) {
		if(document.getElementById('search_tab'+x)) {
			document.getElementById('search_tab'+x).className = 'searchtab';
		}
		if(document.getElementById('search'+x)) {
			document.getElementById('search'+x).style.display = 'none';
		}
	}
	SelectedSearchTab = id;
	if(document.getElementById(id)) {
		document.getElementById(id).className = 'searchtabOver';
	}
	if(document.getElementById('search'+Right(id,1))) {
		document.getElementById('search'+Right(id,1)).style.display = '';
	}
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function setInspirator(fId,i){
	if(i == 1){
		if(document.inspirator.daytrip.checked == false && document.inspirator.shortbreak.checked == false){
			document.inspirator.uktours.disabled = true
			document.inspirator.europetours.disabled = true
			
			document.inspirator.uktours.checked = false
			document.inspirator.europetours.checked = false
			
			document.inspirator.firsttime.checked = false
			document.inspirator.culture.checked = false
			document.inspirator.shopping.checked = false
			document.inspirator.anytype.checked = false
				
			document.inspirator.firsttime.disabled = true
			document.inspirator.culture.disabled = true
			document.inspirator.shopping.disabled = true
			document.inspirator.anytype.disabled = true
		}else{
			if(document.inspirator.uktours.checked == false && document.inspirator.europetours.checked == false){
				document.inspirator.uktours.disabled = false
				document.inspirator.europetours.disabled = false
				
				document.inspirator.uktours.checked = false
				document.inspirator.europetours.checked = false
				
				document.inspirator.firsttime.checked = false
				document.inspirator.culture.checked = false
				document.inspirator.shopping.checked = false
				document.inspirator.anytype.checked = false
				
				document.inspirator.firsttime.disabled = true
				document.inspirator.culture.disabled = true
				document.inspirator.shopping.disabled = true
				document.inspirator.anytype.disabled = true
			}
		}		
	}else if(i == 2){
		if(document.inspirator.uktours.checked == false && document.inspirator.europetours.checked == false){
			document.inspirator.firsttime.disabled = true
			document.inspirator.culture.disabled = true
			document.inspirator.shopping.disabled = true
			document.inspirator.anytype.disabled = true
			
			document.inspirator.firsttime.checked = false
			document.inspirator.culture.checked = false
			document.inspirator.shopping.checked = false
			document.inspirator.anytype.checked = false
		}else{		
			document.inspirator.firsttime.disabled = false
			document.inspirator.culture.disabled = false
			document.inspirator.shopping.disabled = false
			document.inspirator.anytype.disabled = false
		}
	}
}

// Clears a text field only if the value matches what is passed in
function clearField(fId, sMatch, sChange){
	if(sChange == null) sChange = '';
	if(fId.value == sMatch)
	{ fId.value = sChange; }
}