// Timeout
var _timeout = null;
var _timeoutInterval;

var _tt = "";

function getTime()
{
	var now = new Date();
	return now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
}
 
 function doTimeout()
 {
	_tt += 'Timeout at ' + getTime() + '\n';
	//alert('Timeout \n' + _tt);
	location.href = "SessionExpired.aspx";
 }
 
 function resetTimeout() 
 {
	_tt += 'Reset at ' + getTime() + '\n';
 	if (_timeout != null)
		clearTimeout(_timeout);
 	if (_timeoutInterval != null)
		_timeout = setTimeout(doTimeout, _timeoutInterval);
 }

 function initTimeout(timeoutInterval) 
 {
	_timeoutInterval = timeoutInterval;
	resetTimeout();
	if (document.onkeydown == null)
		document.onkeydown = resetTimeout;
	if (document.onclick == null)
		document.onclick = resetTimeout;
 }

// security window

function OpenSecurityWindow()
{
	SecurityWindow = window.open('http://www.webjet.com.au/standalone/secured-page/', 'SecurityWindow', 'height=500, width=600,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes');
}


function OpenNewSecurityWindow(obj)
{
    var str='webjet';
    if(obj!='')
    {
        str=obj;
    }
    SecurityWindow = window.open('http://www.' + obj + '.com.au/standalone/secured-page/', 'SecurityWindow', 'height=500, width=600,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes');
}



// From Portal Framework
__doPostBack = function(eventTarget, eventArgument)
{
    onPrePostBack(eventTarget, eventArgument);
    var theform;
    if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
        theform = document.forms["ControllerForm"];
    }
    else {
        theform = document.ControllerForm;
    }
    theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
    theform.__EVENTARGUMENT.value = eventArgument;
    theform.submit();
}

function onPrePostBack(eventTarget,eventArgument)
{
	if(eventTarget == null || eventTarget == ''){return;}
	var theform = document.ControllerForm;
	eventTarget = eventTarget.replace(/:/g,'_');
	eventTarget = eventTarget.replace(/\$/g,'_');
	var candidateControl = 'PFControlArray_' + eventTarget ;
	var controlFound = false;
	for(i = 0; i < PF_ControlArray.length; i++)
	{
		if(PF_ControlArray[i] == candidateControl)
		{
			controlFound = true;
			break;			}
	}
	if(!controlFound){return;}
	var PageID = eval('PFControlArray_' + eventTarget + '[0]');
	var LayoutID = eval('PFControlArray_' + eventTarget + '[1]');
	var Bookmark;		
	if(PageID == null){Bookmark = false}else{Bookmark = true};
	theform.action=bookmarkquerystring(PageID,LayoutID,Bookmark);
}


function bookmarkquerystring(PageID,LayoutID,Bookmark)
{
	var theform = document.ControllerForm;
	var initAction = new String(theform.action);		
	var initActionArr = initAction.split('?');
	var newAction = new String(initActionArr[0]);
	var newQS = new String('');
	if(initActionArr.length > 1)
{
		if(initActionArr[1].indexOf('PageID=',0) < 0 && Bookmark)
		{
			newQS = addQSBit(newQS, 'PageID=' + PageID);
		}
		if(initActionArr[1].indexOf('LayoutID=',0) < 0 && Bookmark)
		{
			newQS = addQSBit(newQS, 'LayoutID=' + LayoutID);
		}
		var initQSNVPairsArr = initActionArr[1].split('&');
		if(initQSNVPairsArr.length >= 0)
		{
			for(var i=0;i < initQSNVPairsArr.length;i++)
			{
				var currPair = initQSNVPairsArr[i];
				if(currPair.indexOf('PageID',0) == 0)
				{
					if(Bookmark == true)
					{
						newQS = addQSBit(newQS, 'PageID=' + PageID);
					}
				}
				else if(currPair.indexOf('LayoutID',0) == 0)					{
					if(Bookmark == true)
					{
						newQS = addQSBit(newQS, 'LayoutID=' + LayoutID);
					}
				}
				else
				{
					newQS = addQSBit(newQS ,currPair);
				}
			}
		}
		else
		{
			return initAction + getBMAddon(PageID,LayoutID,Bookmark);
		}
	}
	else
	{
		return initAction + getBMAddon(PageID,LayoutID,Bookmark);
	}
	if(newQS.length > 0)
	{
		return newAction + '?' + newQS;
	}
	else
	{
		return newAction;
	}
}
function addQSBit(baseQS ,currPair)
{
	if(baseQS.length > 0)
	{
		return baseQS + '&' + currPair;
	}
	else
	{
		return baseQS + currPair;
	}
}
function getBMAddon(PageID,LayoutID,Bookmark)
{
	if(Bookmark == true){return '?PageID=' + PageID + '&LayoutID=' + LayoutID}else{return ''}
}
function UnpauseHiddenPolling(top)
{
	 if(top!=undefined)
	 {
	 	if (typeof (top.hidden) != 'undefined' && typeof (top.hidden.unpausePolling) != 'undefined')
	     {
	       top.hidden.unpausePolling();
         }
	 }  
}
function PauseHiddenPolling(top) {
	if (top != undefined) {
		if (top.hidden != undefined) {
			top.hidden.pausePolling();
		}
	}
}

////TODO: move to common JS code or use AJAX standard control
//// from http://p2p.wrox.com/topic.asp?TOPIC_ID=4135 style.visiblity makes the element visible or hidden, it is still rendered and takes up space on the page even if you can't see it. 
//If you set style.display to "none" the markup is not processed and does not take up space on the page.
//		 // document.getElementById(IDdivCollapsed).style.visibility = 'visible';
// -->
  function ShowCollapsableExpandedPair(bShow, IDdivCollapsed, IDdivExpanded)
  {
 // debugger;
	  if (bShow) 
	  {
		  document.getElementById(IDdivExpanded).style.display = '';//or 'block' from none
		  document.getElementById(IDdivCollapsed).style.display = 'none';
	  }
	  else 
	  {
		  document.getElementById(IDdivExpanded).style.display = 'none';
		  document.getElementById(IDdivCollapsed).style.display = '';//or 'block'
	  }
  }

function OpenPrintItineraryWindow()
{
	newwin = window.open('printitinerary.aspx?Lang=' + WebjetTsaResource.LocaleId);
}
function executeEnterKey(buttonToClick) {
    if (event.keyCode == 13) {
        event.cancelBubble = true;
        event.returnValue = false;
        document.getElementById(buttonToClick).click();
    }
}
function setControlIndexByID(ID, value) {
    document.getElementById(ID).selectedIndex = value;
}
function getControlIndexByID(ID) {
    return document.getElementById(ID).selectedIndex;
}
function padTime(value) {
    res = value.toString();
    if (res.length == 1)
        res ='0' + res;
    return res;
}
