
function buy_tix(reserve_only)
{
  var params;
  var event_id = 0;
  var i;

  if ( ! parent.frames[2].loaded ) {
    return false;
  }

  if ( parent.frames[2].document.forms.length > 1 && parent.frames[2].document.forms[1].name == "section_info" ) {
    event_id = parent.frames[2].event_id;
    params = get_form_selections(parent.frames[2].document.forms[1]) + "&event_id=" + event_id;
  } else if ( parent.frames[2].document.applets.length > 0 ) {
    var java_app = parent.frames[2].document.applets[0];
    params = "" + java_app.get_selection(3);
    if ( parent.frames[2].document.forms.length > 1 ) {
      var index = parent.frames[2].document.forms[1].event_select.selectedIndex;
      if ( index <= 0 ) {
        if (window.confirm("You should select an event before buying tickets.\nWould you like to browse SeatTrader?")) {
          parent.document.location = "/trader/search.html";
	}
	return false;
      }
      var value = parent.frames[2].document.forms[1].event_select.options[index].value;
      if ( (i = value.indexOf('/')) < 1 ) {
        return false;
      }
      event_id  = parseInt(value.substring(0,i));
      params = params + "&event_id=" + event_id;
    } else {
      if (window.confirm("No tickets available for this venue.\nWould you like to browse SeatTrader?")) {
        parent.document.location = "/trader/search.html";
      }
      return false;
    }
    store_selections();
  } else {
    params = parent.frames[2].query_string;
    if ( parent.frames[2].event_id ) {
      event_id = parent.frames[2].event_id;
      params = params + "&event_id=" + parent.frames[2].event_id;
    }
  }

  if ( reserve_only != 'price_key' && params && params.indexOf("seat0") < 0 ) {
    window.alert("Please select at least one seat for this operation");
    return false;
  }

  if ( event_id <= 0 ) {
    if (window.confirm("Sorry, no tickets available from this page.\nWould you like to browse SeatTrader?")) {
      parent.document.location = "/trader/search.html";
    }
    return false;
  }


  if (reserve_only == 'price_key') {  
    // dga- hack to display price key instead of proceeding with Buy Tickets action
    // If above validations succeed, OK to show price key, since we have an event_id
    var url = '/cgi/price_key.pl';
    url = url + "?action=display";
    url = url + "&event_id=" + escape(event_id);

    new_win = window.open(url, 'price_key', 'width=500,height=400,status=1,resizable=0,scrollbars=1');
    new_win.focus();

    //dga- I'm putting this return false since other js functions seem to do that.  But don't know why.
    return false;
  }


  if ( parent.frames[2].document.forms.length > 0 && parent.frames[2].document.forms[0].name == "param_info" ) {
    var theform = parent.frames[2].document.forms[0];
    theform.params.value = params;
    if ( reserve_only ) { 
      //
      // NB: 'target' object is read-only in IE 3...
      //
      theform.target = "_top";
      theform.action = "/cgi/lookup_venue_dyn.pl";
      store_cookie("SEATADVISOR_RESERVE", "1");
    } else {
      theform.target = "_self";
      theform.action = "/cgi/trader/lookup_ad_detail.pl";
    }
    theform.submit();
  }
  return false;
}

function get_form_selections(theform)
{
  var i = 0, j = 0;
  var selections = "venue_name=" + parent.frames[2].venue_name + "&layout_desc=" + parent.frames[2].layout_desc;

  if ( theform.section ) {
    if ( theform.section.checked ) {
      selections = selections + "&seat" + j++ + "=" + theform.section.value;
    }
  }
  if ( theform.rows ) {
    if ( theform.rows.length ) {
      for ( i = 0; i < theform.rows.length; i++ ) {
        if ( theform.rows[i].checked ) {
          selections = selections + "&seat" + j++ + "=" + theform.rows[i].value;
        }
      }
    } else if ( theform.rows.checked ) {
      selections = selections + "&seat" + j++ + "=" + theform.rows.value;
    }
  }
  if ( theform.seats ) {
    if ( theform.seats.length ) {
      for ( i = 0; i < theform.seats.length; i++ ) {
        if ( theform.seats[i].checked ) {
          selections = selections + "&seat" + j++ + "=" + theform.seats[i].value;
        }
      }
    } else if ( theform.seats.checked ) {
      selections = selections + "&seat" + j++ + "=" + theform.seats.value;
    }
  }
  if ( theform.seats_tt ) {
    if ( theform.seats_tt.length ) {
      for ( i = 0; i < theform.seats_tt.length; i++ ) {
        if ( theform.seats_tt[i].checked ) {
          selections = selections + "&seat" + j++ + "=" + theform.seats_tt[i].value;
        }
      }
    } else if ( theform.seats_tt.checked ) {
      selections = selections + "&seat" + j++ + "=" + theform.seats_tt.value;
    }
  }
  return selections;
}

var action_time = null;

