// Email validation
function isEmail(email){
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0){
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
		}
	return result;
}

function doError(oElement, oFocus){
	// Set the background color of the offending element
	//oElement.style.background = "red";
	// If this is the first field to have an error give it focus
	if(oFocus.focused == false){
		oElement.focus();
		oFocus.focused = true;
	}
	// Return the string that should be appended to the error message
	if(oElement.getAttribute('dfErrorMsg') == ''){
		return "Form field " + oElement.name + " has an error.\n";
	}
	return oElement.getAttribute('dfErrorMsg') + "\n";
}

function validate(frmName) {
	var str = "";
	function focusObj() {
		this.focused = false;
		}
	setFocus = new focusObj();
	
	// Regular expressions
	var reNotInteger = new RegExp('[^0-9]', 'g');
	var reFloat = new RegExp('^[0-9]*\.[0-9]+$', 'g');
	var reNotPhone = new RegExp('[^0-9\-\(\) ]+', 'g');
	
	var arrValidatedRadio = new Array();
	var elements = frmName.getElementsByTagName('input');
	
	// loop through all input elements in form
	for(var i = 0; i < elements.length; i++) {
		// A pointer to the current element
		var currElement = elements.item(i);
		// Change the background colour of the element to white
		//currElement.style.background = "white";
		// Get the value of the current element
		var value = currElement.value;
		// Check if the Field is required
		var required = currElement.getAttribute('dfRequired');
		// Get the validation type of the current element
		var validationType = currElement.getAttribute('dfValidate');

		if(currElement.type == 'text'){
			// If the element value is empty and required show an error then continue, if its empty and not required just continue
			if(value == ''){
				if(required == 'yes'){
					str += doError(currElement, setFocus);
				}
				continue;
			}

			// Validate this field as an email
			if(validationType == 'email'){
				if(!isEmail(value)){
					str += doError(currElement, setFocus);
				}
			}
			// Validate as integer
			else if(validationType == 'integer'){
				if(reNotInteger.test(value)){
					str += doError(currElement, setFocus);
				}
			}
			// Validate as float
			else if(validationType == 'float'){
				if(!reFloat.test(value)){
					str += doError(currElement, setFocus);
				}
			}
			// Validate as phone number
			else if(validationType == 'phone'){
				if(reNotPhone.test(value)){
					str += doError(currElement, setFocus);
				}
			}
		}
		else if(currElement.type == 'radio'){
			// Because this part of the code is executed for each individual radio button we check if 
			// this set of radio buttons has already been checked
			var checked = false;
			for(j = 0; j < arrValidatedRadio.length; j++){
				if(arrValidatedRadio[j] == currElement.name){
					checked = true;
					break;
				}
			}
			// If this radio set hasn't been checked, check it
			if(checked == false){
				// Assign the array of radio buttons (the group of radio buttons with the same name) to a variable
				var arrRadio = frmName.elements[currElement.name];
				
				if(arrRadio[0].getAttribute("dfRequired") == 'yes'){
					var errorMsg = arrRadio[0].getAttribute("dfErrorMsg");
					var oneSelected = false;
					for(j = 0; j < arrRadio.length; j++){
						if(arrRadio[j].checked){
							oneSelected = true;
							break;
						}
					}
					if(oneSelected == false){
						str += doError(currElement, setFocus);
					}
				}
				// Push this radio array name onto the checked array so we know not to check it again
				arrValidatedRadio[arrValidatedRadio.length] = currElement.name;
			}
		}
	}
	
	if (str != "") {
	  // do not submit the form
	  alert("Form Error\n==========\n" +str);
	  return false;
	} else {
	  // form values are valid; submit
	  return true;
	}
}
