
function submit_form()
{
	var the_form = document.getElementById("quote_form");
	
	the_form.action = URL_SITE + 'form.php?sid=72&act=submit_form';
	the_form.submit();
}

function save_form()
{
	var the_form = document.getElementById("quote_form");
	
	the_form.action = URL_SITE+"form.php?sid=129";
	the_form.submit();
}

function go_to_previous_stage()
{
	if(document.getElementById("previous_page"))
	{
		var previous_page = document.getElementById("previous_page").value;
	}
	
	document.getElementById("quote_form").action = URL_SITE + "form.php?sid=" + previous_page
	document.getElementById("quote_form").submit();
}

function go_to_next_stage()
{
	if(document.getElementById("next_page"))
	{
		var next_page = document.getElementById("next_page").value;
	}
	
	document.getElementById("quote_form").action = URL_SITE + "form.php?sid=" + next_page
	document.getElementById("quote_form").submit();
}


function show_applicant(total_applicants, to_show)
{
	for(i = 1;i <= total_applicants;i++)
	{
		document.getElementById("applicant_details_"+i).style.display = "none";
		if(total_applicants > 1)
		{
			document.getElementById("tab_"+i).style.backgroundColor = "#ffffff";
			document.getElementById("tab_"+i).style.color = "#646464";
		}
	}
	
	document.getElementById("applicant_details_"+to_show).style.display = "block";
	
	if(total_applicants > 1)
	{
		document.getElementById("tab_"+to_show).style.backgroundColor = "#6B126D";
		document.getElementById("tab_"+to_show).style.color = "#ffffff";
	}
	document.location = "#top";
}

function v_text(input_id, input_desc)
{
	var the_input = document.getElementById(input_id);
	
	if(the_input.value == "")
	{
		alert("Please enter a value for '"+input_desc+"'");
		the_input.focus();
		return false;
	}else{
		return true;
	}
}

function v_email(input_id, input_desc)
{
	var the_input = document.getElementById(input_id);
	
	if(!isValidEmailAddr(the_input.value))
	{
		alert("Please check the email address supplied.\nIt appears not to be correct.");
		the_input.focus();
		return false;
	}else{
		return true;
	}
}

function v_select(input_id, input_desc)
{
	var the_input = document.getElementById(input_id);
	
	if(the_input.options.selectedIndex == 0)
	{
		alert("Please select a value for '"+input_desc+"'");
		the_input.focus();
		return false;
	}else{
		return true;
	}
}

function v_radio(input_id, input_desc)
{
	var the_inputs = eval("document.getElementById('quote_form')." + input_id);
	var sel = 0;
	
	for(var p = 0;p < the_inputs.length;p++)
	{
    if(the_inputs[p].checked)
		{
   		sel = 1;
   		break;
		}
	}   	
	if(sel == 0)
	{
		alert("Please select an option for '"+input_desc+"'");
		return false;
	}else{
		return true;
	}
}

function activate(num, input_id, ext)
{
	document.getElementById("question_"+num+"_number"+ext).style.color = "#640466";
	document.getElementById("question_"+num+ext).style.color = "#640466";
	document.getElementById(input_id).disabled = false;
}

function deactivate(num, input_id, ext)
{
	document.getElementById("question_"+num+"_number"+ext).style.color = "#AD90AC";
	document.getElementById("question_"+num+ext).style.color = "#AD90AC";
	document.getElementById(input_id).disabled = true;
}

function deactivate_radio(num, input_id, ext)
{
	if(ext == "")
	{
		var the_inputs = eval("document.getElementById('quote_form')." + input_id);
	}else{
		var the_inputs = eval("document.getElementById('quote_form')." + input_id);
	}
	
	document.getElementById("question_"+num+"_number"+ext).style.color = "#AD90AC";
	document.getElementById("question_"+num+ext).style.color = "#AD90AC";
	the_inputs[0].disabled = true;
	the_inputs[1].disabled = true;
}

function activate_radio(num, input_id, ext)
{
	if(ext == "")
	{
		var the_inputs = eval("document.getElementById('quote_form')." + input_id);
	}else{
		var the_inputs = eval("document.getElementById('quote_form')." + input_id);
	}
	
	document.getElementById("question_"+num+"_number"+ext).style.color = "#640466";
	document.getElementById("question_"+num+ext).style.color = "#640466";
	the_inputs[0].disabled = false;
	the_inputs[1].disabled = false;
}


/* STAGE 1 CODE */

