$(function() {
	$(document).ready(function(){
		if (document.forms['seminarform']) {
			var f = document.forms['seminarform'];
			if (f.elements['a2jobtitle'].value.length < 1 && f.elements['a2firstname'].value.length < 1 && f.elements['a2email'].value.length < 1) {
				$("#a2").hide();
			}
			if (f.elements['a3jobtitle'].value.length < 1 && f.elements['a3firstname'].value.length < 1 && f.elements['a3email'].value.length < 1) {
				$("#a3").hide();
			}
			if (f.elements['a4jobtitle'].value.length < 1 && f.elements['a4firstname'].value.length < 1 && f.elements['a4email'].value.length < 1) {
				$("#a4").hide();
			}
			if (f.elements['a5jobtitle'].value.length < 1 && f.elements['a5firstname'].value.length < 1 && f.elements['a5email'].value.length < 1) {
				$("#a5").hide();
			}
		} else {
			$("#a2").hide();
			$("#a3").hide();
			$("#a4").hide();
			$("#a5").hide();
		}

		if ($("#posted").val() == 1) {
			if ($("#sameasabove").attr("checked") != "checked") {
				$("#sameasabove").click();
			}
		} else {
			$("#billingInfo").hide();
		}

		$("#sameasabove").click(function(){ 
			$("#billingInfo").toggle(); 
		});


		$("[id^='a1']").change(function(){
			if ($(this).val().length > 0) {
				$("#a2").show();
			}
			calculateCost();
		});

		$("[id^='a2']").change(function(){
			if ($(this).val().length > 0) {
				$("#a3").show();
			}
			calculateCost();
		});

		$("[id^='a3']").change(function(){
			if ($(this).val().length > 0) {
				$("#a4").show();
			}
			calculateCost();
		});

		$("[id^='a4']").change(function(){
			if ($(this).val().length > 0) {
				$("#a5").show();
			}
			calculateCost();
		});

		$("[id^='a5']").change(function(){
			calculateCost();
		});

		$("#contactattending").click(function(){
			if ($(this).attr("checked")) {
				$("#a1firstname").val($("#contactfirstname").val() + " " + $("#contactsurname").val());
				$("#a1jobtitle").val($("#contactjobtitle").val());
				$("#a1email").val($("#contactemail").val());
				$("[id^='a1']").attr("readonly", "readonly");
				$("[id^='a1']").change();
			} else {
				$("[id^='a1']").removeAttr("readonly");
			}
		});

		$("#seminarform").submit(function(){
			return sendSeminarForm();
		});

	});
});

function calculateCost() {
	var f = document.forms['seminarform'];
	var prices = [];
	if (f.elements['a1jobtitle'].value.length > 0 || f.elements['a1firstname'].value.length > 0 || f.elements['a1email'].value.length > 0) {
		prices[0] = f.elements['a1attendencetype'].value;
	}
	if (f.elements['a2jobtitle'].value.length > 0 || f.elements['a2firstname'].value.length > 0 || f.elements['a2email'].value.length > 0) {
		prices[1] = f.elements['a2attendencetype'].value;
	}
	if (f.elements['a3jobtitle'].value.length > 0 || f.elements['a3firstname'].value.length > 0 || f.elements['a3email'].value.length > 0) {
		prices[2] = f.elements['a3attendencetype'].value;
	}
	if (f.elements['a4jobtitle'].value.length > 0 || f.elements['a4firstname'].value.length > 0 || f.elements['a4email'].value.length > 0) {
		prices[3] = f.elements['a4attendencetype'].value;
	}
	if (f.elements['a5jobtitle'].value.length > 0 || f.elements['a5firstname'].value.length > 0 || f.elements['a5email'].value.length > 0) {
		prices[4] = f.elements['a5attendencetype'].value;
	}

	var total = 0;
	for(i=0; i<prices.length; i++) { 
		if (prices[i] == 'both days') total += 598;
		if (prices[i] == 'day one' || prices[i] == 'day two') total += 349;
	}

	var cost = formatCurrency(total);
	document.getElementById('totalattending').innerHTML = prices.length;
	document.getElementById('totalcostdisplay').innerHTML = cost;
	f.elements['totalcost'].value = total;
}

