if(document.location.href.indexOf('.qntdev.com') == -1) // Se  sviluppo
    window.onerror = ShowErr;


String.prototype.trim = function() {
	return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
}
String.prototype.ltrim = function () {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function () {
	return this.replace(/\s+$/,"");
}
String.prototype.removeSpace = function () {
	return this.replace(/\s*/,"");
}

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('ErrorMessageBarraAlta',false);
        //alert(Em);
        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="'+url+'" 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;
    //alert(NomeId);
    if(NomeId != null && NomeId != '')
    {
	    if (IsIE) 
	    	Ret = eval('document.all.'+NomeId);
	    else 
	    	Ret = document.getElementById(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(...');
        //alert('Attenzione! Il controllo \''+NomeId+'\' non è stato trovato con document.' + ((document.getElementById) ? 'all...' : 'getElementById(...'));
    }
    return Ret;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

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;");
   window.showModalDialog(Dest,"","dialogHeight: '+hi+'; dialogWidth: '+wi+'px; edge: Sunken; center: Yes; help: No; resizable: No; status: No;");
}

function OpenUrlCenter(Dest, Trg, wi, hi)
{
   window.open (Dest, Trg, 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,top=' + ((screen.height-hi)/2) + ',left=' + ((screen.width/2)- wi/2) + ',width=' + wi + ',height=' + hi);
}

function OpenUrlCenterScrollbars(Dest, Trg, wi, hi)
{
   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)
{
   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);
}


/* >>> 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="/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 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);
}

function AnnoBisestile(datea)
{
	datea = parseInt(datea);

	if(datea%4 == 0)
	{
		if(datea%100 != 0)
		{
			return true;
		}
		else
		{
			if(datea%400 == 0)
				return true;
			else
				return false;
		}
	}
return false;
}
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 'EN':
                            strDay = strDateArray[1];
                            strMonth = strDateArray[0];
                            strYear = strDateArray[2];
                            break;
                        case 'JP':
                            strDay = strDateArray[2];
                            strMonth = strDateArray[1];
                            strYear = strDateArray[0];
                            break;
                        //case 'IT':
                        default:
                            strDay = strDateArray[0];
                            strMonth = strDateArray[1];
                            strYear = strDateArray[2];
                            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 = strDay.trim();
     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=strMonth.trim();
      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=strYear.trim();
                
      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 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;    
}


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 ChangeClass(obj, cls) 
{
    if(obj != null)
        obj.className = cls;
	//var clsMtc = new RegExp('\\b' + cls + '\\b', 'g');
	//if(clsMtc.test(obj.className))
	//	obj.className = obj.className.replace(clsMtc, ' ');
}
function ChangeClass2(idObj, cls, idObj2, cls2) 
{
    var obj = FindCtrl(idObj);
    var obj2 = FindCtrl(idObj2);
    if(obj != null)
        obj.className = cls;
        
    if(obj2 != null)
        obj2.className = cls2;
	//var clsMtc = new RegExp('\\b' + cls + '\\b', 'g');
	//if(clsMtc.test(obj.className))
	//	obj.className = obj.className.replace(clsMtc, ' ');
}
var menuOpener = null;
var menuIdOpen = null;
var timeoutClose = null;
function OpenMenu(src, offsetX, offsetY, idObj)
{
    var x = parseInt(findPosX(src));
    var y = parseInt(findPosY(src));

    x = x + parseInt(offsetX);
    y = y + parseInt(offsetY);
    
    if(menuOpener != src.id)
    {
        if(menuIdOpen != idObj)
        {
            ShowHideObject(menuIdOpen, false);
        }
        var obj = document.getElementById(idObj);
        if(obj)
        {
            obj.onmouseover = function(e) 
            { 
                ChangeMenuOpener(idObj);
                ChangeMenuIdOpen(idObj); 
                //ChkMenuActive(src);
                if(timeoutClose != null) { clearTimeout(timeoutClose); }
                
                /*if (!e) var e = window.event;
	            e.cancelBubble = true;
	            if (e.stopPropagation) e.stopPropagation();*/
                
                //ChangeClass(obj,'tdVoceMenuApertoOver');
            };
            obj.onmouseout = function(e) 
            { 
                timeoutClose = setTimeout('CloseMenu(\'' + src.id + '\', \'' + idObj + '\', \'' + idObj + '\');', 100); 

                //ChangeClass(obj,'tdVoceMenuAperto');
                
                /*if (!e) var e = window.event;
	            e.cancelBubble = true;
	            if (e.stopPropagation) e.stopPropagation();*/
            };

            obj = (obj.style) ? obj.style : obj;
            obj.top = '' + y + 'px';
            obj.left = '' + x + 'px';
        }
        ChangeMenuOpener(src.id);
        ChangeMenuIdOpen(idObj);
        ShowHideObject(idObj, true);
    }
}
function ChangeMenuOpener(id)
{
    menuOpener = id;
}
function ChangeMenuIdOpen(id)
{
    menuIdOpen = id;
}
function ChkMenuActive(src)
{
    if(src.id.indexOf('tdMenu_') != -1)
    {
        if(src.className.indexOf('TopActive') == -1)
        {
            ChangeClass(src,'TopActive');
            ChangeClass(document.getElementById(src.id+'b'),'BtmActive');
        }
    }
    else if(src.id.indexOf('tdPrimo') != -1)
    {
        if(src.className.indexOf('PrimoActive') == -1)
        {
            ChangeClass(src,'PrimoActive');
            var id = src.id.replace(/tdPrimo/, 'tdMenu_01');
            ChangeClass(document.getElementById(id),'TopActive');
            ChangeClass(document.getElementById(id+'b'),'BtmActive');
        }
    }
}
function CloseMenu(srcId, idObj, idClose)
{
    if(menuOpener == srcId || menuOpener == idClose)
    {
        menuOpener = '';
        var src = document.getElementById(srcId);
        ShowHideObject(idObj, false);
        if(src.id.indexOf('tdMenu_') != -1)
        {
            ChangeClass(src,'Top');
            ChangeClass(document.getElementById(src.id+'b'),'Btm');
        }
        else if(srcId.indexOf('tdPrimo') != -1)
        {
            ChangeClass(src,'Primo');
            var id = src.id.replace(/tdPrimo/, 'tdMenu_01');
            ChangeClass(document.getElementById(id),'Top');
            ChangeClass(document.getElementById(id+'b'),'Btm');
        }
    }
}
function ShowHideObject(idObj, show)
{
    var obj = document.getElementById(idObj);
    if(obj)
    {
        obj = (obj.style) ? obj.style : obj;
        //if(show && obj.display != 'block') { obj.display = 'block'; }
        //else if(!show && obj.display != 'none') { obj.display = 'none'; }
        if(show && obj.visibility != 'visible') { obj.visibility = 'visible'; }
        else if(!show && obj.visibility != 'hidden') { obj.visibility = 'hidden'; }
    }
}

function ResetDate(lingua)
{
    var strDate = '';
    switch(lingua)
    {
        case 'EN':
            strDate = 'mm/dd/yyyy';
            break;
        case 'JP':
            strDate = 'yyyy/mm/dd';            
            break;
        default:
            strDate = 'dd/mm/yyyy';
            break;            
    }
    return strDate;
}

function setCookie(name, value, expires) 
{ 
	var argv = setCookie.arguments;  
	var argc = setCookie.arguments.length;  
	//var expires = (argc > 2) ? argv[2] : null;  
	var path = "/";  
	var domain = null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}
function getCookie(name)
{  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen)
	{
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}
function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}