
/* Nuovi metodi di trimming sulle stringhe */
function strltrim() {
        return this.replace(/^\s+/,'');
}

function strrtrim() {
        return this.replace(/\s+$/,'');
}
function strtrim() {
        return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
function strleft( num ) {
        return this.substring(0,num);
}
function strright( num ) {
        return this.substring(this.length-num,this.length);
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;
String.prototype.left = strleft;
String.prototype.right = strright;

/* Nuove funzioni sulle date */

/* Return day of week as 0 for Sunday ... 6 for Saturday
   http://tech.irt.org/articles/js045/index.htm */
function getdayofweek()
{
        var a = Math.floor((14 - (this.getMonth() + 1))/12);
        var y = this.getYear() - a;
        var m = (this.getMonth() + 1) + 12*a - 2;
        var d = (this.getDate() + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;

        return d;
}

/* Return number of days in a month
   http://developer.irt.org/script/533.htm */
function getdaysinmonth (year, month) {
     return 32 - new Date( this.getYear(), this.getMonth(), 32).getDate();
}

Date.prototype.getDayOfWeek = getdayofweek;
Date.prototype.getDaysInMonth = getdaysinmonth;

/* Varie funzioni di controllo */
function CheckData( data )
{
        var arr = data.split("/");
        var giorno = arr[0],
                mese = arr[1],
                anno = arr[2];

        return !(arr.length != 3 ||
                         (isNaN( giorno ) || giorno < 1 || giorno > 31) ||
                         (isNaN( mese ) || mese < 1 || mese > 12) ||
                         (isNaN( anno ) || anno < 1900 || anno > 2100));
}

function CheckMail( mail )
{
        var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/

        return email_reg_exp.test( mail );
}

function CheckPercorsoGB( percorso )
{
        var arr =  percorso.split( /[.;-]/ );
        var reg = /^([Ff][1-9]|0?[1-9]|1[0-6])$/;

        for( i = 0; i < arr.length; i++ )
        {
                if( !reg.test( arr[i] ))
                        return false;
        }

        return true;
}

/* Cambia il colore per le text box in input */
window.onload=function(){
        if(document.getElementsByTagName) {
                var focus = false;

                var inp=document.getElementsByTagName("input");
                for(i=0;i<inp.length;i++){
                        var type = inp[i].getAttribute("TYPE");

                        if (type == "text" || type == "password" || type == "file")
                        {
                                focus = true;

                                inp[i].onfocus=function(){this.style.backgroundColor="#FFC"};
                                inp[i].onblur=function(){this.style.backgroundColor="#FFF"};
                        }
                }

                var txtar=document.getElementsByTagName("textarea");
                for(i=0;i<txtar.length;i++)
                {
                        focus = true;

                        txtar[i].onfocus=function(){this.style.backgroundColor="#FFC"};
                        txtar[i].onblur=function(){this.style.backgroundColor="#FFF"};
                }

                // Se c'è un campo in input, posizionamo direttamente sul primo elemento della form
                // che non sia disabilitato
                if( focus )
                {
                        var form=document.getElementsByTagName("form");

                        if( form.length > 0 )
                                for( i = 0; i < form[0].length; i++ )
                                        if( !form[0][i].disabled && form[0][i].type != "hidden" )
                                        {
                                                form[0][i].focus();
                                                break;
                                        }
                }
        }
}

// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e)
{
        var key;
        var keychar;

        if (window.event)
           key = window.event.keyCode;
        else if (e)
           key = e.which;
        else
           return true;
        keychar = String.fromCharCode(key);

        // control keys
        if ((key==null) || (key==0) || (key==8) ||
                (key==9) || (key==13) || (key==27) )
           return true;

        // numbers
        else if ((("0123456789.").indexOf(keychar) > -1))
           return true;

        return false;
}

function dateonly(myfield, e)
{
        var key;
        var keychar;

        if (window.event)
           key = window.event.keyCode;
        else if (e)
           key = e.which;
        else
           return true;
        keychar = String.fromCharCode(key);

        // control keys
        if ((key==null) || (key==0) || (key==8) ||
                (key==9) || (key==13) || (key==27) )
           return true;

        else if( ("0123456789/").indexOf(keychar) > -1 )
                return true;

        return false;
}

function normaldate(field)
{
        var date = field.value;

        if( date.length == 8 && date.indexOf("/") == -1 )
            date = date.substring(0,2) + "/" + date.substring(2,4) + "/" + date.substring(4,8);
        else if( date.length == 6 && date.indexOf("/") == -1 )
            date = date.substring(0,2) + "/" + date.substring(2,4) + "/20" + date.substring(4,6);

        field.value = date;
}

function showTooltip(srcObj,message,distanzaX,distanzaY) {
        var pos;
        pos=findPos(srcObj);
        tooltip.style.left=pos[0] + srcObj.offsetWidth + distanzaX;
        tooltip.style.top=pos[1] + distanzaY;
        tooltip.style.display="block";
        tooltiptext.innerHTML=message;
}

function hideTooltip() {
        tooltip.style.display="none";
}

        function findPos(obj) {
                var curleft = curtop = 0;
                        if (obj.offsetParent) {
                                curleft = obj.offsetLeft;
                                curtop = obj.offsetTop;
                                while (obj = obj.offsetParent) {
                                        curleft += obj.offsetLeft;
                                        curtop += obj.offsetTop;
                                }
                        }
                        return [curleft,curtop];
        }

//<div id="tooltip" class="tooltipBox" >
//  <table border="0" cellpadding="0" cellspacing="0">
//    <tr>
//      <td><img src="../immagini/tooltip/TT_TL.gif" border="0"></td>
//      <td style="background-image:url(../immagini/tooltip/TT_T.gif);"></td>
//      <td><img src="../immagini/tooltip/TT_TR.gif" border="0"></td>
//    </tr>
//    <tr>
//      <td style="background-image:url(../immagini/tooltip/TT_L.gif);" height="2"></td>
//      <td style="background-color:#ffffcc;"></td>
//      <td style="background-image:url(../immagini/tooltip/TT_R.gif);" height="2"></td>
//    </tr>
//    <tr>
//      <td style="background-image:url(../immagini/tooltip/TT_L.gif);" valign="middle"><img src="../immagini/tooltip/TT_CL.gif" border="0"></td>
//      <td style="background-color:#ffffcc;"><span id="tooltiptext" class="tooltipText"></span></td>
//      <td style="background-image:url(../immagini/tooltip/TT_R.gif);"></td>
//    </tr>
//    <tr>
//      <td style="background-image:url(../immagini/tooltip/TT_L.gif);" height="2"></td>
//      <td style="background-color:#ffffcc;"></td>
//      <td style="background-image:url(../immagini/tooltip/TT_R.gif);" height="2"></td>
//    </tr>
//    <tr>
//      <td><img src="../immagini/tooltip/TT_BL.gif" border="0"></td>
//      <td style="background-image:url(../immagini/tooltip/TT_B.gif);"></td>
//      <td><img src="../immagini/tooltip/TT_BR.gif" border="0"></td>
//    </tr>
//  </table>
//</div>