function do_action(act)
{
  var win = this;
  if ( eval('parent') && eval('parent.frames') && eval('parent.frames[2]') )
  {
    win = parent.frames[2];
  }

  var params;

  //if ( name == (act + "nav") || (! win.loaded) ) {
  if ( name == (act + "nav") ) {
    return false;
  }

  // Keep 'em from swamping us with clicks/CGI requests.
  if ( action_time ) {
    time_now = new Date();

    if ( (time_now.getTime() - action_time.getTime()) < 2000 ) {
      window.status='One moment please...';
      return false;
    }
  }

  if ( win.document.forms.length > 1 && win.document.forms[1].name == "section_info" ) {
    params = get_form_selections(win.document.forms[1]);
    if ( win.event_id ) {
      params = params + "&event_id=" + win.event_id;
    }
  } else if ( win.document.applets.length > 0 ) {
    var java_app = win.document.applets[0];
    params = "" + java_app.get_selection(3);
    store_selections();
  } else {
    params = win.query_string;
  }

  if ( act != "venue" && act !="venue_nf" ) {
    var begin, end, num_seats;

    if ( (begin = params.lastIndexOf("seat")) < 0 ) {
      window.alert("Please select at least one seat for this operation");
      return false;
    }

    begin += 4;
    end = params.indexOf('=', begin);
    num_seats = parseInt(params.substring(begin,end));

    if ( num_seats >= 10 ) {
      if ( !window.confirm("There are " + num_seats + " seats in your selection.\nIt could take time to retrieve all " + act + " information.\n\nContinue?") ) {
        return false;
      }
    }
  }

  action_time = new Date();

  var target = "";
  var dims = "";
  if ( act == "seat" || act == "view" || act == "review" ) {
    if ( act == "seat" || act == "view" ) {
      params = "photo_type=" + act + "&" + params;
      act = "photo";
    }
    target = "sainfo";
    dims = 'width=400,height=480';
  } else if ( act == "venue_nf" || act == "comnt" ) {
    target = act;	// will not work in MSIE 3.x
    dims = 'width=740,height=480';
  }

  if ( target != "" ) {
    var new_win = window.open('', target, dims + ',status=1,resizable=1,scrollbars=1');
      if (parseInt(navigator.appVersion) >= 4 ) {
        new_win.focus();
      }
  }

  if ( win.document.forms.length > 0 && win.document.forms[0].name == "param_info" ) {
    var theform = win.document.forms[0];
    theform.params.value = "venue_code=" + win.venue_code +
                           "&layout_code=" + win.layout_code +
                           "&section_id=" + win.section_id +
                           "&event_id=" + win.event_id +
			   "&" + params;
    theform.action = "/cgi/make_" + act + ".pl";
    theform.target = target;
    theform.submit();
    return false;
  }
  return false;
}

//
// Create event info popup window.
//
function pop_event_info_win(event_id, lsid)
{
  var url = location.protocol + '//' + location.host + '/cgi/trader/event_info.pl' +
                                                       '?event_id=' + event_id +
                                                       '&lsid=' + lsid;

  var pop_win = window.open(url, 'sa_event_info', 'width=350,height=475,status=1,resizable=1,scrollbars=1');
  pop_win.focus();
  return false;
}


function goto_map(sec) {
  var section_id = parent.frames[2].section_id;
  if ( sec || parent.frames[2].document.forms.length > 1 && parent.frames[2].document.forms[1].name == "section_info" ) {
    section_id = 0;
  }
  if ( parent.frames[2].loaded ) {
    parent.document.location.replace("/cgi/lookup_venue_dyn.pl" + 
					        "?venue_code="  + parent.frames[2].venue_code +
					        "&layout_code=" + parent.frames[2].layout_code +
					        "&section_id="  + section_id +
					        "&event_id="    + parent.frames[2].event_id);
  }
  return false;
}

function go_reserve()
{
  if ( parent.frames[2].loaded && parent.frames[2].document.applets.length > 0 ) {
    var java_app = parent.frames[2].document.applets[0];
    var result = java_app.set_reserved(true);
    if ( result != "" ) {
      window.alert(result);
    }
  }
  return false;
}

function go_clear()
{
  if ( parent.frames[2].loaded && parent.frames[2].document.applets.length > 0 ) {
    var java_app = parent.frames[2].document.applets[0];
    java_app.clear_selections(true);
  } else if ( parent.frames[2].document.forms.length > 1 && parent.frames[2].document.forms[1].name == "section_info" ) {
    parent.frames[2].document.forms[1].reset();
  }
  store_cookie("SEATADVISOR_SELECTION", "");
  return false;
}

function validate_venue_name(theform)
{
  if ( theform.venue_name.value == "" ) {
    window.alert("Please specify a venue search pattern.");
    return false;
  }
  return true;
}

function do_submit(theform)
{
  if ( validate_venue_name(theform) ) {
    theform.submit();
  }
  return false;
}
