function Login_Validator(theForm)
{
  if (theForm.username.value == "")
  {
    alert("Please enter your username");
    theForm.username.focus();
    return (false);
  }

  if (theForm.password.value == "")
  {
    alert("Please enter your password");
    theForm.password.focus();
    return (false);
  }

  return (true);
}



function LostPassword_Validator(theForm)
{
  if (theForm.email_address.value == "")
  {
    alert("Please fill your email address.");
    theForm.email_address.focus();
    return (false);
  }

  if ( !( validEmail(theForm.email_address.value) ) ) {
    alert('Please enter a valid email address!');
    theForm.email_address.focus();
    return (false);	
  }
}



function ContactUs_Validator(theForm)
{
  if (theForm.name.value == "")
  {
    alert("Please fill in your name.");
    theForm.name.focus();
    return (false);
  }

  if (theForm.email.value == "")
  {
    alert("Please fill your email address.");
    theForm.email.focus();
    return (false);
  }

  if ( !( validEmail(theForm.email.value) ) ) {
    alert('Please enter a valid email address!');
    theForm.email.focus();
    return (false);	
  }

  if (theForm.subject.value == "#" )
  {
    alert("Please select the subject");
    return (false);
  }

   if (theForm.content.value == "" )
  {
    alert("Please write your message");
    theForm.content.focus();
    return (false);
  }

  alert('Dear Valued User,\n\n You have successfully submitted your feedback. Thank You.');
  return (true);
}


// Validate the Registration Form

function Register(theForm)
{
  // Check for blank/empty or spacer input
  
  if (isEmpty(theForm.first_name))
  {
    alert("Please indicate your first name.");
	theForm.first_name.value = "";
    theForm.first_name.focus();
    return (false);
  }
  if (validateName(theForm.first_name))
  {
    alert("Only characters are accepted in the name fields.");
	theForm.first_name.value = "";
    theForm.first_name.focus();
    return (false);
  }
  

  // Validate Last Name
  
  if (isEmpty(theForm.last_name))
  {
    alert("Please indicate your last name.");
	theForm.last_name.value = "";
    theForm.last_name.focus();
    return (false);
  }
  if (validateName(theForm.last_name))
  {
    alert("Only characters are accepted in the name fields.");
    theForm.last_name.focus();
	theForm.last_name.value = "";
    return (false);
  }


  // Validate if gender is specified
  
  genderoption = -1
  for (i=0;i<theForm.gender.length;i++){
	if (theForm.gender[i].checked){
		genderoption = i
	}
  }
  if (genderoption == -1)
  {
    alert("Please indicate your gender.");
    return (false);
   }


  // Validate the year format
  
  /*clear out any spacer characters, such as parentheses, dashes, spaces, 
  and dots. We can do this with a regular expression and the replace() 
  method, replacing anything that matches our regular expression with a null string. 
  */
  var yystripped = theForm.yy.value.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters

  if (yystripped == "")
  {
	alert ("Please indicate your birth year.")
	theForm.yy.value = ""
	theForm.yy.focus()
	return false;
  }
  if (isNaN(yystripped))
  {
	alert("Please indicate your birth year in numeric.")
	theForm.yy.value = ""
	theForm.yy.focus()
	return false;
  }
  if (yystripped > 1989)
  {
  	alert("You should be at least 13 years old to be eligible for registration.");
	theForm.yy.focus()
	return false;
  }
  if (yystripped.length < 4)
  {
  	alert("Please indicate your birth year in 4 digits.");
	theForm.yy.value = ""
	theForm.yy.focus()
	return false;
  }
  
  
  // Validate e-mail address
  // Check for blank / empty / spacer input
  if (isEmpty(theForm.email_address))
  {
    alert("Please indicate your email address.");
	theForm.email_address.value = "";
    theForm.email_address.focus();
    return (false);
  }
  // Allows only characters, numbers, '@', '.', '_'
  // Should conform to standard e-mail address format
  if ( !( validEmail(theForm.email_address.value) ) ) {
    alert('Please indicate a valid email address!');
	theForm.email_address.value = "";
    theForm.email_address.focus();
    return (false);	
  }


  // Validate username
  
  if (isEmpty(theForm.username))
  {
    alert("Please enter your username.");
	theForm.username.value = "";
    theForm.username.focus();
    return (false);
  }
  if (theForm.username.value.length <3 || theForm.username.value.length > 20)
  {
  	alert("Username can only be 3 - 20 characters.");
	theForm.username.value = "";
    theForm.username.focus();
    return (false);
  }
  var illegalChars = /\W/;
  // allow only letters, numbers, and underscores
  if (illegalChars.test(theForm.username.value)) {
     alert("Only characters, numbers and underscores are accepted in the username.");
     theForm.username.value = "";	 
	 theForm.username.focus();
	 return (false);
  } 

  
  // Validate if country is specified
  if (theForm.country.value == "#" )
  {
    alert("Please select your country.");
    return (false);
  }
    
  // Validate if education level is specified
  if (theForm.education.value == "#" )
  {
    alert("Please indicate your education level.");
    return (false);
  }

  // Validate if profession is specified
  if (theForm.profession.value == "#" )
  {
    alert("Please indicate your profession.");
    return (false);
  }
 
  //alert('Dear Valued User,\n\n You have successfully submitted your application. Thank You.');
  return (true);
}


