// Script EMMEDATA per DModa WEB

// Prototype per funzione Trim delle stringhe;

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}

Date.prototype.addGiorni = function(giorni) {
  this.setTime(this.getTime() + (giorni * 86400000));
}


Date.prototype.toViewString = function() {
  
  var gg = this.getDate().toString();
  var mm = (this.getMonth() + 1).toString();
  
  if (gg.length == 1) gg = "0" + gg;
  if (mm.length == 1) mm = "0" + mm;
  
  return  gg + "/" + mm + "/" + this.getFullYear();
}

Date.differenzaGiorni = function(argdata1, argdata2) {

  var data1 = new Date(argdata1.getFullYear(), argdata1.getMonth(), argdata1.getDate());
  var data2 = new Date(argdata2.getFullYear(), argdata2.getMonth(), argdata2.getDate());
  
  var valore1 = data1.getTime();
  var valore2 = data2.getTime();
  
  if (valore1 == valore2) return 0;
  
  var diff = Math.abs(valore1 - valore2);  
  var diffday = (diff / 86400000).toFixed();
  var diffr = (diff % 86400000);
  
  if (diffr > 0) diffday++;
  if (valore1 < valore2) diffday *= -1;
  
  return diffday;  
}



function ed_CreaPopAtr(argSrcForm,argWAM,argRoutine,argCampo,argValore,argNomeHid,argDescTDobj,argDescField)
{
 argSrcForm.__curPopAtr = new Object();
 argSrcForm.__curPopAtr.WAM = argWAM;
 argSrcForm.__curPopAtr.Routine = argRoutine;
 argSrcForm.__curPopAtr.Campo = argCampo;
 argSrcForm.__curPopAtr.Valore = argValore;
 argSrcForm.__curPopAtr.HidObjName = argNomeHid;
 argSrcForm.__curPopAtr.objDescTD = argDescTDobj;
 argSrcForm.__curPopAtr.strDescField = argDescField;
 argSrcForm.__curPopAtr.__stato = "inizio";
}


function ed_HandlerClosePopup(evt)
{
  
  if ((event.propertyName == "_stato") && (event.srcElement._stato == "finito"))
  {
    var f = event.srcElement.form;
    
    if ((f != null) && (f.__curPopAtr != null))
    {

      if (f.__curPopAtr.Routine != "")
      {
        
        if (f.__curPopAtr.Campo && f.__curPopAtr.Campo.length > 0) insertHiddenField(f, f.__curPopAtr.Campo, f.__curPopAtr.Valore);
      	   	   	   	    	
      	submitForm(f, f.__curPopAtr.WAM, f.__curPopAtr.Routine);
      	
      }
      
    }
      
  }
 
}


function ed_GetAdjacentTD(objBase)
{
  var ParentTD = null;
  var AdjacentTD = null;
  
  var testObj = null;
  
  if (objBase == null) return null;
  
  testObj = objBase;

  while (true)
  {
    
    if (testObj.parentElement == null) break;
    if (typeof(testObj.parentElement) != "object") break;
    
    if (testObj.parentElement.tagName == "TD")
    {
       ParentTD = testObj.parentElement;
       break;
       
    } else {
    
      testObj = testObj.parentElement;
    }
    
  }
  
  
  if (ParentTD != null)
  {
    if (ParentTD.nextSibling != null)
    {
      AdjacentTD = ParentTD.nextSibling;
    }
  }
    
  return AdjacentTD;
}



