﻿//  Created: 08/04/2010
//  Author: Chris Caserta
//  Description: Javascript to handle the form submission of the google search box.
//

//search function for google search box, builds the query string according to google requirements
//and redirects the browser to the results page.
function search() {
    var q = document.getElementById('q').value;
    //strip leading and trailing white space
    q = q.replace(/^\s*/, "").replace(/\s*$/, "");
    if (q || q != "") {
        var cof = document.getElementById('cof').value;
        var cx = document.getElementById('cx').value;
        var ie = document.getElementById('ie').value;
        var GoogleSearchSite = document.getElementById('GoogleSearchSite');
        var GoogleSearchWeb = document.getElementById('GoogleSearchWeb');
        var sitesearch = "";
        if (GoogleSearchSite.checked==true) {
            sitesearch = "&as_sitesearch=" + encodeURIComponent(location.hostname);
        }
        //encode values and redirect to search page
        location.href = "/googlesearch.aspx?q=" + encodeURIComponent(q) + "&cof=" + encodeURIComponent(cof) + "&cx=" + encodeURIComponent(cx) + "&ie=" + encodeURIComponent(ie) + sitesearch;
    }
    //else { return false; }
}

// for setting the search term back in the textbox call this function on page load
//also remembers radio box selection
function setSearchTerm() {
    GoogleBrand();
    var q = document.getElementById('q');
    var regEx = new RegExp("([^?=&]+)(=([^&]*))?");
    //var m = regEx.exec(location.search);
    var GoogleSearchWeb = document.getElementById('GoogleSearchWeb');
    var qparam = getQueryVariable("q");
    if (qparam) {
        q.value = decodeURIComponent(qparam);
    }
    else {
        q.value = "";
    }
    //q.focus();

    var sitesearch = getQueryVariable("as_sitesearch");
    if ((!sitesearch) && qparam) {
        GoogleSearchWeb.checked = true;
    }
    return false;     
}

// to apply a search when enter is pressed and search box has focus
function onSearchEnter(e) {
    var characterCode
    if(e && e.which){ //if which property of event object is supported (NN4)
    e = e
    characterCode = e.which //character code is contained in NN4's which property
    }
    else{
    e = event
    characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    
    if (characterCode == 13) {
        if (document.activeElement && document.activeElement.id == 'q') {
            search(); return false;
        }
        else { return false; }
    }
    else { return false; }
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
}

//set watermark on google search box copied from googles script
function GoogleBrand() {
    var q = document.getElementById("q");
    if (q) {
        var n = navigator;
        var l = location;

        if (n.platform == 'Win32') {
            q.style.cssText = 'border: 1px solid #7e9db9; padding: 2px;';
        }

        if (window.history.navigationMode) {
            window.history.navigationMode('compatible');
        }
        var b = function() {
            if (q.value == '') {
                q.style.background = '#FFFFFF url(http:\x2F\x2Fwww.google.com\x2Fcse\x2Fintl\x2Fen\x2Fimages\x2Fgoogle_custom_search_watermark.gif) left no-repeat';
            }
        };
        var f = function() {
            q.style.background = '#ffffff';
        };
        q.onfocus = f;
        q.onblur = b;
        if (!/[&?]q=[^&]/.test(l.search)) {
            b();
        }
    }

}

window.onload = setSearchTerm;
