/* 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;
}
/* ------------------------------------------------------------------- */






/* CHECK POSITIONING AND REMOVE COUNT
------------------------------------------------------------------- *
	checks whether any rows have been selected or if the positions have changed. if not, 
	it lets the user know that they must either change a position or select rows to remove 
	in order to use this button.
/* ------------------------------------------------------------------- */
function check_rem_count()
{
var act_div = document.getElementById('act');
	
	if(act_div.value != 'db_repos')
	{
		check_count = document.getElementById('rem_id_list').value.length;
		count_alert = 'you must select at least one to remove';
	
		/* if no rows are being selected, 
		then stop the form from posting
		-------------------------------------- */
		if(!check_count){ alert(count_alert); return false;	}
		else return true;
	}
}
/* ------------------------------------------------------------------- */






/* 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 	= $(id);
	var confirm_field 	= $('confirm_'+id);
	
	/* let the user know
	-------------------------------------- */
	if(original_field.value != confirm_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 = '';

	}
}





/* ------------------------------------- /*
	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();
	}
}