﻿function alCarrito(aform,idproducto,cantidad) {
   aform.action = "/shop/ai/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 = "/shop/ai/oppedido.aspx";
   aform.op.value = "del";
   aform.id.value = idproducto;   
   aform.urlretorno.value = location.href;
   aform.submit();
}


function showCarrito(aform) {
   aform.action = "/shop/ai/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<=99)
}



function modCantidad(aform,idproducto,cantField) {
   if (checkCantidad(cantField.value)) { 
       aform.action = "/shop/ai/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";
}

function jqryResizeImg(jimg,h,w) {
    var maxwidth = w;
    var maxheight = h;
    var imagen = new Image();
    imagen.src = jimg.attr('src');
    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);
        jimg.attr('width', new_width);
        jimg.attr('height', new_height);                
    }    
}


function ordenar_listado() {
    var selorder = document.getElementById('selordenlistado');
    urldestino = selorder[selorder.selectedIndex].value ;
    document.location.href = urldestino;
}

function avisoDispo() {
    var avidisin = $('#avidisbox div.avidisin');
    var avidisbox_link = $('#avidisbox a');
    avidisbox_link.hide();
    avidisin.show("slow");
}


function validateAjaxInputs() {
    $.validity.start();
    
    $("#nombreDispo, #emailDispo, #cantiDispo").require('este dato es necesario');
    $("#emailDispo").match('email','el email inroducido es incorrecto');
    $("#cantiDispo").match('integer','la cantidad incorrecta');
    $("#cantiDispo").range(1,999,'la cantidad debe estar entre 1 y 999');
            
    var result = $.validity.end();    
    return result.valid;
}

function inscraviso(referencia) {
    if (validateAjaxInputs()) {
    
        var nombre = $('#nombreDispo').val();
        var email = $('#emailDispo').val();
        var cantidad = $('#cantiDispo').val();
        var parcial =  $('#partDispo').is(':checked');
        var avidisin = $('#avidisbox div.avidisin');
        avidisin.html('<p align="center">Procesando, espere un momento por favor ...</p>');
        $.ajax({
           type: "POST",
           url: "/shop/ai/oppedido.aspx",
           data: "op=avidispo&ref="+referencia+"&nombre="+nombre+"&email="+email+"&cant="+cantidad+"&parcial="+parcial,
           success: function(msg) {         
             if (msg == 'ok')         
               avidisin.html('Hemos recibido su petición correctamente. Le avisaremos mediante un email en cuanto el producto esté disponible para su compra. Muchas gracias.');
             else
               avidisin.html('Por razones técnicas, no ha sido posible registrar su petición. Por favor, vuelva a intentarlo más tarde.');         
           }
         });
     }
}

