

var popUpParamWin=0; 

var ajaxDebug=0;

var contaTappe=0;

var type='';
var ricerca="NO";
var oldLenS=1;


var numerosettorimax=4;
if (numeroportimax == null)
    var numeroportimax=4;
var numerosettoriventomax=2;


function popUpParamWindow(myIndex,theWidth,theHeight) { 



  var width = theWidth;

  var height = theHeight;

  var left = 100;

  var top = 100;

  theLink = "METEOMED_" + myIndex;





  if(popUpParamWin) {if(!popUpParamWin.closed) popUpParamWin.close(); } 

  //popUpParamWin = window.open(theLink, 'popUpParamWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbarsyes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+''); 

  popUpParamWin = window.open(theLink, 'popUpParamWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+''); 







}





function checkBrowser() {

  if (navigator.appName == "Microsoft Internet Explorer") {

    return "plugin/Player8.5_B2_AX_03-20.exe";

  } else {

    return "plugin/Player8.5_B2_PI_03-20.exe";

  }

}



function addItemToSelect(idSelect,values,txtValues,errId)

{



		document.getElementById(errId).innerHTML="";

		//document.getElementById('hidden_'+idSelect).value="";

		if (values.length==0) return; 

		if (document.getElementById(idSelect).options.length==0)

		{

			oOption = document.createElement("OPTION");

			document.getElementById(idSelect).options.add(oOption);

			oOption.text=txtValues;

			oOption.value=values;

			oOption.selected=true;

			//document.getElementById('hidden_'+idSelect).value=values+','+document.getElementById('hidden'+idSelect).value;

			return;

		}

		for(i=0; i<document.getElementById(idSelect).options.length;i++)

		{

			if (document.getElementById(idSelect).options[i].value==values)

			{

				document.getElementById(errId).innerHTML="Il valore e' gia' stato inserito!";

				return;

			}	

			else

			{

				oOption = document.createElement("OPTION");

				document.getElementById(idSelect).options.add(oOption);

				oOption.text=txtValues;

				oOption.value=values;

				oOption.selected=true;

				//document.getElementById('hidden_'+idSelect).value=values+','+document.getElementById('hidden'+idSelect).value;

				return;

			}	

	}

}



function removeSelectedItemFromSelect(idSelect)

{

	//document.getElementById('hidden_'+idSelect).value='';

	for(i=0; i<document.getElementById(idSelect).options.length;i++)

	{

		if(document.getElementById(idSelect).options[i].selected) document.getElementById(idSelect).remove(i);

	}

	for(i=0; i<document.getElementById(idSelect).options.length;i++)

	{

		document.getElementById(idSelect).options[i].selected=true;

		//document.getElementById('hidden_'+idSelect).value=document.getElementById(idSelect).options[i].value+','+document.getElementById('hidden_'+idSelect).value;

	}

}



function emailCheck(email,errId) 

{

	document.getElementById(errId).innerHTML="";

	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (!filter.test(email)) 

	{

		document.getElementById(errId).innerHTML="la e-mail "+email+" non e' valida!";

		return false;	

	}

	else

	{

		return true;

	}		

}



	

function validateAndAddEmail(idSelect,email,confirmEmail,errId)

{

	document.getElementById(errId).innerHTML="";

	if (!emailCheck(document.getElementById(email).value,errId)) return;

	

	if (document.getElementById(email).value=='') return;

	if (document.getElementById(confirmEmail)!=null && confirmEmail!='')

	{

		if(!emailCheck(document.getElementById(confirmEmail).value,errId)) return;

		if (document.getElementById(email).value==document.getElementById(confirmEmail).value) 

		{ 

			addItemToSelect(idSelect,document.getElementById(email).value,document.getElementById(email).value,errId); 

			document.getElementById(email).value=''; 

			document.getElementById(confirmEmail).value=''; 

		} 

		else 

		{ 

			document.getElementById(errId).innerHTML="Gli indirizzi e-mail devono essere uguali!";

			document.getElementById(email).value=''; 

			document.getElementById(confirmEmail).value=''; 

		}

	}

	else

	{ 

		addItemToSelect(idSelect,document.getElementById(email).value,document.getElementById(email).value,errId); 

		document.getElementById(email).value=''; 

	}

		

}



function showDiv(div) 

{

	if(document.getElementById(div).style.display=="none") document.getElementById(div).style.display="block";		

}



function hideDiv(div) 

{

	if(document.getElementById(div).style.display!="none") document.getElementById(div).style.display="none";		

}



function ajaxSendForm(form,divResult)

{

		var salSendForm = new salajax();

		salSendForm.Debug =ajaxDebug;

		salSendForm.EnableBackButton(true);  

		salSendForm.PresendHtml = '<img border="0" src="/images/others/loading.gif"/>';

		salSendForm.EvalScripts = true; 

		salSendForm.SetInnerHTMLFromAjaxResponse(form,divResult); 



}



function ajaxRequest(url,placeholder)

{

		var salGeneralAjax = new salajax();

		salGeneralAjax.Debug=ajaxDebug;

		salGeneralAjax.EnableBackButton(true);  

		salGeneralAjax.PresendHtml = '<img border="0" src="/images/others/loading.gif"/>';

		salGeneralAjax.EvalScripts = true; 

		salGeneralAjax.SetInnerHTMLFromAjaxResponse(url,placeholder); 

}



function ajaxRequestFunction(r)

{

	document.write(r); 

}



function ajaxRequestToFunction(url)

{

		var salGeneralAjax = new salajax();

		salGeneralAjax.Debug=ajaxDebug;

		salGeneralAjax.EnableBackButton(true);  

		salGeneralAjax.PresendHtml = '<img border="0" src="/images/others/loading.gif"/>';

		salGeneralAjax.EvalScripts = true; 

		salGeneralAjax.PassAjaxResponseToFunction(url,'ajaxRequestFunction'); 

}





function setSmsExtended(value,id)

{

		if(value=='1')

		{ 

			document.getElementById(id).style.display='block';

	 	} 

		else 

		{ 

		 	document.getElementById(id).style.display='none'; 

		}

}	



function _selectMacroSector(area,last)

{

	letter=new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","Y","Z");

	for (i=0;i<letter.length;i++)

	{

		if (document.getElementById(letter[i])!=null && document.getElementById(letter[i]).id==area)

		{

			document.getElementById(letter[i]).style.visibility='visible';

			document.getElementById(letter[i]).style.height='200px';

		}

		else

		{

			if (document.getElementById(letter[i])!=null) 

			{

				document.getElementById(letter[i]).style.visibility='hidden';

				document.getElementById(letter[i]).style.height='0px';

			}	

		}

		if (document.getElementById(letter[i])!=null && document.getElementById(letter[i]).id==last) return;

	}

}



function checkMobileTel(tel,idSelect,placeholder)

{

	ajaxRequest("METEOMED_registrazione_controllo_cellulare.php?CELL="+tel,placeholder);

		

	

}



function selectMacroSector(area,user,code,placeholder,segnala, conf)

{
	if (segnala=='segn') {
            ajaxRequest("METEOMED_mappa_macro_settore_"+area.toLowerCase()+".php?segnala=1&TIPO=sms&AREA="+area+"&USR="+user+"&CODE="+code+"&CONF="+conf,placeholder);
	}
	else {
            ajaxRequest("METEOMED_mappa_macro_settore_"+area.toLowerCase()+".php?TIPO=sms&AREA="+area+"&USR="+user+"&CODE="+code+"&CONF="+conf,placeholder);
	}
}





function selectSector(typeSelect,area,sector,placeholder, conf)
{

    if (typeSelect=='' && numerosettori<numerosettorimax)
    {

            var oldLen=1;

            numerosettori=numerosettori+1;

            var elencoSettori=document.getElementById('ELENCO_SETTORI');

            var elencoSettoriSelezionati=document.getElementById('ELENCO_SETTORI_SELEZIONATI');
            
            if (elencoSettoriSelezionati == null)
            	elencoSettoriSelezionati=document.getElementById('SETTORI_SELEZIONATI');

            for(i=0;i<elencoSettoriSelezionati.options.length;i++)

            {

                    if (elencoSettoriSelezionati[i].value==sector) return;

            }

            for(i=0;i<elencoSettori.options.length;i++)

            {

                    if (elencoSettori[i].value==sector)

                    {

                            var testata="<div  id='"+sector+"'>\n";

                            var corpo_table="\t<table width='255' style='color:#037AB5;' cellpadding='2'  cellspacing='0'>\n";

                            var corpo_tr="\t\t<tr><td width='36' valign='top' align='right' style='padding-right:3px;'>"+elencoSettori[i].value+"</td><td width='205'>" + elencoSettori[i].text + "</td><td width='14'><a href=\"javascript:removeSector('"+sector+"');\"><img src='images/ui/symbols/delete.gif' border='0' width='12' height='12' alt='elmina'></a></td></tr>";

                            var corpo_table_closed="\t</\table>\n";

                            var coda="</div>\n";

                            var settore=testata+corpo_table+corpo_tr+corpo_table_closed+coda;

                            if (document.getElementById(sector)!=null)

                            {

                                    document.getElementById(sector).innerHTML=corpo_table+corpo_tr+corpo_table_closed;

                            }

                            else

                            {

                            document.getElementById(placeholder).innerHTML=document.getElementById(placeholder).innerHTML + settore;

                            }

                            oldLen=elencoSettoriSelezionati.options.length;

                            elencoSettoriSelezionati.options[elencoSettoriSelezionati.options.length]=new Option(elencoSettori[i].value,elencoSettori[i].value,false,false);

                    }

            }

             if (document.getElementById('cfg_smsnecessari')!=null)
             {
                    if(!oldLen) oldLen=1;

                    if(!elencoSettoriSelezionati.options.length) eqse=1; else eqse=elencoSettoriSelezionati.options.length;

                    document.getElementById('cfg_smsnecessari').value=(parseInt(document.getElementById('cfg_smsnecessari').value)/oldLen)*eqse;
                     if (document.getElementById('sms_necessari'))document.getElementById('sms_necessari').innerHTML=document.getElementById('cfg_smsnecessari').value;
             }

     }

     else

    {

         if (document.getElementById(typeSelect)!=null) document.getElementById(typeSelect).value=sector;

         ajaxRequest("settori_macrosettore_selezionato.php?SETTORE="+sector,'txt_'+typeSelect);

        }



}

function saveSettoreToFavourites(value, codiceutente, codiceconfigurazione)
{
    ajaxRequest('settori_cerca_ajax.php?USR=' + codiceutente + '&codiceutente=' + codiceutente + '&CODE=' + codiceconfigurazione + '&TIPO=SALVA_CONF&COSA=SETTORE&VALORE='+value,'errSettore');
}

function aggiungiRimuoviSettore(obj)
{

    var oldLenS = numerosettori;
    if (oldLenS == 0) oldLenS = 1;

	if (obj.checked)
	{
		if (numerosettori<numerosettorimax)
			numerosettori=numerosettori+1;
		else
			obj.checked=false;
	}
	else
	{
		if (numerosettori>0)
		{
			numerosettori=numerosettori-1;
		}
	}
	
	if(numerosettori>0) eqse=numerosettori; else eqse=1;

	document.getElementById('cfg_smsnecessari').value=(parseInt(document.getElementById('cfg_smsnecessari').value)/oldLenS)*eqse;
	if (document.getElementById('sms_necessari')!=null)	document.getElementById('sms_necessari').innerHTML=document.getElementById('cfg_smsnecessari').value;
	oldLenS=eqse;
}

function setAllSelected(idSelect)

{

	if(document.getElementById(idSelect)!=null)

	{

		for(i=0;i<document.getElementById(idSelect).options.length;i++)

		{

			document.getElementById(idSelect).options[i].selected=true;

		}

	}

}


function removeSector(sector)
{

	var oldLen=1;
	if (numerosettori>0) numerosettori=numerosettori-1;
	var elencoSettoriSelezionati=document.getElementById('ELENCO_SETTORI_SELEZIONATI');
	if (document.getElementById('ELENCO_SETTORI_SELEZIONATI')!=null)
	{
		oldLen=elencoSettoriSelezionati.options.length;
	
		for(i=0;i<elencoSettoriSelezionati.options.length;i++)
	
		{
			if (elencoSettoriSelezionati[i].value==sector)
			{
				document.getElementById(sector).innerHTML='';
				elencoSettoriSelezionati.options[i]=null;
			}
		}
	}
	else
	{
				document.getElementById(sector).innerHTML='';
	}


	if (document.getElementById('cfg_smsnecessari')!=null)
	 {

			if(!oldLen) oldLen=1;
			
			if(!elencoSettoriSelezionati.options.length) eqse=1; else eqse=elencoSettoriSelezionati.options.length;
			
	 	document.getElementById('cfg_smsnecessari').value=(parseInt(document.getElementById('cfg_smsnecessari').value)/oldLen)*eqse;

		if (document.getElementById('sms_necessari')!=null)	document.getElementById('sms_necessari').innerHTML=document.getElementById('cfg_smsnecessari').value;

	}

}


function aggiungiRimuoviPorto(obj)
{
	var oldLenS = numeroporti;
	if (obj.checked)
	{
		if (numeroporti<numeroportimax) 
			numeroporti=numeroporti+1;
		else
			obj.checked=false;
	}
	else
	{
		if (numeroporti>0) 
		{
			numeroporti=numeroporti-1;
		}	
	}
	if(oldLenS==0) oldLenS=1;
	
	if(numeroporti>0) eqse=numeroporti; else eqse=1;
	document.getElementById('cfg_smsnecessari').value=(parseInt(document.getElementById('cfg_smsnecessari').value)/oldLenS)*eqse;
	if (document.getElementById('sms_necessari')!=null)	document.getElementById('sms_necessari').innerHTML=document.getElementById('cfg_smsnecessari').value;
	oldLenS=eqse;
}

//La funzione viene lanciata alla selezione di porti o settori per l'invio di sms o alert
function update_sms_alert_dove(obj, tipo, codiceutente, codiceconfigurazione, demo)
{
    document.getElementById('ELENCO').innerHTML='';
    //document.getElementById('cfg_smsnecessari').value = 1;

    if (tipo == 'alert')
        document.getElementById('alert_dove').value=obj.value; 

    var oldLenS = 0;
    if (obj.value == "PORTO")
    {
    	oldLenS = numerosettori;
        numeroporti = 0;
    }
	else if (obj.value == "SETTORE")
	{    
		oldLenS = numeroporti;
		numerosettori = 0;
	}
    
    eqse = 1;
    document.getElementById('cfg_smsnecessari').value=(parseInt(document.getElementById('cfg_smsnecessari').value)/oldLenS)*eqse;
	if (document.getElementById('sms_necessari')!=null)	document.getElementById('sms_necessari').innerHTML=document.getElementById('cfg_smsnecessari').value;
	//oldLenS=eqse;    
    
    document.getElementById('DETTAGLIO_ELENCO').innerHTML='';

    if (demo == "demo")
    	ajaxRequest('METEOMED_demo_ajax.php?TIPOAREA='+obj.value+'&TIPO=' + tipo + '&USR='+codiceutente+'&CODE='+codiceconfigurazione+')','ELENCO');
	else
    	ajaxRequest('METEOMED_configurazione_ajax.php?TIPOAREA='+obj.value+'&TIPO=' + tipo + '&USR='+codiceutente+'&CODE='+codiceconfigurazione+')','ELENCO');
	

}

function AggiungiTappaRotta(divId,tappa,idZonaPartenza,idPortoPartenza,idPartenzaEstero,idZonaArrivo,idPortoArrivo,idArrivoEstero,idTappaCorrente,velocita)

{

	var Intestazione="<tr align='center'><td>Partenza</td><td>Arrivo</td><td>Velocita'</td><td>&nbsp;</td></tr>";

	 

	var partenzaEstero=0;

	var arrivoEstero=0;

	

	var objZonaPartenza=document.getElementById(idZonaPartenza); 

	var objZonaArrivo=document.getElementById(idZonaArrivo); 

	var objPortoPartenza=document.getElementById(idPortoPartenza);

	var objPortoArrivo=document.getElementById(idPortoArrivo);

	var objPartenzaEstero=document.getElementById(idPartenzaEstero); 

	var objArrivoEstero=document.getElementById(idArrivoEstero);

	

	var descrizioneZonaPartenza=objZonaPartenza.options[objZonaPartenza.options.selectedIndex].text;

	var valoreZonaPartenza=objZonaPartenza.options[objZonaPartenza.options.selectedIndex].value;



	var descrizioneZonaArrivo=objZonaArrivo.options[objZonaArrivo.options.selectedIndex].text;

	var valoreZonaArrivo=objZonaArrivo.options[objZonaArrivo.options.selectedIndex].value;



	var descrizionePortoPartenza=objPortoPartenza.options[objPortoPartenza.options.selectedIndex].text;

	var valorePortoPartenza=objPortoPartenza.options[objPortoPartenza.options.selectedIndex].value;

		

	var descrizionePortoArrivo=objPortoArrivo.options[objPortoArrivo.options.selectedIndex].text;

	var valorePortoArrivo=objPortoArrivo.options[objPortoArrivo.options.selectedIndex].value;

	

	if(objPartenzaEstero.checked) 

		partenzaEstero='1';

	else

		partenzaEstero='0';

		

	if(objArrivoEstero.checked) 

		arrivoEstero='1';

	else

		arrivoEstero='0';

	

	var valoreTappa=tappa+"|"+valoreZonaPartenza+"|"+valorePortoPartenza+"|"+partenzaEstero+"|"+valoreZonaArrivo+"|"+valorePortoArrivo+"|"+arrivoEstero;

	

	if (tappa>0) Intestazione="";	

	

	Corpo="\n<tr align='center'><td><input type='hidden' id='tappa_"+tappa+"' name='TAPPE[]' value='"+valoreTappa+"'>"+descrizionePortoPartenza+" ("+descrizioneZonaPartenza+")</td><td>"+descrizionePortoArrivo+"("+descrizioneZonaArrivo+")</td><td>"+velocita+"</td><td><a href='#' onClick='document.getElementById(\""+tappa+"\").innerHTML=\"\"; return false;'><img src='' alt='Elimina' title='Elimina'></a></td></tr>\n";

	

	document.getElementById(divId).innerHTML += "<div id='"+tappa+"'><table align='center' border='0' width='90%' cellpadding='3' cellspacing='0'>\n"+Intestazione+Corpo+"\n</table></div>";



}

function isArray(obj) {
	   if (obj.constructor.toString().indexOf("Array") == -1)
	      return false;
	   else
	      return true;
}

function DisplayQuickButtons()
{
    if (document.getElementById('PORTO')!=null)
    {
        if (document.getElementById('PORTO').value!='')
            document.getElementById('trQuickShow').style.display='';
        else
            document.getElementById('trQuickShow').style.display='none';
    }
    else
        document.getElementById('trQuickShow').style.display='none';
}

function loadWindSectorImage(codiceutente, codiceconfigurazione, windSector)
{

        if (document.getElementById(windSector).value == "1")
        {
            document.getElementById(windSector).value = "0";
            numerosettorivento = numerosettorivento - 1;
        }
        else if (numerosettorivento < numerosettoriventomax)
        {
            document.getElementById(windSector).value = "1";
            numerosettorivento = numerosettorivento + 1;
        }

    
    var url = "&";
    url = url + "ormeggio_vento_nord=" + document.getElementById("ormeggio_vento_nord").value + "&";
    url = url + "ormeggio_vento_est=" + document.getElementById("ormeggio_vento_est").value + "&";
    url = url + "ormeggio_vento_sud=" + document.getElementById("ormeggio_vento_sud").value + "&";
    url = url + "ormeggio_vento_ovest=" + document.getElementById("ormeggio_vento_ovest").value + "&";

    ajaxRequest("METEOMED_settori_vento.php?USR="+codiceutente+"&CODE="+codiceconfigurazione+url, "SETTORI_VENTO");
}

function confirmPortDelete(portCode)
{
	//Se il porto corrente � associato a dei servizi chiedo la conferma prima di eliminarlo
	var answer = confirm("Questo porto risulta essere associato a dei servizi. Vuoi eliminarlo ugualmente?");
	
	if (answer)
	{
		document.getElementById('prt_codice').value=portCode; 
		document.getElementById('prt_operazione').value='DELETE'; 
		document.forms['porti'].submit(); 
	}
	return false;
}

function confirmSectorDelete(sectorCode)
{
	//Se il porto corrente � associato a dei servizi chiedo la conferma prima di eliminarlo
	var answer = confirm("Questo settore risulta essere associato a dei servizi. Vuoi eliminarlo ugualmente?");
	
	if (answer)
	{
		removeSector(sectorCode); 
		ajaxRequest('settori_cerca_ajax.php?TIPO=ELIMINA&VALORE='+sectorCode,'delSettore');  
	}
	return false;
}

function confirmRouteDelete(routeCode)
{
	//Se il porto corrente � associato a dei servizi chiedo la conferma prima di eliminarlo
	var answer = confirm("Questa rotta risulta essere associata a dei servizi. Vuoi eliminarla ugualmente?");
	
	if (answer)
	{
		document.getElementById('codicerotta').value=routeCode; 
		document.getElementById('elimina').value='DELETE'; 
		document.forms['rotte'].submit();
	}
	return false;
}