if (document.layers) 
{ // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} 
else if (document.all) 
{ // Internet Explorer
    document.onmousemove = captureMousePosition;
} 
else if (document.getElementById) 
{ // Netcsape 6
    document.onmousemove = captureMousePosition;
}
var languageCode = "en_US";
function setLanguageCode(lang)
{
	languageCode = lang; // global variable
}
// Global variables
xMousePos = 0; 
yMousePos = 0; 
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page
function captureMousePosition(e) 
{
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has 
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } 
	else if (document.all) 
	{
        // When the page scrolls in IE, the event's mouse position 
        // reflects the position from the top/left of the screen the 
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no 
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } 
	else if (document.getElementById) 
	{
        // Netscape 6 behaves the same as Netscape 4 in this regard 
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}

function highlight(rowid)
{
	var ele = document.getElementById(rowid);
	if (ele)
		ele.style.backgroundColor='#FFFF66';
}
function lowlight(rowid)
{
	var ele = document.getElementById(rowid);
	if (ele)
		ele.style.backgroundColor='#FFFFFF';
}

function getScrollXY()
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {x:scrOfX, y:scrOfY};
}

function mouseCoords(ev) {
	var oScroll = getScrollXY();
	if (ev.pageX || ev.pageY) {
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:(ev.clientX + document.body.scrollLeft - document.body.clientLeft)+oScroll.x,
		y:(ev.clientY + document.body.scrollTop  - document.body.clientTop)+oScroll.y
	};
}

function getWindowWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}
 
function getWindowHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function showbox(boxname, ev)
{
	// amend to use CSR script for more precious
	elebox = document.getElementById(boxname);
	
	if (elebox)
	{
		if (elebox.innerHTML != "")
		{
			var mousePos = mouseCoords(ev);
			currWidth = getWindowWidth() - 400;
			currLeft = mousePos.x;
			
			if (currLeft > currWidth) { currLeft = currWidth; }
			
			elebox.style.left = currLeft + "px";
			elebox.style.top = (mousePos.y + 5) + "px";
			elebox.style.display='block';
			
			// alert (' currWidth : '+ currWidth +'\n currLeft : '+ currLeft);
		}
	}
}

function hidebox(boxname)
{
	elebox = document.getElementById(boxname);
	if (elebox)
	{
		elebox.style.display = 'none';
	}
}

function notifyComplete(doit)
{
	if (doit==0)	{ return;	}
	if (window.parent.location!=self.location)
	{
		docheight = new Number(110)
		//docheight = document.layers?document.height:document.body.offsetHeight;
		hotcount = new Number(document.dat.hotcount.value);
		stdcount = new Number(document.dat.stdcount.value);
		tabcount = new Number(document.dat.tabcount.value);
		tabsplit = new Number(document.dat.tabsplit.value);
		docheight += hotcount*18;
		if (tabsplit>0)
		{
			if (hotcount<=10)
			{
				docheight += stdcount*49;
			}
			else
			{
				docheight += tabcount*70;
			}
		}
		else
		{
			docheight += tabcount*60;
		}
		window.parent.handleResponse(docheight, hotcount);
	}
	
}

function refreshit()
{

}

