/* ------ Delete a time from a Journey ------ */
var divToRemove;
function deleteRecurrence(journeyId,recurrenceId, recurrenceDiv) 
{
	divToRemove = recurrenceDiv;
	
	new Ajax.Request(
		"ajaxDeleteJourneyTime.do", 
		{
	 		onSuccess  : deleteTimeSuccessful,
	 		onFailure  : deleteTimeFailed,
			parameters : "type=deleteRecurrence&journeyId=" + journeyId + "&recurrenceId=" + recurrenceId
		}
	);			
}

function deleteExcludedDate(journeyId,dateId, dateDiv) 
{
	divToRemove = dateDiv;
	
	new Ajax.Request(
		"ajaxDeleteJourneyTime.do", 
		{
	 		onSuccess  : deleteTimeSuccessful,
	 		onFailure  : deleteTimeFailed,
			parameters : "type=deleteExcluded&journeyId=" + journeyId + "&excludedDateId=" + dateId
		}
	);
}

function deleteTimeSuccessful(xhrObject)
{
	if ( xhrObject.responseText.indexOf("success") < 0) {
		// Was really a failure on the server-side but HTTP request processed OK so this method was still called...
		deleteTimeFailed(xhrObject);
	} else  {
		var elem = $(divToRemove); 
		var vis = elem.style;  
		vis.display = 'none';
	}
}
	
function deleteTimeFailed(xhrObject)
{
	//  First try to display any message embedded in the response text
	var index = xhrObject.responseText.indexOf("failure:");
	if ( index >= 0) {
		alert(xhrObject.responseText.substring(index+8,xhrObject.responseText.length));
	} else {
		alert("An error occurred while processing your deletion. Please try again later.");
	}
}

/* ------ Delete a Journey ------ */
function deleteJourney(journeyId)
{
	var isConfirmed = confirm("Are you sure you want to delete this tour?");
	if (isConfirmed) {
		new Ajax.Request(
			"ajaxDeleteJourney.do", 
			{
		 		onSuccess  : deleteJourneySuccessful,
		 		onFailure  : deleteJourneyFailed,
				parameters : "journeyId=" + journeyId
			}
		);
	}
}

function deleteJourneySuccessful(xhrObject)
{
	if ( xhrObject.responseText.indexOf("success") < 0) {
		// Was really a failure on the server-side but HTTP request processed OK so this method was still called...
		deleteJourneyFailed();
	} else  {
		updatePager(0);  // Don't yet know how to get the actual offset from Pager
	}
}
	
function deleteJourneyFailed()
{
	//  First try to display any message embedded in the response text
	var index = xhrObject.responseText.indexOf("failure:");
	if ( index >= 0) {
		alert(xhrObject.responseText.substring(index+8,xhrObject.responseText.length));
	} else {
		alert("An error occurred while removing this tour from your profile. Please try again later.");
	}
}

function updateRadioPanels() 
{
	var selectedRadio;
	for (i=0;i<document.saveRecurrenceForm.recurrenceType.length;i++) {
		if (document.saveRecurrenceForm.recurrenceType[i].checked) {
			selectedRadio = document.saveRecurrenceForm.recurrenceType[i].value;
		}
	}
	if ( selectedRadio == "Weekly") {
		toggleLayerBlock('weeklyRecurDiv',true);
		toggleLayerBlock('monthlyRecurDiv',false);
		toggleLayerBlock('yearlyRecurDiv',false);
		toggleLayerBlock('dateRangeDiv',true);			
	} else if ( selectedRadio == "Monthly") {
		toggleLayerBlock('weeklyRecurDiv',false);
		toggleLayerBlock('monthlyRecurDiv',true);
		toggleLayerBlock('yearlyRecurDiv',false);
		toggleLayerBlock('dateRangeDiv',true);			
	} else if ( selectedRadio == "Yearly") {
		toggleLayerBlock('weeklyRecurDiv',false);
		toggleLayerBlock('monthlyRecurDiv',false);
		toggleLayerBlock('yearlyRecurDiv',true);
		toggleLayerBlock('dateRangeDiv',false);
	}
}

function deleteTimePanel(spanId) 
{
	$(spanId).innerHTML = "";
}

function updateRadioPanels() 
{
	var selectedRadio;
	for (i=0;i<document.saveRecurrenceForm.recurrenceType.length;i++) {
		if (document.saveRecurrenceForm.recurrenceType[i].checked) {
			selectedRadio = document.saveRecurrenceForm.recurrenceType[i].value;
		}
	}
	if ( selectedRadio == "Weekly") {
		toggleLayerBlock('weeklyRecurDiv',true);
		toggleLayerBlock('monthlyRecurDiv',false);
		toggleLayerBlock('yearlyRecurDiv',false);
		toggleLayerBlock('dateRangeDiv',true);			
	} else if ( selectedRadio == "Monthly") {
		toggleLayerBlock('weeklyRecurDiv',false);
		toggleLayerBlock('monthlyRecurDiv',true);
		toggleLayerBlock('yearlyRecurDiv',false);
		toggleLayerBlock('dateRangeDiv',true);			
	} else if ( selectedRadio == "Yearly") {
		toggleLayerBlock('weeklyRecurDiv',false);
		toggleLayerBlock('monthlyRecurDiv',false);
		toggleLayerBlock('yearlyRecurDiv',true);
		toggleLayerBlock('dateRangeDiv',false);
	}
}

