// JavaScript for www.relatebradford.org.uk
// Created at The Cellar Project - www.thecellarproject.co.uk

function showItem(id)
{
	fs = document.getElementById("FS"+id);
	p = document.getElementById(id);
	if(fs)
		fs.style.display="block";
	p.style.display="block";
	
}

function hideItem(id)
{
	fs = document.getElementById("FS"+id);
	p = document.getElementById(id);
	if(fs)
		fs.style.display="none";
		p.style.display="none";
		// reset the values of fields being hidden
		if(id=="legacy"){
			document.getElementById("legacyLeft").checked = false;
			document.getElementById("legacyIntended").checked = false;
	
		} else if(id=="skills"){
			document.getElementById("strSkills").value = "";
		
		} else if(id=="employment"){
			document.getElementById("strEmployment").value = "";
		
		} else if(id=="appointment"){
			document.getElementById("strAppointment").value = "";
		
		}
}

function changeForm(id)
{	
	if(id==0){
		hideOnload();
	} else {
		var ids=new Array();
		ids[0]="legacy";
		ids[1]="trustees";
		ids[2]="skills";
		ids[3]="employment";
		ids[4]="appointment";	
	
		for(i=0;i<ids.length;i++)
		{
			if(ids[i]==id)
				showItem(id);
			else
				hideItem(ids[i]);
		}
	}
}

function hideOnload()
{
	hideItem("legacy");
	hideItem("trustees");
	hideItem("skills");
	hideItem("employment");
	hideItem("appointment");
}

function validateForm()
{
	blnFormOK = 1;
	// get the option selected
	var selectedID = document.getElementById("contactType").value;
	var arrInvalidFields = new Array();
	// create array of required fields
	var arrRequiredFieldIDs = new Array();
	arrRequiredFieldIDs[0]="firstName";
	arrRequiredFieldIDs[1]="surName";
	arrRequiredFieldIDs[2]="address1";
	arrRequiredFieldIDs[3]="postcode";
	arrRequiredFieldIDs[4]="email";
	arrRequiredFieldIDs[5]="confirmEmail";
	
	// decide which other field is required base on selected option
	if(selectedID == "appointment"){
		arrRequiredFieldIDs[6]="strAppointment";
	
	} else if(selectedID == "employment"){
		arrRequiredFieldIDs[6]="strEmployment";
	
	} else if(selectedID == "skills"){
		arrRequiredFieldIDs[6]="strSkills";
	}
	
	// if they havent selected a form type
	if(selectedID == "0"){
		blnFormOK = 0;
	
	// ok so they've picked a form type
	} else {
		// at this point we have our array of required fields
		// loop thru the array checking the values of the fields
		for(i=0;i<arrRequiredFieldIDs.length;i++) {
			strElementID = arrRequiredFieldIDs[i];
			strEnteredValue = document.getElementById(arrRequiredFieldIDs[i]).value;
			// if(i == 6){ alert(document.getElementById(arrRequiredFieldIDs[i]).innerText); }
			// check the field is populated
			if(!strEnteredValue.length){
				blnFormOK = 0;
			}
			// if the field is an email address, check that it is valid
			if(strElementID == "email"||strElementID == "confirmEmail"){
				// check is valid
				if(!checkIsValidEmail(strEnteredValue)){ blnFormOK = 0; }
			}
			
		}
		// if this is a legacy form, check an option has been selected
		if(selectedID == "legacy"){
			if(!document.getElementById("legacyLeft").checked && !document.getElementById("legacyIntended").checked){
				blnFormOK = 0;
			}
		}
		
	}
	
	// if the form is populated, submit it
	if(blnFormOK){
		document.getElementById("frmEnquiry").submit();
		// alert("submit form!")
	} else {
		// alert the user
		alert("Please ensure you have filled in all of the required fields.");
		// highlight fields necessary
		return false;
	}
	
}

function checkIsValidEmail(strEmailToCheck) {
	return true;
}