var EF_prevElement; // previous element
var EF_prevColor; //prev row colour
var EF_prevClass;
var EF_FieldNameIdLookup = new Array();
var __focusObject;

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;

	//radio/checkbox list element
	obj =__EFGetElementByIDByTag(id, "TABLE");
	
	if (obj != null)
	{
		EF_FieldNameIdLookup[id] = '';
		return __EFLoadListOptions(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)
			{
				//set to cache
				EF_FieldNameIdLookup[fieldname] = aInputs[index].id;

				return aInputs[index];
			}
		}
		else
		{
			var parentNode = aInputs[index].parentNode;
					
			if (parentNode != null && typeof(parentNode.getAttribute) != "undefined")
			{
				if (parentNode.getAttribute("effieldname") != null && parentNode.getAttribute("effieldname") == fieldname)
				{
					//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;

	//radio/checkbox list element
	obj = __EFGetElementByEFFieldNameByTag(fieldname, "TABLE");
	
	if (obj != null)
	{
		//reset cache
		EF_FieldNameIdLookup[fieldname] = '';
		return __EFLoadListOptions(obj);
	}

	return null;
}

function __EFRedirect(url)
{
	location.href = url;
	return true;
}

//function to compare values of two fields
//if matched returns true else display errMsg value and returns false
function __EFCompareValues(efFielA, efFieldB, errMsg)
{
	var objA = __EFGetElementByEFFieldName(efFielA);
	var objB = __EFGetElementByEFFieldName(efFieldB);
	
	//exit if one of the two fields do not exists
	if (objA == null || objB == null)
	{
		return true;
	}
	
	if (objA.value == objB.value)
	{
		return true;
	}
	else
	{
		__EFAlert(errMsg);
		return false;
	}
}

//displays message if not empty
function __EFAlert(message)
{
	if (message != null && message != '')
		alert(message);
}

//load objects from checkbox list and radio lists
function __EFLoadListOptions(obj)
{
	var list = new Array();  //stores array of list options
	var count = 0; //counter
	
	if (obj == null)
		return obj;
	
	while(true)
	{
		var listObj = document.getElementById(obj.id + '_' + count);
		if (listObj != null)
		{
			list[count] = listObj;
			count++;
		}
		else
		{
			break;
		}
	}
	
	return list;	
}


//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;
}

function __EFFocusDelay(fieldId)
{
    //alert('focusDelay Called');
    __focusObject = document.getElementById(fieldId);
    if (__focusObject != null)
        window.setTimeout("__focusObject.focus()", 700);
}