// JavaScript Document
// betaforms v1.0 
// Last Updated 5/28/2010
// Initial Developer: Jake DiMare http://www.jakedimare.com
// Developed under open source guidelines - feel free to use and modify at will
// REQUIREMENTS:
//
//  Forms calling functions in betaform.js must:
//		1. To check for required fields the form must have a hidden field with an ID="required". 
//		   The value of this field will be the required fields through the rest of the form as a comma separated list of two values separated by a pipe(|). 
//         The first value in each pair must be the ID of the required field and the second value must be the Name. 
//		2. Each form field including the hint feature must call the function 'runBetaHint' and pass three values in the following format: ('fname','fname-hint','alphanumeric')
//         The first value is the ID of the calling form element. The Second value is the ID of the DIV containing the hint text. The third value is what type of validation is required. 
//         Validation options currently include: alphanumeric, numeral, phone, email and message. More information on validation options can be found in the readme file. 
//      3. The emailValidator function requires two parameters, the ID of the input field and the ID of the hint DIV in the following format: ('email','emailhint')
// 		4. A hidden field with an id="fields" and the names of all other fields in the form (except the other hidden field for required fields) is necessary to use betaform.js 
//
//
//	CONFIGURABLE VARIABLES:  
		//var processFile = '/forms/form-handler.php';
		var emailField = 'email'; //this must match the ID of the email input in the form calling the emailValidator function
		var emailHintField = 'emailhint'; //this must match the ID of the hint DIV for the email input
		var borderColorON = "#903"; //border of input field highlighted
		var borderColorOFF = "#666"; //normal border of input field
		var alphanumericHintText = "I'm sorry, this field may only contain alphanumeric characters (Letters and Numbers).";
		var emailHintText = "I'm sorry, this field may only contain alphanumeric characters, periods (.) and an at (@) symbol.";
		var phoneHintText = "I'm sorry, this field may only contain numeric characters, dashes and one set of parenthesis.";
		var numberHintText = "I'm sorry, this field may only contain numbers.";
		var amountHintText = "I'm sorry, this field may only contain numbers, a dollar sign and/or a period";
		var messageHintText = "I'm sorry, this field may only contain alphanumeric characters and basic punctuation.";
		var atSymbolHint = "The email address must include an @ symbol to be considered valid."
		var emailPeriodHint = "The email address must include a period to be considered valid.";
		var emailLengthHint = "This email address is too short to be considered valid";
// 	END CONFIGURABLE VARIABLES
//
//
//	NON-CONFIGURABLE VARIABLES - DO NOT CHANGE
		var devAlert = "A BetaForms function has a problem. Check to ensure the form is passing all required variables when calling functions in betaforms.js";
		var emailValid = 1;
		var globalError = '';
		var reqFieldCheck = 0;
		var emailCheck = 0;
// 	END NON-CONFIGURABLE VARIABLES		
//
//
//submit function
function submitForm() {
	
		var formcatcheck = document.getElementById('formcat').value;
		//alert(formcatcheck);
		if(formcatcheck == 'finance') {
			var processFile = '../forms/finForm-handler.php';//THIS IS ANOTHER THING THAT WILL NEED TO BE CHANGED FOR THE HOSTING ENVIRONMENT
		} else {
			var processFile = '../forms/form-handler.php';//THIS IS ANOTHER THING THAT WILL NEED TO BE CHANGED FOR THE HOSTING ENVIRONMENT
		}
	
	var fieldString = document.getElementById('fields').value;	
	var fieldArray = fieldString.split(',');
	var arrayLen = fieldArray.length;
	var queryString = processFile + '?';
	
	for(i=0; i < arrayLen; i++) {
		var field = fieldArray[i];
		var value = document.getElementById(fieldArray[i]).value;
		if (value == '') {value = 'NULL'};
		queryString = queryString + field + '=' + value + '&';
	}
	window.location = queryString;
}

//validation program
function runBetaForm() {

	if (document.getElementById(emailField)) {
		emailValidator();
	}
	
	//Determine whether or not there are required fields
	if (document.getElementById('required')) {
		var checkReq = document.getElementById('required').value; //get the required fields
		if (checkReq != '') {
			requiredFields()
		}
	}
	
	var submitCheck = reqFieldCheck + emailCheck;
	if (submitCheck == 2) {
		submitForm();	
	}
}

