/* VALIDATE FORM (ONSUBMIT)
------------------------------------------------------------------- *
	checks all form elements passed in by required_fields.
	
	required_fields is an array set on every form page. It contains
	all the fields that need to be validated (does not use get element by id)
	
	First part makes sure the field isn't blank.
	Second, the 2 email fields are checked, field_1@field_2, no wierd characters
	Third, 
/* ------------------------------------------------------------------- */ 
function sf_validate(form)
{
	var errors			= 0;
	var errorMessage	= '';
	var emailError 		= false;
	var blankError 		= false;
	var checkBoxGroups	= new Array();
	var checkBoxes		= new Array();
	var validBoxGroups	= new Array();
	var checkBoxParents	= new Array();
	
	$$('input','select','textarea').each(function(e){
		var name	= e.get('name');
		var type	= e.get('type');
		var value	= e.get('value');
		var checked	= e.get('checked');
		var array	= (e.name.indexOf('[]') != -1) ? true : false;
		var parent	= e.getParents('form').get('id');
		
		// we will deal with required checkbox arrays on their own.
		// to do that, we need to put them in an array for later
		// and skip them during regular checking
		if(parent == form && required_fields.contains(name) && type == 'checkbox'){
			checkBoxGroups.include(name);
			//alert(e.getParent('span').setStyle('background','#F00'));
			checkBoxParents.include(e.getParent('span'));
			checkBoxes.include(e);
		}
		
		// if this element is part of the form we are checking
		if(parent == form && required_fields.contains(name) && type != 'checkbox'){
			
			// if the field is blank then change to error style
			if(value == ''){
				errors++;
				toggleErrorDisplay(e,1);
				if(!blankError){
					errorMessage += 'Please make sure all enabled fields are filled in correctly.\n';
					blankError = true;
				}
			}
			// if the field is filled, then make sure the style is default
			else toggleErrorDisplay(e,0);
			
			
			// if this is an email field,
			if(blankError != true && name.indexOf('email') != -1){
				var atSym 		= value.indexOf('@');
				var lastAt 		= value.lastIndexOf('@');
				var dot			= value.indexOf('.');
				var lastDot		= value.lastIndexOf('.');
				var semiColon	= value.indexOf(';');
				var badChars 	= new Array(',',';',':','/','\\','&','#','!','$','%','*','<','>','?','\'','"');
				
				// verify that this is a good format (1 @ symbol in the middle, and a dot somewhere afterwards before the end)
				if( atSym == -1 || (atSym != lastAt) || !dot || (lastDot < atSym) || (lastDot == value.length-1) || semiColon != -1){
					errors++;
					errorMessage += 'Please check email address\n';
					emailError = true;
				}
				
				// check for any bad characters
				if(!emailError){
					badChars.each(function(e){
						if(value.indexOf(e) != -1){
							errors++;
							errorMessage += 'Email addresses cannot contain: "'+e+'"\n';
							emailError = true;
						}
					});
				}
				
				if(emailError) toggleErrorDisplay(e,1);
				else toggleErrorDisplay(e,0);
			}
		}
	});
	
	
	// check through the checkboxes
	// if at least one checkbox in each required group is checked,
	// then the form is ok/valid
	if(checkBoxes.length != 0){
		checkBoxes.each(function(e,i){
			// only search if there is something to be found
			// if a checkbox group has been validated,
			// skip the rest of the checkboxes
			if(!validBoxGroups.contains(name)){
				// the name is also the name of the group
				// this is what we will use to signify which groups
				// have been validated
				var name = e.get('name');
				var checked = e.get('checked');
				// if an item is checked, put it's name in the basket
				// this will indicate that the field is valid and
				// prevent any more boxes from being scanned
				if(checked) validBoxGroups.include(name);
			}
		});
		// confirm whether all required checkBoxGroups have
		// had at least one box checked
		if(validBoxGroups.length != checkBoxGroups.length){
			errors++;
			errorMessage += 'You must select at least one checkbox option from required checkbox field(s).\n';
			// now that we know which checkbox groups are valid and which aren't
			// we can change the background to illustrate which groups are required
			checkBoxGroups.each(function(e,i){
				// if the checkbox group is not available in the validBoxGroups array,
				// then the checkbox group was required, but not filled in.
				if(!validBoxGroups.contains(e)) toggleErrorDisplay(checkBoxParents[i],1);
				else toggleErrorDisplay(checkBoxParents[i],0);
			});
		}
		// if all checkbox groups are valid, then just make sure the user knows (ie: un-border the error areas)
		else checkBoxGroups.each(function(e,i){ toggleErrorDisplay(checkBoxParents[i],0); });
	}
	/* ------------------------ */
	
	
	
	if(errors){
		alert(errorMessage);
		return false;
	}
	else return true;
}

