//var current_tab = "lodging";
var criterion_index = -1;
var table_fields;
var last_alias_text;
var last_alias_crit;

/*****************************************************************************
 *						XMLHTTPRequest Initialization						 *
 *****************************************************************************/
/*
 * xmlHTTP()
 *
 * Construct and initialize an xmlHTTP object for communication with the web
 * server.
 *
 * Returns an initialized xmlHTTP object on success; boolean FALSE on failure.
 */
function xmlHTTP() {
	var xmlhttp;
	var moz = (typeof document.implementation != 'undefined') && (typeof
		document.implementation.createDocument != 'undefined');

	/* Are we using Mozilla? */
	if (moz)
	{
		if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		    try {
				xmlhttp = new XMLHttpRequest();
			}catch (e) {
				xmlhttp = false;
			}
		}
		return xmlhttp;
	}
	else
	{

		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (E) {
				xmlhttp = false;
			}
		}

		if (!xmlhttp)
		{
			alert("Attempting IE initialization: " + failed);
			return false;
		}

		return xmlhttp;
	}
}

var http = xmlHTTP();

/*
 * Alias code.
 */

/**
 *
 * @access public
 * @return void
 **/
function view_alias_response()
{
	if (http.readyState == 4)
	{
		if (http.responseText == "error")
		{
			alert("Oops!  There was an error adding your alias.  Please try again.");
			exit;
		}

		alias_id = http.responseText;

		alias_container = document.getElementById("aliases_container");

		alias_container.innerHTML +=
			"<input type=\"checkbox\" name=\"use_alias*" + alias_id + "\""
			+ " id=\"use_alias*" + alias_id + "\">"
			+ " <a href=\"javascript:check_alias(" + alias_id + ")\" title=\""
			+ last_alias_crit + "\">"
	 		+ last_alias_text
	 		+ "</a><br>";
	}

	alias_container = document.getElementById("field_alias*" + criterion_index);
	alias_container.className = "alias_saved";
	alias_container.innerHTML = "Saved as \"" + new_alias + "\".";
}

/**
 *
 * @access public
 * @return void
 **/
function add_alias(alias_name, alias_text)
{
	var url = "/admin/aliases.php?c=new&n="
		+ alias_name
		+ "&t="
		+ alias_text;

	last_alias_text = alias_name;
	last_alias_crit = alias_text;

	http.open('GET', url, true);
	http.onreadystatechange = view_alias_response;
	http.send(null);
}

/**
 *
 * @access public
 * @return void
 **/
function show_air_options()
{
	document.getElementById("vehicletravel_options").style.display = "none";
	//document.getElementById("traintravel_options").style.display = "none";
	document.getElementById("airtravel_options").style.display = "block";
}

/**
 *
 * @access public
 * @return void
 **/
function show_vehicle_options()
{
	document.getElementById("vehicletravel_options").style.display = "block";
	//document.getElementById("traintravel_options").style.display = "none";
	document.getElementById("airtravel_options").style.display = "none";
}

/**
 *
 * @access public
 * @return void
 **/
function show_train_options()
{
	document.getElementById("vehicletravel_options").style.display = "none";
	//document.getElementById("traintravel_options").style.display = "block";
	document.getElementById("airtravel_options").style.display = "none";
}

/*
 * Code to handle the preparation of lists, primarily for population of
 * HTML "select" elements.
 */

/* Fetch a list of pathologists */
function view_fields_response()
{
	if (http.readyState == 4)
	{
		if (http.responseText == "error")
		{
			alert("Oops!  There was an error retrieving the fields list from the server.");
			exit;
		}

		var fields_array = http.responseText.split(",");
		var fields_index = 0;
		table_fields = fields_array; // Store for later use.

		// Set up a first criterion if there aren't any already.
		if (criterion_index == -1)
		{
			next_criterion();
		}

		target_list = document.getElementById("field_name*" + criterion_index);
		target_list.options.length = 0;

		selected_fields_table = "<table class=\"selected_columns\"><tr><td>";

		while(fields_index < fields_array.length)
		{
			if (fields_array[fields_index] == "")
			{
				++fields_index;
				continue;
			}

			if (fields_index % 10 == 0 && fields_index != 0)
				selected_fields_table += "</td><td valign=\"top\">";

			selected_fields_table += "<input type=\"checkbox\" name=\"show_field*"
				+ fields_array[fields_index]
				+ "\"> "
				+ fields_array[fields_index]
				+ "<br>";

			target_list.options[fields_index]
				= new Option(fields_array[fields_index],
							fields_array[fields_index],
							false,
							false);



			++fields_index;
		}

		selected_fields_table += "</tr></table>";

		columns_container = document.getElementById("select_columns_container");
		columns_container.innerHTML = selected_fields_table;

		return;
	}
}

function view_fields_list()
{
	target_loading = document.getElementById("select_columns_container");
	target_loading.innerHTML = "<a class=\"caption_text\">[Loading columns; please wait.]</a>";

	target_table = document.getElementById("source_table");

	selected_table = target_table.options[target_table.selectedIndex].text;

	if (selected_table == "")
		return false;

	var url = "/admin/table_fields.php?t=" + selected_table;
	http.open('GET', url, true);
	http.onreadystatechange = view_fields_response;
	http.send(null);
}

