// included by templates with /modules/tessitura/tessitura.js
// Button Rollover Code
function domLoaded(){
	Dialogs.load();	
}

function searchCalendar(year,month,season,newLocation) {

	if (year == null){
		year  = $('year').getValue();
		month = $('month').getValue();
	} else {
		$('year').setValue(year);
		$('month').setValue(month);
	}
	
	$('season').setValue(season);
	$('location').setValue(newLocation);
	$('calendarContainer').hide();
	$('calendarLoadingMessage').show();
	
	$$('.calNavMonth').each(function(item){item.removeClassName('tCalTabNavOn');});
	$('calNavMonth_' + year + '_' + month).addClassName('tCalTabNavOn');
	
	// $$('.calNavSeason').each(function(item){item.removeClassName('tCalSeasonTabNavOn');});
	// $('calNavSeason' + season).addClassName('tCalSeasonTabNavOn');	
	
	$$('.calNavLocation').each(function(item){item.removeClassName('tCalLocationTabNavOn');});
	$('calNavLocation' + newLocation).addClassName('tCalLocationTabNavOn');	
	
	
	new Ajax.Updater('calendarContainer', '/h/tessitura/ShowCalendar', 
    { 	evalScripts: false, 
        method: 'get', 
        parameters: {nonav:'1',year:year,month:month,season:season,location:newLocation},
		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]="Februrary";
	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:0.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:0.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)
	});
	
	$('zonePricingInitial').hide();
	$('zonePricingMessage').show();
}

function showShippingMethodsDialog() {
	globalDialog = new  Dialog({
		title:"Add a new shipping address",
		opacity:0.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: 15px 25px;"></div>',
		afterOpen:function(){
			new Ajax.Updater(
			'shippingMethodsDialog', 
			'/h/tessitura/ShippingMethodsDialog', { evalScripts: true, method: 'get'}
			);
		}
	});
	globalDialog.open();  
}

function showSecondSubscriberDialog() {
	globalDialog = new Dialog({
		title:"Add subscriber household information",
		opacity:0.50,
		width:610,
		height:585,
		padding:0,
		margin:0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="secondSubscriberDialog" style="padding: 15px 25px;">Loading...</div>',
		afterOpen:function(){
			new Ajax.Updater(
			'secondSubscriberDialog', 
			'/h/tessitura/SecondSubscriberDialog', { evalScripts: true, method: 'get'}
			);
		}
	});
	globalDialog.open();  
}

function submitLoginDialog(sourceDivID) {
	new Ajax.Updater(sourceDivID, '/h/tessitura/LoginDialog', 
		{
			parameters : $('login').serialize(true),
			method : 'post'
		});
}

function submitSecondSubscriberForm() {
	new Ajax.Updater('secondSubscriberDialog', '/h/tessitura/AddSecondSubscriber', 
		{
			parameters : $('SecondSubscriberForm').serialize(true),
			method : 'post',
			onSuccess: function(response) {
			},
			onFailure: function(response) {
				alert('There was an error trying to add the household info. Please try again.');
			},
			evalScripts: true
		});
}

function showPromoCodeDialog(eventid) {
	globalDialog = new  Dialog({
		title:"Enter a promo code",
		opacity:0.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();  
}

// toggleTab for Event Detail pages
function toggleTab(TabID) {
	$$('.optionOn').each(function(item){
		item.removeClassName('optionOn');
		item.addClassName('option');
		});

	$$('.tabContent').each(function(item){
		item.removeClassName('displayBlock');
		item.addClassName('displayNone');
		});

	//APPLY THE ACTIVE STYLE TO THE SELECTED TAB
	$('tab' + TabID).removeClassName('option');
	$('tab' + TabID).addClassName('optionOn');
	$('eventDetail' + TabID).removeClassName('displayNone');
	$('eventDetail' + TabID).addClassName('displayBlock');
}


// 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');
	}
}

function startCheckout() {
	var problem = false;
	if ($('updateCosubscribers')) {
		$$('.cosub_dropdown').each(function(ele) {
			dropdownValue = ele.childElements().find(function(option){return !!option.selected;});
			if (dropdownValue.empty()) {
				globalDialog = new  Dialog({
				title:"Please choose an action",
				opacity:0.50,
				width:400,
				height:50,
				padding:25,
				margin:0,
				background:["#000000", "#ffffff"],
				close:{link:true,esc:true,overlay:false},
				content:'<div id="additionalSubscribersDialog">Please confirm whether or not listed co-subscribers will remain on this package.</div>'
				});
				globalDialog.open();			
				problem = true;
				return;
			}}
		);
		
		new Ajax.Request('/h/tessitura/updateCosubscribers',
		{
			method: 'get',
			parameters: $('updateCosubscribers').serialize(true),
			evalScripts: true
		});
	}

	if (! problem) {
		location.href = '/h/tessitura/StartCheckout';
	}
}

