//	Standard Routines for Form Handling

//	Copyright (c) gaelITech 2008
//	www.gaelitech.ie
//	Modifications:
//	Date		Who				What
//	01/10/2009	Peter McPoland	Release 2

var	gi_Form_Confirmed = 1;
var	gi_Form_SubmitButton = 0;	// if set, a Form 'submit' Button has been clicked
var	gi_Form_Anchor = 0;			// if set, a Form anchor has been clicked
var	gi_Form_FieldChanged = 0;	// if set, a Form Field has been changed

//	Insert caret into an <input> field at specific offset
function gi_Form_SetFocus(id, offset)
{
	var oElement = document.getElementById(id);

	if	(oElement)
	{
		oElement.focus();
		if	(oElement.isTextEdit)
		{
			oTextRange = oElement.createTextRange();
			oTextRange.move("character", offset);
			if	(offset != 0)
			{
				oTextRange.moveEnd("character", 1);
				oTextRange.select();
			}
			oTextRange.scrollIntoView();
		}
	}
}

//	Confirm use of CANCEL button
function gi_Form_Confirm(bIfChanged)
{
	gi_Form_Confirmed = 1;

	if	((bIfChanged == 0) || ((bIfChanged == 1) && (gi_Form_FieldChanged == 1)))
		if	(!window.confirm("You are about to lose any changes you may have made\nAre you sure?"))
			gi_Form_Confirmed = 0;
}

//	Trap Form submission
function gi_Form_Confirm_Submit()
{
	var	save = gi_Form_Confirmed;
	
	gi_Form_Confirmed = 1;
	
	if	(save == 1)
		return	true;
	else
	{
		gi_Form_SubmitButton = 0;
		return	false;
	}
}

//	Trap browser window close
function	gi_Form_Catch_Winclose(s)
{
	var oElement = document.getElementById('gi_winclose_form');

	if	((gi_Form_SubmitButton == 0) && (gi_Form_Anchor == 0))
	{
		if	(oElement)
			oElement.submit();
	}
}

//	Set global flag when an anchor is clicked (via onclick event)
function	gi_Form_Anchor_Clicked()
{
	gi_Form_Anchor = 1;
}

//	Set global flag when a Form 'submit' button is clicked (via onclick event)
function	gi_Form_SubmitButton_Pressed()
{
	gi_Form_SubmitButton = 1;
}

//	Set global flag when a Form Field is changed (via onchange event)
function	gi_Form_Field_Change()
{
	gi_Form_FieldChanged = 1;
}

//	Trigger Submit on a Hidden Form
function	gi_Form_Submit_Hidden(id)
{
	var oForm = document.getElementById(id);

	if	(oForm)
		oForm.submit();
}
