/*
 * Miami ARTCC Notams System (MANS)
 */

function update_notamdisp_inner() {


         $('#notaminner').html("<div class='center'>Loading...<br /><img class='center' src='images/loading.gif' /></div>");

 $.get("/mans/index.php?op=view&target=notamdispinner", function(data){
	 $('#notaminner').html(data);
  });

}


function send_approve() {

vars = { 
	    op: "approve",
	target: "approve",
	    id: $('#ua_notam_sel').val()
	};

	if(vars.id < 0) { return false; }
	
	$.post('/mans/index.php',vars,function(data){
        	alert(data);
		return true;
	});

	// trigger an update of the notam display
	$('#notamdisp').trigger('update_notamdisp');

 return true;
}


function send_unapprove() {

vars = {   
            op: "approve",
        target: "unapprove",
            id: $('#a_notam_sel').val() 
        };

        if(vars.id < 0) { return false; }   


        $.post('/mans/index.php',vars,function(data){
                alert(data);
                return true;
        });

	// trigger an update of the notam display
	$('#notamdisp').trigger('update_notamdisp');

 return true;
}


function send_add() {

vars = {
// addNotam($auth, $notamid, $description, $facility, $details, $dateStart, $dateEnd)
		op: "add",
	    target: "notam",
	   notamid: $('#addnotam_notamid').val(),
       description: $('#addnotam_description').val(),
  	  facility:  $('#addnotam_facility').val(),
	   details: $('#addnotam_details').val(),
	 dateStart: Date.parse($('#addnotam_datestart').val())/1000,
	   dateEnd: Date.parse($('#addnotam_dateend').val())/1000
};

if(vars.dateEnd == Date.parse('0')) {
	vars.dateEnd = '0';
	}


$.post('/mans/index.php',vars,function(data){ 
	alert(data);
});


}

function validate_add() {

valid = true;
 // the sizzle selector engine inside jquery owns!
$('input,textarea', '#addnotam').each(function(index,element){
        id = '#' + element.id;
        if($(id).val() === '') {
                 $(id).addClass('ui-state-error');
                 $(id + '_error').fadeIn();
                 valid = false;
                } else {
                 $(id).removeClass('ui-state-error');
                 $(id + '_error').fadeOut();
                }
        });

if(!valid) {
        return false;
        }

// everything is ok!
return true;
}


function doDisplayNew() {

 
   $('#popup').dialog('destroy');
           $.get("/mans/index.php?op=view&target=new", function(data){
                $('#popup').html(data);
                $('#popup').dialog({
                        modal: true,
                        stack: false,
                        width: 800,
                       height: 'auto',
                    resizable: false,
                    draggable: false,
                        title: 'Notams awaiting moderation.',
                      buttons: {
                        "Close": function() {
                                   $(this).dialog("close");
                                }
                            },
                        close: function(){
                                  $(this).dialog('destroy');
                                }
                });

        });


}


function doDisplayDelete() {

   $('#popup').dialog('destroy');
           $.get("/mans/index.php?op=delete&target=dialog", function(data){
                $('#popup').html(data);
                $('#popup').dialog({
                        modal: true,
                        stack: false,
                        width: 800,
                       height: 'auto',
                    resizable: false,
                    draggable: false,
                        title: 'Delete Notam:',
                      buttons: {
                        "Close": function() {
                                   $(this).dialog("close");
                                }
                            },
                        close: function(){
                                  $(this).dialog('destroy');
                                }
                });

        });



}


function doDisplayModify() {

   $('#popup').dialog('destroy');
           $.get("/mans/index.php?op=mod&target=mod", function(data){
                $('#popup').html(data);
                $('#popup').dialog({
                        modal: true,
                        stack: false,
                        width: 800,
                       height: 'auto',
                    resizable: false,
                    draggable: false,
                        title: 'Modify Notam:',
                      buttons: {
                        "Close": function() {
                                   $(this).dialog("close");
                                }
                            },
                        close: function(){
                                  $(this).dialog('destroy');
                                }
                });

        });


}


function doDisplayApprove() {


   $('#popup').dialog('destroy');
           $.get("/mans/index.php?op=approve&target=form", function(data){
                $('#popup').html(data);
                $('#popup').dialog({
                        modal: true,
                        stack: false,
                        width: 800,
                       height: 'auto',
                    resizable: false,
                    draggable: false,
                        title: 'Notam Approval Control:',
                      buttons: {
                        "Close": function() {
                                   $(this).dialog("close");
                                }
                            },
                        close: function(){
                                  $(this).dialog('destroy');
                                }
                });

        });

}

function doDisplayAdd() {

   $('#popup').dialog('destroy');
	   $.get("/mans/index.php?op=add&target=form", function(data){
                $('#popup').html(data);
                $('#popup').dialog({
                        modal: true,
		 	stack: false,
                        width: 800,
                       height: 'auto',
                    resizable: false,
                    draggable: false,
                        title: 'Add Notam:',
                      buttons: {
                        "Save": function() {
                                   if(validate_add()) { send_add(); $(this).dialog('close'); }
                                }
                            },
                        close: function(){
                                  $(this).dialog('destroy');
                                }
                });

        });

}



function doDisplayBox(id) {

   $('#popup').dialog('destroy');

   $.get("/mans/index.php?op=view&target=detail&id=" + id, function(data){
   		$('#popup').html(data);
		$('#popup').dialog({
			modal: true,
			width: 800,
		       height: 'auto',
		    resizable: false,
		    draggable: false,
                	title: 'Details:',
		      buttons: { 
			"Ok": function() { 
				   $(this).dialog("close"); 
				} 
			    },
                	close: function(){
				  $(this).dialog('destroy');
				}
                });

 	});


}




$(function() { 


	// prevent caching of any AJAX call.
	$.ajaxSetup({
   		url: "/mans/",
   		global: true,
   		cache: false
 	});
	

	$('#notamdisp').bind('update_notamdisp',function(event) {
		update_notamdisp_inner();		
	});

	$('button, tr', '#notamdisp').hover(
		function(){ 
			$(this).addClass('ui-state-hover');
			$('td', this).addClass('ui-widget-header');
			$('td', this).addClass('noborder');
			
		}, 
		function(){
			
			$(this).removeClass('ui-state-hover');
                        $('td', this).removeClass('ui-widget-header');
			$('td', this).removeClass('noborder');
                       
 
		}
	);

});

