﻿/*	******************************************************************************************
	******************************************************************************************
	LIBRERIA FUNZIONI JS 
	******************************************************************************************
	****************************************************************************************** */



var txtCodice;
var txtDescrizione;

var oldValue;
var nAnno;
var totale;
var objLabel;
/***** FUNZIONE PER CERCARE IL CODICE NEGOZIO ***/
function setCodiceNegozio(codice, textBox)
{
    objFire = document.getElementById(textBox);
    objFire.value = codice;
    fireHTMLEvent(objFire,"onchange");
}
function changeText(obj, obj2)
{
    txtCodice = obj.id;
    txtDescrizione = obj2;
    PageMethods.getStore(obj.value, onComplete);
}
function onComplete(args)
{
    document.getElementById(txtCodice).value = args[0];
    document.getElementById(txtDescrizione).value = args[1];
}


/***** FUNZIONE PER CERCARE IL CODICE ZONA ***/
function setCodiceZona(codice, textBox)
{
    objFire = document.getElementById(textBox);
    objFire.value = codice;
    fireHTMLEvent(objFire,"onchange");
}
function changeTextZona(obj, obj2)
{
    txtCodice = obj.id;
    txtDescrizione = obj2;
    PageMethods.getZone(obj.value, onCompleteZone);
}
function onCompleteZone(args)
{
    document.getElementById(txtCodice).value = args[0];
    document.getElementById(txtDescrizione).value = args[1];
}


function focusTextBox(obj, className)
{
    oldValue = obj.value;
    obj.value ='';
    obj.className =className;
}


function changeTextBox(obj, className, objCheckBox)
{
   var chk = document.getElementById(objCheckBox);
    obj.className =className;
    chk.checked = true;
}
function isUpdate(objTextBox, className, objCheckBox)
{ 
    var chk = document.getElementById(objCheckBox);
    var obj = document.getElementById(objTextBox);
    if(chk.checked)
    {
        obj.className =className;
    }
}
function blurTextBox(obj, className)
{
    if(obj.value=="0" || obj.value=="")
    {
        obj.value = oldValue;
    }
    obj.className =className;
    
    
}


function fireHTMLEvent(obj,eventType) 
{ 
    if(typeof obj.fireEvent != "undefined") 
    {
        obj.fireEvent(/^on/.test(eventType) ? eventType : "on" + eventType) 
    }
    else 
    { 
        var evt = document.createEvent("HTMLEvents"); 
        evt.initEvent(eventType.replace(/^on/,""),false,true) 
        obj.dispatchEvent(evt); 
    } 
}

