// Change requested by defect S0456107.
var g_regExpInvalidLogonChars = /[!(^\"\(\)<>)]+/g;

function checkLogonUserPasswordInput(formID,event,inputs,submitFocus) {
    var keyCode = ( event.which != null ) ? event.which : event.keyCode;
    var stopProcessing = false;
    // return key was pressed
    if (keyCode === '\r'.charCodeAt(0)) {
        var submitForm = true;

        // if all input elements are non-blank then submit the form
        if (inputs != null) {
            for (var i=0; i<inputs.length; i++) {
                if (findDOM(inputs[i]) != null
                    && findDOM(inputs[i]).value.match(/^\s*$/)) {
                    submitForm = false;

                // set focus to next input component
                } else if (i+1 < inputs.length
                           && findDOM(inputs[i+1]) != null) {
                    if (document.forms[formID].elements[inputs[i+1]] != null) {
                        document.forms[formID].elements[inputs[i+1]].focus();
                    } else {
                        if (findDOM(input[i+1]) != null) {
                            findDOM(input[i+1]).focus();
                        }
                    }
                } else if (submitFocus != null && findDOM(submitFocus) != null){
                    findDOM(submitFocus).focus();
                }
            }
        }
        if (submitForm) {
            pageSubmit();
        }

    // must be not be one of the forbidden characters
    } else if (String.fromCharCode(keyCode).match(g_regExpInvalidLogonChars)) {
        stopProcessing = true;
        // IE has different mechanism to cancel event processing
        if (navigator.appName.indexOf('Netscape') == -1) {
            event.returnValue = false;
        }
    }
    return stopProcessing;
}

function checkFieldsThenSubmit(formID,event,inputs,submitFocus) {
    var keyCode = ( event.which != null ) ? event.which : event.keyCode;
    // return key was pressed
    if (keyCode === '\r'.charCodeAt(0)) {
        var submitForm = true;

        // if all input elements are non-blank then submit the form
        if (inputs != null) {
            for (var i=0; i<inputs.length; i++) {
                if (findDOM(inputs[i]) != null
                    && findDOM(inputs[i]).value.match(/^\s*$/)) {
                    submitForm = false;

                // set focus to next input component
                } else if (i+1 < inputs.length
                           && findDOM(inputs[i+1]) != null) {
                    if (document.forms[formID].elements[inputs[i+1]] != null) {
                        document.forms[formID].elements[inputs[i+1]].focus();
                    } else {
                        if (findDOM(input[i+1]) != null) {
                            findDOM(input[i+1]).focus();
                        }
                    }
                // avoid a bell if submit of form while focus not on button
                } else if (submitFocus != null && findDOM(submitFocus) != null) {
                    findDOM(submitFocus).focus();
                }
            }
        }
        if (submitForm) {
            pageSubmit();
            // fix for S0361990:
			if (event.preventDefault) {
				event.preventDefault()
			}
			else { 	// Is IE
				event.returnValue=false;
			}
			return false;
        }
    }
	return false;

}
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX CLASS BREAK XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
//------------------------------------------------------------------------------
// CLASS: RemoveInvalidLogonChars
//
// @param textInputField - the "textarea" element whose input is to be restricted
// the "textarea"
//------------------------------------------------------------------------------
function RemoveInvalidLogonChars(textInputField)
{
    this.textField = textInputField;
}

//------------------------------------------------------------------------------
// For any events that would normally cause the value of the associated
// "textarea" to change this method responds by blocking any additonal
// input characters that would cause the maximum number of characters to be
// exceeded .
//------------------------------------------------------------------------------
RemoveInvalidLogonChars.prototype.handleEvent = function(event)
{
    event = event != null ? event : window.event;
    if( event != null )
    {
        if( isPasteEvent != null && isPasteEvent(event)  )
        {
            var strPasteData = _removeBadLogonCharacters(window.clipboardData.getData('Text'));

            var currentCharacterCount = strPasteData.length;
            if( currentCharacterCount > 0 )
            {
                // => insert paste chars at caret position
                // => use document's selection object to obtain insertion point
                if( document.selection != null )
                {
                    var insertionRange = document.selection.createRange();
                    insertionRange.text = strPasteData;
                    //automatically unselect text so subsequent input adds
                    insertionRange.collapse(false);
                    insertionRange.select();
                } else {
                    this.textField.value = this.textField.value + strPasteData;
                }
            }
        }
        //simply block the default paste and keypress behavior
        return _terminateLogonEventProcessing(event);
    }//if( event != null )
    else
    {
        alert("event == null");
        return false;
    }
}

//------------------------------------------------------------------------------
// Initializes the object by registering itself as a listener for the relevant
// events fired by the associated "textarea" object. Events listened for include
// paste events.
//
// NOTE: Users of this class must call this method for the class to have any effect.
//------------------------------------------------------------------------------
RemoveInvalidLogonChars.prototype.initialize = function ()
{
    registerEventHandler( this.textField, 'paste', this, false );
}
//------------------------------------------------------------------------------
// Private method that removes all logon violation characters.
//
// Listed in g_regExpInvalidLogonChars
//------------------------------------------------------------------------------
function _removeBadLogonCharacters(value)
{
    var returnValue = value;
    // replace anything that is not one of these characters
	returnValue = returnValue.replace(g_regExpInvalidLogonChars, "");
    return returnValue;
}
//------------------------------------------------------------------------------
// Private method that prevents the event being processed from being propagated
// up the document hierarchy. Also prevents the default behavior for the event
// being processed from occurring.
//------------------------------------------------------------------------------
function _terminateLogonEventProcessing(eventObj)
{
    if( eventObj.preventDefault != null ) {
        //alert("preventDef");
        eventObj.preventDefault();
    }

    eventObj.returnValue = false;
    //eventObj.cancelBubble = true;

    if( eventObj.stopPropagation != null ) {
        //alert("stopPropagation");
        eventObj.stopPropagation();
    }

    return false;
}

