/* 
 * Copyright 2009-2010 Emily Stark, Mike Hamburg, Dan Boneh, Stanford University.
 * All rights reserved.
 *  
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *  
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *  
 * The views and conclusions contained in the software and documentation
 * are those of the authors and should not be interpreted as representing
 * official policies, either expressed or implied, of the authors.
 */
!function(){"use strict";var b={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(t){this.toString=function(){return"CORRUPT: "+this.message},this.message=t},invalid:function(t){this.toString=function(){return"INVALID: "+this.message},this.message=t},bug:function(t){this.toString=function(){return"BUG: "+this.message},this.message=t},notReady:function(t){this.toString=function(){return"NOT READY: "+this.message},this.message=t}}};function t(){this.i=0,this.j=0,this.S=new Array}"undefined"!=typeof module&&module.exports&&(module.exports=b),b.cipher.aes=function(t){this._tables[0][0][0]||this._precompute();var e,i,r,n,o,s=this._tables[0][4],a=this._tables[1],h=t.length,c=1;if(4!==h&&6!==h&&8!==h)throw new b.exception.invalid("invalid aes key size");for(this._key=[n=t.slice(0),o=[]],e=h;e<4*h+28;e++)r=n[e-1],(e%h==0||8===h&&e%h==4)&&(r=s[r>>>24]<<24^s[r>>16&255]<<16^s[r>>8&255]<<8^s[255&r],e%h==0&&(r=r<<8^r>>>24^c<<24,c=c<<1^283*(c>>7))),n[e]=n[e-h]^r;for(i=0;e;i++,e--)r=n[3&i?e:e-4],o[i]=e<=4||i<4?r:a[0][s[r>>>24]]^a[1][s[r>>16&255]]^a[2][s[r>>8&255]]^a[3][s[255&r]]},b.cipher.aes.prototype={encrypt:function(t){return this._crypt(t,0)},decrypt:function(t){return this._crypt(t,1)},_tables:[[[],[],[],[],[]],[[],[],[],[],[]]],_precompute:function(){var t,e,i,r,n,o,s,a,h=this._tables[0],c=this._tables[1],l=h[4],u=c[4],p=[],f=[];for(t=0;t<256;t++)f[(p[t]=t<<1^283*(t>>7))^t]=t;for(e=i=0;!l[e];e^=r||1,i=f[i]||1)for(o=(o=i^i<<1^i<<2^i<<3^i<<4)>>8^255&o^99,a=16843009*p[n=p[r=p[u[l[e]=o]=e]]]^65537*n^257*r^16843008*e,s=257*p[o]^16843008*o,t=0;t<4;t++)h[t][e]=s=s<<24^s>>>8,c[t][o]=a=a<<24^a>>>8;for(t=0;t<5;t++)h[t]=h[t].slice(0),c[t]=c[t].slice(0)},_crypt:function(t,e){if(4!==t.length)throw new b.exception.invalid("invalid aes block size");var i,r,n,o,s=this._key[e],a=t[0]^s[0],h=t[e?3:1]^s[1],c=t[2]^s[2],l=t[e?1:3]^s[3],u=s.length/4-2,p=4,f=[0,0,0,0],d=this._tables[e],y=d[0],m=d[1],g=d[2],v=d[3],_=d[4];for(o=0;o<u;o++)i=y[a>>>24]^m[h>>16&255]^g[c>>8&255]^v[255&l]^s[p],r=y[h>>>24]^m[c>>16&255]^g[l>>8&255]^v[255&a]^s[p+1],n=y[c>>>24]^m[l>>16&255]^g[a>>8&255]^v[255&h]^s[p+2],l=y[l>>>24]^m[a>>16&255]^g[h>>8&255]^v[255&c]^s[p+3],p+=4,a=i,h=r,c=n;for(o=0;o<4;o++)f[e?3&-o:o]=_[a>>>24]<<24^_[h>>16&255]<<16^_[c>>8&255]<<8^_[255&l]^s[p++],i=a,a=h,h=c,c=l,l=i;return f}},b.bitArray={bitSlice:function(t,e,i){return t=b.bitArray._shiftRight(t.slice(e/32),32-(31&e)).slice(1),void 0===i?t:b.bitArray.clamp(t,i-e)},extract:function(t,e,i){var r=Math.floor(-e-i&31);return(-32&(e+i-1^e)?t[e/32|0]<<32-r^t[e/32+1|0]>>>r:t[e/32|0]>>>r)&(1<<i)-1},concat:function(t,e){if(0===t.length||0===e.length)return t.concat(e);var i=t[t.length-1],r=b.bitArray.getPartial(i);return 32===r?t.concat(e):b.bitArray._shiftRight(e,r,0|i,t.slice(0,t.length-1))},bitLength:function(t){var e,i=t.length;return 0===i?0:(e=t[i-1],32*(i-1)+b.bitArray.getPartial(e))},clamp:function(t,e){if(32*t.length<e)return t;var i=(t=t.slice(0,Math.ceil(e/32))).length;return e&=31,0<i&&e&&(t[i-1]=b.bitArray.partial(e,t[i-1]&2147483648>>e-1,1)),t},partial:function(t,e,i){return 32===t?e:(i?0|e:e<<32-t)+1099511627776*t},getPartial:function(t){return Math.round(t/1099511627776)||32},equal:function(t,e){if(b.bitArray.bitLength(t)!==b.bitArray.bitLength(e))return!1;var i,r=0;for(i=0;i<t.length;i++)r|=t[i]^e[i];return 0===r},_shiftRight:function(t,e,i,r){var n,o,s;for(void 0===r&&(r=[]);32<=e;e-=32)r.push(i),i=0;if(0===e)return r.concat(t);for(n=0;n<t.length;n++)r.push(i|t[n]>>>e),i=t[n]<<32-e;return o=t.length?t[t.length-1]:0,s=b.bitArray.getPartial(o),r.push(b.bitArray.partial(e+s&31,32<e+s?i:r.pop(),1)),r},_xor4:function(t,e){return[t[0]^e[0],t[1]^e[1],t[2]^e[2],t[3]^e[3]]}},b.codec.hex={fromBits:function(t){var e,i="";for(e=0;e<t.length;e++)i+=(0xf00000000000+(0|t[e])).toString(16).substr(4);return i.substr(0,b.bitArray.bitLength(t)/4)},toBits:function(t){var e,i,r=[];for(i=(t=t.replace(/\s|0x/g,"")).length,t+="00000000",e=0;e<t.length;e+=8)r.push(0^parseInt(t.substr(e,8),16));return b.bitArray.clamp(r,4*i)}},b.codec.utf8String={fromBits:function(t){var e,i,r="",n=b.bitArray.bitLength(t);for(e=0;e<n/8;e++)0==(3&e)&&(i=t[e/4]),r+=String.fromCharCode(i>>>24),i<<=8;return decodeURIComponent(escape(r))},toBits:function(t){t=unescape(encodeURIComponent(t));var e,i=[],r=0;for(e=0;e<t.length;e++)r=r<<8|t.charCodeAt(e),3==(3&e)&&(i.push(r),r=0);return 3&e&&i.push(b.bitArray.partial(8*(3&e),r)),i}},b.codec.base64={_chars:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",fromBits:function(t,e,i){var r,n="",o=0,s=b.codec.base64._chars,a=0,h=b.bitArray.bitLength(t);for(i&&(s=s.substr(0,62)+"-_"),r=0;6*n.length<h;)n+=s.charAt((a^t[r]>>>o)>>>26),o<6?(a=t[r]<<6-o,o+=26,r++):(a<<=6,o-=6);for(;3&n.length&&!e;)n+="=";return n},toBits:function(t,e){t=t.replace(/\s|=/g,"");var i,r,n=[],o=0,s=b.codec.base64._chars,a=0;for(e&&(s=s.substr(0,62)+"-_"),i=0;i<t.length;i++){if((r=s.indexOf(t.charAt(i)))<0)throw new b.exception.invalid("this isn't base64!");26<o?(o-=26,n.push(a^r>>>o),a=r<<32-o):a^=r<<32-(o+=6)}return 56&o&&n.push(b.bitArray.partial(56&o,a,1)),n}},b.codec.base64url={fromBits:function(t){return b.codec.base64.fromBits(t,1,1)},toBits:function(t){return b.codec.base64.toBits(t,1)}},void 0===b.beware&&(b.beware={}),b.beware["CBC mode is dangerous because it doesn't protect message integrity."]=function(){b.mode.cbc={name:"cbc",encrypt:function(t,e,i,r){if(r&&r.length)throw new b.exception.invalid("cbc can't authenticate data");if(128!==b.bitArray.bitLength(i))throw new b.exception.invalid("cbc iv must be 128 bits");var n,o=b.bitArray,s=o._xor4,a=o.bitLength(e),h=0,c=[];if(7&a)throw new b.exception.invalid("pkcs#5 padding only works for multiples of a byte");for(n=0;h+128<=a;n+=4,h+=128)i=t.encrypt(s(i,e.slice(n,n+4))),c.splice(n,0,i[0],i[1],i[2],i[3]);return a=16843009*(16-(a>>3&15)),i=t.encrypt(s(i,o.concat(e,[a,a,a,a]).slice(n,n+4))),c.splice(n,0,i[0],i[1],i[2],i[3]),c},decrypt:function(t,e,i,r){if(r&&r.length)throw new b.exception.invalid("cbc can't authenticate data");if(128!==b.bitArray.bitLength(i))throw new b.exception.invalid("cbc iv must be 128 bits");if(127&b.bitArray.bitLength(e)||!e.length)throw new b.exception.corrupt("cbc ciphertext must be a positive multiple of the block size");var n,o,s,a=b.bitArray,h=a._xor4,c=[];for(r=r||[],n=0;n<e.length;n+=4)o=e.slice(n,n+4),s=h(i,t.decrypt(o)),c.splice(n,0,s[0],s[1],s[2],s[3]),i=o;if(0==(o=255&c[n-1])||16<o)throw new b.exception.corrupt("pkcs#5 padding corrupt");if(s=16843009*o,!a.equal(a.bitSlice([s,s,s,s],0,8*o),a.bitSlice(c,32*c.length-8*o,32*c.length)))throw new b.exception.corrupt("pkcs#5 padding corrupt");return a.bitSlice(c,0,32*c.length-8*o)}}},b.misc.hmac=function(t,e){this._hash=e=e||b.hash.sha256;var i,r=[[],[]],n=e.prototype.blockSize/32;for(this._baseHash=[new e,new e],t.length>n&&(t=e.hash(t)),i=0;i<n;i++)r[0][i]=909522486^t[i],r[1][i]=1549556828^t[i];this._baseHash[0].update(r[0]),this._baseHash[1].update(r[1])},b.misc.hmac.prototype.encrypt=b.misc.hmac.prototype.mac=function(t,e){var i=new this._hash(this._baseHash[0]).update(t,e).finalize();return new this._hash(this._baseHash[1]).update(i).finalize()},b.hash.sha256=function(t){this._key[0]||this._precompute(),t?(this._h=t._h.slice(0),this._buffer=t._buffer.slice(0),this._length=t._length):this.reset()},b.hash.sha256.hash=function(t){return(new b.hash.sha256).update(t).finalize()},b.hash.sha256.prototype={blockSize:512,reset:function(){return this._h=this._init.slice(0),this._buffer=[],this._length=0,this},update:function(t){"string"==typeof t&&(t=b.codec.utf8String.toBits(t));var e,i=this._buffer=b.bitArray.concat(this._buffer,t),r=this._length,n=this._length=r+b.bitArray.bitLength(t);for(e=512+r&-512;e<=n;e+=512)this._block(i.splice(0,16));return this},finalize:function(){var t,e=this._buffer,i=this._h;for(t=(e=b.bitArray.concat(e,[b.bitArray.partial(1,1)])).length+2;15&t;t++)e.push(0);for(e.push(Math.floor(this._length/4294967296)),e.push(0|this._length);e.length;)this._block(e.splice(0,16));return this.reset(),i},_init:[],_key:[],_precompute:function(){var t,e=0,i=2;function r(t){return 4294967296*(t-Math.floor(t))|0}t:for(;e<64;i++){for(t=2;t*t<=i;t++)if(i%t==0)continue t;e<8&&(this._init[e]=r(Math.pow(i,.5))),this._key[e]=r(Math.pow(i,1/3)),e++}},_block:function(t){var e,i,r,n,o=t.slice(0),s=this._h,a=this._key,h=s[0],c=s[1],l=s[2],u=s[3],p=s[4],f=s[5],d=s[6],y=s[7];for(e=0;e<64;e++)e<16?i=o[e]:(r=o[e+1&15],n=o[e+14&15],i=o[15&e]=(r>>>7^r>>>18^r>>>3^r<<25^r<<14)+(n>>>17^n>>>19^n>>>10^n<<15^n<<13)+o[15&e]+o[e+9&15]|0),i=i+y+(p>>>6^p>>>11^p>>>25^p<<26^p<<21^p<<7)+(d^p&(f^d))+a[e],y=d,d=f,f=p,p=u+i|0,u=l,l=c,h=i+((c=h)&l^u&(c^l))+(c>>>2^c>>>13^c>>>22^c<<30^c<<19^c<<10)|0;s[0]=s[0]+h|0,s[1]=s[1]+c|0,s[2]=s[2]+l|0,s[3]=s[3]+u|0,s[4]=s[4]+p|0,s[5]=s[5]+f|0,s[6]=s[6]+d|0,s[7]=s[7]+y|0}},b.random={randomWords:function(t,e){var i,r,n=[],o=this.isReady(e);if(o===this._NOT_READY)throw new b.exception.notReady("generator isn't seeded");for(o&this._REQUIRES_RESEED&&this._reseedFromPools(!(o&this._READY)),i=0;i<t;i+=4)(i+1)%this._MAX_WORDS_PER_BURST==0&&this._gate(),r=this._gen4words(),n.push(r[0],r[1],r[2],r[3]);return this._gate(),n.slice(0,t)},setDefaultParanoia:function(t){this._defaultParanoia=t},addEntropy:function(t,e,i){i=i||"user";var r,n,o,s=(new Date).valueOf(),a=this._robins[i],h=this.isReady(),c=0;switch(void 0===(r=this._collectorIds[i])&&(r=this._collectorIds[i]=this._collectorIdNext++),void 0===a&&(a=this._robins[i]=0),this._robins[i]=(this._robins[i]+1)%this._pools.length,typeof t){case"number":void 0===e&&(e=1),this._pools[a].update([r,this._eventId++,1,e,s,1,0|t]);break;case"object":var l=Object.prototype.toString.call(t);if("[object Uint32Array]"===l){for(o=[],n=0;n<t.length;n++)o.push(t[n]);t=o}else for("[object Array]"!==l&&(c=1),n=0;n<t.length&&!c;n++)"number"!=typeof t[n]&&(c=1);if(!c){if(void 0===e)for(n=e=0;n<t.length;n++)for(o=t[n];0<o;)e++,o>>>=1;this._pools[a].update([r,this._eventId++,2,e,s,t.length].concat(t))}break;case"string":void 0===e&&(e=t.length),this._pools[a].update([r,this._eventId++,3,e,s,t.length]),this._pools[a].update(t);break;default:c=1}if(c)throw new b.exception.bug("random: addEntropy only supports number, array of numbers or string");this._poolEntropy[a]+=e,this._poolStrength+=e,h===this._NOT_READY&&(this.isReady()!==this._NOT_READY&&this._fireEvent("seeded",Math.max(this._strength,this._poolStrength)),this._fireEvent("progress",this.getProgress()))},isReady:function(t){var e=this._PARANOIA_LEVELS[void 0!==t?t:this._defaultParanoia];return this._strength&&this._strength>=e?this._poolEntropy[0]>this._BITS_PER_RESEED&&(new Date).valueOf()>this._nextReseed?this._REQUIRES_RESEED|this._READY:this._READY:this._poolStrength>=e?this._REQUIRES_RESEED|this._NOT_READY:this._NOT_READY},getProgress:function(t){var e=this._PARANOIA_LEVELS[t||this._defaultParanoia];return this._strength>=e?1:this._poolStrength>e?1:this._poolStrength/e},startCollectors:function(){if(!this._collectorsStarted){if(window.addEventListener)window.addEventListener("load",this._loadTimeCollector,!1),window.addEventListener("mousemove",this._mouseCollector,!1);else{if(!document.attachEvent)throw new b.exception.bug("can't attach event");document.attachEvent("onload",this._loadTimeCollector),document.attachEvent("onmousemove",this._mouseCollector)}this._collectorsStarted=!0}},stopCollectors:function(){this._collectorsStarted&&(window.removeEventListener?(window.removeEventListener("load",this._loadTimeCollector,!1),window.removeEventListener("mousemove",this._mouseCollector,!1)):window.detachEvent&&(window.detachEvent("onload",this._loadTimeCollector),window.detachEvent("onmousemove",this._mouseCollector)),this._collectorsStarted=!1)},addEventListener:function(t,e){this._callbacks[t][this._callbackI++]=e},removeEventListener:function(t,e){var i,r,n=this._callbacks[t],o=[];for(r in n)n.hasOwnProperty(r)&&n[r]===e&&o.push(r);for(i=0;i<o.length;i++)delete n[r=o[i]]},_pools:[new b.hash.sha256],_poolEntropy:[0],_reseedCount:0,_robins:{},_eventId:0,_collectorIds:{},_collectorIdNext:0,_strength:0,_poolStrength:0,_nextReseed:0,_key:[0,0,0,0,0,0,0,0],_counter:[0,0,0,0],_cipher:void 0,_defaultParanoia:6,_collectorsStarted:!1,_callbacks:{progress:{},seeded:{}},_callbackI:0,_NOT_READY:0,_READY:1,_REQUIRES_RESEED:2,_MAX_WORDS_PER_BURST:65536,_PARANOIA_LEVELS:[0,48,64,96,128,192,256,384,512,768,1024],_MILLISECONDS_PER_RESEED:3e4,_BITS_PER_RESEED:80,_gen4words:function(){for(var t=0;t<4&&(this._counter[t]=this._counter[t]+1|0,!this._counter[t]);t++);return this._cipher.encrypt(this._counter)},_gate:function(){this._key=this._gen4words().concat(this._gen4words()),this._cipher=new b.cipher.aes(this._key)},_reseed:function(t){this._key=b.hash.sha256.hash(this._key.concat(t)),this._cipher=new b.cipher.aes(this._key);for(var e=0;e<4&&(this._counter[e]=this._counter[e]+1|0,!this._counter[e]);e++);},_reseedFromPools:function(t){var e,i=[],r=0;for(this._nextReseed=i[0]=(new Date).valueOf()+this._MILLISECONDS_PER_RESEED,e=0;e<16;e++)i.push(4294967296*Math.random()|0);for(e=0;e<this._pools.length&&(i=i.concat(this._pools[e].finalize()),r+=this._poolEntropy[e],this._poolEntropy[e]=0,t||!(this._reseedCount&1<<e));e++);this._reseedCount>=1<<this._pools.length&&(this._pools.push(new b.hash.sha256),this._poolEntropy.push(0)),this._poolStrength-=r,r>this._strength&&(this._strength=r),this._reseedCount++,this._reseed(i)},_mouseCollector:function(t){var e=t.x||t.clientX||t.offsetX||0,i=t.y||t.clientY||t.offsetY||0;b.random.addEntropy([e,i],2,"mouse")},_loadTimeCollector:function(t){b.random.addEntropy((new Date).valueOf(),2,"loadtime")},_fireEvent:function(t,e){var i,r=b.random._callbacks[t],n=[];for(i in r)r.hasOwnProperty(i)&&n.push(r[i]);for(i=0;i<n.length;i++)n[i](e)}},function(){try{var t=new Uint32Array(32);crypto.getRandomValues(t),b.random.addEntropy(t,1024,"crypto.getRandomValues")}catch(t){}}(),function(){for(var t in b.beware)b.beware.hasOwnProperty(t)&&b.beware[t]()}(),t.prototype.init=function(t){var e,i,r;for(e=0;e<256;++e)this.S[e]=e;for(e=i=0;e<256;++e)i=i+this.S[e]+t[e%t.length]&255,r=this.S[e],this.S[e]=this.S[i],this.S[i]=r;this.i=0,this.j=0},t.prototype.next=function(){var t;return this.i=this.i+1&255,this.j=this.j+this.S[this.i]&255,t=this.S[this.i],this.S[this.i]=this.S[this.j],this.S[this.j]=t,this.S[t+this.S[this.i]&255]};var e,i,r,n,o=256;function s(){var t;t=(new Date).getTime(),i[r++]^=255&t,i[r++]^=t>>8&255,i[r++]^=t>>16&255,i[r++]^=t>>24&255,o<=r&&(r-=o)}if(null==i){var a;if(i=new Array,r=0,window.crypto&&window.crypto.getRandomValues){var h=new Uint8Array(32);for(window.crypto.getRandomValues(h),a=0;a<32;++a)i[r++]=h[a]}if("Netscape"==navigator.appName&&navigator.appVersion<"5"&&window.crypto){var c=window.crypto.random(32);for(a=0;a<c.length;++a)i[r++]=255&c.charCodeAt(a)}for(;r<o;)a=Math.floor(65536*Math.random()),i[r++]=a>>>8,i[r++]=255&a;r=0,s()}function l(){if(null==e){for(s(),(e=new t).init(i),r=0;r<i.length;++r)i[r]=0;r=0}return e.next()}function u(){}u.prototype.nextBytes=function(t){var e;for(e=0;e<t.length;++e)t[e]=l()};function _(t,e,i){null!=t&&("number"==typeof t?this.fromNumber(t,e,i):null==e&&"string"!=typeof t?this.fromString(t,256):this.fromString(t,e))}function E(){return new _(null)}"Microsoft Internet Explorer"==navigator.appName?(_.prototype.am=function(t,e,i,r,n,o){for(var s=32767&e,a=e>>15;0<=--o;){var h=32767&this[t],c=this[t++]>>15,l=a*h+c*s;n=((h=s*h+((32767&l)<<15)+i[r]+(1073741823&n))>>>30)+(l>>>15)+a*c+(n>>>30),i[r++]=1073741823&h}return n},n=30):"Netscape"!=navigator.appName?(_.prototype.am=function(t,e,i,r,n,o){for(;0<=--o;){var s=e*this[t++]+i[r]+n;n=Math.floor(s/67108864),i[r++]=67108863&s}return n},n=26):(_.prototype.am=function(t,e,i,r,n,o){for(var s=16383&e,a=e>>14;0<=--o;){var h=16383&this[t],c=this[t++]>>14,l=a*h+c*s;n=((h=s*h+((16383&l)<<14)+i[r]+n)>>28)+(l>>14)+a*c,i[r++]=268435455&h}return n},n=28),_.prototype.DB=n,_.prototype.DM=(1<<n)-1,_.prototype.DV=1<<n;_.prototype.FV=Math.pow(2,52),_.prototype.F1=52-n,_.prototype.F2=2*n-52;var p,f,d="0123456789abcdefghijklmnopqrstuvwxyz",y=new Array;for(p="0".charCodeAt(0),f=0;f<=9;++f)y[p++]=f;for(p="a".charCodeAt(0),f=10;f<36;++f)y[p++]=f;for(p="A".charCodeAt(0),f=10;f<36;++f)y[p++]=f;function m(t){return d.charAt(t)}function g(t){var e=E();return e.fromInt(t),e}function S(t){var e,i=1;return 0!=(e=t>>>16)&&(t=e,i+=16),0!=(e=t>>8)&&(t=e,i+=8),0!=(e=t>>4)&&(t=e,i+=4),0!=(e=t>>2)&&(t=e,i+=2),0!=(e=t>>1)&&(t=e,i+=1),i}function v(t){this.m=t}function w(t){this.m=t,this.mp=t.invDigit(),this.mpl=32767&this.mp,this.mph=this.mp>>15,this.um=(1<<t.DB-15)-1,this.mt2=2*t.t}v.prototype.convert=function(t){return t.s<0||0<=t.compareTo(this.m)?t.mod(this.m):t},v.prototype.revert=function(t){return t},v.prototype.reduce=function(t){t.divRemTo(this.m,null,t)},v.prototype.mulTo=function(t,e,i){t.multiplyTo(e,i),this.reduce(i)},v.prototype.sqrTo=function(t,e){t.squareTo(e),this.reduce(e)},w.prototype.convert=function(t){var e=E();return t.abs().dlShiftTo(this.m.t,e),e.divRemTo(this.m,null,e),t.s<0&&0<e.compareTo(_.ZERO)&&this.m.subTo(e,e),e},w.prototype.revert=function(t){var e=E();return t.copyTo(e),this.reduce(e),e},w.prototype.reduce=function(t){for(;t.t<=this.mt2;)t[t.t++]=0;for(var e=0;e<this.m.t;++e){var i=32767&t[e],r=i*this.mpl+((i*this.mph+(t[e]>>15)*this.mpl&this.um)<<15)&t.DM;for(t[i=e+this.m.t]+=this.m.am(0,r,t,e,0,this.m.t);t[i]>=t.DV;)t[i]-=t.DV,t[++i]++}t.clamp(),t.drShiftTo(this.m.t,t),0<=t.compareTo(this.m)&&t.subTo(this.m,t)},w.prototype.mulTo=function(t,e,i){t.multiplyTo(e,i),this.reduce(i)},w.prototype.sqrTo=function(t,e){t.squareTo(e),this.reduce(e)},_.prototype.copyTo=function(t){for(var e=this.t-1;0<=e;--e)t[e]=this[e];t.t=this.t,t.s=this.s},_.prototype.fromInt=function(t){this.t=1,this.s=t<0?-1:0,0<t?this[0]=t:t<-1?this[0]=t+this.DV:this.t=0},_.prototype.fromString=function(t,e){var i;if(16==e)i=4;else if(8==e)i=3;else if(256==e)i=8;else if(2==e)i=1;else if(32==e)i=5;else{if(4!=e)return void this.fromRadix(t,e);i=2}this.t=0,this.s=0;for(var r,n,o=t.length,s=!1,a=0;0<=--o;){var h=8==i?255&t[o]:(r=o,null==(n=y[t.charCodeAt(r)])?-1:n);h<0?"-"==t.charAt(o)&&(s=!0):(s=!1,0==a?this[this.t++]=h:a+i>this.DB?(this[this.t-1]|=(h&(1<<this.DB-a)-1)<<a,this[this.t++]=h>>this.DB-a):this[this.t-1]|=h<<a,(a+=i)>=this.DB&&(a-=this.DB))}8==i&&0!=(128&t[0])&&(this.s=-1,0<a&&(this[this.t-1]|=(1<<this.DB-a)-1<<a)),this.clamp(),s&&_.ZERO.subTo(this,this)},_.prototype.clamp=function(){for(var t=this.s&this.DM;0<this.t&&this[this.t-1]==t;)--this.t},_.prototype.dlShiftTo=function(t,e){var i;for(i=this.t-1;0<=i;--i)e[i+t]=this[i];for(i=t-1;0<=i;--i)e[i]=0;e.t=this.t+t,e.s=this.s},_.prototype.drShiftTo=function(t,e){for(var i=t;i<this.t;++i)e[i-t]=this[i];e.t=Math.max(this.t-t,0),e.s=this.s},_.prototype.lShiftTo=function(t,e){var i,r=t%this.DB,n=this.DB-r,o=(1<<n)-1,s=Math.floor(t/this.DB),a=this.s<<r&this.DM;for(i=this.t-1;0<=i;--i)e[i+s+1]=this[i]>>n|a,a=(this[i]&o)<<r;for(i=s-1;0<=i;--i)e[i]=0;e[s]=a,e.t=this.t+s+1,e.s=this.s,e.clamp()},_.prototype.rShiftTo=function(t,e){e.s=this.s;var i=Math.floor(t/this.DB);if(i>=this.t)e.t=0;else{var r=t%this.DB,n=this.DB-r,o=(1<<r)-1;e[0]=this[i]>>r;for(var s=i+1;s<this.t;++s)e[s-i-1]|=(this[s]&o)<<n,e[s-i]=this[s]>>r;0<r&&(e[this.t-i-1]|=(this.s&o)<<n),e.t=this.t-i,e.clamp()}},_.prototype.subTo=function(t,e){for(var i=0,r=0,n=Math.min(t.t,this.t);i<n;)r+=this[i]-t[i],e[i++]=r&this.DM,r>>=this.DB;if(t.t<this.t){for(r-=t.s;i<this.t;)r+=this[i],e[i++]=r&this.DM,r>>=this.DB;r+=this.s}else{for(r+=this.s;i<t.t;)r-=t[i],e[i++]=r&this.DM,r>>=this.DB;r-=t.s}e.s=r<0?-1:0,r<-1?e[i++]=this.DV+r:0<r&&(e[i++]=r),e.t=i,e.clamp()},_.prototype.multiplyTo=function(t,e){var i=this.abs(),r=t.abs(),n=i.t;for(e.t=n+r.t;0<=--n;)e[n]=0;for(n=0;n<r.t;++n)e[n+i.t]=i.am(0,r[n],e,n,0,i.t);e.s=0,e.clamp(),this.s!=t.s&&_.ZERO.subTo(e,e)},_.prototype.squareTo=function(t){for(var e=this.abs(),i=t.t=2*e.t;0<=--i;)t[i]=0;for(i=0;i<e.t-1;++i){var r=e.am(i,e[i],t,2*i,0,1);(t[i+e.t]+=e.am(i+1,2*e[i],t,2*i+1,r,e.t-i-1))>=e.DV&&(t[i+e.t]-=e.DV,t[i+e.t+1]=1)}0<t.t&&(t[t.t-1]+=e.am(i,e[i],t,2*i,0,1)),t.s=0,t.clamp()},_.prototype.divRemTo=function(t,e,i){var r=t.abs();if(!(r.t<=0)){var n=this.abs();if(n.t<r.t)return null!=e&&e.fromInt(0),void(null!=i&&this.copyTo(i));null==i&&(i=E());var o=E(),s=this.s,a=t.s,h=this.DB-S(r[r.t-1]);0<h?(r.lShiftTo(h,o),n.lShiftTo(h,i)):(r.copyTo(o),n.copyTo(i));var c=o.t,l=o[c-1];if(0!=l){var u=l*(1<<this.F1)+(1<c?o[c-2]>>this.F2:0),p=this.FV/u,f=(1<<this.F1)/u,d=1<<this.F2,y=i.t,m=y-c,g=null==e?E():e;for(o.dlShiftTo(m,g),0<=i.compareTo(g)&&(i[i.t++]=1,i.subTo(g,i)),_.ONE.dlShiftTo(c,g),g.subTo(o,o);o.t<c;)o[o.t++]=0;for(;0<=--m;){var v=i[--y]==l?this.DM:Math.floor(i[y]*p+(i[y-1]+d)*f);if((i[y]+=o.am(0,v,i,m,0,c))<v)for(o.dlShiftTo(m,g),i.subTo(g,i);i[y]<--v;)i.subTo(g,i)}null!=e&&(i.drShiftTo(c,e),s!=a&&_.ZERO.subTo(e,e)),i.t=c,i.clamp(),0<h&&i.rShiftTo(h,i),s<0&&_.ZERO.subTo(i,i)}}},_.prototype.invDigit=function(){if(this.t<1)return 0;var t=this[0];if(0==(1&t))return 0;var e=3&t;return 0<(e=(e=(e=(e=e*(2-(15&t)*e)&15)*(2-(255&t)*e)&255)*(2-((65535&t)*e&65535))&65535)*(2-t*e%this.DV)%this.DV)?this.DV-e:-e},_.prototype.isEven=function(){return 0==(0<this.t?1&this[0]:this.s)},_.prototype.exp=function(t,e){if(4294967295<t||t<1)return _.ONE;var i=E(),r=E(),n=e.convert(this),o=S(t)-1;for(n.copyTo(i);0<=--o;)if(e.sqrTo(i,r),0<(t&1<<o))e.mulTo(r,n,i);else{var s=i;i=r,r=s}return e.revert(i)},_.prototype.toString=function(t){if(this.s<0)return"-"+this.negate().toString(t);var e;if(16==t)e=4;else if(8==t)e=3;else if(2==t)e=1;else if(32==t)e=5;else{if(4!=t)return this.toRadix(t);e=2}var i,r=(1<<e)-1,n=!1,o="",s=this.t,a=this.DB-s*this.DB%e;if(0<s--)for(a<this.DB&&0<(i=this[s]>>a)&&(n=!0,o=m(i));0<=s;)a<e?(i=(this[s]&(1<<a)-1)<<e-a,i|=this[--s]>>(a+=this.DB-e)):(i=this[s]>>(a-=e)&r,a<=0&&(a+=this.DB,--s)),0<i&&(n=!0),n&&(o+=m(i));return n?o:"0"},_.prototype.negate=function(){var t=E();return _.ZERO.subTo(this,t),t},_.prototype.abs=function(){return this.s<0?this.negate():this},_.prototype.compareTo=function(t){var e=this.s-t.s;if(0!=e)return e;var i=this.t;if(0!=(e=i-t.t))return this.s<0?-e:e;for(;0<=--i;)if(0!=(e=this[i]-t[i]))return e;return 0},_.prototype.bitLength=function(){return this.t<=0?0:this.DB*(this.t-1)+S(this[this.t-1]^this.s&this.DM)},_.prototype.mod=function(t){var e=E();return this.abs().divRemTo(t,null,e),this.s<0&&0<e.compareTo(_.ZERO)&&t.subTo(e,e),e},_.prototype.modPowInt=function(t,e){var i;return i=t<256||e.isEven()?new v(e):new w(e),this.exp(t,i)},_.ZERO=g(0),_.ONE=g(1);var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",D="=";function T(t){var e,i,r="";for(e=0;e+3<=t.length;e+=3)i=parseInt(t.substring(e,e+3),16),r+=A.charAt(i>>6)+A.charAt(63&i);for(e+1==t.length?(i=parseInt(t.substring(e,e+1),16),r+=A.charAt(i<<2)):e+2==t.length&&(i=parseInt(t.substring(e,e+2),16),r+=A.charAt(i>>2)+A.charAt((3&i)<<4));0<(3&r.length);)r+=D;return r}function C(){this.n=null,this.e=0,this.d=null,this.p=null,this.q=null,this.dmp1=null,this.dmq1=null,this.coeff=null}function R(d){d=d;this.getBlueSnap=function(){return d},this.encrypt=function(t){if(!t)return"";var e,i,r,n,o,s=(e=d.getPublicKey(),i=e.split("$"),r=i[0],n=i[1],(o=new C).setPublic(n,r),o),a={key:b.random.randomWords(8,0),encrypt:function(t){return this.encryptWithIv(t,b.random.randomWords(4,0))},encryptWithIv:function(t,e){var i,r,n,o;return i=new b.cipher.aes(this.key),o=b.codec.utf8String.toBits(t),n=b.mode.cbc.encrypt(i,o,e),r=b.bitArray.concat(e,n),b.codec.base64.fromBits(r)}},h=a.encrypt(t),c={key:b.random.randomWords(8,0),sign:function(t){var e;return e=new b.misc.hmac(this.key,b.hash.sha256).encrypt(t),b.codec.base64.fromBits(e)}},l=c.sign(b.codec.base64.toBits(h)),u=b.bitArray.concat(a.key,c.key),p=b.codec.base64.fromBits(u),f=s.encrypt_b64(p);return["$bsjs_"+d.getVersion().replace(/\./g,"_"),f,h,l].join("$")}}function I(h){h=h;var c=[],l=function(t){for(var e=[],i=t.children,r=0;r<i.length;r++){var n=i[r];1===n.nodeType&&n.attributes["data-bluesnap"]?e.push(n):null!=n.children&&0<n.children.length&&(e=e.concat(l(n)))}return e};this.extractForm=function(t){return window.jQuery&&t instanceof jQuery?t[0]:t.nodeType&&1===t.nodeType?t:document.getElementById(t)},this.encryptForm=function(r){for(var t=l(r);0<c.length;){try{r.removeChild(c[0])}catch(t){}c.splice(0,1)}function e(t,e){var i=function(t,e){var i=document.createElement(t);for(var r in e)if(e.hasOwnProperty(r)){var n=e[r];i.setAttribute(r,n)}return i}("input",{value:t,type:"hidden",name:e});c.push(i),n.push(r.appendChild(i))}for(var n=[],i=0;i<t.length;i++){var o=t[i],s=o.getAttribute("data-bluesnap");if(7<o.value.length)e(o.value.substr(o.value.length-4),"ccLast4Digits");var a=h.encrypt(o.value);o.removeAttribute("name"),e(a,s)}return n}}C.prototype.doPublic=function(t){return t.modPowInt(this.e,this.n)},C.prototype.setPublic=function(t,e){null!=t&&null!=e&&0<t.length&&0<e.length?(this.n=new _(t,16),this.e=parseInt(e,16)):alert("Invalid RSA public key")},C.prototype.encrypt=function(t){var e=function(t,e){if(e<t.length+11)return alert("Message too long for RSA"),null;for(var i=new Array,r=t.length-1;0<=r&&0<e;){var n=t.charCodeAt(r--);n<128?i[--e]=n:127<n&&n<2048?(i[--e]=63&n|128,i[--e]=n>>6|192):(i[--e]=63&n|128,i[--e]=n>>6&63|128,i[--e]=n>>12|224)}i[--e]=0;for(var o=new u,s=new Array;2<e;){for(s[0]=0;0==s[0];)o.nextBytes(s);i[--e]=s[0]}return i[--e]=2,i[--e]=0,new _(i)}(t,this.n.bitLength()+7>>3);if(null==e)return null;var i=this.doPublic(e);if(null==i)return null;var r=i.toString(16);return 0==(1&r.length)?r:"0"+r},C.prototype.encrypt_b64=function(t){var e;return(e=this.encrypt(t))?T(e):null};var N={},x=!(C.prototype.encrypt_b64=function(t){var e;return(e=this.encrypt(t))?T(e):null});function B(t,e){return{errorCode:t,errorDescription:e}}var k={"3DS_AUTHENTICATION_FAILED":B("14101","3D Secure authentication was failed because the shopper did not enter correct credentials"),"3DS_MISSING_REQUIRED_FIELDS":B("14102","3D Secure object is missing required fields")};var L=function(){var n=new O,h=new O;function o(t){!0===x&&console.log("bluesnap.3ds: "+t)}Cardinal.on("payments.setupComplete",function(){o("payments.setupComplete callback was called");var t=N.submitButton;null!=t&&(o("Enabling the submitButton"),t.disabled=!1),n.getCallback()()}),Cardinal.on("payments.validated",function(t,e){var i;switch(o("payments.validated callback was called"),o(JSON.stringify(t)),t.ActionCode){case"SUCCESS":i="AUTHENTICATION_SUCCEEDED";break;case"NOACTION":i="AUTHENTICATION_UNAVAILABLE";try{"B"===t.Payment.ExtendedData.Enrolled&&(i="AUTHENTICATION_BYPASSED")}catch(t){o("Failed to parse data object. Error: "+t)}break;case"FAILURE":return i="AUTHENTICATION_FAILURE",void h.setError(k["3DS_AUTHENTICATION_FAILED"]);case"ERROR":10001!==t.ErrorNumber&&10002!==t.ErrorNumber&&10003!==t.ErrorNumber||o("Cardinal Cruise is unable to contact the Cardinal servers"),i="AUTHENTICATION_UNAVAILABLE"}if("AUTHENTICATION_FAILURE"!==i){var r={},n={};n.authResult=i,n.threeDSecureResultToken=e,r.threeDSecure=n,h.setResult(r)}}),L.prototype.init=function(t,e){o("threeDSecure.init() was called"),n.setCallback(e);var i=N.submitButton;null!=i&&(o("Disabling the submitButton until 3DS setup is completed"),i.disabled=!0);var r=!1===x?"off":"on";Cardinal.configure({logging:{level:r}}),o("Calling Cardinal.setup"),Cardinal.setup("init",{jwt:t})},L.prototype.process=function(t,e){if(o("threeDSecure.process() was called"),h.setCallback(e),null==t){o("threeDSecureObj = "+t);var i=k["3DS_MISSING_REQUIRED_FIELDS"];i.errorDescription+=" { amount, currency }",h.setError(i)}var r=function(t){var e=null!=N.encryptedCreditCard?N.encryptedCreditCard.value:null;null!=e&&(e=e.replace(/\s/g,""));var i,r=null!=N.expirationMonth?N.expirationMonth.value:null,n=null!=N.expirationYear?N.expirationYear.value:null;if(!1===function(t,e,i,r){var n=r.amount,o=r.currency,s="";null==t&&(s+="creditCard,");null==e&&(s+="expirationMonth,");null==i&&(s+="expirationYear,");null==n&&(s+="amount,");null==o&&(s+="currency,");if(""!==s){s=s.substring(0,s.length-1);var a=k["3DS_MISSING_REQUIRED_FIELDS"];return a.errorDescription+=" { "+s+" }",h.setError(a),!1}return!0}(e,r,n,t))return;try{i={Consumer:{Account:{AccountNumber:e,ExpirationMonth:r,ExpirationYear:n}},OrderDetails:{Amount:t.amount,CurrencyCode:t.currency}},t.email&&(i.Consumer.Email1=t.email);var o,s={};t.billingFirstName&&(o.FirstName=t.billingFirstName),t.billingLastName&&(o.LastName=t.billingLastName),t.billingCountry&&(o.CountryCode=t.billingCountry),t.billingState&&(o.State=t.billingState),t.billingCity&&(o.City=t.billingCity),t.billingAddress&&(o.Address1=t.billingAddress),t.billingZip&&(o.Postalcode=t.billingZip),t.phone&&(o.Phone1=t.phone),t.shippingFirstName&&(s.FirstName=t.shippingFirstName),t.shippingLastName&&(s.LastName=t.shippingLastName),t.shippingCountry&&(s.CountryCode=t.shippingCountry),t.shippingState&&(s.State=t.shippingState),t.shippingCity&&(s.City=t.shippingCity),t.shippingAddress&&(s.Address1=t.shippingAddress),t.shippingZip&&(s.Postalcode=t.shippingZip),0===Object.keys(o).length&&o.constructor===Object||(i.Consumer.BillingAddress=o),0===Object.keys(s).length&&s.constructor===Object||(i.Consumer.ShippingAddress=s)}catch(t){}return i}(t);null!=r&&(o("Calling Cardinal.start"),Cardinal.start("cca",r))}},O=function(){var e=null;O.prototype.getCallback=function(){return e},O.prototype.setCallback=function(t){e=t},O.prototype.setResult=function(t){e(t,null)},O.prototype.setError=function(t){e(null,t)}};function M(t,e){this.isSandbox=null!=e&&e;for(var i=document.querySelectorAll("[data-bluesnap]"),r=0;r<i.length;r++){var n=i[r],o=n.getAttribute("data-bluesnap");N[o]=n}t=t;var s=new I(new R(this));M.prototype.encrypt=function(t,e,i){if("string"==typeof t){var r=s.extractForm(t);if(P("Calling encryptForm"),s.encryptForm(r),null!=this.threeDSecure){if("function"!=typeof i)return void console.log("callback is not a function!");this.threeDSecure.process(e,function(t,e){null==e&&(t.encryptedCardNumber=$("input[name='encryptedCreditCard']").val(),t.encryptedSecurityCode=$("input[name='encryptedCvv']").val(),t.ccLast4Digits=$("input[name='ccLast4Digits']").val(),t.expirationYear=N.expirationYear.value,t.expirationMonth=N.expirationMonth.value),i(t,e)})}}else console.log("Expecting formId to be a string")},this.getPublicKey=function(){return t},this.getVersion=function(){return"1.0.4"}}function P(t){!0===x&&console.log("bluesnap: "+t)}M.prototype.init3DS=function(e,i){if(null==e){var t=k["3DS_MISSING_REQUIRED_FIELDS"];return t.errorDescription+=" { threeDSecureToken }",void i(null,t)}var r="https://includes.ccdc02.com/cardinalcruise/v1/songbird.js";!0===this.isSandbox&&(r="https://includestest.ccdc02.com/cardinalcruise/v1/songbird.js"),function(e,t){try{var i=document.createElement("script");i.type="text/javascript",i.src=e,i.onreadystatechange=t,i.onload=t,document.head.appendChild(i)}catch(t){P("Failed to load: "+e+". Error: "+t)}}(r,function(){var t=new L;bluesnap.setThreeDSecure(t),t.init(e,function(){i(null,null)})})},M.prototype.setThreeDSecure=function(t){this.threeDSecure=t},b.random.startCollectors(),window.BlueSnap=M}();