﻿/**
*
*  uno.upload.js 
*  by Hideki Yamamoto eu-gsm[+392]
*  Version Alpha 0.0.4.0
*  Part of [uno-js] project.
*
*  Released by Hideki Yamamoto - LGPL equiv.
*
*
**/
Namespace.register("uno.upload");
uno.upload = {};
uno.upload.file = function(url,elm,onstart,onfinish){
    var tform = uno.upload._createform(); 
    tform.appendChild($(elm));
	document.body.appendChild(tform);
	uno.upload.form(url,tform,onstart,onfinish);}
uno.upload.inputs = function(url,elm,onstart,onfinish){    
    var onode = $(elm);
    var nnode = onode.cloneNode(true);
    onode.parentNode.replaceChild(nnode,onode);
    var tform = uno.upload._createform();
    tform.appendChild(onode);    
	document.body.appendChild(tform);
	uno.upload.form(url,tform,onstart,onfinish);};
uno.upload.form = function(url, tform, onstart, onfinish) {
	    this.prepform = function(f, onfinish) { return this._prepform(f, this.prepframe(onfinish)); };
	    this._prepform = function(f, name) { f.setAttribute('target', name); return name; };
	    this.prepframe = function(c) {
	        var n = uno.getuqid();
	        var d = document.createElement('DIV');
	        d.innerHTML = '<iframe style="display:none" src="about:blank" id="' + n + '" name="' + n + '" onload="uno.upload._frameloaded(\'' + n + '\')"></iframe>';
	        document.body.appendChild(d);
	        var ifr = document.getElementById(n);
	        ifr.onComplete = c;
	        return n;};
	    tform = $(tform);
	    tform.setAttribute('action', url);
	    if (isIE){tform.getAttributeNode("enctype").value = "multipart/form-data";
	              tform.getAttributeNode("method").value = "POST";
	    }else{tform.setAttribute('enctype', 'multipart/form-data');
	          tform.setAttribute('method', 'POST');}
	    var fname = this.prepform(tform, onfinish);
	    if (onstart && typeof (onstart) == 'function') {
	        try { onstart(fname); } catch (exx) {
	            try { onstart(); } catch (ex) {
	                //TODO: aggiungere stringe errori
	                var msg = 'Could not invoke the onstart handler in any of the following way.Set it to false to not have it executed./n';
	                msg += '- onstart(fname)./n';
	                msg += '- onstart()./n';
	                uno.throwerror(msg);
        }   }   }
	    tform.submit();
	    return fname;
	};    
uno.upload._frameloaded = function(id){
 var i = document.getElementById(id);
 var d = false;
 if (i.contentDocument){d = i.contentDocument;}
 else if (i.contentWindow){d = i.contentWindow.document;}
 //else {d = window.frames[id].document;}
 if (d){
  if (d.location.href == "about:blank"){return;}
  if (i.onComplete && typeof(i.onComplete) == 'function') {
   try{i.onComplete();}catch(exxxx){
   try{i.onComplete(i.id,d);}catch(exxx){
   try{i.onComplete(d);}catch(exx){
   try{i.onComplete(d.body.innerHTML);}catch(ex){   
    //TODO: aggiungere stringhe errori
    var msg = 'Could not invoke the onfinish handler in any of the following way.Set it to false to not have it executed./n';
    msg += '- onfinish(iframe.contentDocument OR contentWindow.document)./n';
    msg += '- onfinish(iframe.body.innerHTML)./n';
    msg += '- onfinish()./n';
    uno.throwerror(msg);
  }}}}}else if (typeof(i.onComplete) == 'string'){
   try{eval(i.onComplete);}catch(ex){
    uno.throwerror('Could not eval the onfinish javascript,Try setting it to a function object instead of a string. Set it to false to not have it executed.');
  }}else{uno.throwerror('Could not invoke the onfinish handler, only function and string types are supported as onfinish handlers.');
 }}else{uno.throwerror('Could not retrieve a document reference for the updated iframe.'); 
}};
uno.upload._createform = function(){
    var tform = document.createElement('form');
	if (isIE){tform.getAttributeNode("enctype").value = "multipart/form-data";
	        tform.getAttributeNode("method").value = "POST";
    	    var fakedata = document.createElement('input');
	        fakedata.setAttribute('type','hidden');
		    fakedata.id='fkedte';fakedata.name='fkedte';fakedata.value='hy';
	        tform.appendChild(fakedata);
	}else{tform.setAttribute('enctype','multipart/form-data');
	      tform.setAttribute('method','POST');}
    tform.style.display='none';
    return tform;
};


uno.upload.appendhtml = function(target,url,onstart,onfinish,cssclass){
    var nfile = document.createElement('input');
    nfile.setAttribute('type','file');
    if (cssclass){
        nfile.setAttribute('class',cssclass);
        nfile.setAttribute("onchange","uno.upload._dhtmlreplace(this,'" + url + "',"+onstart.name+","+onfinish.name+")");
    }else{
        nfile.setAttribute('onchange','uno.upload._dhtmlreplace(this,\'' + url + '\','+onstart.name+','+onfinish.name+',false)');  
    }
    nfile.id = uno.getuqid();
    nfile.name = nfile.id;
    
    $(target).appendChild(nfile);};
uno.upload._dhtmlreplace = function(ifile,url,onstart,onfinish,cssclass){
    var nfile = document.createElement('input');
    nfile.setAttribute('type','file');
    nfile.setAttribute("onchange","uno.upload._dhtmlreplace(this,'" + url + "',"+onstart.name+","+onfinish.name+");");
    nfile.id = uno.getuqid();
    nfile.name = nfile.id;
    ifile.parentNode.replaceChild(nfile,ifile);
    ifile.removeAttribute("onchange");
    ifile.style.display = 'none';
    uno.upload.file(destination,ifile, onstart, onfinish);
};

//uno.upload.appendflash = function(parent,url,width,height,callbackJS){uno.upload.xappendflash(parent,url,width,height,callbackJS);};
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////PRIVATE IMPLEMENTATION DETAILS//////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
uno.upload.flashtml = function(url,width,height,CompleteCallbackJS){
    var divhtml = '';
    if (isIE6){divhtml += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" id="int_FLASHupload" >\n';
               divhtml += '<param name="movie" value="scripts/uno/flashupload.swf"" />';}
    else{divhtml += '<object data="scripts/uno/flashupload.swf" width="'+width+'" height="'+height+'" name="'+uno.getuqid()+'" type="application/x-shockwave-flash">\n';}
    divhtml += '<param name="FlashVars" value="uploadurl=' + url + '&lastjavascript=' + CompleteCallbackJS + '" />\n';
    divhtml += '<param name="quality" value="high"/>\n';
    divhtml += '<param name="bgcolor" value="#FFF"/>\n';
    divhtml += '<param name="allowScriptAccess" value="always"/>\n';
    divhtml += '<param name="play" value="true"/>\n';
    divhtml += '<param name="loop" value="false"/>\n';
    divhtml += '<param name="pluginspage" value="http://www.adobe.com/go/getflashplayer"/>\n';
    divhtml += '</object>';
    return divhtml;};