function ed_RunPopUp(argObjbutton,argSourceForm,argWAMApp,argWEBRoutine,argWidth,argHeight,argFieldToExchange,argPartition,argLanguage,argFieldFrom,argPromptType,arglwebWAMName,argSalvaUtente,argResize,argNomeForzato,argChiudiFinestra)
{
	var __POPTop = 0;
	var __POPLeft = 0;	
	var iHeight = 0;
	var iWidth = 0;	
	var __POPFeature = "directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no";
	var tmpNomeWin = "";
	var tmpCurID = new Date();

	
	if (argResize == undefined) argResize = "no";
	if (argNomeForzato == undefined) argNomeForzato = "";
	if (argChiudiFinestra == undefined) argChiudiFinestra = true;
	
	
	iHeight = parseInt(argHeight);
	iWidth = parseInt(argWidth);	
	
	__POPTop = parseInt((window.screen.availHeight / 2) - (iHeight / 2)); 
	__POPLeft = parseInt((window.screen.availWidth / 2) - (iWidth / 2)); 
	
	__POPFeature += ",width=" + iWidth + ",height=" + iHeight;
	__POPFeature += ",top=" + __POPTop + ",left=" + __POPLeft;
  __POPFeature += ",resizable=" + argResize;
	

	tmpNomeWin = (argNomeForzato != "") ? argNomeForzato : "ED_POPUP_WINDOW_" + tmpCurID.getTime().toString();
	
	window.__CurPopup = window.open("", tmpNomeWin,__POPFeature);
	window.__CurPopup.__PopUpLoaded = false;


  if ((typeof(argSourceForm.__curPopAtr) == "object") && (argSourceForm.__curPopAtr != null))
  {
     argSourceForm.__curPopAtr.Partizione = argPartition;
     argSourceForm.__curPopAtr.Lingua = argLanguage;
  }
  
  
  if (document.thisobjVF_SY503 != null)
  {    
    var tmpDATA1 = document.thisobjVF_SY503.VF_System.strURL_Data1;
		
	  InsertHidden(argSourceForm, 'R_DMUSER', '');
	  InsertHidden(argSourceForm, 'WDM_SESS', '');
	  InsertHidden(argSourceForm, 'WDM_INFO', '');

		argSourceForm.R_DMUSER.value = document.thisobjVF_SY503.VF_System.strURL_User;
		argSourceForm.WDM_SESS.value = tmpDATA1;
    
  }

  InsertHidden(argSourceForm, 'R_FLDEXC', argFieldToExchange);
	document.LANSA.R_FLDEXC.value = argFieldToExchange;
	InsertHidden(argSourceForm, 'R_FLDFROM', argFieldFrom);
	document.LANSA.R_FLDFROM.value = argFieldFrom;
	
	_HandleEvent(argWAMApp,argWEBRoutine,"","",argPartition,argLanguage,argSourceForm,tmpNomeWin); 
	
	if (argChiudiFinestra)	attachEvent('onblur', ed_PopUp_onblur_Handler);
}

function ed_PopUp_onblur_Handler(evt)
{
  if (window.__CurPopup == null) return; 

  try {
    
    if (window.__CurPopup.closed) return;
    if (window.__CurPopup.__PopUpLoaded == true)
    {
      window.__CurPopup.close();
      window.__CurPopup = null;
    }
  } catch(exception) 
  { 
      window.__CurPopup = null;    
  }   
  
}