function Register_check(theForm)
{
  if (isEmpty(theForm.first_name))
  {
    alert("Please indicate your first name.");
	theForm.first_name.value = "";
    theForm.first_name.focus();
    return (false);
  }
  if (validateName(theForm.first_name))
  {
    alert("Only characters are accepted in the name fields.");
	theForm.first_name.value = "";
    theForm.first_name.focus();
    return (false);
  }



  if (isEmpty(theForm.last_name))
  {
    alert("Please indicate your last name.");
	theForm.last_name.value = "";
    theForm.last_name.focus();
    return (false);
  }
  if (validateName(theForm.last_name))
  {
    alert("Only characters are accepted in the name fields.");
    theForm.last_name.focus();
	theForm.last_name.value = "";
    return (false);
  }


  // Validate if gender is specified
  
  genderoption = -1
  for (i=0;i<theForm.gender.length;i++){
	if (theForm.gender[i].checked){
		genderoption = i
	}
  }
  if (genderoption == -1)
  {
    alert("Please indicate your gender.");
    return (false);
   }


  // Validate the year format
  
  /*clear out any spacer characters, such as parentheses, dashes, spaces, 
  and dots. We can do this with a regular expression and the replace() 
  method, replacing anything that matches our regular expression with a null string. 
  */
  var yystripped = theForm.yy.value.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters

  if (yystripped == "")
  {
	alert ("Please indicate your birth year.")
	theForm.yy.value = ""
	theForm.yy.focus()
	return false;
  }
  if (isNaN(yystripped))
  {
	alert("Please indicate your birth year in numeric.")
	theForm.yy.value = ""
	theForm.yy.focus()
	return false;
  }
  if (yystripped > 1989)
  {
  	alert("You should be at least 13 years old to be eligible for registration.");
	theForm.yy.focus()
	return false;
  }
  if (yystripped.length < 4)
  {
  	alert("Please indicate your birth year in 4 digits.");
	theForm.yy.value = ""
	theForm.yy.focus()
	return false;
  }


  // Validate e-mail address
  // Check for blank / empty / spacer input
  if (isEmpty(theForm.email_address))
  {
    alert("Please indicate your email address.");
	theForm.email_address.value = "";
    theForm.email_address.focus();
    return (false);
  }
  // Allows only characters, numbers, '@', '.', '_'
  // Should conform to standard e-mail address format
  if ( !( validEmail(theForm.email_address.value) ) ) {
    alert('Please indicate a valid email address!');
	theForm.email_address.value = "";
    theForm.email_address.focus();
    return (false);	
  }
  if (theForm.email_address.value != theForm.email_address2.value)
  {
    alert("Please ensure you enter correct email address");
	theForm.email_address.value = "";
	theForm.email_address2.value = "";
    theForm.email_address.focus();
    return (false);
  }
  
  // Validate username
  
  if (isEmpty(theForm.username))
  {
    alert("Please enter your username.");
	theForm.username.value = "";
    theForm.username.focus();
    return (false);
  }
  if (theForm.username.value.length <3 || theForm.username.value.length > 20)
  {
  	alert("Username can only be 3 - 20 characters.");
	theForm.username.value = "";
    theForm.username.focus();
    return (false);
  }
  var illegalChars = /\W/;
  // allow only letters, numbers, and underscores
  if (illegalChars.test(theForm.username.value)) {
     alert("Only characters, numbers and underscores are accepted in the username.");
     theForm.username.value = "";	 
	 theForm.username.focus();
	 return (false);
  } 

  if (isEmpty(theForm.mobile))
  {
	alert("Please enter your mobile phone number.");
	theForm.mobile.value = "";
    theForm.mobile.focus();
    return (false);
  }
  if (isNaN(theForm.mobile.value))
  {
  	alert("Please enter your mobile phone number in numeric.");
	theForm.mobile.value ="";
	theForm.mobile.focus();
	return false;
  }


  if (theForm.country.value == "#" )
  {
    alert("Please select your country.");
    return (false);
  }


  // Validate if education level is specified
  if (theForm.education.value == "#" )
  {
    alert("Please indicate your education level.");
    return (false);
  }

  // Validate if profession is specified
  if (theForm.profession.value == "#" )
  {
    alert("Please indicate your profession.");
    return (false);
  }

/*
  if (theForm.bankname.value == "")
  {
    alert("Please indicate the name of your bank. E.g. OCBC, POSBank, UOB etc.");
    theForm.bankname.focus();
    return (false);
  }

  if (theForm.bank_initial.value == "")
  {
    alert("Please indicate your bank initial for Internet Banking. Put 'nil' if none.");
    theForm.bank_initial.focus();
    return (false);
  }

  paymentoption = -1
  for (i=0;i<theForm.payment.length;i++){
	if (theForm.payment[i].checked){
		paymentoption = i
	}
  }

  if (paymentoption == -1)
  {
    alert("Please indicate payment method.");
    return (false);
   }


  if (theForm.subscription_mode.value == "#" )
  {
    alert("Please select the subscription mode.");
    return (false);
  }
*/



  if (theForm.how_did_you_know_us.value == "#")
  {
    alert("Please indicate how did you hear about us");
    return (false);	
  }


  // Validate if bookmaker is specified
  
  bookmakeroption = -1
  for (i=0;i<theForm.bookmaker.length;i++){
	if (theForm.bookmaker[i].checked){
		bookmakeroption = i
	}
  }
  if (bookmakeroption == -1)
  {
    alert("Please indicate your bookmaker.");
    return (false);
   }



  if (theForm.betting_stakes.value == "#")
  {
    alert("Please indicate your average betting stakes");
    return (false);	
  }


  if (theForm.remarks.value == "" )
  {
    alert("Please give us your feedback or queries if any. Put 'nil' if none.");
    theForm.remarks.focus();
    return (false);
  }
  
  if (theForm.read.checked == false)
  {
  	alert("Please tick the checkbox if you agreed to our agreement.");
	return (false);
  }
    
  //alert('Dear Valued User,\n\n You have successfully submitted your application. Thank You.');
  return (true);
}


