// JavaScript Document
//js
function validate_email(element)
{  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	if (emailPattern.test(element.value) == false)
	{
		alert('Invalid email address');
		element.focus();
		return false;
	}
	return true;
}  

function validate_sign_up()
{
	var required = ['cf2_field_3', //registered company name
				  	'cf2_field_5', //postal address
				  	'cf2_field_8', //number of employees
				  	'cf2_field_10', //contact 1 name
					'phone-1-1', //contact 1 phone
					'phone-1-2', 
					'phone-1-3',
				  	'cf2_field_17']; //contact 1 email;
	var messages = ['Please enter Registered Company Name',
					'Please enter postal address',
					'Please enter number of employees',
					'Please enter name of Contact 1',
					'Please enter a valid phone number for Contact 1',
					'Please enter a valid phone number for Contact 1',
					'Please enter a valid phone number for Contact 1',
					'Please enter email of Contact 1'];
	for (var i=0; i < required.length; i++)
	{
		var currentElement = document.getElementById(required[i]);
		if (currentElement.value == null || currentElement.value == "")
		{
    		alert(messages[i]);
			currentElement.focus();
			return false;
    	}
	}
	
	//physical address
	var currentElement = document.getElementById('cf2_field_6');
	if (currentElement.checked != 1)
	{
		var physicalAddress = document.getElementById('cf2_field_7');
		if (physicalAddress.value == null || physicalAddress.value == "")
		{
    		alert('Please enter a physical address for Contact 1');
			physicalAddress.focus();
			return false;
    	}
	}
	
	//validate email 1
	currentElement = document.getElementById('cf2_field_17');
	if (validate_email(currentElement) == false)
	{
		return false;
	}
	//email 2 is not required, so check if it is blank first then validate
	currentElement = document.getElementById('cf2_field_27');
	if (currentElement.value != "")
	{
		if (validate_email(currentElement) == false)
		{
			return false;
		}
	}
	
	//have they agreed to terms and conditions?
	var chkbox = document.getElementById('cf2_field_64');
	if (chkbox.checked != 1)
	{
		alert('Please agree to the terms and conditions');
		chkbox.focus();
		return false;
	}
	return true;
} //validate

