var validator;

function viewdocument(url,format)
{
	var winl = (screen.width + 600) / 2;
    var wint = (screen.height + 500) / 2;
	if(format=="tugs")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Tugs');
	}
	else if(format=="disclaimer")
	{
		$('#documentViewer').dialog( 'option', 'title', 'BMP Website Terms And Conditions');
	}
	else if(format=="portprocedure")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Underkeel clearances - Moreton Bay and Brisbane River');
	}
	else if(format=="airdraft")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Air Draft/Bridge Heights');
	}
	else if(format=="birthdepths")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Brisbane Berth Depths');
	}
	else if(format=="workingloads")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Safe Operating Loads');
	}
	else if(format=="cargohandling")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Safe Cargo Handling');
	}
	else if(format=="australianpilots")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Working With Australian Pilots');
	}
	else if(format=="careersatsea")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Careers at Sea');
	}
	else if(format=="portcareers")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Port Careers');
	}
	else if(format=="governance")
	{
		$('#documentViewer').dialog( 'option', 'title', 'BMP Management Policy');
	}
	else if(format=="passage")
	{
		$('#documentViewer').dialog( 'option', 'title', 'Pre Arrival Passage Information');
	}
		
	$('#documentViewer').dialog( 'option', 'buttons', {
	   	'Close': function() {
	        $(this).dialog('close');
	    }
	});
	    
	$('#documentViewer').dialog({
	   	width: winl,
	   	height: wint
	});
		    
	$('#documentViewer').html('Loading...').dialog('open');
	if(format=="tugs")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="'+url+'"></iframe>');
	}
	else if(format=="disclaimer")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="_data/'+url+'"></iframe>');
	}
	else if(format=="portprocedure")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="'+url+'"></iframe>');
	}
	else if(format=="airdraft")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="'+url+'"></iframe>');
	}
	else if(format=="birthdepths")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="_data/'+url+'"></iframe>');
	}
	else if(format=="workingloads")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="_data/'+url+'"></iframe>');
	}
	else if(format=="cargohandling")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="_data/'+url+'"></iframe>');
	}
	else if(format=="australianpilots")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="'+url+'"></iframe>');
	}
	else if(format=="careersatsea")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="'+url+'"></iframe>');
	}
	else if(format=="portcareers")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="'+url+'"></iframe>');
	}
	else if(format=="governance")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="_data/'+url+'"></iframe>');
	}
	else if(format=="passage")
	{
		$("#documentViewer").html('<iframe style="width:99%;height:95%;" src="_data/'+url+'"></iframe>');
	}
}

function editPDFLink()
{
	$('#editPDFLink').html('Loading...');
    $('#editPDFLink').dialog('open');
    $.ajax({
    	url: 'edit_PDFLinks.php',
        context: document.body,
        success: function(data) {
         	$('#editPDFLink').html(data);
         	var updateForm = $('#editPDFLinks');
            validator = updateForm.validate({
            	onkeyup: false
            });
         	$('#PDFName').change (function () {
         		newvalue = $("input[name='"+this.value+ "']").val();
    			$('#PDFLink').val(newvalue);
    		});
        }
    });
}

function changeDetails()
{
	$('#changeDetails').html('Loading...');
    $('#changeDetails').dialog('open');
    $.ajax({
    	url: 'changeDetails.php',
        context: document.body,
        success: function(data) {
         	$('#changeDetails').html(data);
         	var updateForm = $('#formDetails');
            validator = updateForm.validate({
            	onkeyup: false
            });
        }
    });
}

function displayLoading()
{
	clearTimeout(homebackgroundTimer);
	$('#loginarea').slideUp('slow');
	startBlockUI();
}

function startBlockUI()
{
	$('body').css('cursor', 'wait');
    $.blockUI({
    	message: '<img src="images/loading.gif" width="20" height="20"/><br/><span style="color:blue;"><i>Please wait...</i></span>',
        fadeIn: 50,
        baseZ: 550,
        overlayCSS: {
        	opacity: 0.7
        }
	});
}

function endBlockUI()
{
	$.unblockUI({fadeOut: 50});
    $('body').css('cursor', 'default');
}

$(document).ready(function() {
    $('#login').click(function(){
		$('#loginarea').slideDown('slow');
	});
	$('#loginarea').mouseleave(function(){
		if($('#username').val() == "" && $('#pasword').val() == "")
		{
			$('#loginarea').slideUp('slow');
		}
	});
	$('#documentViewer').dialog({
		autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false
    });
	if($('#errormsg'))
	{
		$('#errormsg').delay(3000).fadeOut();
	}
	$('#editPDFLink').dialog({
		autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width: 640,
        buttons: {
          	'Update': function() {
          		if (validator.form()) {
	            	var form = $('#editPDFLinks').get(0);
	                var formvalues = xajax.getFormValues(form);
	                xajax_editPDFLink(formvalues);
	            }
          	},
            'Close': function() {
            	$(this).dialog('close');
            	window.location.reload();
            }
     	} 
    });
    $('#changeDetails').dialog({
		autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width: 400,
        height:	220,
        title: 'Administrator Details',
        buttons: {
          	'Update': function() {
          		if (validator.form()) {
	            	var form = $('#formDetails').get(0);
	                var formvalues = xajax.getFormValues(form);
	                xajax_changeDetails(formvalues);
	                $(this).dialog('close');
	            }
          	},
            'Close': function() {
            	$(this).dialog('close');
            }
     	} 
    });
});
