var filetmps = new Array();

function setOuterHTML(ElementID, txt) {
	
	var someElement = document.getElementById(ElementID);

	if (someElement.outerHTML) {
		
		//работает для IE, но не работает для Firefox.
		someElement.outerHTML = txt;
		
	} else {
		
		//работает для Firefox, но не работает для IE.
		var range = document.createRange();
		range.setStartBefore(someElement);
		var docFrag = range.createContextualFragment(txt);
		someElement.parentNode.replaceChild(docFrag, someElement);
	}
}

function ufileupload(obj) {
	
	idufile = obj.id;
	if (idufile.substr(idufile.length - 5, 5) == 'ufile') {
	
		form = obj.form;
		id = idufile.substr(0, idufile.length - 5);

		var n = 'f' + Math.floor(Math.random() * 99999);

		filetmps['enctype'] = form.getAttribute('enctype');
		form.setAttribute('enctype', 'multipart/form-data');

		/*filetmps['onsubmit'] = form.getAttribute('onsubmit');
		form.setAttribute('onsubmit', "alert('456'); return true;");*/

		filetmps['target'] = form.getAttribute('target');
		form.setAttribute('target', n);

		filetmps['action'] = form.getAttribute('action');
		form.setAttribute('action', '');	// To the root

		// upload animation on
		document.getElementById(id + 'pi').style.display = 'inline';

		//alert(obj.form.name);

		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="' + n + '" name="' + n + '" onload="ufileupload_finish(\'' + id + '\', \'' + n + '\');"></iframe>';
		document.body.appendChild(d);
	
		filetmps['requested'] = 1;
	
		form.submit();
	}
}

function ufileupload_finish(id, iframe) {
	
	if ((filetmps['requested'] != undefined) && (filetmps['requested'] == 1)) {
		// upload animation off
		document.getElementById(id + 'pi').style.display = 'none';
	
		// Show result
		tm = window.frames[iframe].document.body.innerHTML;
		window.frames[iframe].document.body.innerHTML = '';
		
		setOuterHTML(id + 'div', tm);
		
		//document.getElementById(id + 'div').outerHTML = tm;
	
		filetmps['requested'] = 0;
	} else {
		// Nothing to do
	}
}

