// JavaScript Document


function checkRIF(theForm)
{
	// 1. Check they have entered a name
	if (theForm.name.value == "" )
	{
	 alert("Please enter a name.");
	 theForm.name.focus();
	 return false;
	}
	
	// 2. Check they have at least entered addrs. 
	if (theForm.address.value == "" )
	{
	 alert("Please enter an address.");
	 theForm.address.focus();
	 return false;
	}
	
	// 3. Check they have entered a postcode. 
	if (theForm.postcode.value == "" )
	{
	 alert("Please enter a Post Code.");
	 theForm.postcode.focus();
	 return false;
	}

	// 4. Have they answered the individual/organisation question
	if (!theForm.responding_as_type_id[0].checked && !theForm.responding_as_type_id[1].checked )
	{
	 alert("Please indicate whether you are responding as an individual or an organisation.");
	 return false;
	}
	else {
		// which one is checked (individual or organisation?)
		if (theForm.responding_as_type_id[0].checked) { 
			// test that at least one individual permission option is also checked
			if (!theForm.individual_response_permission[0].checked && !theForm.individual_response_permission[1].checked){
				alert ("Please give an answer to question 2a in your details");
				return false;
			}
	
			else if (theForm.individual_response_permission[0].checked){

			
					if( !theForm.confidentiality_type_id[0].checked && !theForm.confidentiality_type_id[1].checked && !theForm.confidentiality_type_id[2].checked ){
					alert ("Please give an answer to question 2b in your details");
						return false;
					}
			}
		}
		else{
			// Answering as a group. Test that at least one group option is also checked
			if (!theForm.group_organisation_permission[0].checked && !theForm.group_organisation_permission[1].checked){
				alert ("Please give an answer to question 3 in your details");
				return false;
			}
			
		}
	
	}
	
	// 5. Generic form validation
	var x = theForm.elements;
	for (var i=0;i<x.length;i++) {
		if (x[i].className.indexOf("required") > -1) {
			// Ordinary text box
			if (x[i].tagName.toLowerCase() == 'input' && x[i].type=='text' && !x[i].value) {
				alert(x[i].title + " is required.");
				return false;
			}
				
			// Multiline text box
			if (x[i].tagName.toLowerCase() == 'textarea' && !x[i].value) {
				alert(x[i].title + " is required.");
				return false;
			}
			
			//alert(x[i].tagName);
			// Dropdown list
			if (x[i].tagName.toLowerCase() == 'select') {
				if (x[i].selectedIndex < 1) {
					alert(x[i].title + " is required.");
					return false;
				}
				if (x[i].selectedIndex > 0)
					if (x[i].options[x[i].selectedIndex].value == '') {
						alert(x[i].title + " is required.");
						return false;
					}
			}
		}
		
		if (x[i].tagName.toLowerCase() == 'textarea' && x[i].className.toLowerCase().indexOf("maxlength") > -1) {
			var maxlength = x[i].className.substring(x[i].className.indexOf("maxlength")+10)+" "
			
			maxlength = maxlength.substring(0,maxlength.indexOf(" "))
			
			if (x[i].value.length > maxlength) {
				if (x[i].className.toLowerCase().indexOf("truncate") > -1) {
					x[i].value = x[i].value.substring(0,maxlength);
					alert(x[i].title + " limited to " + maxlength + " letters.");
				} else {
					alert(x[i].title + " limited to " + maxlength + " letters. Current length is " + x[i].value.length + ".");
				}
				return false;
			}
			
			//return false;
		}
	}
}

function toggleOrg(state) {

		// Turn on / off the organisation fields
	
		//clear all fields
		document.getElementById("group_organisation_permission1").checked = false; 
		document.getElementById("group_organisation_permission2").checked = false; 	
	
		document.getElementById("group_organisation_permission1").disabled = state; 
		document.getElementById("group_organisation_permission2").disabled = state; 	
		
}

function toggleInd(state) {

		// Turn on / off the individual fields
		
		//clear all the fields
		document.getElementById("individual_response_permission1").checked = false; 
		document.getElementById("individual_response_permission2").checked = false;
		document.getElementById("confidentiality_type_id1").checked = false;
		document.getElementById("confidentiality_type_id2").checked = false;
		document.getElementById("confidentiality_type_id3").checked = false;
				
		document.getElementById("individual_response_permission1").disabled = state; 
		document.getElementById("individual_response_permission2").disabled = state;
		
		if (state){		
			document.getElementById("confidentiality_type_id1").disabled = state;
			document.getElementById("confidentiality_type_id2").disabled = state;
			document.getElementById("confidentiality_type_id3").disabled = state;
		}		

}

function toggle2b(state) {

		// Turn on / off the individual confidentiality fields
			
		document.getElementById("confidentiality_type_id1").disabled = state;
		document.getElementById("confidentiality_type_id2").disabled = state;
		document.getElementById("confidentiality_type_id3").disabled = state;
}