// Determine if the value input is empty

function isEmpty(str){
    strRE = new RegExp( );
    strRE.compile( '^[\s ]*$', 'gi' );
    return strRE.test( str.value );
} 

// Validate name input, can only contain characters.

function validateName(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "			// valid name input
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		return (true);
	}
}


function recommend(theForm) 
{

  if (theForm.Name.value == "")
  {
	alert("Please fill in your name.");
	theForm.Name.focus();
	return (false);
  }

  if (theForm.Email.value == "")
  {
	alert("Please fill in your e-mail address.");
	theForm.Email.focus();
	return (false);
  }

  if ( !( validEmail(theForm.Email.value) ) ) {
    alert('Please enter a valid email address!');
    theForm.Email.focus();
    return (false);	
  }

	/*
  if ( !( validEmail2(theForm.Email.value) ) ) {
    alert('Please enter a valid email address!');
    theForm.Email.focus();
    return (false);	
  }
  */
  var formObj;
  formObj = theForm.elements['f[]'];

	if(formObj.length)
	{	
		for(i=0;i<formObj.length;i++)
		{
			if(formObj[i].value=="")
			{
				alert("Enter your friends E-mail address");
				formObj[i].focus();
				return false;
			}

		}
	}
	else
	{
		if(formObj.value=="")
			{
				alert("Enter your friends E-mail address");
				formObj.focus();
				return false;
			}	

	}

  alert('Dear Valued User,\n\n You have successfully submitted the form. Thank You.');
  return (true);
}


function validEmail(email) {

	invalidChars = " /:"

	if (email == "") {
		return (false);
	}

	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) != -1) {
			return (false);
		}
	}

	atPos = email.indexOf("@", 1)
	if (atPos == -1) {
		return (false);
	}

	//if (email.indexOf("@", atPos + 1) != -1) {
	//	return (false);
	//}

	periodPos = email.indexOf(".", atPos)
	if (periodPos == -1) {
		return (false);
	}

	if (periodPos + 3 > email.length) {
		return (false);
	}

	return (true);
}
function validEmail2(email) {

	invalidChars = " soccer-prediction.com"

	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) != -1) {
			return (false);
		}
	}

	atPos = email.indexOf("@", 1)
	if (atPos == -1) {
		return (false);
	}

	//if (email.indexOf("@", atPos + 1) != -1) {
	//	return (false);
	//}

	periodPos = email.indexOf(".", atPos)
	if (periodPos == -1) {
		return (false);
	}

	if (periodPos + 3 > email.length) {
		return (false);
	}

	return (true);
}