/* Search.bz JavaScript Module
   Copyright (C) 2000 Learnology Ltd
   All rights reserved

   String and field management functions reproduced with the permission of Mark
   Lim. */



/* String Management Functions */

function Separator(TargetCharacter)

/* Determine if the character specified by TargetCharacter is indeed a separator
   or white-space character. */


{
   var ReturnValue;

   if (TargetCharacter == "\t")
	 ReturnValue = true;
   else if (TargetCharacter == "\n")
	 ReturnValue = true;
   else if (TargetCharacter == "\f")
	 ReturnValue = true;
   else if (TargetCharacter == "\r")
	 ReturnValue = true;
   else if (TargetCharacter == " ")
	 ReturnValue = true;
   else
	 ReturnValue = false;

   return ReturnValue;
}



function Trim(TargetString)

/* Return TargetString trimmed of all leading and trailing white-space. */


{
   var LeftIndex  = 0;
   var RightIndex = TargetString.length - 1;

   var TargetCharacter;

   TargetCharacter = TargetString.charAt(LeftIndex);

   while (TargetCharacter != "" && Separator(TargetCharacter))
	 {
		LeftIndex++;
		TargetCharacter = TargetString.charAt(LeftIndex);
	 }

   TargetCharacter = TargetString.charAt(RightIndex);

   while (TargetCharacter != "" && Separator(TargetCharacter))
	 {
		RightIndex--;
		TargetCharacter = TargetString.charAt(RightIndex);
	 }
   RightIndex++;

   if (LeftIndex < RightIndex)
	 return TargetString.substring(LeftIndex, RightIndex);
   else
	 return "";
}



function Collapse(SourceString)

/* Return SourceString with all runs of white-space collapsed to single
   characters. */


{
   var TargetString = "";

   var TargetCharacter;
   PreviousCharacter = new Array(2);

   for (var Index = 0; Index < SourceString.length; Index++)
	 {
		TargetCharacter = SourceString.charAt(Index);

		if (Separator(TargetCharacter))
		  {
			 if (PreviousCharacter[0] == "")
			   {
				  TargetString += TargetCharacter;
				  PreviousCharacter[0] = TargetCharacter;
			   }
			 else
			   {
				  if (PreviousCharacter[0] == "\r" && TargetCharacter == "\n")
					{
					   if (PreviousCharacter[1] == "")
						 {
							TargetString += TargetCharacter;
							PreviousCharacter[1] = TargetCharacter;
						 }
					}
			   }
		  }
		else
		  {
			 TargetString += TargetCharacter;

			 PreviousCharacter[0] = "";
			 PreviousCharacter[1] = "";
		  }
	 }

   return TargetString;
}



/* Cookie Management Functions */

function GetCookie(TargetCookie)

/* Return the value of the cookie specified by TargetCookie. */


{
   var TargetValue = "";

   var LeftIndex;
   var RightIndex;

   TargetCookie += "=";

   if (document.cookie.length > 0)
	 {
		LeftIndex = document.cookie.indexOf(TargetCookie);

		if (LeftIndex != -1)
		  {
			 LeftIndex += TargetCookie.length;

			 RightIndex = document.cookie.indexOf(";", LeftIndex);

			 if (RightIndex == -1)
			   RightIndex = document.cookie.length;

			 return document.cookie.substring(LeftIndex, RightIndex);
		  }
	 }

   return TargetValue;
}



/* Field Management Functions */

function FormatString(TargetField)

/* Format the value entered in the field specified by TargetField as a
   string. */


{
   if (TargetField.value.length > 0)
	 TargetField.value = Trim(TargetField.value);

   if (TargetField.value.length > 0)
	 TargetField.value = Collapse(TargetField.value);
}



/* Form Management Functions */

function InitializeForm()

/* Initialize the fields of the form presented to the user by the current
   page. */


{
   var SearchForm = document.SearchForm;

   SearchForm["who"].value = GetCookie("who");
   SearchForm["what"].value = GetCookie("what");
}



function ClearForm()

/* Clear the fields of the form presented to the user by the current page. */


{
   var SearchForm = document.SearchForm;

   SearchForm["who"].value = "";
   SearchForm["what"].value = "";
}



/* Validation Functions */

function ValidateField(TargetField)

/* Validate the field specified by TargetField. */


{
   FormatString(TargetField);
}



function ValidateForm()

/* Validate the form presented to the user by the current page. */


{
   var SearchForm = document.SearchForm;

   var SubmitForm = true;

   if (SearchForm["category"] == "All Categories")
	 {
		if (SearchForm["who"].value == "")
		  {
			 if (SearchForm["what"].value == "")
			   {
				  SubmitForm = false;
			   }
		  }
	 }

   if (SubmitForm)
	 SearchForm.submit();
   else
	 window.alert("Error: You did not enter any search text");
}