function ed_return_from_Popup(argFieldToExchange, argSrcForm, argTgtForm, argFieldFrom)
{

  if( argFieldToExchange != "" )
  {    
    var targElem = null;
    var srcElem = null;
    var sNomeCampo = "";
    var tmpNome = "";
    var iPosNome = "";
    
    // Controllo se devo gestire un Array di valori
    
    if (argFieldToExchange == "*ARRAY")
    {
      
      var curAry = argTgtForm.__CampiEXC;
      
      if (curAry != null)
      {
        for (i = 0; i < curAry.length; i++)
        {
          
          srcElem = argSrcForm.elements[curAry[i].src];
          targElem = argTgtForm.elements[curAry[i].dest];
                   
          if ((srcElem != null) && (targElem != null))
          {
            targElem.value = srcElem.value;            
          }
        }        
      }
      
      return; 
    }
        
    // Controllo se esiste il campo di destinazione    
    try {
     targElem = argTgtForm.elements[argFieldToExchange];
    } catch(exception) { }
        
    // Controllo se esiste il campo Sorgente
    if (argFieldFrom != "")
    {
      tmpNome = argFieldFrom;
    } else {
      tmpNome = argFieldToExchange;
    }

    iPosNome = tmpNome.lastIndexOf(".");

    // POSSO AVERE IL CAMPO DI UNA LISTA QUINDI SE TROVO UN PUNTO NEL NOME 
    // DEVO PRENDERE SOLO LA PARTE FINALE PER TROVARE IL CAMPO SORGENTE
    if (iPosNome > 0)
    {
      sNomeCampo = tmpNome.substr((iPosNome + 1));
    } else {
      sNomeCampo = tmpNome;
    }

    try {
     srcElem = argSrcForm.elements[sNomeCampo];
    } catch(exception) {}
        
    if ((targElem != null) && (srcElem != null))
    {
      targElem.value = srcElem.value;
      
      // SE HO PASSATO ANCHE IL CAMPO PER LA DESCRIZONE CERCO DI ASSEGNARLA
      
      if (argTgtForm.__curPopAtr.strDescField != "")
      {
        if (argTgtForm.__curPopAtr.objDescTD != null)
        {
          try {
            descElem = argSrcForm.elements[argTgtForm.__curPopAtr.strDescField];            
          } catch(exception) {}
          
          if ((srcElem != null))
          {
            argTgtForm.__curPopAtr.objDescTD.innerHTML = descElem.value;
          }          
        }        
      }
      
      try
      {
        targElem.focus();
        var oRange = targElem.createTextRange();
        oRange.select();
      } catch(exception) { }
      
      if( IsIE() )
      {
        targElem.fireEvent("onchange");
      }else{
        targElem.onchange();
      }        
    }
    
  }else{
  
    var elems = argSrcForm.elements;
    var length = elems.length;
    var primoElement = null;
       
    for(var index = 0; index < length; index++)
    {
      var elem = elems[index];
      var name = elem.name;
      
      if( name.length <= 0 ) name = elem.id;

      if( name.length > 0 && name[0] != "_" )
      {
        if( elem.tagName.toLowerCase() == "input" && (elem.type == "hidden" || elem.type == "text"))
        {
          
          if (name == "R_POPTIT") continue;
          if (name == "PGPOPDESC") continue;
          if (name == "PGPOPFLT1") continue;
          if (name == "PGPOPFLT2") continue;
          if (name == "PGPOPRDBN") continue;
          if (name == "PGPOPSHF1") continue;
          if (name == "PGPOPSHF2") continue;
          
          
          var value = elem.value;
           
          if( value != null )
          {
            var targElem = argTgtForm.elements[name];
                        
            if( targElem != null )
            {
              targElem.value = value;

              if( index == 0 )
              {
                primoElement = targElem;
              }              
            }
          }
        }
      }
    }
    
    if (primoElement != null)
    {
      
      try
      {
        primoElement.focus();
        var oRange = primoElement.createTextRange();
        oRange.select();
      } catch(exception) { }
    	
      if( IsIE() )
      {
        primoElement.fireEvent("onchange");
      }else{
        primoElement.onchange();
      }    	      
      
      
      // SE HO PASSATO ANCHE IL CAMPO PER LA DESCRIZONE CERCO DI ASSEGNARLA
      
      if (argTgtForm.__curPopAtr.strDescField != "")
      {
        if (argTgtForm.__curPopAtr.objDescTD != null)
        {
          try {
            descElem = argSrcForm.elements[argTgtForm.__curPopAtr.strDescField];            
          } catch(exception) {}
          
          argTgtForm.__curPopAtr.objDescTD.innerHTML = descElem.value;
        }        
      }
      
      
    }  
  }
  
    
} // FINE Funzione 


