// *** Functions to ensure validation is not called should the user click the form goback button ***

var globalValidatorMode = 'undef';

function v_on() {
	globalValidatorMode = 'on';
}

function v_off() {
	globalValidatorMode = 'off';
}

function v_undef() {
	globalValidatorMode = 'undef';
}


// *** Form validation ***

function validateForm() {

	// Bypass validation and return true if user has selected goback button
	if (globalValidatorMode == 'off') {
		return true;
	}

	// Get the form element itself
	var f		= $("ooff");
	var msg		= "";					// String to hold all error field titles
	var errs	= 0;					// Error count
	var div;							// Parent of form element
	var label;							// Label element

	// Cycle through all elements within the form object
	for (var i = 0; i < f.length; i++) {
	
		div		= f.elements[i].parentNode; // get div element
		label	= div.getElementsByTagName('label');
		
		if (f.elements[i].type == "radio" && ("false" != f.elements[i].getAttribute("required"))) {
		
			// Handle tricky radio buttons
			// Loop through all radio button group elements
			var group = f.elements[i].name;
			var radmsg = label[0].firstChild.nodeValue;			
			var radioNoErr = false;
			
			for (; f.elements[i].name == group; i++) {
				if (f.elements[i].checked === true) {
					radioNoErr = true;
				}
			}
			i--; // Turn index back one as main loop will also increment 
			
			// Don't process hidden fields (mootools field hidden if margin-top < 0)
			if ((div.style.display != "none") && (div.style.margin-top.toInt() >= 0) && (!radioNoErr)) {
			
				// We have an error condition
				if (errs > 0) {
					msg += '; ';
				}
				msg += radmsg;

				errs += 1;
				div.addClass("error");

			} else {
			
				// No error condition - clear div error class if set
				div.removeClass("error");
			}
		}
		// Don't process the submit button or hidden fields (mootools field hidden if margin-top < 0)
		else if ((f.elements[i].type != "submit") && (div.style.display != "none") && (div.style.margin-top.toInt() >= 0))	{
		
			// Handle all other form objects, ignore non required elements
			if ("" === f.elements[i].value && ("false" != f.elements[i].getAttribute("required"))) {
			
				// We have an error condition
				if (errs > 0) {
					msg += '; ';
				}
				var labelText = label[0].firstChild.nodeValue;
				msg += labelText;
				
				errs += 1;
				div.addClass("error");

			} else {
			
				// No error condition - clear div error class if set
				div.removeClass("error");
			}		
		}
	}
	if (errs > 0) {
	
		// Display error message
		var errID = $("err_msg");

		// Display first line
		errID.firstChild.nodeValue = 'Some of the fields are either incomplete or incorrect. Please correct these fields before proceeding:';

		// Is a line break present?
		if (errID.getElementsByTagName('br').length < 1) {
		
			// No - create line break
			var theBR = document.createElement('br');
			errID.appendChild(theBR);
			
			// Create next line of error fields
			var theNextLine = document.createTextNode(msg);
			errID.appendChild(theNextLine);			
			
		}

		// Line break already present, update following text node with error fields
		errID.lastChild.nodeValue = msg;

		return false;
	}
	
	return true;
}


//*** 'None of the above' shortcut button code ***

function noToAll(controller) {
	
	// if controller == element convert it to id string
	if ($type(controller) == 'element') {
		controller = controller.get('id');
	}
	
	var inputs = $$('input');
	
	inputs.each(function(input) {
		
		if (input.getProperty('value').toUpperCase() == 'NO') {	

			// If value is no, select this field	
			input.setProperty('checked', 'checked');
			
			// Call slide function, if defined
			if (input.onclick) {
				input.onclick();
			}
			
		} else if (input.getProperty('value').toUpperCase() == 'YES') {
		
			// If value is yes, add onclick event to clear controller
			input.setProperty('onchange', '$("' + controller + '").setProperty("checked", "");');
			
		}
	});
}


// *** Make links that launch a new browser XHTML compliant ***

function externalLinks() { 
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName("a"); 
		for (var i=0; i<anchors.length; i++) { 
			var anchor = anchors[i]; 
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			}
		}
	}
}

