﻿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 contaCaracter(text, max, id){
	text.readonly = false;
	var contador = document.getElementById(id);
	
	if(contador){	
		if(text.value.length > 0){
			contador.innerHTML = max - text.value.length;
			contador.className = 'contador';
			if(text.value.length >= (max - 5))
				contador.className = 'contador-aviso';
		}else
			contador.innerHTML = max;
	}	
}

function scrollContent (id, dir) {
	var div = document.getElementById(id);
	clearInterval(div.timer);
	var sections = div.getElementsByTagName('li');
	var length = sections.length;
	var limit;
	if (dir == -1) limit = 0;
	else {
		if (length > 1) limit = sections[length-1].offsetLeft;
		else limit = sections[length-1].offsetWidth - div.parentNode.offsetWidth + 20;
	}
	div.style.opacity = STARTINGOPACITY * .01;
	div.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
	div.timer = setInterval( function() { scrollAnimate(div,dir,limit) }, SCROLLTIMER);
}

function scrollAnimate (div, dir, limit) {
	div.style.left = div.style.left || '0px';
	var left = div.style.left.replace('px','');
	if (dir == 1) {
		if (limit - Math.abs(left) <= SCROLLSPEED) {
			cancelScroll(div.id);
			div.style.left = '-' + limit + 'px';
		} else div.style.left = left - SCROLLSPEED + 'px';
	} else {
		if (Math.abs(left) - limit <= SCROLLSPEED) {
			cancelScroll(div.id);
			div.style.left = limit + 'px';
		} else div.style.left = parseInt(left) + SCROLLSPEED + 'px';
	}
}

function cancelScroll(id) {
	var div = document.getElementById(id);
	div.style.opacity = 1;
	div.style.filter = 'alpha(opacity=100)';
	clearTimeout(div.timer);
}

function scrollSpeed(){
	SCROLLSPEED = SCROLLSPEED + 1;
}