function inserirProduto(v_id_produto) {
	//verifica se o browser tem suporte a ajax
	try {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex) {
			try {
				ajax = new XMLHttpRequest();
			}
			catch(exc) {
				window.location='carrinho.php?acao=Inserir&id_produto='+v_id_produto;
				ajax = null;
			}
		}
	}

	//se tiver suporte ajax
	if(ajax) {

		ajax.open("POST", "xml.inserir.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajax.onreadystatechange = function() {
			//enquanto estiver processando...emite a msg de carregando
			if(ajax.readyState == 1) {
				// idOpcao.innerHTML = "Carregando...!";
			}
			//após ser processado - chama função processXML que vai varrer os dados
			if(ajax.readyState == 4 ) {
				if(ajax.responseXML) {
					processaXMLinseirProduto(ajax.responseXML);
					document.images['icone_carrinho_'+v_id_produto].src = "imagens/icone.inserido.gif";
				} else {
					alert('O produto não foi inserido no carrinho, por favor tente novamente');
				}
			}
		}
		//passa o código do produto a ser inserido
		var params = "id_produto="+v_id_produto;
		ajax.send(params);
	}
}

function processaXMLinseirProduto(obj){
	//pega a tag cidade
	var dataArray   = obj.getElementsByTagName("mensagens");

	if(dataArray.length > 0) {
		//percorre o arquivo XML paara extrair os dados
		for(var i = 0 ; i < dataArray.length ; i++) {
			var item = dataArray[i];

			//contéudo dos campos no arquivo XML
			var v_mensagem =  item.getElementsByTagName("mensagem")[0].firstChild.nodeValue;

			alert(v_mensagem);
		}
	} else {
		alert('O produto não foi inserido no carrinho, por favor tente novamente');
	}
}

function criaAjax() {

	try{
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
	return(xmlhttp);

}

function mudaConteudo(v_link,v_div,v_tipo){

	//Exibe o texto carregando no div conteúdo

	if ( v_tipo == 'frame' ) {

		var conteudo=document.getElementById(v_div)
		conteudo.innerHTML='<iframe src="'+v_link+'" frameborder="0" scrolling="auto"></iframe>';

	} else {

		var conteudo=document.getElementById(v_div)
		conteudo.innerHTML='<div class="carregando"><center><br><b>Aguarde, carregando...</b>&nbsp;&nbsp;<img src=\"imagens/indicator.gif\" width=\"16\" height=\"16\" vspace=\"2\" /><br><br></center></div>';

		//Abre a url
		xmlhttp = criaAjax();
		var pars = 'rd='+ Math.random();
		//xmlhttp.open("GET",v_link+'?rn='+pars,true);
		xmlhttp.open("GET",v_link,true);
	
		//Executada quando o navegador obtiver o código
		xmlhttp.onreadystatechange=function() {
	
			if (xmlhttp.readyState==4){
	
				//Lê o texto
				var texto=xmlhttp.responseText;
	
				//Desfaz o urlencode
				texto=texto.replace(/\+/g," ");
				texto=unescape(texto);

				//Exibe o texto no div conteúdo
				conteudo.innerHTML=texto;
	
			}
		}
		xmlhttp.send(null);

	}

}
