	// =======================================================
	//  Author: RR    
	//  Date: 13/02/2008
	//  Description: Javascript for submitting and validating the
	//  NHII forms
	// =======================================================
	
	// Called when the selected option in the comparator area select is changed.
	function NHII_ChangeComparatorArea(laCode, compArea) 
    {
	    var url = location.pathname + "?la=" + laCode;
    	if(compArea != null && compArea != "")
        {
            url+= "&comp=" + compArea;    	        
        }
	    location.href = url;
    }  
    
    // Called to move to a new page in the current directory
	function NHII_MoveToPage(pageName, laCode, compArea) 
    {
        var url = pageName;
	    if(laCode != null && laCode != "")
	    {
	        url+= "?la=" + laCode;
	        if(compArea != null && compArea != "")
	        {
	            url+= "&comp=" + compArea;    	        
	        }
	    }
	    location.href = url;
    } 
    
    
    // Checks an LA has been selected and moves to a new page in the current directory
	function NHII_CheckLASelectionAndMoveToPage(pageName, laCode, compArea) 
    {
        // first check that a LA has been selected
        if(laCode == "")
        {
            alert("Please select a local authority from the list.");
        } 
        else
        {
            NHII_MoveToPage(pageName, laCode, compArea);
        }
    }         
    
    // trims the given text value
    function NHII_JS_TrimValue(value)
    {
	     return value.replace(/(^\s+|\s+$)/g, "");
    }
    
    // ======================================================================== 
    // The following functions are used on the interventions calculation page
    // to validate the user inputs
    // ======================================================================== 
    
    var AntihypertensiveInputKey = "AH";
    var StatinInputKey = "ST";
    var InputObjectWithFocus = "";
    var FireSubmitAfterCheckValue = false;
    var FireSubmitWithLACode = "";
    var FireSubmitWithComparator = "";
       
    // called when one of the imput objects recieves focus
    function NHII_JS_Interventions_StartingInput(inputObj)
    {
        // record the ID of the object with focus
        InputObjectWithFocus = inputObj.id;
    }
        
    // Checks the value entered is within the correct range
	function NHII_JS_Interventions_CheckValue(inputObj, minValue, maxValue) 
    {
        // work out the type of intervention and input
        var inputName = inputObj.id;
        var inputTypeSuffix = "Value"
        if(inputName.indexOf("MValue") > 0)
        {
            inputTypeSuffix = "MValue"
        }
        else if(inputName.indexOf("FValue") > 0)
        {
            inputTypeSuffix = "FValue"
        }        
        var interventionKey = inputName.replace(inputTypeSuffix,"");
    
        // if a value has been entered check its in the correct range
        if(inputObj.value != "")
        {
            var fValue = parseFloat(inputObj.value) 
            
            // special rules for the statin input
            if(interventionKey == StatinInputKey)
            {
                // use the max value in the corresponding antihypertensive input
                var antiHyperInputObj = document.getElementById(AntihypertensiveInputKey + inputTypeSuffix);
                if(antiHyperInputObj.value == "")
                {
                    alert('Please enter the Antihypertensive value first.');
                    inputObj.value = "";
                    antiHyperInputObj.focus();
                    FireSubmitAfterCheckValue = false;
                    return;
                } 
                else
                {
                    // update the max value and let the check proceed
                    maxValue = parseFloat(antiHyperInputObj.value); 
                }                
            } 
            
            // special rules for the Antihypertensive input       
            if(interventionKey == AntihypertensiveInputKey)
            {
                // use the min value from the corresponding statin input
                var statinInputObj = document.getElementById(StatinInputKey + inputTypeSuffix);
                if(statinInputObj.value != "")
                {
                    // update the min value and let the check proceed
                    minValue = parseFloat(statinInputObj.value); 
                }                
            }      
            
            if(fValue + "" == "NaN" || fValue < minValue || fValue > maxValue)
            {
                alert('Please enter a value between ' + minValue + ' and ' + maxValue);
                inputObj.value = "";
                inputObj.focus();
                FireSubmitAfterCheckValue = false;
                return;
            } 
        }  
        // otherwise allow the user to navigate away from this input and reset the focus value
        InputObjectWithFocus = "";
        //alert("Removing focus");
        if(FireSubmitAfterCheckValue)
        {
            NHII_JS_Interventions_SubmitInterventionForm(FireSubmitWithLACode, FireSubmitWithComparator)
        }
    }  
               
  
    // Checks the value entered is within the correct range
	function NHII_JS_Interventions_ToggleInputAvailability(checkboxObj) 
    {
        var checked = checkboxObj.checked;
        var interventionKey = checkboxObj.id.replace("Checkbox","");
        // check for single input
        var interventionInput = document.getElementById(interventionKey + "Value");
        if(interventionInput != null)
        {
            NHII_JS_Interventions_EnableTextInput(interventionInput, checked);
        }
        // check for male input
        interventionInput = document.getElementById(interventionKey + "MValue");
        if(interventionInput != null)
        {
            NHII_JS_Interventions_EnableTextInput(interventionInput, checked);
        }
        // check for female input
        interventionInput = document.getElementById(interventionKey + "FValue");
        if(interventionInput != null)
        {
            NHII_JS_Interventions_EnableTextInput(interventionInput, checked);
        }
        // special rules for the Antihypertensive check box
        if(interventionKey == AntihypertensiveInputKey)
        {
            // we should update the statin checkbox
            var statinCheckBoxObj = document.getElementById(StatinInputKey + "Checkbox");
            statinCheckBoxObj.disabled = !checked;
            statinCheckBoxObj.checked = false;
            NHII_JS_Interventions_ToggleInputAvailability(statinCheckBoxObj);
        }
    }      
    
    // Enables or disables the given input object
    function NHII_JS_Interventions_EnableTextInput(textObj, enable) 
    {
        if(enable)
        {
            textObj.disabled = false;
            textObj.className = "NHII_EnabledInput";
        } 
        else
        {
            textObj.disabled = true;
            textObj.value = "";
            textObj.className = "NHII_DisabledInput";
        }
    }    
        
    // Checks the intervention inputs and if valid submits the form
    function NHII_JS_Interventions_SubmitInterventionForm(laCode, compArea)
    {
        // check if one of the input objects still has focus (this is required because this method may be called before the onblur method of the last input)
        if(InputObjectWithFocus != "")
        {
            // return and allow the blur action to fire
            FireSubmitAfterCheckValue = true;
            FireSubmitWithLACode = laCode;
            FireSubmitWithComparator = compArea;
            return false;
        }
        
        var errorInputs = "";
        var inputParamList = "";
        var inputList = document.getElementById('InputElementList').value.split(',');
        for(var i=0; i<inputList.length; i++)
        {
            var inputName = inputList[i];
            if(inputName != '')
            {
                // this is the name of an input
                
                // get the checkbox and see if its enabled
                var checkBoxObj = document.getElementById(inputName + 'Checkbox');
                if(checkBoxObj.checked)
                {
                    // we need to check the values
                    var inputObj1 = document.getElementById(inputName + 'Value');
                    var inputObj2 = document.getElementById(inputName + 'MValue');
                    var inputObj3 = document.getElementById(inputName + 'FValue');
                    if(
                        (inputObj1 != null && NHII_JS_TrimValue(inputObj1.value) == "")
                        || (inputObj2 != null && NHII_JS_TrimValue(inputObj2.value) == "")
                        || (inputObj3 != null && NHII_JS_TrimValue(inputObj3.value) == "")
                    )
                    {
                           // there is an error with this input                           
                           errorInputs += "- " + checkBoxObj.title + "\n";
                    } 
                    else 
                    {
                           // this input is ok, work out which params we need to keep
                           if(inputObj1 != null && NHII_JS_TrimValue(inputObj1.value) != "" && parseFloat(inputObj1.value) + "" != "NaN" )
                           {
                                inputParamList += "&" + inputObj1.id + "=" + NHII_JS_TrimValue(inputObj1.value);
                           } 
                           if(inputObj2 != null && NHII_JS_TrimValue(inputObj2.value) != "" && parseFloat(inputObj2.value) + "" != "NaN" )
                           {
                                inputParamList += "&" + inputObj2.id + "=" + NHII_JS_TrimValue(inputObj2.value);
                           } 
                           if(inputObj3 != null && NHII_JS_TrimValue(inputObj3.value) != "" && parseFloat(inputObj3.value) + "" != "NaN" )
                           {
                                inputParamList += "&" + inputObj3.id + "=" + NHII_JS_TrimValue(inputObj3.value);
                           } 
                    }
                    
                }
                
            }
        }
        
        // check whether there are any error inputs to report
        if(errorInputs != "")
        {
            alert('Please enter a value for the following interventions:\n' + errorInputs);
        } 
        // check we have a least one input
        else if(inputParamList == "")
        {
            alert('Please select at least one intervention to model.');
        }        
        // build the new page url and redirect
        else 
        {
            var url = location.pathname + "?la=" + laCode;
    	    if(compArea != null && compArea != "")
            {
                url+= "&comp=" + compArea;    	        
            }
	        url += inputParamList;
	        location.href = url;
        }             
    }
    
    
    // ======================================================================== 
    // The following functions are used on the interventions calculation page
    // to show and hide the custom tool tips
    // ======================================================================== 
        
        
    // updates the contents of the help div and moves it to the correct location 
    function ShowHelpMessage(helpMessage, imageObj, e)
    {
        var xCoord = 0;
        var yCoord = 0;
        var helpContainer;
        var helpMessageContainer;
        //var imagRect = document.getBoxObjectFor(imageObj);
        var imagRect = GetElementCoordinates(imageObj) 
        // retrieve coordinates for the div
        helpContainer = document.getElementById('InterventionHelpContainerDiv');
        helpMessageContainer = document.getElementById('InterventionHelpMessageDiv');
        //alert(imagRect.x);
        if (helpContainer != null && helpContainer != "undefined")
        {
            xCoord = imagRect.x - 9;
            yCoord = imagRect.y - 10;
        }
        // check we've got coordinates, update the div contents and move it
        if (xCoord != 0 && yCoord != 0 && helpContainer != null && helpContainer != "undefined")
        {
            helpMessageContainer.innerHTML = helpMessage;
            helpContainer.style.top = yCoord + "px";
            helpContainer.style.left = xCoord + "px";
            helpContainer.style.visibility = 'visible';
        }
    }

    // hides the help div
    function HideHelpMessage()
    {
       document.getElementById('InterventionHelpContainerDiv').style.visibility = 'hidden'; 
    }    
    
    // gets the co-ordinates of a element
    function GetElementCoordinates(element) 
    {
        var coords = { x: 0, y: 0, width: element.offsetWidth, height:element.offsetHeight };
        while (element) 
        {
            coords.x += element.offsetLeft;
            coords.y += element.offsetTop;
            element = element.offsetParent;
        }
        return coords;
    }
    
    
    // ***    NHII IM tool functions: start    *** //
    // Called when the selected option in the LA select is changed.
	function NHII_IM_ChangeLA(laCode, shaCode) 
    {
        location.href = location.pathname + "?sha=" + shaCode + "&la=" + laCode;
    } 
    
    // Called when the selected option in the SHA select is changed.
	function NHII_IM_ChangeSHA(shaCode) 
    {
        location.href = location.pathname + "?sha=" + shaCode;
    }  
    // ***    NHII IM tool functions: end    *** //

    // ***    NHII Spearhead tool functions: start    *** //
    // Called when the selected option in the select is changed.
	function NHII_S_ChangeArea(laCode) 
    {
        if(location.href.toLowerCase().indexOf("areacode") < 0){
            location.href = location.pathname + "?areacode=" + laCode;
        }
        else{
            var regex = /(areacode=)\w+(.*)$|(areacode=)\w+(\&)/i;
            location.href = location.href.replace(regex,"$1" + laCode + "$2");
        }
        
    } 
    
    // ***   IM help labels - START    *** //
    // shows hidden help div on help icon mouseover
    function NHII_IM_ShowHelp(helpElement) 
    {
        helpElement.className="NHII_IM_HelpDown";
        showHelpLabel(helpElement.id)
    }
    // hides help div on help icon mouseout
    function NHII_IM_HideHelp(helpElement) 
    {
        helpElement.className="NHII_IM_HelpUp";
        hideHelpLabel(helpElement.id)
    }
    
    // toggles visibility of selected help button label
    function showHelpLabel(helpID){
        //hide any currently visible labels
        var aLabels = [
            "help2_TeenagePregnancy",
            "help2_SUDI",
            "help2_SmokingInPregnancy",
            "help2_Obesity",
            "help2_Poverty",
            "help2_Breastfeeding",
            "help3_TeenagePregnancy",
            "help3_SUDI",
            "help3_SmokingInPregnancy",
            "help3_Obesity",
            "help3_Poverty",
            "help3_Breastfeeding"
            ];
        for (var i = 0; i < aLabels.length; i++) {
            hideHelpLabel(aLabels[i]);
        }
        // now show the desired one
        var labelItem = document.getElementById(helpID + "_popup");
        if(labelItem) labelItem.style.visibility = "visible";
    }
    function hideHelpLabel(helpID){
        var labelItem = document.getElementById(helpID + "_popup");
        if(labelItem) labelItem.style.visibility = "hidden";
    }      
    // ***   IM help labels - END    *** //
    
    // ***   Spearhead help labels - START    *** //
    // shows hidden help div on help icon mouseover
    function NHII_S_ShowHelp(helpElement) 
    {
        helpElement.className="NHII_S_HelpDown";
        showHelpLabel_S(helpElement.id)
    }
    // hides help div on help icon mouseout
    function NHII_S_HideHelp(helpElement) 
    {
        helpElement.className="NHII_S_HelpUp";
        hideHelpLabel_S(helpElement.id)
    }
    
    // toggles visibility of selected help button label
    function showHelpLabel_S(helpID){
        //hide any currently visible labels
        var aLabels = [
            "help2_1",
            "help2_2",
            "help2_3",
            "help2_4",
            "help2_5",
            "help3_1",
            "help3_2",
            "help3_3",
            "help3_4",
            "help3_5"
            ];
        for (var i = 0; i < aLabels.length; i++) {
            hideHelpLabel_S(aLabels[i]);
        }
        // now show the desired one
        var labelItem = document.getElementById(helpID + "_popup");
        if(labelItem) labelItem.style.visibility = "visible";
    }
    function hideHelpLabel_S(helpID){
        var labelItem = document.getElementById(helpID + "_popup");
        if(labelItem) labelItem.style.visibility = "hidden";
    }
    
    // shows hidden help div on help icon mouseover
    function NHII_S_ShowTableHelp(helpElement) 
    {
        helpElement.src='images/help_down.gif';
        showTableHelpLabel_S(helpElement.id);
    }
    // hides help div on help icon mouseout
    function NHII_S_HideTableHelp(helpElement) 
    {
        helpElement.src='images/help_up.gif';
        hideTableHelpLabel_S(helpElement.id);
    }
    
    function showTableHelpLabel_S(helpID){
        //hide any currently visible labels
        var aLabels = [
            "helpTable_1",
            "helpTable_2",
            "helpTable_3",
            "helpTable_4",
            "helpTable_5",
            "helpTable_6",
            "helpTable_7"
            ];
        for (var i = 0; i < aLabels.length; i++) {
            hideTableHelpLabel_S(aLabels[i]);
        }
        // now show the desired one
        var labelItem = document.getElementById(helpID + "_popup");
        if(labelItem) labelItem.style.visibility = "visible";
    }
    
    function hideTableHelpLabel_S(helpID){
        var labelItem = document.getElementById(helpID + "_popup");
        if(labelItem) labelItem.style.visibility = "hidden";
    }
    // ***   Spearhead help labels - END    *** //
          
    // Called when the selected option in the select is changed.
	function NHII_S_ChangeChart(dataType) 
    {
        if(location.href.toLowerCase().indexOf("datatype") < 0){
            if(location.href.toLowerCase().indexOf("areacode") > 0){
                var regex = /(\?areacode=\w+).*$|(\?areacode=\w+)[\&\#]/i;
                location.href = location.href.replace(regex,"$1" + "&datatype=" + dataType); 
            }
        }
        else{
            var regex = /(datatype=)\w+(.*)$|(datatype=)\w+(\&)/i;
            location.href = location.href.replace(regex,"$1" + dataType + "$2");
        }
        
    } 
    
    function NHII_S_ShowLoadingMessage(){
        var loadingMessageDiv = document.getElementById("loadingMessageDiv");
        if(loadingMessageDiv) loadingMessageDiv.style.display = "";
    }
    // ***    NHII Spearhead tool functions: end    *** //