﻿var ROOT = "";

var current_pkg = 0;
var current_selected_perf_list = [];
var current_selected_perf_list_index = 0;
var current_performance = 0;
var count_selected_groups = 0;
var NFSPkgSummary = null;
var selectedZone = 0;
var currentRadioBtn = null;
var multiplier = 1;

// default: false. Make zone selection by package when false
var SelectZoneByPerformance = false;
var selectedFlexZoneDescription = "";

var PerformanceGroups = [];
var nfspkg_num_perf_min = 0;
var nfspkg_num_perf_min = 0;

var pkg_production_min_max = { min: 0, max: 0, current: 0 };

var login_source_no = -1;

function setLoginSource(iSourceNo)
{
	try
	{
		login_source_no = parseInt(iSourceNo);
	}
	catch (err) { }
}


function flexitem( _perf_group_no, _perf_no, _pricetype, _quantity, _zone)
{
	this.perf_group_no = _perf_group_no;
	this.perf_no = _perf_no;
	this.pricetype = _pricetype;
	this.quantity = _quantity;
	this.zone = _zone;
}


function selectProduction(oChkBox, iProdSeasonNo)
{
	var req = getreq();

	this.handler = function()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				rslt = JSON.parse(req.responseText);
				try
				{
					rslt = rslt.d;
				}
				catch (err) { }
				if (rslt.Success)
				{
					pkg_production_min_max.current += (oChkBox.checked ? 1 : -1);
				}
				else
				{
					alert(rslt.ErrorMessage);
					if (oChkBox.checked)
						oChkBox.checked = false;
				}
				checkPackage(current_pkg);
			}
		}
	}

	req = getreq();
	req.open(HTTP_VERB, (srvr + WEBSERVICE + "MakePackageProductionSelection"), true);
	req.setRequestHeader(CONTENT_TYPE, APP_JSON);
	req.setRequestHeader(ORGANIZATION_CODE_KEY, ORGANIZATION_CODE);
	req.onreadystatechange = this.handler;
	req.send("{ prod_season_no: " + iProdSeasonNo + ", doAdd: " + oChkBox.checked + " }");	
}

function checkProductionList()
{
	var req;

	this.handler = function()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				rslt = JSON.parse(req.responseText);
				try
				{
					rslt = rslt.d;
				}
				catch (err) { }
				if (rslt.Success)
				{
					var message = "<p>Please select ";
					if (pkg_production_min_max.min == pkg_production_min_max.max)
						message += "" + pkg_production_min_max.min
					else
						message += "at least " + pkg_production_min_max.min + " and no more than " + pkg_production_min_max.max;
					message += " production titles from the list below";
					$("#ProductionSelection").html("<h3>Production Selection</h3>" + message + rslt.FormattedHTML);
					$("#ProductionSelection").slideDown();
					$("table.perfs").hide();
				}
			}
		}
	}

	if (document.getElementById('ProductionSelection') != null)
	{
		var pkg_production_selection = document.getElementById("pkg_production_selection");
		var lpkg_production_min_max = document.getElementById("pkg_production_min_max");
		if (pkg_production_selection && lpkg_production_min_max)
		{
			req = getreq();
			req.open(HTTP_VERB, (srvr + WEBSERVICE + "GetPackageProductionSelection"), true);
			req.setRequestHeader(CONTENT_TYPE, APP_JSON);
			req.setRequestHeader(ORGANIZATION_CODE_KEY, ORGANIZATION_CODE);
			req.onreadystatechange = this.handler;
			req.send("{prod_season_no: [" + pkg_production_selection.value + "] }");
			var mm = lpkg_production_min_max.value.split(",");
			if (mm)
			{
				if (mm.length == 1)
				{
					pkg_production_min_max = { min: mm[0], max: mm[0], current: 0 };
				}
				else if (mm.length > 1)
				{
					pkg_production_min_max = { min: mm[0], max: mm[1], current: 0 };
				}
				else
				{
					pkg_production_min_max = { min: 0, max: 0, current: 0 };
				}
			}
		}
	}
	else if (document.getElementById("package_multiplier") != null)
	{
		try
		{
			// multiplier = parseInt(document.getElementById("package_multiplier").value);
			// any "multiplying" is done at the purchase package call.
			$("table.perfs").hide();
		}
		catch (E) { }
	}
}

function getPackage(iSeason,iPkgNo)
{	
	current_pkg = iPkgNo;
	count_selected_groups = 0;

	var req = getreq();
	var pkg_no = iPkgNo;
	var season_no = iSeason;

	this.handler = function()
	{
		if (req.readyState == 4)
		{
			document.body.style.cursor = "default";
			if (req.status == 200)
			{
				rslt = JSON.parse(req.responseText);
				try
				{
					rslt = rslt.d;
				}
				catch (err) { }
				if (rslt.Success)
				{
					selectedFlexZoneDescription = "";
					current_selected_perf_list = [];

					currentPerfGroupPerfs = [];
					currentPerfGroupPerfsIndex = 0;

					current_selected_perf_list_index = 0;

					eval(rslt.JS);

					$('#pkg_detail' + season_no).html(rslt.FormattedHTML);
					checkProductionList(); // added 2008-11-06
					NFSPkgSummary = rslt.NFSPkgSummary;
					/*
					if (NFSPkgSummary != null)
					{
					// determine if there are boxes already checked
					try
					{
					count_selected_groups = parseInt($('#count_selected_groups').html());
					}
					catch (e) { }

					}
					*/
					var zone_table = document.getElementById("zone_table");
					if (zone_table)
					{
						var zone_table_radioinputs = zone_table.getElementsByTagName("input");
						if (zone_table_radioinputs && zone_table_radioinputs.length == 1)
							selectedFlexZoneDescription = "only zone";
					}

					try
					{
						// rslt.Message only exists when there's only 1 zone
						if (rslt.Message.length > 0)
						{
							var perfzone = rslt.Message.split(",");
							var tobj = getpackagehandler();
							tobj.zone = parseInt(perfzone[1]);
							quantities["pkg" + perfzone[0]] = tobj;
							selectedFlexZoneDescription = perfzone[2];
							var selectedRadio = document.getElementById("perfzone" + perfzone[0] + "_" + perfzone[1]);
							if (selectedRadio)
								selectedRadio.checked = true;
						}
						else
						{
							var zone_table = document.getElementById("zone_table");
							if (zone_table)
							{
								var zoneInputs = zone_table.getElementsByTagName("input");
								if (zoneInputs.length == 1)
								{
									zoneInputs[0].checked = true;
									selectedZone = zoneInputs[0].value;
								}
							}
						}
					}
					catch (err) { }
					// alert( req.responseText );

					try
					{
						if (accordion_mode)
							$('#accordion_holder' + iSeason).accordion("activate", 1);
					}
					catch (E) { }

					try
					{
						postPackageLoad(); // might not exist;
					}
					catch (e) { }
				}
				else
					alert(req.responseText);
			}
			else
				alert(req.responseText);

			if (currentRadioBtn)
				currentRadioBtn.style.cursor = "pointer";

		}
	}	
	
	if (req)
	{
		document.body.style.cursor = "wait";
		req.open(HTTP_VERB, ( srvr + WEBSERVICE + "GetPackage"), true);
		req.setRequestHeader( CONTENT_TYPE, APP_JSON );
		req.setRequestHeader( ORGANIZATION_CODE_KEY, ORGANIZATION_CODE );
		req.onreadystatechange = this.handler;
		req.send("{ pkg_no: " + iPkgNo + "}");
		if (currentRadioBtn)
			currentRadioBtn.style.cursor = "wait";
	}
}

