function _isInteger(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}
function search_reset() {
    var url =  root + "/" + currentSection + "/";
    if ( currentCategory != null && currentCategory.length > 0 )
        url = url + "category/" + currentCategory + "/";
    document.location.href = url;
}

function search_doit() {

    if ( ! auth_isLoggedIn() ) {
        alert('Please login to use the filters!');
        return false;
    }

    //var attributeIDlist = ( attributeIDlist == null ? null : attributeIDlist );
    //var extraSearchFields = ( extraSearchFields == null ? null : extraSearchFields );
    //var topCategory = ( topCategory == null ? "" : topCategory );
    var cat = "";
    if ( $('search_category_any') != null && $('search_category_any').checked )
        cat = topCategory;
    else
        cat = currentCategory;
    var url = root + "/" + currentSection + "/";
    if ( cat != null && cat.length > 0 )
        url = url + "category/" + cat + "/";
    
    //alert( 'root is ' + root );

    // Free mode, only for downloads
    var freemode_subscriber = $('freemode_subscriber');
    var freemode_free = $('freemode_free');
    if ( freemode_subscriber != null && freemode_subscriber.className == "on" )
        url = url + "free/0/";
    else if ( freemode_free != null && freemode_free.className == "on" )
        url = url + "free/1/";

    // Expansion packs filter... only for downloads, does not work with generic way
    var expfilter = $('search_expansionpacksfilter');
    if ( expfilter != null && expfilter.checked )
        url = url + "emask/" + getExpansionPacks() + "/";
    
       
    if ( attributeIDlist != null /*&& attributeIDlist.length > 1 */) {
        var attributes = "";
        var attributeIDarr = attributeIDlist.split(",");
        for ( i = 0; i < attributeIDarr.length; i++ ) {
            attr = $('attr_' + attributeIDarr[i]);
            if ( attr != null ) {
                if ( attr.selectedIndex > 0 ) {
                    if ( attributes.length > 0 )
                        attributes = attributes + ",";
                    attributes = attributes + "attr" + attributeIDarr[i] + "_" + attr.options[attr.selectedIndex].value;
                } else if ( attr.value.length > 0 ) {
                    if ( attributes.length > 0 )
                        attributes = attributes + ",";
                    attributes = attributes + "attr" + attributeIDarr[i] + "_" + attr.value;
                }
            }
        }
        if ( attributes.length > 0 )
            url = url + "attributes/" + attributes + "/";
    }
    
    if ( extraSearchFields != null && extraSearchFields.length > 0 ) {
        //var searchFields = "";
        var fieldsArr = extraSearchFields.split(',');    
        for ( i = 0; i < fieldsArr.length; i++ ) {
            field = $('search_' + fieldsArr[i]);
            if ( field != null ) {
                if ( field.selectedIndex > 0 ) // Is checkbox
                    url = url + fieldsArr[i] + "/" + field.options[field.selectedIndex].value + "/";
                else if ( field.type != null && field.type.toLowerCase() == "checkbox" && field.checked ) // 
                    url = url + fieldsArr[i] + "/" + field.value + "/";
                else if ( field.type != null && field.type.toLowerCase() == "text" && field.value.length > 0 )
                    url = url + fieldsArr[i] + "/" + field.value + "/";
            } else
                alert(fieldsArr[i] + ' not found');       
        }
    }

    var searchPhrase = $('search_searchfield').value;
    if ( searchPhrase != null && searchPhrase.length > 0 ) {
        // Trim it from spaces
        searchPhrase = searchPhrase.replace(/^\s+|\s+$/g, '');
        // Remove quotes if just a single word
        if ( searchPhrase.indexOf(' ') == -1 && ( searchPhrase.indexOf('&quot;') > -1 || searchPhrase.indexOf('"') > -1 ) )
            searchPhrase = searchPhrase.replace(/"/g,'');
        if ( searchPhrase.length < 3 ) {
            alert('The search phrase must a least contain 3 characters!');
            return false;
        }
        // Check if an integer, then it might be the itemid
        if ( searchPhrase.length > 3 && _isInteger(searchPhrase) ) {
            if ( confirm('You have entered a number. Is this the ID of the item you are looking for?') ) {
                url = root + "/" + currentSection.replace(/browse/g,'details') + "/category/" + topCategory + "/id/" + searchPhrase + "/";
                document.location.href = url;
                return(false);
            }
        }
        url = url + "search/" + encodeURI(searchPhrase.toLowerCase()) + "/";
    }    
        
    var orderSelect = $('search_order');
    if ( orderSelect != null && orderSelect.selectedIndex > 0 && orderSelect.options[orderSelect.selectedIndex].value != "" ) {
        url = url + "order/" + orderSelect.options[orderSelect.selectedIndex].value + "/";
    }
    var viewSelect = $('search_view');
    if ( viewSelect != null && viewSelect.selectedIndex > 0 && viewSelect.options[viewSelect.selectedIndex].value != "" ) {
        url = url + "view/" + viewSelect.options[viewSelect.selectedIndex].value + "/";
    }

    
    document.location.href = url;
    return(false);
}