var EstaEditando = false; //variable para ver que la opcion editar esta disponible
function AJAX(){ //funcion que sirve para conectarse
    var ajaxs = ["Msxml2.XMLHTTP","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.3.0","Microsoft.XMLHTTP"];
    var ajax = false;
    for (var i=0 ; !ajax && i<ajaxs.length ; i++) {
        try{ ajax = new ActiveXObject(ajaxs[i]); }
        catch(e) { ajax = false; }
    }
    if(!ajax && typeof XMLHttpRequest!='undefined') {
        ajax = new XMLHttpRequest();
    }
    return ajax;
}

function EditarCampo(fil,col,subcampo){
    if(EstaEditando)return false;
    EstaEditando = true;
    var campo = document.getElementById("fil"+fil+"col"+col);
    campo.innerHTML = "<input maxlength=\"100\" type=\"text\" value=\"" + subcampo.innerHTML + "\" size=\"10\" onblur=\"return Guardar(this,"+fil+","+col+")\" />";
    campo.firstChild.focus();
}

function Guardar(subcampo,fil,col){
    var ajax = AJAX();
    if(!ajax){
        alert("Imposible Editar el Campo");
        return false;
    }
    document.getElementById("capaload").style.visibility = "visible";
    ajax.open("POST","insertar.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            campo = document.getElementById("fil"+fil+"col"+col);
            campo.innerHTML = subcampo.value;
            EstaEditando = false;
            document.getElementById("capaload").style.visibility = "hidden";
        }
    }
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("fil="+fil+"&col="+col+"&valor="+subcampo.value);
}

function actualizaCarrito (eltovisible) {
    if (eltovisible==null) {
        eltostr="";
    }
    else {
        eltostr="elto="+eltovisible;
    }
    var ajax = AJAX();
    if(!ajax) {
        alert("No se pudo actualizar el carrito");
        return false;
    }
    ajax.open ("POST","miajax/contenidocarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            campo = document.getElementById('cuerpopanelverde');
            campo.innerHTML = ajax.responseText;
            EstaEditando = false;
            document.getElementById("cartelon").style.visibility = "hidden";
//          actualizaCarritoXml();
        }
    }
    ajax.send( eltostr ); // no mandamos parámetros
    //    return true;
}

function actualizaCarritoXml (eltovisible) {
    if (eltovisible==null) {
        eltostr="";
    }
    else {
        eltostr="elto="+eltovisible;
    }
    var ajax = AJAX();
    if(!ajax) {
        alert("No se pudo actualizar el carrito");
        return false;
    }
    ajax.open ("POST","miajax/contenidocarritoxml.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            var xml,p,it;
            campo = document.getElementById('seccionCarrito');
         // campo.innerHTML = ajax.responseText;
            xml=ajax.responseXML;
            p=xml.getElementsByTagName('info').item(0);
            alert("Primero: "+p.firstChild.data);
            alert("Ultimo: "+p.childNodes[p.childNodes.length].data);
            EstaEditando = false;
            document.getElementById("cartelon").style.visibility = "hidden";
        }
    }
    ajax.send( eltostr ); // no mandamos parámetros
    //    return true;
}

function actualizaCarritoPadre (eltovisible) {
    if (eltovisible==null) {
        eltostr="";
    }
    else {
        eltostr="elto="+eltovisible;
    }
    var ajax = AJAX();
    if(!ajax) {
        alert("No se pudo actualizar el carrito");
        return false;
    }
    ajax.open ("POST","miajax/contenidocarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            campo = opener.document.getElementById('cuerpopanelverde');
            campo.innerHTML = ajax.responseText;
            EstaEditando = false;
            document.getElementById("cartelon").style.visibility = "hidden";
//          actualizaCarritoXml();
        }
    }
    ajax.send( eltostr ); // no mandamos parámetros
    //    return true;
}


