// seats_check.js
// created for the Stanford Shakespeare Company's updated website 2009
// michael hammersley (michael.hammersley@gmail.com)
//
// it's AJAX coding to make sure that nobody can choose more seats than are
// available for a given performance. also makes things a little user-friendlier
// in case they already chose the number of seats they wanted before
// choosing the show they wanted.

var xmlHttp;
var MAX_SEATS = 8;

/////////////////////////////////////////////////////////////////////
// function .defaults (taken from http://parentnode.org/javascript/
//											default-arguments-in-javascript-functions/)
/////////////////////////////////////////////////////////////////////
// basically, what this allows for is adding default values to a javascript
// function by modifying the given function definition.
// when it finds '.defaults(var)' after a function declaration, it makes the values
// it finds to be the functions defaults.
Function.prototype.defaults = function()
{
  var _f = this;
  var _a = Array(_f.length-arguments.length).concat(
    Array.prototype.slice.apply(arguments));
  return function()
  {
    return _f.apply(_f, Array.prototype.slice.apply(arguments).concat(
      _a.slice(arguments.length, _a.length)));
  }
}

//////////////////////////////////////////////////////////////////////
// function GetNumSeats(pID)
//////////////////////////////////////////////////////////////////////
// input: pID - the performanceID for a given performance
// output: none.
// sets up the xmlHTTP request for the reservation form
// what it's designed to do is find the number of remaining sits for a
// given performance and create an optionslist accordingly.
function GetNumSeats(pID) {
	document.getElementById('reserve_button').disabled=true;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		return;
	}

	var url="php/get_seating.php";
	url += "?pid=" + pID;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


//////////////////////////////////////////////////////////////////////
// function stateChanged
//////////////////////////////////////////////////////////////////////
// input: none
// output: returns OptionsList(xmlHttp.responseText)
// i.e., if the xmlHttp request succeeded, pass the values to OptionsList
function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		//create options list and change it
		OptionsList(xmlHttp.responseText);
	}
}

//////////////////////////////////////////////////////////////////////
// function GetXmlHttpObject
//////////////////////////////////////////////////////////////////////
// input: none
// output: xmlhttp object depending on browser requirements
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
	 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	}	catch (e) {
	 //Internet Explorer
		try {
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}	catch (e) {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
//////////////////////////////////////////////////////////////////////
// function OptionsList
//////////////////////////////////////////////////////////////////////
// input: seats_remaining - the number of seats_remaining for a given performance
// output: none
// assigns html code to creating a number of option_value elements maxing out at the
// greater of MAX_SEATS or the number of seats remaining for the performance.
// also disables the form if there are no seats remaining.
var OptionsList = function(seats_remaining) {
	var cur_num = document.getElementById("num_reserved").value;
	var html = CreateOptions(cur_num,seats_remaining);

	document.getElementById("num_reserved_wrapper").innerHTML=html;
	if(seats_remaining < 1) {
		document.getElementById("num_reserved").disabled=true;
		document.getElementById("reserve_button").disabled=true;
		document.getElementById('error_message').innerHTML =
				'Reservations no longer available; waiting list available at door.';

	} else {
		document.getElementById("num_reserved").disabled=false;
		document.getElementById("reserve_button").disabled=false;
	}

} .defaults(MAX_SEATS);

///////////////////////////////////////////////////////////
// function CreateOptions
///////////////////////////////////////////////////////////
// input:	cur_num - number selected from previous choices of the form
//				seats_remaining - number of seats remaining in the performance
// output: html code defining the options for the select field
function CreateOptions(cur_num, seats_remaining) {

//	alert("cur-num: " + cur_num + "remaining: " + seats_remaining);
	var set_num = cur_num;
	if(parseInt(cur_num) > parseInt(seats_remaining)) {
		set_num = seats_remaining;
	}

	//because of an IE bug; there are easier methods for this for firefox, etc.
	html = "";
	html += "<label for=\"num_reserved\">Number of seats to reserve: </label>";
	html += "<select id=\"num_reserved\" name=\"num_reserved\">";
	if(seats_remaining == 0) {
		html += "<option value=\"0\">0</option>";
	}
	if(seats_remaining > MAX_SEATS) {seats_remaining = MAX_SEATS};
	for(var n = 1; n <= seats_remaining; n++) {
		html += "<option value=\"" + n + "\"";
		if(set_num == n) {html += " selected ";}
		html += ">" + n + "</option>";
	}
	html += "</select><br>";

	return html;
}