/***   SELEZIONA DESELEZIONA TUTTI I CHECKBOX DI UNA TABELLA     ***/
 function chkUnchkAllElements( objTabella, sElementCheckAll, sIndexOf )
 {
    var oForm = document.getElementById(objTabella);
    var sElementCheckAll = sElementCheckAll;
    if (oForm.getElementsByTagName("input").length != undefined) 
    {
        for(i=0; i<oForm.getElementsByTagName("input").length; i++)
         {
            if(oForm.getElementsByTagName("input")[i].type == "checkbox"  && oForm.getElementsByTagName("input")[i]!= sElementCheckAll && oForm.getElementsByTagName("input")[i].id.indexOf(sIndexOf)>-1)
              {  
                    oForm.getElementsByTagName("input")[i].checked = sElementCheckAll.checked;
                    oForm.getElementsByTagName("input")[i].disabled =  sElementCheckAll.checked;
            }
                    
         }
    } 
    else 
    {
         if(oForm.getElementsByTagName("input").type == "checkbox" && oForm.getElementsByTagName("input") != sElementCheckAll && oForm.getElementsByTagName("input").id.indexOf(sIndexOf)>-1)
         {
            oForm.getElementsByTagName("input").checked = sElementCheckAll.checked;
            oForm.getElementsByTagName("input").disabled =  sElementCheckAll.checked;
            
         }
    }
    
 }
 
 
 var bCheck =  true;
  function chkUnchkUnlock( objTabella, sIndexOf )
 {
    var oForm = document.getElementById(objTabella);
    var sElementCheckAll = sElementCheckAll;
    if (oForm.getElementsByTagName("input").length != undefined) 
    {
        for(i=0; i<oForm.getElementsByTagName("input").length; i++)
         {
            if(oForm.getElementsByTagName("input")[i].type == "checkbox" && oForm.getElementsByTagName("input")[i].id.indexOf(sIndexOf)>-1)
                if(!oForm.getElementsByTagName("input")[i].disabled)
                    oForm.getElementsByTagName("input")[i].checked = bCheck;
         }
    } 
    else 
    {
         if(oForm.getElementsByTagName("input").type == "checkbox" && oForm.getElementsByTagName("input").id.indexOf(sIndexOf)>-1)
            oForm.getElementsByTagName("input").checked = bCheck;
    }
    bCheck = !bCheck;
 }
 function calcolaTotale(objTabella)
 {
    var oForm = document.getElementById(objTabella);
    var sElementCheckAll = sElementCheckAll;
    totale = 0;
    if (oForm.getElementsByTagName("input").length != undefined) 
    {
      for(i=0; i<oForm.getElementsByTagName("input").length; i++)
        {
         if(oForm.getElementsByTagName("input")[i].type == "checkbox")
            if(oForm.getElementsByTagName("input")[i].checked)
            {
            
                if(oForm.getElementsByTagName("input")[i+2].type == "text")
                {
                     contenuto = oForm.getElementsByTagName("input")[i+2].value;
                     contenuto = contenuto.replace(",",".");
                     if(isNaN(parseFloat(contenuto)))
                     {
                        contenuto = 0;
                     }
                     totale+=parseFloat(contenuto);
                }
            }
        }
    } 
    document.getElementById(objLabel).innerHTML ="&euro; "+totale.toFixed(2).toString().replace(".",",");
 }
/********** APRIRE UN POPUP **********/
function popup(URL,NOME,H,W) { 
    window.open(URL,NOME,'toolbar=no,scrollbars=yes,resizeable=no,width='+W+',height='+H+''); 
        }
        
        
/***** FUNZIONI PER CERCARE LA STRADA ***/
var txtIdVia;
var txtSVia;
var txtSCap;
var txtSCentroAbitato;
var txtLocalita;
var txtProvincia;
var txtRegione;
var txtNote;


function setCodiceStrada(codice, textBox)
{
    objFire = document.getElementById(textBox);
    objFire.value = codice;
    fireHTMLEvent(objFire,"onchange");
}
//ALLA FUNZIONE CHANGETEXTSTRADA PASSO L'OGGETTO TEXTBOX OBJ E I NOMI DEGLI ALTRI TEXTBOX DA RIEMPIRE
//NELL'ORDINE txtSVia;
//txtSCap;
//txtSCentroAbitato;
//txtLocalita;
//txtProvincia;
//txtRegione;
//txtNote;
function changeTextStrada(obj, obj1, obj2, obj3, obj4, obj5, obj6, obj7)
{
    txtIdVia = obj.id;
    txtSVia = obj1;
    txtSCap = obj2;
    txtSCentroAbitato = obj3;
    txtLocalita = obj4;
    txtProvincia = obj5;
    txtRegione = obj6;
    txtNote = obj7;
    //CHIAMO IL WEB SERVICE NELLA PAGINA CHE MI RESTITUISCE L'OGGETTO VIA IN FORMATO STRING[]
    // [System.Web.Services.WebMethod]
    // public string[] getVia(int idVia)
    PageMethods.getVia(parseFloat(obj.value), onCompleteStrada);
}
function onCompleteStrada(args)
{
    document.getElementById(txtIdVia).value = args[0];
    document.getElementById(txtSVia).value = args[1];
    document.getElementById(txtSCap).value = args[2];
    document.getElementById(txtSCentroAbitato).value = args[3];
    document.getElementById(txtLocalita).value = args[4];
    document.getElementById(txtProvincia).value = args[5];
    document.getElementById(txtRegione).value = args[6];
    document.getElementById(txtNote).innerText = args[7];
} 