function render_stage_1()
{
	deactivate(4, "your_name", "");
	deactivate(5, "your_company", "");
	deactivate(6, "your_telephone", "");
	deactivate(7, "your_email", "");
	deactivate(8, "number_of_applicants", "");

	switch(document.getElementById("quote_for").selectedIndex)
	{
		case 2:
			activate(8, "number_of_applicants", "");
		break;
		case 3:
			activate(4, "your_name", "");
			activate(5, "your_company", "");
			activate(6, "your_telephone", "");
			activate(7, "your_email", "");
			activate(8, "number_of_applicants", "");
		break;
	}
}

function validate_stage_1()
{
	var the_form = document.getElementById("quote_form");
	
	if(!v_select("country_of_purchase", "Country of purchase"))
	{
		return false;
	}
	
	if(!v_select("source", "How did you find out about us?"))
	{
		return false;
	}
	
	if(!v_select("quote_for", "Who is the quote for?"))
	{
		return false;
	}
	
	switch(document.getElementById("quote_for").selectedIndex)
	{
		case 2:
			if(!v_select("number_of_applicants", "Number of applicants"))
			{
				return false;
			}
		break;
		case 3:
			if(!v_text("your_name", "Your name"))
			{
				return false;
			}
			if(!v_text("your_company", "Your company"))
			{
				return false;
			}
			if(!v_text("your_telephone", "Your telephone"))
			{
				return false;
			}
			if(!v_email("your_email", "Your email"))
			{
				return false;
			}
			if(!v_select("number_of_applicants", "Number of applicants"))
			{
				return false;
			}
		break;
	}
	return true;
}

/* STAGE 2 CODE */

function render_stage_2()
{
	
}

function validate_stage_2()
{
	var the_form = document.getElementById("quote_form");
	
	if(!v_select("stage_of_buying_process", "Stage of buying process"))
	{
		return false;
	}
	
	if(!v_select("type_of_property", "Type of property"))
	{
		return false;
	}
	
	if(!v_select("property_state", "Is the property?"))
	{
		return false;
	}
	
	if(!v_select("purchase_currency", "Purchase currency"))
	{
		return false;
	}
	
	if(!v_text("purchase_price", "Purchase price"))
	{
		return false;
	}
	
	if(!v_select("borrowing_currency", "Borrowing currency"))
	{
		return false;
	}
	
	if(!v_text("borrowing_amount", "Borrowing amount"))
	{
		return false;
	}
	
	if(!v_select("type_of_loan", "Type of loan"))
	{
		return false;
	}
	
	if(!v_select("term_of_loan", "Term of loan"))
	{
		return false;
	}
	
	if(!v_radio("requires_stage_payments", "Stage payments"))
	{
		return false;
	}
	
	return true;
}

/* STAGE 3 CODE */

function render_stage_3()
{
	var num_applicants = 1;
	var extension = "";
	var the_inputs = "";
	
	if(document.getElementById("number_of_applicants"))
	{
		num_applicants_box = document.getElementById("number_of_applicants");
	}
	
	if(document.getElementById('applicant_title').selectedIndex==6)
	{
		document.getElementById('applicant_title_other').style.display='block';
	}else{
		document.getElementById('applicant_title_other').style.display='none';
	}
	
	deactivate(37, "value_of_main_residence","");
	deactivate(38, "outstanding_mortgage","");
	deactivate(39, "number_of_other_properties","");
	deactivate_radio(41, "passed_probation","");
	deactivate(42, "occupation","");
	deactivate_radio(43, "two_years_recorded_accounts","");
	deactivate(48, "monthly_mortgage_repayments","");
	deactivate(49, "monthly_mortgage_repayments_on_other","");
	deactivate(50, "monthly_rental_payments","");

	the_inputs = document.getElementById("quote_form").owns_current_residence;
	
	if(the_inputs[0].checked)
	{
		activate(37, "value_of_main_residence","");
		activate(38, "outstanding_mortgage","");
		activate(39, "number_of_other_properties","");
		activate(48, "monthly_mortgage_repayments","");
	}

	if(the_inputs[1].checked)
	{
		document.getElementById("number_of_other_properties").selectedIndex = 0;
		activate(50, "monthly_rental_payments","");
	}
	
	switch(document.getElementById("nature_of_current_employment").selectedIndex)
	{
		case 1:
			activate_radio(41, "passed_probation","");
			activate(42, "occupation","");
		break;
		case 2:
			activate(42, "occupation","");
			activate_radio(43, "two_years_recorded_accounts","");
		break;
	}

	switch(document.getElementById("number_of_other_properties").selectedIndex)
	{
		case 0:
		
		break;
		case 1:
		
		break;
		default:
			activate(49, "monthly_mortgage_repayments_on_other","");
		break;
	}
	
	/* PROCESS FURTHER APPLICANTS */

	if(num_applicants_box.value > 1)
	{
		for(var i=2;i <= num_applicants_box.value;i++)
		{
			ext = "_" + i;

			if(document.getElementById("applicant_title_"+i).selectedIndex==6)
			{
				document.getElementById("applicant_title_other_"+i).style.display='block';
			}else{
				document.getElementById("applicant_title_other_"+i).style.display='none';
			}

			deactivate(37, "value_of_main_residence_"+i, ext);
			deactivate(38, "outstanding_mortgage_"+i, ext);
			deactivate(39, "number_of_other_properties_"+i, ext);
			deactivate_radio(41, "passed_probation_"+i, ext);
			deactivate(42, "occupation_"+i, ext);
			deactivate_radio(43, "two_years_recorded_accounts_"+i, ext);
			deactivate(48, "monthly_mortgage_repayments_"+i, ext);
			deactivate(49, "monthly_mortgage_repayments_on_other_"+i, ext);
			deactivate(50, "monthly_rental_payments_"+i, ext);
			
			the_inputs = eval("document.getElementById('quote_form').owns_current_residence_"+i);
			
			if(the_inputs[0].checked)
			{
				activate(37, "value_of_main_residence_"+i, ext);
				activate(38, "outstanding_mortgage_"+i, ext);
				activate(39, "number_of_other_properties_"+i, ext);
				activate(48, "monthly_mortgage_repayments_"+i, ext);
			}

			if(the_inputs[1].checked)
			{
				document.getElementById("number_of_other_properties_"+i).selectedIndex = 0;
				activate(50, "monthly_rental_payments_"+i, ext);
			}

			switch(document.getElementById("nature_of_current_employment_"+i).selectedIndex)
			{
				case 1:
					activate_radio(41, "passed_probation_"+i, ext);
					activate(42, "occupation_"+i, ext);
				break;
				case 2:
					activate(42, "occupation_"+i, ext);
					activate_radio(43, "two_years_recorded_accounts_"+i, ext);
				break;
			}
		
			switch(document.getElementById("number_of_other_properties_"+i).selectedIndex)
			{
				case 0:
				
				break;
				case 1:
				
				break;
				default:
					activate(49, "monthly_mortgage_repayments_on_other_"+i, ext);
				break;
			}
		}
	}
}

