var WatchDogTimer = 0;
var checkLink = '<a href="javascript:checkAccountID(document.SIGNUP)"><img border=0 alt=Go src=http://www.rtsports.com/img/go.gif></a>';
var isWorking		= false;
var validAccount	= false;
var http		      = getHTTPObject(); 


function handleHttpResponse_AccountID() 
{

	if (http.readyState == 4) 
	{
		if (http.responseText.indexOf('invalid') == -1) 
		{
			try
			{
				// Use the XML DOM 
				var xmlDocument = http.responseXML; 

		        //
				//  get the result object
				//
            var newAccountDiv = document.getElementById('NewAccountDiv');
            var passwdDiv = document.getElementById('PasswdDiv');
				var statusDiv = document.getElementById('SIGNUPSTATUS');
				var exists = xmlDocument.getElementsByTagName('check').item(0);
				if (exists.firstChild.data == 'EXISTS') 
				{
               passwdDiv.style.display = '';
               newAccountDiv.style.display = 'none';
					statusDiv.innerHTML = checkLink;
               window.location.hash = "ACCOUNTFOUND";
					validAccount = true;
				} else {
               passwdDiv.style.display = 'none';
               newAccountDiv.style.display = '';
					statusDiv.innerHTML = checkLink;

               var accountID = document.getElementById('ACCOUNTID');
               var email = document.getElementById('EMAIL');
               email.value = accountID.value;
               email = document.getElementById('EMAILREF');
               email.innerHTML = '<b>' + accountID.value + '</b>';
               window.location.hash = "ACCOUNTNOTFOUND";
					validAccount = false;
				}	
			}
			catch (e)
			{
				validAccount = true;  // do nothing
			}

		} 
		else
			//  invalid xml
			validAccount = true;
	}
    isWorking = false;    
}



function checkAccountID(theform)
{
	theform.ACCOUNTID.value = trim(theform.ACCOUNTID.value); 
	if (theform.ACCOUNTID.length < 1) 
	{
		alert ('Please enter a your email address.');
		theform.ACCOUNTID.focus();
		theform.ACCOUNTID.select();
		validAccount = false;
		return;
	} 
	
   var validemail = checkEmail(theform.ACCOUNTID.value);

	if (validemail != 0) 
	{
		alert ('Email not valid. Please enter a your email address.');
		theform.ACCOUNTID.focus();
		theform.ACCOUNTID.select();
		validAccount = false;
		return;
    }

	var statusDiv = document.getElementById('SIGNUPSTATUS');
	statusDiv.innerHTML = "Checking...";	

	if (!isWorking && http) 
	{
		isWorking = true;
		http.open("GET", "/php/check-exists.php?FLD=E&CHECK=" + escape(theform.ACCOUNTID.value) + "&X=" + Math.floor(Math.random() * 99999), true);
		http.onreadystatechange = handleHttpResponse_AccountID;
 	   http.send(null);
	} else {
		validAccount = true;
      theform.submit();   // submit the form to check on the server
   }
}


function CheckLoginInfo(theform)
{
	var eMailElement = theform.ACCOUNTID;
 	eMailElement.value = trim(eMailElement.value);
	if (eMailElement.value.length == 0)
	{
		alert('Please enter a valid email address.');
		theform.ACCOUNTID.focus();
		theform.ACCOUNTID.select();
		return;
	}
	var eMailValid = checkEmail(eMailElement.value);
	if (eMailValid == 2)  // bad email
	{
		alert ('The email address ' + eMailElement.value + ' is not valid.  Please re-enter a valid email address and try again.');
		theform.ACCOUNTID.focus();
		theform.ACCOUNTID.select();
		return;
	}	

	if (trim(theform.PASSWORD.value).length == 0)
	{
      alert ("You must enter a password.  Please try again.");
	   theform.PASSWORD.select();
  	   return;
	}

   if (theform.ENTRYNICK1)
   {
      if (!CheckTeamName(theform.ENTRYNICK1)) return;
   }

   if (theform.ENTRYNICK2)
   {
      if (trim(theform.ENTRYNICK2.value).length > 0)
      {
         if (!CheckTeamName(theform.ENTRYNICK2)) return;
      }
   }

   if (theform.LGNAME)
   {
      if (!CheckLeagueName(theform.LGNAME)) return;
   }

   theform.submit();
}





