// JavaScript Document

$(document).ready(function(){
	if($('#field_marketing').val() == 'Other')
	{
		$('#other').show();
	}
	else
	{
		$('#other').hide();
		$('#field_marketing_other').val('');
	}
	
	$('#field_marketing').change(function(){
		var $str = $(this).val();
		if($str == 'Other')
		{
			$('#other').show();	
		}
		else
		{
			$('#other').hide();	
			$('#field_marketing_other').val('');
			$('#field_marketing_other').focus().closest('li').removeClass('err').find('p.err').remove();
		}
	});
	
});

function validate(form)
{
	$(form).find('li.err').removeClass('err').find('p.err').remove();
	var input_valid=true;
	
	if(!$('#field_first_name').val().match(/\w/)){
		$('#field_first_name').focus().closest('li').addClass('err').append('<p class="err">You must enter your first name</p>');
		input_valid=false;
	}
	
	if(!$('#field_last_name').val().match(/\w/)){
        $('#field_last_name').focus().closest('li').addClass('err').append('<p class="err">You must enter your last name</p>');
        input_valid=false;
    }
	
	if(!$('#field_job_title').val().match(/\w/)){
		$('#field_job_title').focus().closest('li').addClass('err').append('<p class="err">You must enter your job title</p>');
		input_valid=false;
	}
	
	if(!$('#field_company_name').val().match(/\w/)){
		$('#field_company_name').focus().closest('li').addClass('err').append('<p class="err">You must enter your company name</p>');
		input_valid=false;
	}
	
    if(!$('#field_address1').val().match(/\w/)){
        $('#field_address1').focus().closest('li').addClass('err').append('<p class="err">You must enter your address</p>');
        input_valid=false;
    }
    
    if(!$('#field_city').val().match(/\w/)){
        $('#field_city').focus().closest('li').addClass('err').append('<p class="err">You must enter your city</p>');
        input_valid=false;
    }
    
    if($('#field_state').val() == ''){
        $('#field_state').focus().closest('li').addClass('err').append('<p class="err">You must enter your state</p>');
        input_valid=false;
    }
    
    if(!$('#field_zip').val().match(/\w/)){
        $('#field_zip').focus().closest('li').addClass('err').append('<p class="err">You must enter your zip code</p>');
        input_valid=false;
    }
	
	/*if(!$('#field_zip').val().match(/^\d{5}(\-\d{4})?$/)){
        $('#field_zip').focus().closest('li').addClass('err').append('<p class="err">You must enter your zip code</p>');
        input_valid=false;
    }*/
	
	if(!$('#field_email').val().match(/^\s*[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,4}\s*$/)){
        $('#field_email').focus().closest('li').addClass('err').append('<p class="err">You must enter your a valid email address</p>');
        input_valid=false;
    }
	
	if($('#field_phone').val() != "")
	{
		if(!$('#field_phone').val().match(/^\s*[\(]?\d{3}[\-\)\s\.]*\d{3}[\-\s\.]*\d{4}\s*$/)){
			$('#field_phone').focus().closest('li').addClass('err').append('<p class="err">You must enter your phone number in xxx-xxx-xxxx format</p>');
			input_valid=false;
		}
	}
	
    if($('#literature input:checkbox:checked').length==0){
        $('#literature').focus().addClass('err').append('<p class="err">You must select a literature option</p>');
        input_valid=false;
    }
	
	if($('#field_marketing').val() == '')
	{
		$('#field_marketing').focus().closest('li').addClass('err').append('<p class="err">Please indicate how your heard about the DM Series</p>');
		input_valid=false;
	}
	
	if($('#field_marketing').val() == 'Other' && $('#field_marketing_other').val() == '')
	{
		$('#field_marketing_other').focus().closest('li').addClass('err').append('<p class="err">You must explain how you heard about the DM Series</p>');
        input_valid=false;
	}
    
    if(!input_valid){
        window.scrollBy(0,-30);
    }
	
	return input_valid;
}
	

