﻿/**
*
*  uno.ajax.js 
*  by Hideki Yamamoto eu-gsm[+393453332248]
*  Version Alpha 0.0.3.0
*  This file is part of the uno project.
*  The user must agree with the terms specified in
*
*  ©Hideki Yamamoto, all rights reserved.
*
*  Licence       : Http://www.unodot.net/framework/current/uno.licence.txt
*  Documentation : Http://www.unodot.net/framework/current/uno.js.usage.txt
*  History       : Http://www.unodot.net/framework/current/uno.js.history.txt
*
**/
Namespace.register("uno.upload");
if (!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 tform = uno.upload._createform();
    var onode = $(elm);
    var nnode = onode.cloneNode(true);
    onode.parentNode.replaceChild(nnode,onode);
    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;
	};
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////PRIVATE IMPLEMENTATION DETAILS//////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
	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(i.id, d); } catch (exxxx) {
	                alert(exxxx.message);
	                try { i.onComplete(d); } catch (exxx) {
	                    try { i.onComplete(d.body.innerHTML); } catch (exx) {
	                        try { i.onComplete(); } catch (ex) {
	                            //TODO: aggiungere stringe 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){
//    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;
//    $(target).appendChild(nfile);};
//uno.upload._dhtmlreplace = function(ifile,url,onstart,onfinish){
//    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);
//};