$(document).ready(function() {
	$("#txtUser").focus(function() {
		this.value = '';
	});
	$("#txtPW").focus(function() {
		this.value = '';
	});

	/*var ajaxLoader = '<div class="jqmLoading">Content loading, please wait...</div>';
	var tempContent = '';
	var hbForm = null;
	var serializedFormVars = null;
	
	// load health brief form or "jqmLink" hyperlinks in lightbox
	$('#jqmWindow1').jqm({
		ajax: '@title'
		, target: $('#jqmWindow1 .jqmWindowContent')
		, trigger: 'a.jqmLink'
		, ajaxText: ajaxLoader
		, modal: true
		, onLoad: function() {
			hbForm = $('#frmItinerary').get(0);			
		}
		, onHide: function(hash) {
			if ($("#jqmWindow1").data("newBrief")) {
				$("#jqmWindow1").removeData("newBrief");
				hash.w.fadeOut('1',function(){ hash.o.remove(); location.reload(); }); 
			} else {
				hash.w.fadeOut('1',function(){ hash.o.remove(); }); 
			}
		}
	});

	// when New Health Brief link is clicked, do the following:	
	$("#itineraryLink").click(function() {
		// hide Back button in dialog
		$("#jqmWindow1 .jqmBack").css("visibility", "hidden");
		
		// show Help button in dialog and upon click do the following
		$("#jqmWindow1 .jqmHelp").css("visibility", "visible").click(function() {
			// store the current form state in a temporary variable
			tempContent = $("#jqmWindow1 .jqmWindowContent").html();
			serializedFormVars = $(hbForm).serialize();
			
			// show loading animation
			if ($("#jqmWindow1 .jqmLoading")) {
				$("#jqmWindow1 .jqmWindowContent").html(ajaxLoader);
			}
			
			// load help content
			$.get(this.href, function(data) {
				$("#jqmWindow1 .jqmHelp").css("visibility", "hidden");
				$("#jqmWindow1 .jqmBack").css("visibility", "visible");
				$("#jqmWindow1 .jqmLoading").remove();
				$("#jqmWindow1 .jqmWindowContent").html(data);		
			});
			return false;
		});
	});
	
	// when Back button in dialog is clicked, do the following:	
	$(".jqmBack").click(function() {
		// load the stored form from temporary variable
		$("#jqmWindow1 .jqmWindowContent").html(tempContent);

		// hide Back button
		$(this).css("visibility", "hidden");
		
		// show Help button again
		$("#jqmWindow1 .jqmHelp").css("visibility", "visible");	
		
		// re-enable the datepicker
		$(".dateInput").removeClass("hasDatepicker").datepicker({ dateFormat: 'dd/mm/yy' });
		
		if (hbForm != null && serializedFormVars != null) {
			restoreFormValues(hbForm, serializedFormVars);
		}
	});	*/
});

// function to submit New Health Brief form with AJAX
function submitHBForm(hbForm) {
	var serializedFormVars = $(hbForm).serialize();
	
	// remove error message if any, store the content temporarily and then replace the whole thing with AJAX loader
	$("#newBrief").attr("disabled", "true");
	$("#serverMsgError").hide();
	$("#serverMsgError").html("");
	$(".itineraryOverlay").css("visibility", "visible");
	$(".itineraryLoading").css("visibility", "visible");
	
	// submit form with AJAX
	var postUrl = $(hbForm).attr("action");
	$.ajax({
		type: "POST"
		, url: postUrl
		, data:  serializedFormVars + '&newBrief=1'
		, dataType: "json"
		, success: function(jsonData) {
			if (jsonData.success === 0) {
				// re-enable the save button first, in case something else breaks
				$("#newBrief").removeAttr("disabled");

				$(".itineraryOverlay").css("visibility", "hidden");
				$(".itineraryLoading").css("visibility", "hidden");
				
				// add error messages to error box and display it
				var errorMsg = '<p>There were errors while processing the form:</p><ul>';
				if (jsonData.travellersDetailsErrors) {
					errorMsg += '<li>' + jsonData.travellersDetailsErrors.join('</li><li>') + '</li>';
				}
				if (jsonData.destinationDetailsErrors) {
					errorMsg += '<li>' + jsonData.destinationDetailsErrors.join('</li><li>') + '</li>';
				}
				if (jsonData.insufficientCredits) {
					errorMsg += '<li>Insufficient credits. Please contact MASTA accounts.</li>';
				}				
				errorMsg += '</ul>';
				$("#serverMsgError").append(errorMsg).show();

				// re-enable the delete row link(s)
				$("a.tdDeleteRowLink").click(function() {
					removeDestinationRow($(this).attr("rel"));
				});
				
				// re-enable the datepicker
				$(".dateInput").removeClass("hasDatepicker").datepicker({ dateFormat: 'dd/mm/yy' });
				
				restoreFormValues(hbForm, serializedFormVars);
				
				scroll(0,0);
			} else {
				location.href = 'external.php?stampid=' + jsonData.hbId;
				opener.location.href = 'history.php';
			}
		}
		, error: function(jqXHR, textStatus, errorThrown) {
			// re-enable the save button first, in case something else breaks
			$("#newBrief").removeAttr("disabled");

			$(".itineraryOverlay").css("visibility", "hidden");
			$(".itineraryLoading").css("visibility", "hidden");

			var errorMsg = '<p>There was a fatal error when submitting this health brief. Please try again.</p>';
			errorMsg += '<p>Contact MASTA if the problem continues and provide the following error message:</p>';
			errorMsg += '<ul><li>' + errorThrown + '</li></ul>';
			$("#serverMsgError").append(errorMsg).show();

			// re-enable the delete row link(s)
			$("a.tdDeleteRowLink").click(function() {
				removeDestinationRow($(this).attr("rel"));
			});
			
			// re-enable the datepicker
			$(".dateInput").removeClass("hasDatepicker").datepicker({ dateFormat: 'dd/mm/yy' });
			
			restoreFormValues(hbForm, serializedFormVars);
			
			scroll(0,0);
		}
	});
}

function restoreFormValues(hbForm, serializedFormVars) {
	var formVars = [];
	
	$.each(serializedFormVars.split('&'), function(i, val) {
		var fVar = val.split('=');
		var fieldName = fVar[0];
		var fieldValue = Url.decode(fVar[1]).replace(/\+/g, ' ');
		$('#' + hbForm.id + ' .[name=' + fieldName +']').val(fieldValue);
	});
}