// Submit Finalize
function submitPaymentInfo() {
	if ( $F('wassubmitted') == '1' ) {
		alert("Please be patient while we process your transaction.");
	}
	$('wassubmitted').setValue('1');
	$('PaymentInformation').submit();
}

// Submit Login
function submitLoginForm() {
	$('login').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() {
	if ($('tAJAXshippingmethods')) {
		new Ajax.Updater('tAJAXshippingmethods','/h/tessitura/shippingMethods', 
		{
			method: 'get',
			evalScripts: false
		});
	}
	
	if ($('tAJAXmerchshippingmethods')) {
		new Ajax.Updater('tAJAXmerchshippingmethods','/h/tessitura/merchShippingMethods', 
		{
			method: 'get',
			evalScripts: false
		});	
	}
}

function updateAddedSecondSubscribers() {
	new Ajax.Updater('tAJAXaddedsecondsubscribers','/h/tessitura/ShowAddedSubscribers', 
	{
		method: 'get',
		evalScripts: false
	});
}

function textCounter( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Comments value can only be 255 characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

function hide_selector(url)
{
 document.getElementById('tEventDetailChooseSeats').style.display = 'none';
 document.getElementById('tEventDetailChooseSeatsSearching').style.display = 'block';
 if (url != '')
 {
  location.href = url;
 }
}
function show_seatchart()
{
 document.getElementById('tEventDetailChooseSeatsSeatMapWrapper').style.display = '';
 document.getElementById('tEventDetailChooseSeatsSeatMapWrapperBackground').style.display = '';
 document.getElementById('tEventDetailChooseSeatsForm').style.display = 'none';
}
function hide_seatchart()
{
 document.getElementById('tEventDetailChooseSeatsSeatMapWrapper').style.display = 'none';
 document.getElementById('tEventDetailChooseSeatsSeatMapWrapperBackground').style.display = 'none';
 document.getElementById('tEventDetailChooseSeatsForm').style.display = '';
}

function toggleBestAvailable() {
  document.getElementById('chooseSeatingOptions').className = 'displayNone';
  document.getElementById('chooseBestAvailable').className = 'displayBlock';
}

function toggleSubscriptionInfo(tagId) {
  if (tagId == 'subscriptionPerformancePkg') {
    $('tabseatingOptions').className = '';
    $('tabsubscriptionPerformancePkg').className = 'on';
    $('seatingOptions').hide();
    $('subscriptionPerformancePkg').show();
  } else {
    $('tabsubscriptionPerformancePkg').className = '';
    $('tabseatingOptions').className = 'on';
    $('subscriptionPerformancePkg').hide();
    $('seatingOptions').show();
  }
}

function loadSeasonOverview(season, category, location) {
	$('seasonListingContent').hide();
	$('seasonListingLoading').show();
	new Ajax.Updater(
		'seasonListingDiv', 
		'/h/tessitura/seasonOverviewList', { evalScripts: true, method: 'get', parameters: {season: season, category: category, location: location} }
	);
}

function changeSeason(oldSeason, newSeason) {
	$(oldSeason + '_btn').src = '/img/btn_' + oldSeason + '.gif'
	$(newSeason + '_btn').src = '/img/btn_' + newSeason + '_on.gif'
	$('seasonCategories_' + oldSeason).hide();
	$('seasonCategories_' + newSeason).show();
	$('season').setValue(newSeason);
	$$('.on').each(function(item){item.removeClassName('on');});
	$(newSeason + '_Main company_link').addClassName('on');
	$('category').setValue('Main company');
	$('location').setValue('');

}

function changeEventDetailPage(newPage) {
	if (newPage != '') {
		if (newPage.indexOf('perf') > -1) {
			location.href = '/whats_on/event_detail?perfid=' + newPage.substring(4);
		} else {
			location.href = '/whats_on/event_detail?prodid=' + newPage.substring(4);
		}
	}
}

function changeCategory(newCategory) {
	$$('.on').each(function(item){item.removeClassName('on');});
	var season = $('season').value;
	$(season + "_" + newCategory + "_link").addClassName('on');
	$('category').setValue(newCategory);
	$('location').setValue('');
	loadSeasonOverview(season, newCategory, $('location').value);
}

function changeLocation(newLocation) {
	$('location').setValue(newLocation);
	loadSeasonOverview($('season').value, $('category').value, newLocation);
}

function modifySubsLoginLink(perfno) {
	if ($('subsLoginLink')) {
		$('subsLoginLink').href = '/tessitura/account/login?frompath=' + escape('/whats_on/tickets?perfid=' + perfno);
	}
}

function showGiftCertDeliveryOption(giftCertId) {
	globalDialog = new  Dialog({
		title:"Select a gift certificate delivery method",
		opacity:0.50,
		width:400,
		height:150,
		padding:25,
		margin:0,
		background:["#000000", "#ffffff"],
		close:{link:true,esc:true,overlay:false},
		content:'<div id="giftCertDeliveryOptionsDialog">Loading...</div>',
		afterOpen:function(){
			new Ajax.Updater(
			'giftCertDeliveryOptionsDialog', 
			'/h/tessitura/ShowGiftCertDeliveryOptions', { evalScripts: true, method: 'get', parameters: {giftCertId:giftCertId} }
			);
		}
	});
	globalDialog.open();
}

function PostGiftCert(giftCertId) {
			new Ajax.Updater(
			'giftCertDeliveryOptionsDialog', 
			'/h/tessitura/PostGiftCertificate', { evalScripts: true, method: 'get', parameters: {giftCertId:giftCertId} }
			);
}

function PrintGiftCert(giftCertId) {
window.open('/h/tessitura/PrintGiftCertificate?gc_no=' + giftCertId, '_blank');
globalDialog.close();
}

function showGiftCertificateForm() {
	$('tCheckOutPaymentInfo').hide();
	$('tApplyGiftCertificate').show();
}

function showCreditCardForm() {
	$('tCheckOutPaymentInfo').show();
	$('tApplyGiftCertificate').hide();
}

function togglePerfGroup(perf_group_no) {
  if ($('tShowNFSPackageFormPerformanceGroup' + perf_group_no).style.display == 'none') {
    $('tShowNFSPackageFormPerformanceGroup' + perf_group_no).show();
    $('tShowNFSPackageFormPerformanceGroupHeader' + perf_group_no).className="tShowNFSPackageFormPerformanceGroupHeaderOn";
    $('tShowNFSPackageFormPerformanceGroupHeaderDescription' + perf_group_no).className="tShowNFSPackageFormPerformanceGroupHeaderOnDescription";
    $('tShowNFSPackageFormPerformanceGroupHeaderNumPerfsAdjusted' + perf_group_no).className="tShowNFSPackageFormPerformanceGroupHeaderOnNumPerfsAdjusted";
  } else {
    $('tShowNFSPackageFormPerformanceGroup' + perf_group_no).hide();
	$('tShowNFSPackageFormPerformanceGroupHeader' + perf_group_no).className="tShowNFSPackageFormPerformanceGroupHeader";
    $('tShowNFSPackageFormPerformanceGroupHeaderDescription' + perf_group_no).className="tShowNFSPackageFormPerformanceGroupHeaderDescription";
    $('tShowNFSPackageFormPerformanceGroupHeaderNumPerfsAdjusted' + perf_group_no).className="tShowNFSPackageFormPerformanceGroupHeaderNumPerfsAdjusted";
  }
}

function showAllPerfGroups(){
	$$('.tShowNFSPackageFormPerformanceGroup').invoke('show');
	$$('.tShowNFSPackageFormPerformanceGroupHeader').invoke('addClassName', 'tShowNFSPackageFormPerformanceGroupHeaderOn').invoke('removeClassName', 'tShowNFSPackageFormPerformanceGroupHeader');
	$$('.tShowNFSPackageFormPerformanceGroupHeaderDescription').invoke('addClassName', 'tShowNFSPackageFormPerformanceGroupHeaderOnDescription').invoke('removeClassName', 'tShowNFSPackageFormPerformanceGroupHeaderDescription');
	$$('.tShowNFSPackageFormPerformanceGroupHeaderNumPerfsAdjusted').invoke('addClassName', 'tShowNFSPackageFormPerformanceGroupHeaderOnNumPerfsAdjusted').invoke('removeClassName', 'tShowNFSPackageFormPerformanceGroupHeaderNumPerfsAdjusted');
}

function hideAllPerfGroups(){
	$$('.tShowNFSPackageFormPerformanceGroup').invoke('hide');
	$$('.tShowNFSPackageFormPerformanceGroupHeaderOn').invoke('addClassName', 'tShowNFSPackageFormPerformanceGroupHeader').invoke('removeClassName', 'tShowNFSPackageFormPerformanceGroupHeaderOn');
	$$('.tShowNFSPackageFormPerformanceGroupHeaderOnDescription').invoke('addClassName', 'tShowNFSPackageFormPerformanceGroupHeaderDescription').invoke('removeClassName', 'tShowNFSPackageFormPerformanceGroupHeaderOnDescription');
	$$('.tShowNFSPackageFormPerformanceGroupHeaderOnNumPerfsAdjusted').invoke('addClassName', 'tShowNFSPackageFormPerformanceGroupHeaderNumPerfsAdjusted').invoke('removeClassName', 'tShowNFSPackageFormPerformanceGroupHeaderOnNumPerfsAdjusted');
}

function doReserveSeats(perf_no) {
  new Effect.SlideUp('tShowNFSPackageFormPerformanceAdd' + perf_no);
  new Effect.SlideDown('tShowNFSPackageFormPerformanceAddLoading' + perf_no, { duration: 0.5, queue: { position: 'end', scope: 'Loading1' } });
  document.getElementById('chooseseats'+perf_no).submit();
  return false;
}