//var validNickname	= false;


function handleHttpResponse_Forum() 
{

	if (http.readyState == 4) 
	{
		if (http.responseText.indexOf('invalid') == -1) 
		{
			try
			{
				// Use the XML DOM 
				var xmlDocument = http.responseXML; 

		        //
				//  get the result object
				//
				var statusDiv = document.getElementById('nicknameStatus');
				var exists = xmlDocument.getElementsByTagName('check').item(0);
				if (exists.firstChild.data == 'EXISTS') 
				{
					statusDiv.innerHTML = '<font color=red><b>&lt;&lt; NICKNAME TAKEN</b></font>';
					alert ('Sorry, the forum nickname   ' + document.REGISTER.NICKNAME.value + '   has been taken.  Re-enter the nickname and try again.')
					document.REGISTER.NICKNAME.select();
//					validNickname = false;
				}
				else
				{
               //  Nickname is good to go
					statusDiv.innerHTML = '&nbsp;';
//					validNickname = true;
               document.REGISTER.submit();
				}	
			}
			catch (e)
			{
            //   general error, check the nickname on the back side
//				validNickname = true;  
            document.REGISTER.submit();
			}

		} 
		else
			//  invalid xml, check the nickname on the back side
//			validNickname = true;
         document.REGISTER.submit();
	}
    isWorking = false;    
}



function checkForumNickname(theform)
{
	if (trim(theform.NICKNAME.value).length < 1) 
	{
		alert ('Please enter a nickname to be used as your name in our forums.');
		theform.NICKNAME.focus();
		theform.NICKNAME.select();
//		validNickname = false;
		return;
	} 
	
	if (!checkNickname(theform.NICKNAME.value)) 
	{
      alert ('Your forum nickname must be at least 4 characters long and can contain letters, numbers and underscores.  Re-enter the nickname and try again.')
//		validNickname = false;
		theform.NICKNAME.select();
		return;
    }

	var statusDiv = document.getElementById('nicknameStatus');
	statusDiv.innerHTML = "Checking...";	

	if (!isWorking && http) 
	{
		isWorking = true;
		http.open("GET", "/php/check-exists.php?FLD=F&CHECK=" + escape(theform.NICKNAME.value) + "&X=" + Math.floor(Math.random() * 99999), true);
		http.onreadystatechange = handleHttpResponse_Forum;
 	   http.send(null);

	}	else {
      //  Does not support  HTTPRequest
//		validNickname = true;
      document.REGISTER.submit();
   }
}



