/**
 * @category  Application
 * @package   reisen.de
 * @author    Unister GmbH <teamleitung-dev@unister-gmbh.de>
 * @version   $Id: global.js 11430 2011-07-14 06:55:25Z a.staps $
 * @copyright Copyright (c) 2006-2008, Unister GmbH
 * @license   this code is property of company Unister GmbH and unterlays internal rules of company.
 * @link      http://www.reisen.de
 */

function popup(url, media, name) {
    var params = {};
    switch(media) {
        case "newsflash":
            params = {width: 600, height: 660};
            break;
        case "donation":
            params = {width: 600, height: 720};
            break;
        case "print":
            params = {width: 700, height: 480};
            break;
        case "info":
            params = {width: 360, height: 480};
            break;
        case "agb":
            params = {width: 550, height: 600, status: true};
            break;
        case "screen":
            params = {width: 800, height: 600, location:true, menubar: true, status: true, toolbar: true};
            break;
        case "flightinfo":
            params = {width: 600, height: 400};
            break;
        case "vaitin":
            params = {width: 620, height: 400};
            break;
        case "evaluation":
            params = {width: 660, height: 680};
            break;
        case "voucher":
            params = {width: 650, height: 550, scrollbars: false};
            break;
        case "travelvideo":
            params = {width: 486, height: 426, scrollbars: false, resizable: false};
            break;
        case "sponsors":
            params = {width: 833, height: 380, scrollbars: false};
            break;
        case "ervpayment":
            params = {width: 570, height: 580, scrollbars: false};
            break;
        case "itinerary":
            params = {width: 700, height: 510};
            break;
        case "tariffinfo":
            params = {width: 450, height: 600};
            break;
        case "lctariffinfo":
            params = {width: 800, height: 600};
            break;
        case "cvc":
            params = {width: 500, height: 400};
            break;
    }
    params.name = name || media;
    params.url  = url;
    jQuery.popup.create(params);
}

function resizeIframe(id , height){
    document.getElementById(id).height = height; // required for Moz bug, value can be "", null, or integer
    document.getElementById(id).height = window.frames[id].document.body.scrollHeight ;
}

function cumulateFees() {
    // new servicefee-handling
    if($('ibePrice[taxPrice]')) {
        var value = parseFloat ( serviceFee );
        if(!($('ibePrice[HaPrice]').innerHTML.match(/^0/))) {
            value = parseFloat ( parseFloat ( serviceFee ) - parseFloat( $('ibePrice[HaPrice]').innerHTML ) );
        }

        if( $('ibeBookingInput[erv][offerId]').getValue() == '2') {
            value = parseFloat ( value - parseFloat ( baseFee ) );
        }
        if( value <= parseFloat( 0 ) ) {
            value = parseFloat( 0 );
        }

        var specialService = ($('specialServicesTotalPrice') && parseFloat($('specialServicesTotalPrice').getValue()) > 0) ? $('specialServicesTotalPrice').getValue() : 0;
        var ervOfferPrice = (parseFloat($('ibePrice[ervOffer]').innerHTML) > 0) ? $('ibePrice[ervOffer]').innerHTML : 0;
        var fullPrice = parseFloat(travelPrice) + (parseFloat(value) * parseFloat(paxCount)) + parseFloat(ervOfferPrice) + parseFloat( $('ibeBookingInput[ha][price]').getValue() ) + parseFloat(specialService) + parseFloat( $('ibePrice[paymentFee]').innerHTML) - parseFloat( $('ibePrice[voucherPrice]').innerHTML);

        $('ibePrice[taxPrice]').update(value.toFixed(2).replace(/\./g, ",") + " &euro;");
        $('ibePrice[full]').update(fullPrice.toFixed(2).replace(/\./g, ",") + " &euro;");
    }
}

