﻿/*	******************************************************************************************
	******************************************************************************************
	LIBRERIA FUNZIONI JS 
	******************************************************************************************
	****************************************************************************************** */

 // Utility function for converting degrees to radians
    Math.deg2rad = function ( x ) {
        return x * (Math.PI / 180.0); 
    }

    var EARTH_RADIUS = 6367;

    // Calculates the distance (in km's) between 2 points. 
    function distance_between_points ( p1, p2 ) {
        var a = Math.deg2rad( 90 - p1.y);
        var b = Math.deg2rad( 90 - p2.y);
        var theta = Math.deg2rad( p2.x - p1.x);
        var c = Math.acos( Math.cos(a) * Math.cos(b) + Math.sin(a) * Math.sin(b) * Math
        .cos(theta));

        return c * EARTH_RADIUS; 
    }



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 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/,""),true,true) 
        obj.dispatchEvent(evt); 
    } 
}

/***   SELEZIONA DESELEZIONA TUTTI I CHECKBOX DI UNA TABELLA     ***/
 function chkUnchkAllElements( objTabella, sElementCheckAll )
 {
    var oForm = objTabella;//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].checked = sElementCheckAll.checked;
         }
    } 
    else 
    {
         if(oForm.getElementsByTagName("input").type == "checkbox" && oForm.getElementsByTagName("input") != sElementCheckAll)
            oForm.getElementsByTagName("input").checked = sElementCheckAll.checked;
    }
    
 }
 
 function chkUnchkAllElementsByName( objTabella, sElementCheckAll, Name )
 {
    var oForm = objTabella;//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)
            if(oForm.getElementsByTagName("input")[i].id.indexOf(Name)>-1)
                oForm.getElementsByTagName("input")[i].checked = sElementCheckAll.checked;
         }
    } 
    else 
    {
         if(oForm.getElementsByTagName("input").type == "checkbox" && oForm.getElementsByTagName("input") != sElementCheckAll)
            if(oForm.getElementsByTagName("input").id.indexOf(Name)>-1)
            oForm.getElementsByTagName("input").checked = sElementCheckAll.checked;
    }
    
 }
 
 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,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();

	}




var undefined;
	
	// *************************************************************
	// *************************************************************
	// ******************  BROWSER SNIFFER *************************
	// *************************************************************
	// *************************************************************
		var exclude=1;
		var agt=navigator.userAgent.toLowerCase();
		var win=0;var mac=0;var lin=1;
		if(agt.indexOf('win')!=-1){win=1;lin=0;}
		if(agt.indexOf('mac')!=-1){mac=1;lin=0;}
		var lnx=0;if(lin){lnx=1;}
		var ice=0;
		var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;
		var op5=0;var op6=0;var op7=0;
		var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;
		if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){
			var thisKDE=agt;
			var splitKDE=thisKDE.split("konqueror/");
			var aKDE=splitKDE[1].split("; ");
			var KDEn=parseFloat(aKDE[0]);
			if(KDEn>=2.2){
				kde=1;
				ns6=1;
				exclude=0;
				}
			}
		else if(agt.indexOf('webtv')!=-1){exclude=1;}
		else if(typeof window.opera!="undefined"){
			exclude=0;
			if(/opera[\/ ][5]/.test(agt)){op5=1;}
			if(/opera[\/ ][6]/.test(agt)){op6=1;}
			if(/opera[\/ ][7-9]/.test(agt)){op7=1;}
			}
		else if(typeof document.all!="undefined"&&!kde){
			exclude=0;
			ie=1;
			if(typeof document.getElementById!="undefined"){
				ie5=1;
				if(agt.indexOf("msie 6")!=-1){
					ie6=1;
					dcm=document.compatMode;
					if(dcm!="BackCompat"){com=1;}
					}
				}
			else{ie4=1;}
			}
		else if(typeof document.getElementById!="undefined"){
			exclude=0;
			if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}
			else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}
			else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}
			if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}
			}
		else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){
			exclude=0;
			ns4=1;
			if(typeof navigator.mimeTypes['*']=="undefined"){
				exclude=1;
				ns4=0;
				}
			}
		if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}
		if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}
	
	
	// *************************************************************
	// *************************************************************
	// ******************  BROWSER SNIFFER *************************
	// *************************************************************
	// *************************************************************
	
	
	
	
	
	///Tooltip text
	function toolTipText(sStore, sAddress)
	{
	var text = '<strong>'+sStore+'</strong><br/>';
	text +=sAddress.split("[-]").join("<br/>");
	return text;
	}
	
	function genericToolTipText(sText)
    {
    
    var text =sText;
	return text;
   
    }
	
	
	function PDIToolTipText(sText)
    {
       var text = '';
       /*'<table width="5" class="toolTipPDI">';
	text +='<tr><td  height="35" align="center" valign="top" background="images/toolTip/pdi.gif"><table width="200" border="0" cellpadding="0" cellspacing="0">';
    text +='<tr><td align="left">'+sText+'</td></tr></table></td></tr></table>';*/
    text = sText;
    return text;
    }
    
function elementsShowHideObject(objSender, objShowHide)
 {
  var oObj = document.getElementById(objShowHide);
  if(objSender.src.indexOf("images/ico/ico_plus.gif")>0)
  {
    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";
  }

 }
