function uploadfile(){
 $('filefield').click();
} //function





function formathistory(){
 if (!document.getElementById) { alert('No way to traverse DOM'); return; }
 var daList = document.getElementById("filelist");
 if (!daList) { alert('Missing File Listing'); return; }
 var daItems = daList.getElementsByTagName("DIV");
 if (!daItems) { alert('No files found!'); return; }
 for (iItem=0; iItem < daItems.length; iItem++){
  var o=daItems[iItem];
  if (o.innerHTML == '') {
   var a=o.className.split(' ');
   var q=o.id;
   var nm=getthename(q);
// o.ondblclick = doubleclick;
   o.onclick = function(){
                if (  !($(this.id.substr(1,this.id.length)))    ) {
                 if (this.className){
                  var dest=getthename(this.id);
                  var c=this.className.split(' ');
                  if (c[0]=='nor')
                   {this.className=this.className.replace('nor','sel');
                   } else {
                    this.className=this.className.replace('sel','nor');
                   }
                 }
                 $('showfile').value=this.id.substr(1,this.id.length);
                 runForm('showfileform');
                 var newdiv = document.createElement("div");
                 newdiv.addClassName('block');
                 newdiv.id=this.id.substr(1,this.id.length);
                 $('content').insertBefore(newdiv,$('content').lastChild).innerHTML='<table width="100%" height="100%"><tr><td align="center" valign="center">'+this.id.substr(1,this.id.length)+'</td></tr></table>';
                } else {
                 new Effect.Highlight($(this.id.substr(1,this.id.length)), { startcolor: '#c4d9c4', endcolor: '#eeeeee' });
                }
               };
   newSpan=document.createElement('SPAN');
   newText=document.createTextNode(' '+nm);
   newimg=document.createElement('IMG');
   newimg.src='/clearimage/images/'+a[2].toLowerCase()+'-small.gif';
// newimg.onclick=iconclick;
   newSpan.appendChild(newimg);
   newSpan.appendChild(newText);
   o.appendChild(newSpan);
  }


 }





}





function aestivafilename(myfile){
 var temp="";
 var y="";
 var i="";
 var x="";
 // This function will take a file name uploaded from a client machine and convert it to conform to the File naming conventions used by Aestiva
 temp=myfile;
 y="";
 for (i=0; i < temp.length; i++) {
  x=temp.charCodeAt(i);
  // Allowed Characters (0-9)(a-z)(A-Z).-  everything else is changed to an undescore.
  // ASCII Chart can be found at http://www.jimprice.com/ascii-0-127.gif
  if ((x>47 && x<58) || (x>64 && x<91) || (x>96 && x<123) || x==46 || x==45){} else {x=95;}
  y=y+String.fromCharCode(x);
 }
 return y;
} //function





function removeElement(ele){
 if ($('F'+ele.id)) {$('F'+ele.id).className=$('F'+ele.id).className.replace('sel','nor');}
 if(ele.parentNode && ele.nodeType){
  ele.parentNode.removeChild(ele);
  return true;
 } else {
  return false;// not an element or no parent :(
 }
} //function





function pasteclipboard(){
 var newdivid = getTimeStamp();
 var newdiv = document.createElement("div");
 newdiv.addClassName('block');
 newdiv.id=newdivid;
 $('content').insertBefore(newdiv,$('content').firstChild).innerHTML='<div style="position: absolute; left: 8px; top: 40px; border: 0px solid #000000;"><applet id="p8ste_applet" code="PasteImageApplet" archive="'+p8steJavaAppletURL+'" width="290" height="60" mayscript="true"></applet></div>';
 $(newdivid).style.backgroundColor='#a7a7a7';
 intervalID = setInterval(function(){pasteit(newdivid);}, 500);
} //function




function stoptextpaste(newdivid){
  clearInterval(hasPaste);
  removeElement($(newdivid));
}

function pastecheck(newdivid){
 if ($('mypastetext').value=="") {
 } else {
  clearInterval(hasPaste);
  $('mypastetext').select();
  $('textpasteinfo').innerHTML='Pasting Text Clipboard...';
//  $('pastetextsubmit').click();
  runForm('pastetextform');
 }
}


function pasteit(newdivid){
 obj = $('p8ste_applet');

// if ( obj.getClipboardImageURL ) {
//  alert('1');
// } else {
//  alert('2');
// }

// if (typeof obj.getClipboardImageURL == "function") {
   if (obj && obj != null) {
    clearInterval(intervalID);
    postTo= $('uploadclipboard').action;
    image = obj.getClipboardImageURL(postTo,"myclipboard","")
    if (image==''){
     $(newdivid).innerHTML='<div style="position: absolute; left: 12px; top: 3px; border: 0px solid #000000;"><a href="javascript:stoptextpaste();" style="position: relative; top:0; left: 0;">x</a></div><div id="textpasteinfo" style="position: absolute; left: 10px; bottom: 4px; width: 290px; border: 0px solid #000000; text-align: center; overflow: hidden;">p8ste the text contents of your clipboard here.</div><div style="position: absolute; left: 5px; top: 30px; border: 0px solid #000000;"><form method="post" action="'+submittexturl+'" class="ajaxrequest" id="pastetextform"><textarea name="mypastetext" id="mypastetext" value="" style="width:294px; height: 256px;"></textarea><input type="hidden" name="pastetextdiv" id="pastetextdiv" value="'+newdivid+'"></form>';
     ajaxForms('ajaxrequest');
     $('mypastetext').focus();
     $(newdivid).style.backgroundColor='#c4d9c4';
     hasPaste = setInterval(function(){pastecheck(newdivid);}, 500);
    } else {
     finishP8ste(image,newdivid);
    }
 }
} //function