function requiredFields () {
	
	var reqString = document.getElementById('required').value; //get the required fields
	var reqArray = reqString.split(','); //turn them into an array
	var reqLen = reqArray.length;
	
	for(i=0; i <= reqLen; i++) {
		var fieldString = (reqArray[i]);
		if (fieldString != undefined) {
			var fieldArray = fieldString.split('|');
			var thisField = document.getElementById(fieldArray[0]).value;
			if (thisField == '') {
				var formField = fieldArray[0];
				var hintField = fieldArray[2];
				var hint = "I'm sorry, the " + (fieldArray[1]) + " is a required field. The form was not submitted.";
				hintON(formField,hint,hintField);
				reqFieldCheck = 0;
				break;
			} else {
				if (reqFieldCheck != 1) {
					reqFieldCheck = 1;
				}
			}
		}
	}
}

//email validator
function emailValidator() {
	var hintText = 0;
	var value = document.getElementById(emailField).value;
	if (value != '') {	
		
		if (value.length < 6) {
			hintText = emailLengthHint;
		} else {
			hintText = 0;
		}
		
		if (value.search('@') == -1) {
			hintText = atSymbolHint;
		}
		
		if (value.indexOf('.') == -1) {
			hintText = emailPeriodHint;
		}
		
		if (hintText != 0) {
			hintON(emailField,hintText,emailHintField);
			emailValid = 0;
			emailCheck = 0;
		} else {
			emailValid = 1;
			emailCheck = 1;
		}
	}
}

//hinting program
function runBetaHint(field,hint,category) {	 
	
	//alert(globalError);

	//check to ensure all three required variables were passed to the function.
	if (field == undefined) {alert(devAlert);}
	if (hint == undefined) {alert(devAlert);}
	if (category == undefined) {alert(devAlert)}
		
	//Determine which filter to use based on the category passed in
	//try {
		switch(category) {
			
			case 'alphanumeric':
			alphaNumericHint(field,hint);
			break;
			
			case 'phone':
			phoneHint(field,hint);
			break;
			
			case 'email':
			emailHint(field,hint);
			break;
			
			case 'numbers':
			numberHint(field,hint);
			break;
			
			case 'amount':
			amountHint(field,hint)
			break;
			
			case 'message':
			messageHint(field,hint);
			break;
			
		}
	//} catch (error) {
		//alert("Error: " + devAlert + "The system error is: " + error.message);
	//}
}

//Turns hints on and submit off
function hintON (formField,hint,hintField) {
	theID = hintField;
	theField = document.getElementById(theID);
	
	theField.innerHTML = hint;
	theField.style.visibility = "visible"; //show the hidden hint 
	jQuery(document).ready(function(){
	jQuery(".beta-hint").fadeIn("slow");});
	hideSubmit();
	redBoxON(hintField);
}

//Turns hints off and submit on
function hintOFF (beta_hint) {
	if (emailValid == 1) {	
		showSubmit(); 
		beta_hint.style.visibility = "hidden";  //hide the hint
		jQuery(document).ready(function(){
		jQuery(".beta-hint").fadeOut("slow");});
	}
}

//highlights the border of the field
function redBoxON (field) {
	var box = document.getElementById(field);
	box.style.borderColor = borderColorON;	
}

//turns the border highlight off
function redBoxOFF (field) {
	var box = document.getElementById(field);
	box.style.borderColor = borderColorOFF;	
}

function hideSubmit() {
	jQuery(document).ready(function(){
	jQuery(".beta-submit").fadeOut("slow");});
}

function showSubmit() {
	if (globalError == '') {
		jQuery(document).ready(function(){
		jQuery(".beta-submit").fadeIn("slow");});
	}
}

//The alpha-numeric filters
function alphaNumericHint(field,hint) {
	var current_value = document.getElementById(field).value; //get the value of the current field 		
	var beta_hint = document.getElementById(hint); //get the hint div
	cleaned_value = current_value.replace(/\s+/g,''); //clear all spaces before checking
	character_search = cleaned_value.search(/\W/); //look for any characters besides letters or numbers
	if (character_search != -1) { //if there are some 
		var hintText = alphanumericHintText;
		hintON(field,hintText,hint);
		redBoxON(field);
		
		//this next block is to indicate this is an error present on the form in order to keep the submit button off
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch == -1) { 
			globalError = globalError + errorField;
		}
	}
	clearAlphaNumericHint(cleaned_value,hint,field);	
}

