

function switchSearchEngine(e) {
    var sb = $('searchbox_google');
    
    var chosenSE = this.getValue();

    if(chosenSE == "all_ncee")
    {
        sb['cx'].value = $('a').getValue();
        sb.action = "http://www.epa.gov/children/nceesearch/3/results.html";
    }
    else if(chosenSE == "other")
    {
        sb['cx'].value = $('s').getValue();
        sb.action = "http://www.epa.gov/children/nceesearch/3/results.html";
    }
    else
    {
        sb['cx'].value = "";
        sb.action = "http://www.google.com/search";
    }
}

document.observe('dom:loaded', function() {
    var sb = $('searchbox_google');
    
    var urlVars = window.location.href.toQueryParams();
    
    if(typeof(urlVars.cx) != 'undefined')
    {
        // Set searchbox_google's cx value
        sb['cx'].value = urlVars.cx;
        if(typeof(urlVars.a) != 'undefined') sb['a'].value = urlVars.a;
        if(typeof(urlVars.s) != 'undefined') sb['s'].value = urlVars.s;
        
        var otherLabel = $$('label[for="other"]').reduce();
        if(typeof(urlVars.n) != 'undefined') {
            sb['n'].value = urlVars.n;
		    otherLabel.update(urlVars.n.replace(/\+/g, ' '));
        } else {
            $('other').hide();
            otherLabel.hide();
        }
        
        if(urlVars.cx == urlVars.a) {
            $('all_ncee').checked = true;
        } else
        if(urlVars.cx == urlVars.s) {
            $('other').checked = true;
        }
    }

    // Add an observer to the radio buttons
    sb.getInputs('radio', 'whichSE').invoke('observe', 'click', switchSearchEngine);
});

