/**
 * h5Validate
 * @version v0.9.0
 * Using semantic versioning: http://semver.org/
 * @author Eric Hamilton http://ericleads.com/
 * @copyright 2010 - 2012 Eric Hamilton
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

(function(e){"use strict";var t=window.console||function(){},n={defaults:{debug:false,RODom:false,patternLibrary:{phone:/([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{1,6}[\)])?([0-9A-Za-z \.\-]{1,32})(([A-Za-z \:]{1,11})?[0-9]{1,4}?)/,email:/((([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-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-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD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&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?/,number:/-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?/,dateISO:/\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}/,alpha:/[a-zA-Z]+/,alphaNumeric:/\w+/,integer:/-?\d+/},classPrefix:"h5-",errorClass:"ui-state-error",validClass:"ui-state-valid",activeClass:"active",requiredClass:"required",requiredAttribute:"required",patternAttribute:"pattern",errorAttribute:"data-h5-errorid",customEvents:{validate:true},kbSelectors:":input:not(:button):not(:disabled):not(.novalidate)",focusout:true,focusin:false,change:true,keyup:false,activeKeyup:true,mSelectors:'[type="range"]:not(:disabled):not(.novalidate), :radio:not(:disabled):not(.novalidate), :checkbox:not(:disabled):not(.novalidate), select:not(:disabled):not(.novalidate), option:not(:disabled):not(.novalidate)',click:true,requiredVar:"h5-required",patternVar:"h5-pattern",stripMarkup:true,submit:true,focusFirstInvalidElementOnSubmit:true,validateOnSubmit:true,invalidCallback:function(){},validCallback:function(){},allValidSelectors:":input:visible:not(:button):not(:disabled):not(.novalidate)",markInvalid:function(n){var r=e(n.element),i=e(n.errorID);r.addClass(n.errorClass).removeClass(n.validClass);r.addClass(n.settings.activeClass);if(i.length){if(r.attr("title")){i.text(r.attr("title"))}i.show()}r.data("valid",false);n.settings.invalidCallback.call(n.element,n.validity);return r},markValid:function(n){var r=e(n.element),i=e(n.errorID);r.addClass(n.validClass).removeClass(n.errorClass);if(i.length){i.hide()}r.data("valid",true);n.settings.validCallback.call(n.element,n.validity);return r},unmark:function(n){var r=e(n.element);r.removeClass(n.errorClass).removeClass(n.validClass);r.form.find("#"+n.element.id).removeClass(n.errorClass).removeClass(n.validClass);return r}}},r=n.defaults,i=r.patternLibrary,s=function(n){return e.extend({customError:n.customError||false,patternMismatch:n.patternMismatch||false,rangeOverflow:n.rangeOverflow||false,rangeUnderflow:n.rangeUnderflow||false,stepMismatch:n.stepMismatch||false,tooLong:n.tooLong||false,typeMismatch:n.typeMismatch||false,valid:n.valid||true,valueMissing:n.valueMissing||false},n)},o={isValid:function(t,n){var r=e(this);n=t&&n||{};if(n.revalidate!==false){r.trigger("validate")}return r.data("valid")},allValid:function(t,n){var r=true,i=[],s=e(this),o,u,a=[],f=function(t,n){n.e=t;i.push(n)},l=e.extend({},t,n);n=n||{};s.trigger("formValidate",{settings:e.extend(true,{},l)});s.undelegate(l.allValidSelectors,".allValid",f);s.delegate(l.allValidSelectors,"validated.allValid",f);o=s.find(l.allValidSelectors);u=o.filter(function(e){var t;if(this.tagName==="INPUT"&&this.type==="radio"){t=this.name;if(a[t]===true){return false}a[t]=true}return true});u.each(function(){var t=e(this);r=t.h5Validate("isValid",n)&&r});s.trigger("formValidated",{valid:r,elements:i});return r},validate:function(n){var r=e(this),i=r.filter("[pattern]")[0]?r.attr("pattern"):false,o=new RegExp("^(?:"+i+")$"),u=null,a=r.is("[type=checkbox]")?r.is(":checked"):r.is("[type=radio]")?(u=r.parents("form").find('input[name="'+r.attr("name")+'"]')).filter(":checked").length>0:r.val(),f=n.errorClass,l=n.validClass,c=r.attr(n.errorAttribute)||false,h=c?"#"+c.replace(/(:|\.|\[|\])/g,"\\$1"):false,p=false,d=s({element:this,valid:true}),v=e("<input required>"),m;if(v.filter("[required]")&&v.filter("[required]").length){p=r.filter("[required]").length&&r.attr("required")!=="false"}else{p=r.attr("required")!==undefined}if(n.debug&&window.console){t.log('Validate called on "'+a+'" with regex "'+o+'". Required: '+p);t.log("Regex test: "+o.test(a)+", Pattern: "+i)}m=parseInt(r.attr("maxlength"),10);if(!isNaN(m)&&a.length>m){d.valid=false;d.tooLong=true}if(p&&!a){d.valid=false;d.valueMissing=true}else if(i&&!o.test(a)&&a){d.valid=false;d.patternMismatch=true}else{if(!n.RODom){n.markValid({element:this,validity:d,errorClass:f,validClass:l,errorID:h,settings:n})}}if(!d.valid){if(!n.RODom){n.markInvalid({element:this,validity:d,errorClass:f,validClass:l,errorID:h,settings:n})}}r.trigger("validated",d);if(u!==null&&n.alreadyCheckingRelatedRadioButtons!==true){n.alreadyCheckingRelatedRadioButtons=true;u.not(r).trigger("validate");n.alreadyCheckingRelatedRadioButtons=false}},delegateEvents:function(t,n,r,i){var s={},o=0,u=function(){i.validate.call(this,i)};e.each(n,function(e,t){if(t){s[e]=e}});for(o in s){if(s.hasOwnProperty(o)){e(r).delegate(t,s[o]+".h5Validate",u)}}return r},bindDelegation:function(t){var n=e(this),r;e.each(i,function(n,r){var i=r.toString();i=i.substring(1,i.length-1);e("."+t.classPrefix+n).attr("pattern",i)});r=n.filter("form").add(n.find("form")).add(n.parents("form"));r.attr("novalidate","novalidate").submit(u);r.find("input[formnovalidate][type='submit']").click(function(){e(this).closest("form").unbind("submit",u)});return this.each(function(){var e={focusout:t.focusout,focusin:t.focusin,change:t.change,keyup:t.keyup},n={click:t.click},r={keyup:t.activeKeyup};t.delegateEvents(":input",t.customEvents,this,t);t.delegateEvents(t.kbSelectors,e,this,t);t.delegateEvents(t.mSelectors,n,this,t);t.delegateEvents(t.activeClassSelector,r,this,t);t.delegateEvents("textarea[maxlength]",{keyup:true},this,t)})}},u=function(t){var n,r=l.call(this),i;if(r.submit!==true){return}n=e(this);i=n.h5Validate("allValid",{revalidate:r.validateOnSubmit===true});if(i!==true){t.preventDefault();if(r.focusFirstInvalidElementOnSubmit===true){var s=e(r.allValidSelectors,n).filter(function(t){return e(this).h5Validate("isValid",{revalidate:false})!==true});s.first().focus()}}return i},a=[],f=function(n){var i=e.extend({},r,n,o),s=i.classPrefix+i.activeClass;return e.extend(i,{activeClass:s,activeClassSelector:"."+s,requiredClass:i.classPrefix+i.requiredClass,el:this})},l=function(){var n=e(this).closest("[data-h5-instanceId]");return a[n.attr("data-h5-instanceId")]},c=function(n){var r=a.push(n)-1;if(n.RODom!==true){e(this).attr("data-h5-instanceId",r)}e(this).trigger("instance",{"data-h5-instanceId":r})};e.h5Validate={addPatterns:function(e){var t=r.patternLibrary,n;for(n in e){if(e.hasOwnProperty(n)){t[n]=e[n]}}return t},validValues:function(t,n){var r=0,i=n.length,s="",o;for(r=0;r<i;r+=1){s=s?s+"|"+n[r]:n[r]}o=new RegExp("^(?:"+s+")$");e(t).data("regex",o)}};e.fn.h5Validate=function(n){var r,i,s;if(typeof n==="string"&&typeof o[n]==="function"){s=l.call(this);i=[].slice.call(arguments,0);r=n;i.shift();i=e.merge([s],i);return s[r].apply(this,i)}s=f.call(this,n);c.call(this,s);return o.bindDelegation.call(this,s)}})(jQuery);
/* end h5Validate */
