$(function(){
  $("#citytownname").change(function(){
    $("#community").hide();
    $("#Neightbourhood").empty();
    
    if($(this).find(':selected').text()=="Calgary North West"){
      popSelect('redbab.communities','redblud_val','redblud_desc','id',"NW",'Neightbourhood','redblud_desc');
      $("#community").show();
	  $(this).find(':selected').val('@0046');
	  $(this).find(':selected').text('Calgary North West');
      //$(this).attr("name","notneeded");
      //@streetdir=@NW
    } else if ($(this).find(':selected').text()=="Calgary North East") {
      popSelect('redbab.communities','redblud_val','redblud_desc','id',"NE",'Neightbourhood','redblud_desc');
      $("#community").show();
	   $(this).find(':selected').val('@0046');
	  $(this).find(':selected').text('Calgary North East');
      //$(this).attr("name","notneeded");
    } else if ($(this).find(':selected').text() == "Calgary South West") {
      popSelect('redbab.communities','redblud_val','redblud_desc','id',"SW",'Neightbourhood','redblud_desc');
      $("#community").show();
	   $(this).find(':selected').val('@0046');
	  $(this).find(':selected').text('Calgary South West');
      //$(this).attr("name","notneeded");
    } else if ($(this).find(':selected').text() =="Calgary South East") {
      popSelect('redbab.communities','redblud_val','redblud_desc','id',"SE",'Neightbourhood','redblud_desc');
      $("#community").show();
	   $(this).find(':selected').val('@0046');
	  $(this).find(':selected').text('Calgary South East');
      //$(this).attr("name","notneeded");
    } else {
      //$("#community").show();
       //$(this).attr("name","notneeded");
      //popSelect('redbab.communities','redblud_val','redblud_desc','id',$(this).val(),'Neightbourhood','redblud_desc');
    }
  });
  
   $("#Neightbourhood").change(function(){
		if($(this).val()){
				$("#citytownname").attr("name","notneeded");
				$("#Neightbourhood").val($(this).val());
		}
	});
  
  $("#CGYmainHomeSearch #listingtype").change(function(){

    if($("#CGYmainHomeSearch #listingtype option:selected").text() == "Condo"){

        $("#CGYmainHomeSearch #respropclass").val("@COND");

    }
    if($("#CGYmainHomeSearch #listingtype").val() == "rrl"){
       popSelect("redbcalgary.vow_rural","optionValue","optionDisplay","","","rrlsel","optionDisplay","Rural Area");
       $("#resarea").hide();
       $("#rrlarea").show();
       $("#community").hide();
    } else {
      $("#rrlarea").hide();
       $("#resarea").show();
    }

  });

  

  
})

function popSelect(tb,val,name,fltf,filtv,opt,or){
  $.getJSON("/php-bin/gmaps_ab.php",{overlord: "gfilters", tb: tb, val: val, name: name, fltf: fltf, filtv : filtv, ord: or },function(j){
             var optn = '';
      var all_;
      
      for (var i = 0; i < j.length; i++) {
                               // alert("here");
        optn += '<option value="@' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        if(!all_) all_ =  '@'+j[i].optionValue;
        else all_ += '-@'+j[i].optionValue;
      }  
      optn = '<option value="'+all_+'">Select Community </option>'+optn;
      
      $('#'+opt).html(optn);
      $('#'+opt+' option:first').attr('selected', 'selected');
        })

}
/*
$('#role').html($(this).children("[@selected]").text()).show(); ...
$("#PropertiesChildDD").ready(function() {
  $("#PropertiesChildDD").change(function() {
     $("#Neightbourhood").empty();
     popSelect('redbab.communities','redblud_val','redblud_desc','id',$(this).val(),'Neightbourhood','redblud_desc');
  });
});

*/

