var fieldNum=0;var isBusy=false;var busySlotId=-1;var timeouts=[];var LOAD_WAIT_TIMEOUT=3000;var g_stop=false;function registerTimeoutFunction(A,F,C,E){var B={func:A,slotId:F,type:C,timeoutId:E};for(var D=0;D<timeouts.length;D++){if(timeouts[D].slotId==F&&timeouts[D].type==C){timeouts[D]=B;return }}timeouts[timeouts.length]=B}function getTimeoutFunction(C,A){for(var B=0;B<timeouts.length;B++){if(timeouts[B].slotId==C&&timeouts[B].type==A){return timeouts[B].func}}return null}function isTimeoutFunctionExists(C,A){for(var B=0;B<timeouts.length;B++){if(timeouts[B].slotId==C&&timeouts[B].type==A){return true}}return false}function resetSlotTimeout(C,A){for(var B=0;B<timeouts.length;B++){if(timeouts[B].slotId==C&&timeouts[B].type==A){clearTimeout(timeouts[B].timeoutId);return }}}function initUpload(B){fieldNum=B;var A="entry"+B;addOnChangeHandlers(A,B);addOnFocusBlurHandlers(A,B)}function setBusyFlag(A){busySlotId=A;isBusy=true}function resetBusyFlag(A){if(busySlotId==A){isBusy=false}}function getFieldCode(){var A='<input type="hidden" id="switch'+fieldNum+'" name="switch'+fieldNum+'" value="0" /><div class="switch" onclick="switchField('+fieldNum+');"></div><div class="fields_file"><input name="file'+fieldNum+'" type="file" size="48" /></div><div class="fields_url"><input name="url'+fieldNum+'" type="text" /></div><div class="fields_msg">Uploading...</div><div class="controls"><div class="minus" onclick="removeField('+fieldNum+')"></div><div class="plus"><div class="plus1" onclick="addField(1);"></div><div class="plus2" onclick="addField(5);"></div></div></div>';return A}function setEntryMessage(D,B){var A="entry"+B;var C=$(A).getElement(".fields_msg");if(C){C.innerHTML=D;$(A).className="entry_msg"}else{alert("element not found, please contact support")}}function getInitialTimeoutFunction(elementName,elementIndex,element,fieldType){var sessionElement=$("SessionId");var minusElement=$(elementName).getElement(".minus");var func=function(){var fileTimeoutFunction=function(){if(isBusy){var elementFunction=null;if(fieldType==1){elementFunction=function(){element.onchange()}}else{if(fieldType==2){elementFunction=function(){element.onBlur()}}else{alert("exception in getInitialTimeoutFunction: unsupported type");return }}var timeoutId=window.setTimeout(func,1000);registerTimeoutFunction(elementFunction,elementIndex,fieldType,timeoutId);return }setBusyFlag(elementIndex);var fileFrame=null;if(window.ActiveXObject){fileFrame=document.createElement('<iframe id="fileFrame'+elementIndex+'" name="fileFrame'+elementIndex+'" class="invisible" />');fileFrame.src="javascript:false"}else{fileFrame=document.createElement("iframe");fileFrame.setAttribute("id","fileFrame"+elementIndex);fileFrame.setAttribute("name","fileFrame"+elementIndex);fileFrame.setAttribute("class","invisible");fileFrame.setAttribute("src","javascript:false")}var fileForm=document.createElement("form");fileForm.setAttribute("method","POST");fileForm.setAttribute("target","fileFrame"+elementIndex);fileForm.setAttribute("action","/uploadfile.aspx");fileForm.setAttribute("class","invisible");if(fileForm.encoding){fileForm.encoding="multipart/form-data"}else{fileForm.enctype="multipart/form-data"}fileForm.appendChild(element);var newSessionElement=document.createElement("input");newSessionElement.setAttribute("type","hidden");newSessionElement.setAttribute("id","sessionId"+elementIndex);newSessionElement.setAttribute("name","sessionId"+elementIndex);newSessionElement.setAttribute("value",sessionElement.value);fileForm.appendChild(newSessionElement);document.body.appendChild(fileForm);document.body.appendChild(fileFrame);$(elementName).className="entry_msg";window.setTimeout(function(){if(fileFrame.attachEvent){fileFrame.attachEvent("onload",uploadCallback)}else{if(fileFrame.addEventListener){fileFrame.addEventListener("load",uploadCallback,false)}}function uploadCallback(a){if(g_stop){return }var response=null;try{var responseText=null;if(fileFrame.contentWindow){responseText=fileFrame.contentWindow.document.body?fileFrame.contentWindow.document.body.innerHTML:null}else{if(fileFrame.contentDocument){responseText=fileFrame.contentDocument.document.body?fileFrame.contentDocument.document.body.innerHTML:null}}response=eval("("+responseText+")")}catch(e){}if(response&&response.status=="ok"){var html1='<a href="'+response.filePath+'" target="_blank">'+response.fileName+"</a> &nbsp; &nbsp; &nbsp; "+response.fileSize;var html_input_field='<input type="hidden" name="includeId'+elementIndex+'" value="'+response.id+'" />';setEntryMessage(html1+html_input_field,elementIndex);if(response.newSessionId){$("SessionId").value=response.newSessionId}}else{if(response&&response.status=="err"){setEntryMessage(response.msg,elementIndex)}else{var html2="Unknown error, contact support please";setEntryMessage(html2,elementIndex)}}if(fileFrame.detachEvent){fileFrame.detachEvent("onload",uploadCallback)}else{if(fileFrame.removeEventListener){fileFrame.removeEventListener("load",uploadCallback,false)}}setTimeout(function(){try{document.body.removeChild(fileFrame);document.body.removeChild(fileForm)}catch(e){}},200);resetBusyFlag(elementIndex)}},500);setTimeout(function(){fileForm.submit()},100)};var timeoutId=window.setTimeout(fileTimeoutFunction,LOAD_WAIT_TIMEOUT);registerTimeoutFunction(fileTimeoutFunction,elementIndex,fieldType,timeoutId);minusElement.onclick=null;var newMinusOnClickFunction=function(){resetBusyFlag(elementIndex);resetSlotTimeout(elementIndex,fieldType);removeField(elementIndex)};if(minusElement.attachEvent){minusElement.attachEvent("onclick",newMinusOnClickFunction)}else{if(minusElement.addEventListener){minusElement.addEventListener("click",newMinusOnClickFunction,false)}}};return func}function addOnChangeHandlers(A,C){var B=$E("input[type=file]",A);B.onchange=getInitialTimeoutFunction(A,C,B,1)}function addOnFocusBlurHandlers(A,C){var B=$E("input[type=text]",A);B.onblur=function(){checkTextField(C)};B.onfocus=function(){resetSlotTimeout(C,2)}}function addField(E){var B=$("uploads");for(var D=0;D<E;D++){fieldNum++;var A="entry"+fieldNum;var C=document.createElement("div");C.setAttribute("id",A);C.className="entry_file";C.innerHTML=getFieldCode();$("uploads").appendChild(C);addOnChangeHandlers(A,fieldNum);addOnFocusBlurHandlers(A,fieldNum);handleSigns()}}function handleSigns(){handlePlusSign();handleMinusSign()}function handlePlusSign(){var C="";for(var B=0;B<=fieldNum;B++){var A="entry"+B;if($(A)){$(A).getElement(".plus").style.visibility="hidden";C=A}}$(C).getElement(".plus").style.visibility=""}function handleMinusSign(){var B="";var D=0;for(var C=0;C<=fieldNum;C++){var A="entry"+C;if($(A)){if(B==""){B=A}$(A).getElement(".minus").style.visibility="";D++}}if(D==1){$(B).getElement(".minus").style.visibility="hidden"}}function removeField(C){var B=$("uploads");var A="entry"+C;var D=$(A);B.removeChild(D);handleSigns()}function switchField(A){var F="entry"+A;var D=-1;var C=-1;if($(F).className=="entry_file"){$(F).className="entry_url";D=1;C=2;$("switch"+A).value="1"}else{if($(F).className=="entry_url"){$(F).className="entry_file";D=2;C=1;$("switch"+A).value="0"}else{return }}resetSlotTimeout(A,D);if(isTimeoutFunctionExists(A,C)){var B=getTimeoutFunction(A,C);var E=setTimeout(B,LOAD_WAIT_TIMEOUT);registerTimeoutFunction(B,A,C,E)}else{resetBusyFlag(A)}}function checkLink(B){var A=B.value;if(A.length==0){return -1}if(A.test("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?")&&A.length>0){return 1}else{return 0}}function checkTextField(C){var B="entry"+C;var E=$E("input[type=text]",B);var A=checkLink(E);if(A==1){E.removeClass("field_hl");resetSlotTimeout(C,2);var D=getInitialTimeoutFunction(B,C,E,2);D()}else{if(A==0){resetSlotTimeout(C,2);E.addClass("field_hl")}}}function onOkayButtonHandler(){g_stop=true;var D=0;for(D=0;D<timeouts.length;D++){clearTimeout(timeouts[D].timeoutId)}var A=document.forms[0];for(D=1;D<document.forms.length;D++){var E=$ES("input",document.forms[D]);for(var C=0;C<E.length;C++){var B=E[C].getProperty("type").toLowerCase();if(B=="file"||B=="text"){E[C].className="invisible";A.appendChild(E[C])}}}return true};