//mostrar(paginaque ira buscar, id, div onde será aberto)
function mostrar(pagina, id, recebe){
	div=recebe;
	url=pagina;
	post = "id="+encodeURI(id);
	ajax(url, post, div); 
}

function PesquisaPorNumeroPedido(pagina, id_pedido, recebe){
	div=recebe;
	url=pagina;
	post = "id_pedido="+encodeURI(id_pedido);
	ajax(url, post, div); 
}

//atualiza_presente(pagina que irá buscar, presente s/n, id_carrinho, id_produto, id_usuario, div onde será aberto)
function atualiza_carrinho(pagina, valor, idca, idpr, idus, recebe){
	div=recebe;
	url=pagina;
	post = "valor="+encodeURI(valor) + "&idca="+encodeURI(idca) + "&idpr="+encodeURI(idpr) + "&idus="+encodeURI(idus);
	ajax(url, post, div);
}


function soma_carrinho(pagina, id, valor, recebe, parametro){
	div=recebe;
	url=pagina;
	abreCarrinho = parametro;
        post = "id="+encodeURI(id) + "&valor_venda=" + encodeURI(valor) + "&qp=" + encodeURI(document.getElementById("quantidade").value);
	if(document.getElementById("saldo_produto").value == '0.0000' || document.getElementById("quantidade").value == ''){
		if(document.getElementById("saldo_produto").value == '0.0000'){
			alert('Faltou informar o tamanho!');
		} else if(document.getElementById("quantidade").value == '') {
			alert('Faltou informar quantidade!');
			document.getElementById("quantidade").focus();
		}
	} else {
		ajax2(url, post, div, abreCarrinho);
	}
}

//funcao que retorna o ajax
function ajax(url, valores, div)
{
    req = null;

// Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST",url,true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", valores.length);
        req.send(valores);
    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("POST",url,true);
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                                
			req.setRequestHeader("Content-length", valores.length);
            req.send(valores);
        }
    }
}


//funcao que retorna o ajax
function ajax2(url, valores, div, abreCarrinho)
{
    req = null;

// Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST",url,true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", valores.length);
        req.send(valores);
    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("POST",url,true);
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                                
			req.setRequestHeader("Content-length", valores.length);
            req.send(valores);
        }
    }
}


function processReqChange() 
{ 
	document.getElementById(div).innerHTML = '<div align="center"><img src="imagens/ajax-loader.gif"/></div>';	
    // apenas quando o estado for "completado" 
    if (req.readyState == 4) { 
	document.getElementById(div).innerHTML = '<div align="center"><img src="imagens/ajax-loader.gif"/></div>';	
	
	// apenas se o servidor retornar "OK" 
        if (req.status ==200) { 
	
            // procura pela div id="pagina" e insere o conteudo 
            // retornado nela, como texto HTML 
			document.getElementById(div).innerHTML = req.responseText;

			// executa scripts
   			 extraiScript(req.responseText);
			 
			 if(abreCarrinho == 1){ window.location.href = "carrinho_compras.php"; return false; }
			 
			 
        } else { 
            alert("Houve um problema ao obter os dados:\n" + req.statusText);  
        } 
    } 
} 


function extraiScript(texto){
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            eval(codigo);
        }
    }
}