function UpdateCurrency(oSelect) {

	var currency = oSelect.value;

	if (currency != "0")
	{
		// change cursor to hour glass
		document.body.style.cursor = 'wait';
		
		var to_rate = CurrencyArray[currency]["Value"];

		var cname = CurrencyArray[currency]["Name"];
		var l_cname = cname.length;

		document.getElementById("currency_display").value = cname;
		document.getElementById("currency_display").size = l_cname;

		var to = new Number(0);

		var hotel_count = document.getElementById("hotel_count").value;
		for (var i=0; i<hotel_count; i++)
		{
			// full rate in thai baht
			var oThaiBest = document.getElementById("hotel_"+i+"_pub");
			if (oThaiBest.value != 'n/a')
			{
				from = document.getElementById('hotel_'+i+'_thbest').value;
				from = from.replace(",", "");
				to = new Number(from/to_rate);
				to = to.toFixed(4);
				to = Math.ceil(to);
				to = addCommas(to);
				oThaiBest.value = to;
				var size = to.length;
				oThaiBest.size = size;				
			}		
		}
		
		//--------------------------------------------------------------------------
		// <ake> calculate rate in new currency for daily rate in availability chart
		//--------------------------------------------------------------------------
		var total_rate = document.getElementById("RateCount").value;
		var arrRateInSelectedCurrency = new Array();
		for (var j=0; j<total_rate; j++)
		{
			from = document.getElementById('thb_rate_'+j).value;
			to = new Number(from/to_rate);
			to = to.toFixed(4);
			to = Math.ceil(to);
			to = addCommas(to);
			arrRateInSelectedCurrency[j] = to;
		}
		
		var arrSpan = document.getElementsByTagName("span");
		for (var j=0; j<arrSpan.length; j++)
		{
			var span_id = arrSpan[j].id;
			if (span_id.substr(0, 5) == "rate_")
			{
				var idx = span_id.substr(5);
				arrSpan[j].innerHTML = arrRateInSelectedCurrency[idx];
			}
		}
		//--------------------------------------------------------------------------
		
		document.body.style.cursor = 'default';
	}
}

