var globalDialog = null;
document.observe("dom:loaded", function() {
  Dialogs.load();
});

function selectExchangePerf(li_seq_no) {

	$('liSeqNo').value = li_seq_no;
	
	var selectedExchangePerf = getOriginalPerfNo();

	$('alternateDateSelect').update('Loading...');
	
	new Ajax.Updater('alternateDateSelect', '/main.taf', 
    { 	evalScripts: false, 
        method: 'get', 
        parameters: {p:'8,28,2', live:'live', perf_no:selectedExchangePerf}
	});
	
}

function getOriginalPerfNo(){
	var matchingRadio = Form.getInputs('exchangeForm','radio','tessitura.perf_no').find(function(radio) { return radio.checked; });
	if (matchingRadio==undefined){
		return "";
	} else {
		var selectedExchangePerf = matchingRadio.value;
		return selectedExchangePerf;
	}
}

function checkExchangeForm(){
	var rvar = true;

	if ($F('altPerfNo') === ""){
		alert('Please Select an Alternate Date');
		rvar = false;
	}
	
	if (getOriginalPerfNo() === ""){
		alert('Please Select a Performance to Exchange');
		rvar = false;
	}

	if (rvar){
		$('exchangeForm').submit();
	}
}

function submitTempestRequest() {
	globalDialog = new  Dialog({
		title:"Please Wait...",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;">We are adding "The Tempest..." tickets to your cart.</div>'
	});
	globalDialog.open();  	
	new Ajax.Request("main.taf?erube_fh=tessitura&tessitura.submit.AddFakePackageItem=1", 
	{
		evalScripts: true, 
		method: 'get',
		parameters: {'tessitura.numSeats':$('numSeats').value, 'tessitura.performance':$('TempestPerfNo').value, 'tessitura.pricetypes':$('TempestPricetypes').value},
		onSuccess: function(response) {
			if (response.responseText.match('OK')) 
				{window.location = '/main.taf?p=8,6'; return true;} 
			else if (response.responseText.match('FAIL')) {
				globalDialog.close();
				globalDialog = new  Dialog({
				title:"Try again",
				opacity:0.50,
				padding: 0,
				margin: 0,
				background:["#000000", "#ffffff"],
				close:{link:true,esc:true,overlay:false},
				content:'<div id="waitingDialog" style="padding: 15px 25px;"><p>We were unable to add "The Tempest..." tickets to your cart. Please try again using another date.</p><p><a href="#" onclick="globalDialog.close(); return false;">Close Window</a></p></div>'
				});
				globalDialog.open();  
				return false;
			}}
		}
	);
}

function submitRexRequest() {
	globalDialog = new  Dialog({
		title:"Please Wait...",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;">We are adding "Elizabeth Rex" tickets to your cart.</div>'
	});
	globalDialog.open();  	
	new Ajax.Request("main.taf?erube_fh=tessitura&tessitura.submit.AddFakePackageItem=1", 
	{
		evalScripts: true, 
		method: 'get',
		parameters: {'tessitura.numSeats':$('numSeats').value, 'tessitura.performance':$('RexPerfNo').value, 'tessitura.pricetypes':$('RexPricetypes').value},
		onSuccess: function(response) {window.location = '/main.taf?p=8,6';}
	}
	);
}

function submit1001Request() {
	var checkoutupdate = $('checkoutupdate');
	pkgno = checkoutupdate['tessitura.addonPkgNo'];
	
	if ($F(pkgno) === "") {
	globalDialog = new  Dialog({
		title:"Please Wait...",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;">You must specify a date before adding these tickets to your cart</div>'
	});
	globalDialog.open();  		
	return false;
	}
	
	numAddOnSeats = checkoutupdate['tessitura.numAddonSeats'];
	var numSeats = $F(numAddOnSeats);
	
	if (numSeats === "" || numSeats === 0) {
	globalDialog = new  Dialog({
		title:"Please Wait...",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;">Please choose a valid number of seats to add to your cart.</div>'
	});
	globalDialog.open();  		
	return false;
	}
	
	globalDialog = new  Dialog({
		title:"Please Wait...",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;">We are adding "One Thousand and One Nights" tickets to your cart.</div>'
	});
	globalDialog.open();  	
		
	new Ajax.Request("main.taf?erube_fh=tessitura&tessitura.submit.AddAddonPackage=1",
	{
		evalScripts: true,
		method: 'get',
		parameters: {'tessitura.NumberOfSeats0':numSeats, 'tessitura.doSeated':'true', 'tessitura.PackageID':$F(pkgno), 'tessitura.Zone':'0'},
		onSuccess: function(response) {window.location = '/main.taf?p=8,6';}
	}
	);
	return true;
}