function flightErvTrueHandler (offerId,ervPrice,update) {
    $('ibeBookingInput[erv][offerId]').value = offerId;
    $('ibeBookingInput[erv][book]').value = "TRUE";
    $('insuranceOffTeaser').hide();

    if ( update == true ) {

        ervPrice = (typeof(ervPrice) == 'undefined') ? '0.00' :  parseFloat( ervPrice.toString().replace(/,/g, ".") );
        travelPrice = (typeof(travelPrice) == 'undefined') ? '0.00' : parseFloat ( travelPrice.toString().replace(/,/g, ".") );
        serviceFee = (typeof(serviceFee) == 'undefined') ? '0.00' : parseFloat( serviceFee.toString().replace(/,/g, ".") );
        baseFee = (typeof(baseFee) == 'undefined') ? '0.00' : parseFloat( baseFee.toString().replace(/,/g, ".") );

        if(typeof(ibeErvOffersTravelPrice1) == 'undefined') {
            ibeErvOffersTravelPrice1 = '0';
        }
        if(typeof(ibeErvOffersTravelPrice2) == 'undefined') {
            ibeErvOffersTravelPrice2 = '0';
        }
        if(typeof(ibeErvOffersTravelPrice3) == 'undefined') {
            ibeErvOffersTravelPrice3 = '0';
        }

        if ( offerId == '0' ) {
            if($('ibeErvOffersPriceTd1')) {
                $('ibeErvOffersPriceTd1').update("Inbegriffen");
            }
            tPrice2 = parseFloat ( ibeErvOffersTravelPrice2 ) - parseFloat ( ibeErvOffersTravelPrice1 );
            $('ibeErvOffersPriceTd2').update(addSign( tPrice2 ) + ",00 &euro;");
            tPrice3 = parseFloat ( ibeErvOffersTravelPrice3 ) - parseFloat ( ibeErvOffersTravelPrice1 );
            $('ibeErvOffersPriceTd3').update(addSign( tPrice3 ) + ",00 &euro;");
            $('ibeErvOffersPriceTd4').update('- ' + parseFloat ( ibeErvOffersTravelPrice1 ) + ",00 &euro;");
            $('ibePrice[ervOfferPrice]').update(parseFloat ( ibeErvOffersTravelPrice1 )+",00 &euro;");

            if($('ibePrice[taxPrice]')) {
                $('ibePrice[taxPrice]').update(parseFloat ( parseFloat ( serviceFee ) ).toFixed(2)+" &euro;");
                $('ibePrice[ervOffer]').update(parseFloat(ibeErvOffersTravelPrice1));
            }
        }
        else if( offerId == '1' ) {
            tPrice1 = parseFloat ( ibeErvOffersTravelPrice1 ) - parseFloat ( ibeErvOffersTravelPrice2 );
            if($('ibeErvOffersPriceTd1')) {
                $('ibeErvOffersPriceTd1').update(addSign( tPrice1 ) + ",00 &euro;");
            }
            $('ibeErvOffersPriceTd2').update("Inbegriffen");
            tPrice3 = parseFloat ( ibeErvOffersTravelPrice3 ) - parseFloat ( ibeErvOffersTravelPrice2 );
            $('ibeErvOffersPriceTd3').update(addSign( tPrice3 ) + ",00 &euro;");
            $('ibeErvOffersPriceTd4').update('- ' + parseFloat ( ibeErvOffersTravelPrice2 ) + ",00 &euro;");
            $('ibePrice[ervOfferPrice]').update(parseFloat ( ibeErvOffersTravelPrice2 ) +",00 &euro;");
            if($('ibePrice[taxPrice]')) {
                $('ibePrice[taxPrice]').update(parseFloat ( parseFloat ( serviceFee ) ).toFixed(2)+" &euro;");
                $('ibePrice[ervOffer]').update(parseFloat(ibeErvOffersTravelPrice2));
            }
        }
        else if( offerId == '2' ) {
            tPrice1 = parseFloat ( ibeErvOffersTravelPrice1 ) - parseFloat ( ibeErvOffersTravelPrice3 );
            if($('ibeErvOffersPriceTd1')) {
                $('ibeErvOffersPriceTd1').update(addSign( tPrice1 ) + ",00 &euro;");
            }
            tPrice2 = parseFloat ( ibeErvOffersTravelPrice2 ) - parseFloat ( ibeErvOffersTravelPrice3 );
            $('ibeErvOffersPriceTd2').update(addSign( tPrice2 ) + ",00 &euro;");
            $('ibeErvOffersPriceTd3').update("Inbegriffen");

            $('ibeErvOffersPriceTd4').update('- ' + parseFloat ( ibeErvOffersTravelPrice3 ) + ",00 &euro;");
            $('ibePrice[ervOfferPrice]').update(parseFloat ( ibeErvOffersTravelPrice3 )+",00 &euro;");
            $('ibePrice[taxPrice]').update(parseFloat ( parseFloat ( serviceFee ) - parseFloat ( baseFee ) ).toFixed(2)+" &euro;");
            $('ibePrice[ervOffer]').update(parseFloat(ibeErvOffersTravelPrice3));
        }
        cumulateFees();
        $('ervTeaser').style.visibility = '';
    }

    return;
}

function flightErvFalseHandler () {
    $('ibePrice[ervOfferPrice]').update("keine Versicherung ausgewählt");

    ervPrice = (typeof(ervPrice) == 'undefined') ? '0.00' :  parseFloat( ervPrice.toString().replace(/,/g, ".") );
    travelPrice = (typeof(travelPrice) == 'undefined') ? '0.00' : parseFloat ( travelPrice.toString().replace(/,/g, ".") );
    serviceFee = (typeof(serviceFee) == 'undefined') ? '0.00' : parseFloat( serviceFee.toString().replace(/,/g, ".") );
    baseFee = (typeof(baseFee) == 'undefined') ? '0.00' : parseFloat( baseFee.toString().replace(/,/g, ".") );

    if(typeof(ibeErvOffersTravelPrice1) == 'undefined') {
        ibeErvOffersTravelPrice1 = '0';
    }
    if(typeof(ibeErvOffersTravelPrice2) == 'undefined') {
        ibeErvOffersTravelPrice2 = '0';
    }
    if(typeof(ibeErvOffersTravelPrice3) == 'undefined') {
        ibeErvOffersTravelPrice3 = '0';
    }

    if($('ibePrice[taxPrice]')) {
        $('ibePrice[taxPrice]').update(parseFloat ( serviceFee ).toFixed(2) + " &euro;");
        $('ibePrice[ervOffer]').update(parseFloat(0));
    }

    if($('ibeErvOffersPriceTd1')) {
        $('ibeErvOffersPriceTd1').update("+ " + ibeErvOffersTravelPrice1 + ",00 &euro;");
    }
    $('ibeErvOffersPriceTd2').update("+ " + ibeErvOffersTravelPrice2 + ",00 &euro;");
    $('ibeErvOffersPriceTd3').update("+ " + ibeErvOffersTravelPrice3 + ",00 &euro;");
    $('ibeErvOffersPriceTd4').update("0,00 &euro;");

    $('ibeBookingInput[erv][offerId]').value = "";
    $('ibeBookingInput[erv][book]').value = "FALSE";

    $('insuranceOffTeaser').show();
    $('ervTeaser').style.visibility = 'hidden';

    cumulateFees();
}

function addSign( val ) {
    var retval = val;
    if( val > 0.00 || val > 0 ) {
        retval = '+ ' + val;
    } else if ( val < 0.00 || val < 0 ) {
        retval = '- ' + Math.abs( val );
    }
    return retval;
}

