var changedIndex=0;
function checkForm()
{
if(document.getElementById('filterOpt1').selectedIndex==0)
return false;
else
return true;
}

function populateCategoryFilterSelects(r){
    for (var i=0; i<r.selects.length; i++) {
	    
        populateCfSelect( $("#"+ r.selects[i].id), r.selects[i].options,r.selects[i].id,i+1);
		
    }
	
}

function populateCfSelect(obj, options, selectid,index)
{
    var selectedIdx = 0;
    var selectedValue = obj.find("option:selected")[0].value;
    var label = obj.find("option")[0].text;
    var allLabel = 'All ';
    if (label.indexOf(allLabel) == 0) {
        label = label.substring(allLabel.length);
    }
	
	//to reload values on the basis of previoous selection and prevent reloading of selected index
    if(document.getElementById(selectid).length>1&&options.length==1&&index<=changedIndex)
    {
     return;
    }
    obj.children().remove();

    if (options.length == 1 && selectedValue.length > 0) {
        obj.append('<option value="">' + allLabel + label + '</option>');
    } else {
       obj.append('<option value="">' + label + '</option>');
    }
	 
	 
	 
	 
    for (var i = 0; i<options.length; i++)
    {
        if (selectedIdx==0 && selectedValue.length > 0 && selectedValue == options[i]) {
            selectedIdx = obj.find("option").length;  // getting the count *before* adding the new option
        }
        obj.append('<option value="' + options[i] + '">' + options[i] + '</option>');
    }	
    obj[0].selectedIndex=selectedIdx;
	

	if(options.length<=0)
	{
	//document.getElementById(selectid).style.display='none';
	}
	else
	document.getElementById(selectid).style.display='block';
	
}

function categoryFilterUpdate(formData)
{
   $.getJSON("/getFilteredCategoryFilters.ndi?" + formData, function(r) {
        populateCategoryFilterSelects(r);
    });
	
}

function showMoreCategoryFilterOptions(show) {
    if (show) {
        $("#categoryFilterForm").find("select").show();
    } else {
        $("#categoryFilterForm").find("select").each(function(idx) {
            if (idx > 2) {
                $(this).hide();
                $(this)[0].selectedIndex=0;
            }
        });
        return  categoryFilterUpdate($("#categoryFilterForm").fastSerializeUrlString());
    }
}

function categoryFilterSubmit(formData)
{
    window.location = "landing.cn?formSubmission=Y&pageNumber=1" + formData;
	return false;
}

$(function() {
    $("select.categoryFilterSelect").change(function() {
	
	   var hval=document.getElementById('h').value;
	  
	   var data='h='+hval+'&categoryName='+category;
        
         	 
	 
	 // if we reselect the subcategory without any option
	  if(document.getElementById('filterOpt1').selectedIndex==0&&changedIndex==1)
	  {
	  
	   var x=2;
	   for(;x<=count;x++)
	   {
	  
	    document.getElementById('filterOpt'+x).selectedIndex=0;
	     if(x<=3)
	     {
		 document.getElementById('filterOpt'+x).options[0].text=''
		  document.getElementById('filterOpt'+x).style.display='block';
	       document.getElementById('filterOpt'+x).disabled=true;
		  
	     }
		 else
		 {
		 document.getElementById('filterOpt'+x).style.display='none';
		   
		 }
	   }
	   
	   return;
	  }
	  if(document.getElementById('filterOpt1').selectedIndex!=0&&changedIndex==1)
	  { var x=2;
	   for(;x<=count;x++){
	   if(document.getElementById('filterOpt'+x).length==1){
	    document.getElementById('filterOpt'+x).style.display='none';
	   }
	   
	   }
	  }
	  
	  
	  
	
	  var j=1
	  
	  // chage the shown value of dropdown ie to show label or selected value.
      for(;j<=count;j++)
	  {
	   var ind;
	 
	   document.getElementById('filterOpt'+j).disabled=false;
	  
	   document.getElementById('filterOpt'+j).options[0].text=label[j-1]
	   if(document.getElementById('filterOpt'+j).selectedIndex==0||changedIndex<j){
	   ind='';
	     
	  // break;
	   }
	   else
	   ind=document.getElementById('filterOpt'+j).value
         ind=ind.replace('&','amp');


	   data=data+"&values['"+vals[j-1]+"']="+ind
	  }
	  for(;j<=count;j++)
	  {
	   var ind='';
	 
	   document.getElementById('filterOpt'+j).disabled=false;
	  
	   document.getElementById('filterOpt'+j).options[0].text=label[j-1]
	   
	   data=data+"&values['"+vals[j-1]+"']="+ind
	  }
	  
	  //to clear reselected values
	  for(var k=changedIndex+1;k<=count;k++)
	  {
	    if(document.getElementById('filterOpt'+k).selectedIndex!=0){
		 document.getElementById('filterOpt'+k).selectedIndex=0;
		}
	  }
	  
	  
	  return  categoryFilterUpdate(data);
    });
    $("#categoryFormSubmit").click(function() {
       //alert('submit')
    });
    if ($("#moreCategoryFilterOptions")) {
	
        $("#moreCategoryFilterOptions").toggle(
            function() { showMoreCategoryFilterOptions(true); },
            function() { showMoreCategoryFilterOptions(false);});
    }
});
