/*************************************************
	This file contains core javascript functionality.
**************************************************/

addListener(Event.LOAD, window, initialiseFields);
addListener(Event.MOUSE_DOWN, document, handleMouseDown);

/*method to be called on page load event to initialize various fields*/
function InitPage()
{
		if (document.getElementById("View_txtLocation")!=null)
		{
				if (document.getElementById("View_txtLocation").value=='')
				{
					document.getElementById("View_txtLocation").value='Enter town or postcode here';
				}
		}
}

/*sets a location textbox's display text*/
function setLocationText(control,action)
{
	sString=control.value;
	if (action=='FILL')
	{
		if (trimAll(sString)=='')
		{
			setControlText(control,'Enter town or postcode here');
		}
	}
	else if (action=='CLEAR')
	{
		if (sString=='Enter town or postcode here')
		{
			setControlText(control,'');
		}
	}
}

/*removes white spaces from a string*/
function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}	

/*sets a controls display text*/
function setControlText(control,content)
{
	control.value=content;
}

/*handles mouse click events*/
function handleMouseDown(evt)
{
	mouseDownFrame(top, evt);
}

/*handles mouse down for the given frame*/
function mouseDownFrame(parentFrame, evt)
{
	try
	{
		parentFrame.closeDatePicker(evt);
	}
	catch(e)
	{
	}
	for (var i = 0; i < parentFrame.frames.length; i++)
	{
		mouseDownFrame(parentFrame.frames[i], evt);
	}
}

/*Method used by custom controls so that they work with browser back navigation. 
Initialises fields, potentially changed using javascript, to their initial values.
Initialises classes gives controls the chance to set class names dynamically, mainly
so that certain elements can be set visible when javascript is on.
Sets focus to the focus field element if there is one.
If the menu has been hidden display the show menu image*/
var fieldsInitialised = false;
function initialiseFields(){
	if (fieldsInitialised) return;
	var initLength = 0;
	try
	{
		initLength = FieldInitialiser.length;
	}
	catch(e)
	{
		//no field initialiser present
	}
	for (i = 0; i < initLength; i++)
	{
		document.getElementById(FieldInitialiser[i][0]).value = FieldInitialiser[i][1];
	}
	initLength = 0;
	try
	{
		initLength = ClassInitialiser.length;
	}
	catch(e)
	{
		//no class initialiser present
	}
	for (i = 0; i < initLength; i++)
	{
		document.getElementById(ClassInitialiser[i][0]).className = ClassInitialiser[i][1];
	}
	try
	{
		document.getElementById(focusField).focus();
	}
	catch(e)
	{
		//no field to request focus so just set focus to itself
		self.focus();
	}
	fieldsInitialised = true;
}

//sets focus to the given field
function requestFocus(fieldId)
{
	document.getElementById(fieldId).focus();
}

/*Check to see if the form has already been submitted. If so return false,
otherwise increment the count and return true*/
function validateSubmit()
{
	if (!runJavaScript) return true;
	if (submitCount == 0)
	{
		submitCount++;
		return true;
	}
	else
	{
		return false;
	}	
}

/* method used by custom controls to keep track of changes made on the client.
Adds addition to current if not already present and removes it otherwise */
function CombineStrings(current, addition)
{
	if (current == '')
	{
		current = addition;
	}
	else
	{
		var additionPresent = false;
		var newList = new Array();
		var currentList = current.split(',');
		for (var i = 0; i < currentList.length; i++)
		{
			if (currentList[i] != addition)
			{
				newList.push(currentList[i]);
			}
			else
			{
				additionPresent = true;
			}
		}
		if (!additionPresent)
		{
			newList.push(addition);
		}
		current = newList.join(',');
	}
	return current;
}

function findByClass(el, className)
{
	var i, tmp;
	if (el.className == className)
		return el;

	for (i = 0; i < el.childNodes.length; i++) 
	{
		tmp = findByClass(el.childNodes[i], className);
		if (tmp != null)
			return tmp;
	}
	return null;
}

/* removes a class name from a given element's space delimited class name list*/
function removeClassName(el, name) 
{
	var i, curList, newList;  
	if (el.className == null)
	{
		return;
	}
	newList = new Array();
	curList = el.className.split(" ");
	for (i = 0; i < curList.length; i++)
	{
		if (curList[i] != name)
		{
			newList.push(curList[i]);
		}
	}
	el.className = newList.join(" ");
}

/* checks for the presence of a class name from a given element's space delimited 
class name list*/
function hasClassName(el, name) 
{
	var i, list;
	list = el.className.split(" ");
	for (i = 0; i < list.length; i++)
	{
		if (list[i] == name)
		{
			return true;
		}
	}
	return false;
}

//hides messages - controls with javascript need to behave the same when scripting
//is off and on, creating the need for any messages to be hidden
function hideMessages()
{
	try
	{
		document.getElementById(messageHolder).style.display = 'none';
	}
	catch(e)
	{
		//do nothing as no messages to hide
	}
}

//returns the real top position of an element
function getTrueTop(el) 
{
    
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) 
    {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

//returns the real left position of an element
function getTrueLeft(el) 
{
    
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) 
    {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}