﻿// File JScript

function HideTable(tableName)
{
    var tabella = document.getElementById(tableName);

    if (tabella.style.display == '')
    {
        tabella.style.display = 'none';
    }
    else
    {
	    tabella.style.display = '';
    }
}

function HideElement(elementName)
{
    var el = document.getElementById(elementName);

    if (el.style.display == '')
    {
        el.style.display = 'none';
    }
    else
    {
	    el.style.display = '';
    }
}

function ShowProgress()
{
    api.load();
    return true;;
}

function SetOptionsFields(newValue, country, airport)
{
    var country = document.getElementById(country);
    var airport = document.getElementById(airport);

    var v1 = newValue.value.substr(0, newValue.value.indexOf("-"));
    var v2 = newValue.value.substr(newValue.value.indexOf("-") + 1);

    country.value = v1;
    airport.value = v2;
}
        
function openPopUp(url,w,h)
{
    var lt = (screen.width - w) / 2;
	var tp = (screen.height - h) / 2;
	window.open(url,'','width='+w+',height='+h+',left='+lt+',top='+tp+',scrollbars=yes,resize=no');
}

function sommaData(start_day, number_nights, the_div)
{	  
    var days = start_day;

    var exp = days.split("-");
    var giorno = exp[0];
    var mese = exp[1];
    var anno = exp[2];

    var diff = parseInt(number_nights, 10);
    var part = new Date(anno, parseInt(mese,10)-1, giorno);  

    var currentDate = new Date();
    var currYear = currentDate.getFullYear() + 1;
    var currMonth = currentDate.getMonth();

    var sumDayToCheck = parseInt(giorno,10) + diff;
    
    if(mese == 12 && sumDayToCheck > 31) anno = currYear;

    var fine = part;
    fine.setDate(fine.getDate() + diff);   

    var prova = fine.toLocaleString();  
    var meseToDisplay = part.getMonth() + 1;
    
    if((meseToDisplay/10) < 1) meseToDisplay = '0' + meseToDisplay;
    
    var virgola = 0;
    virgola = prova.indexOf(",");
    
    if(virgola != -1)
    {  	      
        var tmp = prova.split(",");
        var uno = tmp[1];  	
        var tmp2 = uno.split(" ");

        var str = tmp2[2]+'-'+meseToDisplay+'-'+anno;
    }
    else
    {
        var tmp = prova.split(" "); 
        if((tmp[1] / 10) < 1) tmp[1] = '0' + tmp[1];

        var str = tmp[1] + '-' + meseToDisplay + '-' + anno;
        if(str.length > 10) str = str.substr(1, str.length);
    }

    document.getElementById(the_div).innerHTML = str;   
}

function fillcombo(thecombo, value)
{
	var dest = document.getElementById(thecombo);

	for (var i=dest.options.length-1; i>=1; i--)
	{
		dest.options[i] = null;
	}

	dest.selectedIndex = 0;

	if (eval("typeof("+value+")!='undefined'"))
	{
		var arrDest = eval(value);

		for (var i = 0; i < arrDest.length; i++)
		{
			dest.options[dest.options.length] = new Option(arrDest[i].value, arrDest[i].text);
		}
	}
	//else
	//{
	//    dest.options[0] = new Option("", "");
	//}	
}

function fillcomboPoi(poiRow, thecombo, value)
{
    fillcombo(thecombo, value);
    
    var poi_row = document.getElementById(poiRow);
    var dest = document.getElementById(thecombo);
    
    if (dest.options.length == 0 || (dest.options.length == 1 && dest.options[0].text == "Seleziona...")) 
        poi_row.style.display = "none";
    else
        poi_row.style.display = "";
}

function dataConvertiIn(data, tipo)
{
    var dd, mm, yyyy;
    if (tipo == "primaGG")
    {
        dd = data.substring(8, 10);
        mm = data.substring(5, 7);
        yyyy = data.substring(0, 4);
        return dd + "-" + mm + "-" + yyyy;
    }
    else if (tipo == "primaAA")
    {
        dd = data.substring(0, 2);
        mm = data.substring(3, 5);
        yyyy = data.substring(6, 10);
        return yyyy + "-" + mm + "-" + dd;
    }
}

function sommaData2(start_day, number_nights, the_div)
{	
	var str = sommaGiorni(start_day, number_nights);

    document.getElementById(the_div).value = str;
    
    $find('calExt2').set_selectedDate(Date.parseLocale(str, "dd-MM-yyyy"));
}

function sommaGiorni(data, number_nights)
{	
    var giorno = data.substring(0, 2);
    var mese = data.substring(3, 5);
    var anno = data.substring(6, 10);
  
    var d = new Date(mese + "/" + giorno + "/" + anno);
    d.setDate(d.getDate()+parseInt(number_nights,10));
    
    var giornoToDisplay = d.getDate();
    if((giornoToDisplay/10) < 1) giornoToDisplay = '0' + giornoToDisplay;
    
    var meseToDisplay = d.getMonth() + 1;
    if((meseToDisplay/10) < 1) meseToDisplay = '0' + meseToDisplay;
    
	var str = giornoToDisplay + '-' + meseToDisplay + '-' + d.getFullYear();
	
	return str;
}

function dataOggi()
{
    var data = new Date();
    var anno = data.getFullYear() + '';
    var mese = data.getMonth()+1 + '';
    var giorno = data.getDate() + '';
    if (mese.length == 1) mese = '0' + mese;
    if (giorno.length == 1) giorno = '0' + giorno;
    
    return (giorno + "-" + mese + "-" + anno);
}

function checkDate(data) {
    var myDayStr = data.substring(0, 2);
    var myMonthStr = data.substring(3, 5);
    var myYearStr = data.substring(6, 10);
    
    myMonthStr = parseInt(myMonthStr,10)-1;
    var myDate = new Date();
    myDate.setFullYear(myYearStr, myMonthStr, myDayStr );

    if ( myDate.getMonth() != myMonthStr )
      return false;
    else
      return true;
}

function selezionaCampo(obj, evidenzia)
{
    setTimeout(
        function () {
            obj.focus();
            if (evidenzia) obj.select();
        }, 50
    );
}
