$(function() {
	
	var name = $( "#name" ),
		phone = $( "#phone" );
	
	$("#dialog-emergencycontact").dialog({
		autoOpen: false, 
		modal: true, 
		buttons: {
			Send: function() {
				$.ajax({
      				type: "POST",
      				url: "scripts/process_emergencycontact.php",
      				data:$("#form_emergencycontact").serialize(),
					success: function(data) {
						if (data.result == 'success') {
							$('.ui-dialog-buttonset').hide();
							$('#dialog-emergencycontact').html("<div id='message_emergencyresponse'></div>");
							$('#message_emergencyresponse').html('<h5>Thank You!</h5>')
							.append('<p class="responsemsg">'+data.msg+'</p>')
							.hide()
							.fadeIn(1500);
						} else {
							$("#dialog-emergencycontact p").html(data.msg).addClass("form_notify").show();
						}
					},
					dataType: "json"
     			});
			}, 
			Cancel: function() {
				$("#dialog-emergencycontact p").html('Submit your information and we will return your call immediately.').removeClass("form_notify");
				name.val( "" );
				phone.val( "" );
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$("#dialog-emergencycontact p").html('Submit your information and we will return your call immediately.').removeClass("form_notify");
			name.val( "" );
			phone.val( "" );
		}
	});
	
	$("#btn_emergency_service").click(function() {
		$("#dialog-emergencycontact").dialog("open");
	});
	
	function checkLength( o, n, min, max ) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( "Length of " + n + " must be between " +
				min + " and " + max + "." );
			return false;
		} else {
			return true;
		}
	}
	
});
