var groups = new Array(); groups["a"] = new Array(); groups["b"] = new Array(); $(document).ready(function() { $(".votebutton").click(function() { var qvid = $(this).attr("id"); var group = qvid.substr(4,1); var qid = qvid.substr(5); var voted = $("#voted"+qid).val(); if (voted == 0) { if (sizeOfArray(groups[group]) < 5) { $(this).parent().css('background-color','#DCDEE3'); $("#voted"+qid).val(1); groups[group][qid] = 1; } else { alert('You can only select 5 models per group for a total of 10!'); } } else { $(this).parent().css('background-color','#FFF'); $("#voted"+qid).val(0); groups[group][qid] = null; } }); $("#submitvotes").click(function(){ if (sizeOfArray(groups["a"]) != 5 || sizeOfArray(groups["b"]) != 5) { alert ('You can only select 5 models per group for a total of 10!'); } else { $('#dialog').dialog('open'); } }); $("#dialog").dialog({ bgiframe: true, autoOpen: false, height: 210, modal: true, buttons: { 'SUBMIT YOUR VOTE' : function() { var bValid = true; var email = $("#email"); var allFields = $([]).add(email); allFields.removeClass('ui-state-error'); bValid = bValid && checkLength(email,"email",6,80); // 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) { var groupAID = getKeys(groups["a"]); var groupBID = getKeys(groups["b"]); var ids = groupAID.concat(groupBID); $.post("includes/votequals.php", {emailaddr: email.val(), 'choices[]':ids}, function(str) { alert(str); }); $(this).dialog('close'); } }, Cancel: function() { $(this).dialog('close'); } }, close: function() { allFields.val('').removeClass('ui-state-error'); } }); }); 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; } } function updateTips(t) { $("#validateTips").text(t).effect("highlight",{},1500); } function sizeOfArray(array){ var size = 0; for (var i in array) { if (array[i] != null) size ++; } return size; } function getKeys(array){ var values = new Array(); for (var i in array) { if (array[i] != null) values.push(i); } return values; }