// JavaScript Document
<!--
    var alreadySubmitted = false;
    /**
     * This method is used to change the submit action of
     * the form.
     */
    function changeSubmitAction(formId, action)
    {
        
        var form = document.getElementById(formId);
        // verify that the form has not been submitted.
        if (alreadySubmitted == true) {
              window.status="Submitted";
              return;
        }

        // else set the submitted indicator
       alreadySubmitted = true;

        // reset the submitted indicator after 10 seconds.
       setTimeout('alreadySubmitted = false;window.status="Unlocked";', (1000));

        form.action = action;
        
        return true;
    }

    /**
     * This method is used to submit form.
     */
    function submitForm(formId, action)
    {
        var form = document.getElementById(formId);
        // verify that the form has not been submitted.
        if (alreadySubmitted == true) {
              window.status="Submitted";
              return;
        }

        // else set the submitted indicator
       alreadySubmitted = true;

        // reset the submitted indicator after 10 seconds.
       setTimeout('alreadySubmitted = false;window.status="Unlocked";', (1000));

        form.action = action;

        // submit form
        form.submit();
        
        return false;
    }


    function freshSearch(formId, action)
    {
        var form = document.getElementById(formId);
 
        form.uniqueSortProperty.value = "";

        return submitForm(formId, action);
    }

    function changePage(formId, action, offset)
    {
        var form = document.getElementById(formId);
 
        form.pageOffset.value = offset;

        return submitForm(formId, action);
    }

    function sortByProperty(formId, action, property)
    {
        var form = document.getElementById(formId);

        // switch order direction if same property

        if (form.uniqueSortProperty.value == property) {
            if (form.uniqueSortPropertyOrder.value == '0') {
                form.uniqueSortPropertyOrder.value = 1;
            } else {
                form.uniqueSortPropertyOrder.value = 0;
            }
        } else {
            form.uniqueSortPropertyOrder.value = 0;
        }
 
        form.uniqueSortProperty.value = property;
        


        return submitForm(formId, action);
    }
    
  /** 
   *  This is used to display the menu in IE...otherwise the dynamic menu does not work in IE
   */
   startList = function() {
   if (document.all && document.getElementById) {
       navRoot = document.getElementById("content-head-navigation");
       if(navRoot && navRoot.childNodes) {
	       for (i=0; i<navRoot.childNodes.length; i++) {
	           node = navRoot.childNodes[i];
	           if (node.nodeName=="LI") {
	               node.onmouseover=function() {
	                   this.className+=" over";
	                   }
	               node.onmouseout=function() {
	                   this.className=this.className.replace
	                   (" over", "");
	                   }
	               }
	           }
	       }
	   }
   }
   window.onload=startList; 

    /**
     * Change css class
     */
    function changeCssClass(id, newClass) {
        identity=document.getElementById(id);
        identity.className=newClass;
    }
    
    function changeCssAndRefresh(id, newClass, formId, action) {
        submitForm(formId, action);
        changeCssClass(id, newClass);
    }

    /**
     * Open a popup
     */
    function popUp(id, URL) {
        window.open(URL, id, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,width=500,height=300');
        return false;
    }
    
    /**
     * Open selection page in popup
     */
    function goToSelectionPopup(url, formId, formElement, searchParameter) {
        var form = document.getElementById(formId);
        var searchEntry = form.elements[formElement].value;
        return popUp(formElement, url + '?' + searchParameter + '=' + searchEntry);
    }
    
    /**
     * This method is used to update the parent form with the selection made in the current page
     */
    function updateSelectionAndExit(currentForm, selection, parentForm, parentFormSearchEntry) {
        var currentForm = document.getElementById(currentForm);
        var parentForm = window.opener.document.getElementById(parentForm);
        var citySelectObject = currentForm.elements[selection];
        var citySelectObjectText = citySelectObject.options[citySelectObject.options.selectedIndex].text
        parentForm.elements[selection].value = citySelectObject.value;
        parentForm.elements[parentFormSearchEntry].value = citySelectObjectText;
        window.close();
    }


    /**
     * Nullifys an element of the form that are passed as parameters
     */
    function nullifyFormElement(formId, elementId, e) {
        if(!e && document.all)e = event;
        if(e && e.keyCode != 9 && e.keyCode != 13) {
            var form = document.getElementById(formId);
            form.elements[elementId].value='';
        }
    }
    /**
     * Flags the window as loaded in the status flag
     */
    function flagWindowLoaded() {
        window.statusFlag = 'windowLoaded';
        return true;
    }
    
    /**
     * Flags the window as not loaded in the status flag
     */
    function flagWindowNotLoaded() {
        window.statusFlag = 'windowNotLoaded';
        return true;
    }
    
        /**
     * This method is used to update the parent form with the selection made in the current page, 
     * to submit the parent form with a selected action and to close
     */
    function updateSelectionReloadParentAndExit(currentFormDescriptor, selection, parentFormDescriptor, parentFormSearchEntry, submitAction) {
        var currentForm = document.getElementById(currentFormDescriptor);
        var parentForm = window.opener.document.getElementById(parentFormDescriptor);
        var citySelectObject = currentForm.elements[selection];
        var citySelectObjectText = citySelectObject.options[citySelectObject.options.selectedIndex].text
        parentForm.elements[selection].value = citySelectObject.value;
        parentForm.elements[parentFormSearchEntry].value = citySelectObjectText;
        submitParentForm(parentForm, submitAction);
        window.close();
    }
    
    /**
     * This method is used to submit form.
     */
    function submitParentForm(form, action) {
        if (alreadySubmitted == true) {
              window.status="Submitted";
              return;
        }
        alreadySubmitted = true;
        setTimeout('alreadySubmitted = false;window.status="Unlocked";', (1000*10));
        form.action = action;
        form.submit();
        return false;
    }
    
    
    /**
     * Opens a popup to open pdf and then redirects action
     */    
    function printPDFAndChangeSubmitAction(popUpUrl, elementId, formId, action) {
        var form = document.getElementById(formId);
        var id = form.elements[elementId].value;
    	popUp("releasePdf", popUpUrl + "?id=" + id + "&extension=pdf");
    	return changeSubmitAction(formId, action);
    }
    
    
    /** 
     * Adds an event to an event listener
     */
    function addEvent(elem, evType, func, useCapture) {
        if( elem.addEventListener ) {
            elem.addEventListener(evType, func, useCapture);
            return true;
        } else if( elem.attachEvent ) {
            var r = elem.attachEvent("on" + evType, func); return r;
        } else {
            var onEvt = "on" + evType;
            var elOldEvFuncs = eval("elem." + onEvt);
            if( (typeof elem[onEvt]) != 'function' ) {
                elem[onEvt] = func; }
            else {
                elem[onEvt] = function() {
                    elOldEvFuncs();
                    func();
                }
            }
        }
    }


	/**
	 * Get the xml fragment from a requestobject
	 */
    function getXMLFragment(requestObject) {
    	var returnedXMLDocumentFragment = null;
    	//Not IE 
		if (document.implementation && document.implementation.createDocument){
	        var returnedDataislandXMLDocument = requestObject.responseXML;		
		} 
		//IE
		else if (window.ActiveXObject) {
			var returnedDataislandXMLDocument = new ActiveXObject("MSXML.DomDocument");
      		returnedDataislandXMLDocument.loadXML(requestObject.responseText); 
		}

        if(returnedDataislandXMLDocument.firstChild && returnedDataislandXMLDocument.firstChild.nodeName == "xml") {
            returnedXMLDocumentFragment = returnedDataislandXMLDocument.childNodes[1];
        } else if(returnedDataislandXMLDocument.firstChild) {
            returnedXMLDocumentFragment = returnedDataislandXMLDocument.firstChild;
        } else {
            returnedXMLDocumentFragment = returnedDataislandXMLDocument;
        }
        
        return returnedXMLDocumentFragment;
    }
    
    function changeCssClassAndFocus(id, newClass) {
        identity=document.getElementById(id);
        if(identity) {
            identity.className=newClass;
        }
    }
    
    function nullifyElement(elementId, e) {
        if(!e && document.all) {
            e = event;
        }
        if(e && e.keyCode != 9 && e.keyCode != 13) {
            document.getElementById(elementId).value = null;
        }
    }

    function findChild(childList, tagName) {
        for(var i = 0; i < childList.length; i++) {
            var child = childList[i];
            if(tagName && child.tagName) {
                if(child.tagName.toLowerCase() == tagName.toLowerCase()) {
                    return child;
                } 
            }
        }
        return null;
    }

    function setCheckedValue(radioObj, newValue) {
        if(!radioObj)
            return;
        var radioLength = radioObj.length;
        if(radioLength == undefined) {
            radioObj.checked = (radioObj.value == newValue.toString());
            return;
        }
        for(var i = 0; i < radioLength; i++) {
            radioObj[i].checked = false;
            if(radioObj[i].value == newValue.toString()) {
                radioObj[i].checked = true;
            }
        }
    }
    
    function notifyOfSimilarNames(elementId, relativeActionUrl, pathPrefix) {
        var subName = $(elementId).value;
        if(subName.length >= 5) {
            subName = subName.substring(0, 5);

            var AJAXRequest = new Ajax.Request(
                pathPrefix + relativeActionUrl + "?name=" + subName,
                {
                    method: "get",
                    requestHeaders: ['cache-control','no-cache','pragma','no-cache', 'content-type', 'application/xml'],
                    onSuccess: function(requestObject) {
                        try {
                            MdaradMessageHandler.removeAllMessages("content-body-messages");
                            var returnedXMLDocumentFragment = MdaradUtils.getXMLFragment(requestObject, pathPrefix);
                            if(returnedXMLDocumentFragment && returnedXMLDocumentFragment.tagName == 'Message') {
                                displayErrorMessageForNameMatchingRetrieval(xmlDocument.firstChild.nodeValue);
                            } else {
                                var resultsNode = MdaradUtils.findChild(returnedXMLDocumentFragment.childNodes, 'results');                             
                                var entityNodes = resultsNode.childNodes;
                                if(entityNodes && entityNodes.length > 0) {
                                    var message = "Entities with similar name: ";                             
                                    for(var i = 0; i < entityNodes.length; i++) {
                                        if (entityNodes[i].nodeType == 1) {
                                            message += entityNodes[i].getAttribute('label');
                                        }
                                        if((i+1) != entityNodes.length) {
                                           message += ", ";
                                        }
                                    }
                                    MdaradMessageHandler.clearAndDisplayMessage("warning", "content-body-messages", message);
                                }
                            }
                        } catch(e) {
                            displayErrorMessageForNameMatchingRetrieval("Unable to fetch matching name list");
                        }
                    },
                    onFailure: function() {
                        displayErrorMessageForNameMatchingRetrieval("Unable to fetch matching name list");
                    }
                }
           );
        } else {
            MdaradMessageHandler.removeAllMessages("content-body-messages");
        }
    }
    
    function displayErrorMessageForNameMatchingRetrieval(message) {
        //doNothing
    }
-->
