﻿function alCarrito(aform,idproducto,cantidad) {
   aform.action = "/action/cat/oppedido.aspx";
   aform.op.value = "add";
   aform.id.value = idproducto;
   aform.cantidad.value = cantidad;
   aform.urlretorno.value = location.href;
   aform.submit();
}


function delCarrito(aform,idproducto) {
   aform.action = "/action/cat/oppedido.aspx";
   aform.op.value = "del";
   aform.id.value = idproducto;   
   aform.urlretorno.value = location.href;
   aform.submit();
}


function showCarrito(aform) {
   aform.action = "/action/cat/oppedido.aspx";
   aform.op.value = "show";
   aform.urlretorno.value = location.href;
   aform.submit();
}



function checkCantidad(cant){   
   if(isNaN(parseInt(cant)))
      return false;
   else
      var ic = parseInt(cant) 
      return (ic>0 && ic<=9999)
}



function modCantidad(aform,idproducto,cantField) {
   if (checkCantidad(cantField.value)) { 
       aform.action = "/action/cat/oppedido.aspx";
       aform.op.value = "modcant";
       aform.id.value = idproducto;
       aform.cantidad.value = parseInt(cantField.value);
       aform.urlretorno.value = location.href;
       aform.submit();   
   }
   else {
       alert("Introduzca una cantidad correcta");
       cantField.value = "1";
       cantField.focus();
       cantField.select();       
   }
}


function resizeImg(imgid,h,w) {
    var maxwidth = w;
    var maxheight = h;
    var imagen = document.getElementById(imgid);
    if (imagen.width>maxwidth || imagen.height>maxheight) {
        var scale = Math.min((maxwidth/imagen.width),(maxheight/imagen.height), 1 );
        var new_width = Math.floor(scale*imagen.width);
        var new_height = Math.floor(scale*imagen.height);
        imagen.width = new_width;
        imagen.height = new_height;        
    }
    imagen.style.visibility = "visible";
}


var actDisplayID;

function tgDisplay(itemid) {
    var item = document.getElementById(itemid);
    
    if (item.style.display == "none" || item.style.display == "") {
      if ((actDisplayID != null) && (actDisplayID != "")) {
        document.getElementById(actDisplayID).style.display = "none";
      }
    
       item.style.display = "block";
       actDisplayID = itemid;
    } else {
       item.style.display = "none";
       actDisplayID = null;
    }
}


function mostrarLogin() {
   var item = document.getElementById('registro');
   item.style.visibility = "visible";
}


function catLogin(aform) {
   
   if ((aform.catpwd.value == "") || (aform.catuser.value == "")) {
     alert("Introduzca el usuario y la contraseña para continuar");      
   } else {   
     aform.op.value = "login";
     aform.urlretorno.value = location.href;     
     aform.submit();
   }
}

function catSendPassword(formid) {
   aform = document.getElementById(formid);
   if (aform.catuser.value == "") {
     alert("Introduzca el usuario para continuar");      
   } else {
      var orightm = $("#logindync").html();
      $("#logindync").empty().html('Recuperando contraseña ...');
      $.ajax({
         type: "POST",
         url: "/action/cat/login.aspx",
         data: {op: 'sendpassword', catuser: encodeURIComponent(aform.catuser.value)},
         success: function(msg){
           $("#logindync").empty().html(orightm);
           alert( msg );           
         },
         error: function (xhr, ajaxOptions, thrownError){
            $("#logindync").empty().html(orightm);
            alert('Error '+xhr.status+'\n'+thrownError);           
         }         
       });   
   }
}


function catLogout(aform) {
   aform.action = '/action/cat/login.aspx';
   aform.op.value = "logout";
   aform.urlretorno.value = location.href;     
   aform.submit();
}

