$(document).ready(function(){
	// HIDE DIALOG MESSAGE BOX
	$("#dialog_message").hide();
	$("#StartDate" ).datepicker();
			
	// FIELDS NEEDING VALIDATION
	var FirstName						= $("#FirstName"),
		LastName						= $("#LastName"),
		PhoneWork						= $("#PhoneWork"),
		Email							= $("#Email"),
		EventName 						= $("#EventName"),
		EventSummary 					= $("#EventSummary"),
		EventLocation					= $("#EventLocation"),
		EventMoreInfo					= $("#EventMoreInfo"),
		EventHost 						= $("#EventHost"),
		StartDate 						= $("#StartDate"),
		StartTime 						= $("#StartTime"),
		EndTime							= $("#EndTime"),
		allFields 						= $([]).add(FirstName).add(LastName).add(PhoneWork).add(Email).add(EventName).add(EventSummary).add(EventLocation).add(EventMoreInfo).add(EventHost).add(StartDate).add(StartTime).add(EndTime),
		tips 							= $(".validateTips");
	
	
	// Update tips Function
	function updateTips(t) {
		tips
			.text(t)
			.addClass('ui-state-highlight');
		setTimeout(function() {
			tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}
	
	// Check Length Function
	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;
		}
	}
	
	// Dialog Link Click
	$('#event_submit').click(function(){
		
		// OPEN DIALOG
		$('#dialog').dialog('open');
			return false;
			
	});
	
	// Dialog Show
	$("#dialog").dialog({
		autoOpen: false,
		title: 'Submit An Event',
		height: 650,
		width: 800,
		modal: true,
		buttons: {
			'Submit': function() {
				var bValid 									= true;
				var params 									= {};
				var message									= '';

				allFields.removeClass('ui-state-error');

				bValid 										= bValid && checkLength(FirstName,"FirstName",3,16);
				bValid 										= bValid && checkLength(LastName,"LastName",3,16);
				bValid 										= bValid && checkLength(Email,"Email",6,80);
				
				if (bValid) {
					
					// Show Status div
					$("#dialog_message").show();
					
					// Loop Through Fields and Create POST OBJECT (this is not a key/value pair as I originally thought but an object)
					$('#dialog :input').each(function(index) {
						var value;
						
						if ($(this).attr('checked'))
							value = 'Yes';
						else
							value = $(this).val();	
					    
					    params[$(this).attr('id')] 			= value;
					});
					
					$('#dialog select').each(function(index) {
					    
					    params[$(this).attr('id')] 			= $(this).find('option:selected').val();	
					});
					
					$('#dialog textarea').each(function(index) {
						
						params[$(this).attr('id')] 			= $(this).val();	
					});
					
					// POST contact changes (creates contact)
					$.post("/client/assets/php/event_submit.php",params,
							function(data){	
								console.log(data);
								$("#dialog_message").html('Saved Contact Information');
							}); 
					
					$(this).dialog('close');

				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	$("#dialog_message").ajaxStop(function(){
      	$(this).delay(5000).html('Success! <br/>We will be contacting you shortly').delay(1000).hide('slow');
     });

});
