

/**
* @section include plugins
*/
/** @LightBox **/
(function($)
{   $.LightboxClass=function(){this.construct()};
    
    $.fn.lightbox=function(options)
    {   $.Lightbox=$.Lightbox||new $.LightboxClass();
        options=$.extend({start:false},options);
        var obj=$(this);
        $(obj).click(function()
        { var url=$(obj).attr('href');
            if(!$.Lightbox.start(url)){return false}
            return false
        });
        if(options.start)
        {    var obj=$(this);
            var url=$(obj).attr('href');
            if(!$.Lightbox.start(url)){return this}
        }return this
    };
    
    $.extend($.LightboxClass.prototype,
    {   params:new Object(),
        className:'lbanchor',
        width:550,
        height:460,
        opacity:0.8,
        src:'/imq/files/blank.html',
        construct:function(){return true},
        domReady:function()
        {    $('body').append('<div id="lb_overlay"></div><div id="lb_wrap"><div id="lb_window"><div id="lb_close"><a href="#">%txt_close%</a></div><iframe frameborder="0" scrolling="no" hspace="0" src="'+this.src+'" id="lb_iframe" name="lb_iframe"></iframe></div></div>');
            this.resize();
            this.reposition();
            $('#lb_overlay, #lb_wrap').hide();
            $(window).resize(function(){$.Lightbox.resize()});
            $.Lightbox.bind();return true
        },
        bind:function()
        {    $.each($('[@class*='+this.className+']'),
            function(index,obj){$(obj).lightbox()});
            return true
        },
        start:function(url)
        {    this.parseQuery(url);
            $('#lb_iframe').attr({'src':url});
            $('embed, object, select').css({'visibility':'hidden'});
            $.Lightbox.resize();
                        if(navigator.userAgent.toLowerCase().indexOf('msie 6')!=-1)
                        {    $('#lb_overlay').css({'backgroung':'#CCCCCC','display':'block','position':'absolute'});
                        }
                        else
                        {    $('#lb_overlay').css({'position':'fixed'});
                            $('#lb_overlay').css({opacity:this.opacity}).fadeIn('fast');
                        }
            $('#lb_wrap').show();
            var PageLang = $('html').attr('lang');
                        if(PageLang=="es")
                            $('#lb_close a').text('ABRIR');
                        else
                            $('#lb_close a').text('CERRAR');
            $('#lb_overlay, #lb_close a, #lb_wrap').click(function(event){$.Lightbox.finish();return false});
            this.resize();
            this.reposition();return true
        },
        finish:function()
        {   $('#lb_wrap').hide();
            $('embed, object, select').css({'visibility':'visible'});
            $('#lb_iframe').attr({'src':this.src});
            if(navigator.userAgent.toLowerCase().indexOf('msie 6')!=-1)
                        {    $('#lb_overlay').hide();
                        }
                        else
                        {    $('#lb_overlay').fadeOut('fast',function(){$('#lb_overlay').hide()});
                        }
                        if(navigator.userAgent.toLowerCase().indexOf('msie 6')==-1){$("html").css("overflow","auto")}
            this.params['height']=undefined;
            this.params['width']=undefined
        },
        resize:function()
        {    var pageSize=this.getPageSize();
            $('#lb_overlay').css({width:pageSize.largestWidth,height:pageSize.largestHeight});
            $('#lb_window').css("width",this.getWindowWidth()+'px');
            $('#lb_iframe').css("width",this.getWindowWidth()+'px');
            $('#lb_iframe').css("height",this.getWindowHeight()+'px')
        },
        reposition:function(options)
        {    options=$.extend({},options);
            var pageSize=this.getPageSize();
            var chHeight=parseInt($('#lb_wrap').height());
            var cbTop=$(window).scrollTop()+(pageSize.windowHeight-chHeight)/4;
            var cbLeft=$(window).scrollLeft();
            if(cbTop<0){cbTop=0}$('#lb_wrap').css({left:cbLeft,top:cbTop})
        },
        parseQuery:function(url)
        {    query=url.replace(/^[^\?]+\??/,'');
            if(!query)return this.params;
            pairs=query.split(/[;&]/);
            for(var i=0;i<pairs.length;i++)
            {    kv=pairs[i].split('=');
                if(!kv||kv.length!=2)continue;
                key=unescape(kv[0]);
                val=unescape(kv[1]);
                val=val.replace(/\+/g,' ');
                this.params[key]=val
            }
        },
        getWindowWidth:function(){return this.params['width']||this.width},
        getWindowHeight:function(){return this.params['height']||this.height},
        getPageSize:function()
        {    var xScroll,yScroll;
            xScroll=$(document).width();
            yScroll=$(document).height();
            var windowWidth,windowHeight;
            if(self.innerHeight)
            {    if(document.documentElement.clientWidth)
                {    windowWidth=document.documentElement.clientWidth
                }
                else
                {    windowWidth=self.innerWidth
                }
                windowHeight=self.innerHeight
            }
            else if(document.documentElement&&document.documentElement.clientHeight)
            {    windowWidth=document.documentElement.clientWidth;
                windowHeight=document.documentElement.clientHeight
            }
            else if(document.body)
            {    windowWidth=document.body.clientWidth;
                windowHeight=document.body.clientHeight
            }
            if(yScroll<windowHeight)
            {    pageHeight=windowHeight
            }
            else
            {    pageHeight=yScroll
            }
            if(xScroll<windowWidth)
            {    pageWidth=xScroll
            }
            else
            {    pageWidth=windowWidth
            }
            var largestWidth,largestHeight,smallestWidth,smallestHeight;
            if(pageWidth>=windowWidth)
            {        largestWidth=pageWidth;
                smallestWidth=windowWidth
            }
            else
            {    largestWidth=windowWidth;
                smallestWidth=pageWidth
            }
            if(pageHeight>=windowHeight)
            {    largestHeight=pageHeight;
                smallestHeight=windowHeight
            }
            else
            {    largestHeight=windowHeight;
                smallestHeight=pageHeight
            }
            return{'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'largestWidth':largestWidth,'largestHeight':largestHeight}
        }
    });
    $(function()
    {    $.Lightbox=$.Lightbox||new $.LightboxClass();
        $.Lightbox.domReady()
    })
})(jQuery);
/** @END - LightBox **/
/** @PopUp **/
$.fn.popup = function(options)
{    $(this).click(function()
    {   var thisURL = $(this).attr('href');
        window.open(thisURL, '','width='+options['width']+',height='+options['height']+',scrollbars='+options['scrollbars']+',resizable='+options['resizable']+',menubar='+options['menubar']+',location='+options['location']+'');
        return false;
    })
}
$('a.pu_500').popup({width:'500', height:'600',scrollbars:'yes',resizable:'no',menubar:'no',location:'no'});
$('a.pu_550').popup({width:'600', height:'600',scrollbars:'yes',resizable:'no',menubar:'no',location:'no'});
$('a.pu_600').popup({width:'600', height:'600',scrollbars:'yes',resizable:'no',menubar:'no',location:'no'});
/** @END - PopUp **/
/** @Form validation **/
//Filtros
var filters =
    {   requerido:function(el){return ($(el).val()!='' && $(el).val()!=-1);},
        cuescheck:function(el){return },
        escheck:function(el){return ($('#'+$(el).attr('id')).is(':checked'));},
        alfanum:function(el){return /^[\w\sàáèéíòóúñç.,;:!¡@"%=?¿']+$/.test($(el).val());},
        alfanumno:function(el){return /^[\w\sàáèéíòóúñç.,;:!¡@"%=?¿']*$/.test($(el).val());},
        email:function(el){return /(^[0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,3})$/.test($(el).val());},
        fecha:function(el){return /^[0][1-9]|[1][0-2]\/[1-2][09][0-9][0-9]$/.test($(el).val());},
        entero:function(el){return /^(?:\+|-)?\d+$/.test($(el).val());},
        enterono:function(el){return /^(?:\+|-)?\d*$/.test($(el).val());},
        real:function(el){return /^(?:\+|-)?\d+\.\d+$/.test($(el).val());},
        realno:function(el){return /^(?:\+|-)?\d+\.\d*$/.test($(el).val());},
        minlengthnum:function(el){return }
    };
// Error txt
var errorrequerido=' El campo es requerido';
var errorescheck=' La clausula debe ser aceptada';
var erroralfanum=' El campo contiene caracteres no v&aacute;lidos';
var erroremail=' Introduzca un correo electr&oacute;nico v&aacute;lido';
var errorfecha=' : el formato intorducido no es correcto';
var errorentero=' : requiere un valor n&uacute;merico sin espacios';
var errorreal=' El campo requiere un valor númerico sin espacios';
var errorminlengthnum=' : D&iacute;gitos requeridos = ';
// Extend
$.extend({stop:function(e){if(e.preventDefault) e.preventDefault();if(e.stopPropagation) e.stopPropagation();}});
/** @END - Form validation **/
$(document).ready(function()
{    $('html').addClass('js');
    $('#navegacion ul.level02 > li:last').addClass('noborder');
    $('#navegacion ul.level03 > li:last').addClass('noborder');
    // Ancho en caja + imagen
    $.each($('div[@class^=txt_img]'),function()
  {    var widthIMG=($(this).children($('div.imgLeft img'))).width();
    if(($(this).parent().parent().attr("class"))=='two_colmnus')
    {    $(this).find(".imgwidth").css({'width':widthIMG+'px'});
      $(this).find(".txtwidth").css({'width':(242-widthIMG)+'px'});
    }
    else
    {    $(this).find(".imgwidth").css({'width':widthIMG+'px'});
      $(this).find(".txtwidth").css({'width':(518-widthIMG)+'px'});
    }
    });
    // Validacion de campos
    $('form.estandarform').bind('submit', function(e)
    {   if(typeof filters=='undefined') return;
        var errortxt='';
        var times=0;
        $(this).find('input,textarea,select,table').each(function(x,el)
        {    if($(el).attr('className')!='undefined')
            {    $.each(new String($(el).attr('className')).split(' '),function(x,klass)
                {    if($.isFunction(filters[klass]))
                    {    if(!filters[klass](el))
                        {    $(el).addClass('error');
                            // Mostar el error dependiendo de la clase
                            if(klass=='requerido')
                            {    errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorrequerido+'<br />';
                            }
                            if(klass=='minlengthnum')
                            {    if($('#'+$(el).attr('id')).attr('maxlength')!=$('#'+$(el).attr('id')).val().length)
                                {    errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorminlengthnum+$('#'+$(el).attr('id')).attr('maxlength')+'<br />';
                                }
                                else
                                {    if(!filters['entero'](el))
                                    {    errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorentero+'<br />';
                                    }
                                    else
                                    {    $(el).removeClass('error');
                                    }
                                }
                            }
                            switch(klass)
                            {   case 'escheck':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorescheck+'<br />';
                                break;
                                case 'cuescheck':
                                var numtable=$('table.cuescheck').size();
                                $.each($('table.cuescheck'),function()
                                {   if(times<numtable)
                                    {   var numcues=$(this).attr('id');
                                        var howmany=$('#'+numcues+' input.seecheck').size();
                                        times++;
                                        var valido=0
                                        $.each($('#'+numcues+' input.seecheck'),function()
                                        {   if($('#'+$(this).attr('id')).is(':checked') || $('#'+$(this).attr('id')+'0').is(':checked'))
                                            { valido++; }
                                        });
                                        if(valido==howmany) { $(this).removeClass('cuescheck'); $(el).removeClass('error'); $('#anchor'+times).removeClass('error'); }
                                        else { $('#anchor'+times).addClass('error'); errortxt+='ERROR: Cuestionarios de salud son requeridos<br />'; }
                                    }
                                });
                                break;
                                case 'alfanum':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+erroralfanum+'<br />';
                                break;
                                case 'alfanumno':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+erroralfanum+'<br />';
                                break;
                                case 'email':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+erroremail+'<br />';
                                break;
                                case 'fecha':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorfecha+'<br />';
                                break;
                                case 'entero':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorentero+'<br />';
                                break;
                                case 'enterono':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorentero+'<br />';
                                break;
                                case 'real':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorreal+'<br />';
                                break;
                                case 'realno':
                                errortxt+='<strong>'+$('#'+$(el).attr('id')).attr('title')+'</strong>'+errorreal+'<br />';
                                break;
                            }
                        }
                        else
                        {    $(el).removeClass('error');
                        }
                    }
                });
            }
            // Quitar los errores al borrar
            $('#borrar').click(function()
            {    $(el).removeClass('error');
                $('#'+$(el).attr('id')+'er').removeClass('errortxt');
                $('#'+$(el).attr('id')+'er').html('');
            });
            
        });
        if(errortxt!='')
                {    $('#error').removeClass('fine');
                    $('#error').addClass('error');
                    $("#error span").html(errortxt);
                }
                else
                {    $('#error').addClass('fine');
                    $('#error').removeClass('error');
                    $("#error span").html(''); 
                }
        if(errortxt!='')
        {    $.stop(e || window.event);
            return false;
        }
      return true;
    });
    
    $('#txtPrefijo,#txtNumPoliza').change(function()
    { if($(this).attr('size')!=$(this).val().length)
        { var rest=$(this).attr('size')-$(this).val().length;
            var newval="";
            for(i=0;i<rest;i++){newval+=0;}
            var newval=newval+$(this).val();
            $(this).val(newval);
        }
    });
});