var waitpage = null;
$(function() {
	waitpage = new WaitPage();
});

/**
 * Pagination: Go to the page number specified.
 * @param Number pNo
 */
function gotoPage(tripType, pNo){
	loadToBox( 'requestFor=navigatePage&pg=' + pNo, tripType );
}

/**
 * Sort trips list
 * @param String tripType
 * @param String sortBy
 * @return
 */
function sortList(tripType, sortBy){
	loadToBox( 'requestFor=sortList&sortTripBy=' + sortBy, tripType );
}

/**
 * Removes trip for the ID specified
 * @param String id
 * @return
 */
function removeTrip(id){
	if( confirm("Are you sure you want to remove trip # " + id + " ?") ){
		loadToBox( 'requestFor=removeTrip&tripId=' + id, 'saved' );
	}
}

/**
 * View saved trip for the ID specified
 * @param String id
 * @return
 */
function viewSavedTrip(id, source){
	document.savedTripForm.action = "/aev/mytrips.action?requestFor=viewSavedTrip&tripId="+id;
	document.savedTripForm.submit();
	if( waitpage ){waitpage.show('LOADING YOUR SELECTED TRIP',2000);}
}

/**
 * View booked trip for the ID specified
 * @param String id
 * @return
 */
function viewBookedTrip(id, source){
	document.bookedTripForm.action = "/aev/mytrips.action?requestFor=viewBookedTrip&tripId="+id;

	if (source )
		document.bookedTripForm.action += "&source=" + source;

	document.bookedTripForm.submit();
	if( waitpage ){waitpage.show('LOADING YOUR BOOKED TRIP',2000);}
}

/**
 * Loads trips list to page via ajax
 * @param String params
 * @param String tripType
 * @return
 */
function loadToBox(params,tripType){
	params = encodeURI(params+'&tripIs='+tripType);

	var box = '#'+tripType+'TripsBox';
	var waitBox = '#'+tripType+'TripsWaitBox';
	$(box).hide();
	$(waitBox).show();
	$(box).load('/aev/tripslist.action?'+params, null, function(){
		$(waitBox).hide();
		$(box).fadeIn('fast');
	});
}
/**
 * updating trip experience rating
 * @param String params
 * @return
 */
function updateTripExpRating(){
	document.rateExpFrm.action = "/aev/ratingExp.action";
	document.rateExpFrm.submit();
	if( waitpage ){waitpage.show('UPDATING YOUR TRIP EXPERIENCE RATING',2000);}
}
/**
 * back to previous page
 * @param String params
 * @return
 */
function backToPrevious(actionName){
	alert(actionName);
	document.dataform.action = actionName+"?source=myTrips";
	document.dataform.submit();

}

/**
 * get destination guide for destcode
 * @param String params
 * @return
 */
function getDestinationGuides(airportCode)
{
   loadXMLDocument(airportCode);

   var searchterm = airportCode;
   var allCodeItems = xmlDoc.getElementsByTagName("code");
   var allUrlItems = xmlDoc.getElementsByTagName("url");
   var allNameItems = xmlDoc.getElementsByTagName("name");

   for (var i=0; i < allCodeItems.length; i++)
   {
		var url = allCodeItems[i].lastChild.nodeValue;
		var exp = new RegExp(searchterm, "i");
		if ( url.match(exp) != null) 
		{
			var htmlStr = "<b>Get your Trip Advisor Destination Guide:</b> Know before you go! Great places to eat, sleep and play in ";
				htmlStr += allNameItems[i].lastChild.nodeValue + "!<br/><br/>";
				htmlStr += "<img border=\"0\" src=\"https://www.americanexpressvacations.com/images/hotel/aev_arrowbullet.gif\" width=\"8\" height=\"8\">";
				htmlStr += "&nbsp;&nbsp;<a href=\"";
				htmlStr += allUrlItems[i].lastChild.nodeValue + "\" target=\"_blank\" class=\"style41 hrefstyle\">Download "; 
				htmlStr += allNameItems[i].lastChild.nodeValue;
				htmlStr += " Guide (PDF)</a><br/>";

			document.write(htmlStr);			
		    return allUrlItems[i].lastChild.nodeValue;
		}
   }
   return "";
}

/**
 * load xml guide for destcode
 * @param String params
 * @return
 */
function loadXMLDocument( airportCode )
{
   var url = "/scripts/data/destinationguide.xml";

   try
   {
      xmlDoc=document.implementation.createDocument("","",null);
      xmlDoc.async=false;
      xmlDoc.load(url);
   }
   catch(e)
   {
      try //Google Chrome
      {
         var xmlhttp = new window.XMLHttpRequest();
         xmlhttp.open("GET", url, false);
         xmlhttp.send(null);
         xmlDoc = xmlhttp.responseXML.documentElement;
      }
      catch(e)
      {
         error=e.message;
      }
   }
}