function CheckInfo()
{
	// Because of the onBlur event the nickname could possibly get checked first.  Wait for the check to finish before
	// continuing.
	
    var eMailElement = document.REGISTER.EMAIL;
	eMailElement.value = trim(eMailElement.value);
	if (eMailElement.value.length == 0)
	{
		alert('Your account ID must be a valid e-mail address.');
		document.REGISTER.EMAIL.select();
		return;
	}
	var eMailValid = checkEmail(eMailElement.value);
	if (eMailValid == 2)  // bad email
	{
		alert ('The e-mail address ' + eMailElement.value + ' is not valid.  Please re-enter a valid e-mail address and try again.');
		document.REGISTER.EMAIL.select();
		return;
	}
	if (eMailValid == 1)	// Close to good e-mail
	{
		alert('The e-mail address ' + eMailElement.value + ' does not appear to be valid.  Please re-enter a valid e-mail address and try again.');
		document.REGISTER.EMAIL.select();
		return;
	}


    if (document.REGISTER.EMAIL.value != document.REGISTER.EMAILVERIFY.value)
    {
        alert ("Email verification error.  Re-enter and verify the email and try again.")
		document.REGISTER.EMAIL.select();
    	return
    }


	if (!checkPassword(trim(document.REGISTER.PASSWORD.value)))
	{
        alert ('Your password must be 6 characters long and contain only printable characters.  Re-enter the password and try again.')
		document.REGISTER.PASSWORD.select();
    	return
	}

	if (checkEasyPassword(trim(document.REGISTER.PASSWORD.value)))
	{
       alert ("That password is too easy to guess try again.  Please try again.");
	   document.REGISTER.PASSWORD.select();
  	   return
	}

    if (document.REGISTER.PASSWORD.value != document.REGISTER.VERIFY.value)
    {
        alert ("Password verification error.  Re-enter and verify the password and try again.")
		document.REGISTER.PASSWORD.select();
    	return
    }


	// The user must agree to the disclaimer before he can get an account.
    if (!document.REGISTER.DISCLAIMER.checked)
    {
        alert("You must check the box agreeing to the terms and conditions of use before registering.")
        return
    }



	if (document.REGISTER.QUESTION.value == "NONE")
	{
        alert ('Please select a secret question, you will need it if you forget your password.')
    	return
	}

	if (trim(document.REGISTER.ANSWER.value).length < 1)
	{
        alert ('Please provide a secret answer, you will need it if you forget your password.')
		document.REGISTER.ANSWER.select();
    	return
	}



	if (trim(document.REGISTER.FIRSTNAME.value).length < 1)
	{
        alert ('Please enter your first name to register.')
		document.REGISTER.FIRSTNAME.select();
    	return
	}

	if (trim(document.REGISTER.LASTNAME.value).length < 1)
	{
        alert ('Please enter your last name to register.')
		document.REGISTER.LASTNAME.select();
    	return
	}

	if (trim(document.REGISTER.STREETADDRESS.value).length < 1)
	{
        alert ('Please enter your address to register.')
		document.REGISTER.STREETADDRESS.select();
    	return
	}

	if (trim(document.REGISTER.CITY.value).length < 1)
	{
        alert ('Please enter the city in which you currently live.')
		document.REGISTER.CITY.select();
    	return
	}

	if (trim(document.REGISTER.STATE.value).length < 1)
	{
        alert ('Please enter the state/province in which you currently live.')
		document.REGISTER.STATE.select();
    	return
	}

	if (trim(document.REGISTER.ZIP.value).length < 1)
	{
        alert ('Please enter your zip/postal code.')
		document.REGISTER.ZIP.select();
    	return
	}

	if (trim(document.REGISTER.COUNTRY.value).length < 1)
	{
        alert ('Please enter the country in which you currently live.')
		document.REGISTER.COUNTRY.select();
    	return
	}

	if (trim(document.REGISTER.AGE.value).length < 1)
	{
        alert ('Please enter your age.')
		document.REGISTER.AGE.select();
    	return
	}
	else if (isNaN(parseInt(document.REGISTER.AGE.value)))
	{
        alert ('Please enter a valid age.')
		document.REGISTER.AGE.select();
    	return
	}
	else if (parseInt(document.REGISTER.AGE.value) <= 13)
	{
        alert ('You must be at least 14 years of age to register.')
		document.REGISTER.AGE.select();
    	return
	}

   if (document.REGISTER.ENTRYNICK1)
   {
      if (!CheckTeamName(document.REGISTER.ENTRYNICK1)) return;
   }

   if (document.REGISTER.ENTRYNICK2)
   {
      if (document.REGISTER.ENTRYNICK2.value.length > 0)
      {
         if (!CheckTeamName(document.REGISTER.ENTRYNICK2)) return;
      }
   }

   if (document.REGISTER.LGNAME)
   {
      if (!CheckLeagueName(document.REGISTER.LGNAME)) return;
   }

	if (document.REGISTER.FIND[0])
	{
		// Check the discovery method and source.
		if (!(document.REGISTER.FIND[0].checked || document.REGISTER.FIND[1].checked || 
			  document.REGISTER.FIND[2].checked || document.REGISTER.FIND[3].checked || 
			  document.REGISTER.FIND[4].checked || document.REGISTER.FIND[5].checked))
		{
		alert ("You must select how you found out about our service.\n" +
			   "Please select the appropriate button claiming how you discovered our site and try again.")
		return
		}
		if (document.REGISTER.FIND[0].checked && document.REGISTER.Magazine_Source.selectedIndex == 0)
		{
			alert ("You specified that you discovered our site from a magazine, but you did not specify the magazine. " +
				   "Please select a magazine and try again.")
			return
		}

		if (document.REGISTER.FIND[1].checked && document.REGISTER.Engine_Source.selectedIndex == 0)
		{
			alert ("You specified that you discovered our site using a search engine, but you did not specify which one. " +
				   "Please select a search engine and try again.")
			return
		}

		if (document.REGISTER.FIND[2].checked && document.REGISTER.Website_Source.selectedIndex == 0)
		{
			alert ("You specified that you discovered our site from a website or message board, but you did not specify which one. " +
				   "Please select a website or message board and try again.")
			return
		}

		if (document.REGISTER.FIND[3].checked && trim(document.REGISTER.Account.value).length == 0)
		{
			alert ("You specified that you are a returning customer or were referred by an existing customer, but you did not " +
				   "specify the referring account ID or league name.  Please enter this information and try again.")
			return
		}

		if (document.REGISTER.FIND[4].checked && trim(document.REGISTER.Banner.value).length == 0)
		{
			alert ("You specified that you found out about our service from a banner or link on a web page, but you did not " +
				   "specify which page.  Please enter the web page and try again.")
			return
		}

		if (document.REGISTER.FIND[5].checked && trim(document.REGISTER.Other_Source.value).length == 0)
		{
			alert ("You specified that you found out about our service from a source not listed, but you did not " +
				   "specify the source.  Please enter the source and try again.")
			return
		}
	}    
	
   checkForumNickname(document.REGISTER);

}


