
function replaceAllUp(valor,from,to) {
	while (valor.indexOf(from) > 0) {
		valor = valor.replace(from,to);
	}
	return valor;
}

function ajaxUploadThis(id) {
	var idq = new String((Math.random()+"").replace('.','')+"");
	
	var va = formX.getAttributeNode("action").value;
	var vt = formX.getAttributeNode("target").value;
	var ve = formX.getAttributeNode("enctype").value;
	
	formX.getAttributeNode("action").value="ajaxUpload?ajaxUploadPath=" + formX.ajaxUploadPath.value + "&ajaxUploadRenameTo=" + formX.ajaxUploadRenameTo.value + "&ajaxUploadExcecao=" + formX.ajaxUploadExcecao.value + "&idq=" + idq + "&AjaxRequestUniqueId=" + (Math.random()+"").replace('.',''); 
	formX.getAttributeNode("target").value="ifajaxUpload";
	formX.getAttributeNode("enctype").value="multipart/form-data";
	formX.submit();
	
	formX.getAttributeNode("action").value=va;
	formX.getAttributeNode("target").value=vt;
	formX.getAttributeNode("enctype").value=ve;
	
	document.getElementById("ajaxUploadProg"+id).innerHTML = "<br/>Processando...";

	ajaxTimer(id,idq);
}

function ajaxTimer(id,idarquivo) {
	var req = null;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (req==null) {
		alert('Sem AJAX');
	}
	
	var idf = new Number(id);
	var idq = new String(idarquivo);
	req.onreadystatechange=function(){
		if (req.readyState == 4) {
			var strPercent= new Number(0);
			if (req.status == 200) {

				strPercent= new Number( (req.responseText.substring( req.responseText.lastIndexOf('*')+1, req.responseText.lastIndexOf(':') ))*1 );
				var strKs= new String(req.responseText.substring( req.responseText.lastIndexOf(':')+1, req.responseText.lastIndexOf('?') ));

				document.getElementById("ajaxUploadProg"+idf).innerHTML =
				'<div style="width:'+strPercent+'%;height:20px;position:absolute;border:1px #3399FF solid;background:#3399FF">&nbsp;</div>'+
				'<div style="width:100%;height:20px;position:absolute;border:1px #3399FF solid">'+
					'<table cellpadding="0" cellspacing="0" border="0" align="center">'+
					'<tr>'+
						'<td align="right" class="texto">'+
							strPercent +
						'%'+
						'</td>'+
						'<td>'+
							'&nbsp;'+
						'</td>'+
						'<td align="left" class="texto">'+
							strKs +
						' kbyte/s'+
						'</td>'+
					'</tr>'+
					'</table>'+
				'</div>'+
				'<br/>'
				;
			}
			if (strPercent != 100) {
				setTimeout("ajaxTimer("+ idf +", '"+ idq +"')",1000);
			} else {
				if(formX.ajaxUploadExcecao.value="10"){ // TERMÔMETRO
					document.getElementById("ajaxUploadProg"+idf).style.display = "none";
					termometroListFiles();
				} else {
					document.getElementById("ajaxUploadProg"+idf).innerHTML = "<br/>";
					if (req.responseText.lastIndexOf('[f]') > 0) {
						document.getElementById("ajaxUploadProg"+idf).innerHTML += req.responseText.substring( req.responseText.lastIndexOf('[f]')+3 );
					} else {
						var caminho = req.responseText.substring( req.responseText.lastIndexOf('?')+1 );
						document.getElementById("ajaxUploadProg"+idf).innerHTML += "<a target='_blank' href='"+ caminho +"'>"+ req.responseText.substring( req.responseText.lastIndexOf('/')+1 ) +"</a> - <nobr>http://www.maquina.inf.br/maquinaNet/" + caminho + "</nobr>";
					}
				}
			
				var diva = new String("ajaxUpload"+idf);
				var ida = new Number(idf+1);
				document.getElementById(diva).innerHTML = replaceAllUp(document.getElementById("ajaxUploadBk").innerHTML,'0',ida);
			}
		}
	};

	try {
		var url = new String("ajaxUpload?AjaxRequestUniqueId=" + (Math.random()+"").replace('.','') + "&idq=" + idarquivo + "&ajaxUploadPath=" + formX.ajaxUploadPath.value + "&ajaxUploadRenameTo=" + formX.ajaxUploadRenameTo.value + "&ajaxUploadExcecao=" + formX.ajaxUploadExcecao.value );
		req.open("GET", url, true);
		req.setRequestHeader('Content-type', 'charset=UTF-8');
		req.send(url);
	} catch(e) {
		// alert("Erro ao enviar arquivo. Entre em contato com o administrador do sistema");
	}
	
}
