var EF_prevElement; // previous element
var EF_prevColor; //prev row colour
var EF_prevClass;
var EF_FieldNameIdLookup = new Array();

function EF_HighLightRow(oRow){  
	
	//if prevElement has been set, reset it to original settings (ie. deselect row)
	if (typeof(EF_prevElement) != 'undefined') {						
		EF_prevElement.style.backgroundColor = EF_prevColor;
	} 
	
	EF_prevColor = oRow.style.backgroundColor;
	EF_prevElement = oRow;

	oRow.style.backgroundColor = '#ffff99';

}

function __EFGetFieldValueFromObject(obj1)
{
	var obj = obj1;
	var tmp;

	if (obj != null && obj.value)
	{
		tmp = parseFloat(obj.value);
		if (! isNaN(tmp))
			return tmp;
		else
			return 0;
	}
	else if (obj != null && obj.options)
	{
		//for option fields
		var count;
		for (count = 0; count < obj.options.length; count++)
		{
			if (obj.options[count].checked)
			{
				tmp = parseFloat(obj.value);
				if (! isNaN(tmp))
					return tmp;
				else
					return 0;
			}
		}
	} 
	else if (obj != null && obj.innerText)
	{
		tmp = parseFloat(obj.innerText);
		if (! isNaN(tmp))
			return tmp;
		else
			return 0;	
	}
	else if (obj != null && obj.innerHTML)
	{
		tmp = parseFloat(obj.innerHTML);
		if (! isNaN(tmp))
			return tmp;
		else
			return 0;
	}
	else
	{
		return 0;
	}

}

//GET a float value from a field.
//if field value contain a non-number, zero is returned
function __EFGetFieldValue(fieldId)
{
	var obj = document.getElementById(fieldId);
	
	if (obj != null)
	{
		return __EFGetFieldValueFromObject(obj);
	}
	else
	{
		return 0;
	}
}

//SET a given value to a form field
function __EFSetFieldValue(fieldId, fieldValue)
{	
	var obj = document.getElementById(fieldId);

	if (obj != null)
	{
		//alert(obj);
		if (obj.value)
		{
			//alert('Value');
			obj.value = fieldValue;
		}
		else if(obj.innerText || obj.innerText == '')
		{
			obj.innerText = fieldValue;
		}
		else if (obj.innerHTML)
		{
			//alert('HTML ' + fieldValue);
			obj.innerHTML = fieldValue;
		}
		else
		{
			//alert('Text ' + fieldValue);
			obj.innerText = fieldValue;
		}
	}
}

function __EFGetElementFromCache(id)
{
	//determines if already stored in lookup cache
	var cachedId = EF_FieldNameIdLookup[id];
	var obj = null;
	
	if (cachedId != null && cachedId != '')	
	{
		//load get from id
		obj = document.getElementById(cachedId);
		
		if (obj != null)
			return obj;
		else
			return null;
	}

	return null;

}

function __EFGetElementByIDByTag(id, tag)
{
	//load from cache first
	var obj = __EFGetElementFromCache(id);
	
	if (obj != null)
		return obj;

	//try all input boxes first
	var aInputs = document.getElementsByTagName(tag);
	
	//alert(id + " " + tag + ' ' + aInputs.length);
	
	for(index = 0; index < aInputs.length; index++)
	{
	
		if (aInputs[index].id)
		{
			//alert(aInputs[index].id);
			if (aInputs[index].id.indexOf(id) > -1)
			{
				//alert('FOUND');
				//set to cache
				EF_FieldNameIdLookup[id] = aInputs[index].id;
				
				return aInputs[index];
			}
		}
	}

	return null;
}

function __EFGetElementByID(id)
{
	//try all input boxes first
	var obj;
	
	
	obj = __EFGetElementByIDByTag(id, "INPUT");
	
	if (obj != null)
		return obj;

	obj = __EFGetElementByIDByTag(id, "SELECT");
	
	if (obj != null)
		return obj;

	obj = __EFGetElementByIDByTag(id, "TEXTAREA");
	
	if (obj != null)
		return obj;
		
	obj = __EFGetElementByIDByTag(id, "SPAN");
	
	if (obj != null)
		return obj;


	obj = __EFGetElementByIDByTag(id, "DIV");
	
	if (obj != null)
		return obj;

	return null;
}


function __EFGetElementByEFFieldNameByTag(fieldname, tag)
{

	//load from cache first
	var obj = __EFGetElementFromCache(fieldname);
	
	if (obj != null)
		return obj;
		
		
	//try all input boxes first
	var aInputs = document.getElementsByTagName(tag);
	
	//alert(fieldname + " " + tag + ' ' + aInputs.length);
	
	for(index = 0; index < aInputs.length; index++)
	{
		//alert(aInputs[index].getAttribute("effieldname") + ' = ' + fieldname);
		
		if (aInputs[index].getAttribute("effieldname") != null)
		{
			if (aInputs[index].getAttribute("effieldname") == fieldname)
			{
				//alert('FOUND');
				//set to cache
				EF_FieldNameIdLookup[fieldname] = aInputs[index].id;

				return aInputs[index];
			}
		}
	}

	return null;
}


function __EFGetElementByEFFieldName(fieldname)
{
	//try all input boxes first
	var obj;
	
	//alert(fieldname);

	obj = __EFGetElementByEFFieldNameByTag(fieldname, "INPUT");
	
	if (obj != null)
		return obj;
		
		
	obj = __EFGetElementByEFFieldNameByTag(fieldname, "SPAN");
	
	if (obj != null)
		return obj;

	obj = __EFGetElementByEFFieldNameByTag(fieldname, "SELECT");
	
	if (obj != null)
		return obj;

	obj = __EFGetElementByEFFieldNameByTag(fieldname, "TEXTAREA");
	
	if (obj != null)
		return obj;

	obj = __EFGetElementByEFFieldNameByTag(fieldname, "DIV");
	
	if (obj != null)
		return obj;

	return null;
}

function __EFRedirect(url)
{
	//alert('window open ' + url);
	window.open(url, "_self");
	return true;
}

//the two functions below obtained from 
// http://www.webreference.com/dhtml/diner/realpos_old/realpos4.html

function __EFGetRealLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function __EFGetRealTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

