
    /**
    * deletes blank spaces in right and left of string
    */
    function core__trim(string) 
    {
        return string.replace(/^\s+|\s+$/g,"");
    }
    
    /**
    * deletes blank spaces in left of string
    */
    function core__ltrim(string) 
    {
        return string.replace(/^\s+/,"");
    }
    
    /**
    * deletes blank spaces in right of string
    */
    function core__rtrim(string) 
    {
        return string.replace(/\s+$/,"");
    }
    
    /**    
    * translates the given text    
    */    
    function core__lang_t(text)
    {    
        // Pass through the array
        for(var key in aT)
        {               
            // if found the match text
            if(key == text)
            {
                // return
                return(aT[key]);
            }
        }
        
        // if not exists returns the given text
        return(text);
    }
    
    /**    
    * clean symbols how to /r /n or rare chars from begining or ends text
    */
    function core__sanitizeText(text) 
    {    
        var output = "";
        for (var i = 0; i < text.length; i++) 
        {
            if ((text.charCodeAt(i) == 13) && (text.charCodeAt(i + 1) == 10)) 
            {
                i++;
                output += " ";
            }
            else
            {
                output += text.charAt(i);
            }
        }    
        return output.replace(/\s+/g," ").replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,"$1");         
    }
    
    /**    
    * open a new window popup
    */
    function core__win(url, target, scrollbars, width, height)
    {    
        window.open(url, target, "width="+width+", height="+height+", location=no, titlebar=no, status=no, scrollbars="+scrollbars);
    }
    
    /**
    * redirects to the url page in the target
    */
    function core__redirect(url, target)
    {
        window.open(url, target);
    }
    
    /**    
    * get all elements in a form and returns a string with format
    * name=harrison|years=20|lastname=ford
    */
    function core__formData2QueryString(docForm)
    {     
        var submitContent = '';
        var formElem;
        var lastElemName = '';
        
        for (i = 0; i < docForm.elements.length; i++) 
        {            
            formElem = docForm.elements[i];

            switch (formElem.type) 
            {
                
                // Text fields, hidden form elements
                case 'text':
                case 'hidden':
                case 'password':
                case 'textarea':
                case 'select-one':
                
                        
                    valorPost = escape(formElem.value).replace('&', '%26');
                    valorPost = escape(formElem.value).replace('+', '%2B');
                    
                    // Anteriormente : submitContent += formElem.name + '=' + escape(formElem.value) + '|'                    
                    //submitContent += formElem.name + '=' + escape(formElem.value).replace('&', '%26') + '|';
                    if(formElem.name != '')
                    {
                        submitContent += formElem.name + '=' + valorPost + '|';
                    }
                    else
                    {
                        if(formElem.id != '')
                        {
                            submitContent += formElem.id + '=' + valorPost + '|';
                        }
                    }
                    break;
            
                // Radio buttons
                case 'radio':
                    if (formElem.checked) 
                    {
                        // Anteriormente : submitContent += formElem.name + '=' + escape(formElem.value) + '|'
                        submitContent += formElem.name + '=' + formElem.value + '|';
                    }
                    break;

                // Checkboxes
                case 'checkbox':                
                    if (formElem.checked) 
                    {
                        // Continuing multiple, same-name checkboxes
                        if (formElem.name == lastElemName) 
                        {
                            // Strip of end ampersand if there is one
                            if (submitContent.lastIndexOf('|') == submitContent.length-1) 
                            {
                                submitContent = submitContent.substr(0, submitContent.length - 1);
                            }
                            // Append value as comma-delimited string
                            // Anteriormente : submitContent += formElem.name + '=' + escape(formElem.value) + '|'
                            submitContent += ',' + formElem.value;
                        }
                        else 
                        {
                            // Anteriormente : submitContent += formElem.name + '=' + escape(formElem.value) + '|'
                            submitContent += formElem.name + '=' + formElem.value;
                        }
                        submitContent += '|';
                        lastElemName = formElem.name;
                    }
                    break;
            }
        }
        // Remove trailing separator

        // delete the last '|'
        submitContent = submitContent.substr(0, submitContent.length - 1);
        return submitContent;
    }        
    
    /**
    * verify the key pressed capturing the event onkeypress() and check with the reg match
    */
    function core__validateKey(event , pattern) 
    {
        key = (document.all) ? event.keyCode : event.which;

        //key =  8 => retroceso (para borrar) 
        //key =  9 y 0 =>  (para tabulador) 
        if (key==8 || key==9 || key==0 || key==13 || key==27) 
            return true;  

        te = String.fromCharCode(key);
        if (pattern.test(te))
        {
            return  true;
        }
        else
        {
            alert("\n"+core__lang_t('El dato introducido')+" ["+te+"] "+core__lang_t('no es válido.\n\n\tIntente de nuevo'));            
            return false;
        }
    }
    
    /**
    * shows a modal window
    * title string
    * func string
    * params string parameters in format param1|param2|param3
    * width
    * height
    */
    function core__modalWinOpen(title, func, params, width, height)
    {
        $('.fix-z-index').bgiframe();

        $('#dialog').dialogr('destroy').remove();
        $('<div id="dialog">').dialogr({
                title   : title, 
                width   : width, 
                height  : height,
                modal   : true
        });

        // if we want to call a php function
        if(func!="")
        {
            $('#dialog').html('Loading...');
            agent.call('', func, 'core__callbackModalWinOpen', params);
        }        
    }
    
    /**
    * callback of the php function calls
    */
    function core__callbackModalWinOpen(str)
    {
        $('#dialog').html(str);
    }
    
    /**
    * shows a modal window
    * title string
    * func string
    * params string parameters in format param1|param2|param3
    * width
    * height
    */
    function core__modalWinOpenjQuery(title, url, width, height)
    {
        $('.fix-z-index').bgiframe();
        $('#dialog').dialogr('destroy').remove();
        $('<div id="dialog">').dialogr({
                title   : title,
                width   : width, 
                height  : height,
                modal   : true                
        });
        
        // if we want to call a php function
        if(url!="")
        {
            $('#dialog').html('Loading...');            
            $.post(url, function(data) {                        
                $('#dialog').html(data);
            });
        }        
    }
    
    /**
    * closes the modal window
    */
    function core__modalWinClose()
    {        
        $("#dialog").dialogr('close');    
    }
    
    /**
    * shows an dialog for display animations processes
    */
    function core__modalWinOpenMessage(title, width, height)
    {
        $('#dialog').dialogr('destroy').remove();
        $('<div id="dialog">').dialogr({
                title     : title, 
                width     : width, 
                height    : height,
                modal     : true,
                resizable : false
        });   
    }
    
    /**
    * call ajax with sync mode and returns the result
    */
    function core__ajaxSyncCall(url)
    {
        var html = $.ajax({
            url: url,
            async: false
        }).responseText;
        
        return html;
    }
    
    /**
    * shows the div
    */
    function core__showDiv(div)
    {    
        document.getElementById(div).style.display     = 'block';
        document.getElementById(div).style.visibility  = 'visible';        
    }
    
    /**    
    * hide the div
    */
    function core__hideDiv(div)
    {    
        document.getElementById(div).style.display     = 'none';
        document.getElementById(div).style.visibility  = 'hidden';        
    }
    
    /**
    * checks if the date is in format dd/mm/yyyy
    */
    function core__checkDate(f)
    {    
        var validformat = /^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity
        var returnval   = false;
        if (!validformat.test(f.value))
        {
            returnval = false; 
        }
        else
        {
            // detailed check for valid date ranges
            var dayfield    = f.value.split("/")[0];
            var monthfield  = f.value.split("/")[1];    
            var yearfield   = f.value.split("/")[2];
            var dayobj = new Date(yearfield, monthfield-1, dayfield);
            
            if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
            {
                returnval = false;
            }
            else
            {
                returnval = true;
            }
        }    
        return(returnval);
    }    
    
    /**
    * checks if the year is leap (bisiesto)
    */    
    function core__LeapYearCheck(any)
    {
        if ( ( any % 100 != 0) && ((any % 4 == 0) || (any % 400 == 0)))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /***
    * set the focus in the field that has en Error
    */
    /*
    function core__setFocus(sForm, sElemento, bFocus)
    {     
        docForm = document.forms[sForm];
        var formElem;
        for (i = 0; i < docForm.elements.length; i++) 
        {
            formElem = docForm.elements[i];                
            if(formElem.name == sElemento)
            {
                if(bFocus)
                {
                    document.getElementById(sElemento).style.background = "#FFEAEA";
                    formElem.focus();
                }
                else
                {
                    document.getElementById(sElemento).style.background = "#FFEAEA";
                    formElem.focus();
                } 
                           
            }
        }
    }
    */
    
    /**
    * only shows a div with common name
    * for example if you have divlang_es, divlang_en, divlang_fr, you can tell to show only divlang_fr and hide all the 
    * other divlanx_xxxxx
    */
    function core__onlyOneDiv(name, common)
    {        
        if(document.getElementById(name).style.visibility=="visible")
        {
            core__hideDiv(name);
        }
        else
        {
            core__closeCommonDivs(common);
            core__showHideDiv(name);
        }
    }
    
    /**
    * close divs with common name pattern
    */
    function core__closeCommonDivs(common)
    {
        var aDivs = document.getElementsByTagName("div");        
        for(i=0; i<aDivs.length; i++)
        {            
            aValores = aDivs[i].id.split('_');                    
            if(aValores[0] === common)
            {                                
                core__hideDiv(aDivs[i].id);
            }
        }        
    }
    
    /**
    * shows or hide the div
    */
    function core__showHideDiv(div)
    {    
        if(document.getElementById(div).style.visibility != 'hidden')
        {        
            core__hideDiv(div);
        }
        else
        {                                        
            core__showDiv(div);                                
        }    
    }
    
    /**
    * check if a input is blank
    */
    function core__validateBlank(field, message)
    {
        var myField = document.getElementById(field);    
        if(myField != null)
        {
            if(myField.value=="")
            {
                alert(message);
                myField.focus();
                myField.style.background="#FFF0F0";
                return(false);
            }
        }
        return(true);     
    }
    
    /**    
    * check minimal len for a field
    */
    function core__validateMinLen(field, message, min)
    {
        var myField = document.getElementById(field);
        if(myField.value.length < min)
        {
            alert(message);
            myField.focus(); 
            myField.style.background="#FFEAEA"; 
            return(false);
        }
        return(true);
    }
    
    /**    
    * checks if the field accomplish the max leng restriction
    */
    function core__validateMaxLen(field, message, max)
    {
        var myField = document.getElementById(field);
        if(myField.value.length > iMax)
        {
            alert(message);
            myField.focus(); 
            myField.style.background="#FFEAEA"; 
            return(false);
        }
        return(true);
    }
    
    /**
    * checks if two fields are equal
    */
    function core__validateEquals(field1, field2, message)
    {
        var myField1 = document.getElementById(field1);
        var myField2 = document.getElementById(field2);
    
        if(myField1.value!=myField2.value)
        {
            alert(message);
            myField1.focus(); 
            myField1.style.background="#FFEAEA"; 
            return(false);
        }
        return(true);    
    }
    
    /**    
    * checks for a valid email
    * @modify 03/08/2010 Ivan (multiple emails)
    */
    function core__validateEmail(field, message)
    {
        var myField = document.getElementById(field);
        if(!core__validateBlank(field, message))
        {
            return(false);
        }
        else
        {
            // Maybe comes multiple e-mails on field
            myField.value = myField.value.replace(";",",");
            var emails = myField.value.split(",");
            for(i = 0; i < emails.length; i++)
            {
                emails[i] = core__trim(emails[i]);
                if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emails[i]))
                {
                } 
                else 
                {    
                    alert (core__lang_t('La dirección de email es incorrecta.'));    
                    myField.focus(); 
                    myField.style.background="#FFEAEA"; 
                    return(false); 
                }
            }
        }
        return(true);
    }
        
    /**    
    * checks for a valid url
    */
    function core__validateUrl(myField) 
    {   

        var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 

        if(RegExp.test(myField.value))
        {
        } 
        else 
        {    
            alert (core__lang_t('La URL es incorrecta.'));    
            myField.focus(); 
            myField.style.background="#FFEAEA"; 
            return(false); 
        }

        
        return(true);
    }   
      
    /**
    * checks if a field value is numeric.
    */
    function core__validateIsNumeric(field, message)
    {  
        var myField = document.getElementById(field);
        var value   = myField.value.toString();
        for (var i=0; i<value.length; i++)
        { 
            var car = value.charAt(i); 
            if (car<"0" || car>"9") 
            {
                alert(message);
                return false; 
            }
        } 
        return true;
    }
    
    /**
    * checks if a checkbox is selected
    */
    function core__validateCheckBox(field, message)
    {
        var myField = document.getElementById(field);
        if(myField.checked==false)
        {
            alert(message);
            myField.focus();
            myField.style.background="#FFF0F0";
            return(false);
        }
        return(true);
    }   
    
    /**
    * gets the readio button element selected of a radio control
    */
    function core__getRadioValue(ctrl) 
    {        
        for(i=0;i<ctrl.length;i++)
        {        
            if(ctrl[i].checked)
            {                
                return ctrl[i].value;
            }
        }        
        return(false);
    }    

    /**
    * core__validateFormatDate(fecha,sFrm)
    * Validate a date entered in a text input in the format dd /mm /yyyy
    */
    function core__validateFormatDate(fecha,sFrm)
    {
        var fecha_actual=new Date();
        anio_actual = fecha_actual.getFullYear();
        
        if (fecha != undefined && fecha.value != "" )
        {
            if (!/^\d{2}\/\d{2}\/\d{4}$/.test(fecha.value))
            {
                alert(core__lang_t('El formato de fecha es no válido. \nIngrese el formato (dd/mm/aaaa)'));
                fecha.style.background = "#FFEAEA"; 
                fecha.focus();
                return false;
            }
            var dia  =  parseInt(fecha.value.substring(0,2),10);
            var mes  =  parseInt(fecha.value.substring(3,5),10);
            var anio =  parseInt(fecha.value.substring(6),10);
     
            switch(mes)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8: 
                case 10:
                case 12:
                    numDias=31;
                    break;
                case 4: case 6: case 9: case 11:
                    numDias=30;
                    break;
                case 2:
                    if (core__LeapYearCheck(anio)){ numDias=29 }else{ numDias=28};
                    break;
                default:
                    alert(core__lang_t('El mes de la fecha introducida es errónea'));
                    fecha.style.background = "#FFEAEA"; 
                    fecha.focus();                    
                    return false;
            }
            if( anio < anio_actual )
            {
                alert(core__lang_t('El año de la Fecha introducida (debe ser mayor o igual al año actual), es errónea'));
                fecha.style.background = "#FFEAEA"; 
                fecha.focus();                    
                return false;
            }        
            else
            {
                any2 = anio_actual+2;
                if (anio > any2 )
                {
                    alert(core__lang_t('El año de la Fecha introducida (es mucho mayor al año actual, solo puede ser hasta ')+any2+"), "+core__lang_t('es errónea'));
                    fecha.style.background = "#FFEAEA"; 
                    fecha.focus();                    
                    return false;
                }
            }
     
            if (dia>numDias || dia==0)
            {
                alert(core__lang_t('El d&iacute;a de la Fecha introducida es errónea'));
                fecha.style.background = "#FFEAEA"; 
                fecha.focus();                                    
                return false;
            }

            return true;
        }
    }
    
    /**
    * check text added by user... without count junk generated for HTML TAGS
    * if text is longer than limit, force return with values of limit position
    * @param string text text to check
    * @param int length length of text
    * @param int limit default limit for trim text
    * @return string with to values (counter of all text and counter of real characters)
    */
    function core__checkRealText(text, length, limit)
    {
        realText = 0;
        countTotal = 0;
        inTag = false;
        
        for(var i = 0; i < length; i++)
        {
            if(text[i] == '<')
            {
                inTag = true;
            }
            else
            {
                if(inTag == false)
                {
                    realText++;
                    if(realText == limit)
                    {
                        return countTotal+'|'+realText;
                    }
                }
                else
                {
                    if(text[i] == '>')
                    {
                        inTag = false;
                    }
                }
            }
            countTotal++;
        }
        return countTotal+'|'+realText;
    }
    
