function generateCheckList(Locations, type){
	var container = document.getElementById("advancedLocationSearch")
	container.innerHTML = "";
    container.style.width = "100%"

    if (Locations.value.length > 0){
		createCheckListItem(container, "Any", "", 1,true);
		for ( var i = 0; i < Locations.value.length; i++ ){
			var aitem = Locations.value[i];
			var sname = "";
			var svalue = "";
			if ( type == 1 ){
				sname = aitem[1];
				svalue = aitem[0];
			}else{
				sname = aitem;
				svalue = aitem;
			}
			createCheckListItem(container, sname, svalue, type,false);
		  }
		  var aitems = container.getElementsByTagName("input");
		  aitems[0].checked = true;
		} else {container.innerHTML = "No Locations found for this criteria";}
}

function createCheckListItem(container,sText,sValue,type,isChecked){
    var subContainer = document.createElement("div")
    subContainer.style.width = "33.3%";
    subContainer.className ="floatLeft";

    var chkLocation  = document.createElement("input");
    var txtLocation = document.createElement("span");
    chkLocation.setAttribute("type","checkbox");
    var name = "";
    var value = "";
    if(type == 1){
        chkLocation.setAttribute("name","location");
        name = sText;
        value = sValue;
    } else {
        chkLocation.setAttribute("name","area");
        name = sText;
		value = sText;
    }
    chkLocation.value = value;
    txtLocation.innerHTML = name;
    chkLocation.setAttribute("checked", "checked");
    chkLocation.onclick = function(){
		if ( this.value.length > 0 ){
			document.getElementById("advancedLocationSearch").getElementsByTagName("input")[0].checked = false;
		}else{
			for ( var i = 1; i < document.getElementById("advancedLocationSearch").getElementsByTagName("input").length; i ++ ){
				document.getElementById("advancedLocationSearch").getElementsByTagName("input")[i].checked = false;
			}
			this.checked = true;
		}
    }
    
    subContainer.appendChild(chkLocation);
    subContainer.appendChild(txtLocation);
    container.appendChild(subContainer);
}

var listingType = 5;
var searchType = 1;
var resultsType = 1;
var categoryID = 1;
var includedGBSIDs = "";
var excludedGBSIDs = "";
var includedMasterGBSIDs = "9";
var excludedMasterGBSIDs = "";
    
window.onload = function (){
    listingType = HttpManager.Document.GetObject("listingtype").value;
    searchType = HttpManager.Document.GetObject("locationtype").value;
    var response = Searching.GetOffices(listingType, "");
    generateCheckList(response,searchType)
    PropertySearch_ListingType_OnSelectionChange(HttpManager.Document.GetObject("listingtype"));
   }
   
   
function listingType_OnClick(o){
    listingType = o.value
    var response = Searching.GetOffices(o.value, listingType);
     if (searchType == 1){
        response = Searching.GetOffices(listingType, "");
    }else {
        response = Searching.GetAreas(listingType, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs);
    }
    generateCheckList(response,searchType);
    PropertySearch_ListingType_OnSelectionChange(o);
}

function searchType_OnClick(type){
    searchType  = type
    if (type == 1){
        response = Searching.GetOffices(listingType, "");
    }else {
        response = Searching.GetAreas(listingType, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
    }
    
    generateCheckList(response,searchType);
    PropertySearch_ListingType_OnSelectionChange(HttpManager.Document.GetObject("listingtype"));
}

function resultsType_OnClick(type){
    resultsType = type;
}



function AdvancedSearchForm_OnSubmit(o){
	var frmAdvancedSearch = HttpManager.Document.GetObject("isc");
	//if (frmAdvancedSearch.resultsType[1].checked){resultsType = frmAdvancedSearch.resultsType[1].value}else{resultsType = frmAdvancedSearch.resultsType[0].value}
//	if (resultsType == 1){
		frmAdvancedSearch.action = "search.aspx"
//	}else{frmAdvancedSearch.action = "searchmapview.aspx"}
	frmAdvancedSearch.submit()
}


