// Button Rollover Code
function domLoaded(){
	Dialogs.load();	
}

function searchCalendar(year,month,keyword) {

	if (year == null){
		year  = $('year').getValue();
		month = $('month').getValue();
	} else {
		$('year').setValue(year);
		$('month').setValue(month);
	}
	
	keyword = $('keyword').getValue();

	$('calNav').hide();
	$('calNavCurrentMonthLabel').update('');	
	$('calendarContainer').hide();
	$('calendarLoadingMessage').show();
	$$('.calNavMonth').each(function(item){item.removeClassName('calNavMonthCurrent');});
	$('calNavMonth_' + year + '_' + month).addClassName('calNavMonthCurrent');
	$$('.keywordNav').each(function(item){item.removeClassName('thisTabOn');});
	$('bgKeywordNav_' + keyword.replace(' ', '')).addClassName('thisTabOn');
	new Ajax.Updater('calendarContainer', '/h/tessitura/ShowCalendar', 
    { 	evalScripts: false, 
        method: 'get', 
        parameters: {nonav:'1',year:year,month:month,keyword:keyword,view:'grid'},
		onSuccess: function(response){
			$('calendarLoadingMessage').hide();
			$('calendarContainer').show();
			// Create a date object on the 5th to avoid timezone related issues
			var d = new Date(year,month-1,5);
			$('calNavCurrentMonthLabel').update(formatDateForNavTab(d));
		}
	});	
}

function formatDateForNavTab(d){
	var monthname=new Array(7);
	monthname[0]="January";
	monthname[1]="February";
	monthname[2]="March";
	monthname[3]="April";
	monthname[4]="May";
	monthname[5]="June";
	monthname[6]="July";	
	monthname[7]="August";	
	monthname[8]="September";	
	monthname[9]="October";	
	monthname[10]="November";	
	monthname[11]="December";	

	var monthNum = d.getUTCMonth();
	var yearNum  = d.getUTCFullYear();
	return monthname[monthNum] + " " + yearNum;
}

function getThisMonth(year,month) {
	if (year == null){
		year  = $('year').getValue();
		month = $('month').getValue();
	} else {
		$('year').setValue(year);
		$('month').setValue(month);
	}
	
	keyword = $('keyword').getValue();
	
	$('calendarMonthContainer').hide();
	$('calendarLoadingMessage').show();
	$$('.calNavMonth').each(function(item){item.removeClassName('calNavMonthCurrent');});
	$('calNavMonth_' + year + '_' + month).addClassName('calNavMonthCurrent');	
	new Ajax.Updater('calendarMonthContainer', '/h/tessitura/ShowCalendar', 
	{ 	evalScripts: false, 
			method: 'get', 
			parameters: {nonav:'1',year:year,month:month,keyword:keyword,view:'list'},
	onSuccess: function(response){
		$('calendarLoadingMessage').hide();
		$('calendarMonthContainer').show();
		var d = new Date(year,month-1,5);
		$('calNavCurrentMonthLabel').update(formatDateForNavTab(d));
	}
});
}

var globalDialog = null;


function dialogOpenTest(){
	globalDialog = new  Dialog({
			title:"dialogOpenTest",
			opacity:.50,
			width:400,
			height:300,
			padding:0,
			margin:0,
			background:["#000000", "#ffffcd"],
			close:{link:true,esc:true,overlay:false},
			content:'Hello World'
	});
	globalDialog.open();
}

function showSYOSDialog(houseid,type,id){

	globalDialog = new  Dialog({
			title:"Select Your Own Seat",
			opacity:.50,
			width:847,
			height:553,
			padding:0,
			margin:0,
			background:["#000000", "#ffffcd"],
			close:{link:true,esc:true,overlay:false},
			content:'<div id="syosDialog"></div>',
			afterOpen:function(){
			new Ajax.Updater(
				'syosDialog', 
				'/h/tessitura/SYOSTool', { evalScripts: true,
											method: 'get',
											parameters: {
											houseid:houseid,
											type:type,
											id:id}
											});
			}
	});
	globalDialog.open();  
		
}

