// JavaScript Utilities File

var objUtils = {
    BuildDropDownOptionsFromXML: function (ElementID, DropDownOptionsXML) 
    {

    /*
    This function takes an XML document in which the drop down options are 
    are listed within XML tags, and creates the drop down options.
    <DropDownOptions>
        <DropDownOption>
            <Text>Option1 Text</Text>
            <Value>Option1 Value</Value>
        </DropDownOption>
        <DropDownOption>
            <Text>Option2 Text</Text>
            <Value>Option2 Value</Value>
        </DropDownOption>
    </DropDownOptions>
    NOTE: Because of the way this function is written, the actual names of 
    tags are not important. The structure, where the individual options tags are listed 
    or nested under a top level container tag, is important.

    1. First step is to get the  <DropDownOption> nodes within the
    <DropDownOptions> tag.
    This is achieved simply by defining an "DropDownOptions" array and setting
    it as follows:
    var DropDownOptions = objXMLReq.responseXML.getElementsByTagName("DropDownOption");
    Note the "SecondList" argument - not "DropDownOptions" since we are interested
    in the "DropDownOption' nodes in the XML tree.

    Next, to access the tags, within a "DropDownOption" tag, use:
    var text = DropDownOptions[i].firstChild.nodeValue;
    Hence, we use the .firstChild.nodeValue to get the value.
    */

                var DropDownOptionsContainer = DropDownOptionsXML;
                // Now place all items in the drop down list:
                // Get the DropDownList element and assign to variable
                var ddl = document.getElementById(ElementID);
               
                // loop through <DropDownOption> elements, and add to ddl  element
                var opt;
                /* Essentials of adding an option element
                opt = document.createElement("option");
                opt.value = i;
                opt.text = i
                ddl.appendChild(opt);
                */

                // Remove existing option elements
                ddl.innerHTML = "";
                if (DropDownOptionsContainer) {
                    
                    /*
                    DropDownOptionsContainer.childNodes[0] gives the container XML tag: <DropDownOptions>
                    DropDownOptionsContainer.childNodes[0].childNodes[i] gives the i-th option
                    tag
                    */
                    //alert("# of nodes = " + DropDownOptionsContainer.childNodes[0].childNodes.length);
                    DropDownOptions = DropDownOptionsContainer.childNodes[0]; 
                    // or getElementsByTagName("DropDownOption");
                    
                    for (var i=0;i<DropDownOptions.childNodes.length;i++) {
                        opt = document.createElement("option");
                     
                        // Note: to get access to the text within a tag, you 
                        // need to access it as .firstChild
                        // DropDownOptions.childNodes[i] = i-th <DropDownOption> tag
                        opt.text = DropDownOptions.childNodes[i].childNodes[0].firstChild.nodeValue;
                        opt.value = DropDownOptions.childNodes[i].childNodes[1].firstChild.nodeValue;
                       
                        //ddl.appendChild(opt);  // works in Firefox but not IE
                                                // hence, use the "add" method instead
                        try {
                            //ddl.appendChild(opt)
                            ddl.add(opt,null); // standards compliant; doesn't work in IE
                        } catch (ex) {
                            ddl.add(opt); // IE only
                        }
                    }
                }
    }, // end of BuildDropDownOptionsFromXML
    
    getHtmlElementPositionLeft: function(HtmlElement) {
        // This function gets left position of the HTML element (using the offsetLeft property
        // and recursively going up the HtmlElementect tree using the offsetTop property
	    var curLeft = 0;
	    if (HtmlElement.offsetParent) {
		    do {
			    curLeft += HtmlElement.offsetLeft;
		    } while (HtmlElement = HtmlElement.offsetParent);
	    }
	    else if (HtmlElement.x) {
		    curLeft += HtmlElement.x;
	    }
	    return curLeft;	    
    }, // end of getElementPositionLeft
    
    getHtmlElementPositionTop: function (HtmlElement) {
        // This function gets top position of the target element (using the offsetTop property
        // and recursively going up the HtmlElementect tree using the offsetTop property
        var curTop = 0;
        if (HtmlElement.offsetParent) {
            do {
	            curTop += HtmlElement.offsetTop;
            } while (HtmlElement = HtmlElement.offsetParent);
        }
        else if (HtmlElement.y) {
            curTop += HtmlElement.y;
        }	
        return curTop;    
    }, // end of getHtmlElementPositionTop
    
    addEvent: function(elm, evType, fn, useCapture) {
    // elm: Element that triggers event, eg. window
    // evType: Event type such as click, etc.
    // fn: function that gets called when event is triggered
    
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } // end of addEvent function 
    
    
} // end of objUtils

















