/* Flight Centre UK - v1.0 Generic Function Library - KF 2004 */

//************	SHOW HIDE CONTROLS	************
function hideLyr(sLyrName) {
	document.getElementById(sLyrName).style.display = "none";
}

function showLyr(sLyrName) {
	document.getElementById(sLyrName).style.display = "block";
}


//	Super Breaks Searches & Tabs - - start
function ShowUkTheatre() {
		showLyr('UkTheatre');
		hideLyr('UkEvents');
}

function ShowUkEvents() {
		hideLyr('UkTheatre');
		showLyr('UkEvents');
}
//	Super Breaks Searches & Tabs - - end


//	Hotels Searches & Tabs - - start
function ShowEuro() {
		showLyr('Euro');
		hideLyr('World');
}

function ShowWorld() {
		hideLyr('Euro');
		showLyr('World');
}
//	Hotels Searches & Tabs - - end

// variables to hold the main/popup window handles
var window_handle;
var window_page; 

// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
	offset = document.cookie.indexOf(search);
	if (offset != -1)
	{ 
	  offset += search.length;
	  end = document.cookie.indexOf(";", offset);
	  if (end == -1) end = document.cookie.length;
	  cookieValue = unescape(document.cookie.substring(offset, end))
	}
  }
  return cookieValue;
}

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
	expire = new Date((new Date()).getTime() + hours * 3600000);
	expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

// function to check if we have already shown this popup to the user since the last session.
// If not, show the popup
function open_popup(page) {
	// Check if the user has already visited this page within the last 24 hours
	//if (readCookie("FcPopupRegistration") != "" )
	//{
		// User has, hence do not show them the popup
		//return;
	//}
	//else
	//{
		// First time user has come to this page, hence write popup flag to 
		// the popup cookie. Cookie will when browser session ends
		//writeCookie("FcPopupRegistration", "yes");
		
		var width="301";
		var height="310";
		var left = (screen.width/2) - width/2;
	  	var topSize = (screen.height/2) - height/2;
		// Next thing, open the window
		top.window_handle = open(page, 'popupWindowName', 'resizable=no,width='+width+',height='+height+',toolbar=no,scrollbars=no,menubar=no, left='+left+',top='+topSize);
		top.window_handle.focus();
		if (!top.window_handle.opener) top.window_handle.opener = self;
	//}
	
    return false;
}

// function to move focus back to the main window and
// then redirect to the necessary url
function open_main(page)
{
	if (opener && !opener.closed)
	{
		window.close();
		opener.location.href = page;
		//below didn't work :(
		//opener.form.email.value = "conorIsTesting@somewhere.com";
		//opener.document.forms(0).email.value = "conorIsTestingAgain@somewhere.com";
	}
	else {
		window.open('home.jsp');
		window_page = page;
		setTimeout('reopen_opener()',100);
	}
    
    return false;
}

// in the event that the main window of the popup has been closed, 
// this function will reopen the window
function reopen_opener() {
    opener = window.open(window_page,'main');
    opener.top.window_handle = self;
    opener.top.window_handle.closed = false;
}

// function that initializes a given label to a certain text value
// In our case, we update our span check out label with the value
// contained within a hidden check out input field
function changeText(label,text)
{
  var node;
  if (document.getElementById)
  { 
	 var node = document.getElementById(label);
	 if (node)
	   {
		 /*See if we already inserted a node. */
		 var nextNode = node.firstChild;
		 if(nextNode)
		   {  
			 /* Yes, replace the text. */
			 nextNode.data = text.value;
		   }
		 else
		   {
			 /* No, Insert the new node. */
			 node.appendChild(
				 document.createTextNode(text.value));
			 node.text = text;
		   }
	   }
	else
	  { 
		alert("You need to create a span tag with  " +
			  
			  "the id of " + label + ".");
		return;
	  }
  }
  else
	{ 
	  alert(" document.getElementByID failed. " +
		"You need to use a newer browser."); 
	  return;
	}  
 }
 
