/*
	functions.forms.js
	Form Validation / Interactivity Functions
	Created: Nov. 5, 2007
	Creator: Matt Kircher, Mainline Media LLC
*/

/* CONTACT FORM */
function setupContactForm(){
	if($('#contactForm').length){
		
		//format reCaptcha form
		$('#recaptcha table tr').find('td:eq(2)').remove();
		
		$('#contact_phone1, #contact_phone2, #contact_phone3').numeric();
		
		//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'); });
		}
		
		//set focus to first form field
		$('#contactForm input[@tabindex="1"]').focus();
	}
}
function validateForm(){
	
	//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;
}
function validateEmail(e){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test($.trim(e));
}

/* RFP FORM */
function setupRFPForm(){
	if($('#rfpForm').length){
		
		$('#rfp_phone1, #rfp_phone2, #rfp_phone3').numeric();
		
		//handle 'other' selections
		$('#rfp_project_other, #rfp_objectives_other, #rfp_details_other, #rfp_form_budget_other').hide();
		if($('#rfp_project option:selected').val() == "Other"){ $('#rfp_project_other').show(); }
		if($('#rfp_objectives4').is(':checked')){ $('#rfp_objectives_other').show(); }
		if($('#rfp_details10').is(':checked')){ $('#rfp_details_other').show(); }
		if($('#rfp_form_budget option:selected').val() == "Other"){ $('#rfp_form_budget_other').show(); }
			
		$('#rfp_project').bind('change', function(){
			if($('#rfp_project option:selected').val() == "Other"){	$('#rfp_project_other').show(); }
			else {	$('#rfp_project_other').hide(); }
		});
		$('#rfp_objectives4').bind('change', function(){
			if($(this).is(':checked')){	$('#rfp_objectives_other').show();	}
			else { $('#rfp_objectives_other').hide(); }
		});
		$('#rfp_details10').bind('change', function(){
			if($(this).is(':checked')){	$('#rfp_details_other').show(); }
			else {	$('#rfp_details_other').hide(); }
		});
		$('#rfp_form_budget').bind('change', function(){
			if($('#rfp_form_budget option:selected').val() == "Other"){ $('#rfp_form_budget_other').show(); }
			else {	$('#rfp_form_budget_other').hide(); }
		});
		
		//remove required attention classes from required fields
		$('#rfpForm .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){
			$('#rfpForm input[@type="text"]')
			.bind('focus', function(){ $(this).addClass('highlighted_form_field'); })
			.bind('blur', function(){  $(this).removeClass('highlighted_form_field'); });
		}
		
		//set focus to first form field
		$('#rfpForm input[@tabindex="1"]').focus();
	}
}