﻿function mostraSubMenu(li){
	if(li){
		for (i=0; i<li.childNodes.length; i++) {
			node = li.childNodes[i];
			if (node.nodeName=="UL") {
				node.style.display = "block";
			}
		}
		
		li.className = "ativo";
	}
}

function escondeSubMenu(li){
	if(li){
		for (i=0; i<li.childNodes.length; i++) {
			node = li.childNodes[i];
			if (node.nodeName=="UL") {
				node.style.display = "none";
			}
		}
		
		li.className = "";
	}
}

function startDelete() {
	var a;
	if ((a = getElementsByClassName("delete", document, "a"))) {
		for (var i=0; i<a.length; i++) {
			if (!a[i].onclick) {
				a[i].onclick = function(){
					if(confirm(this.title)) window.location.href = this.href+"/sim";
					return false;
				}
			}
		}
	}
	
	if ((a = getElementsByClassName("undo", document, "a"))) {
		for (var i=0; i<a.length; i++) {
			if (!a[i].onclick) {
				a[i].onclick = function(){
					if(confirm(this.title)) window.location.href = this.href+"/sim";
					return false;
				}
			}
		}
	}	
}

function getElementsByClassName(searchClass, node, tag){
	var classElements = new Array();

	if (node == null) node = document;

	if (tag == null) tag = '*';

	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (var i = 0, j = 0; i < elsLen; i++){
		if (els[i].className && pattern.test(els[i].className)){
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function clearIt(e) {
	if(e) {
		while (e.hasChildNodes()) e.removeChild(e.childNodes[0]);
	}
}

// changeSelection(this, 'nome do select que vai alterar(mesmo que o do objeto)', 'frase padrão no primeiro option');
// EXEMPLO: changeSelection(this, 'setor', 'Selecione um Setor:');
function changeSelection() {
	var a = arguments[0];
	var b = arguments[1];
	var c = arguments[2];
	var choice = a.options[a.selectedIndex].value;
	var db = eval(b+"[choice]");
	
	if(arguments[3]){
		var chooser = document.getElementById(arguments[3]);
		clearIt(chooser);
		
		var option = document.createElement("option");
		option.value = "";
		option.innerHTML = c;
		chooser.appendChild(option);
		
		if ((choice != "") && (db)) {
			for(var i = 0; i < db.length; i++){
				option = document.createElement("option");
				option.value = db[i].value;
				option.innerHTML = db[i].text;
				chooser.appendChild(option);
			}
		}
	} else {
		var chooser = a.form.elements[b];
		var x;
		chooser.options.length = 0;
		if (c != '') {
			chooser.options[0] = new Option(c, "", true, false);
			x = 1;
		} else x = 0;
		if (choice != "") {
			if(db) for (var i = 0; i < db.length; i++) chooser.options[i + x] = new Option(db[i].text, db[i].value);
		}
	}
}

// selectOption("nome do formulario", ("b,c,d" correspondem "a,b,c" da função "changeSelection"), "valor da opção para selecionar");
// EXEMPLO selectOption('formFuncionario', 'empresa', 'setor', 'Selecione um Setor:', '2');
function selectOption() {
	var a = arguments[0];
	var b = arguments[1];
	var c = arguments[2];
	var d = arguments[3];
	var e = arguments[4];
	
	var form = eval("document."+a);
	
	if(arguments[5]){
		var element = document.getElementById(arguments[5]);
		element = element.getElementsByTagName("option");
		
		for(i=0; i<element.length; i++){
			if (element[i].value == e) element[i].selected = true;
		}
		
		changeSelection(form.elements[b], c, d, arguments[5]);
	} else {
		var element = form.elements[c];
		changeSelection(form.elements[b], c, d);
	}
	for(i=0; i<element.length; i++){
		if (element[i].value == e) element[i].selected = true;
	}
}

function formataValor(valor, casas){
   var num = Math.round(valor * Math.pow(10, casas)) / Math.pow(10, casas);
   return(num);
}

function openWindow(url, propriedades) {
  window.open(url,'_blank',"toolbar=no,location=no,status=no,menubar=no," + propriedades);
}

function str_replace ( search, replace, subject ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Martijn Wieringa
    // +      input by: penutbutterjelly
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: str_replace('l', 'l', 'HeLLo');
    // *     returns 1: 'Hello'
    
    var i;
    
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
 
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
 
 
    for(var k=0; k<search.length; k++){
        reg = new RegExp(search[k], 'gi');
        subject = subject.replace(reg, replace[k]);
    }
 
 
    return subject;
}

function strlen(string) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Sakimori
    // +      input by: Kirk Strobeck
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strlen('Kevin van Zonneveld');
    // *     returns 1: 19
 
    var tmp_str = '', l = 0;
    tmp_str = string + '';
    
    if (tmp_str.length) {
        return tmp_str.length; 
    }  
    
    return 0;
}

function mark(a) {
	var x = document.forms[a.form.name].elements[a.name];
	if (a.value == 'c') {
			if (a.checked == false) {
					for(i=1; i<x.length; i++) x[i].checked = false;
			}
	} else {
			for(i=0; i<x.length; i++) x[i].checked = (x[i].value == 'c') ? true : x[i].checked;
	}
}

function markAll(a, b) {
	var x = document.forms[a].elements[b];
	
	if(a && b){
		if(x){
			var test = false;
			for(i=0; i<x.length; i++) {
				if (x[i].checked == true) test = true;
			}
			if (test == true) {
				for(i=0; i<x.length; i++) x[i].checked = false;
			} else {
				for(i=0; i<x.length; i++) x[i].checked = true;
			}
		}
	}
}

function goUrl(url) {
  parent.location = url;
}

function ajustaLarguraRoundCorner(){
	if((navigator.appName != 'Netscape')){
		var ul = document.getElementById('lista_de_produtos');
		if(ul){
			var spans = ul.getElementsByTagName('span');
			if(spans.length > 0){
				for(var i = 0; i < spans.length; i++){
					if((spans[i].className == 'imagem-produto-fundo') || (spans[i].className == 'imagem-produto-fundo roundCornersParent')){
						var img = spans[i].getElementsByTagName("img");
						if(img[0]){
							spans[i].style.width = img[0].width + "px";
						}				
					}
				}
			}
		}
	}
	
	var ul;
	if ((ul = getElementsByClassName("lista_de_imagens", document, "ul"))) {
		for (var i=0; i<ul.length; i++) {
			if(ul[i]){
				var spans = ul[i].getElementsByTagName('span');
				if(spans.length > 0){
					for(var j = 0; j < spans.length; j++){
						if((spans[j].className == 'img') || (spans[j].className == 'img roundCornersParent')){
							var img = spans[j].getElementsByTagName("img");
							if(img[0]){
								spans[j].style.width = img[0].width + "px";
							}				
						}
					}
				}
			}
		}
	}
	
	var ul;
	if ((ul = getElementsByClassName("todos_os_registros", document, "ul"))) {
		for (var i=0; i<ul.length; i++) {
			if(ul[i]){
				var spans = ul[i].getElementsByTagName('span');
				if(spans.length > 0){
					for(var j = 0; j < spans.length; j++){
						if((spans[j].className == 'img') || (spans[j].className == 'img roundCornersParent')){
							var img = spans[j].getElementsByTagName("img");
							if(img[0]){
								spans[j].style.width = img[0].width + "px";
							}				
						}
					}
				}
			}
		}
	}	
}

function carregaImg(id){
	if(id != ''){
		var div = document.getElementById('div_img_g');		
		var imgG = document.getElementById('img_g');
		var refG = document.getElementById('ref_g');
		var descG = document.getElementById('desc_g');
		
		var li = document.getElementById('li_'+id);
		var imgP = document.getElementById('img_'+id);
		var refP = document.getElementById('ref_'+id);	
		var refPP = document.getElementById('ref_p_'+id);	
		var descP = document.getElementById('desc_'+id);	
		
		if(imgG && imgP){
			var imgAuxAlt = imgG.getAttribute("alt");
			var imgAuxSrc = imgG.getAttribute("src");
			var refAux = "";
			var descAux = "";
			if(refG) refAux = (refG.innerHTML != "") ? refG.innerHTML : "";
			if(descG) descAux = (descG.innerHTML != "") ? descG.innerHTML : "";
			
			imgG.setAttribute("src", imgP.getAttribute("alt"));
			imgG.setAttribute("alt", imgP.getAttribute("src"));
			/*div.style.background = "#0F0F0F url('"+url_img+"f/i/loading.gif') no-repeat center center";
		    window.setTimeout(function(){
				verificaCarregamento(imgG, div);
			}, 100);*/
			
			// Destrói o elemento p da referência se ele existir
			if(refG){
				div.removeChild(refG);	
			}
			
			if(refP){
				// Cria o elemento p da referência
				refG = document.createElement("p");
				refG.setAttribute("id", "ref_g");
				refG.className = "ref";
				refG.innerHTML = refP.innerHTML;
				div.appendChild(refG);
			}
			
			// Destrói o elemento p da descrição se ele existir
			if(descG){
				div.removeChild(descG);	
			}
				
			if(descP){
				// Cria o elemento p da descrição
				descG = document.createElement("p");
				descG.setAttribute("id", "desc_g");
				descG.className = "desc";
				descG.innerHTML = descP.innerHTML;
				div.appendChild(descG);	
			}
			
			// Passa a imagem grande para o local da pequena
			imgP.setAttribute("src", imgAuxAlt);
			imgP.setAttribute("alt", imgAuxSrc);
				
			// Destrói o elemento span da referência curta se ele existir
			if(refPP){
				li.removeChild(refPP);	
			}				
			
			// Destrói o elemento span da referência completa se ele existir
			if(refP){
				li.removeChild(refP);	
			}
			
			// Destrói o elemento span da referência completa se ele existir
			if(descP){
				li.removeChild(descP);	
			}			
			
			if(refAux != ""){
				// Cria o elemento span da referência curta
				refPP = document.createElement("span");
				refPP.setAttribute("id", "ref_p_"+id);
				refPP.innerHTML = ((strlen(refAux) > 10) ? refAux.substr(0,10) + "..." : refAux);
				li.appendChild(refPP);
				
				// Cria o elemento span da referência completa
				refP = document.createElement("span");
				refP.setAttribute("id", "ref_"+id);
				refP.className = "invisivel";
				refP.innerHTML  = refAux;
				li.appendChild(refP);								
			}
			
			if(descAux != ""){				
				// Cria o elemento span da descrição
				descP = document.createElement("span");
				descP.setAttribute("id", "desc_"+id);
				descP.className = "invisivel";
				descP.innerHTML = descAux;
				li.appendChild(descP);				
			}
			
			window.scrollTo(0,0);			
					
			/*
			if(navigator.appName != 'Netscape')
				var t = 500;
			else
				var t = 1000;
			*/
			var t = 1000;
			window.setTimeout(function() {
				reajustaDiv();
			}, t);
		}
	}
}

function verificaCarregamento(imgG, div){
    if(imgG.complete){
		div.style.background = "#0F0F0F";
    }else{
		window.setTimeout(function(){
			verificaCarregamento(imgG, div);
		}, 100);
    }
}

function reajustaDiv(){
	var div_lb = document.getElementById("black_content");
	var body = document.getElementsByTagName("body");
	if(div_lb){
		var imgG = document.getElementById('img_g');
		var refG = document.getElementById('ref_g');
		var descG = document.getElementById('desc_g');		
		var h3 = div_lb.getElementsByTagName('h3');		
		if(imgG){
			var wD = ((imgG.width <= 150) ? 160 : imgG.width) + 10;
			var hD = imgG.height + 20;
			
			// Calcula a altura do título (h3)
			if(h3[0]){
				var totalCaracter = strlen(h3[0].innerHTML);
				if(totalCaracter > 0){
					var wDTmp = Math.floor((wD * 60) / 500); // baseado em 60 caracteres por linha para uma largura de 500px
					var totalLinha = Math.ceil(totalCaracter / wDTmp);
					hD = hD + (totalLinha * 16); // 16 = linhe-height
				}
			}
			
			// Calcula a altura da referência
			if(refG){
				var totalCaracter = strlen(refG.innerHTML);
				if(totalCaracter > 0){
					var wDTmp = Math.floor((wD * 85) / 500); // baseado em 85 caracter por linha para uma largura de 500px
					var totalLinha = Math.ceil(totalCaracter / wDTmp);
					hD = hD + (totalLinha * 12) + 10; // 12 = linhe-height ; 10 = padding
				}
			}
			
			// Calcula a altura da descrição
			if(descG){
				var totalCaracter = strlen(descG.innerHTML);
				if(totalCaracter > 0){
					var wDTmp = Math.floor((wD * 85) / 500); // baseado em 85 caracter por linha para uma largura de 500px
					var totalLinha = Math.ceil(totalCaracter / wDTmp);
					hD = hD + (totalLinha * 12); // 12 = linhe-height
				}
			}
			
			var imgs = div_lb.getElementsByTagName("img");
			if(imgs.length > 1){
				hD = hD + 10; // 10 = margin-top do ul
				
				var x = imgs.length - 1;
				var totalLi = Math.floor(wD / 100); // Calcula o total de li's por linha
				var totalLinha = 1;
				if(x > totalLi){
					totalLinha = Math.ceil(x / totalLi); // Calcula o total de linhas
				}
				
				hD = hD + (totalLinha * 110); // 110px = height li;
			}
			
			if(navigator.appName != 'Netscape') hD = hD + 10;
			
			// Atualizar altura e largura
			div_lb.style.width  = wD + "px";
			div_lb.style.height = hD + "px";
			
			// Ajusta valor do LEFT
			var wBody = body[0].clientWidth;
			var wTmp = parseFloat(wBody - wD);
			var l = (wTmp - 10) / 2;
			div_lb.style.left = (Math.round(l) - 10) + 'px';
		}
	}
}