var searchButton_disabled;
/**
 * Called when the script is loaded for the first time.
 **/
function Search_Load()
{
	this.Search_Draw();
}

/**
 * Called when the search panel needs to be drawn.
 **/
function Search_Draw()
{
	var searchContainer = this.GetElementByID("SearchContainer");
	var output = "";
	
	output += "<form id=\"SearchForm\">";
	output += "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"font-size: 8pt\" width=\"250\">";
	output += "<tr>";
	output += "<td width=\"92\">White</td>";
	output += "<td>";
	output += "<input id=\"LastNameWhite\" onkeyup=\"Search_Validate()\" title=\"Please enter at least 3 characters\" style=\"width: 156px\" type=\"text\" />";
	output += "</td>";
	output += "</tr>";
	output += "<tr>";
	output += "<td width=\"92\">Black</td>";
	output += "<td>";
	output += "<input id=\"LastNameBlack\" onkeyup=\"Search_Validate()\" title=\"Please enter at least 3 characters\" style=\"width: 156px\" type=\"text\" />";
	output += "</td>";
	output += "</tr>";
	output += "<tr>";
	output += "<td width=\"92\">&nbsp;</td>";
	output += "<td align=\"left\">";
	output += "<input id=\"IgnoreColors\" onclick=\"Search_IgnoreColorsClicked()\" type=\"checkbox\" /> Ignore colors";
	output += "</td>";
	output += "</tr>";
	output += "<tr>";
	output += "<td width=\"92\">Place</td>";
	output += "<td>";
	output += "<input id=\"Place\" onkeyup=\"Search_Validate()\" title=\"Please enter at least 3 characters\" style=\"width: 156px\" type=\"text\" />";
	output += "</td>";
	output += "</tr>";
	output += "<tr>";
	output += "<td width=\"92\">Year (min/max)</td>";
	output += "<td>";
	output += "<input id=\"MinYear\" onkeyup=\"Search_Validate()\" title=\"Only in combination with lastname white, lastname black and place\" style=\"width: 74px\" type=\"text\" /> &nbsp; ";
	output += "<input id=\"MaxYear\" onkeyup=\"Search_Validate()\" title=\"Only in combination with lastname white, lastname black and place\" style=\"width: 73px\" type=\"text\" />";
	output += "</td>";
	output += "</tr>";
	output += "<tr>";
	output += "<td>&nbsp;</td>";
	output += "<td align=\"right\" style=\"padding-top: 5px;\"><img id=\"SearchButton\" onclick=\"Search_Execute()\" src=\"../Common/Images/SearchButton.gif\" class=\"imageButton\" alt=\"Search\" style=\"margin-right: 10px;\" /><img id=\"ClearButton\" onclick=\"Search_ClearForm()\" class=\"imageButton\" src=\"../Common/Images/ClearButton.gif\" alt=\"Clear\" /></td>";
	output += "</tr>";
	output += "</table>";
	output += "</form>";
	
	output += "<p>";
	output += "<strong>Note</strong>: If you search on a combination of fields, it will speed up the performance.";
	output += "<br><br><strong>Note</strong>: You can&nbsp;search&nbsp;faster&nbsp;and&nbsp;more&nbsp;accurately&nbsp;by&nbsp;adding&nbsp;the&nbsp;first&nbsp;name&nbsp;of a player,<br>e.g. Nikolic,Predrag";
	output += "</p>";
	
	searchContainer.innerHTML = output;
	
	this.Search_Validate();
}

/**
 * Clears the form and disables the Search button.
 **/
function Search_ClearForm()
{
	var searchForm = this.GetElementByID("SearchForm");
	var searchButton = this.GetElementByID("SearchButton");
	
	searchForm.reset();
	this.Search_DisableSearchButton();
}

/**
 * Enables/disables the lastNameBlack textbox depending on the ignoreColors checked in the form.
 **/	
function Search_IgnoreColorsClicked()
{
	var lastNameBlack = this.GetElementByID("LastNameBlack");

	//lastNameBlack.disabled = this.GetElementByID("IgnoreColors").checked;
	Search_Validate();
}

/**
 * Enables/disables the search button depending on the values in the form.
 **/	