// function addcart(subcampo,fil,col){
function addcart(elto,agrupacion) {
    // si no defino las variables el IE explota
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    document.getElementById("txt_cartelon").innerHTML="A&ntilde;adiendo articulo a carrito";
    document.getElementById("cartelon").style.top = (y-20)+"px";
    document.getElementById("cartelon").style.left = (x-500)+"px";
    // tan largo para que no caiga encíma de los select que en IE6 no funciona
    document.getElementById("cartelon").style.zIndex = 100;
    document.getElementById("cartelon").style.visibility = "visible";

    ajax.open("POST","miajax/modificarcarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
//          campo = document.getElementById("fil"+fil+"col"+col);
//          campo.innerHTML = subcampo.value;
            EstaEditando = false;
//          document.getElementById("cartelon").style.visibility = "hidden";
            actualizaCarrito();
        }
    }
    campoUnidades=document.getElementById('cantidad'+agrupacion);
    campoItem=document.getElementById('precios'+agrupacion);
    unidades=campoUnidades.value;
    item=campoItem.options[campoItem.selectedIndex].value;
    campoUnidades.value='';

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//  ajax.send("fil="+fil+"&col="+col+"&valor="+subcampo.value);
    ajax.send("item="+item+"&unidades="+unidades);
}

function addcart2(elto,agrupacion) {
    // si no defino las variables el IE explota
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    document.getElementById("txt_cartelon").innerHTML="A&ntilde;adiendo articulo a carrito";
    document.getElementById("cartelon").style.top = (y-20)+"px";
    document.getElementById("cartelon").style.left = (x-500)+"px";
    // tan largo para que no caiga encíma de los select que en IE6 no funciona
    document.getElementById("cartelon").style.zIndex = 100;
    document.getElementById("cartelon").style.visibility = "visible";

    ajax.open("POST","miajax/modificarcarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
//          campo = document.getElementById("fil"+fil+"col"+col);
//          campo.innerHTML = subcampo.value;
            EstaEditando = false;
//          document.getElementById("cartelon").style.visibility = "hidden";
            actualizaCarrito();
        }
    }
    campoUnidades=document.getElementById('cantidad'+agrupacion);
    //campoItem=document.getElementById('precios'+agrupacion);
    unidades=campoUnidades.value;
    //item=campoItem.options[campoItem.selectedIndex].value;
    campoUnidades.value='';

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//  ajax.send("fil="+fil+"&col="+col+"&valor="+subcampo.value);
    ajax.send("item="+elto+"&unidades="+unidades);
}

function addproduct(elto,agrupacion) {
    // si no defino las variables el IE explota
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    document.getElementById("txt_cartelon").innerHTML="A&ntilde;adiendo articulo a carrito";
    document.getElementById("cartelon").style.top = (y-20)+"px";
    document.getElementById("cartelon").style.left = (x-500)+"px";
    document.getElementById("cartelon").style.zIndex = 100;
    document.getElementById("cartelon").style.visibility = "visible";

    ajax.open("POST","miajax/modificarcarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            EstaEditando = false;
            actualizaCarrito();
        }
    }
    campoUnidades=document.getElementById('cantidad'+agrupacion);
    campoItem=document.getElementById('item'+agrupacion);
    unidades=campoUnidades.value;
    item=campoItem.value;
    campoUnidades.value='';

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("item="+item+"&unidades="+unidades+"&tipo=2");
}

function addcartFicha(elto,agrupacion) {
    // si no defino las variables el IE explota
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    document.getElementById("txt_cartelon").innerHTML="A&ntilde;adiendo articulo a carrito";
    document.getElementById("cartelon").style.top = "0px";
    document.getElementById("cartelon").style.left = "0px";
    // tan largo para que no caiga encíma de los select que en IE6 no funciona
    document.getElementById("cartelon").style.zIndex = 100;
    document.getElementById("cartelon").style.visibility = "visible";

    ajax.open("POST","miajax/modificarcarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
//          campo = document.getElementById("fil"+fil+"col"+col);
//          campo.innerHTML = subcampo.value;
            EstaEditando = false;
//          document.getElementById("cartelon").style.visibility = "hidden";
            actualizaCarritoPadre();
        }
    }
    campoUnidades=document.getElementById('cantidad'+agrupacion);
    campoItem=document.getElementById('precios'+agrupacion);
    unidades=campoUnidades.value;
    item=campoItem.options[campoItem.selectedIndex].value;
    campoUnidades.value='';

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//  ajax.send("fil="+fil+"&col="+col+"&valor="+subcampo.value);
    ajax.send("item="+item+"&unidades="+unidades);
}