function checkPublicName(nickname)
{
	var regexAccount = /^[a-z0-9\-\_]{4,15}$/i;  // alphanumeric, dashes, and underscores, min length 4, max length 15
	if (regexAccount.test(nickname))
	{
		return true;
	}
	return false;
}


function CheckTeamName(thename)
{
   if (trim(thename.value).length < 1) 
	{
		alert ('Please enter a name for your new entry.');
		thename.focus();
		return false;
	} 
   if (trim(thename.value).length < 3 || trim(thename.value).length > 30) 
	{
		alert ('Your entry name must be between 3 and 30 characters long.');
		thename.focus();
		return false;
	} 

	var regexAccount = /^[a-z0-9\-\_\ \!\$\(\)\*\+\.\?\"\'\@]{3,30}$/i;  // alphanumeric, dashes, and underscores, and spaces min length 3, max length 30
	if (!regexAccount.test(thename.value)) {
		alert ('Your entry name may only contain letters, numbers, dashes and spaces.');
		thename.focus();
		return false;
	} 

   return true;
}

function CheckLeagueName(thename)
{
	if (trim(thename.value).length < 1) 
	{
		alert ('Please enter a name for your new league.');
		thename.focus();
		return false;
	} 
   if (trim(thename.value).length < 3 || trim(thename.value).length > 50 ) 
	{
		alert ('The name for your new league must be between 3 and 50 characters long.');
		thename.focus();
		return false;
	} 
	var regexAccount = /^[a-z0-9\-\_\ \!\$\(\)\*\+\.\?\"\'\@]{3,50}$/i;  // alphanumeric, dashes, and underscores, and spaces min length 3, max length 50
	if (!regexAccount.test(thename.value)) {
		alert ('Your league name may only contain letters, numbers, dashes and spaces.');
		thename.focus();
		return false;
	} 
   return true;
}

