//prototype extensions...

Object.extend(String.prototype,{
    trim: function() {
        var s=this;
        while (s.substring(0,1) == ' ') {
        s = s.substring(1,s.length);
        }
        while (s.substring(s.length-1,s.length) == ' ') {
        s = s.substring(0,s.length-1);
        }
        return s;
    }
});

function obfuscateM(sDom, sUser){
    return("mail"+"to:"+sUser+"@"+sDom.replace(/%23/g,"."));
}

function molajs_mouseover(e) {
    Element.addClassName(e,"mouseover");
}

function molajs_mouseout(e) {
    Element.removeClassName(e,"mouseover");
}

function molajs_ajax_href(aURL) {
    if (arguments[1]) {
        var currentTargetID=arguments[1];
    } else {
        var currentTargetID=molajsDisplayID;
    }
    if (currentTargetID) {
        $(currentTargetID).innerHTML="<div class=\"loading\"><span>loading...</span></div>";
        var pars="action=render&referred="+molajsThisPage;  //could put referer here...
        var molaAjax= new Ajax.Updater(
            currentTargetID,
            aURL,
            {
                method: 'get',
                parameters: pars,
                evalScripts: false
            }
        );
    }
}

function molajs_ajax_registration() {
    //does nothing yet... ever?
}



var molajsReturnPressed=false;


Event.observe(document, 'keypress', function(event){
    if(event.keyCode == Event.KEY_RETURN && molajsCurrentType !="textarea") molajsReturnPressed=true;
});
function molajs_safe_submit() {
    if (molajsCurrentType=="submit") {
        return true;
    } else if (molajsReturnPressed) {
       molajsReturnPressed=false;
       return false;
    } else {
       return true;
    }
}


var molajsInputFields;
var molajsCurrentType="";

function molajs_register_field() {
  molajsCurrentType=this.type;
}

function initialize() {
    molajsInputFields = Form.getElements(document.forms[0]);
    molajsInputFields.each( function(field) {
        field.onfocus=molajs_register_field;
    })
}

/*
 * runOnLoad.js: portable registration for onload event handlers.
 *
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }

    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;





