
// =================================== start DEFAULT BUTTON CODE (author: Matthew Robinson) ===================================

// allows the developer to nominate a submit button to be activated when the user presses the ENTER key.
// the nominated button is indicated by the addition of a suffix (see global constant 'g_conDefaultIDSfx') to the
// button's id in the HTML/ASP source


// -- declare global constants -----
var g_conDefaultIDSfx = "_DFLT";			// we search for this suffix in the default button's id 
var g_conInputFieldName = "txtFundsName";	// the id of the text field used for input, and for the associated hidden field
var g_conHiddenFieldName = "txtFundsName2"	// the id of the hidden field that we use to ensure that a value is posted properly.
											/* 
												NOTE:	the use of underscores in this id seems to be problematic
												NOTE: 	the use of this hidden field only became necessary due to a quirk in IE6
														whereby the input field (see g_conInputFieldName)
                            							seems to retain the focus even after the focus has been given to the submit
														button (see g_conDefaultIDSfx).
                                						this problem prevents any value from being posted when the form is submitted,
														so it is necessary to transfer the value to an alternative field
											*/


// -- declare global variables -----
var g_blnFieldHasFocus;						// used to flag whether the text field (g_conInputFieldName) currently has focus







// ------- f_SetDefaultButton() [attach the main event handlers] --------------------------------------------------------------
function f_SetDefaultButton()
{

	g_blnFieldHasFocus = false;
	
	// holds a reference to the input field
	var objFieldRef;
	
	// get a reference to the 'text' input field (as opposed to the 'hidden' input field)
	var arrInputFields = document.getElementsByTagName('input');
	var intUBound = arrInputFields.length;

	for (var i=0; i<intUBound; i++)
	{
		if (arrInputFields[i].type == 'text') { objFieldRef = document.getElementById(arrInputFields[i].id); }
	}
	
	// if the text field cannot be found then we exit the script
	if (!objFieldRef) { return; }
	

	
	// attach handlers that can be used to flag the onfocus / onblur status of the main input field
	objFieldRef.onfocus = f_FlagFieldFocus;
	objFieldRef.onblur = f_UnFlagFieldFocus;
	
	// attach a handler that will analyse each keypress event to check whether it was triggered by the ENTER key
	document.onkeypress = f_KeyPressHandler;
		
}
// ....... end f_SetDefaultButton() ...........................................................................................







// ------- f_FlagFieldFocus() -------------------------------------------------------------------------------------------------
function f_FlagFieldFocus(e) { g_blnFieldHasFocus = true; }
// ....... end f_FlagFieldFocus() .............................................................................................







// ------- f_UnFlagFieldFocus() -----------------------------------------------------------------------------------------------
function f_UnFlagFieldFocus(e) { g_blnFieldHasFocus = false; }
// ....... end f_UnFlagFieldFocus() ...........................................................................................







// ------- f_KeyPressHandler() ------------------------------------------------------------------------------------------------
function f_KeyPressHandler(e)
{
	// THIS FUNCTION SIMULATES A PARTICULAR BUTTON CLICK (see 'g_conDefaultIDSfx') WHEN THE USER PRESSES THE 'ENTER' KEY
	
	var intDefaultIDSfxLen = g_conDefaultIDSfx.length;
	
	// accomodate both styles of event handling (cross-browser compatibility)
	var evt = e || window.event;

	if (evt.keyCode == 13)
	{
		var objFieldRef;
		var objHiddenRef;
		var objBtnRef;
		
		// get references to all of the input elements in the document
		var arrInputFields = document.getElementsByTagName('input');
		var intUBound = arrInputFields.length;

		
		// iterate through our input element references, assigning each to an appropriate variable depending upon TYPE and ID
		for (var i=0; i<intUBound; i++)
		{
			switch (arrInputFields[i].type)
			{
				case "submit":
				{
					var intFragLen = arrInputFields[i].id.length;
					
					if (intFragLen > intDefaultIDSfxLen)
					{
						var strIdFrag = arrInputFields[i].id.substr(intFragLen-intDefaultIDSfxLen, intDefaultIDSfxLen);
					
						if (strIdFrag == g_conDefaultIDSfx)	{ objBtnRef = arrInputFields[i]; }
					}
					break;
				}
				
				case "text":
				{
					if (arrInputFields[i].id == g_conInputFieldName) { objFieldRef = arrInputFields[i];	}
					break;
				}
				
				case "hidden":
				{
					if (arrInputFields[i].id == g_conHiddenFieldName) { objHiddenRef = arrInputFields[i]; }
					break;
				}
			}
			
		}
		

		
		// if the input field has been found.......
		if (objFieldRef)
		{
			// if the input field has focus.......
			if (g_blnFieldHasFocus)
			{
				// if the specified submit button has been found.......
				if (objBtnRef)
				{
					
					// -- start IE6 hack (see notes at the top of this script) -----
						// assign the value to a hidden field for submission
						objHiddenRef.value = objFieldRef.value;
						
						//disable the input field
						objFieldRef.disabled = true;
					// -- end IE6 hack -----

					
					// set focus on the specified submit button
					objBtnRef.focus();
					
					// simulate a button click
					objBtnRef.click();
				}
			}
		}
		
		
	}

}
// ....... end f_KeyPressHandler() ............................................................................................

