function initDonationForm() {
    // initialize the payment type radios
    initPaymentType();
    checkPaymentType();
                
    // initialize the other gift field
    initOtherGift();
    
    // initialize the credit card fields
    initCreditCard();
    
    // initialize the help dialogs
    initSecurityInfoHelp('securityInfoLink', '/help/security-dialog.cfm');
    initCSCHelp('cscLink', '/help/csc-dialog.cfm');
    
    // initialize the font sizing controls
    initFontSizeOne();
    initFontSizeTwo();
    
    return true;    
}

function initMonthlyDonationForm() {
    // initialize the payment type radios
    initPaymentType();
    checkPaymentType();
    
    // initialize the other and one-time gift fields
    initMonthlyOtherGift();
    initOneTimeGift();
    
    // initialize the credit card fields
    initCreditCard();
    
    // initialize the help dialogs
    initSecurityInfoHelp('securityInfoLink', '/help/security-dialog.cfm');
    initCSCHelp('cscLink', '/help/csc-dialog.cfm');
    
    // initialize the font sizing controls
    initFontSizeOne();
    initFontSizeTwo();
    
    return true;    
}


function initFontSizeOne() {
    $('#fontSize1').click(function() {
        $('#mainContent').css('font-size', $(this).css('font-size'));
        $('#securityInfoLink').html('Your Transaction is Safe &amp; Secure');
        return false;
    });
    
    return true;    
}

function initFontSizeTwo() {
    $('#fontSize2').click(function() {
        $('#mainContent').css('font-size', $(this).css('font-size'));
        $('#securityInfoLink').html('Your Transaction is Safe');
        return false;
    });
    
    return true;
}

function aFieldHasValue(fieldArray) {    
    var oneHasValue = false;    
    
    //console.log('aFieldHasValue() was called');
    for (var a=0; a<fieldArray.length; a++) {
        if ($('#' + fieldArray[a]).val() != "") {
            //console.log(fieldArray[a] + ' has a value of ' + $('#' + fieldArray[a]).val());
            oneHasValue = true;
        } else {
            //console.log(fieldArray[a] + ' has no value');
        }
    }
    
    if (oneHasValue) {
        //console.log('Since one field had a value, returning true');
        return true;
    } else {
        //console.log('Since no fields had a value, returning false');
        return false;
    }
}

function addFieldRequired(fieldArray) {
    //console.log('addFieldRequired() was called');
    for (var a=0; a<fieldArray.length; a++) {
        $('#' + fieldArray[a]).parent('label').addClass('fieldRequired');
        //console.log('Adding required class for label of ' + fieldArray[a] + ' field');
    }
}

function removeFieldRequired(fieldArray) {
    //console.log('removeFieldRequired() was called');
    for (var a=0; a<fieldArray.length; a++) {
        $('#' + fieldArray[a]).parent('label').removeClass('fieldRequired');
        //console.log('Removing required class for label of ' + fieldArray[a] + ' field');
    }
}

function checkFieldsForRequired(fieldArray) {
    //console.log('checkFieldsForRequired() was called');
    //console.log('Does one of the fields have a value?');
    var theseFieldsHaveValue = aFieldHasValue(fieldArray);
    if (!theseFieldsHaveValue) {
        //console.log('No, none of the fields have any values, calling function to remove required classes from labels.');
        //console.log('Calling removeFieldRequired()');
        removeFieldRequired(fieldArray);
    }
}

