/**
* Clase Error * Clase que define los errores que se muestran por pantalla
* @package common
* @author Victor Guardiola <victor.guardiola@atrapalo.com>
* @version $Id: errores.js,v 1.44.2.1 2009-05-07 17:21:37 wwwagent Exp $;
*/
/**
* Objeto error * Vars es un array cuyos valores se substituyen por %s en el mensaje del error.
*/
function Error(id_package,id_class,id_exception,msg,action,vars)
{
    this.id_package=(!isNaN(id_package))?id_package:0;
    this.id_class=(!isNaN(id_class))?id_class:0;
    this.id_exception=(!isNaN(id_exception))?id_exception:0;
    this.action=(action!=undefined)?action:'';
    this.msg=(msg!=undefined && msg!='')?msg+"\n":'';
    this.vars = (vars!=undefined && vars!='')? vars : '';
    if(this.msg=='' && (isNaN(id_package) || !isNaN(id_class) || isNaN(id_exception)))
    {
        if(isNaN(id_package)) this.msg=id_package;
        else if(isNaN(id_class)) this.msg=id_class;
        else if(isNaN(id_exception)) this.msg=id_exception;
        if(this.msg == undefined) this.msg='';
    }
    this.getCodigoError=function()
    {
        for(i=this.id_class.length;i<3;i++) this.id_class='0'+this.id_class;
        for(i=this.id_exception.length;i<3;i++) this.id_exception='0'+this.id_exception;
        return ''+this.id_package+''+this.id_class+''+this.id_exception+'';
    }

    this.setCodigoError=function(val)
    {
        if(val.length<8) return -1;
        this.id_package=val.substr(0,2);
        this.id_class=val.substr(2,3);
        this.id_exception=val.substr(5,3);
    };

    this.setAction=function(val)
    {
        this.action=val;
    };

    this.setMsg=function(val)
    {
        this.msg=val;
    };
    this.getMsg=function()
    {
        return this.msg;
    };
    // Por ahora el vars solo acepta 1 valor.
    this.getVars=function()
    {
        tmp = '';
        if (this.vars.length > 0) tmp = this.vars;
        return tmp;
    }

}

