﻿/********** CONSTANTES **********/


var ESTAMPACION_CAMISETA = 15.00;
var LETRA_ADICIONAL = 4.00;




/********** FUNCIONES **********/


var FORMULARIO = {
	
    init : function() {
        var imgs = document.getElementsByTagName('img');
        for (i=0;i<imgs.length;i++) {
            if (imgs[i].src.length < 30 ) {
                imgs[i].src = 'notfound.gif';
            }
        }
        var campos = document.getElementsByTagName('input');
        for (i=0;i<campos.length;i++) {
            if (campos[i].type == 'text') {
                campos[i].onfocus = function() {
                    this.select();
                }
            }
        }
        campos = document.getElementsByTagName('textarea');
        for (i=0;i<campos.length;i++) {
            campos[i].onfocus = function() {
                this.select();
            }
        }
    }

}




var CESTA = {
	
    init : function() {
        var contenedores = document.getElementsByTagName('div');
        for (i=0;i<contenedores.length;i++) {
            if (contenedores[i].className == 'cesta') {
                if(contenedores[i].getElementsByTagName('a').length > 0)
                {
                    contenedores[i].getElementsByTagName('a')[1].onclick = function() {
                        if (this.parentNode.parentNode.className == '')
                            this.parentNode.parentNode.className = 'activa';
                        else
                            this.parentNode.parentNode.className = '';
                        return false;
						
                    }
                }
            }
        }
    }

}


var contenedorVelo;

var VELO = {
	
    init : function() {
        var contenedores = document.getElementsByTagName('div');
        for (i=0;i<contenedores.length;i++) {
            if (contenedores[i].className == 'accesos-directos') {
                contenedores[i].getElementsByTagName('a')[3].onclick = function() {
                    VELO.mostrar('aviso-legal');
                    return false;
                }
            } else if (contenedores[i].className == 'velo') {
                contenedorVelo = contenedores[i];
                for (j=0;j<contenedorVelo.getElementsByTagName('a').length;j++) {
                    if (contenedorVelo.getElementsByTagName('a')[j].innerHTML .indexOf('Cerrar ventana') != -1) {
                        contenedorVelo.getElementsByTagName('a')[j].onclick = function() {
                            VELO.ocultar();
                            return false;
                        }
                    }
                }
            }
        }
        document.onkeypress = function esc(e) {
            if (typeof(e) == "undefined")
                e = event;
            if (e.keyCode == 27)
                VELO.ocultar();
        }
    },
	
    mostrar : function(cual) {
        if (navigator.appVersion.indexOf('MSIE 6.0') != -1) {
            for (k=0;k<document.getElementsByTagName('select').length;k++) {
                document.getElementsByTagName('select')[k].style.visibility = 'hidden';
            }
        }
        for (k=0;k<contenedorVelo.getElementsByTagName('div').length;k++) {
            if (contenedorVelo.getElementsByTagName('div')[k].className == cual)
                var seleccionado = contenedorVelo.getElementsByTagName('div')[k];
        }
        var alto = ( seleccionado.currentStyle != null)?parseInt(seleccionado.currentStyle.height):parseInt(window.getComputedStyle(seleccionado, null).getPropertyValue("height"));
        seleccionado.style.top = parseInt((parseInt(document.documentElement.clientHeight) - alto)/2) + document.documentElement.scrollTop + 'px';
        contenedorVelo.style.height = document.documentElement.scrollHeight + 'px';
        contenedorVelo.style.width = document.documentElement.scrollWidth + 'px';
        contenedorVelo.className += ' ' + cual;
    },
	
    ocultar : function() {
        if (navigator.appVersion.indexOf('MSIE 6.0') != -1) {
            for (k=0;k<document.getElementsByTagName('select').length;k++) {
                document.getElementsByTagName('select')[k].style.visibility = 'visible';
            }
        }
        contenedorVelo.className = 'velo';
    }
	
}