function formatCurrency( num ) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function sendSeminarForm() {
	var message = '';
	var f = document.forms['seminarform'];
	calculateCost();
	if (f.companyname.value.length < 1) {
		$(f.companyname).addClass('errorField');
		message = message + '<p class="alert" id="companyname_message">Please enter a company name!</p>\n';
		$(f.companyname).focus(function() {
			clearClass($(this));
		});
	}
	if (f.companyaddress.value.length < 1) {
		$(f.companyaddress).addClass('errorField');
		message = message + '<p class="alert" id="companyaddress_message">Please enter a company address!</p>\n';
		$(f.companyaddress).focus(function() {
			clearClass($(this));
		});
	}
	if (f.companysuburb.value.length < 1) {
		$(f.companysuburb).addClass('errorField');
		message = message + '<p class="alert" id="companysuburb_message">Please enter a company suburb!</p>\n';
		$(f.companysuburb).focus(function() {
			clearClass($(this));
		});
	}
	if (f.companycountry.value.length < 1) {
		$(f.companycountry).addClass('errorField');
		message = message + '<p class="alert" id="companycountry_message">Please enter a company country!</p>\n';
		$(f.companycountry).focus(function() {
			clearClass($(this));
		});
	}
	if (f.companyindustry.options[0].selected) {
		$(f.companyindustry).addClass('errorField');
		message = message + '<p class="alert" id="companyindustry_message">Please select the company industry!</p>\n';
		$(f.companyindustry).focus(function() {
			clearClass($(this));
		});
	}
	if (f.contactfirstname.value.length < 1) {
		$(f.contactfirstname).addClass('errorField');
		message = message + '<p class="alert" id="contactfirstname_message">Please enter the contacts first name!</p>\n';
		$(f.contactfirstname).focus(function() {
			clearClass($(this));
		});
	}
	if (f.contactsurname.value.length < 1) {
		$(f.contactsurname).addClass('errorField');
		message = message + '<p class="alert" id="contactsurname_message">Please enter the contacts surname!</p>\n';
		$(f.contactsurname).focus(function() {
			clearClass($(this));
		});
	}
	if (f.contactphone.value.length < 1) {
		$(f.contactphone).addClass('errorField');
		message = message + '<p class="alert" id="contactphone_message">Please enter the contacts phone number!</p>\n';
		$(f.contactphone).focus(function() {
			clearClass($(this));
		});
	}
	if (!f.agree.checked) {
		message = message + '<p class="alert" id="agree_message">Please read and agree to the cancellation and privacy policies.</p>\n';
		$(f.agree).focus(function() {
			clearClass($(this));
		});
	}

	if (f.captcha.value.length < 1) {
		$(f.captcha).addClass('errorField');
		message = message + '<p class="alert" id="captcha_message">Please copy the four letters shown into the field provided, this helps us reduce spam so we can get back to you quicker.</p>\n';
		$(f.captcha).focus(function() {
			clearClass($(this));
		});
	}

	if (message.length > 0) {
		var output = document.getElementById('listcompanyform_results');
		output.style.display = 'block';
		output.innerHTML = message;
		location.href = '#listcompanyform_results';
		return false;
	}
	return true;


	if (!f.elements['agree'].checked) {
		alert('Please read and agree to the Cancellation and Privacy Policies.');
		return false;
	}

	return true;
}

function clearClass ( e ) {
	$(e).removeClass('errorField');
	$("#" + $(e).attr("name") + "_message").fadeOut("slow");
	$(e)[0].focus();
}

function setCaptcha() {
	var f = document.forms['seminarform'];
	$(f.captcha).addClass('errorField');
	$(f.captcha).focus(function() {
		clearClass($(this));
	});
	document.getElementById('listcompanyform_results').style.display = 'block';
	location.href = '#listcompanyform_results';
}
