//Object that manages the search control state on most pages
State = {
	IsModernBrowser: false,
	SearchMode: "Destination",
	
	CheckModernBrowser: function(){
		State.IsModernBrowser = SupportsAjax();
		if (typeof SaveState == "function") SaveState();
	},
	
	SwitchMode: function (){
		State.SearchMode = (State.SearchMode == "Destination") ? "PropertyName" : "Destination";
		if (typeof SaveState == "function") SaveState();
		
		return State.IsModernBrowser;
	},
	
	Set: function(elementId, value){
		var element = GetElementById(elementId);
		if (element) element.value = value;
	}
}

function BindDropDown(result, control, defaultValue)
{
	if (!control) return;
	
	control.options.length = 0;
	for (i = 0; i < result.length; i++)
	{
		control.options[control.options.length] = new Option(result[i].name, result[i].value);
		if(defaultValue && result[i].value == defaultValue)
		    control.selectedIndex = i;
	}
	
	if(control.options.length == 1)
	{
	    control.disabled = true;
	}
	else
	{
	    control.disabled = false;
	}
		
	DataBinding = false;
		
	//call anyone interested in hearing databinding finished
	if(typeof OnDataBoundEvents != 'undefined' && OnDataBoundEvents != null)
	{
	    for (i=0; i < OnDataBoundEvents.length; i++)	
		    OnDataBoundEvents[i]();
	}
}

// This is the callback function that
// processes the Web Service return value.
function SearchModelSuccess(result)
{
    BindDropDown(result, searchModel, searchModelId);
}

function SearchFailed(error){
	searchModel.disabled = true;
	alert(error.get_message());
	DataBinding = false;	
}

function GetModelsForMake()
{
	if (State && productTypeId && searchCategoryId && searchMakeId)
	{
	    // Check if values have changed since last refresh
	    if(searchCategory.options[searchCategory.selectedIndex].value != searchCategoryId)
	        searchCategoryId = searchCategory.options[searchCategory.selectedIndex].value;

	    if(searchMake.options[searchMake.selectedIndex].value != searchMakeId)
	        searchMakeId = searchMake.options[searchMake.selectedIndex].value;
	    
		{
		    DataBinding = true;
		    Enlighten.CablePrice.Administration.Services.ProductService.SelectModelsByTypeCatMake(productTypeId, searchCategoryId, searchMakeId, SearchModelSuccess, SearchFailed);
		}
	}		
}

// This is the callback function that
// processes the Web Service return value.
function SearchCatSuccess(result)
{
    BindDropDown(result, searchCategory, searchCategoryId);
}

// This is the callback function that
// processes the Web Service return value.
function SearchMakeSuccess(result)
{
    BindDropDown(result, searchMake, searchMakeId);
}

function GetCatMakeForType()
{
	if (State && searchProductType)
	{
	    // Check if values have changed since last refresh
	    if(searchProductType.options[searchProductType.selectedIndex].value != searchProductTypeId)
	    {
	        searchProductTypeId = searchProductType.options[searchProductType.selectedIndex].value;
	        // Overwrite response definition
	        productTypeId = searchProductTypeId;
	    }

		{
		    DataBinding = true;
		    Enlighten.CablePrice.Administration.Services.ProductService.SelectCategoriesByType(productTypeId, SearchCatSuccess, SearchFailed);
		    Enlighten.CablePrice.Administration.Services.ProductService.SelectMakesByType(productTypeId, SearchMakeSuccess, SearchFailed);
		}
	}
}