var quantities = [ ];

function setZone(iPkgNo,iZoneNo)
{
	var tobj;
	if (quantities["pkg" + iPkgNo] == null)
		tobj = getpackagehandler();
	else
		tobj = quantities["pkg" + iPkgNo];
	if (iZoneNo)
	{
		tobj.zone = iZoneNo;
		quantities["pkg" + iPkgNo] = tobj;
		checkPackage(iPkgNo);
	}
}

function getpackagehandler()
{
	var tobj = { };
	tobj.qty = [];
	tobj.pt = [];
	tobj.zone = 0;
	return tobj;
}

function setPriceTypeQuantity(howmany, iPkgNo, iPriceType, ptindex)
{
	var tobj;
	if (quantities["pkg" + iPkgNo] == null)
		tobj = getpackagehandler();
	else
		tobj = quantities["pkg" + iPkgNo];

	for (var x=0; x <= ptindex; x++)
	{
		if (tobj.qty[x] == null)
		{
			tobj.qty[x] = 0;
			tobj.pt[x] = 0;
		}
	}
	tobj.qty[ptindex] = howmany;
	try
	{
		tobj.pt[ptindex] = iPriceType;
	}
	catch (e)
	{
		alert(e.description ? e.description : e);
	}

	if (SelectZoneByPerformance)
	{
		tobj.zone = -1; // mark it specially
	}
	else if (selectedZone > 0 )
		tobj.zone = selectedZone; // we only do this when there's only 1 zone to select and [hopefully] it's preselected
	else if (NFSPkgSummary)
	{
		if ( selectedPerfNo == 0 )
		{
			for( var x= (current_selected_perf_list_index - 1); x > -1; x-- )
			{
				selectedPerfNo = current_selected_perf_list[x].PerfNumber;
				if (selectedPerfNo > 0 )
					break;
			}
		}
		if ( selectedPerfNo > 0 )
			tobj.zone = GetZoneIDForPerformance(selectedPerfNo)
	}
	else
		tobj.zone = getZone();

	quantities["pkg" + iPkgNo] = tobj;
	if ( ! checkPackage(iPkgNo) )
	{
		var ddl = document.getElementById("p_" + iPkgNo + "pt_" + iPriceType );
		if (ddl)
			ddl.selectedIndex = 0; // will this generate an on-change event?
	}
}

function getZone()
{
	var zone_no = 0;
	var zone_table = document.getElementById("zone_table");
	if (zone_table)
	{
		var zone_radio_buttons = zone_table.getElementsByTagName("input");
		for (var x=0; x < zone_radio_buttons.length; x++ )
			if (zone_radio_buttons[x].checked )
			{
				zone_no = parseInt( zone_radio_buttons[x].value );
				break;
			}
	}
	return zone_no;
}

function validateNFSPackage()
{
	if (current_selected_perf_list != null && current_selected_perf_list.length > 0)
	{
		for (var y = 0; y < current_selected_perf_list.length; y++)
			if (current_selected_perf_list[y] != null)
				current_selected_perf_list[y].zone = zone_no;
	}

	count_selected_performances = 0;
	
	for (var x = 0; x < PerformanceGroups.length; x++)
	{
		var selected_perfs_this_group = 0;
		for (var y = 0; y < PerformanceGroups[x].performances.length; y++)
		{
			selected_perfs_this_group += (PerformanceGroups[x].performances[y].selected ? 1 : 0);
		}
		
		if ( PerformanceGroups[x].performances.length == 1 && PerformanceGroups[x].min == 1 && PerformanceGroups[x].max == 1 && ! PerformanceGroups[x].selected)
		{
			// these are required performances, but haven't been added yet, so we'll add them now.
			selected_perfs_this_group = 1;
			RecordFlexItem(new flexitem(PerformanceGroups[x].perf_group_no, PerformanceGroups[x].performances[0].perf_no, 0, 1, 0));
			PerformanceGroups[x].performances[0].selected = true;
			PerformanceGroups[x].selected = true;
		}
		
		count_selected_performances += selected_perfs_this_group;
		if (selected_perfs_this_group < PerformanceGroups[x].min || selected_perfs_this_group > PerformanceGroups[x].max)
		{
			if (selected_perfs_this_group < PerformanceGroups[x].min)
				alert("Please review the selection instructions above. You haven't selected enough items yet.");
			else if (selected_perfs_this_group > PerformanceGroups[x].max)
				alert("Please review the selection instructions above. You have selected too many items.");
			return false;
		}
	}
	if (!SelectZoneByPerformance && selectedFlexZoneDescription.length == 0)
		return false;


	/*
	for ( var x=0; x < NFSPkgSummary.Groups.length; x++ )
	{
		var curr_perf_group = NFSPkgSummary.Groups[x];
		
		var curr_perf_group_perf_count = 0;
		
		for ( var y = 0; y < current_selected_perf_list_index; y++ )
		{
			if ( current_selected_perf_list[y] != null && current_selected_perf_list[y].GroupNumber == curr_perf_group.perf_group_no )
			{
				curr_perf_group_perf_count ++;
			}
		}
		
		if ( curr_perf_group_perf_count == 0 && curr_perf_group.num_perf_min > 0 && curr_perf_group.SinglePerfNumber > 0 )
		{
			var selectedObj = { };
			selectedObj.PerfNumber = curr_perf_group.SinglePerfNumber;
			selectedObj.GroupNumber = curr_perf_group.perf_group_no;
			selectedObj.ZoneNumber = GetZoneIDForPerformance(selectedObj.PerfNumber);	
			current_selected_perf_list[current_selected_perf_list_index] = selectedObj;
			current_selected_perf_list_index++;
			curr_perf_group_perf_count ++;		
		}
				
		if ( curr_perf_group_perf_count < curr_perf_group.num_perf_min || curr_perf_group_perf_count > curr_perf_group.num_perf_max )
		{
			alert ( "Performance Group #" + curr_perf_group.perf_group_no + " requires a minimum of " + curr_perf_group.num_perf_min + " and a maximum of " + curr_perf_group.num_perf_max + ". You've selected " + curr_perf_group_perf_count + "." );
			return false;
		}
	}
	*/
	return true;
}

