// Abre janela popup
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/*
// Inserindo SWF
function swf(caminho, largura, altura){
	var insert = document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=3,0,0,0" width="'+largura+'" height="'+altura+'"><param name=movie value="'+caminho+'"><param name=quality value=autohigh><param name=menu value=false><param name=bgcolor><param name=wmode value=opaque><embed src="'+caminho+'" quality=autohigh width="'+largura+'" height="'+altura+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>');
	return insert;
}*/

// Inserindo SWF

function swf(caminho, largura, altura, bg){
	(bg==1)?wmode='transparent':wmode='opaque';
	
	insert = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=3,0,0,0" width="'+largura+'" height="'+altura+'">';
	insert += '<param name=movie value="'+caminho+'"><param name=quality value=autohigh><param name=menu value=false>';
	insert += '<param name=bgcolor>';
	insert += '<param name=wmode value='+wmode+'>';
	insert += '<embed wmode="'+wmode+'"src="'+caminho+'" quality=autohigh width="'+largura+'" height="'+altura+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
	insert += '</object>';
	
	document.write(insert);
}

// Oculta Elementos
function showHide(id) {
	var obj = document.getElementById(id);
	if(obj.style.display == "none" || obj.style.display == "") {
		hideAll();
		obj.style.display = "block";
	} else {
		hideAll();
	}
}

function show(id){
	hideAll();		
	var obj = document.getElementById(id);
	obj.style.display = "block";	
}

function show2(id){		
	var obj = document.getElementById(id);
	obj.style.display = "block";	
}

function hide(id){
	var obj = document.getElementById(id);
	obj.style.display = "none";
}

function hideAll() {
	var divs = document.getElementsByTagName('div');
	for(var x = 1; x < divs.length; x++) {
		var _div = divs[x];
		if(_div.className == "subitem"){
			var id = _div.id
		document.getElementById(id).style.display='none';
		}
	}
}

//Altera a cor de background
function color1(id){
	var obj = document.getElementById(id);	
		//obj.style.backgroundImage = 'url("./imagens/bg_item.png")';
		//obj.style.borderColor = '#036';
		obj.style.color = '#999';
}

function color2(id){
	var obj = document.getElementById(id);
	//obj.style.backgroundImage = 'none';
	//obj.style.borderColor = '#CCC';
	obj.style.color = '#036';
	
}


// Insere Html em uma DIV
function maisHtml (id, conteudo){
 var obj = document.getElementById(id);
 obj.innerHTML = conteudo += obj.innerHTML;
}


function cleanHtml (id){
 var obj = document.getElementById(id);
 obj.innerHTML = "";
}


// Cria objeto para trabalhar com ajax em diferentes Browsers
function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}

// acessa conteúdo

var divLoader = '<div id="carregando"><img src="./imagens/carregador.gif" alt="" /><p style="padding:3px; float:none">Processando</p> </div>';
// Envio pelo método POST
function setDivPost(id, url, form){
	
	var dataPost = BuscaElementosForm(form);	
	var obj = document.getElementById(id);
	var ajax = openAjax();
	
	ajax.open("POST", url, true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	//0-Nao inicializado, 1-Carregando, 2-Carregado, 3-Interativo, 4-Completo
	ajax.onreadystatechange = function(){
		
		/*
			readyState:
			Inteiro representando o estado da requisição:
			0 = não inicializado
			1 = carregando
			2 = carregado
			3 = modo interativo
			4 = completado
		*/
		if(ajax.readyState < 4){
			obj.innerHTML = divLoader;
		}else if(ajax.readyState == 4){				
			//if(ajax.state == 200 || ajax.state == 0){
				obj.innerHTML = ajax.responseText;
				//alert (ajax.state);
			//}
		}
	}
	ajax.send(dataPost);
	return;
}


// Envio pelo método GET
function setDivGet(id, url){
	
	var obj = document.getElementById(id);
	var ajax = openAjax();
	
	obj.innerHTML = divLoader;
	
	ajax.open("GET", url, true);	
	//ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	ajax.onreadystatechange = function(){		
		if(ajax.readyState == 4){				
			//if(ajax.state == 200){
			obj.innerHTML = ajax.responseText;			
			//}
		}
	}
	ajax.send(null);
	return false;
}

//Função para pegar todos os dados de um form
function BuscaElementosForm(idForm) {
    var elementosFormulario = document.getElementById(idForm).elements;
    var qtdElementos = elementosFormulario.length;
    var queryString = "";
    var elemento;

    //Cria uma funcao interna para concatenar os elementos do form
    this.ConcatenaElemento = function(nome,valor) { 
                                if (queryString.length>0) { 
                                    queryString += "&";
                                }
                                queryString += encodeURIComponent(nome) + "=" + encodeURIComponent(valor);
                             };

    //Loop para percorrer todos os elementos
    for (var i=0; i<qtdElementos; i++) {
        //Pega o elemento
        elemento = elementosFormulario[i];
        if (!elemento.disabled) {
            //Trabalha com o elemento caso ele nao esteja desabilitado
            switch(elemento.type) {
                //Realiza a acao dependendo do tipo de elemento
                case 'text': case 'password': case 'hidden': case 'textarea': 
                    this.ConcatenaElemento(elemento.name,elemento.value);
                    break;
                case 'select-one':
                    if (elemento.selectedIndex>=0) {
                        this.ConcatenaElemento(elemento.name,elemento.options[elemento.selectedIndex].value);
                    }
                    break;
                case 'select-multiple':
                    for (var j=0; j<elemento.options.length; j++) {
                        if (elemento.options[j].selected) {
                            this.ConcatenaElemento(elemento.name,elemento.options[j].value);
                        }
                    }
                    break;
                case 'checkbox':
					//alterado em  04/04/2007 por Rafael Douglas
					if (elemento.checked) {						
					 	this.ConcatenaElemento(elemento.name + '[]', elemento.value);
					}
					break;
				
				case 'radio':
                    if (elemento.checked) {
                        this.ConcatenaElemento(elemento.name,elemento.value);
                    }
                    break;
            }
        }
    }
	
	return queryString;
	
}


// Funções pedido

function checkAll(){
	nBox = document.formPedido.del.length;
	
	if(nBox != undefined){
		for(i=0; i<nBox; i++){
			if(!document.formPedido.del[i].checked){
				document.formPedido.del[i].click();
			}
		}
	}else{
		document.formPedido.del.click();
	}
}


function apagar(div, url, form, msg){
	a = confirm (msg);
	if (a == true){			
		setDivPost(div, url, form);
	}
}


function atualiza(div, url, form){
	setDivPost(div, url, form);
}