function generateCheckList(Locations, type){
	//Locations.push({0,0})
	var blankItem = [];
	blankItem.push('Any')
	Locations.value[0] = blankItem
	//alert(Locations.value[1])
	var container = document.getElementById("advancedLocationSearch")
	container.innerHTML = "";
    container.style.width = "100%"
	var column1  = document.createElement("div");
	column1.id="column1"
	column1.className ="floatLeft";
	container.appendChild(column1)
	var column2  = document.createElement("div");
	column2.className ="floatLeft";
	column2.id="column2"
	container.appendChild(column2)
	var column3  = document.createElement("div");
	column3.className ="floatLeft";
	column3.id="column3"
	container.appendChild(column3)
  
    if (Locations.value.length > 0){
		//createCheckListItem(column1, "Any", "", 1,true);
		
		var count = 0
		if (Locations.value.length){
				count = Math.ceil(Locations.value.length/3)
		}

		var lastIndex = 0; 
		var colCount = 3;
		for ( var x = 0; x < colCount; x++ ){
			for ( var i = lastIndex; i < (lastIndex+count); i++ ){
						var aitem = Locations.value[i];
						if(aitem){
							var sname = "";
							var svalue = "";
								if ( type == 1 ){
									sname = aitem[1];
									svalue = aitem[0];
								}else{
									sname = aitem;
									svalue = aitem;
								}
							var subContainer = null;
							subContainer = document.getElementById("column" +(x+1));
							if(subContainer){
								createCheckListItem(subContainer, sname, svalue, type,false,x,i);
							}
						}
			}
		

		lastIndex += count;
	} 
		  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,col,index){
    var subContainer = document.createElement("div")
    subContainer.style.width = "100%";
    //subContainer.className ="floatLeft";

    var chkLocation  = document.createElement("input");
    var txtLocation = document.createElement("label");
    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;
    }
    if ( value == "Any" ){
		value = "";
    }
    chkLocation.value = value;
    txtLocation.innerHTML = name;
    chkLocation.setAttribute("checked", "checked");
    chkLocation.onclick = function(){
		switch ( this.value.toLowerCase() ){
			case "" :
					for ( var i = 1; i < document.getElementById("advancedLocationSearch").getElementsByTagName("input").length; i ++ ){
						document.getElementById("advancedLocationSearch").getElementsByTagName("input")[i].checked = false;
					}		
					if ( !this.checked ){
						this.checked = true;
					}	
				break;
			default:
				document.getElementById("advancedLocationSearch").getElementsByTagName("input")[0].checked = false;
				break;
		}

    }
    
    subContainer.appendChild(chkLocation);
    subContainer.appendChild(txtLocation);
    container.appendChild(subContainer);
}

var listingType = 5
var searchType = 1
var resultsType = 1
    
window.onload = function (){
    listingType = HttpManager.Document.Forms.HtmlRadio.GetValue("isc", "listingtype");
    searchType = HttpManager.Document.GetObject("locationtype").value;
    var response = Searching.GetOffices(listingType, "");
    searchType_OnClick(0);
    var oListingType = new Object();
    oListingType.value = HttpManager.Document.Forms.HtmlRadio.GetValue("isc", "listingtype");
    PropertySearch_ListingType_OnSelectionChange(oListingType);
   }
   
function AdvancedSearch_onLoad(){

}
   
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, "");
    }
    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, "");
    }
    
    generateCheckList(response,searchType);
}

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()
}


