﻿
var activeForm;
document.observe("dom:loaded", function() {
    activeForm = $('donation_form');

    $$('input.required', 'select.required').each(function(input) { 
        input.observe('blur', function() {
            ValidateRequired(this);
        } .bind(input));
    });
    $$('input[name=is_anonymous]').each(function(input) {
        input.observe('click', function() {
            ToggleAnonymous(this);
        } .bind(input));
    });
    $$('input[name=shipping_same]').each(function(input) {
        input.observe('click', function() {
            ToggleShipping(this);
        } .bind(input));
    });
});

function ValidateRequired(input) {
    if (input.selectedIndex == 0 && input.value == '' && !input.ancestors().any(function(e) { return e.visible() == false; })) {
        $(input).addClassName('invalid');
        return false;
    } else if (input.value == "" && !input.ancestors().any(function(e) { return e.visible() == false; })) {
        $(input).addClassName('invalid');
        return false;
    }$(input).removeClassName('invalid');
    return true;
}

function ToggleAnonymous(input) {
    if (input.checked) {        
        $('donor_wrap').hide();
        $('billing_wrap').hide();
        $('shipping_wrap').hide();
    }
    else {
        $('donor_wrap').show();
        $('billing_wrap').show();
        $('shipping_wrap').show();
    }
}

function ToggleShipping(input) {
    if (input.checked) {
        $('saddress').value = $('baddress').value
        $('scity').value = $('bcity').value
        $('sstate').selectedIndex = $('bstate').selectedIndex
        $('spostal_code').value = $('bpostal_code').value
    }
    else {
        $('saddress').value = '';
        $('scity').value = '';
        $('sstate').selectedIndex = 0;
        $('spostal_code').value = '';
    }
}

function ToggleOther(select) {
    var val = select.options[select.selectedIndex].value;
    if (val == 'other') {
        $('other_wrap').show();
    }
    else {
        $('other_amount').value = '';
        $('other_wrap').hide();
    }
}

function validateCaptcha() {
    new Ajax.Request('http://' + window.location.host + '/CaptchaValidator.acux', { parameters: { captcha: $('captcha').value }, onSuccess: function() { $('makeDonation').removeClassName('disabled'); $('captcha').removeClassName('invalid'); }, onFailure: function() { $('makeDonation').addClassName('disabled'); $('captcha').addClassName('invalid'); } });
}

function SubmitForm() {
    $$('input.required', 'select.required').each(function(input) {
        ValidateRequired(input);
    });
    var errors = parseInt(activeForm.select('input.invalid, select.invalid').size());
    if (errors == 0)
        return true;
    else {
        alert("Please fill in required fields marked in red");
        return false;
    }    
}

function showTooltip(e, id) {
    var tooltip = $(id);
    var posx = (e.pageX ? e.pageX : e.clientX + document.body.scrollLeft);
    var posy = (e.pageY ? e.pageY : e.clientY + document.body.scrollTop);
    posx = Number(posx) - 10;
    posy = Number(posy) - 160;
    tooltip.setStyle({
        left: posx + 'px',
        top: posy + 'px'
    });
    tooltip.show();
}
function hideTooltip(id) {
    $(id).hide();
}