// function that takes in a date and adds a number of days to it before
// returning result date
function addDay(input_date, days_to_add)
{
	return new Date(check2k(input_date.getYear()),input_date.getMonth(),1*input_date.getDate()+days_to_add);
}

// function that takes in a date and subtracts a number of days to it before
// returning result date
function subtractDay(input_date, days_to_subtract)
{
	return new Date(check2k(input_date.getYear()),input_date.getMonth(),1*input_date.getDate()-days_to_subtract);
}

// function that checks input date is 2k compatable
function check2k(input_date)
{
	return (input_date<1900)?input_date -= -1900:input_date;
}

// function that updates the check out label to inform
// the user when s/he would check out
function updateCheckOutLabel()
{
	// flag to switch on debugging
	var debug = false;
	
	// Determine what month we are in
	var month_year_str = document.forms[0].month.value;
	var month_length = 0;
	for (var i=0; i < month_year_str.length; i++)
	{
		var letter = month_year_str.charAt(i);
		if (letter == "-")
		{
			month_length = i;
			break;
		}
	}

	var month_str = month_year_str.substring(0,month_length);
	var month_int = parseInt(month_str);
	// Our month select box is zero based, hence need to add
	// 1 to represent properly in javascript date object
	month_int++; 
	
	// Determine what year we are in
	var year_str = month_year_str.substring(month_year_str.length-1,month_year_str.length);
	//if (_isInteger(year_str) == false)
	if (year_str == "-")
	{
		var now = new Date();
		year_str = now.getYear();
	}
	else
	{
		// Adding 200 to make the year 200*
		year_str = "200" + year_str;
	}
	
	// Adding it all up to get a date in string format that's formattable
	var date_basic_str = document.forms[0].date.value + "-" + month_int + "-" + year_str;
	// Now have the date in time format
	var date_time_str = getDateFromFormat(date_basic_str, "d-M-yyyy");
	// Converting date into a date object
	var date_before_adding_days_str = new Date(date_time_str);
	
	// Determine how many nights do we need to add
	// Assume we're doing a worldwide search initially
	var worldwide_search_bool = true;
	var nights_str = "";
	if (document.forms[0].nights == null)
	{
		worldwide_search_bool = false;
	}
	
	if (worldwide_search_bool)
	{
		nights_str = document.forms[0].nights.value;
	}
	else
	{
		nights_str = document.forms[0].Nights.value;
		updateDate(document.forms[0].month,document.forms[0].date);
	}
	
	var nights_int = parseInt(nights_str);
	
	// Add the nights to the existing date, to come up with final total
	var date_str = addDay(date_before_adding_days_str, nights_int);
	
	// Now finally format the dates to what we require
	var date_formatted_before_adding_days_str = formatDate(date_before_adding_days_str, "E dS NNN yyyy");
	var date_formatted_str = formatDate(date_str, "E dS NNN yyyy");
	
	if (debug)
	{
		alert("updateCheckOutLabel: month_year_str: " + month_year_str);
		alert("updateCheckOutLabel: nights_str: " + nights_str);
		alert("updateCheckOutLabel: month_str: " + month_str);
		alert("updateCheckOutLabel: year_str: " + year_str);
		alert("updateCheckOutLabel: date_basic_str: " + date_basic_str);
		alert("updateCheckOutLabel: date_str: " + date_str);
		alert("updateCheckOutLabel: date_formatted_before_adding_days_str: " + date_formatted_before_adding_days_str);
		alert("updateCheckOutLabel: date_formatted_str: " + date_formatted_str);
	}
	
	// Finally update the check out hidden label before updating our check out span
	// html label
	document.forms[0].checkOutLabelHidden1.value = date_formatted_before_adding_days_str.substring(0,3);
	document.forms[0].checkOutLabelHidden2.value = date_formatted_str;
	changeText("checkOutLabel1", document.forms[0].elements["checkOutLabelHidden1"]);
	changeText("checkOutLabel2", document.forms[0].elements["checkOutLabelHidden2"]);
}

