// JavaScript Document

////////////  Sitemap Events /////////////////

function sitemap(){

    $("sitemap").addEvent('click', function(){
        sitemap_show();
    });
    
    $("sitemap_overlay").addEvent('click', function(){
        sitemap_hide();
    });
    
    $("sitemap_close").addEvent('click', function(){
        sitemap_hide();
    });
    
}

function sitemap_show(){

    var overlay = $('sitemap_overlay'), wrap = $('sitemap_wrap');
    var fx = overlay.effects({
        duration: 800,
        transition: Fx.Transitions.Quart.easeOut
    });
    var fx2 = wrap.effects({
        duration: 500,
        transition: Fx.Transitions.Quart.easeOut
    });
    
    $("sitemap_overlay").setStyle('opacity', 0);
    $("sitemap_wrap").setStyle('opacity', 0);
    $("sitemap_overlay").setStyle('bottom', 0);
    
    fx2.start({
        'opacity': 1
    });
    
    fx.start({
        'opacity': .7
    }).chain(function(){
        fx2.start({
            'width': 700,
            'margin-left': -350
        }).chain(function(){
            fx2.start({
                'height': 641,
                'margin-top': -320
            });
        });
    });
}

function sitemap_hide(){

    var overlay = $('sitemap_overlay'), wrap = $('sitemap_wrap');
    var fx = overlay.effects({
        duration: 800,
        transition: Fx.Transitions.Quart.easeOut
    });
    var fx2 = wrap.effects({
        duration: 500,
        transition: Fx.Transitions.Quart.easeOut
    });
    
    $("sitemap_overlay").setStyle('bottom', '');
    fx2.start({
        'height': 0,
        'margin-top': 0
    }).chain(function(){
        this.start({
            'width': 0,
            'margin-left': 0
        });
    }).chain(function(){
        fx.start({
            'opacity': 0
        });
        fx2.start({
            'opacity': 0
        });
    });
}

/////////////////////////////////////////////////// AJAX Form ///////////////////////////////////

function send_contacto(){
    $('myForm').addEvents({
        'submit': function(e){
        
            new Event(e).stop();
            
            $('log_res').setStyle('text-align', 'left');
            $('log_res').setStyle('color', '#000000');
            
            var fx1 = $('log_res').effects({
                duration: 1000,
                transition: Fx.Transitions.Cubic.easeOut
            });
            fx1.start({
                'opacity': 1
            });
            var log = $('log_res').empty().addClass('ajax-loading');
            this.send({
                update: log,
                onComplete: function(){
                
                    log.removeClass('ajax-loading');
                    
                    if (pagina == 'updatepage') {
                        $('b_borrar').disabled = false;
                    }
                    else 
                        if (pagina == 'solicitud') {
                            $('nombre').disabled = true;
                            $('especialidad').disabled = true;
                            $('mail').disabled = true;
                            $('telefono').disabled = true;
                            $('extension').disabled = true;
                            $('celular').disabled = true;
                            $('estudio').disabled = true;
                        }
                        else {
                            $('mensaje').disabled = true;
                            $('correo').disabled = true;
                            $('telefono2').disabled = true;
                            $('nombre2').disabled = true;
                            $('b_enviar').disabled = true;
                        }
                    
                }
            });
        },
        'reset': function(e){
            $('log_res').empty();
            $('log_res').setStyle('color', '#ff0000');
            $('log_res').setStyle('text-align', 'center');
            
            if (pagina == 'updatepage') {
                $('med_nombres').disabled = false;
                $('med_especialidad').disabled = false;
                $('med_ciudad').disabled = false;
                $('med_telefono').disabled = false;
                $('med_direccion').disabled = false;
                $('med_apellidos').disabled = false;
                $('b_enviar').disabled = false;
            }
            else 
                if (pagina == 'solicitud') {
                    $('nombre').disabled = false;
                    $('especialidad').disabled = true;
                    $('mail').disabled = true;
                    $('telefono').disabled = true;
                    $('extension').disabled = true;
                    $('celular').disabled = true;
                    $('estudio').disabled = true;
                }
                else {
                    $('mensaje').disabled = true;
                    $('correo').disabled = true;
                    $('telefono2').disabled = true;
                    $('b_enviar').disabled = true;
                    $('nombre2').disabled = false;
                }
        }
    });
}

