function digitsOnly(evt)
{
	if (evt == null)
	{
		evt = window.event;
	}
	var key = evt.keyCode;
	// Block characters, shifted digits or 'other' characters
	if ((key >= 65 && key <= 90) || (evt.shiftKey && key >= 48) || key > 128)
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		
		return false;
	}
	return true;
}

function padField(field, ch, len)
{
	var val = field.value;
	if (val.length > 0)
	{
		while (val.length < len)
		{
			val = ch + val;
		}
		field.value = val;
	}
}

function decorateButtons(noAutoComplete)
{
	if (arguments.length == 0)
	{
		noAutoComplete = false;
	}
	// Conceivably, if the user hits back this will counteract its effect.
	window.history.forward();
	
	var inp = document.getElementsByTagName("input");

	for (var i = 0; i < inp.length; i++)
	{
		if (inp[i].type == 'submit' && !inp[i].disabled)
		{
			inp[i].onmouseover = function() {this.style.color = '#ffff00';};
			inp[i].onmouseout = function() { this.style.color = '#ffffff';};
		}
		else if (inp[i].type == 'submit' && inp[i].disabled)
		{
			// This does nothing on IE, which has a separate disabled color, but it gives buttons on Firefox that same color.
			inp[i].style.color = "#8c8951";
		}
		else if (noAutoComplete && inp[i].type == 'text')
		{
			// This disables autocomplete on all text fields
			inp[i].autocomplete = 'off';
		}
	}		
}

function manageAcceptance()
{
    var accept1 = document.getElementById("generalForm:acceptLiability");
    var accept2 = document.getElementById("generalForm:acceptServices");

    var acceptBtn = document.getElementById("generalForm:cmdAccept");

    if (acceptBtn)
    {

        if (accept1.checked && accept2.checked)
        {

            acceptBtn.style.visibility = 'visible';
            acceptBtn.disabled=false;
        }
        else
        {

            acceptBtn.disabled=true;
        }
    }
}
function initPage()
{
	try {
		manageAcceptance();
	} catch(e) {
		
	}
	document.body.onkeydown = handleBack;
}

function preventDoubleSubmission() {
	// This will add an event handler that should prevent multiple form submissions.
	var form = document.getElementById("generalForm");
	form.onsubmit = function() {return handleSubmit();};
}

// This will prevent a form from being submitted multiple times. 
// Once it has been called once, any further calls will return false, which will
// stop the form from submitting. 
var submitted = false;
function handleSubmit()
{
	if (submitted)
	{
		return false;
	}
	document.body.style.cursor = "wait";
	submitted = true;
	
	return true;
}

function resetSubmit()
{
	submitted = false;
	document.body.style.cursor = "auto";
}

// Block the backspace key except when on an editable text input.
// Failure to do this will cause the key to act like the browser 'Back' button.
function handleBack(evt)
{
	if (evt == null)
	{
		evt = window.event;
		evt.target = evt.srcElement;
	}
	var key = evt.keyCode;
	if (key == 8 || key == 13)
	{
		var tag = evt.target.tagName.toLowerCase();
		var type = evt.target.type.toLowerCase();
		if (!((tag == 'input' && (type == 'text' || type == 'submit')) || tag == 'textarea') || evt.target.readOnly || evt.target.disabled)
		{
			evt.cancelBubble = true;
			evt.returnValue = false;
			if (evt.stopPropagation) {
				evt.stopPropagation();
			}
			if (evt.preventDefault) {
				evt.preventDefault();
			}
			
			return false;
		}
	}
	if (key == 13) // Block all enter key strokes except for those on text areas, active buttons and links.
	{
		var tag = evt.target.tagName;
		var type = evt.target.type;
		if (!((tag == 'INPUT' && (type == 'button' || type == 'submit')) || tag == 'A' || tag == 'TEXTAREA') || evt.target.readOnly || evt.target.disabled)
		{
			evt.cancelBubble = true;
			evt.returnValue = false;
			if (evt.stopPropagation) {
				evt.stopPropagation();
			}
			if (evt.preventDefault) {
				evt.preventDefault();
			}
			
			return false;
		}
	}
	return true;
}

function clearTag(tag)
{
	while (tag.childNodes.length > 0)
	{
		tag.removeChild(tag.childNodes[0]);
	}
}

function focusOnFirstInput()  {
	var forms = document.forms;
	var len = forms.length;
	var i;
	for (i = 0; i < len; i++) {
		var form = forms[i];
		for (var j = 0; j < form.length; j++) {
			var input = form[j];
				if (!input.disabled) {
					input.focus();
					return;
				}
		}
	}
};


function focusOn(idName)  {
	var forms = document.forms;
	var len = forms.length;
	var i;
	for (i = 0; i < len; i++) {
		var form = forms[i];
		for (var j = 0; j < form.length; j++) {
			var input = form[j];
			if(input.name == idName) {
				input.focus();
				return;
			}
		}
	}
};


//JSF will change the ID attribute so it is not possible to guarantee what the value will be.
//This will look for elements of a particular type, within a form, whose ID contain the original ID.
function findElement(formName, idSuffix, type)
{
	var form = document.getElementById(formName);
	var tags = form.getElementsByTagName(type);
	
	var element = null;
	for (var i = 0; i < tags.length; i++)
	{
		var id = tags[i].id;
		if (id.indexOf(idSuffix) > 0)
		{
			element = tags[i];
			break;
		}
	}
	return element;
}

//Jump to the next input when the current one if full.
function autoTab(len,evt)
{
	if (evt == null)
	{
		evt = window.event;
	}
	var key = evt.keyCode;
	var tag = evt.srcElement;
	var valLen = tag.value.length;

	// If the user had entered a digit and the length is now the maximum length,
	// jump to the next input.
	if (key >= 48 && valLen == len)
	{
		var nextElement = tag.nextSibling;
		// Skip any non-input elements
		while (nextElement && nextElement.tagName != 'INPUT')
		{
			nextElement = nextElement.nextSibling;
		}
		if (nextElement)
		{
			nextElement.focus();
		}
	}
}