function checkPackage(iPkgNo)
{
	this.setEnable = function(bVal)
	{
		var btn = document.getElementById("btn_purchase" + iPkgNo);
		var pkg_production_min_max_ok = pkg_production_min_max.max > 0 ? (pkg_production_min_max.min <= pkg_production_min_max.current && pkg_production_min_max.max >= pkg_production_min_max.current) : true;
		btn.disabled = !( bVal && pkg_production_min_max_ok);
	}

	var result = false;
	if ( NFSPkgSummary != null )
	{
		result = validateNFSPackage();
		if ( ! result )
		{
			this.setEnable(false);
			return result;
		}
	}
	
	var pkg_no = iPkgNo;
	var tobj = quantities["pkg" + iPkgNo];
	if (tobj != null )
	{
		if (tobj.zone != 0 )
		{
			var howmany = 0;
			for(var x=0; x < tobj.qty.length; x++ )
				howmany += tobj.qty[x];
			
			this.setEnable(howmany > 0);
			result = (howmany > 0 );
		}
		else
			this.setEnable(false);
	}
	return result;
}

function AddNotesToLastLineItemCallback(oRslt) {
	location.href = '/tickets/cart.aspx';
}

function purchase_subs(iPkgNo)
{
	var target = quantities["pkg" + iPkgNo];
	var req = getreq();
	var qty = 0;
	getptlist = function()
	{
		var retval = "";
		for(var x=0; x < target.qty.length; x++)
		{
			qty += parseInt(target.qty[x]);
			if ( target.qty[x] > 0 )
				for(var y=0; y < target.qty[x]; y++)
				{
					if (retval.length > 0) retval += ",";
					retval += target.pt[x];
				}
		}
		return retval;
	}

	this.handler = function () {
		if (req.readyState == 4) {
			document.body.style.cursor = "default";
			if (req.status == 200) {
				rslt = JSON.parse(req.responseText);
				try {
					rslt = rslt.d;
				}
				catch (err) { }
				if (rslt.Success) {
					var lit_special_request_label = $g("lit_special_request_label");
					if (lit_special_request_label) {
						var special_request_field_label = $g("special_request_field_label");
						var special_request_field_label_value = null;
						if (special_request_field_label)
							special_request_field_label_value = $(special_request_field_label).text();
						if (!lit_special_request_label.value || lit_special_request_label.value.trim().length == 0) {
							location.href = "/tickets/cart.aspx";
							return;
						}
						document.body.style.cursor = "wait";
						AddNotesToLastLineItem("lit_special_request_label", special_request_field_label_value); // item may not exist -- should be no worrys.
						// setTimeout("location.href='/tickets.cart.aspx'", 5000);
					}
					else
						location.href = "/tickets/cart.aspx";
				}
				else
					alert(rslt.ErrorMessage);
			}
			else
				alert(req.responseText);
		}
	}

	this.errorMessage = "";
	
	this.validate = function(bUnseated)
	{
		var pt = getptlist();
		if (bUnseated)
			return { parent_pkg_no: 0, pricetype: pt, pkg_no: iPkgNo, quantity: (qty * multiplier), zone: target.zone };
		else
			return { pricetype: pt, pkg_no: iPkgNo, quantity: (qty * multiplier), zone: target.zone, requestedseats: "", leavesingleseats: false };
	}
	
	if ( req && NFSPkgSummary != null )
	{
		var flexObj = { pkg_no: iPkgNo, performances:[] };
		if ( validateNFSPackage() )
		{
			var ptlist = getptlist();

			var current_performance_index = 0;
			/*
			for ( var x = 0; x < current_selected_perf_list_index; x++ )
				if ( current_selected_perf_list[x] != null )
				{
					var zone_no = current_selected_perf_list[x].ZoneNumber;
					var flexitem = {perf_group_no: current_selected_perf_list[x].GroupNumber, perf_no: current_selected_perf_list[x].PerfNumber, pricetype: ptlist, quantity: qty, zone: zone_no  };
					flexObj.performances[current_performance_index++] = flexitem;
				}
			*/

			var the_zone_no =-1;
			for (var x = 0; x < PerformanceGroups.length; x++)
			{
				if (PerformanceGroups[x].selected)
				{
					for (var y = 0; y < PerformanceGroups[x].performances.length; y++)
					{
						if (PerformanceGroups[x].performances[y].selected)
						{
							if (PerformanceGroups[x].performances[y].selected_zone)
								the_zone_no = PerformanceGroups[x].performances[y].selected_zone;
							else
								the_zone_no = 0;
							if (the_zone_no == 0)
							{
								for (var z = 0; z < NFSPkgSummary.ZonesForNFSPackage.NFSPackageZoneItems.length; z++)
								{

									if (NFSPkgSummary.ZonesForNFSPackage.NFSPackageZoneItems[z].perf_no == PerformanceGroups[x].performances[y].perf_no && NFSPkgSummary.ZonesForNFSPackage.NFSPackageZoneItems[z].zone_desc == selectedFlexZoneDescription)
									{
										the_zone_no = NFSPkgSummary.ZonesForNFSPackage.NFSPackageZoneItems[z].zone_no;
										break;
									}
								}
							}
							var fitem = new flexitem(PerformanceGroups[x].perf_group_no, PerformanceGroups[x].performances[y].perf_no, ptlist, qty, the_zone_no); 
							// { perf_group_no: 0, perf_no: 0, pricetype: ptlist, quantity: qty, zone: 0 };
							// flexitem.perf_group_no = PerformanceGroups[x].perf_group_no;
							// flexitem.perf_no = PerformanceGroups[x].performances[y].perf_no;
							// flexitem.zone = PerformanceGroups[x].performances[y].selected_zone;
							flexObj.performances[current_performance_index++] = fitem;
						}
					}
				}
			}	
			document.body.style.cursor = "wait";
			req.open(HTTP_VERB, ( srvr + WEBSERVICE + "AddFlexPackage"), true);
			req.setRequestHeader( CONTENT_TYPE, APP_JSON );
			req.setRequestHeader( ORGANIZATION_CODE_KEY, ORGANIZATION_CODE );
			req.onreadystatechange = this.handler;
			// alert(JSON.serialize(flexObj));
			req.send(JSON.serialize(flexObj));	
		}
		else
			alert("Flex package validation error: " + this.errorMessage);
	}
	else 
	{
		var sendObj = this.validate(subs_unseated);
		if (req && sendObj)
		{
			var target_method = subs_unseated ? "AddPackageItem" : "AddPackageItemSeated"
			document.body.style.cursor = "wait";
			req.open(HTTP_VERB, ( srvr + WEBSERVICE + target_method), true);
			req.setRequestHeader( CONTENT_TYPE, APP_JSON );
			req.setRequestHeader( ORGANIZATION_CODE_KEY, ORGANIZATION_CODE );
			req.onreadystatechange = this.handler;
			req.send(JSON.serialize(sendObj));	
			// alert(JSON.serialize(sendObj));
		}
	}
}

// -------------------- renewals -----------------------

function doUpgrade()
{
	$('#p_update_explanation').hide();
	$('#tbl_upgrade_temporary_login').slideDown('slow');
}