function clearAlphaNumericHint(cleaned_value,hint,field) {
	var beta_hint = document.getElementById(hint);	//get the hint div
	character_search = cleaned_value.search(/\W/); //look for any characters besides letters or numbers
	if (character_search == -1) { //if there are none
		hintOFF(beta_hint);	
		redBoxOFF(field);
		
		//this next block is to get rid of the field unique error present on the form in order to turn the submit button on
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch != -1) { 
			globalError = globalError.replace(errorField,'');
		}
		
	}
}

//The phone filters
function phoneHint(field,hint) {
	var current_value = document.getElementById(field).value; //get the value of the current field 		
	var beta_hint = document.getElementById(hint); //get the hint div
	cleaned_value = current_value.replace(/-/g,''); //clear accepted characters
	cleaned_value = cleaned_value.replace('(','');
	cleaned_value = cleaned_value.replace(')','');										
	character_search = cleaned_value.search(/\D/); //look for any characters besides letters or numbers
	if (character_search != -1) { //if there are some 
		var hintText = phoneHintText;
		hintON(field,hintText,hint);
		redBoxON(field);
		
		//this next block is to indicate this is an error present on the form in order to keep the submit button off
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch == -1) { 
			globalError = globalError + errorField;
		}
	}
	clearPhoneHint(cleaned_value,hint,field);	
}

function clearPhoneHint(cleaned_value,hint,field) {
	var beta_hint = document.getElementById(hint);	//get the hint div
	character_search = cleaned_value.search(/\D/); //look for any characters besides letters or numbers
	if (character_search == -1) { //if there are none
		hintOFF(beta_hint);
		redBoxOFF(field);
		
		//this next block is to get rid of the field unique error present on the form in order to turn the submit button on
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch != -1) { 
			globalError = globalError.replace(errorField,'');
		}
		
	}
}					
	
//The email filters
function emailHint(field,hint) {
	var current_value = document.getElementById(field).value; //get the value of the current field 		
	var beta_hint = document.getElementById(hint); //get the hint div
	cleaned_value = current_value.replace(/-/g,''); //clear accepted characters
	cleaned_value = cleaned_value.replace('@','');
	cleaned_value = cleaned_value.replace('.','');
	cleaned_value = cleaned_value.replace('.','');
	cleaned_value = cleaned_value.replace('.','');
	character_search = cleaned_value.search(/\W/); //look for any characters besides letters or numbers
	if (character_search != -1) { //if there are some 
		var hintText = emailHintText;
		hintON(field,hintText,hint);
		redBoxON(field);
		
		//this next block is to indicate this is an error present on the form in order to keep the submit button off
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch == -1) { 
			globalError = globalError + errorField;
		}
	}
	clearEmailHint(cleaned_value,hint,field);	
}

function clearEmailHint(cleaned_value,hint,field) {
	var beta_hint = document.getElementById(hint);	//get the hint div
	character_search = cleaned_value.search(/\W/); //look for any characters besides letters or numbers
	if (character_search == -1) { //if there are none
		hintOFF(beta_hint);
		redBoxOFF(field);
		
		//this next block is to get rid of the field unique error present on the form in order to turn the submit button on
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch != -1) { 
			globalError = globalError.replace(errorField,'');
		}
	}
}
	
//The number filters
function numberHint(field,hint) {
	var current_value = document.getElementById(field).value; //get the value of the current field 		
	var beta_hint = document.getElementById(hint); //get the hint div
	cleaned_value = current_value;										
	character_search = cleaned_value.search(/\D/); //look for any characters besides letters or numbers
	if (character_search != -1) { //if there are some 
		var hintText = numberHintText;
		hintON(field,hintText,hint);
		redBoxON(field);
		
		//this next block is to indicate this is an error present on the form in order to keep the submit button off
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch == -1) { 
			globalError = globalError + errorField;
		}
	}
	clearNumberHint(cleaned_value,hint,field);	
}