var FUNCIONES = {
	
    fromAbsurdoToNormal : function(cual) {
        cual.toString();
        if (cual.indexOf(',') != -1) {
            if (cual.indexOf('.') != -1)
                cual = cual.split('.')[0] + cual.split('.')[1];
            cual = cual.split(',')[0] + '.' + cual.split(',')[1];
        } else {
            if (cual.split('.').length == 1)
                cual = cual + '.00';
        }
        return cual;
    },
	
    fromNormalToAbsurdo : function(cual) {
        cual.toString();
        if (cual.indexOf('.') != -1)
            cual = cual.split('.')[0] + ',' + cual.split('.')[1];
        if (cual.split(',')[0].length > 6) {
            cual = cual.split(',')[0].slice(0,-6) + '.' + cual.split(',')[0].slice(cual.split(',')[0].length-6,cual.split(',')[0].length-3) + '.' + cual.split(',')[0].slice(cual.split(',')[0].length-3,cual.split(',')[0].length) + ',' + cual.split(',')[1].slice(0,2);
        } else if (cual.split(',')[0].length > 3)
            cual = cual.split(',')[0].slice(0,-3) + '.' + cual.split(',')[0].slice(cual.split(',')[0].length-3,cual.split(',')[0].length) + ',' + cual.split(',')[1];
        return cual;
    },
	
    validaNumero : function(valor) {
        if (valor == '')
            return false;
        else if (isNaN(valor))
            return false;
        else
            return true;
    },
	
    validaTelefono : function(valor) {
        if (valor == '')
            return false;
        else if (isNaN(valor))
            return false;
        else if (valor.length < 9)
            return false;
        else
            return true;
    },
	
    validaEMail : function(valor) {
        if (valor == '')
            return false;
        else if (valor.indexOf('@') == -1)
            return false;
        else if (valor.indexOf('.') == -1)
            return false;
        else
            return true;
    },
	
    validaNIF : function(valor) {
        if (valor == '')
            return false;
        else {
            var cadena = 'TRWAGMYFPDXBNJZSQVHLCKET';
            var longitudNIF = valor.length;
            if (valor > 9)
                return false;
            var numeroExtraido = valor.substring(0,longitudNIF-1);
            var letraExtraida = valor.substring(longitudNIF-1,longitudNIF);
            var posicion = numeroExtraido%23;
            var letraCalculada = cadena.charAt(posicion);
            if (letraExtraida.toUpperCase() != letraCalculada)
                return false;
            else
                return true;
        }
    }
	
}




// añadido de eventos realizado como en http://simon.incutio.com/archive/2004/05/26/addLoadEvent

function addLoadEvent(fn) {
    var old = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = fn;
    } else {
        window.onload = function() {
            old();
            fn();
        }
    }
}

function validarLogin (){
    var usuario = document.miFormulario.email.value;
    var password = document.miFormulario.password.value;
    var alerta = 1;

    var nodo=document.getElementById("capaError");

    if (password=="") {
        alerta = 0;
        nodo.innerHTML="<div id=\"message_red\"><div class=\"top\"></div><div class=\"mid\"><div class=\"message_header\">"+error+"</div><div class=\"message_body\" id=\"ctl00_body_validationSummary\">"+errorLogin1+"<br /></div></div><div class=\"bot\"></div></div>";
        return;
    }

    if(usuario==""){
        alerta = 0;
        nodo.innerHTML="<div id=\"message_red\"><div class=\"top\"></div><div class=\"mid\"><div class=\"message_header\">"+error+"</div><div class=\"message_body\" id=\"ctl00_body_validationSummary\">"+errorLogin2+"<br /></div></div><div class=\"bot\"></div></div>";
        return;
    }else if(!isEmail(usuario)){
        alerta = 0;
        nodo.innerHTML="<div id=\"message_red\"><div class=\"top\"></div><div class=\"mid\"><div class=\"message_header\">"+error+"</div><div class=\"message_body\" id=\"ctl00_body_validationSummary\">"+errorLogin3+"<br /></div></div><div class=\"bot\"></div></div>";
        return;
    }

   

    if(alerta!=0){
        document.miFormulario.submit();
    }
}

function isEmail(StrObj) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(StrObj)) {
        return true;
    } else {
        return false;
    }
}

addLoadEvent(function() {
    FORMULARIO.init();
});

addLoadEvent(function() {
    CESTA.init();
});

addLoadEvent(function() {
    VELO.init();
});
