﻿/// <summary>
///     Load the category list from the data emitted by the repeater.
/// </summary>
/*function LoadChildCategoryList()
{	   
    var lstFamily = document.getElementById('lstCategories') ;
    var lstCategory = document.getElementById('lstSubCategories');  
    var txtCategory   = document.getElementById('txtCategory');
    var txtSubCategory = document.getElementById('txtSubCategory');
    
    var intSelectedFamilyId = 0;
    
    // Clean out existing entries in the second of the lists.
    for (var i=lstCategory.options.length-1;i>=0;i--) {
        lstCategory.options[i]=null;         
    }    
     
    // Find selected family ID
    if (txtSubCategory != null)
    {
        txtCategory.value = "Please Select Category";
    }
    
    for (var intFamilyScan = 0; intFamilyScan < lstFamily.options.length; intFamilyScan++) {
        if (lstFamily.options[intFamilyScan].selected) {
            intSelectedFamilyId = lstFamily.options[intFamilyScan].value;
            if (txtSubCategory != null)
            {
                txtCategory.value = lstFamily.options[intFamilyScan].text;
            }
            else
            {
                alert("Cannot find txtSubCategory");
            }
        }
    }
    
    if (txtSubCategory != null)
    {
        txtSubCategory.value = "Please Select Sub-Category";
    }
    
    // Find the first selected item in the first list, and populate the second.
    for (var i=0;i<lstFamily.options.length;i++) {
        if (lstFamily.options[i].selected) {        
            // For each child in the list.   
            var intCount = 0;
            for (var intCategoryIndex = 0; intCategoryIndex < objCategories.length; intCategoryIndex++) {
                if (objCategories[intCategoryIndex].FamilyID == intSelectedFamilyId) {
                    lstCategory.options[intCount++] = new Option(objCategories[intCategoryIndex].Name, objCategories[intCategoryIndex].TypeID);
                } 
            }
            
            // Exit the block after the first child.
            break;
        }
    }          
}

/// <summary>
///    Display the child category name.
/// </summary>
function DisplayChildCategoryName()
{
    var txtSubCategory = document.getElementById('txtSubCategory');        
    var lstSubCategories = document.getElementById('lstSubCategories');    
    
    if ((txtSubCategory != null) && (lstSubCategories != null))
    {       
        for (var intFamilyScan = 0; intFamilyScan < lstSubCategories.options.length; intFamilyScan++) {
            if (lstSubCategories.options[intFamilyScan].selected) {
                txtSubCategory.value = lstSubCategories.options[intFamilyScan].text;
            }
        }            
    }
}

/// <summary>
///    Load parent category list. Relies on objCategories being family ordered by name.
/// </summary>
function LoadParentCategories()
{
    var lstFamily = document.getElementById('lstCategories');     
    var txtCategory   = document.getElementById('txtCategory');
    var txtSubCategory = document.getElementById('txtSubCategory');
        
    var strLastCategory = null;
    var intCount = 0;
    
    for (var intCategoryIndex = 0; intCategoryIndex < objCategories.length; intCategoryIndex++) {
        var objCurrentCategory = objCategories[intCategoryIndex];
        
        if (objCurrentCategory.FamilyName == strLastCategory) {
            continue;
        }
        
        lstFamily.options[intCount++] = new Option(objCategories[intCategoryIndex].FamilyName, objCategories[intCategoryIndex].FamilyID); 
        strLastCategory = objCategories[intCategoryIndex].FamilyName;
    }   
    
    if (txtCategory != null)
    {
        txtCategory.value = "Please Select Category";
    }
    else
    {
        alert("Cannot find txtCategory");
    }
    
    if (txtSubCategory != null)
    {
        txtSubCategory.value = "Please Select Sub-Category";
    }
    else
    {
        alert("Cannot find txtSubCategory");
    }
}*/

/// <summary>
///     Execute the search, if possible.
/// </summary>


var c_currentId;
var c_tier;
var c_displayLevels=3;
var c_requiredLevel=2;

var c_level1="";
var c_level2="";
var c_level3="";
var c_level4="";
var c_level5="";

function selectChildren(parentId, businessType, tier)
{
    var ajaxPath="";
    var counter;
    var selectedDetails;
    
    if(tier <= c_displayLevels)
    {
        counter =tier + 1;
        c_currentId = parentId;
        c_tier = tier;
        document.getElementById("selectedLevel" + tier).value = businessType;
       
        eval("c_level" + tier + "='"  + businessType + "'");
        
        while(document.getElementById("selectedLevel" + counter))
        {
           switch (counter)
           {
                case 1:   document.getElementById("selectedLevel" + counter).value = "Category";break;
                case 2:   document.getElementById("selectedLevel" + counter).value = "Subcategory";break;
                default:   document.getElementById("selectedLevel" + counter).value = "";break;
           }
          
            counter = counter + 1;
        }
    
        
        if(document.getElementById("divSubTypes" + parentId).innerHTML.length > 0)
        {
            if(document.getElementById("divSubTypes" + parentId).className=="on")
            {
                document.getElementById("divSubTypes" + parentId).className="off";
                    document.getElementById("li" + tier + parentId).className="level" + tier + "unselected";
            }   
            else
            {
                document.getElementById("divSubTypes" + parentId).className="on"; 
                document.getElementById("li" + tier + parentId).className="level" + tier + "selected";
            }
        }
        else
        {
            ajaxPath = "returnSubTypes.aspx?ParentId=" + parentId;
            AjaxData(ajaxPath);
        }
   }
               
}

