/* 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 form 			= document.forms[form];
	var count 			= form.length;
	var errors			= 0;
	var err_txt 		= '';
	var blank_set 		= false;
	var email_set		= false;
	var email_error 	= false;
	var email_value		= new Array();
	var email_address	= false;
	
	for(i=0;i<count;i++)
	{
		var element			= form[i];
		var req				= required_fields.toString().indexOf(element.name);
		
		
		/* exclude any hidden, submit, or reset elements */
		if(req != -1 && element.type != 'hidden' && element.type != 'submit' && element.type != 'reset' && element.disabled == false)
		{
			var e_value 	= element.value;
			var e_name		= element.name;
			var e_type		= element.type;
			var e_array		= e_name.indexOf('[]');
			var bad_chars 	= new Array(',',';',':','/','\\','&','#','!','$','%','*','<','>','?','\'','"');
			
			/* EMPTY FIELDS
			--------------------------------------------- */
			if(e_value == '')
			{
				// change border to red on error
				element.style.border = error_bdr;
				element.style.background = error_bg;
				
				if(!blank_set)
				{
					errors++;
					err_txt += 'Please make sure all enabled fields are filled in correctly.\n';
					blank_set = true;
				}
				
				continue;
			}
			else
			{
				element.style.border 		= default_bdr;
				element.style.background 	= default_bg;
			}
			/* --------------------------------------------- */
			
			
			
			/* EMAIL ADDRESS VERIFICATION
				checks email address in either a single field
				or an array field
			--------------------------------------------- */
			if(e_name.indexOf('email') != -1)
			{
				// combine email fields 2 at a time		
				if(e_array != -1)
				{
					if(!email_set)
					{
						email_value[0] = e_value;
						email_set = true;
					}
					else
					{
						email_value[1] = e_value;
						email_set = false;
					}
				}
				else
				{
					var at = e_value.indexOf('@');
					email_value[0] = e_value.substr(0,at);
					email_value[1] = e_value.substr(at+1,e_value.length);
				}
								
				// if there is only one value, but we are expecting two
				if(email_value.length == 1)
				{
					email_address 		= email_value[0];
					var e_atsym 		= email_address.indexOf('@');
					var e_lstat 		= email_address.lastIndexOf('@');
					var e_dot			= email_address.indexOf('.');
					
					// if an @ is found this early, this email is already invalid
					if(e_atsym != -1)
					{
						errors++;
						element.style.border 		= error_bdr;
						element.style.background 	= error_bg;
						err_txt 					+= 'Please check email address\n';
						email_error					= true;
					}
					else
					{
						element.style.border 		= default_bdr;
						element.style.background 	= default_bg;
					}
				}
				
				// if there still are no errors, and there are 2 parts to this email
				if(!email_error && (email_value.length == 2))
				{
					// join the parts with an @, then take some stats from the new address
					
					var email_address 		= email_value.join('@');
					var e_atsym 			= email_address.indexOf('@');
					var e_lastat 			= email_address.lastIndexOf('@');
					var e_dot				= email_address.indexOf('.');
					var e_lastdot			= email_address.lastIndexOf('.');
					
					
					// the email address should have an @, only one, and have at least one dot on the right side of @ to qualify
					if( !e_atsym || (e_atsym != e_lastat) || !e_dot || (e_lastdot < e_atsym) || (e_lastdot == email_address.length-1))
					{
						errors++;
						element.style.border 		= error_bdr;
						element.style.background 	= error_bg;
						err_txt 					+= 'Please check email address\n';
						email_error					= true;
					}
					else
					{
						element.style.border 		= default_bdr;
						element.style.background 	= default_bg;
					}
				}
				
				/* BAD CHARACTERS
					if there are no errors so far, we will still
					check for any bad characters that could be harmful
				--------------------------------------------- */
				if(!email_error)
				{
					for(c=0;c<=bad_chars.length;c++) 
					{
						// if bad character found
						if(email_address.indexOf(bad_chars[c]) != -1)
						{	
							// change border color to red		
							errors++;
							element.style.border		= error_bdr;
							err_txt 					+= 'Email Addresses cannot contain: "'+bad_chars[c]+'"\n';
							email_error 				= true;
						}
						else
						{
							element.style.border 		= default_bdr;
							element.style.background 	= default_bg;
						}
					}
				}
				continue;
			}
			/* --------------------------------------------- */
			
		}
	}
	
	if(errors)
	{
		alert(err_txt);
		return false;
	}
	else return true;
}
/* ------------------------------------------------------------------- */





/* --------------------------------------- /*
	checks to make sure that if an admin is
	going to reposition or remove entries, that
	a position was changed or a remove checkbox
	was selected before proceeding. Checked on
	the onSubmit event.	
/* --------------------------------------- */
function check_rem_count()
{
	db_action = document.getElementById('action').value;
	switch(db_action)
	{
		case 'rem_verify':
			check_count = document.getElementById('rem_id_list').value.length;
			count_alert = 'you must select at least one to remove';
			break;
		
		case 'db_repos':
			check_count = document.getElementById('upd_id_list').value.length;
			count_alert = 'You must change at least one value to reposition';
			break;
	}
	
	if(!check_count){ alert(count_alert); return false;	}
	else return true;
}




/* --------------------------------------- /*
	works in conjunction with a confirm text
	field. if the value in the confirm field
	is not the same as the value in the field
	with the id being passed, then an alert is
	displayed to the user. Works on the onChange
	event.	
/* --------------------------------------- */
function confirm_password(id,type)
{
	other_field 	= document.getElementById(id);
	conf_field 		= document.getElementById('confirm_'+id);
	
	if(type == 'val')
	{
		if(other_field.value != conf_field.value) return false;
		else return true;
	}
	else if(other_field.value != conf_field.value) alert('Password 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 = '';
			element.style.border = 'solid 1px #F0F';
		}
		else element.style.border = '';

	}
}