function u_UploadField(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction){
	var objTarget = document.getElementById(strName);
	if(!objTarget){ alert("UPLOAD ERROR: Input hidden não encontrado."); return false; }
	var strValue = objTarget.value;
	
	var fileSelected = false
	if(strValue!=""){ fileSelected=true; }
	
	var optDelete = false
	if(strFunction!=""){ optDelete=true; }
	
	// cria div principal
	if(u_getObj("uD_"+strName)){
		u_divPrincipal = u_getObj("uD_"+strName);
	}else{
		u_divPrincipal = document.createElement("div");
		u_divPrincipal.setAttribute("id","uD_"+strName);
		objTarget.parentNode.insertBefore(u_divPrincipal,objTarget.nextSibling);
	}
	u_divPrincipal.innerHTML="";
	
	// cria texto
	strValue2=(strValue.length>30)?strValue.substr(0,30)+"...":strValue;
	u_spanText = document.createElement("span");
	u_spanText.style.marginRight="5px";
	u_spanBold = document.createElement("b");
	u_spanBold.appendChild(document.createTextNode(fileSelected?strValue2:"arquivo não enviado"));
	u_spanText.appendChild(u_spanBold);
	u_divPrincipal.appendChild(u_spanText);
	
	// cria btn visualizar
	if(fileSelected){
		u_btnView = document.createElement("button");
		u_btnView.setAttribute("type","button");
		u_btnView.setAttribute("title","PRÉ-VISUALIZAR ARQUIVO");
		u_btnView.className="btn";
		u_btnView.style.width="25px";
		u_btnView.style.paddingLeft="3px";
		u_btnView.style.paddingRight="3px";
		u_btnView.onclick = function(){ window.open(strPath+strValue,"u_foto","resizable=1,location=0,menubar=0,status=0,toolbar=0,scrollbars=1,titlebar=0"); };
			u_btnViewImg = document.createElement("img");
			u_btnViewImg.setAttribute("src","imagens/icones/eye.png");
			u_btnViewImg.style.margin="0";
			u_btnView.appendChild(u_btnViewImg);
		u_divPrincipal.appendChild(u_btnView);
	}
	
	// cria btn escolher arquivo
	u_btnChoose = document.createElement("button");
	u_btnChoose.setAttribute("type","button");
	u_btnChoose.setAttribute("title",(fileSelected?"MUDAR ARQUIVO":"ESCOLHER ARQUIVO"));
	u_btnChoose.className="btn";
	u_btnChoose.style.width=fileSelected?"155px":"170px";
	u_btnChoose.style.marginLeft=fileSelected?"3px":"0px";
	u_btnChoose.onclick = function(){ u_fncShowForm(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction); }
		u_btnChooseImg = document.createElement("img");
		u_btnChooseImg.setAttribute("src","imagens/icones/folder_explore.png");
		u_btnChoose.appendChild(u_btnChooseImg);
		u_btnChoose.appendChild(document.createTextNode((fileSelected?"mudar":"escolher")+" arquivo..."));
	u_divPrincipal.appendChild(u_btnChoose);
	
	// cria btn delete
	if(fileSelected && optDelete){
		u_btnDelete = document.createElement("button");
		u_btnDelete.setAttribute("type","button");
		u_btnDelete.setAttribute("title","REMOVER ARQUIVO");
		u_btnDelete.className="btn";
		u_btnDelete.style.width="25px";
		u_btnDelete.style.marginLeft="3px";
		u_btnDelete.style.paddingLeft="3px";
		u_btnDelete.style.paddingRight="3px";
		u_btnDelete.onclick = function(){ u_fncDelete(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction); };
			u_btnDeleteImg = document.createElement("img");
			u_btnDeleteImg.setAttribute("src","imagens/icones/delete.png");
			u_btnDeleteImg.style.margin="0";
			u_btnDelete.appendChild(u_btnDeleteImg);
		u_divPrincipal.appendChild(u_btnDelete);
	}
	
	//setTimeout("u_fncHideForm()",200);
}



function u_fncDelete(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction){
	var objTarget = document.getElementById(strName);
	if(!objTarget){ alert("UPLOAD ERROR: Input hidden não encontrado."); return false; }
	
	if(confirm("Confirma exclusão do arquivo?")){
		eval(strFunction);
		objTarget.value="";
		u_UploadField(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction);
	}
}



