jQuery.fn.defuscate = function(settings) { 
   settings = jQuery.extend({link: true}, settings); 
   regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})(.*)\b/gi; 
   mailto = '<a href="mailto:$1@$2$3">$1@$2</a>'; 
   plain = "$1@$2$3"; 
   return this.each(function() { 
     var href = jQuery(this).attr("href");
     if (href) {
        var hrefdecoded =  href.replace(regex,plain);
        jQuery(this).attr("href",hrefdecoded);

        var defuscated;
        defuscated = jQuery(this).html().replace(regex,plain) 
        jQuery(this).html(defuscated); 
     } else {
        defuscated = jQuery(this).html().replace(regex, settings.link ? mailto : plain) 
        jQuery(this).html(defuscated); 
     }
   }); 
};
jQuery(document).ready(function() {
    jQuery(".email_link").defuscate();
    jQuery(".email_text").defuscate({link: false});
});

//-------------------------------
jQuery(document).ready(function() {
    var mforms = jQuery("form[action*='managedforms.com']"); //all managed forms on the page
    //alert(mforms.length);
    

    mforms.each(function(idx,o) {
        var frm = jQuery(o);
        frm.addClass("managedform");

        //rewrite each managedform's action to include the current querystring parameters.
        var url = location.href;
        var qposn = url.search(/\?/);
        var actionqstring;
        var testmode = false;
        if (qposn > 0) {
            var action = frm.attr("action");
            aposn = action.search(/\?/);
            var actionbase, newaction;
            if (aposn > 0) {
                //the hardcoded action parameter also contains a querystring..
                // - we will need to merge the parameters, with hardcoded ones taking precedence.
                actionbase = action.substring(0,aposn-1);
                actionqstring = action.substring(aposn+1);
                var actionpairs = actionqstring.split("&");
                var actionvars = [];
                for (var a=0;a<actionpairs.length;a++) {
                    var actionparts = pairs[i].split("=");
                }
                jQuery().each(actionpairs, function(i,v) {
                    alert('actionpair:'+i+" "+v);
                });
            } else {
                //no querystring in the action parameter.
                actionbase = action;
                actionqstring = "";
                var actionpairs = [];
                var actionvars = [];
            }
            
        
            var qstring = url.substring(qposn+1);
            var pairs = qstring.split("&");
            for (var i=0;i<pairs.length;i++) {
                var parts = pairs[i].split("=");
                if (parts[0] == "testmode") {
                    if (parts[1] == true) {
                        testmode = true;
                    }
                }
                //temp hack
                if (actionqstring.length > 0) {
                    actionqstring = actionqstring + "&" + pairs[i];
                } else {
                    actionqstring = pairs[i];
                }
                
            }
            if (actionqstring.length > 0) {
                newaction = actionbase + "?" + actionqstring;
            } else {
                newaction = actionbase;
            }
            if (testmode == true) {
                alert(newaction);
            }
            frm.attr("action",newaction);
        }
        
        
        //if there is no div with the class 'errorsummary' within this form - add one.
        var errdiv = frm.find('div.errorsummary');
        if (errdiv.length > 1) {
            alert("Form misconfiguration? - Within the same form, there appear to be multiple 'div' elements with the class 'errorsummary'");
        }
        if (errdiv.length == 0) {
            //insert a div of class 'errorsummary' prior to the closing </form> tag.
            frm.append("<br/><div class='errorsummary' style='display: none;'><span></span></div>");
        }
    });
    
    
});