function clearNumberHint(cleaned_value,hint,field) {
	var beta_hint = document.getElementById(hint);	//get the hint div
	character_search = cleaned_value.search(/\D/); //look for any characters besides letters or numbers
	if (character_search == -1) { //if there are none
		hintOFF(beta_hint);
		redBoxOFF(field);
		
		//this next block is to get rid of the field unique error present on the form in order to turn the submit button on
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch != -1) { 
			globalError = globalError.replace(errorField,'');
		}
	}
}

//The amount filters
function amountHint(field,hint) {
	var current_value = document.getElementById(field).value; //get the value of the current field 		
	var beta_hint = document.getElementById(hint); //get the hint div
	cleaned_value = current_value;
	cleaned_value = cleaned_value.replace('$','');
	cleaned_value = cleaned_value.replace('.','');
	character_search = cleaned_value.search(/\D/); //look for any characters besides letters or numbers
	if (character_search != -1) { //if there are some 
		var hintText = amountHintText;
		hintON(field,hintText,hint);
		redBoxON(field);
		
		//this next block is to indicate this is an error present on the form in order to keep the submit button off
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch == -1) { 
			globalError = globalError + errorField;
		}
	}
	clearAmountHint(cleaned_value,hint,field);	
}

function clearAmountHint(cleaned_value,hint,field) {
	var beta_hint = document.getElementById(hint);	//get the hint div
	character_search = cleaned_value.search(/\D/); //look for any characters besides letters or numbers
	if (character_search == -1) { //if there are none
		hintOFF(beta_hint);
		redBoxOFF(field);
		
		//this next block is to get rid of the field unique error present on the form in order to turn the submit button on
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch != -1) { 
			globalError = globalError.replace(errorField,'');
		}
	}
}
	
//The message filter
function messageHint(field,hint) {
	var current_value = document.getElementById(field).value; //get the value of the current field 		
	var beta_hint = document.getElementById(hint); //get the hint div
	cleaned_value = current_value.replace(/\s+/g,''); //clear all spaces before checking
	
	cleaned_value = cleaned_value.replace('.','');
	cleaned_value = cleaned_value.replace('?','');
	cleaned_value = cleaned_value.replace('!','');
	cleaned_value = cleaned_value.replace(',','');
	
	cleaned_value = cleaned_value.replace('.','');
	cleaned_value = cleaned_value.replace('?','');
	cleaned_value = cleaned_value.replace('!','');
	cleaned_value = cleaned_value.replace(',','');
	
	cleaned_value = cleaned_value.replace('.','');
	cleaned_value = cleaned_value.replace('?','');
	cleaned_value = cleaned_value.replace('!','');
	cleaned_value = cleaned_value.replace(',','');
	
	cleaned_value = cleaned_value.replace('.','');
	cleaned_value = cleaned_value.replace('?','');
	cleaned_value = cleaned_value.replace('!','');
	cleaned_value = cleaned_value.replace(',','');
	
	cleaned_value = cleaned_value.replace('.','');
	cleaned_value = cleaned_value.replace('?','');
	cleaned_value = cleaned_value.replace('!','');
	cleaned_value = cleaned_value.replace(',','');
	
	character_search = cleaned_value.search(/\W/); //look for any characters besides letters or numbers
	if (character_search != -1) { //if there are some 
		var hintText = messageHintText;
		hintON(field,hintText,hint);
		
		//this next block is to indicate this is an error present on the form in order to keep the submit button off
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch == -1) { 
			globalError = globalError + errorField;
		}
	}
	clearAlphaNumericHint(cleaned_value,hint,field);	
}

//clear message hints
function clearMessageHint(cleaned_value,hint,field) {
	var beta_hint = document.getElementById(hint);	//get the hint div
	character_search = cleaned_value.search(/\W/); //look for any characters besides letters or numbers
	if (character_search == -1) { //if there are none
		hintOFF(beta_hint);	
		redBoxOFF(field);
		
		//this next block is to get rid of the field unique error present on the form in order to turn the submit button on
		var errorField = field + ","; 
		errorSearch = globalError.search(errorField);
		if (errorSearch != -1) { 
			globalError = globalError.replace(errorField,'');
		}
	}
}