function showZonePricing(){
	// Get the selected zoneid
	var zoneid = $('zoneid').getValue();
	if (zoneid == "") {
		zoneid = "All";
	}

	// Hide all of the zonePricing nodes
	$$('.zonePricing').each(function(item){item.hide();});
	
	// Show all of the zonePricing nodes for the selected zone
	$$('.zonePricingForZone' + zoneid).each(function(item){
	item.show();
	new Effect.Highlight(item)
	});
	
	$('zonePricingMessage').show();
}

function showShippingMethodsDialog() {
	globalDialog = new  Dialog({
		title:"Add a new shipping address",
		opacity:.50,
		width:300,
		height:370,
		padding:0,
		margin:0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="shippingMethodsDialog" style="padding: 0 10px 10px 10px;"></div>',
		afterOpen:function(){
			new Ajax.Updater(
			'shippingMethodsDialog', 
			'/h/tessitura/ShippingMethodsDialog', { evalScripts: true, method: 'get'}
			);
		}
	});
	globalDialog.open();  
}

function showPromoCodeDialog(eventid) {
	globalDialog = new  Dialog({
		title:"Enter a promo code",
		opacity:.50,
		width:300,
		height:170,
		padding:0,
		margin:0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="promoCodeDialog"></div>',
		afterOpen:function(){
			new Ajax.Updater(
			'promoCodeDialog', 
			'/h/tessitura/ShowPromoCodeForm', { evalScripts: true, method: 'get', parameters: {eventid:eventid} }
			);
		}
	});
	globalDialog.open();  
}

// Toggle tab
function toggleTab(prefix, id) {
	$$('.' + prefix + '_layer').each(function(item){item.hide();});
	if ($(prefix + 'l_' + id)) {
		$(prefix + 'l_' + id).show();
	}

	$$('.' + prefix).each(function(item){item.removeClassName('thisTabOn');});
	$(prefix + 't_' + id).addClassName('thisTabOn');
}


// Toggle layer
function toggleLayer(prefix, id) {
	if ($(prefix + 'Layer_' + id).readAttribute('style').include('display: none')) {
		$(prefix + 'Layer_' + id).show();
		$(prefix + 'Toggler_' + id).writeAttribute('src', $(prefix + 'Toggler_' + id).readAttribute('src').sub('open', 'close'));
		$(prefix + 'Toggler_' + id).writeAttribute('alt', 'Close');
	} else {
		$(prefix + 'Layer_' + id).hide();
		$(prefix + 'Toggler_' + id).writeAttribute('src', $(prefix + 'Toggler_' + id).readAttribute('src').sub('close', 'open'));
		$(prefix + 'Toggler_' + id).writeAttribute('alt', 'Open');
	}
}

// Submit Finalize
function submitFinalizeForm() {
	if ( $F('wassubmitted') == '1' ) {
		alert("Please be patient while we process your transaction.");
	}
	$('wassubmitted').setValue('1');
	globalDialog = new  Dialog({
		title:"Transaction being processed:",
		opacity:.50,
		width:300,
		height:170,
		padding:0,
		margin:0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		target: {
			id:'submitDialog'
		}
	});
	globalDialog.open();  	
	$('finalize').submit();
}

function showConfirmMessage() {
		globalDialog = new  Dialog({
		title:"Please confirm your order:",
		opacity:0.50,
		padding:0,
		margin:0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		target: {
			id:'confirmMessage'
		}
	});
	globalDialog.open();  
}

// Submit Login
function submitLoginForm() {
	$('LoginForm').submit();
}

// Submit Change Password
function submitChgPWForm() {
	$('ChangePassword').submit();
}

function submitAddShipping() {
	new Ajax.Updater('shippingMethodsDialog','/h/tessitura/AddShippingAddressAJAX', 
	{
		method: 'get',
		parameters: $('addShippingAddressForm').serialize(true),
		evalScripts: true
	});	
	return false;
}

function CheckTicketQtyAndSubmitBuyTix() {
	
	var numValidPriceTypes = $$('.ticketQtySelect').any( function(n) {return n.getValue() > 0} );
	if (numValidPriceTypes) {
		$('selectNumberSeats').hide();
		$('buyTix').submit();
	} else {
		$('selectNumberSeats').show();
	}
}

function updateShippingMethods() {
	new Ajax.Updater('tAJAXshippingmethods','/h/tessitura/shippingMethods', 
	{
		method: 'get',
		evalScripts: false
	});
}