var u_objBody,u_DivBg,u_DivForm,u_iFrame
function u_fncShowForm(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction){
	var objTarget = document.getElementById(strName);
	if(!objTarget){ alert("UPLOAD ERROR: Input hidden não encontrado."); return false; }
	var strValue = objTarget.value;
	
	u_objBody = document.body;
	u_fncSHSelects("hidden");
	
	// cria o div de fundo
	if(u_getObj("u_uploadBg")){
		u_DivBg = u_getObj("u_uploadBg");
		u_DivBg.innerHTML = "";
		u_DivBg.style.display = "";
	}else{
		u_DivBg = document.createElement("div");
		u_DivBg.setAttribute("id","u_uploadBg");
		u_objBody.appendChild(u_DivBg);
	}
	
	// cria o div do form
	if(u_getObj("u_uploadForm")){
		u_DivForm = u_getObj("u_uploadForm");
		u_DivForm.innerHTML = "";
		u_DivForm.style.display = "";
	}else{
		u_DivForm = document.createElement("div");
		u_DivForm.setAttribute("id","u_uploadForm");
		u_objBody.appendChild(u_DivForm);
	}
	
	// cria o iframe
	u_iFrame = document.createElement("iframe");
	u_iFrame.setAttribute("src","upload.php?label="+strTitle+"&campo="+strName+"&tipo="+optTipo+"&largura="+numWidth+"&altura="+numHeight+"&tamanho="+numSizeKb+"&antigo="+strValue+"&function="+strFunction);
	u_iFrame.setAttribute("scrolling","no");
	u_iFrame.setAttribute("frameborder","0");
	u_iFrame.setAttribute("marginwidth","0");
	u_iFrame.setAttribute("marginheight","0");
	u_iFrame.setAttribute("hspace","0");
	u_iFrame.setAttribute("vspace","0");
	u_DivForm.appendChild(u_iFrame);
	
	
	// redimensiona e posiciona os div
	var arrayPageSize = u_getPageSize();
	var arrayPageScroll = u_getPageScroll();
	//u_DivBg.style.width = arrayPageSize[0]+"px";
	u_DivBg.style.height = arrayPageSize[1]+"px";
	u_DivBg.style.zIndex="10000";
	
	u_DivForm.style.left = ((arrayPageSize[0]/2)-(u_DivForm.offsetWidth/2))+"px";
	u_DivForm.style.top = (arrayPageScroll[1]+(arrayPageSize[3]/2)-(u_DivForm.offsetHeight/2)-30)+"px";
	u_DivForm.style.zIndex="10001";
}


// ESCONDE O LOADING
function u_fncHideForm(){
	if(u_iFrame){
		u_iFrame.parentNode.removeChild(u_iFrame);
	}
	if(u_DivForm){
		u_DivForm.innerHTML = "";
		u_DivForm.style.display = "none";
		u_DivForm.parentNode.removeChild(u_DivForm);
		//u_DivForm = null;
	}
	if(u_DivBg){
		u_DivBg.innerHTML = "";
		u_DivBg.style.display = "none";
		u_DivBg.parentNode.removeChild(u_DivBg);
		//u_DivBg = null;
	}
	u_fncSHSelects("visible");
}


function u_fncSelectFile(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction,strFile){
	var objTarget = document.getElementById(strName);
	if(!objTarget){ alert("UPLOAD ERROR: Input hidden não encontrado."); return false; }
	objTarget.value=strFile;
	
	u_fncHideForm();
	u_UploadField(strName,strTitle,optTipo,numWidth,numHeight,numSizeKb,strPath,strFunction);
}









// ##################################################
// ##################################################
// OUTRAS FUNCOES UTEIS
// ##################################################
// ##################################################

function u_getObj(uP_objID){
	if(document.getElementById){ return document.getElementById(uP_objID); }
	else if(document.all){ return document.all[uP_objID]; }
	else if(document.layers){ return document.layers[uP_objID]; }
	return false;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function u_getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function u_getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // repete
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function u_fncSHSelects(strShowHide){
	if(navigator.appVersion.indexOf("MSIE")!=-1){
		selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) { selects[i].style.visibility = strShowHide; }
	}
}