/***** FUNZIONE PER CERCARE IL CODICE ARTICOLO ***/
var textBoxArticolo;
function setCodiceArticolo(codice)
{
    objFire = document.getElementById(textBoxArticolo);
    objFire.value = codice;
    fireHTMLEvent(objFire,"onchange");
}
function changeTextArticolo(obj,obj2)
{
    txtCodice = obj.id;
    txtDescrizione = obj2;
    PageMethods.codiceArticolo(obj.value, onComplete);
}



function openPopupStd( url,sNome, dimx, dimy, sScroll) {
    dimwidth = dimx;
    dimheight = dimy;
    x = (800 - dimwidth)/2, y = (600 - dimheight)/2;
    if (screen) {
	    x = (screen.availWidth - dimwidth)/2;
	    y = (screen.availHeight - dimheight)/2;
    }
    finestra1 = window.open(url,sNome,'location=no,status=no,toolbar=no,scrollbars='+sScroll+',resizable=no,width='+dimwidth+',height='+dimheight+',screenX='+x+',screenY='+y+',top='+y+',left='+x);
    if (finestra1) finestra1.focus();
}       

/********************ZOOM **************************/
function ZoomIt( sImgFileName, nBorderInPixel, bOtherZoomWin ) {
		var d = new Date();
		var sPageName = 'ZoomPage';
		if (bOtherZoomWin) sPageName='Zoom'+d.getHours()+d.getMinutes()+d.getSeconds();

		wWin=window.open('',sPageName,'width=200, height=200,resizable=yes');
		wWin.document.write('<HTML><HEAD><TITLE>Zoom image</TITLE>\n');

		wWin.document.write('\n');
		wWin.document.write('\n');

		wWin.document.write('</HEAD><BODY bgcolor="#FFFFFF" onload="ResizeMe();" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">\n');
		wWin.document.write('<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="middle">\n');
		wWin.document.write('<IMG ID="IMMAGINE" SRC="'+ sImgFileName+ '" BORDER="0">\n');
		wWin.document.write('</td></tr></table>\n');

		wWin.document.write('<script language="javascript">\n');	//script = scri+pt per non avere problemi di visualizzazione codice in DW
		wWin.document.write('\n');

		wWin.document.write('var isNS4 = (navigator.appName.indexOf("Netscape") >= 0 && \n');
		wWin.document.write('                parseFloat(navigator.appVersion) >= 4 && parseFloat(navigator.appVersion) < 5) ? 1 : 0; \n');
		wWin.document.write('var isNS6 = (navigator.appName.indexOf("Netscape") >= 0 && \n');
		wWin.document.write('                parseFloat(navigator.appVersion) >= 5) ? 1 : 0; \n');
		wWin.document.write('var isIE4 = (document.all) ? 1 : 0; \n');
		wWin.document.write('\n');
		wWin.document.write('\n');
		wWin.document.write('function ResizeMe() { \n');

		wWin.document.write('if (isNS6) { \n');
		wWin.document.write('window.innerWidth = document.images[0].width+'+2*nBorderInPixel+'; \n');
		wWin.document.write('window.innerHeight = document.images[0].height+'+2*nBorderInPixel+'; \n');
		wWin.document.write('} \n');

		wWin.document.write('if (isNS4) { \n');
		wWin.document.write('window.innerWidth = document.images[0].width+'+2*nBorderInPixel+'; \n');
		wWin.document.write('window.innerHeight = document.images[0].height+'+2*nBorderInPixel+'; \n');
		wWin.document.write('} \n');

		wWin.document.write('if (isIE4) { \n');
		wWin.document.write('window.resizeTo(500, 500); \n');
		wWin.document.write('width = 500 - (document.body.clientWidth -  document.images[0].width) + '+2*nBorderInPixel+'; \n');
		wWin.document.write('height = 500 - (document.body.clientHeight -  document.images[0].height) + '+2*nBorderInPixel+'; \n');
		wWin.document.write('window.resizeTo(width, height); \n');
		wWin.document.write('} \n');

		wWin.document.write('} \n');
		wWin.document.write('\n');
		wWin.document.write('ResizeMe();');

		wWin.document.write('\n');
		wWin.document.write('</script>\n');	//script = scri+pt per non avere problemi di visualizzazione codice in DW

		wWin.document.write('</BODY></HTML>\n');

		wWin.document.close();

	}