function checkDonateForm(formName) {
    var requiredFields = ['payerFirstName','payerLastName','payerStreet1','payerCityName','payerStateOrProvince','payerPostalCard','payerCountry','payer'];
    var alertMessage = '';
    
    //console.log('checkDonateForm() was called');
    
    if ($('#otherGiftAmountRadio:checked').length > 0 && $('#otherGiftAmount').val() == '') {
        alertMessage += '<li>You must enter an amount if you select other for your donation.</li>';
    }
    
    // In Honor Of form
    if (formName == 'honor') {
        alertMessage += checkHonorForm();
    }
    
    // John Muir Heritage Society form
    if (formName == 'jmhs') {
        var jmhsAlertMessage = '';
        
        if ($('#orderTotalContainer input:radio:checked').length == 0) {
            jmhsAlertMessage += '<li>Gift Amount</li>';
        }
        
        if (jmhsAlertMessage != '') {
            alertMessage += + jmhsAlertMessage;
        }    
    }
    
    // Monthly form
    if (formName == 'monthly') {
        var monthlyAlertMessage = '';
        
        if ($('#orderTotalContainer input:radio:checked').length == 0 && $('#oneTimeGift:checked').length == 0) {
            monthlyAlertMessage += '<li>Monthly or One-Time Gift Amount</li>';
        }
        
        if ($('#otherGiftAmountRadio:checked').length > 0 && $('#otherGiftAmount').val() < 5) {
            monthlyAlertMessage += '<li>If you select other for your monthly donation, you must enter an amount equal or greater then $5.00</li>';
        }
        
        if ($('#oneTimeGift:checked').length > 0 && $('#oneTimeGiftAmount').val() == '') {
            monthlyAlertMessage += '<li>If you select One-Time Gift, you must enter an amount</li>';
        }
        
        if (monthlyAlertMessage != '') {
            alertMessage += monthlyAlertMessage;
        }
    }
    
    for (var i=0; i<requiredFields.length; i++) {
        if ($('#' + requiredFields[i]).val() == '') {
            alertMessage += '<li>' + $('#' + requiredFields[i]).attr('title') + '</li>';            
        }
    }
    
    if ($('#paymentTypeVisa:checked, #paymentTypeMasterCard:checked, #paymentTypeDiscover:checked, #paymentTypeAmericanExpress:checked').length > 0) {
        var reqCcFields = ['creditCardNumber','cvv2','expMonth','expYear'];
        var alertCcMessage = '';
        
        for (i=0; i<reqCcFields.length; i++) {
            if ($('#' + reqCcFields[i]).val() == '') {
                alertCcMessage += '<li>' + $('#' + reqCcFields[i]).attr('title') + '</li>';            
            }
        }
        if (alertCcMessage != '') {
            alertCcMessage = '<li>Credit Card Fields\:<ul>' + alertCcMessage + '</ul></li>';
            alertMessage += alertCcMessage;
        }
    }
    
    if ($('#paymentTypeAch:checked').length > 0) {
        var reqAchFields = ['bankName','accountNumber','routingNumber','bankCity','bankState','electronicSignature'];
        var alertAchMessage = '';
        
        for (i=0; i<reqAchFields.length; i++) {
            if ($('#' + reqAchFields[i]).val() == '') {
                alertAchMessage += '<li>' + $('#' + reqAchFields[i]).attr('title') + '</li>';            
            }
        }
        if (alertAchMessage != '') {
            alertAchMessage = '<li>Automatic Debit\:<ul>' + alertAchMessage + '</ul></li>';
            alertMessage += alertAchMessage;
        }
    }
    
    
    if (alertMessage != '') {
        alertMessage = '<p>You must fill out the following fields before you can continue\:<ul>' + alertMessage + '</ul></p>';
        //console.log(alertMessage);
        displayFormErrorDialog(alertMessage);
        return false;
    } else {
        return true;
    }
}

function checkHonoreeFixForm() {
    var alertMessage = '';
    
    alertMessage += checkHonorForm();
    
    if (alertMessage != '') {
        alertMessage = '<p>You must fill out the following fields before you can continue\:<ul>' + alertMessage + '</ul></p>';
        //console.log(alertMessage);
        displayFormErrorDialog(alertMessage);
        return false;
    } else {
        return true;
    }
}

