var POUNDS = '&#xa3;';


if (window.console === undefined) {
    window.console = { log: function() { } };
}

function enrol_select_cc () {
    if (course_options[selected_package].frequencies)
    {
        window.location = createDetailsLink (selected_package, selected_region, 'cc', selected_freq);
    }
    else
    {
        window.location = createDetailsLink (selected_package, selected_region, 'cc');
    }
}

function enrol_select_paypal () {
    if (course_options[selected_package].frequencies)
    {
        window.location = createDetailsLink (selected_package, selected_region, 'paypal', selected_freq);
    }
    else
    {
        window.location = createDetailsLink (selected_package, selected_region, 'paypal');
    }
}

function enrol_show_summary(id) {
    var region,freq,type,price;
    var items = id.split('Z');
    region=items[0];
    type=items[1];
    freq=items[2];
    price=items[3];
    enrol_write_summary(region, type, freq, price);
    selected_freq = freq;
    selected_package = type;
    $('#pagesummary').show();
    window.location = '#summary';
}

function enrol_write_summary(region, type, freq, pidx) {
    var price = get_price(region, type, freq, pidx);
    var ctr = $('div#pagesummary div.options');
    str = '';
    if (price.paypal) {
        str += '<a style="color: blue;" id="paypal_href" href="javascript:enrol_select_paypal()"><img src="/images/paybypaypal.gif" alt="" border="0" rel="Pay by PayPal" id="opt3link" /></a>';
    } else {
        str += '<a id="cc_href" href="javascript:enrol_select_cc()"><img src="images/paybycreditcard.gif" width="189" height="29" alt="" border="0" rel="Pay By Credit Card" id="opt1link" /></a>';
    }
    $('#optboxbutton a').remove();
    $('#optboxbutton').append(str);
    $('span#payment1').html( get_price_string_deposit(region, type, freq, pidx) );
    var eltId = 'h3#' + [region,type].join('Z');
    var elt = $(eltId).clone() // Find appropriate h3 tag (eg 'Join the order and receive...') and clone it.
    elt[0].id = '';
    elt.children('a').remove(); // remove the MORE INFORMATION link
    $('#optbox1').children('h3').remove();
    elt.prependTo('#optbox1'); // Prepend headline to summary box
}


function get_price(region, type, freq, price) {
    var priceroot = packages[type].regions[region];
    if (priceroot.frequencies && priceroot.frequencies[freq]) {
        priceroot = priceroot.frequencies[freq];
    }
    if (!price) { price = 'default'; }
    price = priceroot.prices[price];
    return price;
}

function get_price_string(region, type, freq, price,specialfreq) {
    price = get_price(region, type, freq, price);
    var dur = '';
    switch(freq) {
        case 'monthly':
            var n = '12';
            if (price.numPayments) n = price.numPayments;
            dur = ' for ' + n + ' months';
            break;
        case 'ntimes':
            if (price.description) {
                dur = price.description;
            }
            else if (price.numPayments) {
                var n = price.numPayments;
                dur = 'in ' + n + ' installments';
            }
            break;
        case 'quarterly':
            if (price.description) {
                dur = price.description;
            }   
            else if (price.numPayments) {
                var n = price.numPayments;
                dur = 'in ' + n + ' installments';
            } else {
                dur = 'quarterly';
            }
            break;
        default:
            break;
    }
    if (price.paypal) {
        dur += ' through PayPal';
    }
    var s = POUNDS 
        + price.value
        + ' ' + dur;
    return s; 
}

function get_price_string_deposit(region, type, freq, pidx) {
    var priceroot = packages[type].regions[region];
    if (priceroot.frequencies && priceroot.frequencies[freq]) {
        priceroot = priceroot.frequencies[freq];
    }
    var s = '';
    if ('deposit' in priceroot.prices) {
        var dur = (freq == 'monthly') ? '12 months' : 'quarterly';
        var id = [region, type, freq, 'default'].join('Z');
        s = POUNDS;
        s +=  priceroot.prices.deposit.value;
        s +=  ' deposit and ' ;
        //s +=  POUNDS ;
        //s +=  priceroot.prices['default'].value;
        //s +=  ' ' +  freq +  ' for ' +  dur;
        s += get_price_string(region, type, freq, 'default', false);
    } else {
        var price = priceroot.prices[pidx];
        s = get_price_string(region, type, freq, pidx, false);
    }
    return s;
}

