// 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 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();
}

// form validation - core validation routine
function setupValidation()
{
	if(document.getElementById("executor"))
	{
		var frmvalidator = new Validator("executor");
		frmvalidator.clearAllValidations();
			
		frmvalidator.addValidation("starting_or_defending_a_claim", "dontselect=0", "Please select if you are starting or defending a claim");
		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 - 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");
		}		
	}
}


// global startup function
/////////////////////////////////////////////////
if(document.getElementById && document.createTextNode)
{
	addEvent(window, 'load', function(e)
	{
		// 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();		
	});
	

}