YAHOO.namespace('S2W.enroll');

YAHOO.S2W.enroll.facilitySelect = document.getElementById('facility');



YAHOO.S2W.enroll.init = function ()
{
    YAHOO.util.Event.addListener(document.body, 'load', YAHOO.S2W.enroll.getSelectData(YAHOO.S2W.enroll.facilitySelect, 'future_enrollment'));

    YAHOO.util.Event.addListener('facility', 'change', YAHOO.S2W.enroll.getFutureSessionsForFacility);
};



YAHOO.S2W.enroll.getFutureSessionsForFacility = function ()
{

    var facility_name = document.getElementById(this.id).value;

    var handleSuccess = function (o)
    {
        var data = [];
        try
        {
            data = YAHOO.lang.JSON.parse(o.responseText);
        }
        catch (x)
        {
            alert('JSON parse failed!');
            return;
        }

        YAHOO.S2W.enroll.insertSessionRow(data);
    };

    var handleFailure = function (o) { if (o.responseText !== undefined) { alert('failure'); } };

    var callback = { success : handleSuccess, failure : handleFailure, argument : {} };

    var sUrl = '/t/enroll/get_future_sessions';

    var postData = 'f=' + encodeURIComponent(facility_name);

    var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
};



YAHOO.S2W.enroll.insertSessionRow = function (data)
{
    var rows_container = document.getElementById('rows_container');
    rows_container.innerHTML = '';

    for (var i = 0, len = data.length; i < len; ++i)
    {
        var row = data[i];
        // get hosted_button_id
        // 0       - $    0/session
        // 4592404 - $ 0.01/session $  0.16/0.24
        // 4532595 - $ 2.50/session $ 40/ 60
        // 3965914 - $ 5.00/session $ 80/120
        // 5287269 - $ 6.00/session $ 96/144
		// 5703648 - $ 7.00/session $112/168
        // 5256784 - $ 8.00/session $128/192
        // 5287404 - $ 8.75/session $140/210
        // 5257186 - $ 9.00/session $144/216
        // 5287368 - $10.00/session $160/240
        // 5256840 - $11.00/session $176/264
        // 5287217 - $12.00/session $192/288

        // paypal production
        switch (row.session_price)
        {
            case "0":
                hosted_button_id = 0;
                break;

            case "0.01":
                hosted_button_id = 4592404;
                break;
                
            case "2.50":
                hosted_button_id = 4532595;
                break;
            
            case "5.00":
                hosted_button_id = 3965914;
                break;

            case "6.00":
                hosted_button_id = 5287269;
                break;

			case "7.00":
				hosted_button_id = 5703648;
				break;

            case "8.00":
                hosted_button_id = 5256784;
                break;

            case "8.75":
                hosted_button_id = 5287404;
                break;

            case "9.00":
                hosted_button_id = 5257186;
                break;

            case "10.00":
                hosted_button_id = 5287368;
                break;

            case "11.00":
                hosted_button_id = 5256840;
                break;

            case "12.00":
                hosted_button_id = 5287217;
                break;
                
            default:
                hosted_button_id = 3965914;                
        }

        switch (hosted_button_id)
        {
            case 0:
                rows_container.innerHTML += '<div id="row' + i + '" style="margin: 1em; line-height: 2em;">' +
                    '<form action="admintools/enroll_athlete_in_team2" method="post" target="_parent">' +
                    row.team_name + ' : ' + row.start_date + ' thru ' + row.end_date + '<br>' +
                    'Student Name: ' + '<select id="os2' + i + '" name="os2">' +
                    row.athlete_select + '</select>' +
                    '<input type="hidden" name="team_id" value="' + row.team_id + '">' +
                    '<input onclick="if (document.getElementById(\'os2' + i + '\')[document.getElementById(\'os2' + i + '\').selectedIndex].innerHTML == \'Choose Athlete\')' + 
                                    '{ alert(\'Please choose the name of the athlete you are paying for from the list. If the athlete you are looking for is not listed then they have either already enrolled, or have not registered on the site.\');' +
                                    'return false; } else { return true; }"id="payment_button-' + i + '" type="submit" value="Enroll Now"> - Free!' +
                    '</form>' + '<hr>' +
                    '</div>';
                break;

            default:
                rows_container.innerHTML += '<div id="row' + i + '" style="margin: 1em; line-height: 2em;">' +
                    '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_parent">' +
                    row.team_name + ' : ' + row.start_date + ' thru ' + row.end_date + '<br>' +
                    '<input name="cmd" value="_s-xclick" type="hidden">' +
                    '<input name="hosted_button_id" value="' + hosted_button_id + '" type="hidden">' +
                    '<input name="on0" value="length" type="hidden">' +
                    '<input name="os0" value="' + row.length + '" type="hidden">' +
                    '<input name="on1" value="for" type="hidden">' +
                    '<input name="os1" value="' + row.facility_name + '::' + row.team_name + '" type="hidden"> ' +
                    '<input type="hidden" name="on2" value="student">Student Name: ' + '<select id="os2' + i + '" name="os2">' +
                    row.athlete_select + '</select>' +
                    '<input name="currency_code" value="USD" type="hidden">' +
                    '<input onclick="if (document.getElementById(\'os2' + i + '\')[document.getElementById(\'os2' + i + '\').selectedIndex].innerHTML == \'Choose Athlete\')' + 
                                    '{ alert(\'Please choose the name of the athlete you are paying for from the list. If the athlete you are looking for is not listed then they have either already enrolled, or have not registered on the site.\');' +
                                    'return false; } else { return true; }" id="payment_button-' + i + '" src="http://www.paypal.com/en_US/i/btn/btn_buynow_SM.gif" name="submit" border="0" type="image">' +
                    '<img src="http://www.paypal.com/en_US/i/scr/pixel.gif" border="0" height="1" width="1">' +
                    ' $' + row.session_price + '/session' +
                    '</form>' + '<hr>' +
                    '</div>';
        }
    }
};



// getSelectData(object selectElement)
YAHOO.S2W.enroll.getSelectData = function (selectElement, filter)
{
    var handleSuccess = function (o)
    {
        if (o.responseXML !== undefined)
        {
            YAHOO.S2W.enroll.clearOptionsForSelect(selectElement);
            var names = o.responseXML.getElementsByTagName('name');

            for (var i = 0; i < names.length; i++)
            {
                YAHOO.S2W.enroll.createOptionForSelect(names[i].firstChild.nodeValue, selectElement);
            }
        }
    };

    var handleFailure = function (o) { if (o.responseXML !== undefined) { alert('failure'); } };

    var callback = { success : handleSuccess, failure : handleFailure, argument : {} };

    var sUrl = '/t/enroll/get_facility_list';

    var postData = 'f=' + encodeURIComponent(filter);

    var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
};



// clearOptionsForSelect(object select)
YAHOO.S2W.enroll.clearOptionsForSelect = function (select)
{
    while (select.hasChildNodes())
    {
        select.removeChild(select.firstChild);
    }
};



// createOptionForSelect(string value, object select)
YAHOO.S2W.enroll.createOptionForSelect = function (value, select)
{
    var newOption = document.createElement('option');
    newOption.value = value;
    newOption.appendChild(document.createTextNode(value));
    select.appendChild(newOption);
    if (this.debug) { YAHOO.log('select : option added: ' + select.id + ' : ' + value); }
};



// event handling
YAHOO.util.Event.addListener(window, 'load', YAHOO.S2W.enroll.init);
