function direction_change(routeID){
	var url = "/admin/routes/schedule.php?routeId=" + routeID;
	var dir = document.forms.calform.routeDirection.value;
	if(dir != ""){
		document.getElementById("scheduleFrameID").src = url +"&routeDirection=" + dir;
	}
}

var routeID = "";
var routeDirection = "";
var direction, timeframe;
function deletePopUp(dir, timef){
	direction = dir;
	timeframe = timef;
	document.getElementById("popUpText").innerHTML = "You are about to <b>PERMANENTLY</b> remove the schedule for direction " + direction + " for the timeframe of " + timeframe;
	document.getElementById("popUp").style.display = "block";
}

function confirmDelete(){
	routeID = document.getElementById('specialForm').specialID.value;
	window.location = "/includes/modules/routes/controllers/schedule.php?action=delete&direction=" + direction + "&timeframe=" + timeframe + "&specialID=" + routeID; //routeDirection=" + routeDirection + "&
}

function cancelDelete(){
	document.getElementById("popUp").style.display = "none";
}

$(document).ready(function() {
	/* View or Add single alert */
	$('#route-alerts-table tr.alerts-table-item, #alertsTarget .add-alert').live('click', function() {
		var alertID = $(this).data('alertid');
		var that = this;
		
		$('#alertsAjaxContainer').slideUp().html('<div><img id="blue-loader" src="/includes/modules/base/images/blueLoader.gif" /></div>').slideDown();
		$.post('/includes/modules/routes/controllers/alertsEditInsert.php?alertID=' + alertID + '&formAction=view', function(html) { 
			$('#alertsAjaxContainer').hide().html(html).slideDown();
			MSL_routes = new MultiSelectList('MSL_routes');
			CKEDITOR.replace('routedescription', { height: '125px', width: '400px', toolbar: [['Format', '-', 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'PasteText'],['TextColor']]});			
		});
	});
	
	/* View all alerts button */
	$('#alertsTarget .view-alerts').live('click', function() {
		CKEDITOR.instances.routedescription.destroy();
		$('#alertsAjaxContainer').slideUp().html('<div><img id="blue-loader" src="/includes/modules/base/images/blueLoader.gif" /></div>').slideDown();
		$.post('/includes/modules/routes/controllers/alertsEditInsert.php?formAction=viewall', function(html) { 
			$('#alertsAjaxContainer').hide().html(html).slideDown();
		});		
	});
	
	/* Save alert button */
	$('#alertsItemForm .save-alert').live('click', function() {
		CKEDITOR.instances.routedescription.updateElement();
		var validated = true;
		/* Validate Inputs */
		$('#alertsItemForm .validation-messages').remove();		
		$('#alertsItemForm .validate').each(function(index) {
			if($(this).val().length < 1) {
				$(this).after('<p class="validation-messages warning">This field is required.</p>');
				validated = false;
			}
		});
		
		if(validated === false) return;
		
		$('#alertsItemForm .save-alert').parent().append('<img id="blue-loader" src="/includes/modules/base/images/blueLoader.gif" />');		
		$.post('/includes/modules/routes/controllers/alertsEditInsert.php', $('#alertsItemForm').serialize(), function(data) {
			$('#blue-loader').remove();
			$('#alertsItemForm .save-alert').parent().append(data);
			$('.alert-update-status-message').delay(3000).fadeOut('slow', function() { $(this).remove(); });
		});			
	});
	
	/* Delete alert button */
	$('#alertsItemForm .delete-alert').live('click', function() {
		$('#alertsItemForm input[name=formAction]').val('delete');
		$.post('/includes/modules/routes/controllers/alertsEditInsert.php', $('#alertsItemForm').serialize(), function(data) {
			$('#alertsTarget .view-alerts').trigger('click');
		});		
	});

	/* jQuery Live function for datepicker */
    $('input.dateSelect').live('click', function() {
        $(this).datepicker({showOn:'focus', dateFormat: 'yy-mm-dd'}).focus();
    });

});
