$(function($) {
   //var filter_mode = $("#filter_search_mod").val();
   //if(filter_mode == 0)
    // load_categories(0,0,0);
});

function load_categories(level,catID,root){
  $.ajax({
    type: "GET",
    url: "getCats.php",
    data: { "retID": catID,"level":level,'root':root},
	dataType: "json",
    success: function(data){
	  var optStr;
	  if(data.status == 1){
        var subCats = data.cats;		
        // increment the level
        $("#level").val(parseInt($("#level").val())+1);
		optStr = '<select name="level'+ $("#level").val() +'" level="'+ $("#level").val() +'" onChange="loadCats(\''+ $("#level").val() +'\',this.value,this.options[this.selectedIndex].text)"><option value="0">-- Select --</option>';
        $.each(subCats, function(index, subcat) {
		  optStr=optStr + '<option value="'+ subcat.id +'">'+subcat.name+'</option>';
        });	
		optStr = optStr+'</select>';
	  //insert the categories now.
	  } else if(data.status == '0') {
	   // no subcategories
	   optStr = "This is the last level of categories";
	  }	   
	  if($('#catSelMod').find('#actLevel'+ parseInt($("#level").val())).length == 0){	  
	   var actSel = '<input type="hidden" name="actLevel'+ parseInt($("#level").val()) +'" id="actLevel'+ parseInt($("#level").val()) +'" value="">';
       $(actSel).appendTo('#catSelMod');
	  }
	  if($('#catSelMod').find("#catLevel"+ $("#level").val() +" SELECT").length == 0){	 
	  if(data.status == '1'){
	   if($("#dispType").val() == 'v')
	    optStr = '<div id="catLevel'+ $("#level").val() +'" class="catSelLvl"><label for="level'+$("#level").val()+'">' + data.levelprompt +'</label><br/>'+ optStr +'</div>';
	   else
	    optStr = '<div id="catLevel'+ $("#level").val() +'" class="catSelLvl"><label for="level'+$("#level").val()+'">'+data.levelprompt +'</label>' + optStr+'</div>';
	  }
	  $(optStr).appendTo('#catSelMod');
	  } else {
	   $("#catLevel"+ $("#level").val()+" SELECT").remove();
	   $("#catLevel"+ $("#level").val()).html($("#catLevel"+ $("#level").val()).html() + optStr);
	   //optStr = '<label for="level'+$("#level").val()+'">'+data.levelprompt +'</label>' + optStr+'</div>';
	  }
	 },
	 error:function(data){
	  alert('sorry could not retrive the records right away due to some error !!');
	 }
	});
}
  
function loadCats(level,catID,catName){
//alert('am here ');
 $('#actLevel'+ level).val(catName);
 if(parseInt($("#level").val()) == parseInt($("#maxLevel").val()) && level==parseInt($("#level").val())){
  $("#filterSearch").submit();
  return true;
 }
 if(catID == "0" || catID.length < 1)
  return false;
 // $("#destId").val(catID);
 // mime all above this level
 var curLvl;
 for(curLvl = parseInt($("#level").val());curLvl>parseInt(level);curLvl--)
  mimeLevel(curLvl);
 $("#level").val(level);
 if(level >= $("#minlevel").val())
  $("#filterFind").css("display","block");
 else
  $("#filterFind").css("display","none");
 load_categories(level,catID);
}

function mimeLevel(level){
 var defLevVl = $("#catLevel"+level+'SelDef').val();
 // alert('the textis ' + "#catLevel"+level);
 $("#catLevel"+level+" SELECT").empty().append('<option selected="selected" value="">'+ defLevVl +'</option>');
 $("#actLevel"+level).val('');
}

function checkLevel(){
 if($("#searchLevel").val() > $("#level").val()){
   alert("Please choose the appropriate category above to continue search");
   return false;
 }
}