function checkHonorForm() {
    var honorAlertMessage = '';
    
    if ($('#honoreeTypeContainer input:checked').length == 0) {
        honorAlertMessage += '<li>A selection for "This gift is in\:"</li>';
    }

    if ($('#inHonorOf').val() == "") {
        honorAlertMessage += '<li>The name of the person you\'re honoring</li>';
    }
    
    // acknowledment
    var ackFields = ['notifyFirstName', 'notifyLastName', 'notifyStreet1', 'notifyCityName', 'notifyStateOrProvince', 'notifyPostalCode'];
    var ackFieldsHaveValue = aFieldHasValue(ackFields)

    if (ackFieldsHaveValue) {
        for (var b=0; b<ackFields.length; b++) {
            if ($('#' + ackFields[b]).val() == "") {
                honorAlertMessage += '<li>' + $('#' + ackFields[b]).attr('title') + '</li>';
            }
        }
    }
    
    if (honorAlertMessage != '') {
        honorAlertMessage = '<li>Acknowledgement Information<ul>' + honorAlertMessage + '</ul></li>';
    }

    return honorAlertMessage;
}

function showCreditCard() {
    $('#creditCardFields').show('fast');
    $('#achFields').hide('fast');
}
function showAch() {
    $('#creditCardFields').hide('fast');
    $('#achFields').show('fast');
}
function showPayPal() {
    $('#creditCardFields').hide('fast');
    $('#achFields').hide('fast');
}

// other gift amount
function initOtherGift() {
    lastOtherGiftAmount = '';
    
    $('#otherGiftAmountRadio').change(function() {
        $('#otherGiftAmount').focus();
        if (lastOtherGiftAmount != '') {
            $('#otherGiftAmount').val(lastOtherGiftAmount);
        }
    }).click(function() {
        $('#otherGiftAmount').focus();
        if (lastOtherGiftAmount != '') {
            $('#otherGiftAmount').val(lastOtherGiftAmount);
        }
    });
    $('#otherGiftAmount').click(function() {
        $('#otherGiftAmountRadio').attr('checked','checked');
    }).blur(function() {
        $('#otherGiftAmountRadio').val(this.value);
    }).keydown(function() {
        if ($(this).val() != "") {
            $('#otherGiftAmountRadio').attr('checked','checked');
        }
    });
    
    $('#orderTotalContainer .radioContainer input:radio:not("#otherGiftAmountRadio")').focus(function() {
        if ($('#otherGiftAmount').val() != '') {
            lastOtherGiftAmount = $('#otherGiftAmount').val();
            $('#otherGiftAmount').val('');
        }                
    });
}

function initMonthlyOtherGift() {
    lastOtherGiftAmount = '';

    // new stuff
    $('#orderTotalContainer .radioContainer input:radio:not("#otherGiftAmountRadio")').change(function() {
        //console.log('orderTotalContainer input:radio change event was triggered, clearing otherGiftAmount and calling clearOneTimeGift()');
        if ($('#otherGiftAmount').val() != '') {
            lastOtherGiftAmount = $('#otherGiftAmount').val();
            $('#otherGiftAmount').val('');
        }
        clearOneTimeGift();
    }).click(function() {
        //console.log('orderTotalContainer input:radio change event was triggered, clearing otherGiftAmount and calling clearOneTimeGift()');
        if ($('#otherGiftAmount').val() != '') {
            lastOtherGiftAmount = $('#otherGiftAmount').val();
            $('#otherGiftAmount').val('');
        }
        clearOneTimeGift();
    });
    
    $('#orderTotalContainer #otherGiftAmountRadio').change(function() {
        //console.log('#orderTotalContainer #otherGiftAmountRadio change event was triggered, focusing on otherGiftAmount and if the stored previous value of otherGiftAmount is not blank, restoring that to otherGiftAmount...And calling clearOneTimeGift()');
        $('#otherGiftAmount').focus();
        if (lastOtherGiftAmount != '') {
            $('#otherGiftAmount').val(lastOtherGiftAmount);
        }
        clearOneTimeGift();
    }).click(function() {
        //console.log('#orderTotalContainer #otherGiftAmountRadio change event was triggered, focusing on otherGiftAmount and if the stored previous value of otherGiftAmount is not blank, restoring that to otherGiftAmount...And calling clearOneTimeGift()');
        $('#otherGiftAmount').focus();
        if (lastOtherGiftAmount != '') {
            $('#otherGiftAmount').val(lastOtherGiftAmount);
        }
        clearOneTimeGift();
    });
    
    $('#otherGiftAmount').click(function() {
        //console.log('otherGiftAmount click event was triggered, checking otherGiftAmountRadio and calling clearOneTimeGift()');
        $('#otherGiftAmountRadio').attr('checked','checked');
        clearOneTimeGift();
    }).keydown(function() {
        //console.log('otherGiftAmount keydown event was triggered');
        if ($(this).val() != "") {
            //console.log('otherGiftAmount\'s value is not blank, so checking otherGiftAmountRadio and calling clearOneTimeGift()');
            $('#otherGiftAmountRadio').attr('checked','checked');
            clearOneTimeGift();
        }
    });
}