//////////////// Some Rollovers ////////////////////


function rollover_detailImage(image, speed, opacity){

    var fx = new Fx.Styles(image, {
        duration: speed,
        wait: false,
        transition: Fx.Transitions.Quad.easeOut
    });
    
    image.addEvents({
        'mouseenter': function(e){
            fx.start({
                'opacity': opacity
            })
        },
        
        'mouseleave': function(e){
            fx.start({
                'opacity': 1
            })
        }
    });
}

function rollover_init(element){

    if (element == $('sitemap')) {
        element.addEvent('mouseenter', function(){
            this.setStyle('text-decoration', 'underline');
        });
        element.addEvent('mouseleave', function(){
            this.setStyle('text-decoration', 'none');
        });
    }
    else {
        element.addEvent('mouseenter', function(){
            this.setStyle('color', '#9E3081');
            this.setStyle('text-decoration', 'underline');
        });
        element.addEvent('mouseleave', function(){
            this.setStyle('color', '#767676');
            this.setStyle('text-decoration', 'none');
        });
    }
}

/////////////////// acordion menu //////////////////

function acordeon(seccion){
    var accordion = new Accordion('h3.atStart', 'div.atStart', {
        opacity: false,
        onActive: function(toggler, element){
            toggler.setStyles({
                'background-image': 'url(http://www.bago.com.ec/templates/bago/images/selected.gif)',
                'background-repeat': 'no-repeat',
                'color': '#9E3081',
                'padding-left': '37px',
                'margin-left': '-32px'
            });
        },
        
        onBackground: function(toggler, element){
            toggler.setStyles({
                'background-image': 'none',
                'color': '#767676',
                'margin': '0px',
                'padding': '0 5px'
            });
        }
    }, $('accordion'));
	
	accordion.display(seccion);	
	
}

///////////////////

function get_codbago(){
	var codigo = $('medicoz2Details_field_codbago').getText();
	$('cod_bago').setProperty('value', codigo);
}


///////////////////   form validation   ////////////////

