//constants
var q1_open = 1;
var q2_open = 0;
var q3_open = 0;
var button_open = 0;
var answer_open = 0;

var loc = '';
var ind = '';
var elec ='';

window.addEvent('domready', function() {
    $('q_wrapper').set('tween', {
        duration: 400,
        transition: 'back:out',
        link: 'chain'
    });

    $('answertoquestion').set('fade', {
        duration: 1000,
        link: 'chain'
    });

    var wrapper_height = parseInt($('q_wrapper').getStyle('height'));
    var q1_height = parseInt($('question1').getStyle('height'));
    var q2_height = parseInt($('question2').getStyle('height'));
    var q3_height = parseInt($('question3').getStyle('height'));
    var button_height = parseInt($('button').getStyle('height'));

    //on load, make sure the 2nd and 3rd question box are hidden;
    $('question1').setStyle('visibility', 'visible');
    $('question2').setStyle('display', 'none');
    $('question2').fade('hide');
    $('question3').setStyle('display', 'none');
    $('question3').fade('hide');
    $('button').setStyle('display', 'none');
    $('button').fade('hide');
    $('answertoquestion').setStyle('display', 'none');
    $('content-form').setStyle('display', 'none');
    //$('answertoquestion').fade('hide');

    $$('.q1').each(function(el) {
        el.addEvent('click', function() {
            $$('.q2').each(function(el) {
                el.checked = false;
            });
            $$('.q3').each(function(el) {
                el.checked = false;
            });

            if (answer_open) {
                answer_open = 0;
                $('answertoquestion').setStyle('display', 'none');
                $('content-form').setStyle('display', 'none');
                //$('answertoquestion').fade('out');
            }

            loc = el.get('value');
            //close q3 if open
            if (q3_open != 0) {
                q3_open = 0;
                $('question3').fade('out');
                $('question3').setStyle('display', 'none');
                elec = '';
            }
            if (button_open != 0) {
                button_open = 0;
                $('button').fade('out');
                $('button').setStyle('display', 'none');
            }
            if (loc == 'southernoregon' || loc == 'astoria_hoodriver_easternoregon' || loc == 'centraloregon') {
                button_open = 1;
                if (q2_open != 0) {
                    q2_open = 0;
                    $('question2').fade('out');
                    $('question2').setStyle('display', 'none');
                }

                $('q_wrapper').tween('height', q1_height + button_height + 30);
                $('button').setStyle('display', 'block').fade('in');
            }
            else {
                q2_open = 1;

                $('q_wrapper').tween('height', q1_height + q2_height + 30);

                $('question2').setStyle('display', 'block').fade('in');
            }
        });
    });
    $$('.q2').each(function(el) {
        el.addEvent('click', function() {
            $$('.q3').each(function(el) {
                el.checked = false;
            });

            if (answer_open) {
                answer_open = 0;

                $('answertoquestion').setStyle('display', 'none');
                $('content-form').setStyle('display', 'none');
                //$('answertoquestion').fade('out');
            }


            ind = el.get('value');
            //only display 3rd question if q1 = portland and q2 = other
            if (loc == 'portland' && ind == 'other') {
                if (button_open != 0) {
                    button_open = 0;
                    $('button').fade('out');
                    $('q_wrapper').tween('height', q1_height + q2_height + q3_height + 40);
                    $('button').setStyle('display', 'none');
                }
                q3_open = 1;
                $('q_wrapper').tween('height', q1_height + q2_height + q3_height + 40);
                $('question3').setStyle('display', 'block').fade('in');
            }
            else {
                if (q3_open != 0) {
                    q3_open = 0;
                    elec = '';
                    $('question3').fade('out');
                    $('q_wrapper').tween('height', q1_height + q2_height + button_height + 40);
                    $('question3').setStyle('display', 'none');
                }
                button_open = 1;
                $('q_wrapper').tween('height', q1_height + q2_height + button_height + 40);
                $('button').setStyle('display', 'block').fade('in');
            }
        });
    });
    $$('.q3').each(function(el) {
        el.addEvent('click', function() {
            if (answer_open) {
                answer_open = 0;
                $('answertoquestion').fade('out');
                $('content-form').fade('out');
            }

            elec = el.get('value');
            button_open = 1;
            $('q_wrapper').tween('height', q1_height + q2_height + q3_height + button_height + 50);
            $('button').setStyle('display', 'block').fade('in');
        });
    });
});

