// JavaScript Document

//alert("Register Validation script v1.0");


//document.forms[0].onsubmit = checkRegister;
document.getElementById("btnRegister").onclick = checkRegister;

document.forms[0].userName.onblur = validUsername;

function checkRegister()
{
	var isValid = true;
	var f = document.forms[0];
	var errors = "Please enter your information in the following required fields:               \n";

	if(f.userName.value.length == 0)
	{
		errors += "\n\t *  A Username is required.";
		isValid = false;	
	}
	else
	{
		if (!checkUsername())
		{
			errors += "\n\t * Your Username must be between 6 and 20 characters and cannot include spaces or characters such as * @ / # .";
			isValid = false;	
		}
	}
	
	if(f.password.value.length == 0)
	{
		errors += "\n\t *  A Password is required.";
		isValid = false;	
	}
	if(f.password2.value.length == 0)
	{
		errors += "\n\t *  Your Password requires confirming.";
		isValid = false;	
	}
	if (f.password.value != f.password2.value)
	{
		errors += "\n\t * Your passwords do not match";
		isValid = false;
	}
	
	if(f.email.value.length == 0)
	{
		errors += "\n\t *  A Contact Email Address is required.";
		isValid = false;
	}
	 else
	{
		// Check for Properly Formatted Email Addresses
		if (!(isValidEmail(f.email.value))) {
			errors += "\n\t * A properly formatted Email address is required.";
			isValid = false;
		}
	}
	
	
	if(f.firstName.value.length == 0)
	{
		errors += "\n\t *  A First Name is required.";
		isValid = false;	
	}
	if(f.surname.value.length == 0)
	{
		errors += "\n\t *  A Surname is required.";
		isValid = false;	
	}
	if(f.cardName.value.length == 0)
	{
		errors += "\n\t *  The Name on your Credit Card is required.";
		isValid = false;	
	}
	if(f.address1.value.length == 0)
	{
		errors += "\n\t *  An Address is required.";
		isValid = false;	
	}
	if(f.city.value.length == 0)
	{
		errors += "\n\t *  A City is required.";
		isValid = false;	
	}
	if(f.county.value.length == 0)
	{
		errors += "\n\t *  A County/State is required.";
		isValid = false;	
	}
	if (f.country.selectedIndex == 0)
	{
		errors += "\n\t *  A Country is required.";
		isValid = false;
	}
	if(f.postcode.value.length == 0)
	{
		errors += "\n\t *  A Post Code/Zip Code is required.";
		isValid = false;	
	}
		
	if(f.homePhone.value.length == 0 && f.mobilePhone.value.length == 0)
	{
		errors += "\n\t *  Either a Daytime Phone number or Mobile Phone Number is required.";
		isValid = false;	
	}
	
	
	if(!isValid)
	{
		alert(errors);
		return false;
	}
	else
	{
		return true;
		document.forms[0].submit();
	}
}

// Check for valid Email Address
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function ValidNumber(thestring)
{
    for (i = 0; i < thestring.length; i++) {
        ch = thestring.substring(i, i+1);
        if (ch < "0" || ch > "9")
          {
          return false;
          }
    }
    return true;
}



function validUsername()
{
	if (!checkUsername())
	{
		alert("Your Username must be between 6 and 20 characters and cannot include spaces or characters such as * @ / # .");
		document.getElementById("userName").focus();
	}
}


// Check for valid Username
function checkUsername()
{
	var thestring = document.getElementById("userName").value;
	
	// ---- Check for invalid Characters
	for (i = 0; i < thestring.length; i++)
	{
      ch = thestring.substring(i, i+1);
		if (ch < "0" || ch > "z")
		{
		  return false;
		}
	}

	if (thestring.length <= 5 || thestring.length > 20)
	{
		return false;
	}
	return true;
}