function addproductFicha(elto,agrupacion) {
    // si no defino las variables el IE explota
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    document.getElementById("txt_cartelon").innerHTML="A&ntilde;adiendo articulo a carrito";
    document.getElementById("cartelon").style.top = "0px";
    document.getElementById("cartelon").style.left = "0px";
    document.getElementById("cartelon").style.zIndex = 100;
    document.getElementById("cartelon").style.visibility = "visible";

    ajax.open("POST","miajax/modificarcarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            EstaEditando = false;
            actualizaCarritoPadre();
        }
    }
    campoUnidades=document.getElementById('cantidad'+agrupacion);
    campoItem=document.getElementById('item'+agrupacion);
    unidades=campoUnidades.value;
    item=campoItem.value;
    campoUnidades.value='';

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("item="+item+"&unidades="+unidades+"&tipo=2");
}

function vaciarCarrito() {
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    ajax.open("POST","miajax/vaciarcarrito.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            EstaEditando = false;
            window.location.replace( window.location.href );
            }
    }
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send(null);
}

function cambiaGastosDeEnvio() {
    // si no defino las variables el IE explota
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    ajax.open("POST","miajax/gastosdeenvio.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            campo = document.getElementById("txt_gastosdeenvio");
            campo.innerHTML = ajax.responseText;
//          document.getElementById("cartelon").style.visibility = "hidden";
        }
    }
    campoTipo=document.getElementById('tipoenvio');
    tipo=campoTipo.options[campoTipo.selectedIndex].value;

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("t="+tipo);
}


function actualizaPago() {
    // si no defino las variables el IE explota
    var item;
    var unidades;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    ajax.open("POST","miajax/metodosdepago.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            campo = document.getElementById("hoja2");
            campo.innerHTML = ajax.responseText;
			document.getElementById("cartelon").style.visibility = "hidden";
        }
    }
    campoPago=document.getElementById('metodopago');
    tipo=campoPago.options[campoPago.selectedIndex].value;

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("p="+tipo);
	document.getElementById("cartelon").style.visibility = "visible";
}

function actualizaEnvio() {
    // si no defino las variables el IE explota
    var tipo;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    ajax.open("POST","miajax/metodosdeenvio.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            campo = document.getElementById("hoja2");
            campo.innerHTML = ajax.responseText;
			document.getElementById("cartelon").style.visibility = "hidden";
        }
    }
    campoPago=document.getElementById('metodopago');
    tipo=campoPago.options[campoPago.selectedIndex].value;

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("p="+tipo);
	document.getElementById("cartelon").style.visibility = "visible";
}

function actualizaResumen() {
    // si no defino las variables el IE explota
    var pago;
    var envio;
    var ajax = AJAX();
    if(!ajax){
        alert("No se pudo realizar la transaccion");
        return false;
    }
    ajax.open("POST","miajax/resumen.php",true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            campo = document.getElementById("hoja3");
            campo.innerHTML = ajax.responseText;
//          document.getElementById("cartelon").style.visibility = "hidden";
        }
    }
    campoPago=document.getElementById('metodopago');
    pago=campoPago.options[campoPago.selectedIndex].value;
    campoEnvio=document.getElementById('metodoenvio');
    envio=campoEnvio.options[campoEnvio.selectedIndex].value;

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("p="+pago+"&e="+envio);
}


