function fetchCategories(elem){
    pars = "id=" + elem.options[elem.selectedIndex].value;
    new Ajax.Request( 
    'ajax_fetch_categories',
    {onLoading:function(request){processingFetchCategories()},
     onComplete:handleFetchCategoriesRequest,
     parameters:pars,asynchronous:true});
}
  
function processingFetchCategories() {
    $('progress').style.display = "";
}

function handleFetchCategoriesRequest(request) {
    $('progress').style.display = "none";
    elem = document.getElementById("titleCategories");
    elem.options.length = 0;
    values = request.responseText.split("<|>");
    for (i = 0; i < values.length; i++){
        params = values[i].split("<->");
        if (params.length == 2){
            try{
                opt = new Option(params[1], params[0]);
                elem.add(opt, null);
            }catch (e){
                elem.add(opt, -1);
            }
        }
    }
}  


function searchByForm(f){
	market = f.market.options[f.market.selectedIndex].value;
	type = f.type.options[f.type.selectedIndex].value;
	category = f.category.options[f.category.selectedIndex].value;
	
	if (market == ""){
		alert("Please, choose a market");
		return;
	}
	if (type == ""){
		alert("Please, choose a type");
		return;
	}
	if (category == ""){
		category = "browse";
	}
	
	location.href="/titles/browse/" + market + "/" + type + "/" + category + "/";
	
}

function checkKeywordForm(f){
	empty = /^\s*$/;
    if (empty.test(f.keyword.value) || f.keyword.value.length <= 3){
        alert("Keyword must not be empty and at least 4 characters length!");
        return false;
    }
    return true;
}