// requires jQuery
// add to toprail.js
jQuery(document).ready(function() {
	/**
	 * Switches toprail tabs to show selected search form
	 */
	jQuery("#ToprailSearchTab1,#ToprailSearchTab2").click(function() {
		jQuery(this).addClass("search_tab_on");
		jQuery(jQuery(this).children()).removeClass("mo");
		jQuery(jQuery(this).children()).addClass("tab");
		if (jQuery(this).attr("id")=="ToprailSearchTab1") {
			jQuery("#ToprailSearchTab2").removeClass("search_tab_on");
			jQuery("#ToprailSearchTab2 > *").removeClass("tab");
			jQuery("#Toprail_BusinessSearch").css("display", "none");
			jQuery("#Toprail_Search").css("display", "inline");
		} else {
			jQuery("#ToprailSearchTab1").removeClass("search_tab_on");
			jQuery("#ToprailSearchTab1 > *").removeClass("tab");
			jQuery("#Toprail_BusinessSearch").css("display", "inline");
			jQuery("#Toprail_Search").css("display", "none");
		}
	});
	/**
	 * Handles toprail search tabs hover states
	 */
	jQuery("#ToprailSearchTab1,#ToprailSearchTab2").hover(
		function(e) {
			if (jQuery(this).hasClass("search_tab_on")==false) {
				jQuery(jQuery(this).children()).addClass("mo");
			}
		},
		function(e) {
			if (jQuery(this).hasClass("search_tab_on")==false) {
				jQuery(jQuery(this).children()).removeClass("mo");
			}
		}
	);
});

// add this to MSIE 6 code in toprail.js
jQuery(document).ready(function() {
	jQuery(".imageGo_toprail").hover(
		function () { jQuery(this).addClass("imageGo_toprail_mo"); },
		function () { jQuery(this).removeClass("imageGo_toprail_mo"); }
	);
	jQuery(".imageGo").hover(
		function () { jQuery(this).addClass("imageGo_mo"); },
		function () { jQuery(this).removeClass("imageGo_mo"); }
	);
});

// ----------------------------------------------------------------------------------------------------
// replace var SiteSearchFieldDefault and functions prepSiteSearch and filterSiteSearch with this code
// plus add the two new functions, isDefaultSearchValue and checkSearchValues
// ----------------------------------------------------------------------------------------------------
/* Search field functions */
var SiteSearchFields = [];
SiteSearchFieldClass = function (obj) {
	this.id = obj.id;
	this.defaultVal = obj.value;
}

/**
 * isDefaultSearchValue
 * @param objInput {object} The text field to test if the value is the default value
 * @returns {json} idMatch:boolean isDefault:boolean index:int
 */
function isDefaultSearchValue (objInput)
{
	var json = {idMatch:false, isDefault:false, index:0};
	for (var i=0; i<SiteSearchFields.length; i++) {
		if (SiteSearchFields[i].id == objInput.id) {
			json.idMatch = true;
			if (SiteSearchFields[i].defaultVal == objInput.value || objInput.value=='') {
				json.isDefault = true;
			}
			break;
		}
	}
	json.index = i;
	return json;
}

/**
 * checkSearchValues
 * @param frm {object} The form object whose search values should be checked and sets default values to ''
 */
function checkSearchValues (frm)
{
	var inputs = jQuery("form[id='"+frm.id+"'] :text");
	inputs.each(function () {
		var test = isDefaultSearchValue(this);
		if (test.isDefault || !test.idMatch)
			this.value = '';
	});
}

/**
 * The prepSiteSearch function clears the (site) search field or sets it to msg.
 * prepSiteSearch takes a DOM object as well as a boolean value for arguments.
 * objSearchField is the site search field (object) and fieldHasFocus is true/false.
 * SK (2008-11-25) - fieldHasFocus appears to be a "deprecated" arg, but I left it in
 * 					   since it is passed in many places where the function is called.
 */
function prepSiteSearch (objSearchField,fieldHasFocus) 
{
	var searchTest = isDefaultSearchValue(objSearchField);
	var i = searchTest.index;
	if (!searchTest.idMatch) SiteSearchFields[i] = new SiteSearchFieldClass(objSearchField);
	if (objSearchField.value == SiteSearchFields[i].defaultVal) {
		objSearchField.value = "";
	} else if (objSearchField.value.search(/[0-9A-z]+/g) == -1) {
		objSearchField.value = SiteSearchFields[i].defaultVal;
	}
}

/**
 * The filterSiteSearch function redirects a search to a specific page if a keyword
 * starts with bl and is followed by 5-8 numbers. The redirect is based on the site 
 * argument and the numerical part of the value of the field argument.
 */
function filterSiteSearch ( site, field ) 
{
	site = site.replace(/http:\/\/www\./, "");
	// Test keywords for a client-side redirect based on user not entering a keyword
	var test = isDefaultSearchValue(field);
	if (test.isDefault || !test.idMatch) { location.href = "http://search."+ site +"/"; return false; }
	var regExp = /\b[Bb][Ll]([0-9]{5,8})\b/g;
	if (!regExp.test(field.value)) return true;
	// Test keywords for a client-side redirect based on the format: bl12345[..8].
	location.href = "http://businessfinder."+ site +"/BL/Profile.aspx?adv="+ RegExp.$1; return false;
}