function ed_Reload_DModaWEB(argLingua)
{
 window.open("/cgi-bin/lansaweb?webapp=DMODAWEB+webrtn=HomePage+ml=LANSA:XHTML+part=CAL+lang=" + argLingua,"_self","","");
}



function controllaNuovo()
{  

  if ( (event.ctrlKey) && ((event.keyCode == 110) || (event.keyCode == 78)) )
  {
    alert("????");
    
    event.keyCode = 0;
    
    event.returnValue = false;
  }
  
} 



function ed_Run_NewFrameWork(argIDSessione, argIDFrameWork, argTitolo, argFrameOBJT, argUtente, argAzienda, argDesAzienda, argDepo, argDesDepo, argPagina, argTraceFrameWork, argTraceInterna, argTraceStatistics)
{

  var pMenuBar= "no";
  var pLocationBar= "no";
  var pParametri = "directories=no,resizable=yes,scrollbars=no,status=no,toolbar=no,top=0,left=0";

 
  var strURL = "/images/";
  
  if (argPagina.substr(0, 5) == "DMWEB")
  {
    strURL += "/DModaFrameWorks";
    
  } else {
  
    strURL += "/VLFPersonali";
  }
  
  strURL += "/DMODAWEB_AVVIOFRAME.HTM?WAMS=Y+WAMHELP=Y";
  
  if (argTraceFrameWork == "S")
  {
    strURL += "+TRACE=Y";
  }
  
  if (argTraceStatistics == "S")
  {
    strURL += "+STATS=Y";
  }


  strURL += "+USER=" + argUtente;
  strURL += "+URLDATA1=" + argIDSessione;
  strURL += "+URLDATA2=";
  
	if (argTraceInterna == "S")
	{
	  strURL += "Y";	
	  pMenuBar="yes";
	  pLocationBar = "yes";
	  
	} else {	  
	  strURL += "N";
	}

	var curDate = new Date();
  
  strURL += argIDFrameWork + "_" + curDate.getTime().toString();
  strURL += "+URLDATA3=" + argIDFrameWork + argFrameOBJT;
  strURL += "+P1=" + escape(argTitolo);
  strURL += "+P2=" + escape(argAzienda);
  strURL += "+P3=" + escape(argDesAzienda);
  strURL += "+P4=" + escape(argDepo);
  strURL += "+P5=" + escape(argDesDepo);
  strURL += "+P6=" + escape(argPagina + "_" + g_lxmlLang);
  strURL += "+P7=" + escape(argTraceInterna);
  strURL += "+LANG=" + g_lxmlLang;

  pParametri += ",menubar=" + pMenuBar + ",location=" + pLocationBar;

	window.open(strURL, "_blank", pParametri);

}



function ed_Run_Framework_Window(argIDFrameWork, argTitolo, argFrameOBJT, argUtente, argAzienda, argDesAzienda, argDepo, argDesDepo, argURL, argTraceFrameWork, argTraceInterna, argTraceStatistics, argFinestraUnica)
{

  //var wFrame = null;
	
	var idFinestra = "";
	var prmIDFinestra = "";
  var locHref = "";
  var pMenuBar= "no";
  var pParametri = "directories=no,location=no,resizable=yes,scrollbars=no,status=no,toolbar=no,top=0,left=0";
  
  
  if (argFinestraUnica == "S")
  {
    idFinestra = argIDFrameWork;
    prmIDFinestra = "";
  
  } else {
  
  	var curDate = new Date();
    
    idFinestra = argIDFrameWork + "_" + curDate.getTime().toString();
    prmIDFinestra = idFinestra;
  }

  if (argTraceInterna == "S") pMenuBar="yes";

  
  
  locHref = "/images/DMODAWEB_FRAMEBASE.htm?";
  locHref += "p1=" + escape(argIDFrameWork);
  locHref += "&p2=" + escape(argTitolo);
  locHref += "&p3=" + escape(argFrameOBJT);
  locHref += "&p4=" + escape(argUtente);
  locHref += "&p5=" + escape(argAzienda);
  locHref += "&p6=" + escape(argDesAzienda);
  locHref += "&p7=" + escape(argDepo);
  locHref += "&p8=" + escape(argDesDepo);
  locHref += "&p9=" + escape(argURL);
  locHref += "&p10=" + escape(argTraceFrameWork);
  locHref += "&p11=" + escape(argTraceInterna);
  locHref += "&p12=" + escape(argTraceStatistics);
  locHref += "&p13=" + escape(prmIDFinestra);

  pParametri += ",menubar=" + pMenuBar;
  	
	
	//wFrame = window.open(locHref, idFinestra, "directories=no,location=no,menubar=yes,resizable=yes,scrollbars=no,status=no,toolbar=no,top=0,left=0");	
	//wFrame.resizeTo(window.screen.availWidth, window.screen.availHeight);
  //wFrame.navigate(locHref);

 	window.open(locHref, "", pParametri);
  

  return;

}