function oneTimeGiftAmountHandler(){
    //console.log('oneTimeGiftAmountHandler() was called');
    if ($('#oneTimeGift:checked').length == 0) {
        //console.log('oneTimeGift was not checked, removing checked from orderTotalContainer input.inputRadio fields');
        $('#orderTotalContainer input.inputRadio').removeAttr('checked');
        $('#otherGiftAmount').val('');
        $('#oneTimeGift').attr('checked', 'checked');
    }
}

function clearOneTimeGift() {
    //console.log('clearOneTimeGift() was called');
    //console.log('Removing checked from oneTimeGift, and removing value from oneTimeGiftAmount');
    $('#oneTimeGift').removeAttr('checked');
    $('#oneTimeGiftAmount').val('');
}

function initOneTimeGift() {
    $('#oneTimeGift').change(function() {
        //console.log('oneTimeGift change event was triggered');
        if ($('#oneTimeGift:checked').length > 0) {
            //console.log('oneTimeGift is checked, removing checked from orderTotalContainer input.inputRadio, and setting focus to oneTimeGiftAmount');
            $('#orderTotalContainer input.inputRadio').removeAttr('checked');
            $('#otherGiftAmount').val('');
            $('#oneTimeGiftAmount').focus();
        } else {
            //console.log('oneTimeGift is not checked, clearing oneTimeGiftAmount\'s value');
            $('#oneTimeGiftAmount').val('');
        }
    });
    
    $('#oneTimeGiftAmount').click(function() {
        //console.log('oneTimeGiftAmount\'s click event was triggered, calling oneTimeGiftAmountHandler()');
        oneTimeGiftAmountHandler();
    }).keydown(function() {
        //console.log('oneTimeGiftAmount\'s keydown event was triggered');
        if ($(this).val() != "") {
            //console.log('oneTimeGiftAmount\'s value is not blank, calling oneTimeGiftAmountHandler()');
            oneTimeGiftAmountHandler();
        }
    });

}


// handle cc and csc numbers
function initCreditCard() {
    ccHistory = {
        ccNumber: $('#creditCardNumber').val(),
        csc: $('#cvv2').val()                
    };
    if (ccHistory.ccNumber != '') {
        var $ccNum = $('#creditCardNumber');
        
        $ccNum.focus(function() {
            if ($ccNum.val() == ccHistory.ccNumber) {
                $ccNum.val('');
            }
        });
        $ccNum.blur(function() {
            if (ccHistory.ccNumber != '' && $ccNum.val() == '') {
                $ccNum.val(ccHistory.ccNumber);                        
            } else {
                ccHistory.ccNumber = '';
            }
        });                
    }
    if (ccHistory.csc != '') {
        var $csc = $('#cvv2');
        
        $csc.focus(function() {
            if ($csc.val() == ccHistory.csc) {
                $csc.val('');
            }
        });
        $csc.blur(function() {
            if (ccHistory.csc != '' && $csc.val() == '') {
                $csc.val(ccHistory.csc);                        
            } else {
                ccHistory.csc = '';
            }
        });                
    }
}

