/*------------------------------------
FUNCIONES JAVASCRIPT
------------------------------------*/

function valida_usuarios() {

    nombre = document.usuarios.usua_nombre.value;
    direccion = document.usuarios.usua_direccion.value;
    pais = document.usuarios.pais_id.value;
    provincia = document.usuarios.prov_id.value;
    localidad = document.usuarios.loca_id.value;
    cp = document.usuarios.usua_cp.value;
    telefono = document.usuarios.usua_telefono.value;
    mail = document.usuarios.usua_mail.value;

    if(nombre=="" || isNaN(nombre)==false) {
        alert("Ingrese un nombre correcto.");
        return false;
    }else if(direccion=="") {
        alert("Ingrese una direccion correcta.");
        return false;
    }else if(pais==0) {
        alert("Seleccione un país correcto.");
        return false;
    }else if(provincia==0) {
        alert("Seleecione una provincia correcta.");
        return false;
    }else if(localidad==0) {
        alert("Seleecione una localidad correcta.");
        return false;
    }else if(cp==0 || cp=="") {
        alert("Ingrese un código postal correcto.");
        return false;
    }else if(telefono=="") {
        alert("Ingrese un teléfono correcto.");
        return false;
    }else if(mail=="") {
        alert("Ingrese un correo electrónico correcto.");
        return false;
    }else {
        return true;
    }
}

/*-------------------------------------------------------------------------------*/
function envia_form(form, id) {
    if(id) {
        document.getElementById('id_' + form).value = id;
    }

    form = document.getElementById(form);

    form.submit();

}

/*-------------------------------------------------------------------------------*/
function toogle(div) {
    div = document.getElementById(div);

    if(div.style.display=="none") {
        div.style.display = "";
    }else if(div.style.display=="") {
        div.style.display = "none";
    }
}

/*-------------------------------------------------------------------------------*/
function toogle_imagen(img, pos) {

    switch(pos) {
        case 1:
            document.getElementById(img+"_2").style.display = "none";
            document.getElementById(img+"_3").style.display = "none";
            document.getElementById(img+"_4").style.display = "none";
            document.getElementById(img+"_"+pos).style.display = "";
            break;
        case 2:
            document.getElementById(img+"_1").style.display = "none";
            document.getElementById(img+"_3").style.display = "none";
            document.getElementById(img+"_4").style.display = "none";
            document.getElementById(img+"_"+pos).style.display = "";
            break;
        case 3:
            document.getElementById(img+"_1").style.display = "none";
            document.getElementById(img+"_2").style.display = "none";
            document.getElementById(img+"_4").style.display = "none";
            document.getElementById(img+"_"+pos).style.display = "";
            break;
        case 4:
            document.getElementById(img+"_1").style.display = "none";
            document.getElementById(img+"_2").style.display = "none";
            document.getElementById(img+"_3").style.display = "none";
            document.getElementById(img+"_"+pos).style.display = "";
            break;
    }

}

/*-------------------------------------------------------------------------------*/
function acciones(accion, prod, cant){
    switch(accion){
        case "quitar":
            if(confirm("¿Está seguro que desea eliminar el producto " + prod + " de su Nota de Pedido?")==true){
                return true;
            }else{
                return false;
            }
        break;

        case "actualizar":
            if(isNaN(cant.value)==true || cant.value<=0) {
                alert("Debe ingresar una cantidad válida.");
                cant.focus();
                return false;
            }else{
                document.getElementById('acc').value="act";
                document.carrito.submit();
                return true;
            }
        break;

        case "validar":
            if(confirm("¿Desea confirmar el pedido realizado? \r\n Una vez confirmado no podrá cancelarlo.")==true) {
                document.getElementById('acc').value="ok";
                return true;
            }else {
                return false;
            }
        break;
    }
}

/*-------------------------------------------------------------------------------*/
function valida_envios_pagos(theForm){
    envio=0;
    pago=0;
    if(theForm.envio.length==undefined){
        if(theForm.envio.checked==true){
             envio=1;
        }else{
            envio=0;
        }
    }else{
        for(i=0;i<theForm.envio.length;i++) {
            if(theForm.envio[i].checked==true){
                 envio=1;
                 break;
            }
        }
    }

    if(theForm.pago.length==undefined){
        if(theForm.pago.checked==true){
             pago=1;
        }else{
            pago=0;
        }
    }else{
        for(i=0;i<theForm.pago.length;i++) {
            if(theForm.pago[i].checked==true){
                 pago=1;
                 break;
            }
        }
    }

    if(envio==1 && pago==1){
        return true;
    }else{
        alert("Revise de haber seleccionado un método de envío y de pago válidos.");
        return false;
    }

}

