jQuery( document ).ready( function( $ ) { $( '.dk-speakup-submit' ).click( function() { var petition_wrap = '#' + $( this ).attr( 'name' ); var id = $( petition_wrap ).find( '.dk-speakup-id' ).val(); var permalink = $( petition_wrap ).find( '.dk-speakup-permalink' ).val(); var posttitle = $( petition_wrap ).find( '.dk-speakup-posttitle' ).val(); var first_name = $( petition_wrap ).find( '.dk-speakup-first-name' ).val(); var last_name = $( petition_wrap ).find( '.dk-speakup-last-name' ).val(); var email = $( petition_wrap ).find( '.dk-speakup-email' ).val(); var address = $( petition_wrap ).find( '.dk-speakup-address' ).val(); var optin = ''; var twitter_message = $( petition_wrap ).find( '.dk-speakup-twitter-message' ).val(); if ( $( petition_wrap ).find( '.dk-speakup-optin' ).attr( 'checked' ) ) { optin = 'on'; } // make sure error notices are turned off before checking for new errors $( petition_wrap ).find( 'input' ).removeClass( 'dk-speakup-error' ); // validate form values var errors = 0; var emailRegEx = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; if ( email == '' || !emailRegEx.test( email ) ) { $( petition_wrap ).find( '.dk-speakup-email' ).addClass( 'dk-speakup-error' ); errors ++; } if ( first_name == '' ) { $( petition_wrap ).find( '.dk-speakup-first-name' ).addClass( 'dk-speakup-error' ); errors ++; } if ( last_name == '' ) { $( petition_wrap ).find( '.dk-speakup-last-name' ).addClass( 'dk-speakup-error' ); errors ++; } // if no errors found, submit the data via ajax if ( errors == 0 ) { var data = { action: 'dk_speakup_sendmail', id: id, first_name: first_name, last_name: last_name, email: email, address: address, optin: optin }; // submit form data and handle ajax response $.post( dk_speakup_js.ajaxurl, data, function( response ) { $( petition_wrap ).find( '.dk-speakup-petition' ).hide(); $( petition_wrap ).find( '.dk-speakup-response' ).fadeIn().html( response ); $( petition_wrap ).find( '.dk-speakup-share' ).fadeIn(); // launch Facebook sharing window $( '.dk-speakup-facebook' ).click( function() { var url = 'http://www.facebook.com/sharer.php?u=' + permalink + '&t=' + posttitle; window.open( url, 'facebook', 'height=420,width=550,left=100,top=100,resizable=yes,location=no,status=no,toolbar=no' ); }); // launch Twitter sharing window $( '.dk-speakup-twitter' ).click( function() { var url = 'http://twitter.com/share?url=' + permalink + '&text=' + twitter_message; window.open( url, 'twitter', 'height=420,width=550,left=100,top=100,resizable=yes,location=no,status=no,toolbar=no' ); }); } ); } return false; }); // hide or show form labels depending on input fields $( '.dk-speakup-input' ).focus( function( e ) { var label = $( this ).siblings( 'label' ); if ( $( this ).val() == '' ) { $( this ).siblings( 'label' ).addClass( 'dk-speakup-focus' ).removeClass( 'dk-speakup-blur' ); } $( this ).blur( function(){ if ( this.value === '' ) { label.addClass( 'dk-speakup-blur' ).removeClass( 'dk-speakup-focus' ); } }).focus( function() { label.addClass( 'dk-speakup-focus' ).removeClass( 'dk-speakup-blur' ); }).keydown( function( e ) { label.addClass( 'dk-speakup-focus' ).removeClass( 'dk-speakup-blur' ); $( this ).unbind( e ); }); }); // hide labels on filled input fields when page is reloaded $( '.dk-speakup-input' ).each( function() { if ( $( this ).val() != '' ) { $( this ).siblings( 'label' ).addClass( 'dk-speakup-focus' ); } }); });