function initPaymentType() {
    $('#paymentTypeVisa, #paymentTypeMasterCard, #paymentTypeDiscover, #paymentTypeAmericanExpress').click(function() {
        $('#creditCardFields').slideDown('fast');
        $('#achFields').slideUp('fast');
    });
    $('#paymentTypePayPal').click(function() {
        $('#creditCardFields').slideUp('fast');
        $('#achFields').slideUp('fast');
    });
    $('#paymentTypeAch').click(function() {
        $('#creditCardFields').slideUp('fast');
        $('#achFields').slideDown('fast');
    });
}

function checkPaymentType() {
    if ($('#paymentTypeVisa:checked, #paymentTypeMasterCard:checked, #paymentTypeDiscover:checked, #paymentTypeAmericanExpress:checked').length > 0) {
        $('#creditCardFields').show();
        $('#achFields').hide();
    }
    if ($('#paymentTypePayPal:checked').length > 0) {
        $('#creditCardFields').hide();
        $('#achFields').hide();
    }
    if ($('#paymentTypeAch:checked').length > 0) {
        $('#creditCardFields').hide();
        $('#achFields').show();
    }
}

/***** Initialize Card Security Code Dialog *****/
function initCSCHelp(linkId, urlForHelp) {
    $('#' + linkId).click(function() {
        displayCscHelp(linkId, urlForHelp);
        return false;
    });
}

function displayCscHelp(linkId, urlForHelp) {
    if ($('#cardSecurityCodeInfo').length == 0) {
        $('<div id="cardSecurityCodeInfo" style="display\:none"></div>').insertAfter('#' + linkId);
        
        $.get(urlForHelp, '', function(data) { $('#cardSecurityCodeInfo').html(data); }, 'html');
        
        $('#cardSecurityCodeInfo').dialog({
            title: 'Customer Support \: The Yosemite Fund',
            autoOpen: false,
            modal: true,
            bgiframe: true,
            draggable: false,
            resizable: false,
            width: 450,
            buttons: { 
                'Ok': function() {
                    $(this).dialog("close");
                }
            }
        });
    }
    
    $('#cardSecurityCodeInfo').dialog('open');    
    
    return false;    
}

/***** Initialize Security Dialog *****/
function initSecurityInfoHelp(linkId, urlForHelp) {
    $('#' + linkId).click(function() {
        displaySecurityInfo(linkId, urlForHelp);
        return false;
    });
}

function displaySecurityInfo(linkId, urlForHelp) {
    if ($('#securityInfo').length == 0) {
        $('<div id="securityInfo" style="display\:none"></div>').insertAfter('#' + linkId);
        
        $.get(urlForHelp, '', function(data) { $('#securityInfo').html(data); }, 'html');
        
        $('#securityInfo').dialog({
            title: 'Customer Support \: The Yosemite Fund',
            autoOpen: false,
            modal: true,
            bgiframe: true,
            draggable: false,
            resizable: false,
            width: 450,
            buttons: { 
                'Ok': function() {
                    $(this).dialog("close");
                }
            }
        });
    }
    
    $('#securityInfo').dialog('open');    
    
    return false;    
}

// initialize the Form Error dialog
function displayFormErrorDialog(msg) {
    if ($('#formErrorDialog').length == 0) {
        $('<div id="formErrorDialog" style="text-align\:left; display\:none;"></div>').appendTo('body');
        
        $('#formErrorDialog').dialog({
            title: 'Error with your form submission',
            autoOpen: false,
            bgiframe: true,
            draggable: false,
            resizable: false,
            width: 450,
            buttons: { 
                'Ok': function() {
                    $(this).dialog('close');
                }
            }
        });
    }
    
    $('#formErrorDialog').html(msg);
    $('#formErrorDialog').dialog('open'); 
}

// monthly form minimum checks
function checkMonthlyMinimum() {
    if ($('#otherGiftAmount').val() > 5) {
        return true;
    } else {
        return false;
    }
}









