if(document.location.href.indexOf('.qntdev.com') == -1) // Se  sviluppo
    window.onerror = ShowErr;

function ShowErr(err,sURL,sLine)
{
    try 
    {
        var HtmlMsg = '';
        msgArray = new Array();
        urlArray = new Array();
        lnoArray = new Array();
        msgArray[msgArray.length] = err;
        urlArray[urlArray.length] = sURL;
        lnoArray[lnoArray.length] = sLine;
        var MsgErr = '';
        var Em = FindCtrl('ErrorMessage',false);
        if(Em!=null)
        {
            for (var i=0; i < msgArray.length; i++) 
            {
                MsgErr += 'Errore:[' + msgArray[i] + ']';
                MsgErr += '  nel File:[' + urlArray[i] + '] alla linea:[' + lnoArray[i] + ']';
            }

            MsgErr = MsgErr.replace("'","\'");
            HtmlMsg = '<image src="/utility/InvioEmailErrJs.aspx?ErrJs='+MsgErr+'" width="1" height="1" border="0" style="position:absolute;display:none">';
            Em.innerHTML += HtmlMsg;
        }
        else    // Html ancora non creato
        {
        }
        return true;
    }
    catch(er)
    {
        //alert(err+'\nLinea:'+sLine+'\nin:'+sURL);
        return false;
    }
}

var IsIE = (document.all) ? true:false;
function FindCtrl(NomeId,ShowAlert)
{
    var Ret=null;
    if(NomeId != null && NomeId != '')
    {
	    if (document.getElementById) 
		    Ret = document.getElementById(NomeId);
	    else 
		    Ret = eval('document.all.'+NomeId);
	}
    if(Ret==null && ShowAlert)
        alert('Attenzione! Il controllo \''+NomeId+'\' non è stato trovato con document.'+IsIE?'all...':'getElementById(...');
    
    return Ret;
}
function OpenUrlDialogCenter(Dest, wi, hi)
{
   var top=(screen.height-hi)/2;
   var left=(screen.width/2)- (wi/2);
   //window.showModelessDialog(Dest,"","dialogHeight: '+hi+'; dialogWidth: '+wi+'px; edge: Sunken; center: Yes; help: No; resizable: No; status: No;");
    if(genWnd != null) { if(!genWnd.closed) { genWnd.close(); } }
    window.showModalDialog(Dest,"","dialogHeight: '+hi+'; dialogWidth: '+wi+'px; edge: Sunken; center: Yes; help: No; resizable: No; status: No;");
}

var genWnd = null;
function OpenUrlCenter(Dest, Trg, wi, hi)
{
    if(genWnd != null) { if(!genWnd.closed) { genWnd.close(); } }
    genWnd = window.open(Dest, Trg, 'toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=' + ((screen.height-hi)/2) + ',left=' + ((screen.width/2)- wi/2) + ',width=' + wi + ',height=' + hi);
}

function OpenUrlTop(Dest, Trg, wi, hi)
{
    if(genWnd != null) { if(!genWnd.closed) { genWnd.close(); } }
    genWnd = window.open(Dest, Trg, "toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=0,left=" + ((screen.width/2)- wi/2) + ",width=" + wi + ",height=" + hi);
}

function AddQueryString(url, qsKey, qsValue)
{
    var retVal = url;
    if (url.indexOf('?') != -1 && (url.indexOf('?' + qsKey + '=') != -1 || url.indexOf('&' + qsKey + '=') != -1))
    {
        var pat = '';
        var rep = '';
        if(url.indexOf('?' + qsKey + '=') != -1)
        {
            pat = '?' + qsKey + '=[^&]*';
            rep = '?' + qsKey + '=' + qsValue;
        }
        else if(url.indexOf('&' + qsKey + '=') != -1)
        {
            pat = '&' + qsKey + '=[^&]*';
            rep = '&' + qsKey + '=' + qsValue;
        }
        var rg = new RegExp(pat);
        if(rg.test(retVal))
            retVal = retVal.replace(rg, rep);
    }
    else
    {
        if (url.indexOf('?') == -1)
            retVal += '?' + qsKey + '=' + qsValue;
        else
            retVal += '&' + qsKey + '=' + qsValue;
    }
    return retVal;    
}

/* >>> TOOLTIPS */
var ViewToolTip = false;
var OldBackGround=null;
var OldSpan=null;