function doUpdateLogin()
{
	var req = getreq();
	this.handler = function()
	{
		if (req.readyState == 4 )
		{
			document.body.style.cursor = "default";
			if (req.status == 200)
			{
				rslt = JSON.parse(req.responseText);
				try
				{
					rslt = rslt.d;
				}
				catch(err) { }
				if ( rslt.Success )
				{
					$('#' + pnl_update_login_id ).slideUp('slow', function(){ $('#' + pnl_multiple_renewals_id ).slideDown(); });
					retrieveExistingOrders();
				}
				else
					alert(rslt.ErrorMessage);
			}
		}
	}
	
	this.validate = function()
	{
		var retval = { };
		var errmsg = "";
		var s = document.getElementById("tbx_new_email").value.trim();
		if (s.length > 0 )
			retval.email = s;
		else
			errmsg += "- Email required\r\n";
			
		if (! email_validation(s))
			errmsg += "- Email format is not correct";
		
		var o = document.getElementById("tbx_new_login");
		if (o)
		{
			s = o.value.trim();
			if (s.length > 0 )
				retval.login = s;
			else
				errmsg += "- Login required\r\n";
		}
		else
		{
			retval.login = retval.email;
		}
			
		s = document.getElementById("tbx_new_password").value.trim();
		if (s.length > 0 )
			retval.password = s;
		else
			errmsg += "- Login required\r\n";
			
		s = document.getElementById("tbx_new_password_repeat").value.trim();
		if ( s != retval.password )
			errmsg += "- Passwords must match\r\n";

		if(errmsg.length > 0)
			alert("Please correct the following errors:\r\n" + errmsg);
		else
			return retval;				
		
	}
	
	var sendObj = this.validate();
	if (req && sendObj)
	{
		document.body.style.cursor = "default";
		req.open(HTTP_VERB, ( srvr + WEBSERVICE + "UpdateLogin"), true);
		req.setRequestHeader( CONTENT_TYPE, APP_JSON );
		req.setRequestHeader( ORGANIZATION_CODE_KEY, ORGANIZATION_CODE );
		req.onreadystatechange = this.handler;
		req.send(JSON.serialize(sendObj));	
	}
}

var login_mode = 1;

function setLoginMethod(iMode)
{
	login_mode = iMode;
	if (login_mode == 1 )
	{
		$('#lbl_existing').hide();
		$('#lbl_existing1').hide();
		$('#lbl_temp1').show();
		$('#lbl_temp').show();
		$('#tbx_existing_password').hide();
		$('#tbx_temp_password').show();
	}
	else
	{
		$('#lbl_existing').show();
		$('#lbl_existing1').show();
		$('#lbl_temp1').hide();
		$('#lbl_temp').hide();
		$('#tbx_existing_password').show();
		$('#tbx_temp_password').hide();
	}
}


function do_subs_login(btnSubmit)
{
	var button = btnSubmit;
	var req = getreq();
	this.handler = function () {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            loginResp = JSON.parse(req.responseText);
	            // alert(req.responseText);
	            if (loginResp.d.Success) {
	                if (loginResp.d.UpgradeLoginPolicyRequires) {
	                    $('#img_login_waiting').hide();
	                    document.body.style.cursor = "default";
	                    $('#' + pnl_login_id).slideUp('slow', function () { $('#' + pnl_update_login_id).slideDown('slow'); });
	                }
	                else {
	                    $('#' + pnl_login_id).slideUp('slow', function () { $('#' + pnl_multiple_renewals_id).show(); retrieveExistingOrders(); });
	                }
	            }
	            else {
	                $('#img_login_waiting').show();
	                button.style.display = "inline";
	                document.body.style.cursor = "default";
	                alert(loginResp.d.ErrorMessage);
	            }
	        }
	        else {
	            $('#img_login_waiting').hide();
	            button.style.display = "inline";
	            document.body.style.cursor = "default";
	            alert(req.responseText);
	        }
	    }
	}

	this.validate = function()
	{
		var retval = {};
		retval.promo_code = login_source_no > -1 ? login_source_no : 0;
		var ddl_source_no = document.getElementById("ddl_source_no");
		if (ddl_source_no)
			retval.promo_code = ddl_source_no.options[ddl_source_no.selectedIndex].value;

		var login = document.getElementById("tbx_login").value.trim();
		if (login.length > 0)
		{
			retval.login = login;
		}
		else
			$('#order_no_error').show();

		var password = "";
		if (login_mode == 1)
			password = document.getElementById("tbx_temp_password").value.trim();
		else
			password = document.getElementById("tbx_existing_password").value.trim();
		if (password.length > 0)
		{
			retval.passwd = password;
		}
		else
			$('#cust_id_no_error').show();
		if (login.length > 0 && password.length > 0)
			return retval;
	}
	
	var sendObj = this.validate();
	if (req && sendObj)
	{
		$('#img_login_waiting').show();
		sendObj.remember_me = false;
		req.open("POST", (srvr + WEBSERVICE + "Login"), true);
		req.setRequestHeader(CONTENT_TYPE, APP_JSON);
		req.setRequestHeader( ORGANIZATION_CODE_KEY, ORGANIZATION_CODE );
		req.onreadystatechange = this.handler;
		var qs = JSON.serialize(sendObj);
        //alert(qs);
		req.send(qs);
		document.body.style.cursor = "wait";
	}
}

function retrieveExistingOrders()
{
	
	var req = getreq();
	this.handler = function()
	{
		if (req.readyState == 4 )
		{
			document.body.style.cursor = "default";
			if (req.status == 200)
			{
				var rslt = JSON.parse(req.responseText);
				try
				{
					rslt = rslt.d;
				}
				catch(err) { }
				if (rslt.RedirectUrl != "" )
				{
					document.body.style.cursor="wait";
					$('#i_found_one').html("We've found one order and have added it to your cart. Redirecting to the cart...");
					location.href="/tickets/cart.aspx";
				}
				else if (rslt.Success)
				{
					document.body.style.cursor = "default";
					$('#renewal_orders_to_select').html(rslt.FormattedHTML);
				}
				else
				{
					$('#renewal_orders_to_select').html(rslt.ErrorMessage);
				}
			}
			else
				alert(req.responseText);
		}	
	}
	if (req)
	{
		req.open( HTTP_VERB, (  srvr + ROOT + WEBSERVICE + "GetExistingOrderInfo"), true);
		req.setRequestHeader(CONTENT_TYPE, APP_JSON);
		req.setRequestHeader( ORGANIZATION_CODE_KEY, ORGANIZATION_CODE );
		req.onreadystatechange = this.handler;
		req.send("");
		document.body.style.cursor = "wait";	
	}
}

var selected_order = 0;

function select_order_to_renew(iOrderNo)
{
	selected_order = iOrderNo;
	var btn_retrieve_selected_order = document.getElementById("btn_retrieve_selected_order");
	if (btn_retrieve_selected_order)
		btn_retrieve_selected_order.disabled = false;
}

