function checkoutSubmitHover() {
    $$('.submit_order').each(function(elm) {
        elm.observe('mouseover',
        function() {
            this.src = '/img/site_specific/layout/place-my-order-hover.png';
        });

        elm.observe('mouseout',
        function() {
            this.src = '/img/site_specific/layout/place-my-order.png';
        });
    });
}

document.observe('dom:loaded', checkoutSubmitHover);

function payMethodToggle() {
    if ($('pay_online') && $('pay_other')) {
        $('pay_online').observe('click',
        function(e) {
            e.stop();

            if (!$(this).hasClassName('checked')) {
                $(this).addClassName('checked');
                $('pay_other').removeClassName('checked');
                $('pay_online_form_items').slideDown({
                    duration: 0.25
                });
                $('pay_other_form_items').slideUp({
                    duration: 0.25
                });
                $('CartPayOnlineValue').setValue(1);
                $('CartPayOtherValue').setValue(0);

                // hide/show totals
                $$('div.discount-info').invoke('show');
                $$('div.non-discount-info').invoke('hide');
            }
        });

        $('pay_other').observe('click',
        function(e) {
            e.stop();

            if (!$(this).hasClassName('checked')) {
                $(this).addClassName('checked');
                $('pay_online').removeClassName('checked');
                $('pay_online_form_items').slideUp({
                    duration: 0.25
                });
                $('pay_other_form_items').slideDown({
                    duration: 0.25
                });
                $('CartPayOnlineValue').setValue(0);
                $('CartPayOtherValue').setValue(1);

                // hide/show totals
                $$('div.discount-info').invoke('hide');
                $$('div.non-discount-info').invoke('show');
            }
        });
    }
}

document.observe('dom:loaded', payMethodToggle);

function newClassInstallmentDate(elem) {
    // Get parent ID
    elem = $(elem);
    var parent_id = elem.up(0).id;
    var inst_id = parent_id.replace('installment', '');

    // Get number of dates
    var kids = $$('#installment' + inst_id + ' div.dates div.date');
    lastChild = kids[kids.length - 1];
    var date_id = lastChild.id.replace('_wrapper', '').replace('installment' + inst_id + 'date', '');
    date_id++;

    new Ajax.Request('/cp/courses/displayInstallmentDate/' + inst_id + '/' + date_id, {
        parameters: '',
        onSuccess: function(response) {
            if (response.responseText) {
                $('installment' + inst_id + 'dates_wrapper').insert(response.responseText, 'bottom');
            }
        }
    });
}

function newClassInstallment(elem) {

    var kids = $$('#installments div.installment_wrapper');
    lastChild = kids[kids.length - 1];
    var inst_id = lastChild.id.replace('installment', '');
    inst_id++;
    date_id = 1;

    new Ajax.Request('/cp/courses/displayInstallment/' + inst_id + '/' + date_id, {
        parameters: '',
        onSuccess: function(response) {
            if (response.responseText) {
                $('installments').insert(response.responseText, 'bottom');
            }
        }
    });
}

function hideClassDates() {
    if ($('display_call')) {
        $('display_call').observe('click',
        function(event) {
            if ($('display_call').checked) {
                $('installments_hide_wrapper').fade({
                    duration: 0.1
                });
                $('CourseCost').previous('label').down('span.rc-required').hide();
                if ($('CourseCost').next('div.error-message')) {
                    $('CourseCost').next('div.error-message').hide();
                }
            }
            else {
                $('installments_hide_wrapper').appear({
                    duration: 0.2
                });
                $('CourseCost').previous('label').down('span.rc-required').show();
                if ($('CourseCost').next('div.error-message')) {
                    $('CourseCost').next('div.error-message').show();
                }
            }
        });

        if ($('display_call').checked) $('installments_hide_wrapper').hide();
    }
}
document.observe('dom:loaded', hideClassDates);

function hideStateFee() {
    if ($('show_state_fee')) {
        $('show_state_fee').observe('click',
        function(event) {
            if ($('show_state_fee').checked == false) $('state_fee_wrapper').fade({
                duration: 0.1
            });
            else $('state_fee_wrapper').appear({
                duration: 0.2
            });
        });

        if ($('show_state_fee').checked == false) $('state_fee_wrapper').hide();
    }
}
document.observe('dom:loaded', hideStateFee);

function watchNoticeMessage() {
    if ($('notice')) {
        setTimeout('dismissFlashMessage()', 5000);
    }
}

// dismisses the actual flash message
function dismissFlashMessage() {
    new Effect.BlindUp('notice', {
        duration: 0.5
    });
}

document.observe('dom:loaded', watchNoticeMessage);

function getSessions() {
    if ($('CartInstallmentId')) {
        $('CartInstallmentId').observe('change',
        function(event) {
            new Ajax.Request('/courses/getSessions/' + $('CartInstallmentId').value, {
                parameters: '',
                onSuccess: function(response) {
                    $('sessions').replace(response.responseText);
                }
            });
        });
    }
}
document.observe('dom:loaded', getSessions);