// function that takes the return parameters from calendar
// and sets up the select boxes to the correct index
function setDateMonthYearSelectBoxes(y,m,d)
{
	// flag to switch on debugging
	var debug = false;
	
	// year has value 2005, need to make year significant to a single figure
	y = y - 2000;
	// month is one based for calendar, need to make it zero based for select box
	m = m - 1;
	var year_str = parseInt(y);

	var month_year_str = m + "-" + year_str;
	
	if (debug)
	{
		alert("setDateMonthYearSelectBoxes: y: "+y);
		alert("setDateMonthYearSelectBoxes: m: "+m);
		alert("setDateMonthYearSelectBoxes: d: "+d);
		alert("setDateMonthYearSelectBoxes: month_year_str: "+month_year_str);
	}
	
	// Set up the month-year select box to the correct index
	for (var i=0; i<document.forms[0].month.options.length; i++) {
		if (document.forms[0].month.options[i].value==month_year_str) {
			document.forms[0].month.selectedIndex=i;
			}
		}
	
	// Set up the day select box to the correct index
	for (var i=0; i<document.forms[0].date.options.length; i++) {
		if (document.forms[0].date.options[i].value==d) {
			document.forms[0].date.selectedIndex=i;
			}
		}
}

// function that takes in two strings, namely month-year coupling and day,
// and converts it into a format to calendar popup will understand, yyyy-mm-dd
function getDateString(month_year_select,day_select)
{
	// flag to switch on debugging
	var debug = false;
	
	var month_year_str = month_year_select.options[month_year_select.selectedIndex].value;
	var year_str = month_year_str.substring(2,3);
	var month_str = month_year_str.substring(0,1);
	var day_str = day_select.options[day_select.selectedIndex].value;
	
	if (year_str=="" || month_str=="") { return null; }
	if (day_str=="") { day_str=1; }
	
	var year_int = parseInt(year_str);
	year_int = year_int + 2000;
	
	var month_int = parseInt(month_str);
	month_int++;
	
	var str= year_int+'-'+month_int+'-'+day_str;
	
	if (debug)
	{
		alert("getDateString: year_str="+year_str);
		alert("getDateString: month_str="+month_str);
		alert("getDateString: day_str="+day_str);
		
		alert("getDateString: year_int="+year_int);
		alert("getDateString: month_int="+month_int);
		
		alert("getDateString: str: " + str);
	}
	
	return str;
}

// function to initialize the menu date dropdowns with
// todays date
function initializeMenuDropdowns()
{		
	var today_date = new Date();
	
	initMenus();
	setDateMonthYearSelectBoxes(today_date.getYear(),today_date.getMonth()+1,today_date.getDate());
	updateCheckOutLabel();
}

// function that takes in two strings, namely month-year coupling and day,
// and converts it into a format to calendar popup will understand, yyyy-mm-dd
function updateDate(month_year_select,day_select)
{
	// flag to switch on debugging
	var debug = false;
	
	var month_year_str = month_year_select.options[month_year_select.selectedIndex].value;
	var year_str = month_year_str.substring(2,3);
	//if (_isInteger(year_str) == false)
	if (year_str == "-")
	{
		var now = new Date();
		year_str = now.getYear();
	}
	
	// Remember month figure could be significant to either
	// one or two digits
	var month_str = "";
	if (month_year_str.length == 4)
	{
		// i.e. october to december
		month_str = month_year_str.substring(0,2)
	}
	else
	{
		// i.e. january to september
		month_str = month_year_str.substring(0,1);
	}
	
	var month_int = parseInt(month_str);
	month_int++;
	month_str = month_int;
	
	var day_str = day_select.options[day_select.selectedIndex].value;
	
	if (year_str=="" || month_str=="") { return null; }
	if (day_str=="") { day_str=1; }
	
	if (debug)
	{
		alert("updateDate: year_str="+year_str);
		alert("updateDate: month_str="+month_str);
		alert("updateDate: day_str="+day_str);
	}
	
	document.forms[0].StartYear.value = year_str;
	document.forms[0].StartMonth.value = month_str;
	document.forms[0].StartDay.value = day_str;
}
