(function ($, Drupal) {
Drupal.behaviors.wid = {
attach: function(context, settings) {
console.log('wid');
/*$('#widget-pedido-form #edit-checob', context).once('ch').each(function(){
console.log('check');
$('#widget-pedido-form').on('submit', function(e){
e.preventDefault();
console.log('prevent');
});
$('#widget-pedido-form button.checob', context).once('checob').on('click', function(){
console.log('Click en el botón');
//Calcular
});
$('#widget-pedido-form input[type="text"]', context).once('mov1').on('keyup', function(e){
console.log('keyup');
chequearCampos();
});
$('#widget-pedido-form select', context).once('mov2').on('change', function(e){
console.log('change');
chequearCampos();
});
});*/
function chequearCampos(){
var sumar = 0;
var tipo = $('#widget-pedido-form select').val();
var cant = ($('#widget-pedido-form input[name="cantidad"]').val()).trim();
var cp = ($('#widget-pedido-form input[name="cp-municipio"]').val()).trim();
console.log('CP: ' + cp + '. TIPO: ' + tipo + '. Liters: ' + cant);
if(tipo != ''){
sumar++;
} else {
console.log('Select vacío');
}
if(cp != ''){
//Comprobar formato?
if(cp.length == 5){
if($.isNumeric(cp)){
sumar++;
$('#widget-pedido-form span.msg_cp').html('');
} else {
$('#widget-pedido-form span.msg_cp').html('Sólo se permiten números en este campo')
console.log('CP: no es numero');
}
} else {
console.log('CP: Menos de 5');
}
} else {
console.log('CP: vacío');
}
if(cant != ''){
//Comprobar formato?
if(cant.length < 3){
$('#widget-pedido-form span.msg_cantidad').html('No llega a la cantidad mínima')
console.log('Litros: menos de 3');
} else {
if($.isNumeric(cant)){
sumar++;
$('#widget-pedido-form span.msg_cantidad').html('');
} else {
$('#widget-pedido-form span.msg_cantidad').html('Sólo se permiten números en este campo')
console.log('Litros: no es numero');
}
}
} else {
console.log('Litros: vacío');
}
$('#widget-pedido-form button.checob').attr('disabled', sumar < 3);
console.log('Sumar es: ' + sumar);
}
}
};
})(jQuery, Drupal);