function CloseToolTips() 
{
    ViewToolTip = false;
    var SHOW_TOOLTIPS = FindCtrl('SHOW_TOOLTIPS');
    if(SHOW_TOOLTIPS!=null)
        SHOW_TOOLTIPS.style.display='none';
        
    if(OldSpan!=null && OldSpan.style!=null && OldBackGround!=null)
        OldSpan.style.background=OldBackGround==null?'':OldBackGround;
}

function ShowToolTips(Titolo,Txt,Obj)
{
    var SHOW_TOOLTIPS = FindCtrl('SHOW_TOOLTIPS');
//    if(document.all!=null)
//    {
        OldSpan=Obj;
        ViewToolTip = true;
	    var posx = 0;
	    var posy = 0;
	    var TitoloForm='';
	    var Img = '';
	    if (!e) var e = window.event;
	    if(e==null)
	        return false;
	    if (e.pageX || e.pageY)
	    {
		    posx = e.pageX;
		    posy = e.pageY;
	    }
	    else if (e.clientX || e.clientY)
	    {
		    posx = e.clientX + document.body.scrollLeft;
		    posy = e.clientY + document.body.scrollTop;
	    }
	    Img = '<img src="/images/icone/ico_tips.gif" align="absMiddle" hspace="4" vspace="4" width="16px" height="16px" border="0">';

        TitoloForm =Img+'<span style="font-weight:bold;">'+Titolo+'</span><br>';
        TitoloForm+='<img id="lineaToolTips" src="/his_front/images/spacer.gif" style="background-color:#996600;" vspace="1" width="300" height="1" border="0"><br>';
        
        var Tt = null;
        if(SHOW_TOOLTIPS!=null)
        {
            Tt=SHOW_TOOLTIPS;
            Tt.innerHTML = TitoloForm+Txt;
        }
        else
        {
            var txtDiv = '<div id="SHOW_TOOLTIPS" OnClick="CloseToolTips();" OnMouseOut="CloseToolTips();" ';
            txtDiv += 'style="';
            txtDiv += 'border: solid 1px #996600;background-color:#FFFFCC;';
            txtDiv += 'padding:0px 4px 4px 2px;';
            txtDiv += 'display:inline; position: absolute;z-index:10000;';
            txtDiv += 'filter: progid:DXimageTransform.Microsoft.dropshadow(OffX=4,OffY=4, Color=#88666633, Positive=true);';
            txtDiv += 'visibility:visible;';
            txtDiv += '">';
            txtDiv += TitoloForm+Txt;
            txtDiv += '</div>';
            document.forms[0].innerHTML += txtDiv;
            
            Tt = FindCtrl('SHOW_TOOLTIPS');
        }
                
        if(Obj!=null && Obj.style!=null)
        {
            OldBackGround=Obj.style.background;
            Obj.style.background='#FFFFAA';
        }
        
        if(Tt!=null)
        {
            if(ViewToolTip)
                Tt.style.display = 'block';
            else
                CloseToolTips();
            nX=posx+10;
            nY=posy+10;
            
            if(nX + Tt.clientWidth > document.width)
                nX = document.width - Tt.clientWidth;
                
            if(nY + Tt.clientHeight > document.height)
                nY = document.height - Tt.clientHeight;
            
            if(document.images && document.images.lineaToolTips)
                document.images.lineaToolTips.style.width=Tt.clientWidth-10;
                
            if(nX<0) nX=0;
            if(nY<0) nY=0;
            
            Tt.style.left = nX;
            Tt.style.top  = nY;
        }
}

/* <<< TOOLTIPS */

function setOpacity(Obj,value)
{
    if(Obj != null && Obj.style != null && Obj.style.opacity != null && Obj.style.filter != null)
    {
	    Obj.style.opacity = value/10;
	    Obj.style.filter = 'alpha(opacity=' + value*10 + ')';
    }
}

