/*
	Created by: Greg Hacic
	Last Update: December 04, 2007
	Questions?: greg@interactiveties.com
	(c) Interactive Ties LLC
*/
//script to highlight the rows in the data table
function addStyleClass(elem, clazzName) {
	var n = " "+elem.className+" ";
	if (n.indexOf(" "+clazzName+" ") < 0) {
		var append = (elem.className.length > 0) ? " "+clazzName : clazzName;
		elem.className += append;
	}
}

function delStyleClass(elem, clazzName) {
	var n = " "+elem.className+" ";
	var len = clazzName.length;
	var start = n.indexOf(" "+clazzName+" ");
	if (start >= 0) {
		var end = start + len + 1;
		elem.className = trim(n.substring(0,start) + n.substring(end,n.length));
		return;
	}
}
function hiOn(row) {
	addStyleClass(row,"highlight");
}
function hiOff(row) {
	delStyleClass(row,"highlight");
}
function trim(st) {
	var len = st.length
	var begin = 0, end = len - 1;
	while (st.charAt(begin) == " " && begin < len) {
		begin++;
	}
	while (st.charAt(end) == " " && begin < end) {
		end--;
	}
	return st.substring(begin, end+1);
}

var lastMouseX;
var lastMouseY;
var curPopupWindow = null;
var closeOnParentUnloadWindow = null;
var helpWindow = null;
var win = null;

var beenFocused = false;
document.onmousedown = markFocused;
function markFocused() {
    beenFocused = true;
}

/**
 * Track the element with focus.
 */
var focusedElement = null;
if (document.addEventListener) {
    document.addEventListener('focus', trackFocused, false);
} else if (document.attachEvent) {
    document.attachEvent('onFocus', trackFocused);
}
function trackFocused(e) {
    if (window.event) {
        focusedElement = window.event.target;
    } else {
        focusedElement = e.target;
    }
}

function setLastMousePosition(e) {
    if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
    lastMouseX = e.screenX;
    lastMouseY = e.screenY;
}
/**
 * Handles popup windows.
 * If snapToLastMousePosition is true, then the popup will open up near the mouse click.
 * If closeOnLoseFocus is true, then it will close when the user clicks back into the browser window that opened it.
 */
function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus, closeOnParentUnload) {
    closePopup();

    if (snapToLastMousePosition) {
        if (lastMouseX - pWidth < 0) {
            lastMouseX = pWidth;
        }
        if (lastMouseY + pHeight > screen.height) {
            lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
        }
        lastMouseX -= pWidth;
        lastMouseY += 10;
        features += ",screenX=" + lastMouseX + ",left=" + lastMouseX + ",screenY=" + lastMouseY + ",top=" + lastMouseY;
    }

    if (closeOnLoseFocus) {
        curPopupWindow = window.open(url, name, features, false);
        curPopupWindow.focus ();
    } else {
        // assign the open window to a dummy var so when closePopup() is called it won't be assigned to curPopupWindow
        win = window.open(url, name, features, false);
        win.focus ();
    }

    if (closeOnParentUnload) {
        closeOnParentUnloadWindow = win;
    }
}

function closePopup() {
    if (curPopupWindow != null) {

        try {
            curPopupWindow.close();
        } catch(ex) {
            // This Exception code is to deal with IE issues checking
            // The window's closed property
        }
        curPopupWindow = null;
    }
}