﻿	//============================================================================
	//-                   Copyright © 2006, 2009 Maxxcom Sas                     -
	//-                           All rights reserved.                           -
	//-                                                                          -
	//-    Questo codice sorgente è protetto da copyright in base alle leggi e   -
	//-    ai trattati internazionali. L'uso non autorizzato, la riproduzione    -
	//-    parziale o totale, l'alterazione, la distribuzione di questo codice   -
	//-    sorgente saranno perseguite in sede penale e civile in ottemperanza   -
	//-    alle leggi esistenti. Sono soggette alle rispettive licenze freeware, -
	//-    GPL, LGPL o di altro tipo, le parti di codice sorgente acquisite da   -
	//-    terze parte anche senza il consenso scritto. In tal caso i nomi degli -
	//-    autori sono riportati a monte delle procedure utilizzate e/o incluse. -
	//-    Eventuali omissioni o errori potranno essere comunicati all'indirizzo -
	//-    email info@maxxcom.it per una pronta correzione e/o integrazione.     -
	//-                                                                          -
	//-    www.maxxcom.it                                                        -
	//============================================================================

    window.DepFare = 0;
    window.DepSurcharge = 0;
    window.DepTaxes = 0;
    window.DepTaxesDesc = "";

    window.RetFare = 0;
    window.RetSurcharge = 0;
    window.RetTaxes = 0;
    window.RetTaxesDesc = "";

    SurchAmountD = new Array();
    SurchAmountR = new Array();
    SurchTextD = new Array();
    SurchTextR = new Array();
    TaxAmountD = new Array();
    TaxAmountR = new Array();
    TaxTextD = new Array();
    TaxTextR = new Array();

    //generazione di un evento click sul controllo
    window.fireClickEvent = function(oB) 
    {
	    if (oB.fireEvent)  //funziona in IE
	    {
		    var e = document.createEventObject();
		    oB.fireEvent('onclick', e);
	    }
	    else  //altri browser con DOM
	    {
		    var e = document.createEvent('MouseEvents');
		    e.initEvent('click', true, true);
		    oB.dispatchEvent(e);
	    }
    }

    window.onunload = function ()
    {
        //l'hanlder on-load inibisce la Back-Forward Cache su firefox
        //https://developer.mozilla.org/en/Using_Firefox_1.5_caching
    }

    window.GenerateClick = function()
    {

	    var radiosDep = document.getElementsByName("nmDepartures");
	    var radiosRet = document.getElementsByName("nmReturns");

 	    var buttonEnabled = false;

        checked = false;
        if (radiosDep.length > 0) 
        {
            for (i = 0 ; i < radiosDep.length ; i++)
            {
                if (radiosDep[i].checked == true)
                {
                    checked = true;
                    fireClickEvent(radiosDep[i]);
                }
            }
        }
        buttonEnabled = checked;
        
        if (!isRoundTrip) return buttonEnabled;

        checked = false;
		if (radiosRet.length > 0) 
		{
			for (i = 0 ; i < radiosRet.length ; i++)
			{
			    if (radiosRet[i].checked == true)
			    {
                    checked = true;
                    fireClickEvent(radiosRet[i]);
                }
	        }
		}
        buttonEnabled = checked;

        return buttonEnabled;

    }
     
    window.onload = function() 
    {    
        if (SubmitButton != null)
            SubmitButton.SetEnabled(GenerateClick());
    }

    window.ContinueStatus = function()
    {
	    var radiosDep = document.getElementsByName("nmDepartures");
	    var radiosRet = document.getElementsByName("nmReturns");

 	    var buttonEnabled = false;

        checked = false;
        if (radiosDep != null) 
        {
            if (radiosDep.length > 0) 
            {
                for (i = 0 ; i < radiosDep.length ; i++)
                {
                    if (radiosDep[i].checked == true)
                        checked = true;
                }
            }
        }
        buttonEnabled = checked;
        
        if (!isRoundTrip) return buttonEnabled;

        checked = false;
		if (radiosRet != null)
		{
			if (radiosRet.length > 0) 
			{
				for (i = 0 ; i < radiosRet.length ; i++)
				{
				    if (radiosRet[i].checked == true)
                        checked = true;
		        }
			}
		}
        buttonEnabled = checked;

        return buttonEnabled;
                
    }

    window.ValueField = function(name, amount)
    {
        var obj = getObject(name);
        if (obj != null) obj.innerHTML = FormatCurrency(Round(amount, 2));    
    }

    window.ZeroField = function(name)
    {
        var obj = getObject(name);
        if (obj != null) obj.innerHTML = FormatCurrency(0);    
    }

    window.showPrice = function() 
    {
	    var Total = getObject("txTotalPrice");
	    if (Total != null) {

            var popTxt = "<code>";

	        var totalpriced = window.DepFare;
	        //if (totalpriced > 0) {
                totalpriced += window.DepSurcharge + window.DepTaxes;
                popTxt = popTxt + oTexts.CostoAndata + FormatCurrency(Round(totalpriced, 2)) + "<br><br>";
	        //}

	        var totalpricer = window.RetFare;
	        //if (totalpricer > 0) {
	            totalpricer += window.RetSurcharge + window.RetTaxes;
                popTxt = popTxt + oTexts.CostoRitorno + FormatCurrency(Round(totalpricer, 2)) + "<br><br>";
	        //}
	        
	        var TotalFees = 0;
	        if (window.CardFeeAmount != null) 
	        {
	            TotalFees = window.CardFeeAmount;
                popTxt = popTxt + oTexts.Diritti + FormatCurrency(Round(TotalFees, 2)) + "<br><br>";
	        }
	            
	        totalprice = totalpriced + totalpricer + TotalFees;
	        var sTotalPrice = FormatCurrency(Round(totalprice, 2));
	        Total.innerHTML = sTotalPrice;

            popTxt = popTxt + oTexts.TotaleDovuto + sTotalPrice + "<br></code>";
            var popupControl = GetPopupControl();
            if (popupControl != null)
            {
                SetContentByName(popupControl, "SummaryPopup", popTxt);
            }
	        
            obj = getObject("msgtext");
            if (obj != null) obj.style.visibility = "visible";

            if (SubmitButton != null)
                SubmitButton.SetEnabled(ContinueStatus());        
        }
    };
    
    window.GetPopupControl = function() 
    {
        return InfoPopupControl;
    }

    window.SetContentByName = function(popup, name, text)
    {
        var popwin = popup.GetWindowByName(name);
        popup.SetWindowContentHtml(popwin, text);    
    }

    window.UpdateDep = function (fare, index, farebasis, gruppo, rules, surch, tax, surctxt, taxtxt) 
    { 
        window.DepFare = parseFloat(fare);
        window.DepSurcharge = parseFloat(surch);
        window.DepSurchargeD = surctxt;
        window.DepTaxes = parseFloat(tax);
        window.DepTaxesD = taxtxt;
        
        ValueField("txDepFares", window.DepFare);
        ValueField("txDepSurcharges", window.DepSurcharge);
        ValueField("txDepTaxes", window.DepTaxes);
        
        var popupControl = GetPopupControl();
        if (popupControl != null)
        {
            var result = "";
            if (rules != null) result = rules;
            
            SetContentByName(popupControl, "FareDepPopup", result);
            SetContentByName(popupControl, "SurchargesDepPopup", window.DepSurchargeD);
            SetContentByName(popupControl, "TaxDepPopup", window.DepTaxesD);
        }

	    var radios = document.getElementsByName("nmReturns");    
        if (radios.length > 0) 
        {
            for (i = 0 ; i < radios.length ; i++)
            {
                value = radios[i].value.substring(2, 4);
                if (value != gruppo)
                {
                    if (radios[i].checked)
                        radios[i].checked = false;
                    radios[i].disabled = true;
                }
                else
                    radios[i].disabled = false;
            }
        }
        showPrice();
        
    };

    window.UpdateRet = function(fare, index, farebasis, gruppo, rules, surch, tax, surctxt, taxtxt) 
    {
        window.RetFare = parseFloat(fare);
        window.RetSurcharge = parseFloat(surch);
        window.RetSurchargeD = surctxt;
        window.RetTaxes = parseFloat(tax);
        window.RetTaxesD = taxtxt;

        ValueField("txRetFares", window.RetFare);
        ValueField("txRetSurcharges", window.RetSurcharge);
        ValueField("txRetTaxes", window.RetTaxes);        
        
        var popupControl = GetPopupControl();
        if (popupControl != null)
        {
            var result = "";
            if (rules != null) result = rules;

            SetContentByName(popupControl, "FareRetPopup", result);
            SetContentByName(popupControl, "SurchargesRetPopup", window.RetSurchargeD);
            SetContentByName(popupControl, "TaxRetPopup", window.RetTaxesD);
        }
        showPrice();
    };

	window.checkRadios = function() 
	{
	
	    if (!SubmitButton.GetEnabled()) return false;
	
	    var radiosDep = document.getElementsByName("nmDepartures");
	    var radiosRet = document.getElementsByName("nmReturns");
		var selectedDep = false;
		var selectedRet = false;
		
        if (radiosDep != null) 
        {
            if (radiosDep.length > 0) 
            {
                for (i = 0 ; i < radiosDep.length ; i++)
                {
                    if (radiosDep[i].checked == true)
                        selectedDep = true;
                }
                if (!selectedDep) 
                {
                    alert( oTexts.ErrDeparture );
                    return false;
                }
            }
        }

		if (radiosRet != null) 
		{
			if (radiosRet.length > 0) 
			{
				for (i = 0 ; i < radiosRet.length ; i++)
				{
				    if (radiosRet[i].checked == true)
					    selectedRet = true; 
			    }
			    if (!selectedRet) 
			    {
				    alert( oTexts.ErrArrival );
				    return false;
			    }
			}
		}

		return true;
	}
	
	window.OpenInfantAlert = function(ctrl, index)
	{
        var popupControl = GetPopupControl();
        if (popupControl != null)
        {
            var rules = oTexts.InfantAlert;
            var popwin = popupControl.GetWindowByName("InfantPopup");
            if (popwin != null)
            {
                popupControl.SetWindowContentHtml(popwin, rules);
                popupControl.ShowWindowAtElementByID(popwin, ctrl);
            }
        }
	}

	window.CloseInfantAlert = function()
	{
        var popupControl = GetPopupControl();
        if (popupControl != null)
        {
            var popwin = popupControl.GetWindowByName("InfantPopup");
            if (popwin != null) popupControl.HideWindow(popwin);
        }
	}

    function OnInfoRulesClick(element, category) 
    {
        CallBackPopup.ShowAtElement(element);
        cpPopup.PerformCallback(category);
    }

    window.DepartureCallback = function (param) {
        gridDeparture.PerformCallback(param);
        ZeroField("txDepFares");
        ZeroField("txDepSurcharges");
        ZeroField("txDepTaxes");
        var radiosDep = document.getElementsByName("nmDepartures");
        for (i = 0; i < radiosDep.length; i++) radiosDep[i].checked = false;
        window.DepFare = 0;
        showPrice();
        return false;
    }
    window.EndCallbackDeparture = function () {
        $("#requestVersion").val(gridDeparture.cpchecksum);
    }
    window.EndCallbackReturn = function () {
         $("#requestVersion").val(gridReturn.cpchecksum);
    }

    window.ReturnCallback = function(param)
    {
        gridReturn.PerformCallback(param);
        ZeroField("txRetFares");
        ZeroField("txRetSurcharges");
        ZeroField("txRetTaxes");
	    var radiosRet = document.getElementsByName("nmReturns");
        for (i = 0 ; i < radiosRet.length ; i++) radiosRet[i].checked = false;
        window.RetFare = 0;
        showPrice();
        return false;
    }
