﻿

    function OPP_DisplayCurrentSelection(pAreaCode, cAreaCode) 
    {    
        // select this value and then call the display method to update the child select                 
        var parentSelect = document.getElementById("pArea");
        if(pAreaCode != null)
        {
            parentSelect.value = pAreaCode.toUpperCase();   
        }
        OPP_DisplayChildLevelSelection(parentSelect);
        // now try and select the child option
        var childSelect = document.getElementById("cArea");
        if(cAreaCode != null)
        {
             childSelect.value = cAreaCode.toUpperCase();   
        }         
    }  


    function OPP_DisplayChildLevelSelection(parentSelectObj) 
    {    
        var selectedValue = parentSelectObj.value;
        var childSelect = document.getElementById("cArea");
        if(selectedValue == "")
        {
            childSelect.disabled = "true";
        }
        else
        {
            childSelect.disabled = "";
            // now reset the options available in the child area select
            while(childSelect.options.length > 0)
            {
                childSelect.remove(0);
            }
            var hiddenChildSelect = document.getElementById("childAreaSelect" + parentSelectObj.value);
            if(hiddenChildSelect != null)
            {
                childSelect.options[childSelect.options.length] = new Option("Select your Local Authority...","");
                childSelect.options[childSelect.options.length] = new Option("County\\Unitary Authority profile","PARENTPROFILE");
                for (var i = 0; i < hiddenChildSelect.options.length; i++) 
                { 
                    var hiddenOption = hiddenChildSelect.options[i];
                    childSelect.options[childSelect.options.length] = new Option(hiddenOption.text,hiddenOption.value);
                }
            }
            else
            {
                 childSelect.options[childSelect.options.length] = new Option("County\\Unitary Authority profile","PARENTPROFILE");
            }
        }
    }  
    
    
    // Checks that an area has been selected and moves to a new page in the current directory
	function OPP_CheckParentAreaSelectionAndMoveToPage(pageName, pArea, cArea) 
    {        
        if(pArea == "")
        {
            alert("Please select a County or Unitary Authority from the list.");
        } 
        else if(cArea == "")
        {
            alert("Please select a Local Authority from the list.");
        } 
        else
        {
            OPP_MoveToPage(pageName, pArea, cArea);
        }
    } 


    function OPP_CheckThemeAndIndicatorSelectionAndMoveToPage(pageName, pCode, cCode, themeCode, indicatorCode) 
    {      
        if(themeCode == "")
        {
            alert("Please select a theme from the list.");
        } 
        else if(indicatorCode == "")
        {
            alert("Please select an indicator from the list.");
        } 
        else
        {
            OPP_MoveToPage(pageName, pCode, cCode, themeCode, indicatorCode);
        }
    }   
    
    function OPP_CheckSelectionAndReloadPage(pageName, select, pCode, cCode, anchorText) 
    {   
        var selectVal = select.value;
        if(selectVal != null && selectVal != "")
	    {
	        if('undefined' == typeof pCode)	        
	            OPP_MoveToPage(pageName, selectVal, anchorText);
	        else
	            OPP_MoveToPage(pageName, pCode, cCode, selectVal, null, anchorText);    
	    } 
    } 
    
    // Called to move to a new page in the current directory
	function OPP_MoveToPage(pageName, pCode, cCode, themeCode, indicatorCode, anchorText) 
    {
        var url = pageName;
        // special logic for bedfordshire
        if((pCode == '00KB' || pCode == '00KC') && (cCode == null || cCode == ""))
        {
            cCode = pCode;
            pCode = '09';
        }        
	    if(pCode != null && pCode != "")
	    {
	        url+= "?parea=" + pCode;	        
	    }
	    if(cCode != null && cCode != "")
	    {
	        url+= "&carea=" + cCode;    	        
	    }
	    if(themeCode != null && themeCode != "" && themeCode != "#" && ('undefined' != typeof themeCode))
	    {	        
	        url+= "&theme=" + themeCode;    	        
	    }
	    if(indicatorCode != null && indicatorCode != "" && ('undefined' != typeof indicatorCode))
	    {
	        var indicatorNum = indicatorCode;
	        var sex = null;
	        if(indicatorCode.indexOf("-")>0)
	        {
	            var indicatorSections = indicatorCode.split("-");
	            indicatorNum = indicatorSections[0];
	            sex = indicatorSections[1];
	        }
	        url+= "&indic=" + indicatorNum;
	        if(sex != null)
	        {
	            url+= "&sex=" + sex;
	        }    	        
	    }
	    if(anchorText != null && anchorText != "" && ('undefined' != typeof anchorText))
	    {
	        url+= "#" + anchorText;
	    }
	    location.href = url;
    } 