function renew_selected_order()
{
	if (selected_order == 0 )
	{
		alert("Please select an order to renew");
		return;
	}
	var req = getreq();
	this.handler =function()
	{
		if (req.readyState == 4 )
		{
			if (req.status == 200)
			{
				var rslt = JSON.parse(req.responseText);
				try
				{
					rslt = rslt.d;
				}
				catch(err) { }
				if (rslt.Success)
					location.href="/tickets/cart.aspx";
				else
					alert(rslt.ErrorMessage);
			}
		}	
	}
	if(req)
	{
		req.open( HTTP_VERB, (  srvr + ROOT + WEBSERVICE + "LoadExistingOrder"), true);
		req.setRequestHeader(CONTENT_TYPE, APP_JSON);
		req.setRequestHeader( ORGANIZATION_CODE_KEY, ORGANIZATION_CODE );
		req.onreadystatechange = this.handler;
		req.send("{ order_no: " + selected_order + "}");
	}
}

//	current_pkg = 0;
//	count_selected_groups = 0

var current_PerformanceGroup_index = 0;
var count_selected_performances = 0;

function select_perf_group(oChkbx, pkg_no, pg_no, pkg_min, pkg_max)
{
	for (var x = 0; x < PerformanceGroups.length; x++)
	{
		if (PerformanceGroups[x].perf_group_no == pg_no)
		{
			current_PerformanceGroup_index = x;
			break;
		}
	}
	if (oChkbx.checked)
	{
		$("select.ie6select").hide();
		var pgdetaildiv = document.getElementById('pgdetail' + pg_no);
		if (pgdetaildiv)
		{
			var pgdetaildivcheckboxes = pgdetaildiv.getElementsByTagName("input");
			if (pgdetaildivcheckboxes && pgdetaildivcheckboxes.length > 0)
			{
				for (var x = 0; x < pgdetaildivcheckboxes.length; x++)
					pgdetaildivcheckboxes[x].checked = false;
			}
			for (var x = 0; x < PerformanceGroups[current_PerformanceGroup_index].performances.length; x++)
				$('#perf_details' + PerformanceGroups[current_PerformanceGroup_index].performances[x].perf_no).hide();
		}
		PerformanceGroups[current_PerformanceGroup_index].selected = true;
		if (count_selected_performances < nfspkg_num_perf_max )
		{
			count_selected_groups++;
			var pgdetail = document.getElementById("pgdetail" + pg_no);
			if (pgdetail)
			{
				var xy = getxy(oChkbx);
				pgdetail.style.left = "" + (xy.x + 40) + "px";
				pgdetail.style.top = "" + (xy.y + 12) + "px";
			}
			$('#pgdetail' + pg_no).show();
			// $('#splink' + pkg_no + "_" + pg_no).show(); 		// Show the "Select Performance" link
		}
		else
		{
			// too many selected: display error message and uncheck box
			oChkbx.checked = false;
			alert("Please deselect another package group, then try making this selection again.");
		}
	}
	else
	{
		$("select.ie6select").show();
		$('#splink' + pkg_no + '_' + pg_no).hide();
		PerformanceGroups[current_PerformanceGroup_index].selected = true;
		for (var x = 0; x < PerformanceGroups[current_PerformanceGroup_index].performances.length; x++)
		{
			if (PerformanceGroups[current_PerformanceGroup_index].performances[x].selected)
			{
				PerformanceGroups[current_PerformanceGroup_index].performances[x].selected = false;
				count_selected_performances--;
			}
		}
		var pg_perflist = document.getElementById("pg_perflist" + pg_no);
		if (pg_perflist)
		{
			var checkboxes = pg_perflist.getElementsByTagName("input");
			if (checkboxes && checkboxes.length > 0)
			{
				for (var x = 0; x < checkboxes.length; x++)
					if (checkboxes[x].checked)
						checkboxes[x].checked = false;
			}
		}
		buildSelectedPerfs();
	}
	/*
	current_pkg = pkg_no;
	if (oChkbx.checked)
	{
		if (count_selected_groups < pkg_max )
		{
			count_selected_groups++;
			$('#splink'+pkg_no+ "_" + pg_no).show();			// Show the "Select Performance" link
		}
		else
		{
			// too many selected: display error message and uncheck box
			oChkbx.checked = false;
			alert("Please deselect another package group, then try making this selection again.");
		}
	}
	else
	{
		// user unchecked current box
		count_selected_groups--;
		$('#splink'+pkg_no+ "_" + pg_no).hide();
		for (var x=0; x<current_selected_perf_list.length; x++ )
		{
			if ( current_selected_perf_list[x]!=null && current_selected_perf_list[x].GroupNumber == pg_no  )
			{
				current_selected_perf_list[x] = null;
				break;
			}
		}
		buildSelectedPerfs();
	}
	*/
	// alert( "pkg_no, pg_no, pkg_min, pkg_max = " + pkg_no + ", " + pg_no + ", " + pkg_min + ", " + pkg_max );
}

function select_perf_group_single_perf(sDescription, oChkbx, pkg_no, pg_no, pkg_min, pkg_max, perf_no )
{
	// current_pkg = pkg_no;
	for (var x = 0; x < PerformanceGroups.length; x++)
	{
		if (PerformanceGroups[x].perf_group_no == pg_no)
		{
			current_PerformanceGroup_index = x;
			break;
		}
	}
	
	
	if (oChkbx.checked)
	{
	    PerformanceGroups[current_PerformanceGroup_index].selected = true;
		
		RecordFlexItem(new flexitem(pg_no, perf_no, 0, 1, 0), PerformanceGroups[current_PerformanceGroup_index]);
				
		if (count_selected_performances < nfspkg_num_perf_max )
		{
			count_selected_performances++;
			// count_selected_groups++;
			/*
			var perfobj = { };
			perfobj.perf_no = perf_no;
			perfobj.desc = sDescription;
			currentPerfGroupPerfs[currentPerfGroupPerfsIndex++] = perfobj;
			append_selected_perf_group(pg_no, sDescription, perf_no );
			*/
			PerformanceGroups[current_PerformanceGroup_index].performances[0].selected = true;
			if (SelectZoneByPerformance)
			{
				pd = document.getElementById("perf_details" + perf_no);
				if (!pd)
				{
					var holder = document.createElement("div");
					holder.id = "perf_details" + perf_no + "_holder";
					holder.style.display = "none";
					holder.style.position = "absolute";
					holder.style.zIndex = 100;
					holder.style.padding = "12px";
					holder.style.border = "1px solid #000";
					holder.style.backgroundColor = "#fff";
					var instr = document.createElement("p");
					instr.appendChild(document.createTextNode(sDescription));
					instr.style.fontWeight = "bold";
					instr.style.textAlign = "left";
					holder.appendChild(instr);
					
					instr = document.createElement("p");
					instr.appendChild(document.createTextNode("Please select a seating zone for this performance."));
					holder.appendChild(instr);
										
					pd = document.createElement("div");
					pd.id = "perf_details" + perf_no;
					var xy = getxy(oChkbx);
					holder.style.left = "" + (xy.x + 40) + "px";
					holder.style.top = "" + (xy.y + 12) + "px";
					holder.style.width = "auto";
					holder.style.height = "auto";
					pd.style.height = "auto";
					pd.style.width = "200px";
					pd.style.textAlign = "left";
					holder.appendChild(pd);
					var canceldiv = document.createElement("div");
					canceldiv.style.width = "100%";
					canceldiv.style.textAlign = "right";
					var cancelspan = document.createElement("span");
					cancelspan.setAttribute("onclick", "$('#perf_details" + perf_no + "_holder').slideUp();cancel_select_perf_group(" + pg_no + ")");
					cancelspan.style.fontSize = "smaller";
					cancelspan.style.cursor = "pointer";
					cancelspan.appendChild(document.createTextNode("Cancel"));
					canceldiv.appendChild(cancelspan);
					holder.appendChild(canceldiv);
					document.body.appendChild(holder);
				}
				getSelectedPerfZones(perf_no, oChkbx);
				$("#perf_details" + perf_no + "_holder").slideDown();
			}
		}
		else
		{
			// too many selected: display error message and uncheck box
			oChkbx.checked = false;
			alert("Please deselect another package group, then try making this selection again.");
		}
	}
	else
	{
	    RecordFlexItem(new flexitem(pg_no, perf_no, 0, 0, 0), PerformanceGroups[current_PerformanceGroup_index]);
	    PerformanceGroups[current_PerformanceGroup_index].selected = false;
		PerformanceGroups[current_PerformanceGroup_index].performances[0].selected = false;
		// user unchecked current box
		// count_selected_groups--;
		count_selected_performances--;
		// $('#splink'+pkg_no+ "_" + pg_no).hide();
		/*
		for (var x=0; x<current_selected_perf_list.length; x++ )
		{
			if ( current_selected_perf_list[x]!=null && current_selected_perf_list[x].GroupNumber == pg_no  )
			{
				current_selected_perf_list[x] = null;
				break;
			}
		}
		*/
	}
	buildSelectedPerfs();
}

