'use strict';var AOP=AOP||{};(function($){var isbn13MultilineTextarea=function(el,required,parent){$(el).off('keypress').on('keypress',function(e){$(parent).removeClass('error');});$(el).val($(el).val().replace(/(^[ \t]*\n)/gm,''));$('span[data-isbn-error="true"]').remove();if($(el).val()===''){if(required&&$(el).data('emptyError')){$(parent).find('.error').html($(el).data('emptyError'));}
return!required;}
var re=/^((?!^(\s+)?\d{12}(\d|X)(\s+)?$).)*$/mg;var val=el.value;var m;var valid=true;var errors=[];while((m=re.exec(val))!==null){if(m.index===re.lastIndex){re.lastIndex++;}
valid=false;errors.push({input:m[0],index:m.index});}
if(!valid){var errorHtml=$('<span/>');errors.forEach(function(v){if(errorHtml.html().length>0){errorHtml.append($('<br/>'));}
var remove=$('<a>(delete)</a>');remove.on('click',(function(el,instanceDetails){return function(ev){ev.preventDefault();var index=instanceDetails.index;var input=instanceDetails.input;var length=input.length;var originalValue=$(el).val();var newValue=originalValue.substr(0,index)+ originalValue.substr(index+ length);$(el).val(newValue.replace(/\n$/,''));isbn13MultilineTextarea(el,required,parent);};})(el,v));$(errorHtml).append($('<span data-isbn-error="true">Invalid ISBN: "'+ v.input+'" </span> ').append(remove));});$(parent).find('.error').html(errorHtml);}
if(valid){$(parent).removeClass('error');}
return valid;};var issnMultilineTextarea=function(el,required,parent){$(el).off('keypress').on('keypress',function(e){$(parent).removeClass('error');});$(el).val($(el).val().replace(/(^[ \t]*\n)/gm,''));$('span[data-issn-error="true"]').remove();if($(el).val()===''){if(required&&$(el).data('emptyError')){$(parent).find('.error').html($(el).data('emptyError'));}
return!required;}
var re=/^((?!^(\s+)?(\d|[a-z]|[A-Z]){4}(-|\s)?(\d|[a-z]|[A-Z]){4}(\s+)?$).)*$/mg;var val=el.value;var m;var valid=true;var errors=[];while((m=re.exec(val))!==null){if(m.index===re.lastIndex){re.lastIndex++;}
valid=false;errors.push({input:m[0],index:m.index});}
if(!valid){var errorHtml=$('<span/>');errors.forEach(function(v){if(errorHtml.html().length>0){errorHtml.append($('<br/>'));}
var remove=$('<a>(delete)</a>');remove.on('click',(function(el,instanceDetails){return function(ev){ev.preventDefault();var index=instanceDetails.index;var input=instanceDetails.input;var length=input.length;var originalValue=$(el).val();var newValue=originalValue.substr(0,index)+ originalValue.substr(index+ length);$(el).val(newValue.replace(/\n$/,''));issnMultilineTextarea(el,required,parent);};})(el,v));$(errorHtml).append($('<span data-issn-error="true">Invalid ISSN: "'+ v.input+'" </span> ').append(remove));});$(parent).find('.error').html(errorHtml);}
if(valid){$(parent).removeClass('error');}
return valid;};var orcidRegex=/^((?:(?:\d{4}[-]){3}\d{3}(\d|X)))$/;var emailRegex=/^(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})$/;AOP.emailRegex=emailRegex;$(document).foundation({abide:{timeout:0,live_validate:true,validate_on_blur:true,patterns:{slug:/^[0-9-a-zA-Z]*$/,corepassword:/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/,openUrlCustomText:/^.{0,60}$/,email:emailRegex},validators:{orcidval:function(el,required,parent){var primaryErrorEl=$(parent).find('#primary-error');var customErrorEl=$(parent).find('#custom-error');primaryErrorEl.hide();customErrorEl.hide();var value=el.value;var output=false;if(value===''){primaryErrorEl.show();return true;}
if(!value.match(orcidRegex)){primaryErrorEl.show();return false;}
$.ajax({url:'//pub.orcid.org/v1.1/search/orcid-bio/?q=orcid:'+ value,headers:{'Accept':'application/json'},async:false}).success(function(data){var results=data['orcid-search-results']['num-found'];if(results===0){customErrorEl.show();output=false;}else{output=true;}}).error(function(){output=true;});return output;},orcidformat:function(el,required,parent){var primaryErrorEl=$(parent).find('#primary-error');var customErrorEl=$(parent).find('#custom-error');primaryErrorEl.hide();customErrorEl.hide();var value=el.value;var output=false;if(value===''){primaryErrorEl.show();return true;}
if(!value.match(orcidRegex)){primaryErrorEl.show();return false;}else{primaryErrorEl.hide();return true;}},unique:function(el,required,parent){var primaryErrorEl=$(parent).find('#primary-error');var customErrorEl=$(parent).find('#custom-error');var selected=$(parent).find('#selectedValues').val().split(',').filter(Boolean);var val=$(el).val().toString().toLowerCase().trim();if(val&&selected.indexOf(val)>=0){primaryErrorEl.hide();customErrorEl.show();return false;}else{primaryErrorEl.show();customErrorEl.hide();return true;}},subscriptionNumber:function(el,required,parent){var regex=/^\d{10}$/;var primaryErrorEl=$(parent).find('#primary-error');var customErrorEl=$(parent).find('#custom-error');var selected=$(parent).find('#selectedValues').val().split(',').filter(Boolean);var value=$(el).val().trim();if(value&&selected.indexOf(value)>=0){primaryErrorEl.hide();customErrorEl.show();return false;}else if(value&&!value.match(regex)){primaryErrorEl.show();customErrorEl.hide();return false;}else{primaryErrorEl.hide();customErrorEl.hide();return true;}},shibboleth:function(el,required,parent){var primaryErrorEl=$(parent).find('#primary-error');var customErrorEl=$(parent).find('#custom-error');var selected=$(parent).find('#selectedValues').val().split(',').filter(Boolean);var value=$(el).val().toString().toLowerCase().trim();if(value&&selected.indexOf(value)>=0){primaryErrorEl.hide();customErrorEl.show();return false;}else if((value&&!value.match(emailRegex))||!value){primaryErrorEl.show();customErrorEl.hide();return false;}else{primaryErrorEl.hide();customErrorEl.hide();return true;}},optionRequired:function(el,required,parent){var selected=$(parent).find('input:checked').val();var primaryErrorEl=$(parent).find('#primary-error');if(!selected){primaryErrorEl.show();return false;}else{$(parent).find('label').removeClass('error');primaryErrorEl.hide();return true;}},email:function(el,required,parent){return el.value.match(emailRegex);},checkbox_limit:function(el,required,parent){var group=parent.closest('.checkbox-group, .radio-group');var min=group.attr('data-abide-validator-min');var checked=group.find(':checked').length;var errors=group.find('div.error, label.error');if(checked>=min){group.removeClass('error');group.find('small.error').hide();errors.removeClass('error');return true;}else{group.addClass('error');group.find('small.error').css({display:'block'});return false;}},aopEnhanced:function(el){return $(el).is('[data-aop-is-valid]');},isbn13MultilineTextarea:isbn13MultilineTextarea,issnMultilineTextarea:issnMultilineTextarea,isSanctionedCountry:function(el,required,parent){var endpoint=AOP.baseUrl+'/services/aop-cambridge-membership/konakart/sanctioned';var isSanctioned=$(el).find('option:selected').attr('data-sanctioned');var primaryError=$(el).siblings('.country-required-error');var customError=$(el).siblings('.sanctioned-country-error');primaryError.hide();customError.hide();if(!el.value||el.value.length===0){customError.hide();primaryError.show();return false;}
if(isSanctioned){customError.show();primaryError.hide();return false;}
$(parent).removeClass('error');$(parent).siblings('div').find('label').removeClass('error');return true;}}}});})(jQuery);