/* --- */

function toggleErrorDisplay(element,error){
	
	var eClass	= element.get('class');
	var eType	= element.get('type');
	var eTag	= element.get('tag');
	var eName	= element.get('name');
	
	var emptyClass	= (eClass == '');
	var isCheckbox	= (eType == 'checkbox');
	var isSelect	= (eTag == 'select');
	var isArray		= (eTag == 'span' && eClass == 'array');
	var isError		= (eClass == 'error');
	var hasError	= (eClass.indexOf('_error') != -1);
	
	if(error){
		// regular text fields
		if(emptyClass && !isCheckbox && !isSelect && !hasError && !isArray) element.set('class','error');
		// styled text fields
		else if(!emptyClass && !isCheckbox && !isSelect && !hasError && !isError && !isArray) element.set('class',eClass+'_error');
		// non styled select fields
		else if(emptyClass && !isCheckbox && isSelect && !hasError && !isError && !isArray) element.setStyle('border','solid 1px #D50');
		// for checkbox and radio button arrays
		else if(!emptyClass && !isCheckbox && !isSelect && !hasError && !isError && isArray) element.setStyle('border','solid 1px #D50');
	}
	else{
		// regular text fields
		if(!emptyClass && !isCheckbox && !isSelect && isError && !isArray) element.erase('class');
		// styled text fields
		else if(!emptyClass && !isCheckbox && !isSelect && hasError && !isArray) element.set('class',eClass.replace('_error',''));
		// non styled select fields
		else if(emptyClass && !isCheckbox && isSelect && !hasError && !isError && !isArray) element.setStyle('border','solid 1px #AAA');
		// for checkbox and radio button arrays
		else if(!emptyClass && !isCheckbox && !isSelect && !hasError && !isError && isArray) element.setStyle('border','none');
	}
}
/* ------------------------------------------------------------------- */






/* CONFIRM TEXT FIELD
------------------------------------------------------------------- *
	uses the id of the original field, and then determies the name of
	the confirm field. The confirm field name should be the name of the 
	original field, with a "confirm_" in front. "original_field" becomes 
	"confirm_original_field"
	
	if the fields do not match, an alert is sent out
/* ------------------------------------------------------------------- */
function confirm_textfield(id)
{
	/* get the original, and confirm field
	-------------------------------------- */
	var original_field 	= document.getElementById(id);
	var confirm_field 	= document.getElementById('confirm_'+id);
		
	/* let the user know
	-------------------------------------- */
	if(original_field.value != confirm_field.value) alert('Fields Do Not Match!');
}





/* --------------------------------------- /*
	checks to make sure that the value being
	entered is a valid number type. this also
	includes dashes, dots, parenthesis, and 
	the letters 'e','x', and 't' to allow
	the admin flexibility in how they layout
	the numbers.
	555-555-5555 vs. 555.555.5555 vs. (555) 555-5555
/* --------------------------------------- */
function check_number_only(field_id)
{
	element 		= document.getElementById(field_id);
	valid_chars		= '0123456789.,/-+()';
	
	for(i=0;i<element.value.length;i++)
	{
		ex_char = element.value.substring(i,i+1);
		if(valid_chars.indexOf(ex_char) == -1)
		{
			alert('Only Numbers Are Allowed Here!');		
			element.value = '';
			changeErrorElement(true,element);
		}
		else element.style.border = '';

	}
}





/* ------------------------------------- /*
	validates the length of the field, if
	there are supposed to be 5 numbers in 
	a zip code, this function will make 
	sure the user has entered 5.
/* ------------------------------------- */
function validate_length(id,length)
{
	var field = document.getElementById(id);
	var f_length = field.value.length;
	
	if(f_length < length)
	{
		alert('This field requires '+length+' characters MINIMUM');
		field.focus();
	}
}