// 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("claim"))
	{
		var frmvalidator = new Validator("claim");
		frmvalidator.clearAllValidations();

		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");	
		frmvalidator.addValidation("dateofwill", "req", "Please enter the date of the will");
		frmvalidator.addValidation("address", "req", "Please enter your address");		
		frmvalidator.addValidation("postcode", "req", "Please enter your post code");				
		frmvalidator.addValidation("relationship", "req", "Please enter your relationship to the deceased");

		// 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();
	});
}