function validar_contacto(){
    var ofensa = 0;
    $('log_res').setStyle('color', '#ff0000');
    $('log_res').setStyle('text-align', 'center');
    var txt = $('mensaje'), blog = $('log_res');
    var fx = new Fx.Styles(blog, {
        duration: 1500,
        wait: false,
        transition: Fx.Transitions.Quad.easeOut
    });
    
    $('mensaje').addEvents({
        'focus': function(){
            if (txt.value.contains('mensaje')) 
                txt.value = '';
            txt.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            txt.setStyle('border', '1px solid #cccccc');
            if (txt.value == "") {
                txt.value = "";
            }
        },
        'keyup': function(){
            var b_enviar = $('b_enviar');
            if (txt.value.length > 18) {
                b_enviar.disabled = false;
                //b_enviar.setStyle('color','#98CB00');
                var fxbot = b_enviar.effects({
                    duration: 500,
                    transition: Fx.Transitions.Cubic.easeOut
                });
                fxbot.start({
                    'color': '#98CB00'
                });
            }
            else {
                press = 0;
                var fxbot1 = b_enviar.effects({
                    duration: 500,
                    transition: Fx.Transitions.Cubic.easeOut
                });
                fxbot1.start({
                    'color': '#BCBCBC'
                });
                b_enviar.disabled = true;
            }
            var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
            var regexp2 = /(www)/
            var text = txt.value;
            text = text.toLowerCase();
            var x = regexp.test(text);
            var y = regexp2.test(text);
            
            if (x == true || y == true) {
                txt.fireEvent('burn', 'no ingrese direcciones web!');
                txt.value = txt.value.replace("www", "");
                txt.value = txt.value.replace("http://", "");
            }
            
            if (txt.value.contains("<")) {
                txt.value = txt.value.replace("<", "");
                txt.fireEvent('burn', 'removiendo caracteres potencialmente peligrosos');
            }
            
            if (txt.value.contains(">")) {
                txt.value = txt.value.replace(">", "");
                txt.fireEvent('burn', 'removiendo caracteres potencialmente peligrosos');
            }
            words = new Array("viagra", "fuck", "asshole", "motherfucker", "idiot", "shithead", "cialis", "verga", "chucha", "puta", "huevada", "guevada", "penis", "pennis", "suck", "dick")
            
            for (var i = 0; i < words.length; i++) {
                if (txt.value.contains(words[i])) {
                    txt.fireEvent('burn', '***LENGUAJE OFENSIVO***');
                    txt.value = txt.value.replace(words[i], '');
                    //alert(ofensa);
                    if (ofensa == 1) {
                        alert("Por favor no use lenguaje ofensivo!");
                    }
                    else 
                        if (ofensa >= 2) {
                            alert("No use LENGUAJE OFENSIVO!!")
                        }
                    ofensa += 1;
                }
            }
            
            if (txt.value.contains("sucks")) {
                txt.value = txt.value.replace("sucks", '');
                txt.fireEvent('burn', '***LENGUAJE OFENSIVO***');
                alert("YOU suck!");
                ofensa += 1;
            }
            
        },
        'burn': function(text){
            blog.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        }
    });
    
    // email check
    var txt2 = $('correo'), blog = $('log_res');
    var fx = new Fx.Styles(blog, {
        duration: 2500,
        wait: false,
        transition: Fx.Transitions.Quad.easeInOut
    });
    
    var correo = $('correo');
    correo.addEvents({
        'focus': function(){
            txt2.setStyle('border', '1px solid #d300a9');
        },
        
        'blur': function(){
            txt2.setStyle('border', '1px solid #cccccc');
            if (txt2.value == "") {
                txt2.value = "";
            }
        },
        'click': function(){
            txt2.setStyle('border', '1px solid #98CB00');
            if (correo.value == "correo") {
                correo.value = "";
            }
        },
        'keyup': function(){
            var regex = "^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$";
            var regexp = new RegExp(regex, 'i');
            var text2 = txt2.value;
            text2 = text2.toLowerCase();
            var x = text2.test(regexp);
            if (text2.indexOf(".") == -1) 
                x = false;
            if (x != true) {
                txt2.fireEvent('burn2', 'esperando email valido');
                txt2.value = txt2.value.replace("www", "");
                txt2.value = txt2.value.replace("http://", "");
                //$('telefono2').disabled=true;
                $('mensaje').disabled = true;
                $('b_enviar').disabled = true;
            }
            else {
                txt2.fireEvent('burnemail', 'email valido reconocido');
                $('mensaje').disabled = false;
            }
            
            /*if (txt2.value.contains('�')){
             txt2.value = txt.value.replace("�","oe");
             txt2.fireEvent('burn', 'Traduciendo caracteres especiales Alemanes.');
             }*/
        },
        'burnemail': function(text){
            blog.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        },
        'burn2': function(text){
            blog.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        }
    });
    
    // check name
    var txt3 = $('nombre2'), log = $('log_res');
    var fx = new Fx.Styles(log, {
        duration: 2500,
        wait: false,
        transition: Fx.Transitions.Quad.easeInOut
    });
    //var myEffect = $('log').effect('color', {duration: 500,transition: Fx.Transitions.Quad.easeIn});
    var nombre = $('nombre2');
    nombre.addEvents({
        'focus': function(){
            txt3.setStyle('border', '1px solid #d300a9');
            if (nombre.value == "nombre") {
                nombre.value = "";
            }
        },
        'blur': function(){
            txt3.setStyle('border', '1px solid #cccccc');
            if (txt3.value == "") {
                txt3.value = "";
            }
        },
        'keyup': function(){
            //txt3.setStyle('background','none #6F6F6F');
            var regex = "[^a-zA-Z\\s\\']";
            var regexp = new RegExp(regex);
            var name = txt3.value;
            txt3.value = txt3.value.replace(regexp, '');
            if (name != txt3.value) {
                txt3.fireEvent('burn3', 'removiendo caracteres invalidos! ');
            }
            if (nombre.value.length >= 3) {
                $('telefono2').disabled = false;
            }
        },
        'burn3': function(text){
            blog.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        }
    });
    // check phone number
    var txt4 = $('telefono2'), log = $('log_res');
    var fx = new Fx.Styles(log, {
        duration: 2500,
        wait: false,
        transition: Fx.Transitions.Quad.easeInOut
    });
    var telefono = $('telefono2');
    telefono.addEvents({
        'focus': function(){
            txt4.setStyle('border', '1px solid #d300a9');
            if (telefono.value == "telefono") {
                telefono.value = "";
            }
        },
        'blur': function(){
            txt4.setStyle('border', '1px solid #cccccc');
            if (txt4.value == "") {
                txt4.value = "";
            }
        },
        
        'keyup': function(){
            //txt3.setStyle('background','none #6F6F6F');
            var regex = "[^0-9\-_]";
            var regexp = new RegExp(regex);
            var name = txt4.value;
            txt4.value = txt4.value.replace(regexp, '');
            if (name != txt4.value) {
                txt4.fireEvent('burn4', 'removiendo caracteres invalidos! ');
            }
            if (telefono.value.length >= 7) {
                $('correo').disabled = false;
            }
        },
        'burn4': function(text){
            blog.setHTML(text);
            fx.start({
                'color': ['#ff0000', '#fff'],
                'opacity': [1, 0]
            })
        }
        
    });
    
}

