
////////////////////////////////////////////////////////////////////
//                                                                //
//     Last modified 15th Jan by Chris                            //
//     070115 - Moved inputs declaration into this file           //
//            - Added FormField constructor                       //
//     070111 - Added 'phone' data type                           //
//                                                                //
////////////////////////////////////////////////////////////////////


inputs = new Array() // this holds details of the elements to be checked


function checkForm(errorMessageTxt) {

	/*
	This function will check specified text inputs and select items for values and checkboxes to see if they're checked.
	It will also validate data types such as email addresses.
	If no value is detected, the form submission fails and the user is alerted.
	
	Elements to be checked are specified in the 'inputs' array .
	*/
	
	errorMessage = new Array() // this will store the strings to be added the error message
	firstElement = "" // this will note the first element to be corrected
	
	for (i=0;i<inputs.length;i++) {
		// get the current element id from the inputs array
		currElement = document.getElementById(inputs[i].elementid)
		// put the classname into a variable
		currElementClass = currElement.className;
		// reset all red borders 
		
		if (currElementClass.indexOf("formError") > -1) {
			currElement.className = currElementClass.substr(0,currElementClass.indexOf("formError")-1);
		} // end if currElementClass.indexOf
		
		// hide all error messages
		errorMessageDiv = currElement.id + 'Error';
		if ( document.getElementById(errorMessageDiv))  {
			errorMessageDiv = document.getElementById(errorMessageDiv);
			errorMessageDiv.style.display = 'none'
		}
		// deal with checkboxes separately as checking the value is pointless
		if(currElement.type.toString() != 'checkbox') {
			// see if a value exists for the object
			
			//alert(currElement.id.substring((currElement.id).length-3,(currElement.id).length))
			
			if (currElement.value == "") {
				alertUser()
			} else if (inputs[i].datatype) {
				switch (inputs[i].datatype){
					case "numberonly":
						var numberRegxp = /[^\D]$/
						if (currElement.value.search(numberRegxp)==-1) {
							alertUser()
						} // end if currElement.value.search == ""
					break;
					case "email":
						var emailRegxp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
						if (currElement.value.search(emailRegxp)==-1) {
							alertUser()
						} // end if currElement.value.search == ""
					break;
					case "phone":
						var phoneRegxp = /[\(\)\s-]/g
						var numberRegxp = /[^0-9\+]/
						currElement.value = currElement.value.replace(phoneRegxp, "")	
						if (currElement.value.search(numberRegxp)!=-1) {
							alertUser()
						} // end if currElement.value.search == ""
					break;
				} // end switch
			} // end if currElement.value == ""
			
			if (inputs[i].matchwith) {
				matchElement = document.getElementById((inputs[i].matchwith))
				if(matchElement.value != currElement.value) {
					//alert("1: " + window[inputs[i].matchwith]["errortext"] + " 2:" + inputs[i].errortext)
					alertUser(window[inputs[i].matchwith]["errortext"].toString(),inputs[i].errortext)
				}
			}
		} else {
			// apply the same logic to the checkboxes
			if(currElement.checked != true) {
				alertUser()
			} // end if currElement.checked
		} // end if currElement type
	} // end for loop
	
	if (errorMessage.length > 0) {
		for (j=0;j<errorMessage.length;j++) {
			if ( j != errorMessage.length - 1 ) {
				errorMessageTxt += "\n" + errorMessage[j] } else {
				errorMessageTxt += "\n" + errorMessage[j] + "\n\nThank you."
			}
		}
		
	alert(errorMessageTxt)
	document.getElementById(firstElement.id).focus()
	return false;
	}
} // end checkForm function

function alertUser(field1,field2) {
	//alert("here: " + field1 + " " + field2)
	if(field1&&field2){
		errorMessage[errorMessage.length] = field1 + " should match " + field2
	} else {
		errorMessage[errorMessage.length] = inputs[i].errortext
	}
	
	currElement.className += " formError";
	
	if ( document.getElementById(currElement.id + 'Error'))  {
		errorMessageDiv.style.display = 'block'
	}
	if (firstElement == "") {
		firstElement = currElement;
	}
}

function FormField(elementid,errortext,datatype,matchwith){
	this.elementid=elementid
	this.errortext=errortext
	this.datatype=datatype
	this.matchwith=matchwith
}	
