/**
 *
 *
 *
 *
 */
function CheckPaypal() {
    var error=false;
    var error_message='';

    if(document.memberarea_paypal_review.memberarea_terms.checked==false) {
        error=true;
        error_message+="Bitte akzeptiere unsere Nutzungsbedingungen.\n";
    }

    if(document.memberarea_paypal_review.memberarea_cancellation.checked==false) {
        error=true;
        error_message+="Bitte akzeptiere unsere Widerrufsbelehrung.\n";
    }


    if(error) {
        alert(error_message);
        return false;
    }
    else {
        document.memberarea_paypal_review.submit();
    }
}


/**
 *
 *
 *
 *
 */
function OpenBrWindow(theURL,winName,w,h,features) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    window.open(theURL,winName,'height='+h+',width='+w+',top='+wint+',left='+winl+','+features);
}


/**
 *
 *
 *
 *
 */
function open_byteFMPlayer() {
    o_window = window.open('http://www.byte.fm/player/player.html','fenster','location=no,menubar=no,toolbar=no,status=no,resizable=no,scrollbars=no,width=220,height=500');
    o_window.moveTo(0,0);
    o_window.focus();
}


/**
 *
 *
 *
 *
 */
function open_byteFM_ListenAgainPlayer(memberarea_products_id) {
    o_window = window.open('/player/listen_again_player.php?id='+memberarea_products_id,'fenster','height=500,width=220,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
    o_window.moveTo(0,0);
    o_window.focus();
}

/**
 *Filter for the programlist. Handles the Filter for the Programlist that filters the names of the programs listed in the table.
 *@param filter gives the specific td class that is used to apply the filter.
 */
function filter_products_programlist(filter){
    /**
     *style filter elements. Remove the selection class from every Filter and apply it to the new selected Filter.
     */
    $$('.filter_programlist').each(function(filter_programlist) {
        filter_programlist.removeClassName('selected_filter');
    });
    $('filter_'+filter).addClassName('selected_filter');


    /**
     *filter the cols of the table. iterate over every col and apply the selected filter.
     */
    $$('#table_product_programlist tr').each(function(table_programlist_td) {
        if(!table_programlist_td.hasClassName('programlist_th')){
            if(table_programlist_td.hasClassName(filter) && !table_programlist_td.hasClassName('single_program_hide')){
                table_programlist_td.show();
            }else
            {
                table_programlist_td.hide();
            }
        }
    });

}

/**
 *Handler for the shopping cart. Handles the add/remove requests for the shopping cart.
 *@param sendung_id id of the checked show
 *@param handler_var controls remove or add action
 *@param col_id id of the selected column
 *@param target controls destination table: programlist or shoppingcart
 */
function warenkorb_handler(handler_var, col_id, target){
    var selected_col = $(col_id).childElements(); /**< select the tds of the col. */

    if(target=='programlist'){
        var produkt_id = $F(selected_col[2].down('select'));
    }else
    if(target=='shoppingcart'){
        var produkt_id = $F(selected_col[0].down('input'));
    }

    new Ajax.Request('/php/ajax/memberarea_warenkorb_handler.php', {
        method: 'post',
        parameters:{
            handler: handler_var,
            id_produkt: produkt_id,
            site: target
            },
        onSuccess: function(transport){
            if(target=='programlist'){
                $('div_table_product_programlist').innerHTML = transport.responseText;
                var element_id = $$('.selected_filter')[0].identify();
                filter_products_programlist(element_id.substring(7,10));
            }else
            if(target=='shoppingcart'){
                $('div_table_shoppingcart').innerHTML = transport.responseText;
            }
        }
    });
}


/**
 *validator. Validates the shopping cart when you click on send.
 *@param type. Validation type, can be 'abonnement' or 'einzelsendung'
 */
function validate_shoppingcart(type){
    var selected_abonnement = '';
    if(type=='abonnement'){
        selected_abonnement = Form.getInputs('form_shoppingcart','radio','select_abonnement').find(function(radio) { return radio.checked; }).value;
    }
    $('shoppingcart_error_message_user').hide();
    $('shoppingcart_error_message_product').hide();
    new Ajax.Request('/php/ajax/validate_shoppingcart.php', {
        method: 'post',
        parameters:{
            type:type,
            abo:selected_abonnement
        },
        onSuccess: function(transport){
            var response = transport.responseText.evalJSON();
            var valid = true;
            if(!response.user){
                $('shoppingcart_error_message_user').show();
                valid = false;
            }
            if(!response.shoppingcart){
                $('shoppingcart_error_message_product').show();
                valid = false;
            }

            if(valid){
                $('form_shoppingcart').submit();
             }
        }
    });
}


/**
 * Display a tooltip for a given sendung.
 * Hier hat man nur die id der Sendung, ermittelt welches Datum die
 * Drop-Down-Box anzeigt und gibt dann den Wert des zugehörigen versteckten
 * Input-Feldes zurück.
 */
function getDescriptionText(id_sendung) {
    var product_id = $F($('selSendetermin_' + id_sendung));
    return unescape($('description_text_' + product_id).value);
}

/**
 * Display a tooltip for a given sendung.
 * Hier hat man die ProductID und kann so direkt auf das zugehörige versteckte
 * Input-Feld zugreifen.
 */
function getDescriptionTextByProductID(product_id) {
    return unescape($('description_text_' + product_id).value);
}

