
//
// Javascript to submit forms via a link
//

//
// "rawData" is in the same format as URI data
// (e.g. object=value&object=value&object=value)
//

var nav = window.Event ? true : false;
if (nav) {
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
   document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function submitForm(formName, operationName, rawData)
{
    // Get the form
    var form = document.getElementById(formName);

    // Add operation node
    var input = document.createElement("input");
    input.name = "REQUEST-INFO/operation/" + operationName;
    input.type = "hidden";
    form.appendChild(input);

    // Add data node
    var data = document.createElement("input");
    data.name = "REQUEST-INFO/raw-data";
    data.value = rawData;
    data.type = "hidden";

    form.appendChild(data);
    form.submit();

    return false;
}

function submitFormConfirm(formName, operationName, rawData, confirmMessage)
{
    if (window.confirm(xmlToJavascriptString(confirmMessage))) {
        submitForm(formName,operationName,rawData);
    }
    return false;
}

/*
    This function takes a string that was escaped to
    work properly in XML and replaces the XML
    entities with the proper javascript characters
*/
function xmlToJavascriptString(str)
{
    str = str.replace(/&apos;/, "\'");
    str = str.replace(/&quot;/, "\"");
    str = str.replace(/&gt;/, ">");
    str = str.replace(/&lt;/, "<");
    str = str.replace(/&amp;/, "&");

    return str;
}

/*
	The following two functions are required for controling the enter key on pages
*/

function NetscapeEventHandler_KeyDown(e) {
  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false; }
  return true;
}

function MicrosoftEventHandler_KeyDown() {
  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')
    return false;
  return true;
}

/*
    This function will put focus on the first text input field
    on a page.
    
    NOTE:  This function will not work if the first text field
    in the first form is hidden using DIV's when the page first
    loads.  At some point we need to determine if this can be
    factored in to the logic of this function.  For now if you have a
    page that is broken by this function based on the use of a
    DIV then you should override onLoad to do nothing (call the 
    function doNothing() below since just ommitting the attribute
    or setting onLoad="" will not override the default function).
*/
function placeInitialFormFocus() 
{
    if (document.forms.length > 0) {
        var field = document.forms[0];
        for (i = 0; i < field.length; i++) {
            if ((field.elements[i].type == "text") || 
                (field.elements[i].type == "textarea") || 
                (field.elements[i].type == "")) {
                	try {
	                    document.forms[0].elements[i].focus();
	                    break;
	                } catch (ex) {
	                	// an exception is thrown if an element cannot
	                	// receive focus for some reason, most likely
	                	// because it is invisible (i.e. hidden query
	                	// elements).
	                }
            }
        }
    }
}    		    

/*
    A very simple yet handy JavaScript function that does nothing.
*/
function doNothing()
{}