function validar_solicitud(){
    var ofensa = 0;
    $('log_res').setStyle('color', '#ff0000');
    $('log_res').setStyle('text-align', 'center');
    
    
    var log = $('log_res');
    var fx = new Fx.Styles(log, {
        duration: 2500,
        wait: false,
        transition: Fx.Transitions.Quad.easeInOut
    });
    
    
    //****** check name
    
    $('nombre').addEvents({
        'keyup': function(){
            //txt3.setStyle('background','none #6F6F6F');
            var regex = "[^a-zA-Z\\s\\']";
            var regexp = new RegExp(regex);
            var name = this.value;
            this.value = this.value.replace(regexp, '');
            if (name != this.value) {
                this.fireEvent('burn', 'removiendo caracteres invalidos! ');
            }
            if (this.value.length >= 3) {
                $('especialidad').disabled = false;
            }
        },
        'burn': function(text){
            log.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        }
    });
    
    //******** especialidad
    
    $('especialidad').addEvents({
        'keyup': function(){
            var regex = "[^a-zA-Z\\s\\']";
            var regexp = new RegExp(regex);
            var name = this.value;
            this.value = this.value.replace(regexp, '');
            if (name != this.value) {
                this.fireEvent('burn', 'removiendo caracteres invalidos! ');
            }
            if (this.value.length >= 3) {
                $('mail').disabled = false;
            }
        },
        'burn': function(text){
            log.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        }
    });
    
    //******* email check    
    
    $('mail').addEvents({
        'keyup': function(){
            var regex = "^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$";
            var regexp = new RegExp(regex, 'i');
            var text2 = this.value;
            text2 = text2.toLowerCase();
            var x = text2.test(regexp);
            if (text2.indexOf(".") == -1) 
                x = false;
            if (x != true) {
                this.fireEvent('burn', 'esperando email valido');
                this.value = this.value.replace("www", "");
                this.value = this.value.replace("http://", "");
                //$('telefono2').disabled=true;
                $('estudio').disabled = true;
                $('b_enviar').disabled = true;
            }
            else {
                this.fireEvent('burnemail', 'email valido reconocido');
                $('telefono').disabled = false;
                $('extension').disabled = false;
                $('celular').disabled = false;
            }
        },
        'burnemail': function(text){
            log.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        },
        'burn': function(text){
            log.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        }
    });
    
    //******** check phone number   
    
    $('telefono').addEvents({
        'keyup': function(){
            var regex = "[^0-9\-_]";
            var regexp = new RegExp(regex);
            var name = this.value;
            this.value = this.value.replace(regexp, '');
            if (name != this.value) {
                this.fireEvent('burn', 'removiendo caracteres invalidos! ');
            }
            if (this.value.length >= 7) {
                $('estudio').disabled = false;
            }
        },
        'burn': function(text){
            log.setHTML(text);
            fx.start({
                'color': ['#ff0000', '#fff'],
                'opacity': [1, 0]
            })
        }
        
    });
    
    //******** check mobile number   
    
    $('celular').addEvents({
        'keyup': function(){
            var regex = "[^0-9\-_]";
            var regexp = new RegExp(regex);
            var name = this.value;
            this.value = this.value.replace(regexp, '');
            if (name != this.value) {
                this.fireEvent('burn', 'removiendo caracteres invalidos! ');
            }
            if (this.value.length >= 7) {
                $('estudio').disabled = false;
            }
        },
        'burn': function(text){
            log.setHTML(text);
            fx.start({
                'color': ['#ff0000', '#fff'],
                'opacity': [1, 0]
            })
        }
        
    });
    
    //****** Estudio solicitado
    
    $('estudio').addEvents({
        'keyup': function(){
            var b_enviar = $('b_enviar');
            if (this.value.length > 18) {
                b_enviar.disabled = false;
                //b_enviar.setStyle('color','#98CB00');
                var fxbot = b_enviar.effects({
                    duration: 500,
                    transition: Fx.Transitions.Cubic.easeOut
                });
                fxbot.start({
                    'color': '#98CB00'
                });
            }
            else {
                press = 0;
                var fxbot1 = b_enviar.effects({
                    duration: 500,
                    transition: Fx.Transitions.Cubic.easeOut
                });
                fxbot1.start({
                    'color': '#BCBCBC'
                });
                b_enviar.disabled = true;
            }
            var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
            var regexp2 = /(www)/
            var text = this.value;
            text = text.toLowerCase();
            var x = regexp.test(text);
            var y = regexp2.test(text);
            
            if (x == true || y == true) {
                this.fireEvent('burn', 'no ingrese direcciones web!');
                this.value = this.value.replace("www", "");
                this.value = this.value.replace("http://", "");
            }
            
            if (this.value.contains("<")) {
                this.value = this.value.replace("<", "");
                this.fireEvent('burn', 'removiendo caracteres potencialmente peligrosos');
            }
            
            if (this.value.contains(">")) {
                this.value = this.value.replace(">", "");
                this.fireEvent('burn', 'removiendo caracteres potencialmente peligrosos');
            }
            words = new Array("viagra", "fuck", "asshole", "motherfucker", "idiot", "shithead", "cialis", "verga", "chucha", "puta", "huevada", "guevada", "penis", "pennis", "suck", "dick")
            
            for (var i = 0; i < words.length; i++) {
                if (this.value.contains(words[i])) {
                    this.fireEvent('burn', '***LENGUAJE OFENSIVO***');
                    this.value = txt.value.replace(words[i], '');
                    //alert(ofensa);
                    if (ofensa == 1) {
                        alert("Por favor no use lenguaje ofensivo!");
                    }
                    else 
                        if (ofensa >= 2) {
                            alert("No use LENGUAJE OFENSIVO!!")
                        }
                    ofensa += 1;
                }
            }
            
            if (this.value.contains("sucks")) {
                this.value = this.value.replace("sucks", '');
                this.fireEvent('burn', '***LENGUAJE OFENSIVO***');
                alert("YOU suck!");
                ofensa += 1;
            }
            
        },
        'burn': function(text){
            log.setHTML(text);
            fx.start({
                'color': '#ff0000',
                'opacity': [1, 0]
            })
        }
    });
    
}

//////////////// input form click efects ///////////////

function rrhh_blur(){
    $('nombres').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('correo').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('mensaje').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
}

function solicitud_blur(){
    $('nombre').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('especialidad').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('mail').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('telefono').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('extension').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('celular').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    $('estudio').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
}

function blur_update(){

    $('med_nombres').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    
    $('med_especialidad').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    
    $('med_ciudad').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    
    $('med_telefono').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    
    $('med_direccion').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    
    $('med_apellidos').addEvents({
        'focus': function(){
            this.setStyle('border', '1px solid #d300a9');
        },
        'blur': function(){
            this.setStyle('border', '1px solid #cccccc');
        }
    });
    
}

////////////////  Toggle the update form in Medicos  /////////////////////


function togle_update(){
    var mySlide = new Fx.Slide('actualizacion');
    mySlide.toggle();
    
    $('toggle').addEvent('click', function(e){
        e = new Event(e);
        mySlide.toggle();
        e.stop();
    });
}

////////////////////////////////////////////////
