$(document).ready(function(){

    var $email = $("input#cm-email"),
		$name = $('input[name="name"]'),
		$from = $('input[name="from"]'),
		$message = $('input[name="message"]'),
        $form = $("form#newsletter");
    
    $email.val($email.attr('title'));

    // clear it out on focus
    $email.focus(function(){
        $(this).val("");
    });

    // if it's blank then reset back the title attribute
    $email.blur(function(){
        // alert($(this).val().length);
        if($(this).val().length < 1){
            $email.val($email.attr('title'));
        }
    });

	// NAME
    // clear it out on focus
	$name.focus(function() {
		if($(this).val() == $(this).attr('title')){
			$(this).val("");
		}
	});

	// if it's blank then reset back the title attribute
	$name.blur(function(){
		if($(this).val().length < 1){
			$(this).val($(this).attr('title'));
		}
	});
	
	// FROM
	$from.focus(function() {
		if($(this).val() == $(this).attr('title')){
			$(this).val("");
		}
	});

	// if it's blank then reset back the title attribute
	$from.blur(function(){
		if($(this).val().length < 1){
			$(this).val($(this).attr('title'));
		}
	});
	
	// MESSAGE
	$message.focus(function() {
		if($(this).html() == $(this).attr('title')){
			$(this).html("");
		}
	});

	// if it's blank then reset back the title attribute
	$message.blur(function(){
		if($(this).html().length < 1){
			$(this).html($(this).attr('title'));
		}
	});




    $form.submit(function(e) {
	    
	    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
            $emailVal = $email.val();
        
        if ($emailVal == '' || $emailVal == 'email@domain.com') {
           // $email.addClass('error');
           alert('Please enter an email address');
           return false;
        }
        
        else if(!pattern.test($emailVal)) {
          // $email.addClass('error');
          alert('Please enter an valid email address');
          return false;
        }
       
        $form.append('<img src="/images/loading.gif" class="loading">');
        // $form.find("span").hide();
        
        // Grab form action
		formAction = $form.attr("action");
		
        // Serialize form values to be submitted with POST
        var str = $form.serialize();
        
        // Add form action to end of serialized data
		final = str + "&action=" + formAction;
		
		// Submit the form via ajax
		$.ajax({
			url: "/cm-proxy",
			type: "POST",
			data: final,
			success: function(data){
				//Check to make sure that the email was accepted
				if (data.search(/invalid/i) != -1) {
					alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
				}
				else
				{
				    
				$form.find('div#newsletter-inner').animate({opacity: 0}, 1000, null, function(){
                    $(this).hide();
                    $form.find('div#newsletter-wrap').append('<p><strong>You have successfully subscribed</strong></p>');
                    $form.find('strong').css({opacity: 0}).animate({opacity: 1});     
                });
                    $form.find('img.loading').animate({opacity: 0}, 1500);
				}
			}
		});

        e.preventDefault();       
               
    });
	
});
