//Function: checkInformation
//Purpose: Validate the fields of ActiveWords download/registration forms.
function checkInformation(theForm)
{
	if (theForm.Product &&
		theForm.Product[0] &&
		theForm.Product[1] &&
		theForm.Product[2])
	{
		if (!theForm.Product[0].checked &&
			!theForm.Product[1].checked &&
			!theForm.Product[2].checked)
		{
			alert('Please, choose the product you wish to download.');
			return (false);
		}
	}

	if (theForm.Name)
	{
		theForm.Name.value = Trim(theForm.Name.value);
		if (theForm.Name.value == "")
		{
			theForm.Name.value=prompt("Please, type your name and try again.", "");
			if ((theForm.Name.value == "null") || (theForm.Name.value == ""))
			{
				theForm.Name.value = "";
				theForm.Name.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
	}
	
	//emailpat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	//emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/; 
	//Visual Studio.NET 2003 Regular Expression validator for Internet Email address.
	emailpat = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if (theForm.Email)
	{
		theForm.Email.value = Trim(theForm.Email.value);
		if (theForm.Email.value == "")
		{
			theForm.Email.value=prompt("Please, type your e-mail address and try again.", "");
			if ((theForm.Email.value == "null") || (theForm.Email.value == ""))
			{
				theForm.Email.value = "";
				theForm.Email.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
		if (!emailpat.test(theForm.Email.value))
		{
			document.location.href = "bademail.html";
			return (false);
		}
	}

	if (theForm.Order_Email)
	{
		theForm.Order_Email.value = Trim(theForm.Order_Email.value);
		if (theForm.Order_Email.value == "")
		{
			theForm.Order_Email.value=prompt("Please, type your Order e-mail address and try again.", "");
			if ((theForm.Order_Email.value == "null") || (theForm.Order_Email.value == ""))
			{
				theForm.Order_Email.value = "";
				theForm.Order_Email.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
		
		if (!emailpat.test(theForm.Order_Email.value))
		{
			document.location.href = "bademail.html";
			return (false);
		}
	}

	if (theForm.New_Email)
	{
		theForm.New_Email.value = Trim(theForm.New_Email.value);
		if (theForm.New_Email.value != "")
		{
			if (!emailpat.test(theForm.New_Email.value))
			{
				document.location.href = "bademail.html";
				return (false);
			}
		}
		else
		{
			theForm.New_Email.value = theForm.Order_Email.value;
		}
	}

	if (theForm.Order_Number)
	{
		theForm.Order_Number.value = Trim(theForm.Order_Number.value);
		if (theForm.Order_Number.value == "")
		{
			theForm.Order_Number.value=prompt("Please, type your Order ID and try again.", "");
			if ((theForm.Order_Number.value == "null") || (theForm.Order_Number.value == ""))
			{
				theForm.Order_Number.value = "";
				theForm.Order_Number.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
	}

	if (theForm.Registration_Code)
	{
		theForm.Registration_Code.value = Trim(theForm.Registration_Code.value);
		if (theForm.Registration_Code.value == "")
		{
			theForm.Registration_Code.value=prompt("Please, type your Registration Code and try again.", "");
				if ((theForm.Registration_Code.value == "null") || (theForm.Registration_Code.value == ""))
			{
				theForm.Registration_Code.value = "";
				theForm.Registration_Code.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
	}

	return (true);
}

// modified 2010-02-09 by Nicholas Maddix. Original is below in checkUnlockReq_original()
//Function: checkUnlockReq
//Purpose: Validate the fields of ActiveWords unlocking request form.
function checkUnlockReq(theForm)
{	
	//emailpat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	//emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/; 
	//Visual Studio.NET 2003 Regular Expression validator for Internet Email address.
	emailpat = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if (theForm.Order_Email)
	{
		theForm.Order_Email.value = Trim(theForm.Order_Email.value);
		if (theForm.Order_Email.value == "")
		{
			theForm.Order_Email.value=prompt("Please, type your Order e-mail address and try again.", "");
			if ((theForm.Order_Email.value == "null") || (theForm.Order_Email.value == ""))
			{
				theForm.Order_Email.value = "";
				theForm.Order_Email.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
		
		if (!emailpat.test(theForm.Order_Email.value))
		{
			document.location.href = "bademail.html";
			return (false);
		}
	}

	if (theForm.Registration_Code)
	{
		theForm.Registration_Code.value = Trim(theForm.Registration_Code.value);
		if (theForm.Registration_Code.value == "")
		{
			theForm.Registration_Code.value=prompt("Please, type your Hardware ID and try again.", "");
				if ((theForm.Registration_Code.value == "null") || (theForm.Registration_Code.value == ""))
			{
				theForm.Registration_Code.value = "";
				theForm.Registration_Code.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
	}
	// Only Order_Email and Registration_Code are required.
	
	// If New_Email is provided, then validate it.
	if (theForm.New_Email)
	{
		theForm.New_Email.value = Trim(theForm.New_Email.value);
		if (theForm.New_Email.value != "")
		{
			if (!emailpat.test(theForm.New_Email.value))
			{
				document.location.href = "bademail.html";
				return (false);
			}
		}
		else
		{
			theForm.New_Email.value = theForm.Order_Email.value;
		}
	}
	
	// If New_Email is provided different than Order_Email, then validate the rest of the fields.
	if (theForm.New_Email.value != theForm.Order_Email.value)
	{
		if (theForm.Name)
		{
			theForm.Name.value = Trim(theForm.Name.value);
			if (theForm.Name.value == "")
			{
				theForm.Name.value=prompt("Please, type your name and try again. This is required when your new E-mail address is different than the one used in the original order.", "");
				if ((theForm.Name.value == "null") || (theForm.Name.value == ""))
				{
					theForm.Name.value = "";
					theForm.Name.focus();
				}
				else
				{
					theForm.BtnSubmit.focus();
				}
				return (false);
			}
		}
		
		/*  removed by NM
		if (theForm.Order_Number)
		{
			theForm.Order_Number.value = Trim(theForm.Order_Number.value);
			if (theForm.Order_Number.value == "")
			{
				theForm.Order_Number.value=prompt("Please, type your Order ID and try again. This is required when your new E-mail address is different than the one used in the original order.", "");
				if ((theForm.Order_Number.value == "null") || (theForm.Order_Number.value == ""))
				{
					theForm.Order_Number.value = "";
					theForm.Order_Number.focus();
				}
				else
				{
					theForm.BtnSubmit.focus();
				}
				return (false);
			}
		}
		*/
	}

	return (true);
}/*End of checkUnlockReq(...)*/

//Function: checkUnlockReq
//Purpose: Validate the fields of ActiveWords unlocking request form.
function checkUnlockReq_original(theForm)
{	
	//emailpat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	//emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/; 
	//Visual Studio.NET 2003 Regular Expression validator for Internet Email address.
	emailpat = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if (theForm.Order_Email)
	{
		theForm.Order_Email.value = Trim(theForm.Order_Email.value);
		if (theForm.Order_Email.value == "")
		{
			theForm.Order_Email.value=prompt("Please, type your Order e-mail address and try again.", "");
			if ((theForm.Order_Email.value == "null") || (theForm.Order_Email.value == ""))
			{
				theForm.Order_Email.value = "";
				theForm.Order_Email.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
		
		if (!emailpat.test(theForm.Order_Email.value))
		{
			document.location.href = "bademail.html";
			return (false);
		}
	}

	if (theForm.Registration_Code)
	{
		theForm.Registration_Code.value = Trim(theForm.Registration_Code.value);
		if (theForm.Registration_Code.value == "")
		{
			theForm.Registration_Code.value=prompt("Please, type your Hardware ID and try again.", "");
				if ((theForm.Registration_Code.value == "null") || (theForm.Registration_Code.value == ""))
			{
				theForm.Registration_Code.value = "";
				theForm.Registration_Code.focus();
			}
			else
			{
				theForm.BtnSubmit.focus();
			}
			return (false);
		}
	}
	// Only Order_Email and Registration_Code are required.
	
	// If New_Email is provided, then validate it.
	if (theForm.New_Email)
	{
		theForm.New_Email.value = Trim(theForm.New_Email.value);
		if (theForm.New_Email.value != "")
		{
			if (!emailpat.test(theForm.New_Email.value))
			{
				document.location.href = "bademail.html";
				return (false);
			}
		}
		else
		{
			theForm.New_Email.value = theForm.Order_Email.value;
		}
	}
	
	// If New_Email is provided different than Order_Email, then validate the rest of the fields.
	if (theForm.New_Email.value != theForm.Order_Email.value)
	{
		if (theForm.Name)
		{
			theForm.Name.value = Trim(theForm.Name.value);
			if (theForm.Name.value == "")
			{
				theForm.Name.value=prompt("Please, type your name and try again. This is required when your new E-mail address is different than the one used in the original order.", "");
				if ((theForm.Name.value == "null") || (theForm.Name.value == ""))
				{
					theForm.Name.value = "";
					theForm.Name.focus();
				}
				else
				{
					theForm.BtnSubmit.focus();
				}
				return (false);
			}
		}
		
		if (theForm.Order_Number)
		{
			theForm.Order_Number.value = Trim(theForm.Order_Number.value);
			if (theForm.Order_Number.value == "")
			{
				theForm.Order_Number.value=prompt("Please, type your Order ID and try again. This is required when your new E-mail address is different than the one used in the original order.", "");
				if ((theForm.Order_Number.value == "null") || (theForm.Order_Number.value == ""))
				{
					theForm.Order_Number.value = "";
					theForm.Order_Number.focus();
				}
				else
				{
					theForm.BtnSubmit.focus();
				}
				return (false);
			}
		}
	}

	return (true);
}/*End of checkUnlockReq(...)*/

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}
