    
$(document).ready(function() {
    
    sruForm = document.SRUForm;
    
    max = $("#maxIndex").val()

    $('#resetSearch').click(function(){
            $("input:text[value!='']").removeClass('filled');
            clearForm();
    });
    //toggle searchInfo
    $('#toggleSearchInfo').click(function(){
        $('#searchInfo').css('visibility')=='hidden' ? $('#searchInfo').css('visibility','visible') : $('#searchInfo').css('visibility','hidden');
        $('#showSearchInfo').toggle();
        $('#hideSearchInfo').toggle();
    });
    
    // focus first / 1st filled input
    $("form[name='SRUForm'] input:text[value!='']").length > 0 ? $("input:text[value!='']").eq(0).focus() : $("input[name='term1']").focus();
    
    //mark filled input-fields
    $("input:text[value!='']").addClass('filled');
    $("input:text").blur(function(){
        $("input:text[value='']").removeClass('filled')
        $("input:text[value!='']").addClass('filled')
    })
    
    $(".checkUncheckAll").click(function() {
        var checked = $(this).hasClass("checked");
        if(checked) {
            $(this).removeClass("checked");
            $("#setCollections input").removeAttr("checked")
        } else {
            $(this).addClass("checked");
            $("#setCollections input[type='checkbox']").attr("checked", 'checked');
        }
    });
    

});

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
// reset sruForm
function clearForm() {
    for (var idx = 1; idx <= max; idx++) {
        $("#term" + idx).attr('value','');
    }
    if($("#vlFulltext").length) {
        $("#vlFulltext").attr('value','');
    }    
    if($("#vlStructures").length) {
        $("#vlStructures").attr('value','');
    }
    if($("#searchLanguage").length) {
        $("#searchLanguage option:selected").removeAttr('selected');
    }
    if($("#searchDomain").length) {
        $("#searchDomain option:selected").removeAttr('selected');
    }
    if($("#searchDomain").length) {
        $("#searchDomain option:selected").removeAttr('selected');
    }
    if($('div#setCollections input').length) {
        $("#setCollections input").removeClass("checked");
        $("#setCollections input").removeAttr("checked")
    }
    if($("#sortBy option:selected").length) {
        $("#sortBy option:selected").removeAttr('selected');
    }
    if($("#sortOrder option:selected").length) {
        $("#sortOrder option:selected").removeAttr('selected');
    }
    if($("#maximumRecords option:selected").length) {
        $("#maximumRecords option:selected").removeAttr('selected');
    }
}
// munge sruFormFields to CQL query
function mungeForm() {
    try {
        cql = "";
        // Step through elements in form to create CQL
        for (var idx = 1; idx <= max; idx++) {
            term = trim($("#term" + idx).val());
            field = $("#index" + idx).val()
            if (term != '') {
                if(cql != '') {
                    cql += " " + $("#bool" + idx).val() + " "; 
                }
                
                if (term.indexOf(' ') > -1){
                    if (field == 'dc.identifier' || field == 'vl.signature' || field == 'dc.date'){
                        cql += field + '="' + term + '"';
                    } else {
                        cql += field + "=(" + term + ')';
                    }
                } else {
                    cql += field + "=" + term;
                }
            }
        }
        
        if($("#vlFulltext").length) {
            var vlFulltext = $("#vlFulltext").attr('value');
            if(vlFulltext && vlFulltext != '') {
                if (cql != '') {
                    cql += ' and '
                }
                if (vlFulltext.indexOf(' ') > -1){
                    cql += 'vl.fulltext=(' + vlFulltext + ')';
                } else {
                    cql += 'vl.fulltext=' + vlFulltext;
                }
            }
        }
        
        if($("#vlStructures").length) {
            var vlStructures = $("#vlStructures").attr('value');
            if(vlStructures && vlStructures != '') {
                if (cql != '') {
                    cql += ' and '
                }
                if (vlStructures.indexOf(' ') > -1){
                    cql += 'vls.structures=(' + vlStructures + ')';
                } else {
                    cql += 'vls.structures=' + vlStructures;
                }
            }
        }
        
        //up till now we should have something searchable, the rest is optional
        alertIfEmpty(cql);
        if(!cql) return false;
        
        if($("#searchLanguage").length) {
            var searchLanguage = $("#searchLanguage option:selected").attr('value');
            if(searchLanguage && searchLanguage != '') {
                cql += ' and dc.language=' + searchLanguage;
            }
        }
        
        var domain = ''
        if($("#searchDomain").length) {
            domain = $("#searchDomain option:selected").attr('value');
        } else if ($(".domain#fulldata").attr('value') != 'true'){
            domain = $(".domain#name").attr('value');
        }
        
        if(domain && domain != '') {
            cql += ' and vl.domain=' + domain;
        }
            
        
        
        
        //now handle collections
        if($('div#setCollections input').length) {
            collections = []
            $('div#setCollections input').each(function(){
                inputEl = $(this);
                if (inputEl.attr('checked')){
                    collections.push(inputEl.attr('value'));
                }
            })
            if (collections.length){
                cql += ' and vl.collections=('
                for (var idx = 0; idx < collections.length; idx++) {
                    cql += collections[idx];
                    if (idx < collections.length - 1){
                        cql += ' or ';
                    }
                }
                cql+= ')'
            }
        }
        
        if($("#sortBy option:selected").length) {
            sortBy = $("#sortBy option:selected").attr('value');
            cql += ' sortBy ' + sortBy;
            if($("#sortBy option:selected").length) {
                sortOrder = $("#sortOrder option:selected").attr('value');
                cql += '/' + sortOrder;
            }
        }


        sruForm.query.value = cql;
    
    } catch(e){
        alert(e)
        return false
    }
}