function ed_Run_WAM_window(argModulo, argURL, argWidth, argHeight)
{
  var wWAM = null;
	var iTop = 0;
	var iLeft = 0;
  var iW = 0;
  var iH = 0;

  if (argWidth === undefined)
  {
    iW = 780;
  } else {
    iW = parseInt(argWidth, 10);  
  }

  if (argHeight === undefined)
  {
    iH = 580;
  } else {
    iH = parseInt(argHeight, 10);
  }
	
	if ((iW == 0) || (iW > window.screen.availWidth)) iW = 780;
	if ((iH == 0) || (iH > window.screen.availHeight)) iH = 580;
	
	iLeft = (window.screen.availWidth - iW) / 2;
	iTop = (window.screen.availHeight - iH) / 2;
	
	wWAM = window.open(argURL, argModulo, "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,top=" + iTop + ",left=" + iLeft);
	wWAM.resizeTo(iW, iH);

	return;
}

function ed_Assegna_Stato(argOggetto, argDft, argStatoOn, argStatoOff)
{
  if (argStatoOn == null)
  {
    argStatoOn = "visible";
  }
  
  if (argStatoOff == null)
  {
    argStatoOff = "hidden";
  }
  
  
  if (argOggetto.__statoattuale === undefined)
  {
   argOggetto.__statoattuale = argDft;
  } 
  
  if (argOggetto.__statoattuale == argStatoOff)
  {
  	argOggetto.__statoattuale = argStatoOn;
  }else{
  	argOggetto.__statoattuale = argStatoOff;
  }
  
  return;
}


function ed_Visualizza_DIV_Moduli(argDIVobj, argSubDIVobj, argIMGobj, argVisualizza, argPath)
{
  var baseheight = 0;

	if (argVisualizza == "hidden")
	{
 	  argDIVobj.style.height = "22px";
	  argIMGobj.src = argPath + "/VF_ic3019.gif";		
	} else {
	  baseheight = 50 + argSubDIVobj.style.height;
 	  argDIVobj.style.height = baseheight.toString() + "px";
	  argIMGobj.src = argPath + "/VF_ic3029.gif";		
	}
	
	argDIVobj.style.overflowY = argVisualizza;
	argSubDIVobj.style.visibility = argVisualizza;
	
	return;
}


function ed_Visualizza_DIV_TABELLA(argObj, argDIV, argTR, argImgPath)
{
	var baseheight = 0;

	if (argObj.__statoattuale == "hidden")
	{
	  argTR.style.height = "1px";
	  argObj.src = argImgPath + "/VF_ic3019.gif";		
	} else {
	  baseheight = 5 + argDIV.style.height;
 	  argTR.style.height = baseheight.toString() + "px";
	  argObj.src = argImgPath + "/VF_ic3029.gif";		
	}
	
	argTR.style.overflowY = argObj.__statoattuale;
	argDIV.style.visibility = argObj.__statoattuale;

	return;
  
  
}



