// adv_search.js
//
//  Functions to ensure that form state is not lost when user
//  chooses topics in the advanced search page.

// set up a global variable for the cookie.  Note: the cookie ALWAYS
// returns string values, even if you send in numbers or booleans.  This can
// cause confusion if you try to use the value as-is in the DOM, since it 
// expects booleans, numbers, etc.

var formdata = new Cookie(document, 'adv_search_form', null, '/');


function init_form() {
    if ( !formdata.load() ) {
        // set up defaults.  
        formdata.query = '';
        
        formdata.bibfield = '0';
        formdata.namefield = '0';
        formdata.contentfield = '1';
        formdata.authorfield = '0';
        
        set_radio_cookie('standard');
        
        formdata.precision = '0';
        formdata.distance = '0';
    }
    formdata.store();
        
    // query string
    document.getElementById('querystr').value = formdata.query;
    
    // checkboxes
    document.getElementById('bibfield').checked = Number(formdata.bibfield);
    document.getElementById('namefield').checked = Number(formdata.namefield);
    document.getElementById('contentfield').checked = Number(formdata.contentfield);
    document.getElementById('authorfield').checked = Number(formdata.authorfield);
    
    // type radio buttons
    document.getElementById('standard').checked = Number(formdata.standard);
    document.getElementById('pattern').checked = Number(formdata.pattern);
    document.getElementById('adjacency').checked = Number(formdata.adjacency);
    
    // dropdowns
    document.getElementById('pattern-precision').selectedIndex = Number(formdata.precision);
    document.getElementById('adjacency-distance').selectedIndex = Number(formdata.distance);

    // focus initially on query string
    document.getElementById('querystr').focus();
    
}

function set_form_cookie( name, value ) {
    formdata[name] = value;
    formdata.store();
}

function set_radio_cookie( name ) {
    
    // when a value is set for a radio button (onclick)
    // unset all the others.  This does not happen automatically.
    
    switch ( name ) {
        case 'standard':
            formdata.standard = '1';
            formdata.pattern = '0';
            formdata.adjacency = '0';
            break;
        case 'pattern':
            formdata.standard = '0';
            formdata.pattern = '1';
            formdata.adjacency = '0';
            break;
        case 'adjacency':
            formdata.standard = '0';
            formdata.pattern = '0';
            formdata.adjacency = '1';
    }
    formdata.store();
}

document.observe("dom:loaded",init_form);    


function validate_advsearch_form( advsearch_form ){
    if (advsearch_form.query.value == ''){
        return false;
    }else{
        return true;
    }
}