var submitQ = function() {
    answer_open = 1;

    var disp_loc = '';
    var disp_ind = '';
    var disp_elec = '';
    var contact = '';
    var contact_array = new Array();
    contact_array[0] = 'Your Program Delivery Contractor (PDC) is Cascade Energy Engineering.  Contact them at 503.287.8488';
    contact_array[1] = 'Your Program Delivery Contractor (PDC) is Nexant.  Contact them at 503.219.2444';
    contact_array[2] = 'Your Program Delivery Contractor (PDC) is PGE Customer Technical Services.  Contact them at 503.464.7696';
    contact_array[3] = 'Your Program Delivery Contractor (PDC) is RHT Energy Solutions.  Contact them at 541.770.5884';

    //compose answer box's inner html
    //first switch also provides logic for selecting the pdc contact
    switch (loc) {
        case 'portland':
            disp_loc = 'Portland south to and including Salem';
            if (ind == 'food') {
                contact = contact_array[0];
            }
            else if (ind == 'pulp') {
                contact = contact_array[0];
            }
            else if (ind == 'other') {
                if (elec == 'pp') {
                    contact = contact_array[0];
                }
                else if (elec == 'pge') {
                    contact = contact_array[2];
                }
                else {
                    contact = 'There was an error with your electric utility selection.';
                }
            }
            else {
                contact = 'There was an error with your industry selection.';
            }
            break;
        case 'albany_corvalis':
            disp_loc = 'Albany Corvallis';
            if (ind == 'food') {
                contact = contact_array[0];
            }
            else if (ind == 'pulp') {
                contact = contact_array[3];
            }
            else if (ind == 'other') {
                contact = contact_array[3];
            }
            else {
                contact = 'There was an error with your industry selection.';
            }
            break;
        case 'southernoregon':
            disp_loc = 'Southern Oregon (Medford, Roseburg, Klamath Falls, Coos Bay)';
            contact = contact_array[3];
            break;
        case 'astoria_hoodriver_easternoregon':
            disp_loc = 'Astoria/Hood River/Eastern Oregon';
            contact = contact_array[0];
            break;
        case 'centraloregon':
            disp_loc = 'Central Oregon (Bend and nearby areas)';

            contact = contact_array[1];
            //			if (ind == 'food') {
            //				contact = contact_array[0];
            //			}
            //			else if (ind == 'pulp') {
            //				contact = contact_array[1];
            //			}
            //			else if (ind == 'other') {
            //				contact = contact_array[1];
            //			}
            //			else {
            //				contact = 'There was an error with your industry selection.';
            //			}
            break;
        default:
            disp_loc = '';
            break;
    };

    switch (ind) {
        case 'food':
            disp_ind = 'Food Processing';
            break;
        case 'pulp':
            disp_ind = 'Pulp and Paper';
            break;
        case 'other':
            disp_ind = 'Other (wood products, metals, etc.)';
            break;
        default:
            disp_ind = '';
            break;
    };

    switch (elec) {
        case 'pge':
            disp_elec = 'Portland General Electric';
            break;
        case 'pp':
            disp_elec = 'Pacific Power';
            break;
        default:
            disp_elec = '';
            break;
    };

    var answer_html = '<hr /><h3>Based upon your selections of:</h3> <ul><li>Location: ' + disp_loc + '</li>';
    if (loc !== 'southernoregon' && loc !== 'astoria_hoodriver_easternoregon' && loc !== 'centraloregon') answer_html += '<li>Industry: ' + disp_ind + '</li>';

    if (disp_elec != '') {
        answer_html += '<li>Electric Utility Company: ' + disp_elec + '</li></ul>';
    }
    else {
        answer_html += '</ul>';
    }

    answer_html += '<p><strong>' + contact + '.</strong></p>';

    $j('#ctl00_ctl00_MainContent_MainContent_hfPDC').val(contact);

    //make answer box visible
    $('answertoquestion').set('html', answer_html);
    $('answertoquestion').setStyle('display', 'block') //.fade('in');
    $('content-form').setStyle('display', 'block');
    return false;
};
