JSAttachmentPlaceConfig.prototype=new JSComponentConfig();var AttachmentPlaceConfig=new JSAttachmentPlaceConfig();function JSAttachmentsFactory(){this.version="1.1.20";if(!Global.required("JSTemplate","JSArrayList","JSHashMap","JSDialog")){this.createPlace=function(){};this.removeAttachment=function(){};this.addAttachment=function(){};return null}this.places=[];this.attachmentWindow=null;function createPlace(templateBlockName,attachment){var p=new JSAttachmentPlace(templateBlockName,attachment);this.places.push(p);return p}this.createPlace=createPlace;function removeAttachment(name){for(var p=0;p<this.places.length;p++)this.places[p].removeAttachment(name)}this.removeAttachment=removeAttachment;function removeAllAttachment(){this.currentPlace.removeAllAttachment()}this.removeAllAttachment=removeAllAttachment;function openAttachmentWindow(placeName){var p=this.getPlace(placeName);if(p){this.currentPlace=p;p.openAttachmentWindow()}}this.openAttachmentWindow=openAttachmentWindow;function addAttachment(attachment){this.currentPlace.addAttachment(attachment)}this.addAttachment=addAttachment;function synchronize(attachments){this.currentPlace.synchronize(attachments)}this.synchronize=synchronize;function getPlace(placeName){for(var p=0;p<this.places.length;p++)if(this.places[p].placeName==placeName)return this.places[p];return null}this.getPlace=getPlace}var AttachmentPlace=new JSAttachmentsFactory();function JSAttachmentPlace(templateBlockName,attachment){this.version="1.1.20";if(!Global.required("JSTemplate","JSArrayList","JSHashMap","JSDialog")){this.create=function(){};return null}this.template=new JSTemplate();this.template.loadTemplateContent("main",AttachmentPlaceConfig.scriptPath+""+AttachmentPlaceConfig.htmlFile,true);this.attachment=attachment;this.templateBlockName=templateBlockName;this.emptyHTML="";this.vars=["name","size","type","previewURL","downloadURL","deleteURL","removeAttachment","addAttachment","openAttachWindow"];this.uniqueId=new Date().getTime();this.placeName="AttachmentPlace"+this.uniqueId;function create(){if(this.attachment.instanceOf(Array)){var d=document;d.write("<div id='"+this.placeName+"' style='border:solid 0px red'>");var tpl=this.template;if(this.attachment.length>0&&!this.attachment[this.attachment.length-1].name)this.attachment.pop();if(this.attachment.length==0){tpl.clearVar(this.templateBlockName+"AttachmentList");tpl.parse(this.templateBlockName+"AttachmentListEmpty",false);this.emptyHTML=tpl.printVar(this.templateBlockName+"AttachmentListEmpty")}else{tpl.parse(this.templateBlockName+"AttachmentListEmpty",false);this.emptyHTML=tpl.printVar(this.templateBlockName+"AttachmentListEmpty");tpl.clearVar(this.templateBlockName+"AttachmentListEmpty");var items="";for(var a=0;a<this.attachment.length;a++){var att=this.attachment[a];for(var i in att){if(i=="size")att[i]=this.formatBytes(att[i]);tpl.setVar(i,att[i])}tpl.setVar("removeAttachment","AttachmentPlace.removeAttachment(\""+att.name+"_"+this.placeName+"\")");tpl.parse(this.templateBlockName+"AttachmentListItem",false);for(var v=0;v<this.vars.length;v++)tpl.clearVar(this.vars[v]);items+="<span name='"+att.name+"_"+this.placeName+"'>"+tpl.printVar(this.templateBlockName+"AttachmentListItem")+"</span>"}tpl.setVar(this.templateBlockName+"AttachmentListItem","<SPAN style='border:solid 0px green'>"+items+"</SPAN>");tpl.parse(this.templateBlockName+"AttachmentList",false)}tpl.setVar("openAttachWindow","AttachmentPlace.openAttachmentWindow(\""+this.placeName+"\" )");tpl.parse(this.templateBlockName,false);d.write(tpl.printVar(this.templateBlockName));d.write("</div>")}else this.usage()}this.create=create;function getPlaceElement(){return Global.getObject("AttachmentPlace"+this.uniqueId)}this.getPlaceElement=getPlaceElement;function addAttachment(attachment){var newName=attachment.name+"_"+this.placeName;var tpl=this.template;for(var i in attachment){if(i=="size")attachment[i]=this.formatBytes(attachment[i]);tpl.setVar(i,attachment[i])}tpl.setVar("removeAttachment","AttachmentPlace.removeAttachment(\""+attachment.name+"_"+this.placeName+"\")");tpl.parse(this.templateBlockName+"AttachmentListItem",false);for(var v=0;v<this.vars.length;v++)tpl.clearVar(this.vars[v]);var span=document.createElement("SPAN");span.name=newName;span.innerHTML=tpl.printVar(this.templateBlockName+"AttachmentListItem");var place=this.getPlaceElement();if(place&&place.childNodes&&place.childNodes[0]){var emptyId=this.placeName+"_empty";if(place.childNodes[0].id==emptyId)place.childNodes[0].removeNode(true);var itemContainer=place.childNodes[0];var fileExistsMessage="File '"+attachment.name+"' already exists!\t";for(var i=0;i<itemContainer.childNodes.length;i++){if(itemContainer.childNodes[i]&&itemContainer.childNodes[i].getAttribute){var existsName=itemContainer.childNodes[i].getAttribute("name");if(newName==existsName){if(this.attachmentWindow)this.attachmentWindow.alert(fileExistsMessage);else alert(fileExistsMessage);return null}}}if(AttachmentPlaceConfig.append)itemContainer.appendChild(span);else itemContainer.insertBefore(span,itemContainer.firstChild)}}this.addAttachment=addAttachment;function removeAttachment(name){var p=this.getPlaceElement();if(p&&p.childNodes&&p.childNodes[0]&&p.childNodes[0].childNodes){var k=p.childNodes[0].childNodes;for(var c=0;c<k.length;c++){if(k[c]){var n=k[c].getAttribute("name");if(name==n)k[c--].removeNode(true)}}if(k.length==0)p.innerHTML=this.emptyHTML+p.innerHTML}}this.removeAttachment=removeAttachment;function removeAllAttachment(){var p=this.getPlaceElement();if(p&&p.childNodes&&p.childNodes[0]&&p.childNodes[0].childNodes){var k=p.childNodes[0].childNodes;for(var c=0;c<k.length;c++)if(k[c])k[c--].removeNode(true);if(k.length==0)p.innerHTML=this.emptyHTML+p.innerHTML}}this.removeAllAttachment=removeAllAttachment;function synchronize(attachments){var att=attachments;var p=this.getPlaceElement();if(p&&p.childNodes&&p.childNodes[0]&&p.childNodes[0].childNodes){var k=p.childNodes[0].childNodes;if(k){for(var c=0;c<k.length;c++){if(k[c]&&k[c].getAttribute){var cn=k[c].getAttribute("name");var e=false;for(var a=0;a<att.length;a++){var n=att[a].name+"_"+this.placeName;if(cn==n){e=true;break}}if(!e)k[c--].removeNode(true)}}for(var a=0;a<att.length;a++){var n=att[a].name+"_"+this.placeName;var e=false;for(var c=0;c<k.length;c++){if(k[c]&&k[c].getAttribute){var cn=k[c].getAttribute("name");if(cn==n){e=true;break}}}if(!e)this.addAttachment(att[a])}if(k.length==0)p.innerHTML=this.emptyHTML+""+p.innerHTML;else p.innerHTML=String(p.innerHTML).replace(this.emptyHTML,"")}}}this.synchronize=synchronize;function usage(){alert("Error!\t")}this.usage=usage;function openAttachmentWindow(){this.attachmentWindow=Dialog.showDialog(AttachmentPlaceConfig.attachmentManagerURL,AttachmentPlaceConfig.attachmentManagerOpenParams);AttachmentPlace.attachmentWindow=this.attachmentWindow;this.attachmentWindow.owner=AttachmentPlace}this.openAttachmentWindow=openAttachmentWindow;function formatBytes(bytes){var b=parseInt(bytes);var q="B";if(b>1024){b=b/1024;q="KB"}if(b>1024){b=b/1024;q="MB"}b=String(b);var d=b.indexOf(".");if(d!=-1){b=b.replace(/\./,",");b=b.substr(0,d+3)}return b+"&nbsp;"+q}this.formatBytes=formatBytes;this.create(templateBlockName,attachment)}