//  Inserts a new time span after the span at the provided number
var timeCount = 1;
function addTimePanel() 
{
	var priorSpanId = "time_"+timeCount;
	timeCount+=1;
	var newSpanId   = "time_"+timeCount;

	//  Just add another time div instead of trying to reuse any old ones (with their innerHTML removed by deleteTimePanel())
	var newTimeHtml = '<span id="' + newSpanId + '">and&nbsp;'
					+ '<select name="times" style="width:90px" class="ajComboBox"><option value="12:00 AM">12:00 AM</option>'
					+ '<option value="12:30am">12:30am</option><option value="1:00am">1:00am</option><option value="1:30am">1:30am</option><option value="2:00am">2:00am</option>'
					+ '<option value="2:30am">2:30am</option><option value="3:00am">3:00am</option><option value="3:30am">3:30am</option><option value="4:00am">4:00am</option><option value="4:30am">4:30am</option>'
					+ '<option value="5:00am">5:00am</option><option value="5:30am">5:30am</option><option value="6:00am">6:00am</option><option value="6:30am">6:30am</option><option value="7:00am">7:00am</option><option value="7:30am">7:30am</option>'
					+ '<option value="8:00am">8:00am</option><option value="8:30am">8:30am</option><option value="9:00am">9:00am</option><option value="9:30am">9:30am</option><option value="10:00am">10:00am</option>'
					+ '<option value="10:30am">10:30am</option><option value="11:00am">11:00am</option><option value="11:30am">11:30am</option><option value="12:00pm" selected="selected">12:00pm</option><option value="12:30pm">12:30pm</option>'
					+ '<option value="1:00pm">1:00pm</option><option value="1:30pm">1:30pm</option><option value="2:00pm">2:00pm</option><option value="2:30pm">2:30pm</option><option value="3:00pm">3:00pm</option>'
					+ '<option value="3:30pm">3:30pm</option><option value="4:00pm">4:00pm</option><option value="4:30pm">4:30pm</option><option value="5:00pm">5:00pm</option><option value="5:30pm">5:30pm</option><option value="6:00pm">6:00pm</option>'
					+ '<option value="6:30pm">6:30pm</option><option value="7:00pm">7:00pm</option><option value="7:30pm">7:30pm</option><option value="8:00pm">8:00pm</option><option value="8:30pm">8:30pm</option><option value="9:00pm">9:00pm</option>'
					+ '<option value="9:30pm">9:30pm</option><option value="10:00pm">10:00pm</option><option value="10:30pm">10:30pm</option><option value="11:00pm">11:00pm</option><option value="11:30pm">11:30pm</option></select>'
					+ '<a class="ajHighlitLink" href="javascript:deleteTimePanel(' + "'"+newSpanId + "'"+')">'
					+'&nbsp;<img src="./images/delete.gif" border="0" title="Remove This Time"/></a>&nbsp;</span>';
	new Insertion.After($(priorSpanId), newTimeHtml);
}

function deleteTimePanel(spanId) 
{
	$(spanId).innerHTML = "";
}

var requestedVisibility, requestedJourneyId;
function updateJourneyVisibility(journeyId, newVisibility) 
{
	requestedJourneyId = journeyId;
	requestedVisibility = newVisibility;
	var params = "type=updateJourneyVisibility&isVisible=" + requestedVisibility + "&journeyId=" + journeyId;
	new Ajax.Request(
		"ajaxUpdateJourneyVisibility.do", 
		{
	 		onSuccess  : updateJourneyVisibilitySuccessful,
	 		onFailure  : updateJourneyVisibilityFailed,
			parameters : params
		}
	);
}

function updateJourneyVisibilitySuccessful(xhrObject)
{
	if ( xhrObject.responseText.indexOf("success") < 0) {
		// Was really a failure on the server-side but HTTP request processed OK so this method was still called...
		updateJourneyVisibilityFailed(xhrObject);
	} else {
		toggleLayerInline( $("journeyVisibilityOn_" + requestedJourneyId), requestedVisibility );
		toggleLayerInline( $("journeyVisibilityOff_" + requestedJourneyId), !requestedVisibility );
	}
	selVisibilityCB = null;
}
	
function updateJourneyVisibilityFailed(xhrObject)
{
	//  First try to display any message embedded in the response text
	var index = xhrObject.responseText.indexOf("failure:");
	if ( index >= 0) {
		alert(xhrObject.responseText.substring(index+8,xhrObject.responseText.length));
	} else {
		 $(selVisibilityCB).checked = !$(selVisibilityCB).checked;
		alert("An error occurred during the update. Please report this bug to the web team.");
	}
	selVisibilityCB = null;
}
