// Form constants

var NUMBER_OF_COURSES = 20; //20 booking lines
var LUNCH_COST = 7.00;

function getSelectedCourses()
{
	var aCourseCodes = [];

	for (var row = 1 ; row <= NUMBER_OF_COURSES ; row++ )
	{
		var courseCode = document.getElementById("CourseCode" + row).value.toString();
		if (courseCode != "")
		{
			aCourseCodes.push(courseCode);
			// re - calculate the cost of all the courses of on the page
			document.getElementById("cost" + row).value = getCost(aCourseCodes);
		}
	}
	return aCourseCodes;
}

function ldMenu(row) 
{
	//var Indx=mySubject;

	with(document.getElementById("course" + row))
	{
		options[0]=new Option("Choose a session","");
		options[1]=new Option("‘If I could turn back time’ – Managing Time & Delegation (MF14)","MF14");
		options[2]=new Option("‘Lean’ for Leaders (MF5)","MF5");
		options[3]=new Option("Advanced Communication MASTERCLASS (MF13)","MF13");
		options[4]=new Option("Building the Perfect Team (MF2)","MF2");
		options[5]=new Option("Building Trust within your Senior Management Team and Organisation (MF1)","MF1");
		options[6]=new Option("Communicating Upwards (MF16)","MF16");
		options[7]=new Option("Emotional Intelligence - Handling Relationships (MF11)","MF11");
		options[8]=new Option("Flex your Management Style (MF10)","MF10");
		options[9]=new Option("Get the Right Person in the Right Job (MF6)","MF6");
		options[10]=new Option("Leadership Development (MF15)","MF15");
		options[11]=new Option("Manager as Coach MASTERCLASS (MF17)","MF17");
		options[12]=new Option("Managing for High Performance (MF9)","MF9");
		options[13]=new Option("Managing Performance  (MF8)","MF8");
		options[14]=new Option("Motivate your Staff - TAP into potential MASTERCLASS (MF12)","MF12");
		options[15]=new Option("Nuts and Bolts of Managing People (MF7)","MF7");
		options[16]=new Option("Recognising & Managing Stress in Others (MF4)","MF4");
		options[17]=new Option("Resolving Conflict in a Team (MF3)","MF3");
		options[0].selected=true;
	}
}

function Results(row, select)
{
	//Populate the course code box
	document.getElementById("CourseCode" + row).value = select.options[select.selectedIndex].value;

	//get the cost
	//price1 = getCost(getSelectedCourses());
	//document.getElementById("cost" + row).value = price1;
    getSelectedCourses();
	calculateCourseCost();
	calculateLunchCost();
}

function check_form (){
		var terms_conditions = document.form1.terms_checkbox.checked;
		var check_name = document.form1.contact_name;
		var check_email = document.form1.email;
		var check_organisation = document.form1.organisation;
		var check_address = document.form1.address1;
		var check_telnumber = document.form1.tel;
		var check_postcode = document.form1.post_code;
		var check_confirm = document.form1.email_confirm;
	
		
		if (check_name.value == '')
		{
			alert("Please enter a contact name.");
			document.form1.contact_name.focus();
			return(false);
		}
		if (check_email.value == '')
		{
			alert("Please enter a valid email address.");
			document.form1.email.focus();
			return(false);
		}
		if (check_confirm.value == '')
		{
			alert("Please confirm your email address.");
			document.form1.email_confirm.focus();
			return(false);
		}
		if (check_organisation.value == '')
		{
			alert("Please enter the organisation name, if you do not have an organisation name then enter 'None'");
			document.form1.organisation.focus();
			return(false);
		}
		if (check_address.value == '')
		{
			alert("Please enter your full address including your post code.");
			document.form1.address1.focus();
			return(false);
		}
		if (check_postcode.value == '')
		{
			alert("Please enter your post code.");
			document.form1.post_code.focus();
			return(false);
		}
		if (check_telnumber.value == '')
		{
			alert("Please enter your telephone number.");
			document.form1.tel.focus();
			return(false);
		}		
	        
		if (terms_conditions == false)
		{
			alert("Please read and tick the terms & conditions checkbox.");
			document.form1.terms_checkbox.focus();
			return(false);
		}if (check_confirm.value == check_email.value) { return true; }

		{
			alert("Email addresses are different");
			document.form1.email.focus();
			return(false);
		}
		return(true);
		
}

function calculateCourseCost()
{
	var TotalCost = 0;
	
	for (var i = 1; i <= NUMBER_OF_COURSES; i++ )
	{
		if (document.getElementById("cost" + i).value != '')
		{
			TotalCost += parseFloat(document.getElementById("cost" + i).value);
		}
	}
	//display to two decimal places
	document.getElementById("CourseTotal").value = TotalCost.toFixed(2);

	//display early bird discount if one applies
	if (isEarlyBirdDiscountDate())
	{
		var discount = TotalCost * (1 - EARLY_BIRD_DISCOUNT_RATE);
		document.getElementById("DiscountTotal").value = discount.toFixed(2);
	}
	
	calculateCourseTotal();
}


function calculateLunchCost()
{
	var TotalCost = 0;
	
	for (var i = 1; i <= NUMBER_OF_COURSES; i++ )
	{
		TotalCost += parseFloat(document.getElementById("lunch" + i).value);
	}
	//display to two decimal places
	document.getElementById("LunchTotal").value = parseFloat(LUNCH_COST * TotalCost).toFixed(2);
	
	calculateCourseTotal();
}

function calculateCourseTotal()
{
	var lunchTotal = parseFloat(document.getElementById("LunchTotal").value).toFixed(2);
	var courseTotal = parseFloat(document.getElementById("CourseTotal").value).toFixed(2);
	
	with(document)
	{
		getElementById("GrandTotal").value = parseFloat(lunchTotal*1.00 + courseTotal*1.00).toFixed(2);
	}
}

function termsWindow(){
	terms_window = open("terms.html","terms",'scrollbars,width=600,height=700');
}


