Aver, este es el nuevo codigo, arreglado las deficiencias que tenia:
Código:
<script language="javascript">
function nuevoAjax()
{
var xmlhttp=false;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E) { xmlhttp=false; }
}
if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); }
return xmlhttp;
}
function existeArticulo() {
var msj = document.getElementById('MsjVentas');
var art_a_buscar = document.getElementById('Articulo').value;
var mostrador = document.getElementById('mostrador');
if(document.getElementById(art_a_buscar) == undefined) {
var ajax=nuevoAjax();
ajax.open("POST", "/Libreria/tienda.ajax.php", true);
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("accion=ExistenciaDeArticulo&IdArticulo="+art_a_buscar);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
var resultado = ajax.responseText;
if(resultado=='existente') {
var articuloId = document.createElement('div');
articuloId.id=art_a_buscar;
mostrador.appendChild(articuloId);
alert('existe');
return true;
}
else {
msj.innerHTML = 'no existe indio pasmado';
alert('no existe');
return false;
}
}
}
}
else {
alert('ya existe objeto');
return true;
}
}
function buscarArticulo() {
var button = document.getElementById('buscar__');
var art_a_buscar = document.getElementById('Articulo').value;
button.disabled = true;
if(existeArticulo()) {
alert('entre');
if(document.getElementById('Unidades_'+art_a_buscar) == null) {
var unidades = 9;
var descuento = 0;
alert('entre1');
}
else {
alert('entre2');
var unidades = document.getElementById('Unidades_'+art_a_buscar).value;
alert(unidades);
var descuento = document.getElementById('Descuento_'+art_a_buscar).value;
alert(descuento);
}
var ajax=nuevoAjax();
ajax.open("POST", "/Libreria/tienda.ajax.php", true);
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("accion=CargarArticulo&IdArticulo="+art_a_buscar+"&Unidades="+unidades+"&Descuento="+descuento);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
var articuloId = document.getElementById(art_a_buscar);
articuloId.innerHTML = ajax.responseText;
}
}
}
button.disabled = false;
return false
}
</script>
Ahora el unico problema es que la primera vez que ocurre el evento, cuando se crea el objeto,
existeArticulo() debe retornar true, pero no sucede, retorna true
Los alert me indican que todo ocurre como debe, solo no se porque no retorna true