/**
 * Global variables and functions used by travel planner.
 * Everything is inside the xet namespace (variable).
 */


/**
 * How to recognise success of AJAX call according to Prototype
 * library.
 */
function XEAjaxSuccess(transport) {
	return !transport.status
	|| (transport.status >= 200 && transport.status < 300);
}

// duplicate from utils/Ajax.js
if (!document.baseURI) {
	var base = $(document.documentElement).down('base');
	if (base && base.href) {
		document.baseURI = base.href;
	}
}


var xet = {}
xet.ID = 0; // ID of the plan

/**
 * Simple prompt for a title. Add the title to the url and go to the url.
 * If the user press cancel, return false.
 */
xet.planNew = function(url) {
  xet.nameDialog(
	function(title) {
		location.href = document.baseURI+url+encodeURIComponent(title);
		return true;
	});
	return false;
}
xet.nameDialog = function(callback) {
  throw "This is nolonger operational";
  var text = "<form><label>Please enter a name for your new Travel Plan</label><input name='Name' />" +
        "<input type='submit' name='submit' value='Go' />" +
        "<input type='button' name='Cancel' value='Cancel' />" +
    "</form>";
    var a = new Control.Modal.Dialog(text, { 'onsubmit': function(data) {
      if(data.submit == 'Cancel') {
        Control.Modal.close();
        return false;
      }
      return callback(data.Name);
    }} ); 
}
xet.addToPlan = function(productID, planID) {
  var title = "";
  if(!planID) {
    planID = 0;
    xet.nameDialog(function(title) {
      var url = document.baseURI+"tp/addProduct/"+planID+"?"+ $H({ Class: "XEProduct", ID: productID, 'Title': title }).toQueryString();
      window.open(url, "TravelPlanner");
      return true;
    });
  }
}

/*
Event.observe(window, "load", function() {
  $$('li.plan').each(function(i) {
    var link = null;
    if(link = i.down('a.add')) {
        link.onclick = function() { new Ajax.Request(this.href, {
          onSuccess: function() {
            i.update("<a href='tp/' class='button inTravelPlanner added'>View "+Dict.get('TP.Name')+"</a>");
          }
        }); 
       return false;
      };
    } else {
      link = i.down('a.added');
      //do whatever needs to be done on the added link;
    }
  });
  
});
*/