var pi_checkbox;
var company_checkbox;

function init(){

	pi_checkbox = document.getElementById("type-pi");
	company_checkbox = document.getElementById("type-company");
	
	pi_checkbox.onclick = pi_click;
	company_checkbox.onclick = company_click;

	document.getElementById('cnt').onsubmit = check_form;
}

function check_form(){
	var standard_ok = validateString(this.firstname, 'Please enter your first name', 3, 15) &&
					  validateString(this.surname, 'Please enter your last name', 3, 15) &&
					  validateEmail(this.email, 'Please enter a valid email address') &&
					  validateEmailsSame(this.email, this.email2, 'Email addresses are different') &&
					  validateString(this.address1, 'Please enter the first line of your address', 3, 15) &&
					  validateString(this.town, 'Please enter your town', 3, 15) &&
					  validateString(this.county, 'Please enter your county', 3, 15) &&
					  validateString(this.post_code, 'Please enter your post code', 3, 15) &&
					  validateString(this.phone, 'Please enter your telephone number', 3, 15);
	if(!standard_ok) return false;
	
	if(!document.getElementById("type-pi").checked && !document.getElementById("type-company").checked){
		alert("You much choose an enquiry type");
		return false;
	}
	if(document.getElementById("type-company").checked){
		return (validateString(this.company_name, 'Please enter your company name', 1, 99) && validateString(this.telephone, 'Please enter a contact telephone number', 1, 25));
	}
	else{
		return true;
	}
}

function pi_click(){
	pi_checkbox.checked = true;
	company_checkbox.checked = false;
	
	document.getElementById("company-fields").style.display = 'none';
}


function company_click(){
	pi_checkbox.checked = false;
	company_checkbox.checked = true;
	
	document.getElementById("company-fields").style.display = 'block';
}

window.onload = init;