// ------------------------------------------------------ end DEFAULT BUTTON CODE ---------------------------------------------






// =========================================== start MAP POP-UP CODE (author: Matthew Robinson ) ==============================

function f_GetMapLinks()
{
	var arrMapLinks = document.getElementsByTagName("A");
	var intUBound = arrMapLinks.length;
	
	for (var i=0; i<intUBound; i++)
	{
		if (arrMapLinks[i].className == 'map-link') f_AssignMapLinkEvent(arrMapLinks[i]);
	}
}


function f_AssignMapLinkEvent(objRef_IN)
{
	objRef_IN.onclick = null;
	objRef_IN.onclick = function()	{ f_MapPop(objRef_IN); return false;};
}


function f_MapPop(objRef_IN)
{
	var strMapURL = objRef_IN.href;
	
	var intWinHeight;
	if (window.innerHeight) { intWinHeight = window.innerHeight; }
	else { intWinHeight = document.body.offsetHeight; }
	
	var objNewWin = window.open (strMapURL, "ImageSelector", "location=0, status=0, scrollbars=1, " +
								 "resizable=no, height=" + (intWinHeight / 2) + ", width=750");
		objNewWin.moveTo(0, 0);
}
// ------------------------------------------------------ end MAP POP-UP CODE -------------------------------------------------







// ===================== start MODIFIED VERSIONS OF GENERAL CLIENT-SIDE SCRIPTS (author: Matthew Robinson) ===================
function stripeTables()
{
	var arrTables = document.getElementsByTagName('TABLE');
	var intUBound = arrTables.length;
	
	for (var i=0; i<intUBound; i++)
	{
		if (arrTables[i].className == 'striped')
		{
			var arrRows = arrTables[i].getElementsByTagName('tr');
			var intUBound2 = arrRows.length;
			
			for (var ii=0; ii<intUBound2; ii++) { if (ii/2 == Math.round(ii/2))	arrRows[ii].className = 'stripe'; }
		}
	}
}







function initialiseForms()
{
	var arrInputFields = document.getElementsByTagName('input');
	var objTextAreas = document.getElementsByTagName('TEXTAREA');
	var intUBound = arrInputFields.length;
	
	for (var i=0; i<intUBound; i++)
	{
		if (arrInputFields[i])
		{
			if (arrInputFields[i].type == 'text') f_AttachEvents(arrInputFields[i]);
		}
		
		if (objTextAreas[i])
		{
			if (objTextAreas[i].type == 'textarea') f_AttachEvents(objTextAreas[i]);
		}
		
	}
}







function f_AttachEvents(objRef_IN)
{
	if (objRef_IN.value == '') objRef_IN.value = objRef_IN.title;
	
	objRef_IN.onclick = function()
						{
							if (objRef_IN.value == objRef_IN.title) objRef_IN.value = '';
						};
					
	objRef_IN.onblur = function()
						{
							if (objRef_IN.value == '') objRef_IN.value = objRef_IN.title;
						};
}
// ------------------------------------ end MODIFIED VERSIONS OF GENERAL CLIENT-SIDE SCRIPTS ---------------------------------







window.onload = function(){
    stripeTables();
    initialiseForms();
	f_GetMapLinks();
	f_SetDefaultButton();
};

//solves an issue in certain browsers whereby the input field remains disabled if the user clicks the browser's back button)
window.onunload = function(){ f_SetDefaultButton(); }

