// 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("general"))
	{
		var frmvalidator = new Validator("general");
		frmvalidator.clearAllValidations();
			
		frmvalidator.addValidation("nature_of_enquiry", "req", "Please enter the nature of your enquiry");
		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();
	});
}