/*
## @project: SolidForm Maintenance page w/ ajax contact form
## @filename: ajax-functions.js
## @description: the jquery that handles the ajax calls
## @author: PlasticBrain Media LLC | plasticbrain.net
*/

$(document).ready( function(){
	
	// add the asterisks to the required fields
	$('.required').prev( 'label' ).append( "<span class='required'>*</span>" );
	
	// Handle the contact form
	$('#btn_contact').click( function() {
		var elem = $(this);
		var cName = $('#contact_name');
		var cEmail = $('#contact_email');
		var cMsg = $('#contact_msg');
		var statusMsg = $('#contact_status');
		
		elem.val( 'Sending...' );
		statusMsg.removeClass( 'error' ).addClass( 'loading' ).html( "Sending message...Please wait" );
		elem.hide();
		$.ajax({
			type: "POST",
   		url: "js/ajax-actions.php",
   		data: "action=contact&name="+cName.val()+"&email="+cEmail.val()+"&msg="+cMsg.val(),
   		success: function(msg){
   			if( msg == "true" ) {
   				statusMsg.addClass( 'success' ).html( "Your message was sent. Thank you!" );
   				$("#msg_contact").hide().removeClass( 'error' ).addClass( 'success' ).html( "<p>Your message was sent. We appreciated your feedback!</p>" ).fadeIn( 'slow' );
   			} else {
   				statusMsg.addClass( 'error' ).html( "Please correct the errors above..." );
   				elem.val( 'Send Message' ).show();
   				$("#msg_contact").addClass( 'error' ).html( msg ).fadeIn( 'slow' );
     		}
   		}
 		});
		return false;
	});
	
	// Handle the subscribe form
	$('#btn_subscribe').click( function() {
		var elem = $(this);
		var sEmail = $('#subscribe_email');
		elem.val( 'Subscribing...' );
		elem.hide();
		$.ajax({
			type: "POST",
   		url: "js/ajax-actions.php",
   		data: "action=subscribe&email="+sEmail.val(),
   		success: function(msg){
   			if( msg == "true" ) {
   				$("#msg_subscribe").hide().removeClass( 'error' ).addClass( 'success' ).html( "<p>When the site is ready, we will notify you at "+sEmail.val()+"!</p>" ).fadeIn( 'slow' );
   			} else {
   				elem.val( 'Subscribe' ).show();
   				$("#msg_subscribe").addClass( 'error' ).html( msg ).fadeIn( 'slow' );
     		}
   		}
 		});
		return false;
	});

});