/*
'--------------------------------------------------------------------------------------------------
' Title			: (modal?) Dialog "Leave Feedback" JavaScript
' Description	: JavaScript function for the pop up form (div) on (for example) the member profile page
'					see php file named similarly in inc folder
'--------------------------------------------------------------------------------------------------
' History
' 1/6/2009	: GF - Created Page
'					some code used from http://jqueryui.com/demos/dialog/#modal-form
'--------------------------------------------------------------------------------------------------
*/

$(document).ready(function(){

	var feedbackComment = $("#FeedbackComment"),
		allFields = $([]).add(feedbackComment),
		tips = $("#validateTips");
		
	// Initialize the Feeback dialog
	$('#FeedbackDialog').dialog({
		autoOpen:false,
		modal:true,
		buttons:{
			'Submit Feedback':function(){

				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(feedbackComment,"Comment",5,1000);
								
				if (bValid) {
					document.getElementById('leave_feedback_submitted').value = 'Feedback Form Submitted!';
					tips.text("Processing...");
					
					//Submit the form for processing...
					$("form:FeedbackDialog").submit();

				}
				else
				{
					//tips.text("No");
				}

			
			},
			Cancel:function(){
				$(this).dialog('close');
			}
		}
	});
	$('#FeedbackDialogBtn').click(function() {
		$('#FeedbackDialog').dialog('open');
	});

		
	// tips are like instructions
	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			if (min == max)
			{
				updateTips("Length of " + n + " must be "+min+".");
			}
			else
			{
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			}
			return false;
		} else {
			return true;
		}

	}	

});