function buttonHovers() {
    if ($('save_changes')) {
        $('save_changes').observe('mouseover',
        function(event) {
            $('save_changes').src = '/img/site_specific/layout/btn-save-changes-hover.png';
        });

        $('save_changes').observe('mouseout',
        function(event) {
            $('save_changes').src = '/img/site_specific/layout/btn-save-changes.png';
        });
    }

    if ($('btn_login')) {
        $('btn_login').observe('mouseover',
        function(event) {
            $('btn_login').src = '/img/site_specific/layout/login-hover.png';
        });

        $('btn_login').observe('mouseout',
        function(event) {
            $('btn_login').src = '/img/site_specific/layout/login.png';
        });
    }

    if ($('btn_send')) {
        $('btn_send').observe('mouseover',
        function(event) {
            $('btn_send').src = '/img/site_specific/layout/btn-send-hover.png';
        });

        $('btn_send').observe('mouseout',
        function(event) {
            $('btn_send').src = '/img/site_specific/layout/btn-send.png';
        });
    }

    if ($('btn_cart_add')) {
        $('btn_cart_add').observe('mouseover',
        function(event) {
            $('btn_cart_add').src = '/img/site_specific/layout/add-to-cart-hover.png';
        });

        $('btn_cart_add').observe('mouseout',
        function(event) {
            $('btn_cart_add').src = '/img/site_specific/layout/add-to-cart.png';
        });
    }

    if ($('btn_confirm_remove')) {
        $('btn_confirm_remove').observe('mouseover',
        function(event) {
            $('btn_confirm_remove').src = '/img/site_specific/layout/confirm-remove-all-hover.png';
        });

        $('btn_confirm_remove').observe('mouseout',
        function(event) {
            $('btn_confirm_remove').src = '/img/site_specific/layout/confirm-remove-all.png';
        });
    }
}
document.observe('dom:loaded', buttonHovers);

function addCourseListener() {
    if ($('add_class')) {
        $('add_class').observe('click',
        function() {
            switch ($('add_class').getValue()) {
            case 'seattle':
                window.location = 'http://www.rgatraining.com/class-schedules/seattle-classes';
                break;
            case 'emeryville':
                window.location = 'http://www.rgatraining.com/class-schedules/emeryville-classes';
                break;
            }
        });
    }
}
document.observe('dom:loaded', addCourseListener);

function cvvDetailPopup() {
    if ($('cvv-detail')) {
        $('cvv-detail').observe('click',
        function(e) {
            e.stop();
            var width = 579;
            var height = 233;
            var left = (screen.width / 2) - (width / 2);
            var top = (screen.height / 2) - (height / 2);
            window.open(this.href, 'cvv_detail', 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
        });
    }
}
document.observe('dom:loaded', cvvDetailPopup);

function remoteLinksListener() {
    var anchors = $$('a');
    x = anchors.length;
    var internal = true;
    for (var i = 0; i <= x; i++) {
        internal = true;
        if (anchors[i]) {
            file_type = anchors[i].href.substr(anchors[i].href.lastIndexOf(".") + 1);
            if ((anchors[i].href.match('rgatraining.com') || anchors[i].href.match('rga.rocket-dev.com') || anchors[i].href.match('javascript')) && (file_type != 'pdf')) {
                internal = false;
            } else if (file_type == 'pdf') {
                internal = true;
            }
            if (internal == true && anchors[i].href != '') {
                anchors[i].target = "_blank";
            }
        }
    }
}

document.observe('dom:loaded', remoteLinksListener);

function countrySelectListener() {
	if ($('UserCountryId')) {
		$('UserCountryId').observe('change', function() {
			if (this.options[this.selectedIndex].text.toLowerCase() == 'canada') {
				$('UserStateId').up().hide();
				$('UserProvinceId').up().show();
				$('UserZip').previous('label').down('span.postal-term').innerHTML = 'Postal';
			}
			else {
				$('UserStateId').up().show();
				$('UserProvinceId').up().hide();
				$('UserZip').previous('label').down('span.postal-term').innerHTML = 'Zip';
			}
		});
	}
	
	if ($('CartBillingCountry')) {
		$('CartBillingCountry').observe('change', function() {
			if (this.options[this.selectedIndex].text.toLowerCase() == 'canada') {
				$('CartBillingState').up().hide();
				$('CartBillingProvince').up().show();
				$('CartBillingZip').previous('label').down('span.postal-term').innerHTML = 'Postal';
			}
			else {
				$('CartBillingState').up().show();
				$('CartBillingProvince').up().hide();
				$('CartBillingZip').previous('label').down('span.postal-term').innerHTML = 'Zip';
			}
		});
	}
}

document.observe('dom:loaded', countrySelectListener);