/**
* Classe que contiene y muestra los errores
*/
function Errores()
{
    this.arrErrores=new Array();
    this.messages=new Array();
    this.text='';
    this.push=function(val)
    {
        this.arrErrores.push(val);
    };

    this.toString=function()
    {
        var str=new Array();
        for (y=0;y<this.arrErrores.length;y++)
        {
            if(this.arrErrores[y].msg=='' || this.arrErrores[y].msg.indexOf('undefined')>=0)
            {
                if (this.arrErrores[y].getVars() != '')
                    str.push(this.arrErrores[y].getCodigoError()+'_'+this.arrErrores[y].getVars());
                else
                    str.push(this.arrErrores[y].getCodigoError());
            }else
                        this.messages.push(this.arrErrores[y].msg);
        }
        return str.join('|');
    };

    this.cargarErroresCookie=function()
    {
        try
        {
            vars=new Array();
            var rCookie = getCookie('id_errores');
            if (rCookie)  arr_errores = getCookie('id_errores').split('|');
            else arr_errores = [];
            for (i=0;i<arr_errores.length;i++)
            {
                e=new Error();
                vars[i]=new Array();
                if(arr_errores[i].indexOf("_")>0)
                {
                    tmp=arr_errores[i].split("_");
                    arr_errores[i]=tmp[0];
                    for(y=1;y<tmp.length;y++)
                    {
                        vars[i].push(tmp[y]);
                    }
                }
                if ((arr_errores[i] != undefined) && (arr_errores[i] != ''))
                {
                    if(isNaN(arr_errores[i]))
                    {
                        e.setMsg(unescape(arr_errores[i]));
                    }else{
                        e.setCodigoError(unescape(arr_errores[i]));
                    }
                    if(vars[i].length>0) e.vars=vars[i].join("_");
                    this.push(e);
                }
            }

            var domain=location.hostname.split(".");
            var tld=domain.pop();
            var dname=domain.pop()
            domain=dname+"."+tld;
            deleteCookie('id_errores','/');
                        deleteCookie('id_errores','/',domain);
                        deleteCookie('id_errores','/',domain,true);

            //borramos cookie en funcion del dominio
            if(this.cargarErroresCookie.arguments.length > 0) {
                domain_to_delete = this.cargarErroresCookie.arguments[0];
                deleteCookie('id_errores','/','.'+domain_to_delete);
            }

        }catch(e)
        {
        }
    };

    this.eval=function(val)
    {
        if(this.arrErrores.length==0) return -1;
        codigos=this.toString();
        if(codigos!='')
        {
            try
            {
                //YAHOO.util.Connect.asyncRequest('GET', '/common/index.php?pg=error&mode=simple&squid=yes&id_errores='+codigos,
                YAHOO.util.Connect.asyncRequest('GET', '/common/error/' + codigos + '|',
                {
                    success: function(response)
                             {
                                if(response.responseText!='') errores.messages.push(response.responseText);
                                else errores.messages.push('Error nums: '+codigos);
                                eval(val(errores.messages.join("\n").replace(/<br\/>/g,'\n')));
                             },
                    failure: function (response)
                             {
                                alert('Error Interno 697. Sentimos las molestias, por favor intentelo mas tarde.' + '\n Error 697. Sorry, please try again. Thanks' + '\n Errore interno 697: problemi di comunicazione con il server, per favore riprova più tardi');
                             }
                });

            }catch(err){
//                new Ajax.Request('/common/index.php',{method:'post',parameters:'pg=error&mode=simple&id_errores='+codigos,onSuccess:function(response){
                new Ajax.Request('/common/error/' + codigos + '|',{method:'post',onSuccess:function(response){
                    if(response.responseText!='') errores.messages.push(response.responseText);
                    else errores.messages.push('Error nums: '+codigos);
                    eval(val(errores.messages.join("\n").replace(/<br\/>/g,'\n')));
                }});
            }
        }else{
            eval(val(this.messages));
        }
        this.clean();
    };
    this.layer=function()
    {
        if($('AlertOverlay'))
        {
            $('AlertOverlay').style.height='auto';
            $('lista_de_errores').innerHTML=this.messages.join("<br />").replace(/\n/,'<br'+'/'+'>');
            $('AlertOverlay').style.display='block';
            this.processActions();
            this.clean();
            document.location.href=document.location.href.replace(/#(.)*/,"")+"#";
        }else{
            this.alert();
        }
    };
    this.alert=function()
    {
        tmp = this.messages;
        tmp = tmp.join("\n");
        tmp = tmp.replace(/&lt;/ig,'<');
        tmp = tmp.replace(/&gt;/ig,'>');
        tmp = tmp.replace(/<br\/>/g,'\n');
        tmp = tmp.replace(/<br \/>/g,'\n');
        tmp = tmp.replace(/&aacute;/ig,'á');
        tmp = tmp.replace(/&eacute;/ig,'é');
        tmp = tmp.replace(/&oacute;/ig,'ó');
        tmp = tmp.replace(/&iacute;/ig,'í');
        tmp = tmp.replace(/&uacute;/ig,'ú');
        this.messages = tmp;
		if (this.messages != '') alert(this.messages);
		else
		{
            alert('Error Interno 696. Sentimos las molestias, por favor intentelo mas tarde.' + '\n Error 696. Sorry, please try again. Thanks' + '\n Errore interno 696: problemi di comunicazione con il server, per favore riprova più tardi');
        }
        this.clean();
    };
    this.showLayer=function()
    {
        if(this.arrErrores.length==0) return -1;
        codigos=this.toString();
        if(codigos!='')
        {
            try
            {
                //YAHOO.util.Connect.asyncRequest('GET', '/common/index.php?pg=error&squid=yes&mode=simple&id_errores='+codigos,
                YAHOO.util.Connect.asyncRequest('GET', '/common/error/' + codigos + '|',
                {
                    success: function(response)
                             {
                                if(response.responseText!='') errores.messages.push(response.responseText);
                                else errores.messages.push('Error nums: '+codigos);
                                return errores.layer();
                             },
                    failure: function (response)
                             {
                                alert('Error Interno 698. Sentimos las molestias, por favor intentelo mas tarde.' + '\n Error 698. Sorry, please try again. Thanks' + '\n Errore interno 698: problemi di comunicazione con il server, per favore riprova più tardi');
                             }
                },'id_errores='+codigos);
            }catch(err){
                new Ajax.Request('/common/error/' + codigos + '|',{method:'post',onSuccess:function(response)
                {
                    if(response.responseText!='') errores.messages.push(response.responseText);
                    else errores.messages.push('Error nums: '+codigos);
                    return errores.layer();
                }
                });
            }
        }else return this.layer();
    };

    this.showAlert=function()
    {
        if(this.arrErrores.length==0) return -1;
        codigos=this.toString();

                this.processActions();
        if(codigos!='')
        {
            try
            {
                //YAHOO.util.Connect.asyncRequest('GET', '/common/index.php?pg=error&mode=simple&squid=yes&id_errores='+codigos,
                YAHOO.util.Connect.asyncRequest('GET', '/common/error/' + codigos + '|',
                {
                    success: function(response)
                             {
                                if(response.responseText!='') errores.messages.push(response.responseText);
                                else errores.messages.push('Error nums: '+codigos);
                                return errores.alert();
                             },
                    failure: function (response)
                             {
                                alert('Error Interno 699. Sentimos las molestias, por favor intentelo mas tarde.' + '\n Error 699. Sorry, please try again. Thanks' + '\n Errore interno 699: problemi di comunicazione con il server, per favore riprova più tardi');
                             }
                },'id_errores='+codigos);
            }catch(err){
                new Ajax.Request('/common/error/' + codigos + '|',{method:'post',onSuccess:function(response)
                {
                    if(response.responseText!='') errores.messages.push(response.responseText);
                    else errores.messages.push('Error nums: '+codigos);
                    return errores.alert();
                }
                });
            }
        }else return this.alert();
    };

    this.show=function()
    {
        if($('splash_error')) $('splash_error').style.display='none';
        try{
        this.showAlert();
        }catch(e){}
        this.clean();
    };

    this.hide=function() { $('lista_de_errores').innerHTML=''; }
    this.clean=function() { this.arrErrores=new Array(); this.messages=new Array();}
    this.cuantos=function() { return this.arrErrores.length; };
    this.close=function() { this.processActions(); this.clean(); };
    this.processActions=function()
    {
        var actions='';
        for (y=0;y<this.arrErrores.length;y++)
        {
            if(this.arrErrores[y].action!='') actions+=this.arrErrores[y].action;
        }
        try
        {
            eval(actions);
        }catch(e) {
        }
    };

    this.eval=function(val)
    {
        if(this.arrErrores.length==0) return -1;
        codigos=this.toString();
        if(codigos!='')
        {
            try
            {
                //YAHOO.util.Connect.asyncRequest('GET', '/common/index.php?pg=error&squid=yes&mode=simple&id_errores='+codigos,
                YAHOO.util.Connect.asyncRequest('GET', '/common/error/' + codigos + '|',
                {
                    success: function(response)
                             {
                                if(response.responseText!='') errores.messages.push(response.responseText);
                                else errores.messages.push('Error nums: '+codigos);
                                eval(val(errores.messages.join("\n").replace(/<br\/>/g, '\n')));
                             },
                    failure: function (response)
                             {
                                alert('Error Interno 700. Sentimos las molestias, por favor intentelo mas tarde.' + '\n Error 700. Sorry, please try again. Thanks' + '\n Errore interno 700: problemi di comunicazione con il server, per favore riprova più tardi');
                             }
                },'id_errores='+codigos);
            }catch(err){
            new Ajax.Request('/common/error/' + codigos + '|',{method:'post',onSuccess:function(response){
                if(response.responseText!='') errores.messages.push(response.responseText);
                else errores.messages.push('Error nums: '+codigos);
                eval(val(errores.messages.join("\n").replace(/<br\/>/g, '\n')));
            }});
            }
        }else{
            eval(val(this.messages));
        }
        this.clean();
    };
}

//Metodos para cambiar el color de campos con errores
//Pone el color a amarillo y le da el focus
function setErrorBackColor(obj)
{
    if (!obj.style) alert(obj.name);
    obj.style.backgroundColor='#FFFF75';
    obj.onchange=setGoodBackColor;
}

//Pone el color a blanco
function setGoodBackColor()
{
    this.style.backgroundColor='#FFF';
}

// Mantiene compatibilidad con el infinitamente abusado urchinTracker
function urchinTracker(false_page)
{
    if (typeof pageTracker != 'undefined')
    {
        pageTracker._trackPageview(false_page);
    }
    
    if (typeof gaTracker != 'undefined' && gaTracker != 'null')
    {
        pageTracker._trackPageview(false_page);
        gaTracker._trackPageview(false_page);
    }
}
