    $(document).ready( function() {
      
      var mailto = {};  
       
        function build_workspace(recipient,policy) {
          // destroy any existing workspace
          $(".ajaxform").remove();
          $("#ajaxcontact").remove();
          
          // Create the workspace after the body
          $("body").append("<div class='ajaxform'><div id='close'>X</div><div id='back'><-</div><div class='form-inner'></div></div><div id='ajaxcontact'></div>");
          
          if (policy) {
            //console.log("Workspace Built w/ policy!");
            $(".form-inner").append("<div class='policy'></div><div id='consent'></div>");
            $(".ajaxform").css({"width":"48%","height":"350px","margin-left":"0"});
            
            var consent = "<input type='checkbox' name='agree' id='agree'> I have read the Privacy Policy.<input class='next' type='button' value='Next >>' disabled='disabled' />";
            $("#consent").append(consent);
            
            $("#agree").click(function() {
              var ischecked = $('#agree').is(':checked'); 
              if (ischecked) {
                $("#consent .next").attr("disabled","");
                //console.log("checked");
                } else {
                $("#consent .next").attr("disabled","disabled");
                //console.log("unchecked");
                }   
            });
            
            $(".next").click(function() {
              get_form(recipient);
            });
          }
          
          //Setup close and back buttons  
          $("#close").click(function() {
            $(".ajaxform").remove().end();
            $("#ajaxcontact").remove();
          });
          
          $("#back").hide().click(function() {
            $(".ajaxform .result").remove();
            $("#requestform").show();
            $(this).hide();
          });
          
          $(".ajaxform").slideDown();
        }
        /*******************************************************************
         * Submit the email to the handling page
         */
        function send_form(body,phone,email,businame,recipient,terms) {
          $.ajax( {
            type: "POST",
            url: "/ajaxcontact/form-submit.php",
            data: "recipient="+recipient+"&body="+body+"&businame="+businame+"&phone="+phone+"&email="+email+"&terms="+terms,
            success: function(msg) {
              $("#requestform").hide();
              $("#back").show();
              $(".form-inner").append("<div class='result'>"+msg+"</div>");
              ////console.log("Success - form sent: "+msg);
            }, //end success
            error: function(msg) {
              //console.log("Error - failed to send form: "+msg);
            }
          }); // end ajax
        }
        /************************
         * Build the workspace & Get the form.
         */
        function get_form(recipient) {
          
          /* DEPRECIATED
            Since IE can't handle fixed positioning, we calculate from the top
          
          var browser_top = document.getElementsByTagName('body')[0].scrollTop;
          browser_top = browser_top;
          */
          
          build_workspace(recipient);
          
          $.ajax( {
            type: "POST",
            url: "/ajaxcontact/info-request.php",
            data: "mailto=" + recipient+"&js=1",
            success: function(inforequest){
              //append the form inside the workspace
              $(".form-inner").empty().append(inforequest);
              
              // Submitting the real form
              $(".submit").click( function() {
                var body      = $("#body").val();
                var phone     = $("#phone").val();
                var email     = $("#email").val();
                var businame  = $("#businame").val();
                var recipient = $("#recipient").val();
                var terms     = $("#terms").val();
                 
                send_form(body,phone,email,businame,recipient,terms);
              }) // end click  
            },// end success on first call
            error: function(msg) {
              $("#requestform").hide();
              $("#back").show();
              $(".form-inner").append("<div class='error'>"+msg+"</div>");
              //console.log("Error - failed to get form: "+msg);
            } // end failure
          }); // end ajax first call
        }
        /*******************************************************************
         * Retrieve the allowed email list 
         */
        function get_allowed() {
          var allowed = '';
          $.ajax({
              async: false,
              type: "GET",
              url: "/ajaxcontact/allowed-list.txt",
              success: function(msg) {
                allowed = msg;
                ////console.log("Got allowed! - "+msg);
                return allowed;
              }, // end success
              error: function(msg) {
                //console.log("Error - Failed to get allowed list: "+msg); // failed to get allowed list
              } // error
          });
          return allowed;
        }
        
        /*******************************************************************
         * Compare the current mailto: link with the allowed list
         * 
         */
        function check_allowed(recipient, allowed_list) {
          var good = allowed_list.indexOf(recipient);
          if(good == '-1') {
            $(this).end(); // I hope this does what I think
          } else {
              //rebuild the link with our replacement
              $("a[href^=mailto:"+recipient+"]").attr({href:"#contact", rel: recipient })
                .click( function() {
                  // Get the info-request form
                  var policy = get_policy();
                  ////console.log("Click! - "+policy);
                  build_workspace(recipient,1);
                  $('.ajaxform .policy').append(policy);
                  ////console.log("check allowed policy return: "+policy);
                });// end click function
          } // end if - good check
        }
        
         /*******************************************************************
         * Retrieve the privacy policy page 
         */
        function get_policy() {
          var txt = '';
          $.ajax({
              async: false,
              type: "GET",
              url: "/ajaxcontact/privacy.txt",
              success: function(msg) {
                txt = msg;
                ////console.log("Success - Got policy: "+msg);
              }, // end success
              error: function(msg) {
               // $("body").append("<!--"+msg+"-->"); // failed to get allowed list
                //console.log("Error - failed to get policy: "+msg);
              } // error
          });
          return txt;
        }
        
        /*******************************************************************
         * The real work is done here.
         */
        // get the allowed list
        var allowed_list = get_allowed();
        
        // get all the mailto: links on the page into an array
        var all_mail_tos = [];
        all_mail_tos = jQuery.makeArray($("a[href^=mailto:]"));
        
        // If the recipient is on the list, convert mail link
        jQuery.each(all_mail_tos, function() {
          var recipient = $(this).attr("href");
          recipient = recipient.substr(7); // Get rid of mailto:
          
          check_allowed(recipient,allowed_list);    
        });
      /* The End */
      });
