/*****************************************
 * Copyright (c) 2008, Emmett M. Pate, Jr.
 * emmett@epate.com
 *
 */

function LeadTraxRequest(url) {
    $.ajax({
	url: url,
	cache: false,
	success: function(data) {
	    $("#dialog").html(data);
	    $("#dialog").dialog({
		autoOpen: true,
		modal: true,
		height: 500, width: 500,
		resizable: false,
		draggable: false,
		zIndex: 999999,
		title: "Request More Information",
		buttons: {
		    "Send": function() { $("#inquiryform").submit(); },
		    "Cancel": function() { $(this).dialog("close"); }
		}
	    }); 
	},
	complete: function() {
	    $('#inquiryform').ajaxForm({
		dataType: 'html',
		beforeSubmit: function(formData, jqForm, options) {
		    
		    //Find the dialog button Send.  Once we find the button, we then disable it.
		    //We reneable the button for Sending if the form check returns false.
                    var dialog_selector = "#dialog";

		    $(dialog_selector).parent().find("button").each(function() {
			if( $(this).text() == 'Send' ) {
				$sendbutton = $(this);			    
				$sendbutton.attr('disabled', true);
				$("#LTmessage").html("Sending...please wait.");
			}
		    });  
		    
			
		    /*log.debug('beforeSubmit'); */
		    $("#error").html('');
		    var form = jqForm[0];
		    if (!form.FirstName.value) { $("#error").html('Please enter your first name'); $sendbutton.attr('disabled', false); return false; }
		    if (!form.LastName.value) { $("#error").html('Please enter your last name'); $sendbutton.attr('disabled', false); return false; }
		    if (checkEmail(form.EmailAddress.value) == false) { $("#error").html('Please enter a valid email address'); $sendbutton.attr('disabled', false); return false; }
		},
		success: function(html) {
		    /*log.debug('success'); */
		    $("#dialog").dialog("destroy");
		    _actAlert('Your request has been sent!');
		}
	    });
	}
    });
}	

function _actAlert(t)
{
    $("#dialog").html(t);
    $("#dialog").dialog({
	autoOpen: true,
	modal: true,
	resizable: false,
	draggable: false,
	title: "Alert...",
	buttons: {
	    "OK": function() { $(this).dialog("close"); }
	}
    });
}

function checkEmail(Address) {
 if ((Address.length < 6) || 
  (Address.indexOf("@") == -1) || 
  (Address.indexOf(".") == -1) || 
  (Address.charAt(Address.indexOf("@")+1) == ".") || 
  (Address.charAt(Address.indexOf("@")-1) == ".") || 
  (Address.charAt(0) == ".") || 
  (Address.charAt(0) == "@")) {
   return false;
 }
 return true;
}

