

//***********************************
//***********************************
window.onload = function()
{
	setSearchBy($('#search_by').val());
};



//***********************************
//***********************************
function setSearchBy(val)
{
	if(val == 'online_offline_player')
	{
		type = 'oop';
		$("#search_form").css("background-image", "url(/images/search/form_p_bkgd.gif)");
		$("#col_matching").css("display", "block");
		$("#col_matching").css("left", "215px");
		$("#col_using").css("display", "none");
		$("#col_playing").css("display", "none");
		$("#col_location").css("display", "none");
		$("#col_go").css("left", "404px");
	}
	else if(val == 'profile')
	{
		type = 'pf';
		$("#search_form").css("background-image", "url(/images/search/form_pf_bkgd.gif)");
		$("#col_matching").css("display", "block");
		$("#col_matching").css("left", "402px");
		$("#col_using").css("display", "block");
		$("#col_using").css("left", "215px");
		$("#col_playing").css("display", "none");
		$("#col_location").css("display", "block");
		$("#col_location").css("left", "590px");
		$("#col_go").css("left", "780px");
	}
	else
	{
		type = 's';
		$("#search_form").css("background-image", "url(/images/search/form_bkgd.gif)");
		$("#col_matching").css("display", "block");
		$("#col_matching").css("left", "215px");
		$("#col_using").css("display", "none");
		$("#col_playing").css("left", "402px");
		$("#col_playing").css("display", "block");
		$("#col_location").css("left", "590px");
		$("#col_location").css("display", "block");
		$("#col_go").css("left", "780px");
	}
	//LOCATIONS
	if (val != 'online_offline_player')
	{
		var found = false;
		if ((val == 'clan') || (val == 'profile'))
		{
			var selectedValue = document.getElementById('loc').value;
			$("#loc").html($("#loc_all").html());
			var n1 = document.getElementById('loc');
			for (var i = 0; i < n1.options.length; i++)
			{
				if (n1.options[i].value == selectedValue)
				{
					n1.options[i].selected = true;
					found = true;
					break;
				}
			}
		}
		else
		{
			var selectedValue = document.getElementById('loc').value;
			$("#loc").html($("#loc_visdist").html());
			var n1 = document.getElementById('loc');
			for (var i = 0; i < n1.options.length; i++)
			{
				if (n1.options[i].value == selectedValue)
				{
					n1.options[i].selected = true;
					found = true;
					break;
				}
			}
		}
		if (!found)
		{
			document.getElementById('loc').options[0].selected = true;
		}
	}
}

//***********************************
//***********************************
function validate()
{
	//VALIDATE
	if ($('#search_by').val() == 'online_offline_player')
	{
		if ($('#query').val().length < 3)
		{
			alert('Your query must be at least 3 characters in length.');
			return false;
		}
	}
	//GET BASE URL
	var tempURL = window.location.toString();
	var splitPos = tempURL.indexOf('://');
	if (splitPos == -1)
	{
		alert('Invalid URL');
		return false;
	}
	tempURL = tempURL.substring(splitPos+3);
	var splitPos = tempURL.indexOf('/');
	if (splitPos != -1)
	{
		tempURL = tempURL.substring(0, splitPos);
	}
	tempURL = 'http://' + tempURL + "/search/";


	//BUILD URL
	var searchBy	= document.getElementById("search_by").value
	var game		= document.getElementById("game").value;
	var loc			= document.getElementById("loc").value;

	if (searchBy == "name_ip" || searchBy == "map" || searchBy == "online_player" || searchBy == "clan")
	{
		//GAME/LOCATION
		if (game != "_all" && loc != "_all")
			tempURL = tempURL + game + "/" + loc + "/";
		else if (game != "_all")
			tempURL = tempURL + game + "/";
		else if (loc != "_all")
			tempURL = tempURL + "_all/" + loc + "/";
		var qvarHit = false;
		//SEARCH BY
		if (searchBy == "name_ip")
		{
			//default
		}
		else
		{
			tempURL = tempURL + "?search_by=" + document.getElementById("search_by").value;
			qvarHit = true;
		}
		//QUERY
		if (document.getElementById("query").value == "")
		{
			//empty
		}
		else if (qvarHit)
		{
			tempURL = tempURL + "&query=" + encodeURIComponent(document.getElementById("query").value);
		}
		else
		{
			tempURL = tempURL + "?query=" + encodeURIComponent(document.getElementById("query").value);
			qvarHit = true;
		}
		//SORT
		if (document.getElementById("sort").value == "")
		{
			//empty
		}
		else if (document.getElementById("sort").value == "rank" && document.getElementById("order").value == "asc")
		{
			//default
		}
		else if (qvarHit)
		{
			tempURL = tempURL + "&sort=" + document.getElementById("sort").value + "&order=" + document.getElementById("order").value;
		}
		else
		{
			tempURL = tempURL + "?sort=" + document.getElementById("sort").value + "&order=" + document.getElementById("order").value;
			qvarHit = true;
		}
		window.location = tempURL;
		return false;
	}
	else if (searchBy == "online_offline_player")
	{

		//SEARCH BY
		tempURL = tempURL + "?search_by=" + document.getElementById("search_by").value;
		//QUERY
		if (document.getElementById("query").value == "")
		{
			//empty
		}
		else
		{
			tempURL = tempURL + "&query=" + encodeURIComponent(document.getElementById("query").value);
		}
		//SORT
		if (document.getElementById("sort").value == "")
		{
			//empty
		}
		else if (document.getElementById("sort").value == "name" && document.getElementById("order").value == "asc")
		{
			//default
		}
		else
		{
			tempURL = tempURL + "&sort=" + document.getElementById("sort").value + "&order=" + document.getElementById("order").value;
		}
		window.location = tempURL;
		return false;
	}
	else if (searchBy == "profile")
	{
		//GAME/LOCATION
		if (loc != "_all")
			tempURL = tempURL + "_all/" + loc + "/";
		//SEARCH BY
		tempURL = tempURL + "?search_by=" + document.getElementById("search_by").value;
		//USING
		tempURL = tempURL + "&using=" + document.getElementById("using").value;
		//QUERY
		if (document.getElementById("query").value == "")
		{
			//empty
		}
		else
		{
			tempURL = tempURL + "&query=" + encodeURIComponent(document.getElementById("query").value);
		}
		//SORT
		if (document.getElementById("sort").value == "")
		{
			//empty
		}
		else if (document.getElementById("sort").value == "username" && document.getElementById("order").value == "asc")
		{
			//default
		}
		else
		{
			tempURL = tempURL + "&sort=" + document.getElementById("sort").value + "&order=" + document.getElementById("order").value;
		}
		window.location = tempURL;
		return false;
	}
	return false;
}


//***********************************
//***********************************
function installSearchEngine()
{
	var noncompat_msg = "The browser you are using does not have compatible search engine support. GameTracker in-browser search is currently only supported by Firefox 1.5+ and MSIE 7.";
	if (window.external && ("AddSearchProvider" in window.external))
	{
		// Firefox 2 and IE 7, OpenSearch
		window.external.AddSearchProvider("http://www.gametracker.com/xml/search/opensearch/gt_server.xml");
	}
	else if (window.sidebar && ("addSearchEngine" in window.sidebar))
	{
		// Firefox <= 1.5, Sherlock
		window.sidebar.addSearchEngine("http://www.gametracker.com/search/sherlock/gt_server.src", "http://www.gametracker.com/search/sherlock/gt_server.gif", "GameTracker.com Server Search", "");
	}
	else
	{
		// No search engine support (IE 6, Opera, etc).
		alert(noncompat_msg);
	}
}