function finishP8ste(newdivid,olddivid){
 removeElement($(olddivid));
 $('showfile').value=newdivid;
 var newdiv = document.createElement("div");
 newdiv.addClassName('block');
 newdiv.id=newdivid;
 $('content').insertBefore(newdiv,$('content').lastChild).innerHTML='<div style="position: absolute; left: 30px; top: 3px; border: 0px solid #000000; width: 250px; text-align: center;">loading...</div><div style="position: absolute; left: 5px; top: 80px; border: 0px solid #000000;"><div style="width: 295px; height: 15px; margin: 0; padding:0; border: 1px solid #444444;"><div style="width: 0; height: 15px; background-color: #05e210; background-position: 0 0; background-repeat: no-repeat; background-image: url(\'/pasteboard/images/uploadspinner.gif\');" id="formx_progress"></div></div></div><div style="position: absolute; left: 10px; bottom: 4px; width: 290px; border: 0px solid #000000; text-align: center; overflow: hidden;">'+newdivid+'</div>';
 runForm('showfileform');
}




function addP8ste(id,s,p,k){
 if ($('F'+id)==null){
  var newdiv = document.createElement("div");
  newdiv.addClassName(s);
  newdiv.addClassName(p);
  newdiv.addClassName(k);
  newdiv.id='F'+id;
  $('filelist').insertBefore(newdiv,$('filelist').firstChild);
  formathistory();
 } else {
  if (s=='sel'){$('F'+id).className=$('F'+id).className.replace('nor','sel');}
  if (s=='nor'){$('F'+id).className=$('F'+id).className.replace('sel','nor');}
  formathistory();
 }
}







function getTimeStamp(){
 var d = new Date();
 return right('X0'+d.getMonth(),2)+'-'+right('X0'+d.getDay(),2)+'-'+right('X0'+d.getFullYear(),4)+'_'+right('X0'+d.getHours(),2)+'_'+right('X0'+d.getMinutes(),2)+'_'+right('X0'+d.getSeconds(),2);
}










function getthename(n){
 if (n.substr(0,1)=='F')
   { var dest=n.substr(1); }
 else
  { var dest=n;
    if (dest=='parent')  { dest='..'; }
    if (dest=='current') { dest='.';  }
  }
 return dest;
} //function





function right(t,x) { return t.substr(t.length-x) ; }
























/*********
* Modifications made by Terry Riegel August 3, 2006
* to work with HTMLOS scripting engine.
*
* Mofified by Terry Riegel November 12, 2007 to work with
* updated ajax calls from thomas.epineer.se
* and also removed the prototype.js dependencies
* Works with Aestiva's HTML/OS, and
* Clear Image Media's Image Services.
* also modified ajax code for compatability with Safari 3.
*
*
* Javascript for file upload demo
* Copyright (C) Tomas Larsson 2006
* http://tomas.epineer.se/
*
* Licence:
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
* 
* Software distributed under this License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
*/





function beginUpload(updateurl){
    document.formx.submit();
    var pb = document.getElementById('formx_progress');
//  pb.parentNode.style.display='';
//  t.style.display='none';
    checkupload = new PeriodicalAjax(updateurl,'',4,2,function(request){updateProgress(pb,request)},function(request){updateFailure(pb,request)})
}





function updateProgress(pb,req)
 {
  var a = req.responseText.split("|");
  var percent = parseInt(a[0]);
  var text = a[1];
  if(!percent) percent = 0;
  pb.style.width = percent + "%";
  if (percent == 100){
   pb.style.backgroundImage="url(/pasteboard/images/uploadspinner.gif)";
  } else {
   pb.style.backgroundImage="";
  }
  // If IE just progress, others get progress with text
  if (navigator.appName=='Microsoft Internet Explorer'){} else {pb.innerHTML = text.replace(/\ /g,'&nbsp;');}
 }





function updateFailure(pb,req) {
  var mes = req.responseText;
  pb.style.width=100 + "%";
  pb.innerHTML = mes;
}





function PeriodicalAjax(url, parameters, frequency, decay, onSuccess, onFailure) {
 function createRequestObject() {
  var xhr;
  try {
   xhr = new XMLHttpRequest();
  }
  catch (e) {
   xhr = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xhr;
 }	
 function send() {
  if(!stopped) {
   xhr.open('post', url, false);
   xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
   xhr.onreadystatechange = function() { self.onComplete(); };
   xhr.send(parameters);
  }
 }
 this.stop = function() {
  stopped = true;
  clearTimeout(this.timer);
 }
 this.start = function() {
  stopped = false;
  this.onTimerEvent();
 }	
 this.onComplete = function() {
  if(this.stopped) return false;
  if ( xhr.readyState == 4) {
   if(xhr.status == 200) {
    if(xhr.responseText == lastResponse) {
     decay = decay * originalDecay;
    } else {
     decay = 1;
    }
    lastResponse = xhr.responseText;
    if(onSuccess instanceof Function) {
     onSuccess(xhr);
    }
    this.timer = setTimeout(function() { self.onTimerEvent(); }, decay * frequency * 1000);
   } else {
    if(onFailure instanceof Function) {
     onFailure(xhr);
    }
   }
  }
 }
 this.getResponse = function() {
  if(xhr.responseText) {
   return xhr.responseText;
  }
 }
 this.onTimerEvent = function() {
  send();
 }
 var self = this;
 var stopped = false;
 var originalDecay = decay || 1.2;
 decay = originalDecay;
 var xhr = createRequestObject();
 var lastResponse = "";
 this.start();
}