var last_pkg_no = 0;
var targetrect;
var splinkxy;

function select_performances_for_perf_group(pkg_no, pg_no, bGetZones, iSinglePerfNo, oCaller)
{
	for (var x = 0; x < PerformanceGroups.length; x++)
	{
		if (PerformanceGroups[x].perf_group_no == pg_no)
		{
			current_PerformanceGroup_index = x;
			break;
		}
	}
	last_pkg_no = pkg_no;
	splinkxy = getxy("splink" + pkg_no + "_" + pg_no );
	var divToShow = document.getElementById("pgdetail" + pg_no);
	if (divToShow)
	{
		divToShow.style.left = "" + (splinkxy.x + 40) + "px";
		divToShow.style.top = "" + ( splinkxy.y - 125 ) + "px";
		targetrect = { left: (splinkxy.x + 40), top:( splinkxy.y - 125 ), width: 300, height: 250 };
		var zoom_rect = document.getElementById("zoom_rect");
		if (zoom_rect)
			animate.open(divToShow,function() { divToShow.style.display = "block"; $("select.ie6select").hide();}, null, splinkxy,targetrect);
		else
			divToShow.style.display = "block";
		if (bGetZones) {
			getSelectedPerfZones(iSinglePerfNo, oCaller);
			$g('btn_select_pg' + pg_no).disabled = false;
			$('#cancel_select_perf_grp' + pg_no).hide();
		}
	}
	// alert("display perfrmances for group #" + pg_no );
}

function close_pg(pg_no)
{
	var divToShow = document.getElementById("pgdetail" + pg_no);
	if (divToShow)
	{
		$("select.ie6select").show(); 
		divToShow.style.display= "none";
		var zoom_rect = document.getElementById("zoom_rect");
		if (zoom_rect)
			animate.close(divToShow, null, null,splinkxy,targetrect);	
	}
}

function cancel_select_perf_group(pg_no)
{
	var checkbx = document.getElementById('chk_' + current_pkg + '_' + pg_no);
	if (checkbx)
		checkbx.checked = false;
	for (var x = 0; x < PerformanceGroups[current_PerformanceGroup_index].performances.length; x++)
		PerformanceGroups[current_PerformanceGroup_index].performances[x].selected = false;
	PerformanceGroups[current_PerformanceGroup_index].selected = false;
	buildSelectedPerfs();
	close_pg(pg_no);
	current_PerformanceGroup_index = -1;
}

function buildSelectedPerfs()
{
	var newHTML = "<table cellspacing='0' width='100%' id='perf_list_table'>";

	count_selected_performances = 0;
	
	for (var x = 0; x < PerformanceGroups.length; x++)
	{
		for (var y = 0; y < PerformanceGroups[x].performances.length; y++)
		{
			if (PerformanceGroups[x].performances[y].selected)
			{
				count_selected_performances++;
				newHTML += "<tr><td style='width: 100%'>";
				newHTML += PerformanceGroups[x].performances[y].title + " - " + PerformanceGroups[x].performances[y].perf_date;
				newHTML += "&nbsp;&ndash;&nbsp;<span class='action' onclick='remove_perf_item("
				newHTML += PerformanceGroups[x].perf_group_no + "," + PerformanceGroups[x].performances[y].perf_no;
				newHTML += ")'>Remove</span>";
				newHTML += "</td></tr>";
			}
		}
	}
	newHTML += "</table>";
	$('#selected_performances').html(newHTML);
}

function remove_perf_item(pg_no, perf_no)
{
	for (var x = 0; x < PerformanceGroups.length; x++)
	{
		var count_this_pkg = 0;
		
		if (PerformanceGroups[x].perf_group_no == pg_no)
		{
			current_PerformanceGroup_index = x;
			for (var y = 0; y < PerformanceGroups[x].performances.length; y++)
			{

				if (PerformanceGroups[x].performances[y].perf_no == perf_no)
				{
					PerformanceGroups[x].performances[y].selected = false;
					count_selected_performances--;
				}
				if (PerformanceGroups[x].performances[y].selected)
					count_this_pkg++;
			}
			if (count_this_pkg == 0)
			{
				var chkbox = document.getElementById("chk_" + current_pkg + "_" + pg_no);
				if (chkbox && chkbox.disabled == true)
				{
					$('#splink' + current_pkg + '_' + pg_no).show();
					var pgdetail = document.getElementById("pgdetail" + pg_no);
					if ( pgdetail )
					{
						var inputs = pgdetail.getElementsByTagName("input");
						if ( inputs )
						{
							for (var x = 0; x < inputs.length; x++)
							{
								if (inputs[x].type = "radio")
								{
									inputs[x].checked = false;
									break; // only 1 so break;
								}
							}
						}
					}
					select_performances_for_perf_group(current_pkg, pg_no)
				}
				else if (chkbox )
				{
					chkbox.checked = false;
				}
			}
			break;
		}
	}

	// alert( "remove_perf_item " + iIndexId );
	// var item = current_selected_perf_list[iIndexId];
	// $('#splink'+ current_pkg + '_' + item.GroupNumber).show();
	// current_selected_perf_list[iIndexId] = null;
	buildSelectedPerfs();	
}

