// Account registration validation script
// For blackgum.com.au
// By Dale Fyfe for Quoth Design
// October 2008

function validateRegistration()
{
	var errorMsg = "There has been an error!\n\n";
	
	validateUsername();
	validateRequired();
	validateABN();
	// pass word was commented out
	validatePassword();
	validateContact();
	
	function validateUsername()
	{
		//ensure username not blank	
		if(document.createAccount.uName.value == "")
		{
			errorMsg = errorMsg + "You must specify a Username.\n";	
		}
	}
	
	function validateRequired()
	{
		if(document.createAccount.bName.value == "")
		{
				errorMsg = errorMsg + "You must specify a Business name.\n";	
		}
	}
	
	function validateABN()
	{
		if(document.createAccount.busNum.value == "")
		{
				errorMsg = errorMsg + "You must enter your 11 digit Australian Business Number(ABN).\n";
		}
		else
		{
			if(document.createAccount.busNum.value.length != 11)
			   {
					 errorMsg = errorMsg + "Your entered ABN must have 11 digits.\n";  
			   }
		}
	}
	
	/*function validatePassword()
	{
		//ensure passwords match
		if(document.createAccount.password.value != document.createAccount.password2.value)
		{
			errorMsg = errorMsg + "Passwords do not match.\n";	
		}
		//ensure password at least 5 chars
		if(document.createAccount.password.value.length < 5)
		{
			errorMsg = errorMsg + "Password must be at least 5 characters long.\n";	
		}
	}*/
	
	function validateContact()
	{
		var validNums = "0123456789 ";//use this number string to test the phone number against. These are the only allowed characters in the phone number field.
		var ph = document.createAccount.phNum.value;// store the phone number feild value in a short handy variable
		var invalidPhNum = false;//if this variable is true after checking, then the error message is added to.
		var test; //used to store each of the numbers in the phone number field and check if it matches the valid numbers
		
		// ensure there is some contact info
		//Check there is something in the postal address
		if (document.createAccount.post.value == "")
		{
				errorMsg == errorMsg + "You must enter a Business Postal Address.\n";
		}
		
		//Make sure there is a phone number, it is a required field
		if (ph == "")
		{
			errorMsg = errorMsg + "You must specify a phone number.\n";	
		}
		else
		{
			// make sure phone number is at least 8 numbers
			if (ph.length < 8)
			{
				errorMsg = errorMsg + "Phone number must be at least 8 numbers long.\n";	
			}
			// make sure phone number is only numbers
			var strng = ph;
			var stripped = strng.replace(/[\(\)\.\-\ ]/g, "");
			//strip out acceptable non-numeric characters
			if (isNaN(parseInt(stripped)))
			{
 				  invalidPhNum = true;
			}
			if (invalidPhNum == true)
			{
					errorMsg = errorMsg + "Phone number must contain only numbers.\n";	
			}
		}
		// if there is an email address, make sure it is valid
		if (document.createAccount.email.value != "")
		{
			validateEmail();	
		}
	}
	
	function validateEmail()
	{
		var email = document.createAccount.email.value;
		
		//if there is an email address then we need to make sure it is of a valid syntax of text@moretext.moretext
		
		var first = email.charAt(0);//first char of the email address
		var last = email.charAt(email.length -1);//Last char of the email address
		var foundSymbol = email.indexOf('@');// Where abouts the @ symbol is in the email address, returns -1 if not there
		var foundDot = email.indexOf('.');// Where the dot is in the email address.
		
		//if the @ symbol is not there, is at the start, or at the end, then the email is not valid
		if ((foundSymbol == -1) || (foundSymbol == 0) || (foundSymbol == email.length -1))
		{
			errorMsg = errorMsg + "The @ symbol in your email address is either missing, or in the wrong place.\n";
			document.createAccount.email.value="";	
		}
		//if the dot is not there, is at the start, or at the end, then the email is not valid
		if ((foundDot == -1) || (foundDot == 0) || (foundDot == email.length -1))
		{
			errorMsg = errorMsg + "the . (dot) in your email address is either missing, or in the wrong place.\n (As in someone@something.com)\n";
			document.createAccount.email.value="";	
		}
	}
	
	//Generate error message if there has been one created throughout validation, otherwise return true and go to php server side processing
	if(errorMsg == "There has been an error!\n\n")
	{
		return true;	
	}
	else
	{
		alert(""+errorMsg);
		return false;
	}
}

function validateNewPass()
{
	var errorMsg = "There has been an error!\n\n";
	
	function validatePassword()
	{
		//ensure passwords match
		if(document.createAccount.password.value != document.createAccount.password2.value)
		{
			errorMsg = errorMsg + "Passwords do not match.\n";	
		}
		//ensure password at least 5 chars
		if(document.createAccount.password.value.length < 5)
		{
			errorMsg = errorMsg + "Password must be at least 5 characters long.\n";	
		}
	}
	
	if(errorMsg == "There has been an error!\n\n")
	{
		return true;	
	}
	else
	{
		alert(""+errorMsg);
		return false;
	}
}

// CHECK USERNAME IS NOT ALREADY TAKEN AJAX SCRIPT

var xmlHttp;
var xmlHttp2;

function checkUsername(str)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
 	{
 		alert ("Browser does not support HTTP Request");
 		return;
	}
	var url="../scripts/checkUsername.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 	{ 
 		document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
 	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
 	{
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e)
 	{
 		//Internet Explorer
		try
  		{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e)
  		{
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
return xmlHttp;
}

function checkBusName(str)
{ 
	xmlHttp2=GetXmlHttpObject();
	if (xmlHttp2==null)
 	{
 		alert ("Browser does not support HTTP Request");
 		return;
	}
	var url="../scripts/checkBusName.php";
	url=url+"?b="+str;
	url=url+"&sid="+Math.random();
	xmlHttp2.onreadystatechange=stateChanged2 ;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
}

function stateChanged2() 
{ 
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
 	{ 
 		document.getElementById("txtHint2").innerHTML=xmlHttp2.responseText;
 	} 
}

function GetXmlHttpObject2()
{
	var xmlHttp2=null;
	try
 	{
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp2=new XMLHttpRequest();
 	}
	catch (e)
 	{
 		//Internet Explorer
		try
  		{
  			xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e)
  		{
  			xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
return xmlHttp2;
}