function AjaxData(strPath)
{
    if (document.all){xhttpSearchResults = new ActiveXObject("Msxml2.XMLHTTP");}
    else{xhttpSearchResults = new XMLHttpRequest(); }	
    xhttpSearchResults.onreadystatechange = OnReadyState;
    xhttpSearchResults.open("GET",strPath, true);
    xhttpSearchResults.send("");	
}

function OnReadyState()
{  
  if (xhttpSearchResults.readyState==4) 
    {
        if (document.all) 
        {
	        if (xhttpSearchResults.responseText!="") 
	        { 
		       document.getElementById("divSubTypes" + c_currentId).innerHTML= xhttpSearchResults.responseText;
		       document.getElementById("divSubTypes" + c_currentId).className="on";
		       document.getElementById("li" + c_tier + c_currentId).className="level" + c_tier + "selected";
            }
        }
	    else
	    {
	        if(xhttpSearchResults.responseText!="") 
	        {			
		         document.getElementById("divSubTypes" + c_currentId).innerHTML=  xhttpSearchResults.responseText;
		         document.getElementById("divSubTypes" + c_currentId).className="on";
		         document.getElementById("li" + c_tier + c_currentId).className="level" + c_tier + "selected";
            }
        }
    }
}

function CheckSearchTypes()
{
    var requiredSelectedValue;
    var category="";
    var searchPage="";
    var checktText="";
    document.getElementById("divErrors").innerHTML="";
    requiredSelectedValue = eval("c_level" + c_requiredLevel);
    
    if(requiredSelectedValue.length > 0)
    {
        for(counter=c_displayLevels;counter > c_requiredLevel-1;counter--)
        {
            if(category == "")
            {
                switch (counter)
                {
                    case 1: checktText="Category";break;
                    case 2: checktText="Subcategory"; break;
                    default:checktText="";
                }
                if(document.getElementById("selectedLevel" + counter).value != checktText)
                {
                    category = document.getElementById("selectedLevel" + counter).value;
                }
            }
        } 
    }
    else
    {
       document.getElementById("divErrors").innerHTML="Please specify a Level " + c_requiredLevel + " value";
    }
     return category;          
}

function setSelectedDetails()
{
    selectDetailsHTML=""
    selectDetailsHTML = selectDetailsHTML + "<ul>";
    for(counter=1;counter<=c_displayLevels;counter++)
    {
        switch (counter)
        {
            case 1:selectDetailsHTML = selectDetailsHTML + '<li class="select"><input type="text" autocomplerte="false" enableviewstate="false" id="selectedLevel' + counter + '" value="Category" style="border: none; background-color: Transparent; width: 230px;" class="white" readonly="readonly"  /></li>';break;
            case 2:selectDetailsHTML = selectDetailsHTML + '<li class="select"><input type="text" enableviewstate="false" id="selectedLevel' + counter + '" value="Subcategory" style="border: none; background-color: Transparent; width: 230px;" class="white" readonly="readonly"  /></li>';break;
            default : selectDetailsHTML = selectDetailsHTML + '<li class="unselect"><input type="text" enableviewstate="false" id="selectedLevel' + counter + '" value="" style="border: none; background-color: Transparent; width: 230px;" class="white" readonly  /></li>';break;
            
        }
    }   
    selectDetailsHTML = selectDetailsHTML + "</ul>";
    document.getElementById("divSelectedDetails").innerHTML = selectDetailsHTML
}
        

function CategorySearchClick()
{
    var tbLocation = document.getElementById('SearchControlCategory_TxtLocation');
    var lstSubCategories = document.getElementById('lstSubCategories');   
    var chkShowOnMap = document.getElementById('ChkShowOnMap');
    var strUrl = '/';
    var category="";
    
    // Without a checkbox, we're doomed.
    if (chkShowOnMap == null)
    {
        alert("There was an error with the show on map checkbox.");
        return;
    }
    else if (chkShowOnMap.checked) 
    {
        strUrl += "ResultsMap.aspx?";
    }
    else 
    {
        strUrl += "Results.aspx?";
    }
    
    // Without a location textbox, we're doomed.
    if (tbLocation == null) 
    {
        alert("There was an error with the specified location.");
        return;
    } 
    else if ((tbLocation.value == null) || (tbLocation.value.length < 1)) 
    {
        document.getElementById("divErrors").innerHTML="Please enter a location.";
        return;        
    } 
    else 
    {
        strUrl += "Location=" + escape(tbLocation.value) + "&Radius=10";
    }
        
   
    category = CheckSearchTypes()
 
    if (category.length > 0)
    {
        strUrl += "&Category=" + escape(category);
        document.location = strUrl;
        return;
    }
    else
    {
         document.getElementById("divErrors").innerHTML="Please select the subcategory you want to search by";
        return;
    }
    
}