/*********************************************************
'
'   common.js
'
'	© Copyright 2008 James C. Augustine
'
'   Creation Date:  04/2008
'   Author:         James C. Augustine
'   Description:
'       
'
'   Variable Name Conventions:
'       arr --> Array
'       int --> Integer
'       str --> String
'       lng --> Long
'       bol --> Boolean
'       obj --> Objects
'       counters and constants do not have prefixes
'
'************************************************************/

/**
 * this function is used to handle multiple onload events in one page
 */

function AddWindowOnLoadEvent(f){

	//DOM method for binding an event
	if (window.addEventListener) {
		window.addEventListener("load", f, false)
	}
	//IE exclusive method for binding an event
	else if (window.attachEvent) {
		window.attachEvent("onload", f)
	}
}




/**
 * these functions disable the yellow highlighting that the google 
 * toolbar does as part of the autofill feature
 *
 * note that i reserve background color #ddd for when i want to disable
 * a control and not have this code undo it
 *
 * taken from: http://code.jenseng.com/google/ 
 *
 */
function SetGoogleToolBarAutoFillListeners()
{
	inputList = document.getElementsByTagName("INPUT");

	for(i=0;i<inputList.length;i++)
	{
		if (typeof inputList[i].attachEvent == 'function') { 
			inputList[i].attachEvent("onpropertychange", RevertGoogleToolBarAutoFillStyles);
			if ( inputList[i].style.backgroundColor != "" && inputList[i].style.backgroundColor != "#ddd")
				inputList[i].style.backgroundColor = "";
		}
	}

	selectList = document.getElementsByTagName("SELECT");

	for (i=0;i<selectList.length;i++)
	{
		if (typeof selectList[i].attachEvent == 'function') { 
			selectList[i].attachEvent("onpropertychange", RevertGoogleToolBarAutoFillStyles);
			if ( selectList[i].style.backgroundColor != "" && selectList[i].style.backgroundColor != "#ddd")
				selectList[i].style.backgroundColor = "";
		}
	}
}

function RevertGoogleToolBarAutoFillStyles()
{
	if ( event.srcElement.style.backgroundColor != "" && event.srcElement.style.backgroundColor != "#ddd")
		event.srcElement.style.backgroundColor = "";
}

AddWindowOnLoadEvent( SetGoogleToolBarAutoFillListeners );





/**
 * this is called when a data struct action has the popup attribute set
 *
 */
function DataStructActionOpenPopup(HtmlFile, WindowName, InitialWidth, InitialHeight) 
{
	if (null == InitialWidth)
		Width = 350;
	else
		Width = InitialWidth;
		
	if (null == InitialHeight)
		Height = screen.height / 2;
	else
		Height = InitialHeight;
	
	window.open(HtmlFile, WindowName, "resizable=yes,status=no,scrollbars=yes,width=" + Width + ",height=" + Height + ",left=" + ((screen.width / 2) - (Width / 2)) + ",top=" + ((screen.height / 2 ) - (Height / 2)) + "");
}


function formatInputNumberNoDecimal(field)
{
	var strValue = '';

	if ( field && field.value )	{
		strValue = field.value;
		
		// remove invalid chars
		strValue = strValue.replace(/\$|\,|\ |\-|[A-Za-z]+/g, '');
		
		// if only invalid chars
		if (0 == strValue.length) {
			strValue = '0';
		}
		
		dblValue = parseFloat(strValue);
		
		if (isNaN(dblValue)) {
			dblValue = 0;
		}
		
		dblValue = Math.round(dblValue).toString();

		for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++) {
			dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+dblValue.substring(dblValue.length-(4*i+3));
		}
		
		field.value = dblValue;
	}
}

function formatSignedInputNumberNoDecimal(field)
{
	var strValue = '';

	if ( field && field.value )	{
		strValue = field.value;
		
		// remove invalid chars
		strValue = strValue.replace(/\$|\,|\ |[A-Za-z]+/g, '');
		
		// if only invalid chars
		if (0 == strValue.length) {
			strValue = '0';
		}
		
		dblValue = parseFloat(strValue);
		
		if (isNaN(dblValue)) {
			dblValue = 0;
		}
		
		dblValue = Math.round(dblValue).toString();

		for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++) {
			dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+dblValue.substring(dblValue.length-(4*i+3));
		}
		
		field.value = dblValue;
	}
}
function parseInputNumberNoDecimal(value) {

	if ( ! value )	{
		return null;
	}
	else {
		value = value.toString();
		return parseFloat(value.replace(/\$|\,|\ |\-/g, ''));
	}
	
}

function parseSignedInputNumberNoDecimal(value) {

	if ( ! value )	{
		return null;
	}
	else {
		value = value.toString();
		return parseFloat(value.replace(/\$|\,|\ |/g, ''));
	}
	
}

function getElementLeft(elemName) {
	if(document.getElementById) {
		elem = document.getElementById(elemName);
	} else if (document.all){
		elem = document.all[elemName];
	}
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
	}
	return xPos;
}


function getElementTop(elemName) {
	if(document.getElementById) {	
		elem = document.getElementById(elemName);
	} else if (document.all) {
		elem = document.all[elemName];
	}
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
	}
	return yPos;
}