function Search_Validate()
{
	var ignoreColors = this.GetElementByID("IgnoreColors").checked;
	var lastNameWhite = this.GetElementByID("LastNameWhite").value;
	var lastNameBlack = this.GetElementByID("LastNameBlack").value;
	var place = this.GetElementByID("Place").value;
	var minYear = this.GetElementByID("MinYear").value;
	var maxYear = this.GetElementByID("MaxYear").value;
	
	// Check if all fields have correct values
	if ((lastNameWhite.length > 0 && lastNameWhite.length < 3) ||
		((lastNameBlack.length > 0 && lastNameBlack.length < 3) && !ignoreColors) ||
		(place.length > 0 && place.length < 3) ||
		(!this.Search_ValidateYear(minYear) && minYear.length != 0) || 
		(!this.Search_ValidateYear(maxYear) && maxYear.length != 0))
	{
		this.Search_DisableSearchButton();
		return;
	}
			
	// If names and place are empty, no search is possible
	if ((lastNameWhite.length == 0) && 
		((lastNameBlack.length == 0) || ignoreColors) && 
		(place.length == 0))
	{
		this.Search_DisableSearchButton();
		return;
	}
			
	this.Search_EnableSearchButton();

}

/**
 * Disables the search button
 **/
function Search_DisableSearchButton()
{
	var searchButton = this.GetElementByID("SearchButton");

	searchButton.disabled = searchButton_disabled = true;
	searchButton.src = "../Common/Images/SearchButton_disabled.gif";
	searchButton.className = "imageButtonDisabled";
}

/**
 * Enables the search button
 **/
function Search_EnableSearchButton()
{
	var searchButton = this.GetElementByID("SearchButton");

	searchButton.disabled = searchButton_disabled = false;
	searchButton.src = "../Common/Images/SearchButton.gif";
	searchButton.className = "imageButton";
}

/**
 * Checks if a value is a valid year.
 **/
function Search_ValidateYear(value)	
{
	if (value.length != 4) return false;
			
	var numericValues = "0123456789";
	var i = 0;
			
	for (i = 0; i < value.length; i++)
	{
		if (numericValues.indexOf(value.charAt(i)) == -1)
			return false;
	}
			
	return true;
}

/**
 * Execute the actual search.
 **/		
function Search_Execute()
{
	if (this.searchButton_disabled) return;
	var infoPanel = this.GetElementByID("NicBaseInfoPanel");
	if(infoPanel.style.display == "")
	{
		infoPanel.style.display = "none";
	}
	////////////////////////////////////////////////////////////////////
	var lastNameWhite = this.GetElementByID("LastNameWhite");
	var lastNameBlack = this.GetElementByID("LastNameBlack");
	var place = this.GetElementByID("Place");
	var minYear = this.GetElementByID("MinYear");
	var maxYear = this.GetElementByID("MaxYear");
	var ignoreColors = this.GetElementByID("IgnoreColors");
	if(minYear.value == "") 
	{
		minYear.value = maxYear.value;
	}
	if(maxYear.value == "") 
	{
		maxYear.value = minYear.value;
	}

	Board_SetCommandParameter("LastNameWhite", lastNameWhite.value);
	Board_SetCommandParameter("LastNameBlack", lastNameBlack.value);
	Board_SetCommandParameter("Place", place.value);
	Board_SetCommandParameter("MinYear", minYear.value);
	Board_SetCommandParameter("MaxYear", maxYear.value);
	Board_SetCommandParameter("IgnoreColors", ignoreColors.checked ? "True" : "False");
	Board_ExecuteCommand("SearchGames");			
}
//
function Search_GetGame(value)
{
	var searchFields = value.split('#');
	Board_SetCommandParameter("LastNameWhite",searchFields[0]);
	Board_SetCommandParameter("LastNameBlack",searchFields[1]);
	Board_SetCommandParameter("Place",searchFields[2]);
	Board_SetCommandParameter("MinYear",searchFields[3]);
	Board_SetCommandParameter("MaxYear",searchFields[3]);
	Board_SetCommandParameter("IgnoreColors","False");
	Board_ExecuteCommand("SearchGames");
}