function ReplaceParamValueInUrl(Url,Parametro,Valore)
{
    if(Url != null && Url != '' && Parametro != null && Parametro != '')
    {
        var Ret;
        var StartPos;
        var EndPos
        Pos=-1;
        Ret = '';
        if((StartPos = Url.indexOf(Parametro))>-1)  // Elimina il parametro
        {
            Ret = Url.substring(0,StartPos-1);
            if( (EndPos = Url.indexOf('&',StartPos+1)) > -1 )
                Ret += Url.substring(EndPos);
        }
        else
            Ret = Url;
            
        if(Ret.indexOf('?')<0)      // Non trovato
            return Ret + '?' + Parametro + '=' + Valore;
        else
            return Ret + '&' + Parametro + '=' + Valore;
    }
}
function clipValues(obj, which)
{
	if(obj.clip)
	{
		var re = /rect\(([0-9]+).*[ |,]([0-9]+).*[ |,]([0-9]+).*[ |,]([0-9]+).*\)/;
		var clipv = re.exec(obj.clip);
		if (which=="t") return Number(clipv[1]);
		if (which=="r") return Number(clipv[2]);
		if (which=="b") return Number(clipv[3]);
		if (which=="l") return Number(clipv[4]);
	}
	return 0;
}
function clipTo(obj, t, r, b, l)
{
	if(obj.clip) { obj.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"; }
}
function clipBy(obj, t, r, b, l)
{
	if(obj.clip) { obj.clip = "rect("+(this.clipValues(obj,'t')+t)+"px "+(this.clipValues(obj,'r')+r)+"px "+Number(this.clipValues(obj,'b')+b)+"px "+Number(this.clipValues(obj,'l')+l)+"px)"; }
}
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function AddClass(obj, cls) 
{
	var clsMtc = new RegExp('\\b' + cls + '\\b', 'g');
	if(!clsMtc.test(obj.className))
	    obj.className += ' ' + cls;
}
function RemoveClass(obj, cls) 
{
	var clsMtc = new RegExp('\\b' + cls + '\\b', 'g');
	if(clsMtc.test(obj.className))
		obj.className = obj.className.replace(clsMtc, ' ');
}

function ResizeImg(img)
{
    var maxW = 300.00;
    var maxH = 300.00;
    var srcW = img.width;
    var srcH = img.height;
    var Scale1 = (maxW / srcW);
    var Scale2 = (maxH / srcH);
    var Scale = (Scale1 <= Scale2) ? Scale1 : Scale2;
    img.width = parseInt(srcW * Scale);
    img.height = parseInt(srcH * Scale);
}

var DateFormatInt;
function EvalStrDate(strDate, strTipoData, lingua)
{

    var strSeparatorArray = new Array("-","/","."," ");
    var strMonthArray = new Array(12);
    var err = 0;
    var booFound = false;
    var strDateArray;
    var intElementNr;
    var strDay;
    var strMonth;
    var strYear;
    var intday;
    var intMonth;
    var intYear;
    var intValue;

    strMonthArray[0] = "Jan";
    strMonthArray[1] = "Feb";
    strMonthArray[2] = "Mar";
    strMonthArray[3] = "Apr";
    strMonthArray[4] = "May";
    strMonthArray[5] = "Giu";
    strMonthArray[6] = "Jul";
    strMonthArray[7] = "Aug";
    strMonthArray[8] = "Sep";
    strMonthArray[9] = "Oct";
    strMonthArray[10] = "Nov";
    strMonthArray[11] = "Dic";

    DateFormatInt = null;
    if (strDate.length < 1) 
     {// nel caso la data non sia valorizzata non vi è
      // niente da controllare 
      return true;}
                
     for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
     {      
        if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
        {   strDateArray = strDate.split(strSeparatorArray[intElementNr]);
            if (strDateArray.length != 3) 
            {     // ci si aspetta di avere la data costituita da tre
                  // parti (giorno mese anno) quando ci sono dei separatori
                  err = 1;
                  return false;
             }else{
                    switch(lingua)
                    {
                        case 'IT':
                            strDay = strDateArray[0];
                            strMonth = strDateArray[1];
                            strYear = strDateArray[2];
                            break;
                        case 'EN':
                            strDay = strDateArray[1];
                            strMonth = strDateArray[0];
                            strYear = strDateArray[2];
                            break;
                        case 'JP':
                            strDay = strDateArray[2];
                            strMonth = strDateArray[1];
                            strYear = strDateArray[0];
                            break;
                    }
             }
          booFound = true;
        }
     }
     if (booFound == false) {
         // caso in cui non ci sono dei separatori
         // ci si aspetta di ricevere la data nei formati ddmmyyyy o ddmmyy
         if (strDate.length > 5 ) 
            {strDay = strDate.substr(0, 2);
             strMonth = strDate.substr(2, 2);
             strYear = strDate.substr(4);}
          else
            {return false;}
     }
     if(strYear == undefined || strMonth == undefined || strDay == undefined)
        return false;
     if (strYear.length == 2) {
        strYear = '20' + strYear;
     }else{
        if (strYear.length != 4) {
            err = 2;
           return false;
        }
     }
     strDay = TrimWord(strDay)
     for (i=0; i < strDay.length; i++) 
     {
        intValue = parseInt(strDay.substr(i,1), 10);
        if (isNaN(eval(intValue))) 
        {
           err = 4;
           break;
        }
      }
       
      if(err == 4)
        {return false;}
      else
        {intday = parseInt(strDay, 10);}
      strMonth=TrimWord(strMonth)
      for (i=0; i < strMonth.length; i++)
      {
        intValue = parseInt(strMonth.substr(i,1), 10);
        if (isNaN(intValue)) {
            err = 5;
            break;
          }
      }
      
      if (err == 5)
      {
        if(strMonth.length == 3)
        {
            for (i = 0;i<12;i++) 
            {
                if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
                {
                    err = 0
                    intMonth = i+1;
                    strMonth = strMonthArray[i];
                    i = 12;
                }
             }
         }else{
            //alert(strTipoData.toUpperCase() + ': Inserire le prime tre lettere del nome del mese !');
            return false;
         }
         if (err == 5)
            {return false;}
      }else{
        intMonth = parseInt(strMonth, 10);
      }
      strYear=TrimWord(strYear)
                
      for ( i=0; i < strYear.length; i++)
      {
         intValue = parseInt(strYear.substr(i,1), 10);
        if(strYear.substr(0,1) == 0) {
            err = 6;
                  
            break;
       }
       if (isNaN(intValue)) 
          {err = 6;
           break;
          }
      }

      if(err == 6)
        {return false;}
      else
        {intYear = parseInt(strYear, 10);}  
      if (intMonth > 12 || intMonth < 1) 
      {
        err = 7;
        return false;
      }
      if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
      // si verifica la compatibilità tra mese e giorno
      {
        err = 8;
        return false;
      }
      if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
      // si verifica la compatibilità tra mese e giorno
      {

        err = 9;
        return false;
      }

      if (intMonth == 2) 
      // si verifica la compatibilità tra mese e giorno
      {
        if (intday < 1) 
        {
            err = 10;
            return false;
        }
        if (AnnoBisestile(intYear) == true) 
        {
            if (intday > 29) 
            {
                err = 11;
                return false;
            }
         }else{
            if (intday > 28) 
            {
                err = 12;
                return false;
             }
         }
      }
      DateFormatInt = RetIntDate(intday,intMonth,strYear);
      return true;
}

function RetIntDate(Day,Month,Year)
{

    var intDate = 0;
     if (Month < 10)  
     {
        if (Day < 10) 
            {intDate = Year + '0' + Month + '0' + Day;}
        else
            {intDate =  Year + '0' + Month + Day;}
     }else{
        if (Day < 10) 
            {
            intDate = Year + Month + '0' + Day;}
        else
            {intDate = Year + Month + Day;}
     }
     return intDate;
}
function ResetDate(lingua)
{
    var strDate = '';
    switch(lingua)
    {
        case 'IT':
            strDate = 'gg/mm/aaaa';
            break;
        case 'EN':
            strDate = 'mm/dd/yyyy';
            break;
        case 'JP':
            strDate = 'yyyy/mm/dd';
            break;
    }
    return strDate;
}

function TrimWord(WordArg)
  {
    var theWord;
    var i;
    var j;
      
   theWord = "";
      
   for ( i=0; i < WordArg.length; i++)
        {j = (WordArg.length -1) - i;
         if (WordArg.substr(j,1)!= " ")
            {theWord = WordArg.substr(0,j + 1);
             break;}
        }
   
   for (i=0; i< theWord.length ; i++)
        {if (theWord.substr(i,1)!= " ")
            {theWord = theWord.substr(i);
             break;}
        }
          
   return (theWord); 
  }

function AnnoBisestile(intYear) 
{
    if (intYear % 100 == 0) {
        if (intYear % 400 == 0) 
            { return true; }
    }else{
        if ((intYear % 4) == 0) 
            { return true; }
    }
    return false;
 }

