// JavaScript Document
//this function will validate the contact form
function loginChk(frmId)
{
	//get the form element
	var frmObj = document.getElementById(frmId);
	
	//setup the empty vars for the alert message
	var errorMsg = "";
	var confirmMsg = "";
	//setup the regular expresssion to check the validity of the e-mail address
	reEmail = /^\w+([\.\-]?\w+)*\@\w+([\.\-]?\w+)*(\.\w{2,3})+$/
	//check the sender_name input
	if(banIllegalCharacters(frmObj.username)){
		if(frmObj.username.value == ""){
			//there is no name in the sender box
			errorMsg += "-You have not entered a user name.\n";
			}
	}else{
		return false;
	}
	//check for a password
	if(banIllegalCharacters(frmObj.password)){
		if(frmObj.password.value == ""){
			//there is no username
			errorMsg += "-You have not entered a password.\n";
		}
	}else{
		return false;
	}
	
	
	//now check if there have been any errors and show them or return 'true' 
	if(errorMsg){
		//there are errors
		alert(errorMsg);
		return false;
	}else{
		//there are no errors, submit the form
		frmObj.submit();
	}		
}

function formChk(formObj){
	
	//setup the error messages
	var errorMsg = "";
	//setup the regular expresssion to check the validity of the e-mail address
	reEmail = /^\w+([\.\-]?\w+)*\@\w+([\.\-]?\w+)*(\.\w{2,3})+$/
	rePhoneStart = /^[1-9][0-9]{2}$/
	rePhoneEnd = /^[0-9]{4}$/
	rePassword = /^\w+$/
	
	//check the required form fields
	if(banIllegalCharacters(formObj.name)){
		if(formObj.name.value == ""){
			//empty field
			errorMsg += "- Please enter your name.\n";
		}
	}else{
		return false;
	}
	
	//this will check to see if both the phone and email fields are empty
	if(formObj.email.value == "" && formObj.phone.value ==""){
		//ask them to enter one or both value
		errorMsg += "- Please enter your preferred method for us to contact you (phone or e-mail).\n";
		
	}else{
		//now check to see if they entered an email address
		if(formObj.email.value != ""){
			if(formObj.email.value == "" || formObj.econfirm.value == ""){
				//one of the fields are empty
				errorMsg += "- Please enter your e-mail address twice.\n";
			}else{
				//check to see if they are the same
				if(formObj.email.value == formObj.econfirm.value){
					//check the format of the e-mail
					if(!reEmail.test(formObj.email.value)){
						//the e-mail is not in proper format
						errorMsg += "- Your e-mail address is invalid. Please enter them again.\n";
						//clear the e-mail address'
						formObj.email.value = "";
						formObj.econfirm.value = "";
					}
				}else{
					//the e-mail address are not the same
					errorMsg += "- Your e-mail address' do not match. Please enter them again.\n";
					formObj.econfirm.value = "";
				}
			}
		}
		
		
	}
	
	if(banIllegalCharacters(formObj.desc)){
		if(formObj.desc.value == ""){
			errorMsg += "- please enter a brief description or question.\n";
		}
	}else{
		return false;
	}
			
	//now check if there have been any errors and show them or return 'true' 
	
	if(errorMsg){
		//there are errors
		alert(errorMsg);
		return false;
	}else{
		
		//display submission processing text in place of the form
		return true;
		
	}
}

/**
* @author	http://www.webdeveloper.com/forum/showthread.php?threadid=146239
* 
* @param 	myElement:a form element
* @return	Boolean
*/
function banIllegalCharacters(myElement) {
		var inputFieldName = myElement.name;
		var elementValue = myElement.value;
		var inputFormName = myElement.form.name;
		var illegalCharArray = new Array("(",")","=","+","\"",":",";","<",">",",","*","-","#");
		var foundIllegalCharacters = new Array();
		var counter = 0;
		var errorString = "";

		for (i=0;i < illegalCharArray.length; i++) {
			if (elementValue.indexOf(illegalCharArray[i]) != -1){
				// Illegal character found - add illegal character to array
				foundIllegalCharacters[counter] = illegalCharArray[i];
				counter++;
			}
		}
		if (counter > 0) {
			// Illegal characters are present - build an alert string to notify user
			for (i=0;i < foundIllegalCharacters.length; i++) {
				if (errorString.indexOf(foundIllegalCharacters[i]) == -1) {
					errorString = errorString + foundIllegalCharacters[i] + " ";
				}
			}
			alert("You cannot use the following characters when filling out the form boxes:\n\n\t" + errorString + "\n\nPlease re-enter your information.");
			myElement.focus();
			return false;
		}else{
			return true;
		}
}