function tryCheckout() {
	if ($('TempestPerfNo') && $('TempestPerfNo').value === "") {
		globalDialog = new  Dialog({
		title:"Select a Performance",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;"><p>Please select a performance date for "The Tempest...".</p><p><a href="#" onclick="globalDialog.close(); return false;">Close Window</a></p></div>'
		});
		globalDialog.open();  			
		return false;
	}	
	if ($('RexPerfNo') && $('RexPerfNo').value === "") {
		globalDialog = new  Dialog({	
		title:"Select a Performance",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;"><p>Please select a performance date for "Elizabeth Rex".</p><p><a href="#" onclick="globalDialog.close(); return false;">Close Window</a></p></div>'
		});
		globalDialog.open();  			
		return false;
	}
	document.checkoutupdate.tessitura_checkout.value=1; 
	document.checkoutupdate.submit();
	return true;
}

function showUnseatedMessage() {
		globalDialog = new  Dialog({
		title:"Seating to be determined",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;"><p>For this limited engagement, we will seat you as close as possible to your standard subscription seats.</p><p><a href="#" onclick="globalDialog.close(); return false;">Close Window</a></p></div>'
		});
		globalDialog.open();  			
		return false;

}

function fixCartSpacing() {
	var heightA = null;
	var heightB = null;
	for (x=1; x<= 5; x++) {
		if ($('packageEvent' + x)) {
			heightA = $('packageEvent' + x).getHeight();
			heightB = $('packageEventSeating' + x).getHeight();
			if (heightA > heightB) {
				$('packageEventSeating' + x).setStyle({height: heightA + 'px'});
			} else {
				$('packageEvent' + x).setStyle({height: heightB + 'px'});
			}
		}
	}
	
	if ($('seriesPadding')) {
		$('seriesPadding').setStyle({height: ($('seriesBox').getHeight()+12) + 'px'});
	}

	if ($('seriesPadding2')) {
		$('seriesBox').setStyle({height: ($('seriesPadding2').getHeight()) + 'px'});
	}
	
	if ($('seriesPadding3')) {
		$('seriesBox').setStyle({height: ($('seriesPadding3').getHeight()) + 'px'});
	}
	
	if ($('rex1')) {
		$('rexSeating1').setStyle({height: ($('rex1').getHeight()+12) + 'px'});
	}
	if ($('rex2')) {
		$('rexSeating2').setStyle({height: ($('rex2').getHeight()+12) + 'px'});
	}
	if ($('tempest1')) {
		$('tempestSeating1').setStyle({height: ($('tempest1').getHeight()+12) + 'px'});
	}
	if ($('tempest2')) {
		$('tempestSeating2').setStyle({height: ($('tempest2').getHeight()+12) + 'px'});
	}
	if ($('tempest3')) {
		$('tempestSeating3').setStyle({height: ($('tempest3').getHeight()+12) + 'px'});
	}
		

}

function changePackage(newPkg) {
	globalDialog = new  Dialog({
		title:"Please Wait...",
		opacity:0.50,
		padding: 0,
		margin: 0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="waitingDialog" style="padding: 15px 25px;">We are processing your new package choice.</div>'
	});
	globalDialog.open();
	new Ajax.Request("main.taf?erube_fh=tessitura&tessitura.submit.ResetAndInitialize=1", 
	{
		evalScripts: true, 
		method: 'get',
		parameters: {'tessitura.numPkgs':newPkg},
		onSuccess: function(response) {window.location = '/main.taf?p=8,6'; return true;}
	}
	);
	return false;
}