var selectedPerfDescription = null;
var selectedPerfNo = 0

// returns a "PerfGroupSummary" object (defined in SubscriptionResp.cs) which contains 
//		int perf_group_no
//		int num_perf_max
//		int num_perf_min
//		int SinglePerfNumber	<-- If a package group contains only a single performance then this will contain the performance number
//		
function getGroupInfo(iGroupNo)
{
	for (var x=0; x < NFSPkgSummary.Groups.length; x++ )
	{
		if (NFSPkgSummary.Groups[x].perf_group_no == iGroupNo )
			return NFSPkgSummary.Groups[x];
	}
	return null;
}

function countPerformancesInPerfGroup(iGroupNo)
{
	var perf_count = 0;
	for (var y=0; y < currentPerfGroupPerfsIndex; y++ )
	{
		if ( currentPerfGroupPerfs[y].perf_no > 0 )
			perf_count++;
	}
	return perf_count;
}

function set_nfs_perf_zone(iPerfGroupNo, iPerfNo, iZoneNo )
{
	for (var x = 0; x < PerformanceGroups[current_PerformanceGroup_index].performances.length; x++)
	{
		if (PerformanceGroups[current_PerformanceGroup_index].performances[x].perf_no == iPerfNo)
		{
			PerformanceGroups[current_PerformanceGroup_index].performances[x].selected_zone = iZoneNo;
			break;
		}
	}
	if (PerformanceGroups[current_PerformanceGroup_index].perf_group_no != iPerfGroupNo)
		alert("Performance Group Mismatch");
	else
	{
		if (PerformanceGroups[current_PerformanceGroup_index].performances.length == 1)
			$('#perf_details' + iPerfNo + "_holder").slideUp();
	}
}

function buildPerformanceZoneSelection(oResult, iPerfNo)
{
	var newHTML = "<table cellspacing='0' width='100%' class='nfs_perf_zone_selection'>";

	for (var x = 0; x < oResult.ZonesList.length; x++)
	{
		newHTML += "<tr><td style='width: 100%'>";
		newHTML += "<input type='radio' name='zone_selection" + iPerfNo;
		newHTML += "' value='" + oResult.ZonesList[x].Number + "'";
		newHTML += " onclick='set_nfs_perf_zone(" + PerformanceGroups[current_PerformanceGroup_index].perf_group_no + "," + iPerfNo + "," + oResult.ZonesList[x].Number +")'";
		newHTML += ">&nbsp;";
		newHTML += oResult.ZonesList[x].Description;
		newHTML += "</td></tr>";
	}
	newHTML += "</table>";
	$('#perf_details' + iPerfNo).html(newHTML);
	$('#perf_details' + iPerfNo).slideDown();
}

var currentPerfGroupPerfs = [ ];
var currentPerfGroupPerfsIndex = 0;

function getSelectedPerfZones(iPerfNo, oCaller)
{
	this.handler = function()
	{
		if (req.readyState == 4)
		{
			document.body.style.cursor = "default";
			oCaller.style.cursor = "default";
			if (req.status == 200)
			{
				var rslt = JSON.parse(req.responseText);
				try
				{
					rslt = rslt.d;
				}
				catch (err) { }
				if (rslt.ZonesList && rslt.ZonesList.length > 0)
				{
					buildPerformanceZoneSelection(rslt, iPerfNo);
				}
			}
		}
	}

	var req = getreq();
	document.body.style.cursor = "wait";
	oCaller.style.cursor = "wait";
	req.open(HTTP_VERB, (srvr + WEBSERVICE + "GetZonesForNFSPackage"), true);
	req.setRequestHeader(CONTENT_TYPE, APP_JSON);
	req.setRequestHeader(ORGANIZATION_CODE_KEY, ORGANIZATION_CODE);
	req.onreadystatechange = this.handler;
	var pricetypeList = "";
	try {
		pricetypeList = $g('hidden_pricetype_list' + current_pkg).value;
	}
	catch(e) { }
	req.send("{ perf_no: " + iPerfNo + ", pricetypeList: '" + pricetypeList + "'}");
}

function RecordFlexItem(oFlexitem)
{
	var req = getreq();
	req.open(HTTP_VERB, (srvr + WEBSERVICE + "RecordFlexItem"), true);
	req.setRequestHeader(CONTENT_TYPE, APP_JSON);
	req.setRequestHeader(ORGANIZATION_CODE_KEY, ORGANIZATION_CODE);
	req.send(JSON.serialize({ currentPackageNo: current_pkg, item: oFlexitem }));
}


function selectPerformanceInGroup(iGroupNo, iPerfNo, iSelected, sDescription, oCaller )
{
	var group_selected_count = 0;
	for (var x = 0; x < PerformanceGroups[current_PerformanceGroup_index].performances.length; x++)
	{
		if (PerformanceGroups[current_PerformanceGroup_index].performances[x].selected)
			group_selected_count++;
	}
	PerformanceGroups[current_PerformanceGroup_index].selected = (group_selected_count > 0);

	if (group_selected_count == PerformanceGroups[current_PerformanceGroup_index].max
		&& oCaller.type == "checkbox" && iSelected)
	{
		oCaller.checked = false;
		alert("You have already reached the maximum of " + PerformanceGroups[current_PerformanceGroup_index].max + " items.");
		return;
	}

	for (var x = 0; x < PerformanceGroups[current_PerformanceGroup_index].performances.length; x++)
	{
		if (PerformanceGroups[current_PerformanceGroup_index].performances[x].perf_no == iPerfNo)
		{
			PerformanceGroups[current_PerformanceGroup_index].performances[x].selected = (iSelected ? true : false);
			group_selected_count += (iSelected ? 1 : -1);
		}
		else if (oCaller.type == "radio" || oCaller.type == "select-one" )
		{
			PerformanceGroups[current_PerformanceGroup_index].performances[x].selected = false;
		}
	}

	PerformanceGroups[current_PerformanceGroup_index].selected = (group_selected_count > 0);
	
	if (oCaller.type == "checkbox")
	{
		count_selected_performances += (iSelected ? 1 : -1);
		if (count_selected_performances > nfspkg_num_perf_max)
		{
			count_selected_performances--;
			if (oCaller.tagName == "INPUT" && oCaller.type == "checkbox")
				oCaller.checked = false;
			if (typeof oCaller == "select")
				alert("what to do?");
			alert("Sorry, you've already reached the maximum number of performances for this package: " + nfspkg_num_perf_max);
			return;
		}
	}

	evaluateCurrentPackageGroup(iGroupNo);
	buildSelectedPerfs();
	RecordFlexItem(new flexitem(iGroupNo, iPerfNo, 0, iSelected, 0), PerformanceGroups[current_PerformanceGroup_index]);


	if (SelectZoneByPerformance)
	{
		if (iSelected)
		{
			getSelectedPerfZones(iPerfNo, oCaller)
		}
		else
		{
			$('#perf_details' + iPerfNo).slideUp();
		}
	}	
	
	
	/*
	var perf_no = iPerfNo;
	try
	{
		perf_no = parseInt(iPerfNo);
	}
	catch(e) { }
	if (iSelected)
	{
		if ( perf_no > 0 )
		{
			var current_perf_group = getGroupInfo(iGroupNo);
			var current_perf_count_for_group = countPerformancesInPerfGroup(iGroupNo);
			if ( current_perf_count_for_group > 0 )
			{
				if ( current_perf_group.num_perf_max == 1 )
				{
					// this perf group only wants one performance at a time, so remove anything else
					currentPerfGroupPerfs = [ ];
					currentPerfGroupPerfsIndex = 0;
				}
			}
 			var item = { }
			item.perf_no = perf_no;
			item.desc = sDescription;
			currentPerfGroupPerfs[currentPerfGroupPerfsIndex++] = item;
		}
	}
	else
	{
		for (var x=0; x < currentPerfGroupPerfsIndex; x++ )
		{
			if ( currentPerfGroupPerfs[x].perf_no == perf_no)
			{
				currentPerfGroupPerfs[x].perf_no = 0;
				break;
			}
		}
	}
	evaluateCurrentPackageGroup( iGroupNo );
	*/
}