function validate_stage_3(current_applicant)
{
	var the_form = document.getElementById("quote_form");
	
	if(current_applicant == 1)
	{
		if(!v_select("applicant_title", "Title"))
		{
			return false;
		}
		
		if(!v_text("first_name", "First name"))
		{
			return false;
		}
		
		if(!v_text("surname", "Surname"))
		{
			return false;
		}
		
		if(!v_select("dob_day", "Date of birth - Day"))
		{
			return false;
		}
		if(!v_select("dob_month", "Date of birth - Month"))
		{
			return false;
		}
		if(!v_select("dob_year", "Date of birth - Year"))
		{
			return false;
		}
		
		if(!v_text("telephone_number", "Telephone number"))
		{
			return false;
		}
		if(!v_select("telephone_number_type", "Telephone number type"))
		{
			return false;
		}
		
		if(!v_email("email_address", "Email address"))
		{
			return false;
		}
		
		if(!v_email("email_address_confirmation", "Email address confirmation"))
		{
			return false;
		}
		
		if(document.getElementById("email_address_confirmation").value != document.getElementById("email_address").value)
		{
			alert("Please confirm the email address");
			document.getElementById("email_address_confirmation").focus();
			return false;
		}
		
		if(!v_text("address_1", "Address 1"))
		{
			return false;
		}
		if(!v_text("town_or_city", "Town or city"))
		{
			return false;
		}
		if(!v_text("postcode", "Postcode"))
		{
			return false;
		}
		
		if(!v_select("country", "Country"))
		{
			return false;
		}
		
		if(!v_text("nationality", "Nationality"))
		{
			return false;
		}
		
		if(!v_radio("owns_current_residence", "Current residence"))
		{
			return false;
		}
		
		the_inputs = document.getElementById("quote_form").owns_current_residence;
		
		if(the_inputs[0].checked)
		{
			if(!v_text("value_of_main_residence", "Value of main residence"))
			{
				return false;
			}
			if(!v_text("outstanding_mortgage", "Outstanding mortgage"))
			{
				return false;
			}
			if(!v_select("number_of_other_properties", "Number of other properties"))
			{
				return false;
			}
		}
		
		if(!v_select("nature_of_current_employment", "Nature of current employment"))
		{
			return false;
		}

		switch(document.getElementById("nature_of_current_employment").selectedIndex)
		{
			case 1:
				if(!v_radio("passed_probation", "Passed probation"))
				{
					return false;
				}
				if(!v_text("occupation", "Occupation"))
				{
					return false;
				}
			break;
			case 2:
				if(!v_text("occupation", "Occupation"))
				{
					return false;
				}
				if(!v_radio("two_years_recorded_accounts", "Two years recorded accounts"))
				{
					return false;
				}
			break;
		}
		
		if(!v_text("gross_annual_salary", "Gross annual salary"))
		{
			return false;
		}
		
		the_inputs = document.getElementById("quote_form").owns_current_residence;
		
		if(the_inputs[1].checked)
		{
			if(!v_text("monthly_rental_payments", "Monthly rental payments"))
			{
				return false;
			}
		}
		
		switch(document.getElementById("number_of_other_properties").selectedIndex)
		{
			case 0:
				
			break;
			case 1:
				if(!v_text("monthly_mortgage_repayments", "Monthly mortgage repayments"))
				{
					return false;
				}
			break;
			default:
				if(!v_text("monthly_mortgage_repayments", "Monthly mortgage repayments"))
				{
					return false;
				}
				if(!v_text("monthly_mortgage_repayments_on_other", "Monthly mortgage repayments on other properties"))
				{
					return false;
				}
			break;
		}
		
		if(!v_radio("ever_been_in_arrears", "Ever been in arrears?"))
		{
			return false;
		}
		if(!v_radio("ever_declared_bankrupt", "Ever been declared bankrupt?"))
		{
			return false;
		}
		
		if(!document.getElementById("has_read_terms").checked)
		{
			alert("Please indicate that you have read the terms and conditions");
			return false;
		}
	}else{
		if(!v_select("applicant_title_"+current_applicant, "Title"))
		{
			return false;
		}
		
		if(!v_text("first_name_"+current_applicant, "First name"))
		{
			return false;
		}
		
		if(!v_text("surname_"+current_applicant, "Surname"))
		{
			return false;
		}
		
		if(!v_select("dob_day_"+current_applicant, "Date of birth - Day"))
		{
			return false;
		}
		if(!v_select("dob_month_"+current_applicant, "Date of birth - Month"))
		{
			return false;
		}
		if(!v_select("dob_year_"+current_applicant, "Date of birth - Year"))
		{
			return false;
		}

		if(!v_radio("owns_current_residence_"+current_applicant, "Current residence"))
		{
			return false;
		}

		the_inputs = eval("document.getElementById('quote_form').owns_current_residence_"+current_applicant);
		
		if(the_inputs[0].checked)
		{
			if(!v_text("value_of_main_residence_"+current_applicant, "Value of main residence"))
			{
				return false;
			}
			if(!v_text("outstanding_mortgage_"+current_applicant, "Outstanding mortgage"))
			{
				return false;
			}
			if(!v_select("number_of_other_properties_"+current_applicant, "Number of other properties"))
			{
				return false;
			}
		}
		
		if(!v_select("nature_of_current_employment_"+current_applicant, "Nature of current employment"))
		{
			return false;
		}

		switch(document.getElementById("nature_of_current_employment_"+current_applicant).selectedIndex)
		{
			case 1:
				if(!v_radio("passed_probation_"+current_applicant, "Passed probation"))
				{
					return false;
				}
				if(!v_text("occupation_"+current_applicant, "Occupation"))
				{
					return false;
				}
			break;
			case 2:
				if(!v_text("occupation_"+current_applicant, "Occupation"))
				{
					return false;
				}
				if(!v_radio("two_years_recorded_accounts_"+current_applicant, "Two years recorded accounts"))
				{
					return false;
				}
			break;
		}
		
		if(!v_text("gross_annual_salary_"+current_applicant, "Gross annual salary"))
		{
			return false;
		}
		
		the_inputs = eval("document.getElementById('quote_form').owns_current_residence_"+current_applicant);
		
		if(the_inputs[1].checked)
		{
			if(!v_text("monthly_rental_payments_"+current_applicant, "Monthly rental payments"))
			{
				return false;
			}
		}
		
		switch(document.getElementById("number_of_other_properties_"+current_applicant).selectedIndex)
		{
			case 0:
				
			break;
			case 1:
				if(!v_text("monthly_mortgage_repayments_"+current_applicant, "Monthly mortgage repayments"))
				{
					return false;
				}
			break;
			default:
				if(!v_text("monthly_mortgage_repayments_"+current_applicant, "Monthly mortgage repayments"))
				{
					return false;
				}
				if(!v_text("monthly_mortgage_repayments_on_other_"+current_applicant, "Monthly mortgage repayments on other properties"))
				{
					return false;
				}
			break;
		}
		
		if(!v_radio("ever_been_in_arrears_"+current_applicant, "Ever been in arrears?"))
		{
			return false;
		}
		if(!v_radio("ever_declared_bankrupt_"+current_applicant, "Ever been declared bankrupt?"))
		{
			return false;
		}

	}
	return true;
}