/*-------------------------------------------------------------------------------*/
function valida_contacto(){
    nombre=document.contacto.nombre.value;
    telefono=document.contacto.telefono.value;
    correo=document.contacto.correo.value;
    mensaje=codument.contacto.mensaje;

    if(nombre=="" || isNaN(nombre)==false){
        alert("Debe ingresar un nombre válido.");
        return false;
    }else if(telefono=="" || telefono==0 || telefono.length<5){
        alert("Debe ingresar un teléfono válido.");
        return false;
    }else if(correo=="" || correo==0 || correo.length<10){
        alert("Debe ingresar un correo electrónico válido.");
        return false;
    }else if(mensaje=="" || mensaje==0 || mensaje.length<10){
        alert("Debe ingresar un mensaje válido.");
        return false;
    }else{
        return true;
    }
}

/*-------------------------------------------------------------------------------*/





/*------------------------------------
FUNCIONES AJAX
------------------------------------*/

function actualiza_combo(combo1, combo2_id) {
    valor1 = combo1.value;

    combo2 = document.getElementById(combo2_id);

    combo1.disabled = true;
    combo2.disabled = true;

    if(combo2_id=="loca_id") {
        $.ajax(
            {
            type: 'post', //document.usuariosa en que envo los datos
            dataType: 'json', //document.usuariosa en que devuelve los datos
            url: 'core/ajax.php', //pagina que procesa y devuelve los datos
            data: {prov_id: valor1}, //indocument.usuariosacion que envio a traves del 'type' y que recibe la pag q procesa
            success: function(json) {
                    limpia_combo(combo2);
                    llena_combo(json, combo2);
                    combo1.disabled = false;
                    combo2.disabled = false;
                }

            }
        )
    }else if(combo2_id=="subr_id") {
        $.ajax(
            {
            type: 'post', //document.usuariosa en que envo los datos
            dataType: 'json', //document.usuariosa en que devuelve los datos
            url: 'core/ajax.php', //pagina que procesa y devuelve los datos
            data: {rubr_id: valor1}, //indocument.usuariosacion que envio a traves del 'type' y que recibe la pag q procesa
            success: function(json) {
                    limpia_combo(combo2);
                    llena_combo(json, combo2);
                    combo1.disabled = false;
                    combo2.disabled = false;
                }

            }
        )
    }

}

/*---------------------------------------------------------------------------*/
function limpia_combo(combo) {
    while(combo.length>0) {
        combo.remove(combo.length-1);
    }
}

/*---------------------------------------------------------------------------*/
function llena_combo(json, combo) {
    if(json[0].id==0) {
        for(i = 0; i<json.length; i++) {
            combo.options[combo.length] = new Option(json[i].nombre, json[i].id);
        }
    }else {
        combo.options[0] = new Option("-- Seleccione una opcion --", 0);
        for(i = 0; i<json.length; i++) {
            combo.options[combo.length] = new Option(json[i].nombre, json[i].id);
        }
    }
}

/*-------------------------------------------------------------------------------*/
function TildarAgregar(prodID){
  txtCant=document.getElementById('cant'+prodID);
  chkProd=document.getElementById('chk'+prodID);
  if(isNaN(txtCant.value)==true || txtCant.value<=0) {
      chkProd.checked=false;
  }else{
      chkProd.checked=true;
  }
}

function LlenarCantDef(prodID){
  txtCant=document.getElementById('cant'+prodID);
  chkProd=document.getElementById('chk'+prodID);
  if(chkProd.checked==true) {
      txtCant.value = 1;
  }else{
      txtCant.value = "";
  }
}


function MostrarPrevFotos(nombre, val1, val2){
  if (val1!=''){val1="images/productos/big/"+val1;}else{val1="images/productos/no_disponible_big.jpg";}
  //if (val2!=''){val2="images/productos/big/"+val2;}else{val2="images/productos/no_disponible_big.jpg";}

  document.getElementById('PrevFotos').style.display = "";
  document.getElementById('PrevTitulo').innerHTML  = "<b>" + nombre + "</b>";
  foto1=document.getElementById('prevFoto1');
  //foto2=document.getElementById('prevFoto2');
  foto1.setAttribute("src",val1);
  //foto2.setAttribute("src",val2);
}
