///////////////Buscar

function buscador(vars)
{
    var ids = Array();
    var i = 0;
    $w(vars).each(function(vals){
      ids[i]=vals;
      i++;
    })
    
    window.location = 'index.php?c=catalogo&r='+ids[1]+'&sr='+ids[2]+'&p='+ids[0];
    
}

////////////Newsletter
function is_email(email)
{
    var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
    if(result > -1)
    { 
        return true;
    }
    else
    {
        alert ("Correo Electronico: Formato Incorrecto");
        return false;
    }
}

function registro_news()
{
    if($("machine").value=="" && is_email($("mail").value))
    {
        var url = "registrar_suscriptor.php?email="+$("mail").value+"&ran="+Math.random();

        new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(transport) {
                $("resultado_suscripcion").innerHTML = "<span>Gracias por suscribirse, pronto recibiras nuestras mejores ofertas!</span>";
              },
            onFailure: function() {
                alert("Ocurrio un error al interntar suscribirlo a nuestro newsletter, intentelo nuevamente más tarde");
            }
        });      
        
    }
    return false;
}

///Carrito
function qty_add(n)
{
    var actual = parseInt($("quantity").value);
    var nuevo = 1;
    if(actual == 1 && n == -1)
    {
        nuevo = 1;
    }
    
    if(actual > 1 && n == -1)
    {
        nuevo = actual - 1;
    }
    
    if(n == 1)
    {
        nuevo = actual + 1;
    }
    
    $("quantity").value = nuevo; 
}

function qty_ver()
{
    var actual = parseInt($("quantity").value);  
    if(actual < 1)
    {
        $("quantity").value = 1;   
    }   
}

function agregar(producto_id, accion)
{
    var g = new k.Growler();  
    var url = "ecommerce.php?accion="+accion+"&p="+producto_id+"&agr=1&qty="+$("quantity").value+"&ran="+Math.random();
   
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            g.info("¡Ud agrego con exito el producto en el carrito!", {header: "Agregar al Carrito:", life: 6, location: "bl"}); 
            var i = 0;
            var vals = new Array();
            
            $w(transport.responseText).each(function(t){
                vals[i]=t;
                i++;        
            }); 
            
            $("cant").innerHTML = vals[0];
            $("sub").innerHTML = vals[1]; 
          },
        onFailure: function() {
            g.warn("¡Ocurrio un error!<br/>"+transport.responseText, {life: 10});
        }
    });    
}
  /*
function actualizar(producto_id)
{
    var url = "ecommerce.php?p="+producto_id+"&upd=1&cantidad=0&ran="+Math.random();
    
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            var i = 0;
            var vals = new Array();
            
            $w(transport.responseText)).each(function(t){
                vals[i]=t;
                i++;        
            }); 
            
            $("cant").innerHTML = vals[0];
            $("sub").innerHTML = vals[1];
          },
        onFailure: function() {
            alert("Ocurrio un error, intentelo nuevamente más tarde");
        }
        
        
    });    
}*/

//Recomendar Producto
//function recomendar(p) {
//  Lightview.show({
//    href: 'raua.php',
//    rel: 'ajax',
//    options: {
//      autosize: true,
//      topclose: true,
//      ajax: {
//        onComplete: function() {
          // once the request is complete we observe the form for a submit
          //$('ajaxForm').observe('submit', submitAjaxFormDemonstration);
//        }
//      }
//    }
//  });
//}

//Mi Cuenta Login

function login() {
  Lightview.show({
    href: 'content/mi.cuenta.login.form.php',
    rel: 'ajax',
    options: {
      autosize: true,
      topclose: true,
      ajax: {
        onComplete: function() {
          // once the request is complete we observe the form for a submit
          //$('ajaxForm').observe('submit', submitAjaxFormDemonstration);
        }
      }
    }
  });
}

function executeLogin()
{              
    if($("login_name").value!="" && $("login_pass").value!="")
    {         
        var url = "content/login.php?email="+$("login_name").value+"&pass="+$("login_pass").value+"&ran="+Math.random();
                
        new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(transport) {
                
                if(transport.responseText != 0)
                {
                    if($("login_place").value == "registro")
                    {
                        window.location = "index.php?c=micuenta";        
                    }
                    
                    if($("login_place").value == "checkout")
                    {
                        window.location = "index.php?c=checkout&o=dp";        
                    }
                }
                else
                {
                    $("error").innerHTML = "El E-mail/Usuario o Contraseña son incorrectos!";
                }                                
              },
            onFailure: function() {
                g.warn("¡Ocurrio un error al intentar loguearse!<br/>"+transport.responseText, {life: 10});  
            }
        });     
    }
    else
    {
        alert("Ud. debe completar todos los campos!");
    }
}

function executeLoginCheckout()
{              
    if($("login_name_co").value!="" && $("login_pass_co").value!="")
    {         
        var url = "content/login.php?email="+$("login_name_co").value+"&pass="+$("login_pass_co").value+"&ran="+Math.random();
                
        new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(transport) {
                
                if(transport.responseText != 0)
                {
                    if($("login_place_co").value == "registro")
                    {
                        window.location = "index.php?c=micuenta";        
                    }
                    
                    if($("login_place_co").value == "checkout")
                    {
                        window.location = "index.php?c=checkout&o=dp";        
                    }
                }
                else
                {
                    $("error").innerHTML = "El E-mail/Usuario o Contraseña son incorrectos!";
                }                                
              },
            onFailure: function() {
                g.warn("¡Ocurrio un error al intentar loguearse!<br/>"+transport.responseText, {life: 10});  
            }
        });     
    }
    else
    {
        alert("Ud. debe completar todos los campos!");
    }
}

