/*
	functions.forms.js
	DOM Interactivity / validation Functions for Forms
	Created: 2.15.08
	Creator: Matt Kircher, IDa Creative
*/

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){
		
		//recaptcha
		var recap2 = $('<div id="recap2"></div>');
		$(recap2).insertAfter($('#recaptcha_table'));
		
		$('#recaptcha_table').hide()
		.removeClass('recaptcha_theme_red')
		.find('#recaptcha_image').appendTo('#recap2').end()
		.find('.recaptcha_input_area').appendTo('#recap2').end()
		.find('#recaptcha_reload_btn').appendTo('#recap2 .recaptcha_input_area').end()
		.find('#recaptcha_switch_audio_btn').appendTo('#recap2 .recaptcha_input_area').end()
		.find('#recaptcha_whatsthis_btn').appendTo('#recap2 .recaptcha_input_area').end()
		.remove();
		
		//(format)
			$(recap2).find('.recaptcha_input_area_text').next().remove();
			$(recap2)
			.find('img').attr({ border:'0', align:'left' }).end()
			.find('.recaptcha_input_area').css({ position:'relative', left:'-20px' });
			
			$('#recaptcha_reload').parent().css({ position:'relative', top:'-90px', left:'180px' });
			$('#recaptcha_switch_audio').parent().css({ position:'relative', top:'-75px', left:'205px' });
			$('#recaptcha_whatsthis').parent().css({ position:'relative', top:'-59px', left:'230px' });
			$('#recaptcha_response_field').css({ position:'relative', top:'-15px', left:'131px' });
		
		//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
	$('#contactForm .required').removeClass('required_attention');
	$('#contactForm .select_required_attention').remove();
	$('#contactForm .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){
		$('#contactForm .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;
}

//DOM loaded
$(document).ready(function(){
	setupContactForm();
});