
function findElement(item) 
{
    if (window.mmIsOpera) return (document.getElementById(item));
    if (document.all) return (document.all[item]);
    if (document.getElementById) return (document.getElementById(item));
    return (false);
}

if (typeof VirtualForm == "undefined" || !VirtualForm) {
    /**
     * If the global namespace is already defined, the
     * existing namespace object will not be overwritten so that defined
     * namespaces are preserved.
     * @class
     * @static
     */
    var VirtualForm = {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 * This fails because "long" is a future reserved word in ECMAScript
 *
 * @method namespace
 * @static
 * @param  {String*} arguments 1-n namespaces to create 
 * @return {Object}  A reference to the last namespace object created
 */
VirtualForm.RegisterNamespace = function() 
{
    var a = arguments;
    var o = null;
    var i;
    var j;
    var d; 

    for (i=0; i<a.length; i=i+1) 
    {
        d = a[i].split(".");
        o = VirtualForm; 

        // VirtualForm is implied, so it is ignored if it is included 
        for (j=(d[0] == "VirtualForm") ? 1 : 0; j<d.length; j=j+1) 
        { 
            o[d[j]]=o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o;
};

//declare the Namespace
VirtualForm.RegisterNamespace("VirtualForm.Net");

//declare Virtual Form Object
VirtualForm.Net.Script = function(formDiv, submitBtnId)
{
    this.formDiv = formDiv;
    this.submitBtnId = submitBtnId;   
    
    // When using these functions as event delegates the this keyword no longer points to this object as it is out of context
    // so instead, create an alias and call that instead.
    var me = this;
      
    this.submitVirtual = function()
    {
        var target = findElement(me.submitBtnId);
        //check the type of the target: If a button then call the click method.
        if(target.tagName.toLowerCase() === 'input')
        {
            target.click();
        }
        //If a link button then simulate a click.
        if(target.tagName === 'A')
        {
            window.location.href = target.href;
        }
    };

    this.handleEnterKey = function(event)
    {     
	    var moz = window.Event ? true : false; 
	    if (moz) { 
		    return me.MozillaEventHandler_KeyDown(event); 
	    } else { 
		    return me.MicrosoftEventHandler_KeyDown(); 
	    } 
    }; 

    //Mozilla handler (also Handles Safari)
    this.MozillaEventHandler_KeyDown = function(e) 
    {
	    if (e.which == 13) { 
		    e.returnValue = false; 
		    e.cancel = true; 
		    e.preventDefault();			
		    me.submitVirtual(); // call the delegate function that simulates the correct button click
		    return false; 		
	    }
	    return true; 
    }; 

    //IE Handler
    this.MicrosoftEventHandler_KeyDown = function() 
    {
	    if (event.keyCode == 13) { 
		    event.returnValue = false; 
		    event.cancel = true;
		    me.submitVirtual(); // call the delegate function that simulates the correct button click
		    return false;
	    } 
	    return true; 
    };

    this.addEvent = function(ctl, eventType, eventFunction)
    {
        if (ctl.attachEvent){
	        ctl.attachEvent("on" + eventType, eventFunction);
        }else if (ctl.addEventListener){
	        ctl.addEventListener(eventType, eventFunction, false);
        }else{
	        ctl["on" + eventType] = eventFunction;
        }
    };

    this.removeEvent = function(ctl, eventType, eventFunction)
    {
        if (ctl.detachEvent){
	        ctl.detachEvent("on" + eventType, eventFunction);
        }else if (ctl.removeEventListener){
	        ctl.removeEventListener(eventType, eventFunction, false);
        }else{
	        ctl["on" + eventType] = function(){};
        }
    };

    this.stopEvent = function(e)
    {
        if (e.stopPropagation){
        // for DOM-friendly browsers
	        e.stopPropagation();
	        e.preventDefault();
        }else{
        // For IE
	        e.returnValue = false;
	        e.cancelBubble = true;
        }
    };
    
    //Grab all input elements within virtual form (contents of a div with divID)
    this.inputs = this.formDiv.getElementsByTagName("input");

    //loop through them and add the keypress event to each to listen for the enter key
    for (var i = 0; i < this.inputs.length; i++)
    {
        this.addEvent(this.inputs[i], "keypress", this.handleEnterKey);
        this.addEvent(this.inputs[i], "keydown", this.handleEnterKey);
    }
}