function favoritos(c, p)
{
    if(c!="" && c>0)
    {
        var g = new k.Growler();                
        var url = "content/mi.cuenta.agregar.favorito.php?c="+c+"&p="+p+"&ran="+Math.random();
                
        new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(transport) {
                if(transport.responseText != 0)
                {
                    g.info("¡Ud agrego el producto a Mis Deseados!", {header: "Mis Deseados:", life: 6, location: "bl"}); 
                }
                else
                {
                    $("error").innerHTML = "El producto puede que ya se encuentre agregados a su lista de Deseados";
                }              
                
              },
            onFailure: function() {
                alert("Ocurrio un error!");
            }
        });     
    }
    else
    {
        alert("Ud. debe estar registrado para poder agregar productos a Mis Deseados!");
    }   
}

function favoritosDel(c, p)
{
    if(c!="" && c>0)
    {
        var g = new k.Growler();                
        var url = "content/mi.cuenta.eliminar.favorito.php?c="+c+"&p="+p+"&ran="+Math.random();
                
        new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(transport) {
                if(transport.responseText != 0)
                {
                    g.info("¡Ud eliminó el producto de Mis Deseados!", {header: "Mis Deseados:", life: 6, location: "bl"}); 
                    $("favorito_"+p).fade();
                }
                else
                {
                    //
                }                                 
              },
            onFailure: function() {
                alert("Ocurrio un error!");
            }
        });     
    }
    else
    {
        alert("Ud. debe estar registrado para poder agregar productos a Mis Deseados!");
    }   
}


/*Checkout*/

function idem_domicilio()
{
    if($("ck_idem_domicilio").checked)
    {
        $("ck_direccion").value = $("id_dom").innerHTML;
        $("ck_direccion").disabled = true;
        $("ck_direccion").setStyle({background:'#e0e0e0'});
        
        $("ck_numero").value = $("id_num").innerHTML;
        $("ck_numero").disabled = true;
        $("ck_numero").setStyle({background:'#e0e0e0'});
        
        $("ck_barrio").value = $("id_bar").innerHTML;
        $("ck_barrio").disabled = true;
        $("ck_barrio").setStyle({background:'#e0e0e0'});
        
        $("ck_cp").value = $("id_zip").innerHTML;
        $("ck_cp").disabled = true;
        $("ck_cp").setStyle({background:'#e0e0e0'});
        
        $("ck_ciudad").value = $("id_ciu").innerHTML;
        $("ck_ciudad").disabled = true;
        $("ck_ciudad").setStyle({background:'#e0e0e0'});
        
        $("ck_provincia").value = $("id_pro").innerHTML;
        $("ck_provincia").disabled = true;
        $("ck_provincia").setStyle({background:'#e0e0e0'});
        
    }
    else
    {                                                   
        $("ck_direccion").disabled = false;
        $("ck_direccion").setStyle({background:'#FFF'});
                                                    
        $("ck_numero").disabled = false;
        $("ck_numero").setStyle({background:'#FFF'});
                                                     
        $("ck_barrio").disabled = false;
        $("ck_barrio").setStyle({background:'#FFF'});
                                                 
        $("ck_cp").disabled = false;
        $("ck_cp").setStyle({background:'#FFF'});
                                                    
        $("ck_ciudad").disabled = false;
        $("ck_ciudad").setStyle({background:'#FFF'});
                                       
        $("ck_provincia").disabled = false;
        $("ck_provincia").setStyle({background:'#FFF'});    
    }
}

function activar_campos()
{                                                   
    $("ck_direccion").disabled = false;                                                 
    $("ck_numero").disabled = false;                                                  
    $("ck_barrio").disabled = false;                                               
    $("ck_cp").disabled = false;                                                  
    $("ck_ciudad").disabled = false;                                     
    $("ck_provincia").disabled = false; 
}

var timeout_brands_id = null;

function ver_marcas()
{
    //$("listado_marcas").appear('duration: 0.5');
    $("listado_marcas").show();
    window.clearTimeout(timeout_brands_id);
}

function ocultar_marcas()
{
    timeout_brands_id = window.setTimeout(
    function() { 
        //$("listado_marcas").fade('duration: 0.5'); 
        $("listado_marcas").hide(); 
    }, 500);
}

//Event.observe(window, 'load', function() {
//    Event.observe($("listado_marcas"), 'mouseover', ver_marcas); 
//    Event.observe($("buscar_por_marcas"), 'mouseover', ver_marcas); 
//    Event.observe($("listado_marcas"), 'mouseout', ocultar_marcas); 
//    Event.observe($("buscar_por_marcas"), 'mouseout', ocultar_marcas); 
//});

var buscar = "Buscar...";

Event.observe(window, 'load', function(){
    zIndexWorkaround(); 
    
    $("q").value = buscar;
    
    Event.observe("q", "focus", function() {
        $("q").value = "";
    });
    
    Event.observe("q", "blur", function() {
        $("q").value = buscar;
    });     
});


function isIE()
{
    if(navigator.userAgent.match(/MSIE \d\.\d+/))
        return true;
    return false;
}
 
function zIndexWorkaround()
{
    if(navigator.userAgent.match(/MSIE \d\.\d+/))
    {    
        var zi = 1000;
        $$("ul.submenu").each(function(block) {
            block.style.zIndex = zi--;
        });
        
        $("listado_marcas").style.zIndex = zi-- ; 
        
        $$(".bot-marcas").each(function(block) {
            block.style.zIndex = zi--;
        }); 
        
        $$(".content-izq").each(function(block) {
            block.style.zIndex = zi--;
        }); 
    }      
}
