/* ----------------------------------------------------------------------------------------------------------------------
**  DATA OBJECTS AND VARIABLES                                                                                   
**
** ----------------------------------------------------------------------------------------------------------------------
*/
var _rc_validator;       /* Validator Variable */


/* ----------------------------------------------------------------------------------------------------------------------
**  DOCUMENT READY FUNCTION                                                                                       
**
** ----------------------------------------------------------------------------------------------------------------------
*/
$(document).ready(function(){
	
	RC_InitializeContactForm();
	RC_InitializeContactFormValidation();
	
	// Add a mask for the phone field
	$("#rc_phone").mask("(999) 999-9999");
	
});

/* ----------------------------------------------------------------------------------------------------------------------
**  INITIALIZATION FUNCTION FOR THE FORM                                                                                       
**
** ----------------------------------------------------------------------------------------------------------------------
*/
function RC_InitializeContactForm() {

	// Set up the dialog
	$("#dialog_request_callback").dialog({
		autoOpen: false,
		close: function(event, ui) { RC_ResetRequestCallback(); },
		width: 500,
		modal: true,
		draggable: false,
		resizable: false,
		buttons: {
			"Send Request": function() {
					   
						// Validate the form
						if(RC_IsRequestCallbackValid() != true) return false;
						
						// Validation Passed - So show the send animation 
						$('#EmailSend').show();
						
						// Bundle up the data to be sent
						var rcname = $("input#rc_name").val();
						rcname=rcname.replace(/ /g,"~");
						var rcaddress = $("input#rc_address").val();
						rcaddress=rcaddress.replace(/ /g,"~");
						var rcemail = $("input#rc_email").val();
						var rcphone = $("input#rc_phone").val();
						var rcreason = $("textarea#rc_comment").val();
						rcreason=rcreason.replace(/ /g,"~");
						var form_data = 'rc_name='+ rcname + '&rc_address=' + rcaddress + '&rc_email=' + rcemail + '&rc_phone=' + rcphone + '&rc_comment=' + rcreason;

						// Make an ajax call to send the data to the server
						$.ajax({
							type: 'post',								
							url: "scripts/request_callback.asp",
							data: form_data,
							error: function () {
								$('#EmailSend').hide();
								$('#message')
									.addClass('error')
									.text("An error has occurred. Your request cannot be made at this time. Please try again at a later time.");
								$('.ui-dialog-buttonpane button:contains(Send Request)').hide(0);
							},
							success: function() {
									$('#EmailSend').hide();
									$('#message')
										.addClass('success')
										.text("Your contact request has been sent.");
									$('.ui-dialog-buttonpane button:contains(Send Request)').hide(0);
									$('.ui-dialog-buttonpane button:contains(Cancel)').hide(0);
									$('.ui-dialog-buttonpane button:contains(Close)').show(0);
							}
						});
						return false;
						
			},
			"Cancel": function() { $(this).dialog("close"); },
			"Close": function() { $(this).dialog("close"); }
		}
	});
	
	// Bind the anchor that will show the dialog
	$(".request_callback").click(function() {
													  
		// Open the dialog and hide the close button we wont need it for this application
		$("#dialog_request_callback").dialog("open");
		$('.ui-dialog-buttonpane button:contains(Close)').hide(0);
		return false;
	});
	
}

/* ----------------------------------------------------------------------------------------------------------------------
**  INITIALIZATION FUNCTION FOR THE FORM VALIDATION                                                                                      
**
** ----------------------------------------------------------------------------------------------------------------------
*/
function RC_InitializeContactFormValidation(){
	
	// Add a method to the validator to validate the phone number
	$.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
    }, "Please enter a valid phone number");

	// Now initialize the validator with the following rules and error messages
	_rc_validator = $("#dialog_request_callback").validate({
		rules: {
			rc_name: "required",
			rc_phone: {
				phoneUS: true
			},
			rc_email: {
				required: true,
				email: true
			}
		},
		messages: {
			rc_name: "Please enter your name",
			rc_phone: {
				phoneUS: "Enter a valid phone number"
			},
			rc_email: {
				required: "Please enter your email address",
				email: "Enter a valid email adddress"
			}
		}
	});

}

/* ----------------------------------------------------------------------------------------------------------------------
**  INITIALIZATION FUNCTION FOR THE FORM VALIDATION                                                                                      
**
** ----------------------------------------------------------------------------------------------------------------------
*/
function RC_IsRequestCallbackValid()
{
	var valid = $("#dialog_request_callback").valid();
	return valid;
}

/* ----------------------------------------------------------------------------------------------------------------------
**  INITIALIZATION FUNCTION FOR THE FORM VALIDATION                                                                                      
**
** ----------------------------------------------------------------------------------------------------------------------
*/
function RC_ResetRequestCallback() {
	$("#rc_name").val("");
	$("#rc_address").val("");
	$("#rc_csz").val("");
	$("#rc_phone").val("");
	$("#rc_email").val("");
	$("#rc_comment").val("");
	
	_rc_validator.resetForm(); 
	$("#message")
		.removeClass('success')
		.removeClass('error')
		.text("If you would like to receive a call from someone at our office. Please fill out the form below and we will get back to you within 48 business hours.");
	$("#dialog_request_callback input").removeAttr('readonly');
	$('.ui-dialog-buttonpane button:contains(Send Request)').show(0);
	$('.ui-dialog-buttonpane button:contains(Cancel)').show(0);
	$('.ui-dialog-buttonpane button:contains(Close)').hide(0); 
}



