function sortByCityThenName(a, b) {
    var x = a.city.toLowerCase() + a.name.toLowerCase();
    var y = b.city.toLowerCase() + b.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByDistance(a,b){
	 var x = a.distance;
     var y = b.distance;
     return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function writeLocations(locations){
	
    var searchResults = new String();
    if(typeof(locations)!= 'undefined'){
        
        searchResults += '<h1>Search Results</h1>';
        
        var zip = document.getElementById("postalcode").value;
        var city = document.getElementById("city").value;
        
        var noZip = (zip.toLowerCase()=='zip' || zip.length==0);
        var noCity = (city.toLowerCase()=='city' || city.length==0);
        
        
        if(noZip && noCity){
            locations.sort(sortByCityThenName);         
        }else{
            locations.sort(sortByDistance);
        }
        
        //alert("locations: " + locations.length);        
        //dump(locations);
        
        lastClass='';
        
        //alert(locations.length);
        
        for(myLoc=0; myLoc<locations.length; myLoc++){ 
        
        
        try{         
            thisLocation = locations[myLoc];
            locationOk = true;
            
        }catch(e){            
            locationOk = false;
        }  
        
        if(locationOk){           
            searchResults +='<div class="resultsRow">';  
          
           /*
            switch(thisLocation.columnPosition.toString()){
                case '1':
                    thisClass='leftResultCol';
                    break;
                case '2':
                    thisClass='centerResultCol';
                    break;
                case '3':
                    thisClass='rightResultCol';
                    break;
            }*/
            
              switch(lastClass){
                case '':
                thisClass='leftResultCol';
                break;
                
                case 'leftResultCol':
                thisClass='centerResultCol';
                break;
                
                case 'centerResultCol':
                thisClass='rightResultCol';
                break;
                
                case 'rightResultCol':
                thisClass='leftResultCol';
                break;              
            }
            lastClass=thisClass;
           
            searchResults +='<div class="' + thisClass +'">';    
                        
            var seoStr = seoFacilityUrl(thisLocation['recordId'],thisLocation['city'],thisLocation['state'],thisLocation['name']);
              
            searchResults += seoStr; //+ ' ' + thisClass; 
            searchResults +='<br/>';
            
            searchResults +=thisLocation.address; 
            searchResults +='<br/>';
            searchResults +=thisLocation.city;
            searchResults +=', ';
            searchResults +=thisLocation.state + " " + thisLocation['postal'];
            
            searchResults +='<br/>';
                
            searchResults +='<div class="clearit"><br/></div>';
         
            searchResults +='</div>';
    
            searchResults +='</div>';
            
            }         
           
        }
        
        document.getElementById("AdvSearchresults").innerHTML = searchResults;
        
       
        
    }else{
        //alert('locations not defined');
    }
}
