if(!Number.prototype.toFixed){Number.prototype.toFixed=function(b){var a=Math.pow(10,parseInt(b)),c;a=String(Math.round(this*a)/a);if(b>0){c=a.split(".");c[1]=(c[1]||"")+new Array(++b-(c[1]?c[1].length:0)).join("0");a=c.join(".")}return a}}function focusChanged(a,b){if(b==true){a.form.fileOrUrl[1].checked=true;SetSubmitBtnState()}else{if(a.value.length==0||!validateUrl(a.value)){a.form.fileOrUrl[0].checked=true}SetSubmitBtnState()}}function validateUrl(a){var b=/(http:\/\/|https:\/\/|ftp:\/\/)?.+\..+\/.+\.+\w{2,8}/i;return b.test(a)}function IsReadyForSubmition(){var a=null;if(document.getElementById("radio-btn-file").checked){a=document.getElementById("user-file");if(validateFile(a)){return checkFileType(a,true)}}else{if(document.getElementById("radio-btn-url").checked){a=document.getElementById("user-url");return validateUrl(a.value)}}return false}function validateFile(a){if(a.nodeType&&typeof a.value=="string"&&a.value.length>0){return true}return false}function getSelectedInput(){var a=null;if(document.getElementById("radio-btn-file").checked){a=document.getElementById("user-file");if(validateFile(a)){return a}}if(document.getElementById("radio-btn-url").checked){a=document.getElementById("user-url");if(validateUrl(a.value)){return a}}return null}var UP=null;var Viewer=null;var IsViewerHidden=true;function initializeViewer(){UP=new UploadProgress("filesForm",{additionalFields:["convertTo","originalFormat","convert"],uploadingFields:["user-file","user-url"],beforeSubmit:BeforeSubmit,onProgressChanged:ProgressChanged,onCompleted:Completed});Viewer=new ConvertViewer("viewer",{pageHeight:1169,pageWidth:826,sentinelSize:1200,pageBorderSize:1,imagePrefix:"Lorem",imageExt:".png",imagePath:"/files/web/"})}function GetFilenameFromPath(c){if(typeof c=="string"){var b=c;var a=c.match(/[^\/\\]+$/gi);if(a&&a.length>0){b=a[0]}return decodeURIComponent(b)}return false}function OnChangeFile(c){var a=document.getElementById("fake-file");if(setFilesType(c,false)){c.form.fileOrUrl[0].checked=true;SetSubmitBtnState(true);var b=GetFilenameFromPath(c.value);if(!b){b=c.value}a.value=b}else{SetSubmitBtnState();a.value=""}}function OnChangeUrl(a){if(setFilesType(a,false)){if(a.value.length==0||!validateUrl(a.value)){a.form.fileOrUrl[0].checked=true}SetSubmitBtnState(true)}else{SetSubmitBtnState()}}function SetSubmitBtnState(a){if(typeof a=="undefined"){a=IsReadyForSubmition()}if(a===true){EnableSubmitBtn()}else{DisableSubmitBtn()}return a}function EnableSubmitBtn(){$("#submit").removeAttr("disabled");$("#submit-wrap").removeClass("disabled")}function DisableSubmitBtn(){$("#submit").attr("disabled","disabled");$("#submit-wrap").addClass("disabled")}function BeforeSubmit(){IsViewerHidden=true;var b=GetInputs();if(typeof b!="undefined"&&b.length>0){UP.buildQueue(b)}else{return false}var a=null;var d=document.getElementById("radio-btn-file").checked;if(d){a=$(document.getElementById("user-file"))}else{a=$(document.getElementById("user-url"))}a.clone(true).val((d?null:"")).appendTo(a.parent());SetSubmitBtnState(false);var c=GetFilenameFromPath(a.val());ResetProgressDiv(c);return true}function GetInputs(){var b=new Array();var a=new Array();var f=getSelectedInput();if(f==null){b.push(a);return b}setFilesType(f,false);var c=document.getElementById("originalFormat-input");var d=document.getElementById("convertTo");var e=document.getElementById("convert");a.push(f);a.push($(c).clone());a.push($(d).clone());a.push($(e).clone());b.push(a);return b}function ResetProgressDiv(a){var b="Uploading "+(a.length>0?a:"...");document.getElementById("apcUpload").style.display="block";document.getElementById("total").innerHTML=FormatUploadedSize(0);document.getElementById("loaded").innerHTML=FormatUploadedSize(0);document.getElementById("percentage").style.width=0;document.getElementById("uploading").innerHTML=b}function HideProgressDiv(){document.getElementById("apcUpload").style.display="none"}function ShowViewer(){var a=$("#viewer");if(null!=a&&a.length>0){a.show();IsViewerHidden=false;a[0].redraw()}}function HideViewer(){var a=$("#viewer");if(null!=a&&a.length>0){a.hide();IsViewerHidden=true}}function HideForm(){$("#filesForm").hide()}function ShowForm(){$("#filesForm").show();$("#results").empty()}function EnableForm(){SetSubmitBtnState(true)}function Completed(a){HideProgressDiv();if(a&&(typeof a.error!="undefined"||typeof a.job_info["error"]!="undefined"||typeof a.upload_info["error"]!="undefined")){var b=a.error;if(typeof b=="undefined"||b==null){b=a.upload_info["error"]}if(typeof b=="undefined"||b==null){b=a.job_info["error"]}SetErrorMessage("results",a,b);EnableForm()}else{if(a&&a.job_info&&a.job_info["jobID"]&&a.job_info["key"]){UpdateStatusRunning();RequestNewJobStatus(a.job_info["jobID"],a.job_info["key"])}}}function ProgressChanged(a){if(a&&a.upload_info){ParseUploadResult(document.getElementById("apcUpload"),a.upload_info)}}function ParseUploadResult(g,e,f){var b=0,d=0;if(!g.__rate){g.__rate={time:new Date().getTime(),value:0,percent:0}}if(e&&typeof e=="object"&&e.bytes_uploaded&&e.bytes_total){try{b=parseInt(e.bytes_uploaded);d=parseInt(e.bytes_total)}catch(c){d=0;b=0}}g.__rate.value=b-g.__rate.value;var a=g.__rate.percent;if(b>0&&d>0){g.__rate.percent=Math.round((b*100)/d);document.getElementById("total").innerHTML=FormatUploadedSize(d);document.getElementById("loaded").innerHTML=FormatUploadedSize(b);if(g.__rate.percent>a){$(document.getElementById("percentage")).animate({width:g.__rate.percent+"%"},250)}g.__rate.time=new Date().getTime()}if(f){f()}}function FormatUploadedSize(c){var b=["Bytes","KB","MB","GB","TB","ZB"],a=0;while(c>1024&&++a){c/=1024}return(a?c.toFixed(2):Math.ceil(c))+" "+b[a]}function SetErrorMessage(b,d,e){var f=$(document.getElementById(b));if(f.length>0){var c=(d&&typeof d.filename=="string"?decodeURIComponent(d.filename):"");var a="<div class='conversion-status inline-block failed'></div><span class='filename'>"+c+"</span><span class='status'>"+e+"</span>";f.html(a).show()}}function RequestNewJobStatus(a,b){if(!a){return}setTimeout(function(){jQuery.ajax({async:true,cache:false,type:"GET",url:"./jobStatus.php?jobID="+a+"&view=true&key="+b,success:function(c){if(typeof c==="string"){c=JSON.parse(c);ParseJobStatusResult(c,a,b)}}})},1000)}function ParseJobStatusResult(b,a,d){if(typeof b!="object"&&typeof b.jobStatus=="undefined"){return}switch(b.jobStatus){case"Successful":UpdateViewer(b.outputPath,b.filename,b.page);var c=BuildPermalink(b.outputPath);AppendPermalink(c);UpdateStatusSuccessful();EnableForm();break;case"Running":case"Finalizing":case"Idle":UpdateViewer(b.outputPath,b.filename,b.page);var e=parseInt(b.page);if(e>0){UpdateStatusRunning()}RequestNewJobStatus(a,d);break;default:if(!IsViewerHidden){HideViewer();ShowForm()}EnableForm();UpdateStatusFailed(b.jobError);break}}function UpdateViewer(e,a,b){var c;if(!Viewer){return}if(typeof e=="string"){if(typeof a=="string"&&a.length>0){if(e.charAt(e.length-1)!=="/"){e=decodeURIComponent(e.substring(0,e.lastIndexOf("/")+1));Viewer.setOptions({usePageCounter:false})}c=decodeURIComponent(a)}else{c=GetFilenamePrefixFromPath(e)}Viewer.setOptions({imagePath:e,imagePrefix:c})}if(typeof b!="undefined"){if(b>0&&IsViewerHidden){HideForm();ShowViewer()}var d=parseInt(b)-Viewer.getPageCount();if(d>0){Viewer.appendPages(d)}}}function GetFilenamePrefixFromPath(a){if(typeof a=="string"){if(a.charAt(a.length-1)!=="/"){a+="/"}a=a.substring(a.lastIndexOf("/",a.length-2)+1,a.length-1);a=decodeURIComponent(a)}return a}function UpdateStatusSuccessful(){var a="<a href='/viewer' class='btn-medium'><span class='bgl inline-block'></span><span class='bgc inline-block'>View another file</span><span class='bgr inline-block'></span></a>";UpdateStatus(a)}function UpdateStatusRunning(){var a="<div class='conversion-status inline-block converting'></div><span class='processing'>Processing file ... </span>";UpdateStatus(a)}function UpdateStatusFailed(b){var a="<div class='conversion-status inline-block converting'></div><span>&nbsp;Failed.";if(typeof b=="string"&&b.length>0){a+=" "+b}a+="</span>";UpdateStatus(a)}function UpdateStatus(a){var b=null;if(IsViewerHidden){b=$("#results")}else{b=$("#viewer-status")}if(b.length>0){b.show();if(a!=b.html()){b.html(a).show()}}}function BuildPermalink(b){var c=b.split("/");var a=c.length-1;if(c[a]==""){a--}if(a>0){a--}return"http://www.docspal.com/viewer?id="+c[a]}var isPermalinkVisible=false;function AppendPermalink(b){var c="<a href='#' onclick='TogglePernalinkPanel(); return false;' class='btn-medium'><span class='bgl inline-block'></span><span class='bgc inline-block'>Link</span><span class='bgr inline-block'></span></a><div id='link-panel' style='display:none;'><a href='#' id='close-link-panel' onclick='HidePermalinkPanel();return false;'></a><label for='link-url'>Paste link in <b>email</b> or <b>IM</b></label><div class='url-wrap'><input id='link-url' name='url' value='"+b+"' onclick='this.focus();this.select();' /></div></div>";var a=document.getElementById("get-link");a.innerHTML=c}function TogglePernalinkPanel(){if(isPermalinkVisible){HidePermalinkPanel()}else{ShowPermalinkPanel()}}function ShowPermalinkPanel(){var a=document.getElementById("link-panel");if(a&&a.nodeType){a.style.display="block";var b=document.getElementById("link-url");b.focus();b.select();isPermalinkVisible=true}}function HidePermalinkPanel(){var a=document.getElementById("link-panel");if(a&&a.nodeType){a.style.display="none";isPermalinkVisible=false}};