function ed_open_autologoff_win()
{
	var tmpMonitorName = "";
	var tmpWINFeature =  "top=1,left=1,height=100,width=100,channelmode=no,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no,fullscreen=no";
   
  tmpMonitorName = "DMODAWEB_AUTOLOGOFF_WIN_" + window._objDMWEBTerminator.utente;
  
  return window.open("/images/DModaFrameWorks/DMWEB_HT006.htm", tmpMonitorName, tmpWINFeature);
  
}


function ed_visualizza_immagine_grande(argPicture)
{
  var tmp = window.open("", "_PictureGrande", "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
  tmp.document.write("<HEAD><TITLE>Immagine</TITLE></HEAD>");
	tmp.document.write("<BODY LEFTMARGIN=0 TOPMARGIN=0>");
  tmp.document.write("<IMG BORDER=\"0\" SRC=\"" + argPicture + "\">");
	tmp.document.write("</BODY>");	
	tmp.document.execCommand("Refresh");
}



function ed_expand_DIV(argImgObj, argDIVObj, argImgPath, argImgClps, argImgExpd)
{

  if (argImgObj.__statoattuale == "none")
  {
    argImgObj.__statoattuale = "inline";
  }else{
    argImgObj.__statoattuale = "none";
  }
  
  if (argImgClps == null)
  {
    argImgClps = "VF_ic3029.gif";
  }
  if (argImgExpd == null)
  {
    argImgExpd = "VF_ic3019.gif";
  }

  if (argImgObj.__statoattuale == "none")
  {
		argImgObj.src = argImgPath + "/" + argImgExpd;		
  } else {
		argImgObj.src = argImgPath + "/" + argImgClps;		
  }
  
  argDIVObj.style.display = argImgObj.__statoattuale;

  return;
    
}


function ed_TrovaTextArea(argTextAreaIDName, argFormSrc)
{
  var objTextArea = null;
  
  objTextArea = document.getElementById(argTextAreaIDName);
  
  if (objTextArea == null)
  {
    for (var o = 0; o < argFormSrc.elements.length; o++)
    {
    
      if (argFormSrc.elements[o].tagName != "TEXTAREA") continue;
      
      // se Fast-Part
      
      if (argFormSrc.elements[o].objFP_RXXXX != null)
      {
      
        if (argFormSrc.elements[o].objFP_RXXXX.strFP_Name == argTextAreaIDName)
        {
          objTextArea = argFormSrc.elements[o];
          break;
        }
      
      } else {
      
        if (argFormSrc.elements[o].name == argTextAreaIDName)
        {
          objTextArea = argFormSrc.elements[o];
          break;
        }
      
      }                  
    }
  }
  
  return objTextArea;            
}


function ed_crea_wait_cursor()
{
  
  var waitIMG = document.getElementById("waitIMG");
  
  if (waitIMG != null)
  {
    document.body.removeChild(waitIMG);
  }

  waitIMG = document.createElement("IMG");
  waitIMG.setAttribute("id", "waitIMG");
  
  var datappo = new Date();
  waitIMG.setAttribute("src", "/images/imgwait_loader.gif?dummy=" + datappo.getTime().toString());
  waitIMG.style.position = "absolute";
  waitIMG.style.left = (document.body.clientWidth - waitIMG.width) / 2;
  waitIMG.style.top = (document.body.clientHeight - waitIMG.height) / 2;
  
  document.body.appendChild(waitIMG);

}

function ed_autotab(argSorgente, argDestinazione)
{
  if (  ((event.keyCode >= 48) && (event.keyCode <= 57)) || ((event.keyCode >= 96) && (event.keyCode <= 105)) )
  {
    if (argSorgente.value.length == argSorgente.getAttribute("maxlength")) argDestinazione.focus();	
  }
}