function elementsShowHide(objSender, objShowHide, bShow)
{
  var oObj = document.getElementById(objShowHide);
  if(bShow)
  {
    objSender.src ="../images/ico/ico_minus.gif";
    oObj.style.display="";
  }
  else
  {
    objSender.src ="../images/ico/ico_plus.gif";
    oObj.style.display="none";
  }
 }


function elementsShowHideObject(objSender, objShowHide)
 {
  var oObj = document.getElementById(objShowHide);
  if(objSender.src.indexOf("ico_plus.gif")>-1)
  {
    objSender.src ="../images/ico/ico_minus.gif";
  }
  else
  {
    objSender.src ="../images/ico/ico_plus.gif";
  }
  if( oObj.style.display=="none")
  {
     oObj.style.display="";
  }
  else
  {
      oObj.style.display="none";
  }

 }
 
 
 
 /////////////////////////////////////////////
 /////////////////////////////////////////////
 
 function pingOK(dDate)
{
    var txt ='<table width="150" style="border-style:solid; border-width:1px;" cellpadding="2" cellspacing="0" bordercolor="#0066CC" background="../Images/toolTip/bgToolTip.gif">';
    txt+='<tr><td valign="middle"><img src="../Images/ico/ico_ping_ok.gif" width="19" height="17" style="vertical-align:middle;" /> <strong>OK</strong><br />';
    txt+='Last Ping: '+dDate+'</td></tr></table>';
    return txt;
}

function pingKO(dDate)
{
    var txt ='<table width="150" style="border-style:solid; border-width:1px;" cellpadding="2" cellspacing="0" bordercolor="#FF0000" background="../Images/toolTip/bgAlertToolTip.gif">';
    txt+='<tr><td valign="middle"><img src="../Images/ico/ico_ping_ko.gif" width="19" height="17" style="vertical-align:middle;" /><font color="#990000"> <strong>KO</strong><br />';
    txt+='Last Ping: '+dDate+'</font></td></tr></table>';
    return txt;
}


function genericToolTip(text)
{
    var txt ='<table border="0" style=" border-width:1px; border-style:solid; border-color:#5A99AE" cellspacing="0" cellpadding="2"><tr><td background="../Images/toolTip/bgToolTip.gif" class="testo" style="background-repeat:repeat-x;" align="left">';
    txt +=text+'</td></tr></table>'
    return txt;
}

/***   NATTING AUTOMATICO     ***/
 function AutomaticNatting( objTabella, sIndexOf, startNatting, Prefix )
 {


    var oForm = document.getElementById(objTabella);
    var sElementCheckAll = sElementCheckAll;
    if (oForm.getElementsByTagName("input").length != undefined) 
    {
        for(i=0; i<oForm.getElementsByTagName("input").length; i++)
         {
             if(oForm.getElementsByTagName("input")[i].type == "text"  && oForm.getElementsByTagName("input")[i].id.indexOf(sIndexOf)>-1)
                if(oForm.getElementsByTagName("input")[i].className=="textBox" && oForm.getElementsByTagName("input")[i].value=='')
                {
                    oForm.getElementsByTagName("input")[i].value = Prefix+startNatting;
                    startNatting++
                    fireHTMLEvent(oForm.getElementsByTagName("input")[i],'change') 
                }
         }
    } 
    else 
    {
         if(oForm.getElementsByTagName("input").type == "text"  && oForm.getElementsByTagName("input").id.indexOf(sIndexOf)>-1)
             if(!oForm.getElementsByTagName("input").readonly=="readonly" && oForm.getElementsByTagName("input").value=='')
                {
                    oForm.getElementsByTagName("input").value = Prefix+startNatting;
                    startNatting++
                }
    }
    
 }
 
 function encodeMyHtml(encodeHtml) {
     encodedHtml = escape(encodeHtml);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     return encodedHtml;
   } 
