// ***edit with caution!! shared by redball sites***


var consoleOK = false;
if (typeof console != "undefined") { // safari, firebug  
    if (typeof console.debug != "undefined") { // firebug  
        consoleOK = true;
    } 
}

                
// sendSearchRedirect2()
// handles search submit
// action = original form action
// formId = name of the form
// searchTypeId = name of field used for type of search, use '' if there is none
function sendSearchRedirect2(host, action, formId, searchTypeId, oId, siteId) {
    
    //if (consoleOK) {console.log('sendSearchRedirect2');} 
    var searchForm = document.forms[formId];

    // put everything in a try catch just in case
    try {

        var qs = "?";
        var usrAction = "37"; // UE tracking, default = internal

        // if a search type is defined find set the user action id
        // else assume this is internal
        if (searchTypeId != '') {

            var sel = searchForm[searchTypeId].selectedIndex;
            var searchType = searchForm[searchTypeId].options[sel].value;
            
            if (searchType == 'nw' || searchType == 'http://cnet.search.com/search?chkpt=astg.cnet.fd.search.cnet') {usrAction = "98";} // all cnet
            if (searchType == 'wb' || searchType == 'http://www.search.com/search?chkpt=astg.cnet.fd.search.web') {usrAction = "97";} // the web
           
            // special handling for tips & tricks
            if (siteId == '39') {
                //set new action since this is set to javascript:void
                action = 'http://www.search.com/redirect';
                //append q as kw onto target url
                searchType += 'kw=' + escape(searchForm.q.value);
            }


            // special handling for downloads music
	        if (siteId == '32') {
	            action = '/3607-5_32-0.html';
	            if (searchType == '3608') {
			        action = '/3608-5_32-0.html';
			    }
			    if ( searchType == 'dl-20' || searchType == 'dl-2012' || searchType == 'nw' || searchType == 'wb' ) {
		                searchTypeId = 'tg'
		                action = '/3120-20_4-0.html';
			    }
            }

            // re-add search type to qs
            qs += searchTypeId + "=" + searchType + "&";

        }

        // the rest of the query args
        for (i=0; i<searchForm.childNodes.length; i++) {
            if (searchForm.childNodes[i].tagName == "INPUT") {
                if ((searchForm.childNodes[i].type == "text") || (searchForm.childNodes[i].type == "hidden")) {
                    qs += searchForm.childNodes[i].name + "=" + searchForm.childNodes[i].value + "&";
                }
            }
        }
        // trim off the last &
        qs = qs.substring(0, qs.length-1);

        // add the host if the action is relative
        var action = action.indexOf("http:") < 0 ? host + action : action;

        var destURL = action + escape(qs);
        var url = 'http://dw.com.com/redir?usraction=' + usrAction + '&oId=' + oId + '&siteId=' + siteId + '&destUrl=' + destURL;
        //if (consoleOK) {console.log(url);} else {alert(url);}
        window.location.href=url;

    } catch (e) {
        //if (consoleOK) {console.log('Exception caught, submitting directly. Error: %s',e);} 
        searchForm.action = action;
        searchForm.submit();

    }

}
