var timer;
var myTripsResults;
var myTripsProgress;
var ajax_req;
var itineraries;
/* should get called by home.jsp's body onload event - 
	init variables and initiate first call to fetch itineraries*/
function pageLoaded(itin) {
	itineraries=itin;
	myTripsResults = document.getElementById('myTripsResults');
	myTripsProgress = document.getElementById('myTripsProgress');	
	if(myTripsResults!=null&&myTripsProgress!=null)					
	loadMyTrips(itineraries);
}
/* check for ie/firefox to get proper XML HTTP object */
function getAjaxObject() {
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else
		return new XMLHttpRequest();
}
/* initiate call to get list of itineraries via AJAX */
function loadMyTrips (itineraries) {
	ajax_req = getAjaxObject();				
	ajax_req.open('GET', 'my_trips.jsp?itin='+itineraries, true);
	ajax_req.onreadystatechange = onAjaxResponseAction;
	ajax_req.send(null);
}

function showAll() {
	ajax_req = getAjaxObject();				
	ajax_req.open('GET', 'my_trips.jsp', true);
	ajax_req.onreadystatechange = onAjaxResponseAction;
	ajax_req.send(null);
}
/* called when AJAX gets a reply from server - here we want to check if we got a valid response
	-if valid response, use it as html to fill in our results div 
	-otherwise, means we are in holding, so set a timeout and wait before trying again */
function onAjaxResponseAction () {
	if (ajax_req.readyState == 4) {
    	if (trim(ajax_req.responseText) == '') {
    		timer = setTimeout("loadMyTrips("+itineraries+")", 2000);
    	} else {			    
			html = ajax_req.responseText;
	    	myTripsResults.innerHTML = html.substring(html.indexOf('<body>')+6, html.indexOf('</body>')+7);
	        myTripsProgress.style.display = 'none';
	        myTripsResults.style.display = '';
        }
    }
}
/* called when user clicks Refresh link, this request is like the normal request except the controller
 	will try to refresh the results from the GDS so it might take some time */
function refreshMyTrips () {

    myTripsProgress.style.display = '';
    myTripsResults.style.display = 'none';
       
    ajax_req = getAjaxObject();				
	ajax_req.open('GET', 'my_trips.jsp?refresh=y', true);
	ajax_req.onreadystatechange = onAjaxResponseAction;			
	ajax_req.send(null);
}
function submitPNR(itinid,activity) {
	document.getpnrs.itineraryid.value=itinid;
	document.getpnrs.active.value=activity;
	document.getpnrs.submit();
}
function trim( value ) {	
	return value.replace(/\s*((\S+\s*)*)/, '$1').replace(/((\s*\S+)*)\s*/, '$1');
}
