/*WP Ajax Email Signup Script --Created by: Mike Gehard --Created on: 04/09/2009 --Last modified on: 04/09/2009 --Relies on jQuery, jQueryUI Copyright 2009 Mike Gehard (email : mike [a t ] sam yama tech DOT com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ jQuery(document).ready(function() { var $j = jQuery; $j.ajaxemailsignup = { init: function(){initialize_events(); initialize_ui();} }; function initialize_events() { //Title for new window $j(".ajaxEmailSignup").bind("click", function(){openDialog();}); } function initialize_ui(){ // go get the form UI from the server initialize_forms(); } function initialize_forms(){ $j("#emailSignupForm").dialog({ bgiframe: true, autoOpen: false, modal: true, buttons: { 'Sign Up': function() { var bValid = true; allFields.removeClass('ui-state-error'); bValid = bValid && checkLength(firstName,"first name",1,50); bValid = bValid && checkLength(email,"email",6,80); bValid = bValid && checkLength(lastName,"last name",1,50); bValid = bValid && checkLength(address,"address",1,50); bValid = bValid && checkLength(city,"city",1,50); bValid = bValid && checkLength(state,"state",1,50); bValid = bValid && checkLength(zip,"zip",1,10); // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/ bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com"); if (bValid) { signup(firstName.val(), lastName.val(), address.val(), city.val(), state.val(), zip.val(), email.val()); } }, Cancel: function() { $(this).dialog('close'); } }, close: function() { allFields.val('').removeClass('ui-state-error'); } }); $j("#successDialog").dialog({ bgiframe: true, autoOpen: false, modal: true, buttons: { Ok: function() { $(this).dialog('close'); } } }); $j("#failureDialog").dialog({ bgiframe: true, autoOpen: false, modal: true, buttons: { Ok: function() { $(this).dialog('close'); } } }); } function openDialog(){ $j('#emailSignupForm').dialog('open'); } function initializeAjaxRequest(){ var s = {}; s.dataType = "text"; s.type = "POST"; return s; } function signup(firstName, lastName, address, city, state, zip, emailAddress) { var ajaxRequest = initializeAjaxRequest(); ajaxRequest.url = "http://www.roundupriverranch.org/wp-content/plugins/samyamatech-com-ajax-email-signup/php/samyamatech-com-email-ajax.php"; ajaxRequest.data = $j.extend({ firstName: firstName, lastName: lastName,address: address, city: city, state: state, zip: zip, emailAddress: emailAddress, check: check},''); ajaxRequest.success = signupAjaxSuccess; ajaxRequest.error = signupAjaxError; $j.ajax(ajaxRequest); } function signupAjaxSuccess(data, textStatus) { //var jsonDecodedStatus = JSON.parse(data); processStatus(data); } function signupAjaxError(){ $j('#failureDialog').dialog('open'); } function processStatus(status){ if (status.indexOf("success") > 0){ $j('#emailSignupForm').dialog('close'); $j('#successDialog').dialog('open'); }else{ $j('#failureDialog').dialog('open'); } } /* Code to help set up the dialog */ var firstName = $("#firstName"), email = $("#email"), lastName = $("#lastName"), address = $("#address"), city = $("#city"), state = $("#state"), zip = $("#zip"), check = $("#check"), allFields = $([]).add(firstName).add(email).add(lastName).add(address).add(city).add(state).add(zip), tips = $("#validateTips"); function updateTips(t) { tips.text(t).effect("highlight",{},4500); } function checkLength(o,n,min,max) { if ( o.val().length > max || o.val().length < min ) { o.addClass('ui-state-error'); updateTips("Length of " + n + " must be between "+min+" and "+max+"."); return false; } else { return true; } } function checkRegexp(o,regexp,n) { if ( !( regexp.test( o.val() ) ) ) { o.addClass('ui-state-error'); updateTips(n); return false; } else { return true; } } $j.ajaxemailsignup.init(); });