// JavaScript Document

// toggle visibility for a dom element
function toggleDomElementVisibility(domElement, visibility)
{
	if(visibility == true)
	{	
		domElement.style.display = "block";
	}
	else
	{
		domElement.style.display = "none";
	}
}

// restructure form based on 
function restructureLifeStatusChanged()
{
	// check the current value
	if(document.getElementById("life_status").selectedIndex == 1)
	{
		// show deceased last 6 months
		toggleDomElementVisibility(document.getElementById("deceased_date_li"), true);
	}
	else
	{
		// hide deceased last 6 months
		toggleDomElementVisibility(document.getElementById("deceased_date_li"), false);
	}
	
	// re-apply validation	
	setupValidation();
}

// restructure form based on 
function restructureOriginChanged()
{
	// check the current value
	if(document.getElementById("origin").selectedIndex == 2)
	{
		// show non uk panels
		toggleDomElementVisibility(document.getElementById("origin_li"), true);
		toggleDomElementVisibility(document.getElementById("origin_nonuk_li"), true);
		toggleDomElementVisibility(document.getElementById("origin_ukwritten_li"), true);		
	}
	else
	{
		// show only uk panel
		toggleDomElementVisibility(document.getElementById("origin_li"), true);
		toggleDomElementVisibility(document.getElementById("origin_nonuk_li"), false);
		toggleDomElementVisibility(document.getElementById("origin_ukwritten_li"), false);		
	}
	
	// re-apply validation	
	setupValidation();
}

// restructure form based on mental capacity
function restructureMentalCapacity()
{
	// check the dom element for value
	var checkboxReason = document.getElementById("challenge_reason_4");
	
	if(checkboxReason.checked == true)
	{
		// display the panels
		toggleDomElementVisibility(document.getElementById("mentalSelectBox"), true);
		toggleDomElementVisibility(document.getElementById("mentalOther"), true);
	}
	else
	{
		// hide the panels
		toggleDomElementVisibility(document.getElementById("mentalSelectBox"), false);
		toggleDomElementVisibility(document.getElementById("mentalOther"), false);
	}
	
	// re-apply validation	
	setupValidation();
}

// form validation - core validation routine
function setupValidation()
{
	if(document.getElementById("validity"))
	{
		var frmvalidator = new Validator("validity");
		frmvalidator.clearAllValidations();
			
		frmvalidator.addValidation("starting_or_defending_a_claim", "dontselect=0", "Please select if you are starting or defending a claim");

		// form restructuring - deceased?
		frmvalidator.addValidation("life_status", "dontselect=0", "Please select if the person who made the will is deceased");		
		if(document.getElementById("life_status").selectedIndex == 1)
		{
			frmvalidator.addValidation("deceased_date", "dontselect=0", "Please select if the deceased died less than 6 months ago");
		}

		frmvalidator.addValidation("already_sought_legal_advice", "dontselect=0", "Please select if you have already sought legal advice");
		frmvalidator.addValidation("fullname", "req", "Please enter your name");
		frmvalidator.addValidation("phonenumber", "req", "Please enter your phone number");
		frmvalidator.addValidation("email", "req", "Please enter your email address");
		frmvalidator.addValidation("email", "email", "Please enter a valid email address");		
		
		// form restructuring - mental health
		if(document.getElementById("challenge_reason_4").checked == true)
		{
			// enable select box required
			frmvalidator.addValidation("capacity", "dontselect=0", "Please select the reason for incapacity");
			
			// enable other validation if capacity set to other
			if(document.getElementById("capacity").selectedIndex == 5)
			{
				frmvalidator.addValidation("capacity_other", "req", "Please enter a reason for \"other mental incapacity\"");
			}
		}
				
		// form restructuring - origin
		frmvalidator.addValidation("origin", "dontselect=0", "Please select the country you are enquiring from");
		if(document.getElementById("origin").selectedIndex == 2)
		{
			frmvalidator.addValidation("nonukcountry", "dontselect=0", "Please select the country you are enquiring from");
			frmvalidator.addValidation("ukwritten", "dontselect=0", "Please select yes or no to whether the will was written in the UK");
		}
		
		frmvalidator.addValidation("whodraftedthewill", "dontselect=0", "Please select who made the will");
	}
}

// global startup function
/////////////////////////////////////////////////
if(document.getElementById && document.createTextNode)
{
	addEvent(window, 'load', function(e)
	{
		// form restructuring - deceased?
		addEvent(document.getElementById("life_status"), 'change', restructureLifeStatusChanged);		
		toggleDomElementVisibility(document.getElementById("deceased_date_li"), false);
		
		// form restructuring - mental health
		addEvent(document.getElementById("challenge_reason_4"), 'click', restructureMentalCapacity);
		toggleDomElementVisibility(document.getElementById("mentalSelectBox"), false);
		toggleDomElementVisibility(document.getElementById("mentalOther"), false);
		
		// form restructuring - mental health - other
		addEvent(document.getElementById("capacity"), 'change', setupValidation);
		
		// form restructuring - hide non uk
		addEvent(document.getElementById("origin"), 'change', restructureOriginChanged);		
		toggleDomElementVisibility(document.getElementById("origin_nonuk_li"), false);
		toggleDomElementVisibility(document.getElementById("origin_ukwritten_li"), false);		

		// form validation
		setupValidation();		
	});
	

}