/** Write the prices table for the given frequency and type, appending to _div_ */
function enrol_show_prices(region, type, freq, div) {
    var priceroot = packages[type].regions[region];
    if (priceroot.frequencies && priceroot.frequencies[freq]) {
        priceroot = priceroot.frequencies[freq];
    }
    var str = '';
    if ('deposit' in priceroot.prices) {
        var dur = (freq == 'monthly') ? '12 months' : 'quarterly';
        var id = [region, type, freq, 'default'].join('Z');
        var s = POUNDS;
        s +=  priceroot.prices.deposit.value;
        s +=  ' deposit and ' ;
        //s +=  POUNDS ;
        //s +=  priceroot.prices['default'].value;
        //s +=  ' ' +  freq +  ' for ' +  dur;
        s += get_price_string(region, type, freq, 'default');
        var onclick = ' onclick="enrol_show_summary(this.id);" ';
        str += '<label><input type="radio" id="' + id + '" ' + onclick + ' name="pkg" />' + s + '</label>';
    } else {
        for (var pidx in priceroot.prices) {
            var price = priceroot.prices[pidx];
            var s = get_price_string_deposit(region, type, freq, pidx);
            //var s = get_price_string(region, type, freq, pidx);
            var id = [region, type, freq, pidx].join('Z');
            var onclick = ' onclick="enrol_show_summary(this.id);" ';
            str += '<li><label><input id="' + id + '" ' + onclick + ' type="radio" name="pkg" />' + s + '</label></li>';
        }
    }
    div.append(str);
}

function enrol_write_regions(regions, ctr) {
    var images = {
        'GB' : 'uk.jpg',
        'EU' : 'eu.jpg',
        'ROW' :'globe.jpg'
    };
    var ul = document.createElement('ul');
    for (var ridx in regions) {
        var region = regions[ridx];
        var li = document.createElement('li');
        var str =  '<input type="radio" name="reg" id="reg_' + ridx;
        str +=  '" onclick="enrol_show_packages(\'' + ridx + '\');" />';
        $(li).append(str);
        str =  '<img src="images/' + images[ridx];
        str +=  '" alt="" height="17" width="25" />';
        $(li).append(str);
        str =  getRegionDesc(ridx);
        li.appendChild(document.createTextNode(str));
        ul.appendChild(li);
    }
    ctr.append(ul);
    $('#pageregion').show();
}

function ob_keys(ob) {
    var out = [];
    for(var k in ob) {
        out.push(k);
    }
    return out;
}

function enrol_show_packages(ridx) {
    selected_options.region = ridx;
    selected_region = ridx;
    var div = $('#pageversion div.options');
    div.empty();

    var images = {
        'text': 'text.jpg',
        'audio' : 'audio.jpg',
        'text_audio' : 'textaudio.jpg'
    };
       
    for (var tidx in course_options) {
        var type = packages[tidx];
        var desc = type.description;
        var region = type.regions[ridx];
        if (!region) {
            continue;
        }
        var id = [ridx,tidx].join('Z');
        var img = '<img src="/images/' + images[tidx] + '" />';
        div.append('<h3 id="' + id+ '"'
                + ' class="package">' +
                img + desc
                + "<a href=\"javascript:pop_infowin('" + tidx + "')\">"
                + '<img width="100" height="13" border="0" class="moreinfo" alt="" src="images/bn-moreinfo.gif" />'
                + '</a></h3>'
                );
        var ul = $(document.createElement('ul'));
        if (ob_keys(region.frequencies).length == 0) {
            enrol_show_prices(ridx, tidx, 'once', ul);
        } else {
            for (var fidx in region.frequencies) {
                enrol_show_prices(ridx, tidx, fidx, ul);
            }
        }
        div.append(ul);
        div.append('<hr />');
    }
    $('hr:last', div).remove();
    $('#pageversion').show();
}

function init()
{
    if (!packages || !packages.text.regions) {
        alert("not looaded!");
    }
    var ctr = $('#pageregion div.options');
    enrol_write_regions(packages.text.regions, ctr);
    return;

    // Activate controls for selected options
    if (selected_options.region)
    {
        var regid = 'reg_' + selected_options.region;
        var elt = getByID (regid);
        elt.checked = true;
        $('#reg_' + selected_options.region).click();

        var pidx = 'default';
        if (selected_options.method == 'paypal' ) {
            pidx = 'paypal';
        }
        var p = get_price(selected_options.region, selected_options.package, selected_options.frequencies, pidx);
        $('#' + selected_options.region + '-' + selected_options.package + '-' + selected_options.frequency + '-' + pidx).click();
    }
}


/**
 * This file contains the set of enrolment functions required to make the
 * JavaScript enrolment page work.
 *
 * Alastair Pharo <alastair@appleservices.co.nz>
 * 2006-03-28
 */

/**
 * These remember what is currently selected.
 */
var selected_package = '';
var selected_region = '';
var selected_freq = '';
/**
 * This sets up the document to call the function, but only after the DOM is
 * loaded.
 */
$(document).ready(init);

