-
Notifications
You must be signed in to change notification settings - Fork 6
/
faxrobot.min.js
3 lines (3 loc) · 65.3 KB
/
faxrobot.min.js
1
2
3
var STATUS_POLL_DELAY=2e3,SUPPORTED_FILETYPES=["doc","docx","pdf","txt","png","jpg"],PAYMENT_DEFAULT_AMOUNT=PAYMENT_DEFAULT_AMOUNT||3,ERR_ACCESS_DENIED=401,ERR_NO_FILE_ATTACHED=101,ERR_BAD_DESTINATION=102,ERRORS=[{code:0,title:"An error has occurred!",detail:"Don't worry. It probably wasn't your fault. Maybe. Please try to do what you were doing again, and let us know if the problem persists."},{code:100,title:"File not supported",detail:"Currently, the following file formats are supported: {SUPPORTED_FILETYPES}."},{code:101,title:"Please choose a file.",detail:"You can use any file of the following formats: {SUPPORTED_FILETYPES}."},{code:102,title:"Please enter a valid fax number.",detail:"Don't forget to include an area code :)"},{code:103,title:"Credit Card Error",detail:"There was a problem authorizing the credit card information you provided. Please check the info and try again."},{code:104,title:"Credit Card Error",detail:"We ran into a problem saving your credit card info. Whoops. Try again?"},{code:401,title:"Access denied :-)",detail:"You're not authorized to access whatever it was you were trying to access."},{code:513,title:"Insufficient credit.",detail:"Your account didn't have enough credit remaining to send the fax. Please add credit and try again."},{code:516,title:"No answer.",detail:"The number you dialed did not pick up or was not a fax machine. This transaction is still billed."},{code:517,title:"Line busy.",detail:"Please try again later."},{code:518,title:"File not supported",detail:"Currently, the following file formats are supported: {SUPPORTED_FILETYPES}."},{code:520,title:"Payment failed.",detail:"Your account ran out of funds and we were unable to authorized the stored credit card info. Please add credit and try again."},{code:527,title:"Invalid phone number.",detail:"The specified fax number was invalid or not formatted correctly."},{code:600,title:"Unable to provision your number.",detail:"An uncommon error occurred with our telecommunications provider. Sorry! Please contact {SUPPORT_MAILTO}."},{code:601,title:"Please try a different area code.",detail:"The area code you chose is invalid or has no numbers available at this time."},{code:602,title:"You already set up your receiving fax number!",detail:"Check it out here on your Account Settings page."},{code:603,title:"Well that didn't work...",detail:"You tried to delete your incoming fax number, but it looks like it was already removed from the account."},{code:604,title:"Unable to disable your fax number :(",detail:"Something went wrong with our telecommunications provider. Sorry! Please contact {SUPPORT_MAILTO}."},{code:5025,title:"Rate limit exceeded :(",detail:"To protect our service against automated abuse, we limit the rate at which you can send faxes. Please try again in a few minutes or contact {SUPPORT_MAILTO} if you need to send more frequently."}],routes={"/":["jobs","create"],"/jobs":["jobs","list"],"/jobs/received":["incoming","list"],"/jobs/sent/([a-z]+)/([0-9]+)":["jobs","list"],"/jobs/received/([0-9]+)":["incoming","list"],"/job/([a-z0-9]+)":["jobs","view"],"/receive":["incoming","provision"],"/accounts/login":["accounts","login"],"/accounts/logout":["accounts","logout"],"/accounts/register":["accounts","register"],"/reset/([a-z0-9]+)":["accounts","reset_password"],"/account":["accounts","manage"],"/transactions":["accounts","transactions"],"/transactions/([0-9]+)":["accounts","transactions"],"/faq":["pages","faq"],"/faq/([a-z0-9]+)":["pages","faq"],"/api":["pages","api"],"/api/([a-z0-9]+)":["pages","api"],"/privacy":["pages","privacy"],"/tos":["pages","tos"],".*":["pages","not_found"]},util={set_cookie:function(a,b,c){var d=new Date;d.setTime(d.getTime()+24*c*60*60*1e3);var e="expires="+d.toGMTString();document.cookie=a+"="+b+"; "+e+"; path=/"},get_cookie:function(a){for(var b=a+"=",c=document.cookie.split(";"),d=0;d<c.length;d++){var e=c[d].trim();if(0==e.indexOf(b))return e.substring(b.length,e.length)}return""},set_title:function(a){var b=function(a){return(a+"").replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")},c=new RegExp("(\\s*\\|\\s*"+b(faxrobot.base_window_title)+")*(\\s*\\|)?$","g");a=a.replace(c,""),a=""==a?faxrobot.base_window_title:a+" — "+faxrobot.base_window_title,document.title=a},format_phone:function(a){return a?(11==a.length&&"1"==a.charAt(0)&&(a=a.substr(1)),a.substr(0,3)+"-"+a.substr(3,3)+"-"+a.substr(6)):""},format_date:function(a){return a?(a=new Date(a),a.getMonth()+1+"/"+a.getDate()+"/"+(a.getFullYear()-2e3)+" "+(a.getHours()>12?a.getHours()-12:a.getHours())+":"+(a.getMinutes()>=10?a.getMinutes():"0"+a.getMinutes())+(a.getHours()>12?"pm":"am")):"-"},remove_class:function(a,b){a.className=a.className.replace(new RegExp(b,"g"),"")},notify:function(a){var b=document.createElement("div");b.className="notification closed",b.textContent=a;var c=!1,d=function(){c||(c=!0,b.className+=" closed",setTimeout(function(){document.body.removeChild(b)},500))};b.addEventListener("click",function(){d()}),document.body.appendChild(b),setTimeout(function(){this.remove_class(b,"closed")}.bind(this),50);setTimeout(function(){d()},5e3)}},convert={to_bytes:function(a){for(var b=[],c=0;c<a.length;c++)b.push(255&a.charCodeAt(c));return b},to_words:function(a,b){b||(b={}),b.reverse_endian||(b.reverse_endian=!1);var c=[],d=this.to_word;if("string"!=typeof a)if(b.reverse_endian)for(var e=0;e<a.length;e+=4)c.push(d(a[e+3],a[e+2],a[e+1],a[e]));else for(var e=0;e<a.length;e+=4)c.push(d(a[e],a[e+1],a[e+2],a[e+3]));else if(b.reverse_endian)for(var e=0;e<a.length;e+=4)c.push(d(a.charCodeAt(e+3),a.charCodeAt(e+2),a.charCodeAt(e+1),a.charCodeAt(e)));else for(var e=0;e<a.length;e+=4)c.push(d(a.charCodeAt(e),a.charCodeAt(e+1),a.charCodeAt(e+2),a.charCodeAt(e+3)));return c},to_word:function(){if(4==arguments.length)return(255&arguments[0])<<24|(255&arguments[1])<<16|(255&arguments[2])<<8|255&arguments[3];for(var a=0,b=arguments.length-1;b>=0;b--)a|=(255&arguments[b])<<8*(arguments.length-1-b);return a},words_to_binstring:function(a){for(var b="",c=this.word_to_binstring,d=0;d<a.length;d++)b+=c(a[d]);return b},words_to_hex:function(a){return this.binstring_to_hex(this.words_to_binstring(a))},words_to_bytes:function(a){for(var b=[],c=this.word_to_bytes,d=0;d<a.length;d++)b=b.concat(c(a[d]));return b},word_to_binstring:function(a){return String.fromCharCode(a>>>24&255)+String.fromCharCode(a>>>16&255)+String.fromCharCode(a>>>8&255)+String.fromCharCode(255&a)},word_to_bytes:function(a){return[a>>>24&255,a>>>16&255,a>>>8&255,255&a]},hex_to_binstring:function(a){a.length%2==1&&(a="0"+a);for(var b="",c=0;c<a.length;c+=2)b+=String.fromCharCode(parseInt(a.substr(c,2),16));return b},binstring_to_hex:function(a){for(var b="",c=0;c<a.length;c++)b+=(1==a.charCodeAt(c).toString(16).length?"0":"")+a.charCodeAt(c).toString(16);return b},base64:{chars:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",encode:function(a){var b="";for(i=0,c=a.length;i<c;i+=3){var d=a.charCodeAt(i)>>2,e=(3&a.charCodeAt(i))<<4|a.charCodeAt(i+1)>>4,f=(15&a.charCodeAt(i+1))<<2|a.charCodeAt(i+2)>>6,g=63&a.charCodeAt(i+2);b+=this.chars.charAt(d)+this.chars.charAt(e)+this.chars.charAt(f)+this.chars.charAt(g)}return c%3==1?b=b.substr(0,b.length-2)+"==":c%3==2&&(b=b.substr(0,b.length-1)+"="),b},decode:function(a){var b="";for(i=0,c=a.length;i<c;i+=4){var d=this.chars.indexOf(a.charAt(i)),e=this.chars.indexOf(a.charAt(i+1)),f=this.chars.indexOf(a.charAt(i+2)),g=this.chars.indexOf(a.charAt(i+3));b+=String.fromCharCode(d<<2|e>>4),f!=-1&&(b+=String.fromCharCode((15&e)<<4|f>>2)),g!=-1&&(b+=String.fromCharCode((3&f)<<6|g))}return b}},utf8:{encode:function(a){return unescape(encodeURIComponent(a))},decode:function(a){return decodeURIComponent(escape(a))},is_utf8_string:function(a){return/[^\u0000-\u00ff]/.test(a)}}},Hasher=Composer.Class({buffer:[],h:[],processed_length:0,reverse_endian_words:!1,clear:function(){this.buffer=[],this.processed_length=0},pad:function(){var a=this.processed_length+8*this.buffer.length;this.buffer.push(128);for(var b=0;(this.buffer.length+8)%64!=0;b++)this.buffer.push(0);for(var c=a.toString(2),b=0;c.length%64!=0;b++)c="0"+c;if(0==this.reverse_endian_words)var d=parseInt(c.substr(0,32),2),e=parseInt(c.substr(32),2);else var d=this._reverse_word(parseInt(c.substr(32),2)),e=this._reverse_word(parseInt(c.substr(0,32),2));this.buffer=this.buffer.concat(convert.word_to_bytes(d)),this.buffer=this.buffer.concat(convert.word_to_bytes(e))},finalize:function(a){a||(a={}),a.return_format||(a.return_format="hex"),this.pad(),this.hash([],{stream:!0}),1==this.reverse_endian_words&&(this.h=[this._reverse_word(this.h[0]),this._reverse_word(this.h[1]),this._reverse_word(this.h[2]),this._reverse_word(this.h[3])]);var b=this.h.slice(0);switch(this.clear(),a.return_format){case"hex":return convert.binstring_to_hex(convert.words_to_binstring(b));case"binary":return convert.words_to_binstring(b);case"words":return b}},get_block_size:function(){return this.block_size},get_outlen:function(){return this.outlen},_reverse_word:function(a){return convert.to_word(255&a,a>>>8&255,a>>>16&255,a>>>24&255)}}),MD5=Hasher.extend({Extends:Hasher,block_size:512,outlen:128,reverse_endian_words:!0,_t:[],initialize:function(){if(this.clear(),0==this._t.length)for(var a=0;a<64;a++)this._t[a]=Math.floor(4294967296*Math.abs(Math.sin(a+1)));return this},clear:function(){this.parent(),this.h=[1732584193,4023233417,2562383102,271733878]},hash:function(a,b){b||(b={}),b.stream||(b.stream=!1),"string"==typeof a&&(a=convert.to_bytes(a));for(var c=this.buffer.concat(a),d=this.processed_length,e=this.h,f=this._t,g=function(a,b,c,d,e,g,h){var i=a+(b&c|~b&d)+q[e]+f[h];return(i<<g|i>>>32-g)+b},h=function(a,b,c,d,e,g,h){var i=a+(b&d|c&~d)+q[e]+f[h];return(i<<g|i>>>32-g)+b},i=function(a,b,c,d,e,g,h){var i=a+(b^c^d)+q[e]+f[h];return(i<<g|i>>>32-g)+b},j=function(a,b,c,d,e,g,h){var i=a+(c^(b|~d))+q[e]+f[h];return(i<<g|i>>>32-g)+b},k=0;k+64<=c.length;k+=64){for(var l=e[0],m=e[1],n=e[2],o=e[3],p=c.slice(k,k+64),q=[],r=0;r<64;r+=4)q.push((255&p[r+3])<<24|(255&p[r+2])<<16|(255&p[r+1])<<8|255&p[r]);var l=g(l,m,n,o,0,7,0),o=g(o,l,m,n,1,12,1),n=g(n,o,l,m,2,17,2),m=g(m,n,o,l,3,22,3),l=g(l,m,n,o,4,7,4),o=g(o,l,m,n,5,12,5),n=g(n,o,l,m,6,17,6),m=g(m,n,o,l,7,22,7),l=g(l,m,n,o,8,7,8),o=g(o,l,m,n,9,12,9),n=g(n,o,l,m,10,17,10),m=g(m,n,o,l,11,22,11),l=g(l,m,n,o,12,7,12),o=g(o,l,m,n,13,12,13),n=g(n,o,l,m,14,17,14),m=g(m,n,o,l,15,22,15),l=h(l,m,n,o,1,5,16),o=h(o,l,m,n,6,9,17),n=h(n,o,l,m,11,14,18),m=h(m,n,o,l,0,20,19),l=h(l,m,n,o,5,5,20),o=h(o,l,m,n,10,9,21),n=h(n,o,l,m,15,14,22),m=h(m,n,o,l,4,20,23),l=h(l,m,n,o,9,5,24),o=h(o,l,m,n,14,9,25),n=h(n,o,l,m,3,14,26),m=h(m,n,o,l,8,20,27),l=h(l,m,n,o,13,5,28),o=h(o,l,m,n,2,9,29),n=h(n,o,l,m,7,14,30),m=h(m,n,o,l,12,20,31),l=i(l,m,n,o,5,4,32),o=i(o,l,m,n,8,11,33),n=i(n,o,l,m,11,16,34),m=i(m,n,o,l,14,23,35),l=i(l,m,n,o,1,4,36),o=i(o,l,m,n,4,11,37),n=i(n,o,l,m,7,16,38),m=i(m,n,o,l,10,23,39),l=i(l,m,n,o,13,4,40),o=i(o,l,m,n,0,11,41),n=i(n,o,l,m,3,16,42),m=i(m,n,o,l,6,23,43),l=i(l,m,n,o,9,4,44),o=i(o,l,m,n,12,11,45),n=i(n,o,l,m,15,16,46),m=i(m,n,o,l,2,23,47),l=j(l,m,n,o,0,6,48),o=j(o,l,m,n,7,10,49),n=j(n,o,l,m,14,15,50),m=j(m,n,o,l,5,21,51),l=j(l,m,n,o,12,6,52),o=j(o,l,m,n,3,10,53),n=j(n,o,l,m,10,15,54),m=j(m,n,o,l,1,21,55),l=j(l,m,n,o,8,6,56),o=j(o,l,m,n,15,10,57),n=j(n,o,l,m,6,15,58),m=j(m,n,o,l,13,21,59),l=j(l,m,n,o,4,6,60),o=j(o,l,m,n,11,10,61),n=j(n,o,l,m,2,15,62),m=j(m,n,o,l,9,21,63);e[0]=e[0]+l|0,e[1]=e[1]+m|0,e[2]=e[2]+n|0,e[3]=e[3]+o|0,d+=512}return this.buffer=c.slice(k),this.processed_length=d,0==b.stream?this.finalize(b):this}}),faxrobot={base_window_title:PROJECT_NAME,main_container_selector:"#main",last_url:"",scroll_to_top:!0,router:null,controllers:{},account:!1,_tmp:{},init:function(){this.account=new Account,this.util=util,this.load_controller("pages",PagesController),this.load_controller("header",HeaderController);var a=new Composer.Router(routes);a.bind_links({exclude_class:"external"}),a.bind("fail",function(a){console.log("route: fail: ",a)}),a.bind("statechange",function(a){this.controllers.header.state_change()}.bind(this)),this.router=a,document.getElementById("contact_link").href="mailto:"+EMAIL_CONTACT,this.login_from_cookie()},load_controller:function(a,b,c,d){return d||(d={}),this.controllers[a]?this.controllers[a]:(this.controllers[a]=new b(c,d),this.controllers[a])},render_template:function(a,b){var c=_.template(document.getElementById(a).innerHTML);return c(b)},route:function(a,b){this.router.route(a,b)},access_denied:function(){this.route("/"),this.error.show(ERR_ACCESS_DENIED)},error:{api:function(a,b){if("string"==typeof b)var c=JSON.parse(b);else var c=b;c?this.show(c.code):(console.error("Undecipherable error: ",a,b),this.show(0))},show:function(a){new ErrorModalController({code:a})}},login_from_cookie:function(){var a=faxrobot.util.get_cookie("API_KEY");if(a){var b=new XMLHttpRequest;b.onreadystatechange=function(){4===b.readyState&&200==b.status&&faxrobot.account.login(JSON.parse(b.response))}.bind(this),b.open("get",API_URL+"/accounts/get?api_key="+a,!0),b.send()}}},init=function(){faxrobot.init()},state=document.readyState;"complete"==state||"loaded"==state||"interactive"==state?init():document.addEventListener&&document.addEventListener("DOMContentLoaded",init,!1);var BaseModalController=Composer.Controller.extend({elements:{".overlay":"overlay",".modal_container":"modal_container",".modal":"modal"},events:{"click .modal_container":"close_if_closable","click a.close":"close"},show:function(){if(this.closable){var a=document.createElement("a");a.className="close",a.textContent="×",a.href="#",this.modal.appendChild(a)}this.modal.className+=" invisible",setTimeout(function(){this.overlay.className+=" visible",this.modal.className=this.modal.className.replace(/invisible/g,""),"function"==typeof this.after_show&&this.after_show()}.bind(this),15)},close_if_closable:function(a){a.target==this.modal_container&&this.closable&&(a.preventDefault(),this.close())},close:function(a){a&&a.preventDefault(),this.overlay.className=this.overlay.className.replace(/visible/g,""),this.modal.className+=" invisible",setTimeout(function(){"function"==typeof this.before_close&&this.before_close(),this.release()}.bind(this),250)}}),BlockingModalController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,template:"blocking_modal",text:"Please wait...",elements:{},events:{},init:function(){return this.render(),this},render:function(){var a=faxrobot.render_template(this.template,{text:this.text});return this.html(a),this.show(),this}}),AccountsAPIKeyController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,template:"accounts_api_key",model:null,closable:!0,events:{"click a.reset":"reset"},init:function(){return this.model=faxrobot.account,this.render(),this},render:function(){var a=this.model.toJSON(),b=faxrobot.render_template(this.template,a);return this.html(b),this.show(),this},reset:function(a){a.preventDefault(),setTimeout(function(){new AccountsAPIKeyResetController},250),this.close()}}),AccountsAPIKeyResetController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,template:"accounts_api_key_reset",model:null,closable:!0,elements:{input:"password",".error":"error",button:"button"},events:{"submit form":"submit"},init:function(){return this.model=faxrobot.account,this.render(),this},render:function(){var a=this.model.toJSON(),b=faxrobot.render_template(this.template,a);return this.html(b),this.show(),this},submit:function(a){a.preventDefault(),util.remove_class(this.password.parentNode,"error"),this.error.textContent="",this.button.disabled=!0;var b=new FormData;b.append("api_key",faxrobot.account.get("api_key")),b.append("password",this.password.value);var c=new XMLHttpRequest;c.onreadystatechange=function(){if(4===c.readyState){this.button.disabled=!1;var a=JSON.parse(c.response);200==c.status?(this.model.set(a),this.model.set_cookie(),util.notify("API key changed!"),setTimeout(function(){new AccountsAPIKeyController},250),this.close()):a&&6004==a.code?(this.password.parentNode.className+=" error",this.error.textContent="Please double check that password."):faxrobot.error.api(c.status,c.response)}}.bind(this),c.open("post",API_URL+"/accounts/reset_api_key",!0),c.send(b)}}),AccountsChangePasswordController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,template:"accounts_change_password",model:null,closable:!0,reset_hash:null,reset_title:"Reset Password",elements:{h2:"heading","#old_password":"old_password","#new_password_1":"new_password_1","#new_password_2":"new_password_2","strong.error":"error",button:"button"},events:{"submit form":"submit"},init:function(){return this.model=faxrobot.account,this.render(),this},render:function(){var a=this.model.toJSON(),b=faxrobot.render_template(this.template,a);this.html(b);var c=this.model.get("temporary_password");return(c||this.reset_hash)&&(this.old_password.value=c,this.old_password.parentNode.style.display="none"),this.reset_hash&&(this.heading.textContent=this.reset_title),this.show(),this},before_close:function(){0==faxrobot.controllers.pages.cur_controller&&faxrobot.route("/")},submit:function(a){if(a.preventDefault(),util.remove_class(this.old_password.parentNode,"error"),util.remove_class(this.new_password_1.parentNode,"error"),util.remove_class(this.new_password_2.parentNode,"error"),this.error.textContent="",!this.new_password_1.value)return this.new_password_1.parentNode.className+=" error",this.error.textContent="Please enter a new password.",!1;if(this.new_password_1.value!=this.new_password_2.value)return this.new_password_1.parentNode.className+=" error",this.new_password_2.parentNode.className+=" error",this.error.textContent="Please check that you typed your new password in properly.",!1;this.button.disabled=!0;var b=new FormData;this.reset_hash?b.append("reset_hash",this.reset_hash):(b.append("api_key",faxrobot.account.get("api_key")),b.append("old_password",this.old_password.value)),b.append("password",this.new_password_1.value);var c=new XMLHttpRequest;c.onreadystatechange=function(){if(4===c.readyState){this.button.disabled=!1;var a=JSON.parse(c.response);200==c.status?(this.model.get("temporary_password")&&(this.model.unset("temporary_password",{silent:!0}),this.model.set({changed_tmp_pwd:!0},{silent:!0})),this.reset_hash?(this.model.login(a),util.notify("Welcome, "+this.model.name()+"!")):(this.model.set(a),util.notify("Password changed!")),this.close()):a&&6016==a.code?(this.old_password.parentNode.className+=" error",this.error.textContent=a.msg):a&&6017==a.code?this.error.textContent="Password reset link has expired.":faxrobot.error.api(c.status,c.response)}}.bind(this),this.reset_hash?c.open("post",API_URL+"/accounts/reset_password",!0):c.open("post",API_URL+"/accounts/update",!0),c.send(b)}}),AccountsForgotPasswordController=BaseModalController.extend({inject:faxrobot.main_container_selector,closable:!0,el:!1,code:0,prefill_email:null,template:"accounts_forgot_password",default_button_text:"Reset Password",elements:{"#forgot_email":"email",button:"button"},events:{"submit form":"email_password_reset"},init:function(){return this.render(),this},render:function(){var a={email:this.prefill_email},b=faxrobot.render_template(this.template,a);return this.html(b),this.button.textContent=this.default_button_text,this.show(),this},after_show:function(){this.email.focus()},email_password_reset:function(a){a.preventDefault(),this.button.disabled=!0,this.button.textContent="wait...";var b=new FormData;b.append("email",this.email.value);var c=new XMLHttpRequest;c.onreadystatechange=function(){4===c.readyState&&(faxrobot.controllers.pages.track_subcontroller("check_email_modal",function(){return new BlockingModalController({text:"Please check your email!"})}),setTimeout(function(){var a=this.get_subcontroller("check_email_modal");a&&a.close()}.bind(faxrobot.controllers.pages),5e3),this.close())}.bind(this),c.open("post",API_URL+"/accounts/email_password_reset",!0),c.send(b)}}),AccountsLoginController=BaseModalController.extend({inject:faxrobot.main_container_selector,closable:!0,el:!1,code:0,template:"accounts_login",default_button_text:"Login",elements:{".center_row.email":"email_row",".center_row.password":"password_row","#login_email":"email","#login_password":"password",button:"button",h2:"title"},events:{"submit form":"login","click .forgot":"forgot_password"},init:function(){return this.render(),this},render:function(){var a={},b=faxrobot.render_template(this.template,a);return this.html(b),this.button.textContent=this.default_button_text,this.show(),this},after_show:function(){this.email.focus()},before_close:function(){0==faxrobot.controllers.pages.cur_controller&&faxrobot.route("/")},login:function(a){a.preventDefault(),this.button.disabled=!0,this.button.textContent="wait...";var b=new FormData;b.append("email",this.email.value),b.append("password",this.password.value);var c=new XMLHttpRequest;c.onreadystatechange=function(){if(4===c.readyState)if(200==c.status){var a=JSON.parse(c.response);faxrobot.account.login(a),util.notify("Welcome, "+faxrobot.account.name()+"!"),this.close()}else console.log("FAIL",c.status,c.response),this.email_row.className+=" error",this.password_row.className+=" error",this.button.disabled=!1,this.title.textContent="Login failed! Try again!",this.button.textContent=this.default_button_text}.bind(this),c.open("post",API_URL+"/accounts/login",!0),c.send(b)},forgot_password:function(a){a.preventDefault(),setTimeout(function(){new AccountsForgotPasswordController({prefill_email:this.email.value})}.bind(this),250),this.close()}}),AccountsManageController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,template:"accounts_manage",model:null,account_fields:["first_name","last_name","email","address","address2","city","state","zip"],elements:{"#auto_recharge":"auto_recharge",".edit_form":"form",".cancel_edit":"cancel",".edit_account":"edit",".account_info button":"button","#first_name":"first_name","#last_name":"last_name","#email":"email","#address":"address","#address2":"address2","#city":"city","#state":"state","#zip":"zip","strong.error":"error_info"},events:{"click a.login":"login","click a.cancel_edit":"cancel_edit","click a.remove_card":"remove_card","click a.payment_info":"edit_payment_info","click a.one_off":"one_off_payment","click a.edit_account":"edit_account","click a.change_password":"change_password","click a.api_key":"view_api_key","click a.disable_incoming":"disable_incoming","change #auto_recharge":"change_recharge","change input.email_pref":"change_email_pref","submit form.edit_form":"save_account"},init:function(){return util.set_title("My account"),this.model=faxrobot.account,this.with_bind(faxrobot.account,"change",this.render.bind(this)),this.render(),document.body.style.backgroundColor="#337",this},login:function(a){a.preventDefault(),new AccountsLoginController},render:function(){var a=this.model.toJSON();a.logged_in=faxrobot.account.is_logged_in();var b=faxrobot.render_template(this.template,a);return this.html(b),this},change_recharge:function(){var a=this.auto_recharge.checked?1:0;a&&!faxrobot.account.get("has_stripe_token")&&this.edit_payment_info(),this.model.set({auto_recharge:a}),this.save_fields(["auto_recharge"])},edit_payment_info:function(a){a&&a.preventDefault(),this.track_subcontroller("payment_info",function(){var a=new AccountsPaymentInfoController({model:this.model});return this.with_bind_once(a,"success",function(){this.save_fields(["last4","stripe_token"])}.bind(this),"updated_payment_info"),a}.bind(this))},save_fields:function(a){for(var b=0;b<a.length;b++)"undefined"!=typeof this[a[b]]&&util.remove_class(this[a[b]].parentNode,"error");this.model.save_fields(a,{success:function(a){util.notify("Account updated!")}.bind(this),error:function(a){console.log("lol: ",typeof this[a.field]),"undefined"!=typeof this[a.field]&&(this[a.field].parentNode.className+=" error",this[a.field].focus(),this.error_info.textContent=a.msg)}.bind(this)})},remove_card:function(a){a&&a.preventDefault(),this.track_subcontroller("remove_card_modal",function(){return new BlockingModalController}.bind(this));var b=new FormData;b.append("api_key",faxrobot.account.get("api_key"));var c=new XMLHttpRequest;c.onreadystatechange=function(){if(4===c.readyState){var a=this.get_subcontroller("remove_card_modal");a&&a.close();var b=JSON.parse(c.response);200==c.status?(this.model.set(b),util.notify("Your card was removed.")):faxrobot.error.api(c.status,c.response)}}.bind(this),c.open("post",API_URL+"/accounts/remove_card",!0),c.send(b)},one_off_payment:function(a){a&&a.preventDefault();var b=parseInt(a.target.textContent.substr(1));this.track_subcontroller("checkout_modal",function(){return new BlockingModalController}.bind(this)),this.stripe=StripeCheckout.configure({key:STRIPE_PUBLIC_KEY,closed:function(){var a=this.get_subcontroller("checkout_modal");a&&a.close()}.bind(this),token:function(a){console.log("STRIPE SUCCESS:",a);var c=new FormData;c.append("api_key",faxrobot.account.get("api_key")),c.append("email",faxrobot.account.get("email")),c.append("amount",b),c.append("stripe_token",a.id);var d=new XMLHttpRequest;d.onreadystatechange=function(){if(4===d.readyState){var a=this.get_subcontroller("checkout_modal");a&&a.close();var c=JSON.parse(d.response);200==d.status?(this.model.set(c),util.notify("Thanks for your purchase!"),"undefined"!=typeof hooks&&"function"==typeof hooks.payment&&hooks.payment(b,"oneoff")):c&&6005==c.code?faxrobot.error.show(103):faxrobot.error.api(d.status,d.response)}}.bind(this),d.open("post",API_URL+"/accounts/bootstrap",!0),d.send(c)}.bind(this)}),this.stripe.open({name:"Buy "+PROJECT_NAME+" Credit ($"+b+")",description:"For whenever you need to send a fax.",amount:100*b})},change_email_pref:function(a){var b={};b[a.target.id]=a.target.checked?1:0,this.model.set(b),this.save_fields([a.target.id])},edit_account:function(a){a&&a.preventDefault(),this.cancel.style.display="block",this.edit.style.display="none",this.button.style.display="block";for(var b=this.form.querySelectorAll("input"),c=0;c<b.length;c++)b[c].disabled=!1},cancel_edit:function(a){a&&a.preventDefault(),this.form.reset(),this.grab_form(),this.cancel.style.display="none",this.edit.style.display="block",this.button.style.display="none";for(var b=this.form.querySelectorAll("input"),c=0;c<this.account_fields.length;c++)util.remove_class(this[this.account_fields[c]].parentNode,"error");this.error_info.textContent="";for(var c=0;c<b.length;c++)b[c].disabled=!0},save_account:function(a){a&&a.preventDefault();for(var b=0;b<this.account_fields.length;b++)util.remove_class(this[this.account_fields[b]].parentNode,"error");this.grab_form(),this.save_fields(this.account_fields)},disable_incoming:function(a){if(a&&a.preventDefault(),!confirm("Are you sure you wish to disable your incoming fax number? This change will take effect immediately and you may not be able to get this number back."))return!1;var b=new FormData;b.append("api_key",faxrobot.account.get("api_key"));var c=new XMLHttpRequest;c.onreadystatechange=function(){if(4===c.readyState){var a=JSON.parse(c.response);200==c.status?(this.model.set({incoming_number:null}),util.notify("Your number was removed!")):a&&6023==a.code?faxrobot.error.show(603):a&&6024==a.code?faxrobot.error.show(604):faxrobot.error.api(c.status,c.response)}}.bind(this),c.open("post",API_URL+"/incoming/deprovision",!0),c.send(b)},grab_form:function(){this.model.set({first_name:this.first_name.value,last_name:this.last_name.value,email:this.email.value,address:this.address.value,address2:this.address2.value,city:this.city.value,state:this.state.value,zip:this.zip.value},{silent:!0})},change_password:function(a){a.preventDefault(),new AccountsChangePasswordController},view_api_key:function(a){a.preventDefault(),new AccountsAPIKeyController}}),AccountsPaymentInfoController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,template:"accounts_payment_info",model:null,closable:!0,show_incoming_onboarding:!1,default_button_text:"Save Credit Card",elements:{"#card_number":"card_number","#expires_month":"expires_month","#expires_year":"expires_year","#cvc":"cvc",button:"button"},events:{"submit form":"submit","keyup #card_number":"insert_annoying_dashes"},stripe_check_interval:null,prev_length:0,init:function(){return this.render(),this.stripe_check_interval=setInterval(function(){Stripe&&(Stripe.setPublishableKey(STRIPE_PUBLIC_KEY),clearInterval(this.stripe_check_interval),this.button.disabled=!1)}.bind(this),500),this.card_number.focus(),this},render:function(){var a=this.model.toJSON();a.show_incoming_onboarding=this.show_incoming_onboarding;var b=faxrobot.render_template(this.template,a);if(this.html(b),this.button.textContent=this.default_button_text,this.show(),!document.getElementById("stripe_script")){var c=document.createElement("script");c.type="text/javascript",c.id="stripe_script",c.src="https://js.stripe.com/v2/",document.head.appendChild(c)}return this},insert_annoying_dashes:function(a){var b=this.get_number(),c=Math.ceil(b.length/4)-1;if(b.length<this.prev_length||b.length%4==0&&173==a.keyCode)return void(this.prev_length=b.length);this.prev_length=b.length;for(var d=0;d<c;d++)b=b.substr(0,4*(d+1)+d)+"-"+b.substr(4*(d+1)+d);this.card_number.value=b.substr(0,19)},get_number:function(){return this.card_number.value.replace(/[\s-]/g,"")},submit:function(a){a.preventDefault(),this.button.disabled=!0,util.remove_class(this.card_number.parentNode,"error"),util.remove_class(this.expires_month.parentNode,"error"),util.remove_class(this.cvc.parentNode,"error"),Stripe.card.createToken({number:this.get_number(),cvc:this.cvc.value,exp_month:this.expires_month.value,exp_year:this.expires_year.value},function(a,b){if(this.button.disabled=!1,b.error){if(b.error.param)switch(b.error.param){case"number":this.card_number.parentNode.className+=" error";break;case"exp_month":case"exp_year":this.expires_month.parentNode.className+=" error";break;case"cvc":this.cvc.parentNode.className+=" error"}faxrobot.error.show(103)}else this.model.set({last4:b.card.last4,stripe_token:b.id},{silent:!0}),this.trigger("success"),this.close()}.bind(this))}}),AccountsRegisterController=BaseModalController.extend({inject:faxrobot.main_container_selector,closable:!0,el:!1,code:0,template:"accounts_register",default_button_text:"Register",elements:{".center_row.email":"email_row",".center_row.password":"password_row","#register_name":"name","#register_email":"email","#register_password":"password",button:"button",h2:"title",".error":"error"},events:{"submit form":"register"},init:function(){return this.render(),this},render:function(){var a={},b=faxrobot.render_template(this.template,a);return this.html(b),this.button.textContent=this.default_button_text,this.show(),this},after_show:function(){this.name.focus()},before_close:function(){0==faxrobot.controllers.pages.cur_controller&&faxrobot.route("/")},register:function(a){if(a.preventDefault(),util.remove_class(this.name.parentNode,"error"),util.remove_class(this.email.parentNode,"error"),util.remove_class(this.password.parentNode,"error"),this.error.textContent="",!this.name.value)return this.name.parentNode.className+=" error",void(this.error.textContent="Please enter a name");this.button.disabled=!0,this.button.textContent="wait...";var b=this.name.value.indexOf(" ");if(b!==-1)var c=this.name.value.substr(0,b),d=this.name.value.substr(b+1);var e=new FormData;e.append("first_name",c),e.append("last_name",d),e.append("email",this.email.value),e.append("password",this.password.value);var f=new XMLHttpRequest;f.onreadystatechange=function(){if(4===f.readyState){var a=JSON.parse(f.response);200==f.status?(faxrobot.account.login(a),util.notify("Welcome, "+faxrobot.account.name()+"!"),this.close()):(this.button.textContent=this.default_button_text,this.button.disabled=!1,
a&&a.code>=6e3&&a.code<=6003?(this[a.field].parentNode.className+=" error",this.error.textContent=a.msg):faxrobot.error.api(f.status,f.response))}}.bind(this),f.open("post",API_URL+"/accounts/create",!0),f.send(e)}}),AccountsTransactionsController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,template:"accounts_transactions",model:null,page:1,elements:{"div.table":"table"},events:{"click a.login":"login"},init:function(){return util.set_title("My transactions"),this.with_bind(faxrobot.account,"change",this.render.bind(this)),this.render(),document.body.style.backgroundColor="purple",this},login:function(a){a.preventDefault(),new AccountsLoginController},render:function(){var a={logged_in:faxrobot.account.is_logged_in(),credit:faxrobot.account.get("credit")},b=faxrobot.render_template(this.template,a);return this.html(b),a.logged_in&&this.track_subcontroller("list_table",function(){return new AccountsTransactionsListController({page:this.page,inject:this.table})}.bind(this)),this}}),AccountsTransactionsListController=Composer.Controller.extend({template:"accounts_transactions_list",collection:null,page:1,pages:0,per_page:0,total:0,has_next:!1,has_prev:!1,elements:{},events:{},init:function(){return this.with_bind(faxrobot.account,"change",this.release.bind(this)),this.render(),this.load_transactions(),this},render:function(){var a={collection:this.collection?this.collection.toJSON():null,pages:this.pages,filter:this.filter,page:this.page,per_page:this.per_page,total:this.total,has_next:this.has_next,has_prev:this.has_prev},b=faxrobot.render_template(this.template,a);return this.html(b),this},load_transactions:function(){var a=new XMLHttpRequest;a.onreadystatechange=function(){if(4===a.readyState){var b=JSON.parse(a.response);200==a.status?(this.collection=new Transactions(b.transactions),this.pages=b.pages,this.per_page=b.per_page,this.total=b.total,this.has_next=b.has_next,this.has_prev=b.has_prev,this.render()):b&&4e3==b.code&&faxrobot.access_denied()}}.bind(this);var b="api_key="+faxrobot.account.get("api_key")+"&page="+this.page;a.open("get",API_URL+"/accounts/transactions?"+b,!0),a.send()},update:function(a){this.page=a,this.collection=null,this.render(),this.load_transactions()}}),ErrorModalController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,code:0,template:"errors_modal",elements:{},events:{"click button":"close"},init:function(){return this.render(),this},render:function(){for(var a=!1,b=0;b<ERRORS.length;b++)this.code==ERRORS[b].code&&(a=ERRORS[b]);a||(a=ERRORS[0]),a.detail=a.detail.replace("{SUPPORTED_FILETYPES}",SUPPORTED_FILETYPES.join(", ")),a.detail=a.detail.replace("{SUPPORT_MAILTO}",'<a href="mailto:'+EMAIL_SUPPORT+'">'+EMAIL_SUPPORT+"</a>"),console.log("data: ",a);var c=faxrobot.render_template(this.template,a);return this.html(c),this.show(),this}}),HeaderController=Composer.Controller.extend({inject:"header",el:!1,template:"header_header",elements:{"ul.menu":"menu","ul.nav":"nav","div.avatar":"avatar"},events:{"click a.login":"login","click a.register":"register"},init:function(){return this.render(),this.with_bind(faxrobot.account,"change",this.render.bind(this)),this},render:function(){var a=faxrobot.account.toJSON();a.email&&(a.hash=(new MD5).hash(a.email.toLowerCase()),a.first_name||(a.first_name=a.email));var b=faxrobot.render_template(this.template,a);return this.html(b),this.avatar&&(this.avatar.addEventListener("click",this.show_menu.bind(this)),this.menu.addEventListener("mouseleave",this.hide_menu.bind(this))),this.state_change(),this},login:function(a){a.preventDefault(),new AccountsLoginController({})},register:function(a){a.preventDefault(),new AccountsRegisterController({})},show_menu:function(a){this.menu.style.display="block"},hide_menu:function(a){this.menu.style.display="none"},state_change:function(){for(var a=window.location.origin.length+1,b=window.location.pathname.substr(1),c=0;c<this.nav.childNodes.length;c++){var d=this.nav.childNodes[c];if(d.tagName){var e=d.firstChild.href.substr(a);e&&b.indexOf(e)!=-1?d.className="sel":b||e?d.className="":d.className="sel"}}}}),IncomingListTableController=Composer.Controller.extend({template:"incoming_list_table",collection:null,page:1,pages:0,per_page:0,total:0,has_next:!1,has_prev:!1,elements:{},events:{"click a.view":"click_view","click a.delete":"click_delete","click td":"click_row"},init:function(){return this.with_bind(faxrobot.account,"change",this.release.bind(this)),this.render(),this.load_incoming(),this},render:function(){var a={collection:this.collection?this.collection.toJSON():null,pages:this.pages,page:this.page,per_page:this.per_page,total:this.total,has_next:this.has_next,has_prev:this.has_prev},b=faxrobot.render_template(this.template,a);return this.html(b),this},load_incoming:function(){var a=new XMLHttpRequest;a.onreadystatechange=function(){if(4===a.readyState){var b=JSON.parse(a.response);200==a.status?(this.collection=new Incomings(b.faxes),this.pages=b.pages,this.per_page=b.per_page,this.total=b.total,this.has_next=b.has_next,this.has_prev=b.has_prev,this.render()):b&&4e3==b.code&&faxrobot.access_denied()}}.bind(this);var b="api_key="+faxrobot.account.get("api_key")+"&page="+this.page;a.open("get",API_URL+"/incoming/list?"+b,!0),a.send()},click_view:function(a){a.preventDefault(),this.show_fax(a.target.parentNode.parentNode.parentNode.id.substr(4))},click_delete:function(a){if(a.preventDefault(),!confirm("Are you sure you wish to delete this fax?"))return!1;this.track_subcontroller("plz_wait",function(){return new BlockingModalController}.bind(this));var b=a.target.parentNode.parentNode.id.substr(4),c=new FormData;c.append("api_key",faxrobot.account.get("api_key")),c.append("access_key",b);var d=new XMLHttpRequest;d.onreadystatechange=function(){if(4===d.readyState){var a=this.get_subcontroller("plz_wait");a&&a.close(),200==d.status?(this.collection=null,this.render(),this.load_incoming()):faxrobot.error.show(0)}}.bind(this),d.open("post",API_URL+"/incoming/delete",!0),d.send(c)},click_row:function(a){a.defaultPrevented||this.show_fax(a.target.parentNode.id.substr(4))},show_fax:function(a){window.open(API_URL+"/incoming/view?access_key="+a+"&api_key="+faxrobot.account.get("api_key"))},update:function(a,b){this.page=b,this.collection=null,this.render(),this.load_incoming()}}),IncomingListController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,template:"incoming_list",model:null,page:1,elements:{"div.table":"table"},events:{"click a.login":"login"},init:function(){return util.set_title("My received faxes"),this.with_bind(faxrobot.account,"change",this.render.bind(this)),this.render(),document.body.style.backgroundColor="#acd",this},login:function(a){a.preventDefault(),new AccountsLoginController},render:function(){var a={logged_in:faxrobot.account.is_logged_in(),incoming_number:faxrobot.account.get("incoming_number")},b=faxrobot.render_template(this.template,a);return this.html(b),a.logged_in&&this.track_subcontroller("incoming_table",function(){return new IncomingListTableController({page:this.page,inject:this.table})}.bind(this)),this}}),IncomingProvisionController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,model:null,template:"incoming_provision",elements:{"a.login":"login_link",'input[name="area_code"]':"area_code","#provision_cta":"provision_cta","#already_provisioned":"already_provisioned","#already_provisioned strong":"already_strong",h2:"h2"},events:{"click a.login":"login","submit form.signup":"register","submit form.account":"account"},init:function(){return util.set_title("Receive faxes"),this.model=faxrobot.account,this.render(),this.maybe_hide_link(),this.with_bind(faxrobot.account,"change",this.maybe_hide_link.bind(this)),document.body.style.backgroundColor="#480",this},render:function(){var a={},b=faxrobot.render_template(this.template,a);return this.html(b),this},maybe_hide_link:function(){faxrobot.account.is_logged_in()?(this.login_link.style.display="none",faxrobot.account.get("incoming_number")?(this.provision_cta.style.display="none",this.already_provisioned.style.display="block",this.h2.textContent="Thanks for your business!",this.already_strong.textContent=util.format_phone(faxrobot.account.get("incoming_number")),document.body.style.backgroundColor="#d23636"):(this.provision_cta.style.display="block",this.already_provisioned.style.display="none")):this.login_link.style.display="block"},register:function(a){return a&&a.preventDefault(),faxrobot.account.is_logged_in()?this.logged_in():(new AccountsRegisterController({default_button_text:"Next >"}),void this.do_bind())},login:function(a){a&&a.preventDefault(),new AccountsLoginController({}),this.do_bind()},do_bind:function(){faxrobot.account.unbind("change","dumb_bind"),this.with_bind_once(faxrobot.account,"change",this.logged_in.bind(this),"dumb_bind")},logged_in:function(){if(this.model.get("has_stripe_token")){if(!this.model.get("auto_recharge")){if(confirm("This action will turn on auto-bill for your account in order to keep your fax number active."))return this.auto_recharge_and_provision(["auto_recharge"]);return}return this.provision_number()}this.track_subcontroller("payment_info",function(){var a=new AccountsPaymentInfoController({model:this.model,show_incoming_onboarding:!0,default_button_text:"Confirm Payment"});return this.with_bind_once(a,"success",function(){this.auto_recharge_and_provision(["last4","stripe_token","auto_recharge"])}.bind(this),"updated_payment_info"),a}.bind(this))},auto_recharge_and_provision:function(a){this.plz_wait(),this.model.set({auto_recharge:1}),this.model.save_fields(a,{success:function(a){this.provision_number()}.bind(this)})},plz_wait:function(){this.track_subcontroller("plz_wait",function(){return new BlockingModalController}.bind(this))},provision_number:function(){var a=new FormData;a.append("api_key",faxrobot.account.get("api_key")),a.append("area_code",this.area_code.value);var b=new XMLHttpRequest;b.onreadystatechange=function(){if(4===b.readyState){var a=this.get_subcontroller("plz_wait");a&&a.close();var c=JSON.parse(b.response);200==b.status?(faxrobot.account.set(c),util.notify("Your fax number was created!")):6022==c.code?(faxrobot.route("/account"),faxrobot.error.show(602)):6018==c.code||6019==c.code?faxrobot.error.show(104):6020==c.code?faxrobot.error.show(600):6021==c.code&&faxrobot.error.show(601)}}.bind(this),b.open("post",API_URL+"/incoming/provision",!0),b.send(a)},account:function(a){a.preventDefault(),faxrobot.route("/jobs/received")}}),JobsConfirmPasswordController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,closable:!0,confirmed:null,template:"jobs_confirm_password",elements:{"#confirm_password":"password"},events:{"submit form":"confirm"},init:function(){return this.render(),this},render:function(){var a=faxrobot.render_template(this.template,{});return this.html(a),this.show(),this},confirm:function(a){a.preventDefault(),this.confirmed=this.password.value,this.close()},before_close:function(){this.confirmed?this.trigger("confirm",this.confirmed):this.trigger("close")}}),JobsCoverSheetController=BaseModalController.extend({inject:faxrobot.main_container_selector,el:!1,template:"jobs_coversheet",model:null,error:null,closable:!0,elements:{".error":"msg"},events:{"change input":"do_change","change textarea":"do_change","click button":"close"},init:function(){return this.model.set({cover_edited:!0}),this.render(),this},render:function(){var a=this.model.toJSON(),b=faxrobot.render_template(this.template,a);if(this.html(b),this.error&&this.error.field){var c=document.getElementById(this.error.field);c&&(c.parentNode.className+=" error",this.msg.textContent=this.error.msg)}return this.show(),this},do_change:function(a){var b={};b[a.target.id]=a.target.value,this.model.set(b)}}),JobsCreateController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,template:"jobs_create",model:null,xhr:null,stripe:null,stripe_token:null,stripe_wait_interval:null,confirmed_password:null,button_text_original:null,default_cost_text:"$0.10/page US & Canada!<br/>$0.20/page internationally",elements:{"input[type=file]":"input_file","input[type=checkbox]":"input_coversheet",".edit":"coversheet_edit_link","input[name=destination]":"input_destination",".choose_file em":"filename_area",".cost strong":"cost_area",".row.destination":"destination_row",".row.file":"file_row",button:"button"},events:{"click .choose_file a":"choose_file","click .preview":"choose_file","change input[type=checkbox]":"change_coversheet_checkbox","click .edit a":"open_coversheet_editor","change input[type=file]":"handle_file","change input[name=destination]":"handle_destination","click button":"handle_submit"},init:function(){return util.set_title("Send a fax"),this.model=new Job,this.render(),this.render_cost(),this.stripe_wait_interval=setInterval(function(){"undefined"!=typeof StripeCheckout&&(this.stripe=StripeCheckout.configure({key:STRIPE_PUBLIC_KEY,bitcoin:!0,token:function(a){this.stripe_token=a,this.process_stripe_token()}.bind(this),closed:function(){this.stripe_token||this.enable_send_button()}.bind(this)}),clearInterval(this.stripe_wait_interval))}.bind(this),100),document.body.style.backgroundColor="#480",this},render:function(){var a={},b=faxrobot.render_template(this.template,a);return this.html(b),this},choose_file:function(a){a&&(a.preventDefault(),a.stopPropagation()),this.input_file.click()},change_coversheet_checkbox:function(a){a&&a.preventDefault(),this.model.set({cover:this.input_coversheet.checked}),0==this.model.get("cover_edited")&&this.open_coversheet_editor(),setTimeout(function(){this.coversheet_edit_link.style.display="inline"}.bind(this),500),this.render_cost()},open_coversheet_editor:function(a){a&&(a.preventDefault(),a.stopPropagation()),this.track_subcontroller("coversheet",function(){return new JobsCoverSheetController({model:this.model})}.bind(this))},handle_destination:function(a){var b=this.input_destination.value,c=this.destination_row;if(b=b.replace(/[-\(\)\s\+]/g,""),console.log("destination: ",b),b.length<10||isNaN(b))return this.model.set({destination:null}),c.className+=" error",!1;if(c.className=c.className.replace(/error/g,""),this.model.set({destination:b}),this.model.get("access_key")){this.model.set({cost:null}),this.render_cost();var d=new FormData;d.append("destination",this.model.get("destination"));var e=API_URL+"/jobs/update/"+this.model.get("access_key"),f=new XMLHttpRequest;f.onreadystatechange=function(){if(4===f.readyState){var a=JSON.parse(f.response);this.model.set({cost:a.cost?a.cost:null,cover_cost:a.cover_cost?a.cover_cost:null,international:a.international?a.international:null}),this.render_cost()}}.bind(this),f.open("post",e,!0),f.send(d)}return!0},handle_file:function(a){this.model.new_file();var b=this.input_file.files[0],c=b.name.split(".").pop();if(SUPPORTED_FILETYPES.indexOf(c)==-1)return faxrobot.error.show(100),this.eject_file();this.file_row.className=this.file_row.className.replace(/error/g,""),this.model.set({status:"uploading"});var d=new FormData;d.append("file",b),this.render_filename_area(b.name,c),this.render_cost(),this.model.get("destination")&&d.append("destination",this.model.get("destination")),this.xhr&&(this.xhr.abort(),this.xhr=null),this.xhr=new XMLHttpRequest;var e=this.xhr;e.upload&&(e.upload.onprogress=this.upload_progress),e.onreadystatechange=function(){4===e.readyState&&(200==e.status?this.upload_success(JSON.parse(e.response)):this.handle_error(e.status,e.response))}.bind(this),e.open("post",API_URL+"/jobs/create",!0),e.send(d)},eject_file:function(a){a||(this.file_row.className+=" error"),this.render_cost(),this.render_filename_area()},render_filename_area:function(a,b){if(a){switch(b){case"docx":case"doc":var c="docx.png";break;case"pdf":var c="pdf.png";break;default:var c="other.png"}var d='<img src="images/icons/'+c+'">'+a}else var d="Please choose a supported file";this.filename_area.innerHTML=d},render_cost:function(){if("new"==this.model.get("status"))var a=this.default_cost_text;else if(this.model.get("failed"))var a=this.default_cost_text;else if(this.model.get("cost")){var b=this.model.get("cost");this.model.get("cover")&&(b+=this.model.get("cover_cost"));var a="Just $"+b.toFixed(2)+" ";this.model.get("international")?a+='<em class="error">(international)</em>':(a+="("+this.model.get("num_pages")+" page",this.model.get("num_pages")>1&&(a+="s"),this.model.get("cover")&&(a+=" + cover"),a+=")")}else var a='<img src="images/ajax-load.gif"> Calculating cost...';this.cost_area.innerHTML=a},upload_progress:function(a){},handle_error:function(a,b){var c=JSON.parse(b),d=c&&c.code?c.code:0;switch(console.log("decoded:",c),console.log("error_code:",d),d){case 5001:faxrobot.error.show(102),this.eject_file(!0),this.destination_row.className+=" error";break;default:faxrobot.error.api(a,b),this.model.new_file(),this.eject_file()}this.trigger("fail")},upload_success:function(a){console.log("result: ",a),this.model.set({access_key:a.access_key,id:109}),setTimeout(function(){this.monitor_status(a.access_key)}.bind(this),STATUS_POLL_DELAY)},monitor_status:function(a){console.log("Polling status for "+a);var b=new XMLHttpRequest;b.onreadystatechange=function(){if(4===b.readyState){if(this.model.get("access_key")!=a)return console.log("NEVERMIND! "+a+" was aborted");if(200==b.status){var c=JSON.parse(b.response);"failed"==c.status?(this.model.set({status:c.status,failed:!0,fail_code:c.fail_code}),this.eject_file(),faxrobot.error.show(c.fail_code),this.trigger("fail")):"ready"!=c.status?setTimeout(function(){this.monitor_status(a)}.bind(this),STATUS_POLL_DELAY):this.job_ready(c)}else this.handle_error(b.status,b.response)}}.bind(this),b.open("get",API_URL+"/jobs/get/"+a,!0),b.send()},job_ready:function(a){console.log("JOB IS READY: ",a),this.model.set({status:a.status,cost:a.cost,cover_cost:a.cover_cost,num_pages:a.num_pages,international:a.international}),this.render_cost(),this.trigger("ready")},unbind_upload_listeners:function(){this.unbind("ready","submit_ready_listener"),this.unbind("fail","submit_fail_listener")},disable_send_button:function(){console.log("disable_send_button"),this.button_text_original=this.button.textContent,this.button.textContent="...",this.button.disabled=!0},enable_send_button:function(){console.log("enable_send_button"),this.button.textContent=this.button_text_original,this.button.disabled=!1},handle_submit:function(a){a.preventDefault();var b=function(){this.unbind_upload_listeners(),this.enable_send_button();var a=this.get_subcontroller("uploading_modal");a&&a.close()}.bind(this),c=function(){var a=this.model.get("status");return"ready"!=a&&"uploading"!=a?(faxrobot.error.show(ERR_NO_FILE_ATTACHED),this.file_row.className+=" error",!1):!!this.handle_destination()||(faxrobot.error.show(ERR_BAD_DESTINATION),!1)}.bind(this);return!!c()&&(this.disable_send_button(),"ready"==this.model.get("status")?this.handle_payment():(console.log("STATUS: "+this.model.get("status")),this.track_subcontroller("uploading_modal",function(){return new BlockingModalController({text:"Please wait while your file uploads..."})}.bind(this)),this.with_bind_once(this,"ready",this.handle_payment.bind(this),"submit_ready_listener"),void this.with_bind_once(this,"fail",b,"submit_fail_listener")))},handle_payment:function(){var a=this.get_subcontroller("uploading_modal");a&&a.close(),!faxrobot.account.is_logged_in()||faxrobot.account.get("credit")-this.model.get("cost")<0&&!faxrobot.account.get("auto_recharge")?this.checkout():this.send_fax()},checkout:function(){this.unbind_upload_listeners();var a=this.get_subcontroller("uploading_modal");a&&a.close(),this.stripe.open({name:"Buy "+PROJECT_NAME+" Credit ($3)",description:"For whenever you need to send a fax.",amount:100*PAYMENT_DEFAULT_AMOUNT}),this.with_bind_once(faxrobot.router,"route",function(){console.log("act now supplies are limited"),this.stripe.close()}.bind(this))},process_stripe_token:function(){if(!this.stripe_token)return!1;var a=new FormData;faxrobot.account.get("api_key")&&a.append("api_key",faxrobot.account.get("api_key")),a.append("email",this.stripe_token.email),a.append("stripe_token",this.stripe_token.id),a.append("amount",PAYMENT_DEFAULT_AMOUNT),this.confirmed_password&&a.append("password",this.confirmed_password);var b=new XMLHttpRequest;b.onreadystatechange=function(){if(4===b.readyState){var a=JSON.parse(b.response);if(200==b.status)return this.stripe_token=null,this.confirmed_password=null,faxrobot.account.login(a),"undefined"!=typeof hooks&&"function"==typeof hooks.payment&&hooks.payment(PAYMENT_DEFAULT_AMOUNT,"bootstrap"),this.send_fax();a&&6004==a.code?this.track_subcontroller("confirm_password",function(){var a=new JobsConfirmPasswordController;return this.with_bind_once(a,"close",function(){this.stripe_token=null,this.enable_send_button()}.bind(this),"confirm_password_close"),this.with_bind_once(a,"confirm",function(a){this.confirmed_password=a,this.process_stripe_token()}.bind(this),"confirm_password_confirm"),a}.bind(this)):(this.enable_send_button(),this.stripe_token=null,faxrobot.error.api(b.status,b.response))}}.bind(this),b.open("post",API_URL+"/accounts/bootstrap",!0),b.send(a)},send_fax:function(){console.log("sending fax");var a=new FormData;a.append("api_key",faxrobot.account.get("api_key")),a.append("destination",this.model.get("destination")),a.append("send_authorized",!0),a.append("cover",this.model.get("cover")),a.append("cover_name",this.model.get("cover_name")),a.append("cover_phone",this.model.get("cover_phone")),a.append("cover_email",this.model.get("cover_email")),a.append("cover_company",this.model.get("cover_company")),a.append("cover_to_name",this.model.get("cover_to_name")),a.append("cover_subject",this.model.get("cover_subject")),a.append("cover_comments",this.model.get("cover_comments"));var b=API_URL+"/jobs/update/"+this.model.get("access_key"),c=new XMLHttpRequest;c.onreadystatechange=function(){if(4===c.readyState){var a=JSON.parse(c.response);200==c.status?(faxrobot.account.set({changed_tmp_pwd:!1}),util.notify("Thanks! Your fax is queued for sending."),faxrobot.route("/jobs")):a&&a.code>=5005&&a.code<=5017?(this.enable_send_button(),this.track_subcontroller("coversheet",function(){return new JobsCoverSheetController({model:this.model,error:a})}.bind(this))):(this.enable_send_button(),faxrobot.error.api(c.status,c.response))}}.bind(this),c.open("post",b,!0),c.send(a)}}),JobsInfoController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,template:"job_info",access_key:null,model:null,elements:{button:"button"},events:{"click a.status":"click_status","click button":"start","click a.login":"login"},init:function(){return util.set_title("Job details"),this.with_bind(faxrobot.account,"change",this.render.bind(this)),this.render(),document.body.style.backgroundColor="black",this},login:function(a){a.preventDefault(),new AccountsLoginController},render:function(){var a={logged_in:faxrobot.account.is_logged_in(),model:this.model?this.model.toJSON():null,reason:ERRORS[0].title,detail:ERRORS[0].detail};if(this.model&&"failed"==this.model.get("status"))for(var b=0;b<ERRORS.length;b++)ERRORS[b].code==this.model.get("fail_code")&&(a.reason=ERRORS[b].title,a.detail=ERRORS[b].detail);var c=faxrobot.render_template(this.template,a);return this.html(c),!this.model&&faxrobot.account.is_logged_in()&&this.populate(),this},populate:function(){console.log("Loading job: ",this.access_key);var a=new XMLHttpRequest;if(a.onreadystatechange=function(){4===a.readyState&&(200==a.status?(this.model=new Job(JSON.parse(a.response)),this.render()):faxrobot.access_denied())}.bind(this),64==this.access_key.length)var b=API_URL+"/jobs/get/"+this.access_key;else var b=API_URL+"/jobs/get_by_id/"+this.access_key;a.open("get",b+"?api_key="+faxrobot.account.get("api_key"),!0),a.send()},click_status:function(a){a.preventDefault()},start:function(a){a.preventDefault(),this.button.disabled=!0,this.button.textContent="...";var b=new FormData;if(b.append("api_key",faxrobot.account.get("api_key")),a.target.className.indexOf("restart")!=-1)var c=API_URL+"/jobs/restart/"+this.access_key,d="Your fax is restarting.";else{b.append("access_key",this.access_key),b.append("send_authorized",1);var c=API_URL+"/jobs/update",d="Your fax is starting!"}var e=new XMLHttpRequest;e.onreadystatechange=function(){4===e.readyState&&(200==e.status?(this.model=new Job(JSON.parse(e.response)),util.notify(d),this.render()):faxrobot.access_denied())}.bind(this),e.open("post",c,!0),e.send(b)}}),JobsListTableController=Composer.Controller.extend({template:"jobs_list_table",collection:null,filter:"all",page:1,pages:0,per_page:0,total:0,has_next:!1,has_prev:!1,elements:{},events:{"click a.status":"click_status","click td":"click_row"},init:function(){return this.with_bind(faxrobot.account,"change",this.release.bind(this)),this.render(),this.load_jobs(),this.reload_interval=setInterval(function(){this.update(this.filter,this.page,!0)}.bind(this),3e4),this.bind("release",function(){this.reload_interval&&clearInterval(this.reload_interval)}.bind(this)),this},render:function(){var a={collection:this.collection?this.collection.toJSON():null,pages:this.pages,filter:this.filter,page:this.page,per_page:this.per_page,total:this.total,has_next:this.has_next,has_prev:this.has_prev},b=faxrobot.render_template(this.template,a);return this.html(b),this},load_jobs:function(){var a=new XMLHttpRequest;a.onreadystatechange=function(){if(4===a.readyState){var b=JSON.parse(a.response);200==a.status?(this.collection=new Jobs(b.jobs),this.pages=b.pages,this.per_page=b.per_page,this.total=b.total,this.has_next=b.has_next,this.has_prev=b.has_prev,this.render()):b&&4e3==b.code&&faxrobot.access_denied()}}.bind(this);var b="api_key="+faxrobot.account.get("api_key")+"&page="+this.page;"all"!=this.filter&&(b+="&filter="+this.filter),a.open("get",API_URL+"/jobs/list?"+b,!0),a.send()},click_status:function(a){a.preventDefault(),this.show_status(a.target.parentNode.parentNode.id.substr(4))},click_row:function(a){a.defaultPrevented||this.show_status(a.target.parentNode.id.substr(4))},show_status:function(a){faxrobot.route("/job/"+a)},update:function(a,b,c){this.filter=a,this.page=b,c||(this.collection=null,this.render()),this.load_jobs()}}),JobsListController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,template:"jobs_list",model:null,page:1,filter:"all",elements:{"div.table":"table"},events:{"click a.login":"login","click a.change_password":"change_password"},init:function(){return util.set_title("My faxes"),this.with_bind(faxrobot.account,"change",this.render.bind(this)),this.render(),document.body.style.backgroundColor="brown",this},login:function(a){a.preventDefault(),new AccountsLoginController},render:function(){var a={logged_in:faxrobot.account.is_logged_in(),credit:faxrobot.account.get("credit"),temporary_password:faxrobot.account.get("temporary_password"),changed_tmp_pwd:faxrobot.account.get("changed_tmp_pwd")},b=faxrobot.render_template(this.template,a);return this.html(b),a.logged_in&&this.track_subcontroller("list_table",function(){return new JobsListTableController({page:this.page,filter:this.filter,inject:this.table})}.bind(this)),this},change_password:function(a){a.preventDefault(),new AccountsChangePasswordController}}),ContentPageController=Composer.Controller.extend({inject:faxrobot.main_container_selector,el:!1,template:"page",html_content:null,page:"",title:"",jump_to:null,color:"black",elements:{},events:{"click a.api_key":"show_api_key","click a.support":"contact_support"},init:function(){return util.set_title(this.title),this.with_bind(faxrobot.account,"change",this.render.bind(this)),this.render(),document.body.style.backgroundColor=this.color,this},render:function(){var a={html:this.html_content},b=faxrobot.render_template(this.template,a);return this.html(b),this.html_content?this.jump():this.populate(),this},populate:function(){var a=new XMLHttpRequest;a.onreadystatechange=function(){if(4===a.readyState)if(200==a.status)this.html_content=a.response,this.render();else{var b="<h1>404</h1>/pages/"+this.page+".html not found :(";this.html_content=b,this.render()}}.bind(this),a.open("get","/pages/"+this.page+".html",!0),a.send()},show_api_key:function(a){a.preventDefault(),faxrobot.account.is_logged_in()?new AccountsAPIKeyController:faxrobot.route("/account")},jump:function(){if(this.jump_to){var a=document.getElementById(this.jump_to);a&&a.scrollIntoView()}},contact_support:function(a){a.preventDefault(),window.location.href="mailto:"+EMAIL_SUPPORT}}),PagesController=Composer.Controller.extend({inject:faxrobot.main_container_selector,className:"home",template:"pages/home",cur_controller:!1,init:function(){this.bind("onroute",this.on_route.bind(this)),this.bind("preroute",this.pre_route.bind(this)),this.bind("route",this.pre_load.bind(this)),this.bind("loaded",this.post_load.bind(this))},release:function(){return this.release_current(),this.unbind(),this.parent()},on_route:function(a){},pre_route:function(a){var b=(faxrobot.last_url||window.location.pathname).replace(/\-\-.*/,""),c=a.replace(/\-\-.*/,"");b!=c&&(faxrobot.scroll_to_top=!0)},pre_load:function(){this.page_loading(!0)},post_load:function(){setTimeout(function(){this.page_loading(!1)}.bind(this),200),faxrobot.scroll_to_top&&(window.scrollTo(0,0),faxrobot.scroll_to_top=!1)},load:function(a,b,c){if(b||(b={}),c||(c={}),"string"==typeof a)var a=window[a];c.keep_current&&a==this.cur_controller.$constructor?(Object.each(b,function(a,b){this.cur_controller[b]=a},this),this.cur_controller.init()):(this.release_current(),this.cur_controller=new a(b,{clean_injection:!0})),c.skip_loaded_event||setTimeout(function(){this.trigger("loaded")}.bind(this),1);var d=document.getElementById("wrapper");return d.scrollIntoView(),this.cur_controller},render:function(a){return a||(a={}),this.html(faxrobot.render_template(this.template,a)),this},release_current:function(){return faxrobot.util.set_title(""),!!this.cur_controller&&(this.cur_controller.release(),this.cur_controller=!1,!0)},page_loading:function(a){}}),accounts={login:function(){new AccountsLoginController({})},register:function(){new AccountsRegisterController({})},logout:function(){faxrobot.account.logout(),util.notify("Goodbye!"),faxrobot.router?faxrobot.route("/",{replace_state:!0}):window.location.replace("/")},manage:function(){faxrobot.controllers.pages.load(AccountsManageController,{})},transactions:function(a){a||(a=1);var b=faxrobot.controllers.pages;if(b.cur_controller instanceof AccountsTransactionsController){var c=b.cur_controller.get_subcontroller("list_table");c&&c.update(parseInt(a))}else b.load(AccountsTransactionsController,{page:parseInt(a)})},reset_password:function(a){new AccountsChangePasswordController({reset_hash:a})}},incoming={provision:function(){faxrobot.controllers.pages.load(IncomingProvisionController,{})},list:function(a){a||(a=1);var b=faxrobot.controllers.pages;if(b.cur_controller instanceof IncomingListController){var c=b.cur_controller.get_subcontroller("list_table");c&&c.update(parseInt(a))}else b.load(IncomingListController,{page:parseInt(a)})}},jobs={create:function(){faxrobot.controllers.pages.load(JobsCreateController,{})},list:function(a,b){a||(a="all"),b||(b=1);var c=faxrobot.controllers.pages;if(c.cur_controller instanceof JobsListController){var d=c.cur_controller.get_subcontroller("list_table");d&&d.update(a,parseInt(b))}else c.load(JobsListController,{filter:a,page:parseInt(b)})},view:function(a){var b={access_key:a},c=faxrobot.controllers.pages;if(c.cur_controller instanceof JobsListController){var d=c.cur_controller.get_subcontroller("list_table");if(d){var e=d.collection.find(function(b){return b.get("access_key")==a});e&&(b.model=e)}}faxrobot.controllers.pages.load(JobsInfoController,b)}},pages={faq:function(a){this._load("faq","Frequently Asked Questions","DarkSlateBlue ",a);
},api:function(a){this._load("api","API","Olive",a)},privacy:function(a){this._load("privacy","Privacy Policy","LightSlateGray",a)},tos:function(a){this._load("tos","Terms of Service","black",a)},_load:function(a,b,c,d){var e=faxrobot.controllers.pages;e.cur_controller instanceof ContentPageController&&e.cur_controller.page==a?(e.cur_controller.jump_to=d,e.cur_controller.jump()):e.load(ContentPageController,{page:a,title:b,jump_to:d,color:c})},not_found:function(){alert("404 lol ")}},Account=Composer.Model.extend({defaults:{api_key:null,email:"",first_name:"",last_name:"",address:"",address2:"",city:"",state:"",zip:"",credit:0,auto_recharge:0,has_stripe_token:!1,email_success:1,email_fail:1,email_list:1,incoming_number:null},init:function(a){},is_logged_in:function(){return!!this.get("api_key")},login:function(a){this.set(a),this.set_cookie()},set_cookie:function(){faxrobot.util.set_cookie("API_KEY",this.get("api_key"),14)},logout:function(){this.clear({silent:!0}),this.set(this.defaults),faxrobot.util.set_cookie("API_KEY","",14)},name:function(){return this.get("first_name")&&this.get("last_name")?this.get("first_name")+" "+this.get("last_name"):this.get("first_name")?this.get("first_name"):this.get("email")?this.get("email"):"Guest"},save_fields:function(a,b){b||(b={}),b.success||(b.success=function(){}),b.error||(b.error=function(){}),console.log("saving: ",a);var c=new FormData;c.append("api_key",faxrobot.account.get("api_key"));for(var d=0;d<a.length;d++)c.append(a[d],this.get(a[d]));var e=new XMLHttpRequest;e.onreadystatechange=function(){if(4===e.readyState){var a=JSON.parse(e.response);200==e.status?(this.set(a),b.success(a)):a&&a.field?b.error(a):a&&6006==a.code?faxrobot.error.show(104):faxrobot.error.api(e.status,e.response)}}.bind(this),e.open("post",API_URL+"/accounts/update",!0),e.send(c)}}),Incoming=Composer.Model.extend({defaults:{access_key:"",cost:0,create_date:null,data_deleted:!1,from_number:"",id:null,num_pages:0,to_number:""},init:function(a){}}),Incomings=Composer.Collection.extend({model:Incoming}),Job=Composer.Model.extend({defaults:{status:"new",cost:0,cover_cost:0,international:0,attempts:0,num_pages:0,send_authorized:!1,cover:!1,destination:null,cover_name:"",cover_address:"",cover_city:"",cover_state:"",cover_zip:"",cover_country:"",cover_phone:"",cover_email:"",cover_company:"",cover_to_name:"",cover_subject:"",cover_comments:"",access_key:"",cover_edited:!1,failed:!1,fail_code:0,data_deleted:!1},init:function(a){},new_file:function(){this.set({status:"new",cost:0,cover_cost:0,num_pages:0,send_authorized:!1,failed:!1,fail_code:0})}}),Jobs=Composer.Collection.extend({model:Job}),Transaction=Composer.Model.extend({}),Transactions=Composer.Collection.extend({model:Transaction});