//--------------------------------------------------------------------------------------------------
	function setCurrency()
	{
		var curr = readCookie("currency");
		if (curr)
		{
			var oSelect = document.getElementById("SelectCurrency");
			oSelect.selectedIndex = curr;
			UpdateCurrency(oSelect);
		}
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
// cookie
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	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 readCookie(name)
	{
		var nameEQ = name + "=";
		var 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 null;
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function eraseCookie(name)
	{
		createCookie(name,"",-1);
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function addCommas(nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
//--------------------------------------------------------------------------------------------------

//*************************************************
// below are script use to move availability chart
//*************************************************

var first_col = 3;
var first_show_col = 3;
var last_show_col = 16;
var last_col = 30;

//--------------------------------------------------------------------------------------------------
function moveNext()
{
	var oTable = document.getElementById("tblAvailability");
	var oMultiPage = document.getElementById("multiPage");
	
	var startRow = 1;
	var endRow = oTable.rows.length-1;
	
	if (oMultiPage != null)
	{
		if (oMultiPage.value == 1)
		{
			startRow++;
			endRow--;
		}
	}
	
	for (var r=startRow; r<endRow; r++)
	{
		var oRow = oTable.rows[r];
		
		// hide left most column
		oRow.cells[first_show_col].style.display = "none";
		
		// show right most column
		oRow.cells[last_show_col+1].style.display = "";
	}
	
	// show or hide action button according to current display location
	document.getElementById("movePrevious").style.visibility = "visible";
	
	
	// increase global variable
	first_show_col += 1;
	last_show_col += 1;
	
	// set show or hide move button
	setMoveButton();
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
function movePrevious()
{
	var oTable = document.getElementById("tblAvailability");
	var oMultiPage = document.getElementById("multiPage");
	
	var startRow = 1;
	var endRow = oTable.rows.length-1;
	
	if (oMultiPage != null)
	{
		if (oMultiPage.value == 1)
		{
			startRow++;
			endRow--;
		}
	}
	
	for (var r=startRow; r<endRow; r++)
	{
		var oRow = oTable.rows[r];
		
		// show left most column
		oRow.cells[first_show_col-1].style.display = "";
		
		// hide right most column
		oRow.cells[last_show_col].style.display = "none";
	}
	
	// show or hide action button according to current display location
	document.getElementById("moveNext").style.visibility = "visible";
	
	
	// decrease global variable
	first_show_col -= 1;
	last_show_col -= 1;
	setMoveButton();
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
function setMoveButton()
{
	// set moveNext button
	if (last_show_col == last_col)
	{
		document.getElementById("moveNext").style.visibility = "hidden";	
	}
	else
	{
		document.getElementById("moveNext").style.visibility = "visible";	
	}

	// set movePrevious button
	if (first_show_col == 3)
	{
		document.getElementById("movePrevious").style.visibility = "hidden";	
	}
	else
	{
		document.getElementById("movePrevious").style.visibility = "visible";	
	}
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
function setDisplayDate()
{
	var oTable = document.getElementById("tblAvailability");
	var oMultiPage = document.getElementById("multiPage");
	
	var startRow = 1;
	var endRow = oTable.rows.length-1;
	
	if (oMultiPage != null)
	{
		if (oMultiPage.value == 1)
		{
			startRow++;
			endRow--;
		}
	}
	
	for (var r=startRow; r<endRow; r++)
	{
		var oRow = oTable.rows[r];
		
		for (var c=first_col; c<first_show_col; c++)
		{
			// hide left most column
			oRow.cells[c].style.display = "none";
		
			// show right most column
			var cl = c+14;
			oRow.cells[cl].style.display = "";
		}
	}
	
	// set show or hide move button
	setMoveButton();
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
function getParameter(name) 
{
	var currentUrl = window.location.search;
	currentUrl = currentUrl.replace('%26','&');
	currentUrl = currentUrl.replace(/%20/g,' ');
	var passed = true;
	if (currentUrl.indexOf('?')==-1)
	{
		passed=false;
	}

	if (passed)
	{
		
		// ake, modified to prevent to get value from similar parameter name
		var begin_str_to_find = '?'+name+'=';
		var strBegin = currentUrl.indexOf(begin_str_to_find)
		if (strBegin == -1)
		{
			begin_str_to_find = '&'+name+'=';
			strBegin = currentUrl.indexOf(begin_str_to_find)
			if (strBegin == -1)
			{
				return "";
			}
		}
		
		strBegin += name.length+2;
		var strEnd = currentUrl.indexOf("&", strBegin);

		if (strEnd==-1)
		{
			strEnd = currentUrl.indexOf("%26", strBegin);
			if (strEnd==-1)
			{
				strEnd = currentUrl.length;
			}
		}

		return currentUrl.substring(strBegin, strEnd);
	}
	else
	{
		return "";
	}
}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
function setDisplayPeriod(sStartDate)
{
	var aStartDate = sStartDate.split("-");
	var dStartDate = new Date();
	
	// month value have to decrease by 1 (0-11)
	aStartDate[1] -= 1;
	
	// set to url parameter
	dStartDate.setFullYear(aStartDate[0], aStartDate[1], aStartDate[2]);
	
	// assume that the first day we show in availability chart is 1 day lead time
	var dTomorrow = new Date();
	var seconds = dTomorrow.getTime();
	
	seconds += 86400000;
	
	dTomorrow.setTime(seconds);
	
	
	// if supplied date before tomorrow do nothing
	if ( dStartDate < dTomorrow )
	{
		return false;
	}
	
	// check period
	var dDiff = Math.abs(Math.round((dStartDate-dTomorrow)/86400000));
	
	// if supplied date greater than 28 days from tomorrow, do nothing
	if ( dDiff > 28 )
	{
		return false;
	}
	
	// if date diff more than 14 days, set it to 14 days (last period)
	if ( dDiff > 14 )
	{
		dDiff = 14;
	}
	
	// now value ok, set the first column to show parameter
	// and call function to set the display period
	first_show_col = dDiff+first_col;
	last_show_col = first_show_col+13;
	setDisplayDate();
	return true;	
}
//--------------------------------------------------

// create cookie to log agent booking
var agentId = getParameter("s");
if (agentId != "")
{
	// agent id supplied, create cookie
	createCookie("source", agentId, 30)
}

//--------------------------------------------------
// GTA
var _DestinationID = "";
