registerNS("EXL.UI");

// search-specific derivation of EXL (not YUI) dialog
EXL.UI.SearchDialog = Class.create(EXL.UI.Dialog, {
	depart_date : null,
	return_date : null,
	origin_tla : null,
	dest_tla : null,
	num_adult : null,
	num_senior : null,
	num_youth : null,
	num_child : null,
	num_infant : null,
	infant_in_seat : null,
	
  // instance constructor
  initialize: function($super, dlgName, dlgPropHash, buttonPropHash, onSuccessFn, onFailureFn) {
    // subvert center method and init superclass
    $super(dlgName, dlgPropHash, buttonPropHash, onSuccessFn, onFailureFn);
    this._dlgBox.center = this._center.bind(this._dlgBox);
    this._dlgBox.cfg.setProperty('fixedCenter', true, false);
  },
  
  // subverted yui centering function
  _center: function()
  {
    var nViewportOffset = YAHOO.widget.Overlay.VIEWPORT_OFFSET,
        elementWidth = this.element.offsetWidth,
        elementHeight = this.element.offsetHeight,
        viewPortWidth = YAHOO.util.Dom.getViewportWidth(),
        viewPortHeight = YAHOO.util.Dom.getViewportHeight(),
        x,
        y;

    if (elementWidth < viewPortWidth) {
        x = (viewPortWidth / 2) - (elementWidth / 2) + YAHOO.util.Dom.getDocumentScrollLeft();
    } else {
        x = nViewportOffset + YAHOO.util.Dom.getDocumentScrollLeft();
    }

    if (elementHeight < viewPortHeight) {
        y = (viewPortHeight / 3) - (elementHeight / 3) + YAHOO.util.Dom.getDocumentScrollTop();
    } else {
        y = nViewportOffset + YAHOO.util.Dom.getDocumentScrollTop();
    }

    this.cfg.setProperty("xy", [parseInt(x, 10), parseInt(y, 10)]);
    this.cfg.refireEvent("iframe");
  },
  
  // search-specific validation
	validate: function() 
	{
		var err_nbr_dates = 0;
		var err_nbr_tlas = 0;
    var data = this._dlgBox.getData();
		
		// Pull out of search form
	  this.depart_date = data['depart_date'];
		this.return_date = data['return_date'];
		this.origin_tla = data['origin'];
		this.dest_tla = data['destination'];
		this.num_adult = data['num_adult'];
		this.num_senior = data['num_senior'];
		this.num_youth = data['num_youth'];
		this.num_child = data['num_child'];
		this.num_infant = data['num_infant'];
		this.infant_in_seat = data['infant_lap_or_seat'] == 'lap' ? 0 : 1;
		
		// Replace display city names with tlas (e.g. "Portland (PDX)" with just "PDX") 
    if ( this.origin_tla.match(/\(\w\w\w\)/) ) {  this.origin_tla = this.origin_tla.match(/\(\w\w\w\)/)[0].match(/\w\w\w/)[0]; }
    if ( this.dest_tla.match(/\(\w\w\w\)/) ) {  this.dest_tla = this.dest_tla.match(/\(\w\w\w\)/)[0].match(/\w\w\w/)[0]; }
    
		this._clearValidationErrors();
	
  	var tlasOK = validateTLAs(this.origin_tla, this.dest_tla);
  	var datesOK = validateDates(this.depart_date, this.return_date);
  	var travelersOK = validateTravelers(this.num_adult, this.num_senior, this.num_youth, this.num_child, this.num_infant, this.infant_in_seat);
  	
  	if( tlasOK && datesOK && travelersOK )
		{
		  disableResultsDisplay();
  	  var num_travelers = this.num_adult*1 + this.num_senior*1 + this.num_youth*1 + this.num_child*1 + this.num_infant*1;
    	showInterstitial(this.origin_tla, this.dest_tla, this.depart_date, this.return_date, num_travelers);
			return true;
    }
		else
		{
		  this._showValidationErrors();
			return false;
		}
	},

  // overridden show method
  show: function($super, cancel_btn, me)
  {
    // clear old validation errors
    this._clearValidationErrors();
    
    return $super(cancel_btn, me);
  },
  
  // private support methods
  _showValidationErrors : function()
  {
    $('client_validation_parent').style.display = 'block';
  },
  
  _clearValidationErrors : function()
  {
    $('client_validation_message').innerHTML = "";
    $('client_validation_parent').style.display = 'none';
  }

});

