//
// Web Content Javascript Support
// by Joel Kozikowski
// Unless otherwise noted, all code 
// (C) 2007 Lighthouse Practice Management Group
// All Rights Reserved.
//

/**
 * Returns an object with "top" and "left" properties that represent the coordinates
 * of the element relative to the browser window.
 */
function lpmg_getWindowCoordinates(element) {

	var curleft = curtop = 0;
	if (element.offsetParent) {
	    var obj = element;
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	var results = new Object();
	results.top = curtop;
	results.left = curleft;
	return results;
}

/**
 * Returns the first child of parentElement that has a class name of className
 */
function lpmg_getFirstElementByClass(parentElement, className) {
    for (var i = 0; i < parentElement.childNodes.length; i++){
        var child = parentElement.childNodes[i];
        if (child.nodeType == 1 && child.className == className) {
            return child;
        }
    } // for
    return null;
}


/**
 * Returns the first child of parentElement that has a class name that STARTS WITH className
 */
function lpmg_getFirstElementByClass2(parentElement, className) {
    for (var i = 0; i < parentElement.childNodes.length; i++){
        var child = parentElement.childNodes[i];
        if (child.nodeType == 1 && child.className.startsWith(className)) {
            return child;
        }
    } // for
    return null;
}


/**
 * Returns the first ancestor element of childElement that has a class name of className
 */
function lpmg_getFirstAncestorElementByClass(childElement, className) {
	if (childElement.parentNode) {
	    var obj = childElement;
		while (obj = obj.parentNode) {
            if (obj.nodeType == 1 && obj.className == className) {
                return obj;
            }
		} // while
	}
	return null;
}

/**
 * Returns the first ancestor element of childElement that is of the specified type
 * (i.e. is an element named tagName).
 */
function lpmg_getFirstAncestorElementByTagName(childElement, tagName) {
	if (childElement.parentNode) {
	    var obj = childElement;
		while (obj = obj.parentNode) {
            if (obj.nodeType == 1 && obj.name == tagName) {
                return obj;
            }
		} // while
	}
	return null;
}


/**
 * Returns the first ancestor element of childElement that is an element of the specified tagName
 */
function lpmg_getFirstAncestorElementByTagName(childElement, tagName) {
	if (childElement.parentNode) {
	    var obj = childElement;
		while (obj = obj.parentNode) {
            if (obj.nodeType == 1 && obj.nodeName.toLowerCase() == tagName) {
                return obj;
            }
		} // while
	}
	return null;
}



/**
 * Returns all child elements of oElm that are of type "strTagName" and have
 * a class of "strClassName".
 * Code found on the web at http://www.robertnyman.com/index.php?p=256
 * Written by Jonathan Snook, http://www.snook.ca/jonathan
 * Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function lpmg_getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}


/**
 * Alternate implementation that finds the class if it STARTS WITH the specified
 * class name.
*/

function lpmg_getElementsByClassName2(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oElement.className.startsWith(strClassName)) {
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}


/**
 * Code that adds a "push" function to arrays (which is not available in IE).
 */
if (typeof Array.prototype.push != "function") {
	Array.prototype.push = ArrayPush;
	function ArrayPush(value){
		this[this.length] = value;
	}
}
 

function lpmg_getJSONData(parentDiv, className) {
   var dataNode = lpmg_getFirstElementByClass(parentDiv, className);
   var jsonData = dataNode.innerHTML;
   var apptData = eval('(' + jsonData + ')');
   return apptData;
}

  

if (!String.prototype.startsWith)
{
  String.prototype.startsWith = function(startStr)
  {
    if (typeof startStr == 'undefined' ||
        startStr == null) {
        return false;
    }
    
    var len = startStr.length;
    
    return (this.substr(0, len) == startStr);
  }
}

var lpmg_image_editor = null;

/**
 *  Remembers which editor opened up the image selection button.
 */
function lpmg_setImageEditor(button) {
    lpmg_image_editor = button;
}

/**
 * Sets the "image Id" of the specified image editor to "newImageId". It is assumed that
 * lpmg_setImageEditor() was called with the button DOM element of the editor that opened
 * the image selector.
 */
function lpmg_setImageEditorImageId(newImageId) {
   var imgSelector = lpmg_getFirstAncestorElementByClass(lpmg_image_editor, "editor_image_select");
   var imgIdInput = imgSelector.getElementsByTagName("input")[0];
   imgIdInput.value = newImageId;
   imgIdInput.form.submit();
}