function evaluateCurrentPackageGroup( iGroupNo )
{
	var selectedPerfsThisPackage = 0;
	for (var x = 0; x < PerformanceGroups[current_PerformanceGroup_index].performances.length; x++)
		selectedPerfsThisPackage += PerformanceGroups[current_PerformanceGroup_index].performances[x].selected ? 1 : 0;
	var btn = document.getElementById("btn_select_pg" + iGroupNo);
	if (selectedPerfsThisPackage >= PerformanceGroups[current_PerformanceGroup_index].min && selectedPerfsThisPackage <= PerformanceGroups[current_PerformanceGroup_index].max)
		btn.disabled = false;
	else
		btn.disabled = true;
	/*
	if ( NFSPkgSummary != null )
	{
		for (var x=0; x < NFSPkgSummary.Groups.length; x++ )
		{
			if (NFSPkgSummary.Groups[x].perf_group_no == iGroupNo )
			{
				var perf_count = 0;
				for (var y=0; y < currentPerfGroupPerfsIndex; y++ )
				{
					if ( currentPerfGroupPerfs[y].perf_no > 0 )
						perf_count++;
				}
				var btn = document.getElementById("btn_select_pg" + iGroupNo );
				
				if ( btn && perf_count >= NFSPkgSummary.Groups[x].num_perf_min && perf_count <= NFSPkgSummary.Groups[x].num_perf_max )
					btn.disabled = false;
				else if (btn)
					btn.disabled = true;
				
				break;
			}
		}
	}
	*/
}

function submit_pg(iGroupNo)
{
	// have the animation vanishing point seem to go to the selected performances box;
	splinkxy = getxy("selected_performances" );
	var perf_list_table = document.getElementById("perf_list_table"); // dynamic table containing selected performances
	var rowcount = 0;
	if ( perf_list_table )
		rowcount = perf_list_table.rows.length;
	splinkxy.x += 120;
	splinkxy.y += 10 + (rowcount * 18);
	close_pg(iGroupNo);
	// alert("submit performance group");
	append_selected_perf_group(iGroupNo);

}

function append_selected_perf_group(iGroupNo, sAltDescription, iAltPerfNo )
{
	for(var x = 0; x < NFSPkgSummary.Groups.length; x++ )
	{
		if ( NFSPkgSummary.Groups[x].perf_group_no == iGroupNo )
		{
			for(var y = 0; y < currentPerfGroupPerfsIndex; y++ )
			{
				if (currentPerfGroupPerfs[y] && currentPerfGroupPerfs[y].perf_no > 0)
				{
					var selectedObj = { };
					/*
					var menu = document.getElementById("ddl_perf_group" + iGroupNo);
					if (menu)
					{
						if (menu.options)
							selectedObj.Title = menu.options[menu.selectedIndex].text;
						else
							selectedObj.Title = selectedPerfDescription;
						selectedObj.PerfNumber = selectedPerfNo;
					}
					else
					{
						selectedObj.Title = sAltDescription;
						selectedObj.PerfNumber = iAltPerfNo;
					}
					*/
					
					selectedObj.Title = currentPerfGroupPerfs[y].desc;
					selectedObj.PerfNumber = currentPerfGroupPerfs[y].perf_no;
					selectedObj.GroupNumber = iGroupNo;				
					selectedObj.ZoneNumber = GetZoneIDForPerformance(selectedObj.PerfNumber);
					current_selected_perf_list[current_selected_perf_list_index++] = selectedObj;
				}
			}

			$('#splink'+ current_pkg + '_' + iGroupNo).hide();	// hides the "Select Performance" text
			buildSelectedPerfs();								// displays the currently selected performances
			break;
		}
	}		
	currentPerfGroupPerfs = [ ];
	currentPerfGroupPerfsIndex = 0;
}

function selectFlexZone(sZoneDesc,zone_no)
{
	selectedFlexZoneDescription = sZoneDesc;
	selectedZone = zone_no; // may not be strictly correct zone number, but we'll record it and set it
	
	for (var x = 0; x < current_selected_perf_list.length; x++)
	{
		if(current_selected_perf_list[x])
		{
			current_selected_perf_list[x].ZoneNumber = GetZoneIDForPerformance(current_selected_perf_list[x].PerfNumber);
		}
	}
	if (quantities)
	{
		for (var x = 0; x < quantities.length; x++)
		{
			quantities[x].zone_no = selectedZone;
		}
	}
}

function GetZoneIDForPerformance(iPerfNo)
{
	if ( selectedFlexZoneDescription == "" )
		return 0;

	var pzi = null;
	try
	{
		pzi = NFSPkgSummary.ZonesForNFSPackage.NFSPackageZoneItems;
	}
	catch(er) { }
	if (pzi != null )
	{
		for (var x=0; x < pzi.length; x++ )
			if ( pzi[x].perf_no == iPerfNo && pzi[x].zone_desc == selectedFlexZoneDescription )
			{
				return pzi[x].zone_no;
			}
	}
	return 0;
}

function AddPerformanceToGroup(iPerfNo, iPerfGroupNo, sTitle, sPerfDate)
{
	for ( var x=0; x < PerformanceGroups.length; x++ )
	{
		if ( PerformanceGroups[x].perf_group_no == iPerfGroupNo )
		{
			PerformanceGroups[x].performances[PerformanceGroups[x].performances.length] = { perf_no: iPerfNo, selected: false, title: sTitle, perf_date: sPerfDate, selected_zone: 0 };
			break;
		}
	}
}

$(document).ready(function()
{
	if (SelectZoneByPerformance)
		$('#zone_table').hide();
});