/**
 *
 * @access public
 * @return void
 **/
function change_tab(current_tab, new_tab)
{
	//get the object to the active section and hide it
	current = document.getElementById(current_tab + "_div");
	current.style.display = "none";

	//get the object to the selected section and show it
	source = document.getElementById(new_tab + "_div");
	source.style.display = "block";

	//change the color of the button to the previous active section
	old_tab = document.getElementById(current_tab + "_tab");
	old_tab.style.borderBottom = "1px solid silver";
	old_button = document.getElementById(current_tab + "_button");
	old_button.style.border = "1px solid silver";
	old_button.style.cursor = "pointer";
	old_button.style.background = "white";
	old_button.style.color = "black";

	//current_tab = new_tab;	//set the new active section

	//change the color of the new active section button
	tab = document.getElementById(new_tab + "_tab");
	tab.style.borderBottom = "none";
	button = document.getElementById(new_tab + "_button");
	button.style.color = "white";
	button.style.border = "1px solid black";
	button.style.cursor = "text";
	button.style.background = "#2F5AA2";
}

/**
 *
 * @access public
 * @return void
 **/
function expand(div)
{
	if (div == null)
	{
		div = "hotel_confirm_container";
	}
	document.getElementById(div).style.display = "block";
}

/**
 *
 * @access public
 * @return void
 **/
function collapse(div)
{
	if (div == null)
	{
		div = "hotel_confirm_container";
	}
	document.getElementById(div).style.display = "none";
}

/**
 *
 * @access public
 * @return void
 **/
function check_alias(alias_id)
{
	target_checkbox = document.getElementById("use_alias*" + alias_id);

	// Whatever the checked state is, reverse it.
	target_checkbox.checked = !target_checkbox.checked;
}

/**
 *
 * @access public
 * @return void
 **/
function criterion_alias()
{
	new_alias = prompt("Please type a friendly name for the alias, such as \""
		+ "Has brown eyes\".", "Type your alias here.");

	// Collect some details.
	new_alias_name = new_alias;
	new_alias_crit = document.getElementById("field_name*" + criterion_index).value
		+ "*eq*'" + document.getElementById("field_value*" + criterion_index).value
		+ "'";

	add_alias(new_alias_name, new_alias_crit);
}

/**
 *
 * @access public
 * @return void
 **/
function next_criterion()
{
	live_target = document.getElementById("live_criteria_table");
	target = document.getElementById("criteria_container");

	++criterion_index;

	logic_html = new String();
	column_html = new String();
	op_html = new String();
	value_html = new String();

	if (criterion_index != 0)
	{
		logic_html += "<select name=\"field_"
			+ criterion_index
			+ "_logic\"><option>AND</option><option>OR</option></select>";
	}

	column_html += "  <select id=\"field_name*"
		+ criterion_index
		+ "\" name=\"field_name*" + criterion_index + "\">";

	var crit_index = 0;

	while(crit_index < table_fields.length)
	{
		column_html += "<option>" + table_fields[crit_index] + "</option>";
		++crit_index;
	}

	column_html += "</select>";

	op_html += " <select name=\"field_op*" + criterion_index + "\">"
		+ "<option value=\"=\">equals<option value=\"!=\">does not equal</select>";

	value_html += " <input type=\"text\" id=\"field_value*"
		+ criterion_index
		+ "\" name=\"field_value*"
		+ criterion_index + "\"> "
		+ "<input type=\"button\" onClick=\"javascript:next_criterion();\" value=\"+\">";
		//+ " <span id=\"field_alias*" + criterion_index + "\"> "
		//+ "<a href=\"javascript:criterion_alias()\">Add quick alias</a>"
		//+ "</span>";

	if (criterion_index != -1)
		value_html += "<br>";

	next_row = live_target.insertRow(criterion_index);

	logic_cell = next_row.insertCell(0);
	logic_cell.innerHTML = logic_html;

	column_cell = next_row.insertCell(1);
	column_cell.innerHTML = column_html;

	op_cell = next_row.insertCell(2);
	op_cell.innerHTML = op_html;

	value_cell = next_row.insertCell(3);
	value_cell.innerHTML = value_html;
}

/**
 *
 * @access public
 * @return void
 **/
function change_title(new_title, location, date)
{
	document.getElementById('title_div').style.display = "block";
	document.getElementById('title').innerHTML = new_title;
	document.getElementById('location').innerHTML = location;
	document.getElementById('date').innerHTML = date;
}

/**
 *
 * @access public
 * @return void
 **/
function hide_text()
{
	document.getElementById('title_div').style.display = "none";
}




function check_training()
{
	if (document.getElementById('City').value == "")
	{
		alert("You must enter a city");
		return false;
	}
	else if (document.getElementById('State').value == "")
	{
		alert("You must enter a state");
		return false;
	}
	else if (document.getElementById('num_participants').value == "" || document.getElementById('num_participants').value == "0")
	{
		alert("You must enter the number of participants.");
		return false;
	}
	else if (document.getElementById('Title').value == "")
	{
		alert("You must enter a title for your presentation.");
		return false;
	}
	else if (document.getElementById('ParticipantType').value == "")
	{
		alert("You must enter the participant types.");
		return false;
	}
	else
	{
		alert("Good Job");
		return false;
	}
}