﻿function externalSearchRedirect(baseurl, cntrlId, newWindow)
{
    //create the full external search url
    var query = $('#' + cntrlId).val();
    var url = baseurl + query;
    //log in google analytics
    if(_gaq != null)
        _gaq.push(['_trackEvent', 'Exit', url]); 

    if(newWindow)
    {
        //open a new window
        var newWindow = window.open(url, 'externalSearchWindow');
    }
    else
    {
        //redirect the page
        window.location = url;
    }
}

function externalSearchKeyPress(e, baseurl, cntrlId, newWindow)
{
    //if the user hits enter on the external search box do the search
    if(e.keyCode == 13)
    {
         externalSearchRedirect(baseurl, cntrlId, newWindow);
    }
}

//for any external links add a function call to their click event
$(document).ready(function(){
    var host = location.host;
    $('a').not('[hostname = ""],[hostname = '+ host + ']').click(externalLinkLog);
});

//log an exit event in google analytics
function externalLinkLog()
{
    var url = $(this).attr('href');    
    if(_gaq != null)
        _gaq.push(['_trackEvent', 'Exit', url]); 
}
