/*
	functions.forms.js
	DOM Interactivity / validation Functions for Forms
	Created: 2.15.08
	Creator: Matt Kircher, Mainline Media LLC
*/

function validateEmail(e){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test($.trim(e));
}

/* CONTACT FORM */

function setupContactForm(){
	if($('#contactForm').length){
		
		//remove required attention classes from required fields
		$('#contactForm .required').bind('change', function(){
			if($.trim($(this).val()) != ""){
				$(this).removeClass('required_attention');
				if($(this).get(0).nodeName == "SELECT"){
					if($(this).next().attr('class') == "select_required_attention"){
						$(this).next().remove();
					}
				}
			}
		});
		
		//bind blur / focus actions
		if(!$.browser.msie){
			$('#contactForm input[@type="text"]')
			.bind('focus', function(){ $(this).addClass('highlighted_form_field'); })
			.bind('blur', function(){  $(this).removeClass('highlighted_form_field'); });
		}
		
		//bind product checkbox select/deselect button
		$('#products_all')
		.bind('click', function(){
			if($(this).attr('value') == "Select All"){
				$('#contactForm input[@name="product[]"]').attr('checked', true);
				$(this).attr('value','Deselect All');
			} else {
				$('#contactForm input[@name="product[]"]').removeAttr('checked');
				$(this).attr('value','Select All');
			}
		});
		
		//set focus to first form field
		$('#contactForm input[@tabindex="1"]').focus();
	}
}

function validateContactForm(){
	
	//setup valid object
	var valid = { status:true, response:'', element:null };
	
	//remove classes, go through and check for non-values
	$('form .required').removeClass('required_attention');
	$('form .select_required_attention').remove();
	$('form .required').each(function(){
		if(($(this).val() == "" || $(this).val() == null)){
			$(this).addClass('required_attention');
			valid.status = false;
			valid.response = 'One or more required fields has not been completed. Please complete them and resubmit the form.';
			if(valid.element == null){ valid.element = $(this); }
			
			if($(this).get(0).nodeName == "SELECT"){
				$(this).after('<span class="select_required_attention">&lsaquo;&mdash;</span>');
			}
		}
	});
	
	//if email is not of the form 'name@email.com', don't validate
	if(valid.status){
		$('form .required').each(function(){
			if($(this).attr('name') == "email"){
				if(!validateEmail($(this).val())){
					$(this).addClass('required_attention');
					valid.status = false;
					valid.response = 'Please supply a valid email address.';	
					valid.element = $(this);
				}
			}
		});
	}
	
	//display alert, focus on first non-valued field
	if(!valid.status){ alert(valid.response); $(valid.element).focus(); }
	return valid.status;
}

/* EMPLOYMENT FORMS */
function setupEmploymentForm(){
	if($('#employmentForm').length){
		
		//same address checkbox
		$('#contact_same_address').click(function(){;
			if($(this).is(':checked')){
				$('#contact_perm_addr1').val($('#contact_addr1').val());
				$('#contact_perm_addr2').val($('#contact_addr2').val());
				$('#contact_perm_city').val($('#contact_city').val());
				$('#contact_perm_zip').val($('#contact_zip').val());
				$('#contact_perm_state').val($('#contact_state').val());
			} else {
				$('#contact_perm_addr1').val('');
				$('#contact_perm_addr2').val('');
				$('#contact_perm_city').val('');
				$('#contact_perm_zip').val('');
				$('#contact_perm_state').val('');
			}
		});
		
		$('#employmentForm').bind('submit', function(){
			return validateContactForm();
		});
	}
}


//DOM loaded
$(document).ready(function(){
	setupContactForm();
});