// -----------------------------------------------------------------------------------------------------------
// Functions for form validation of the quiz
// -----------------------------------------------------------------------------------------------------------

var errorBkImg = '/img/fyc/gfx_error_17x17.gif';
var errorWtImg = '/img/white_dot.gif';

// Required fields are  'q1', 'q2', 'q3', 'q4', 'q5'
var reqVals = [ 'q1', 'q2', 'q3', 'q4', 'q5' ];
// create set of answers for scoring
var answers = [ '', '', '', '', '' ];

// Lease vs. Loan Quiz
function leaseVsLoanQuiz() {
    // Check for any required fields or fileds that must have a value
    // Validate Required Fields
    var invalidFields;
    var missingFields;
    var tempFlags = validateRequiredFields();
    missingFields = Number(tempFlags);

    // If any required fields are blank show blank field error
    if (missingFields != 0){
        document.getElementById('missingFields').style.display  = 'block';
        return(false);
    } else {
        document.getElementById('missingFields').style.display  = 'none';
    }
    
    // Score the quiz and display result
    document.getElementById('LvsL_result').innerHTML = scoreAnswers();
}

// -----------------------------------------------------------------------------------------------------------
// Functions that support the form validation
// -----------------------------------------------------------------------------------------------------------

// Validate Required Fields & save answers
function validateRequiredFields() {
    var missingFields = 0;
    var reqValsLen = reqVals.length;
    var eleID_A;
    var eleID_B;
    
    for (var i = 0; i < reqValsLen; i++) {
        var label = document.getElementById('label_' + reqVals[i]);
        var eleID_A = document.getElementById(reqVals[i] + 'a');
        var eleID_B = document.getElementById(reqVals[i] + 'b');
        
        if (!eleID_A.checked && !eleID_B.checked){
            // Flag Label
            label.style.color  = '#f00';
            if (label.childNodes[0].getAttribute("src") != errorBkImg){
                label.childNodes[0].setAttribute("src",errorBkImg);
            }            
            missingFields = missingFields + 1;
        } else {
            // Return label back to default state.
            label.style.color  = '#000';
            label.childNodes[0].setAttribute("src",errorWtImg);
            // Set answer A or B for question
            if (eleID_A.checked) {
                answers[i] = 'A';
            } else {
                answers[i] = 'B';
            }
        }
    }
    return (missingFields);
}

// Score answers
function scoreAnswers() {
    var answersLen = answers.length;
    var total_A = 0;
    var total_B = 0;
    var resultMessage;
    
    for (var i = 0; i < answersLen; i++) {        
        if (answers[i] == 'A'){
            total_A++;
        } else {
            total_B++;
        }
    }
    
    // Message Parts
    var messageStart = 'Based on your answers, ';
    var messageLoanBetter = '<strong>a loan may work well for you.</strong> ';
    var messageLeaseBetter = '<strong>a lease may work well for you.</strong> ';
    var messageEitherOr = 'either <strong>a lease or a loan may work well for you.</strong> ';
    var messageLearnMoreBasic = 'To learn more, please read <a href="/creditcenter/learn.jsp?contentid=25350">Leasing Basics</a> and <a href="/creditcenter/learn.jsp?contentid=25347">Loan Basics</a>.';
    var messageLearnMoreLoan = 'To learn more, please read <a href="/creditcenter/learn.jsp?contentid=25347">Loan Basics</a> and <a href="/creditcenter/learn.jsp?contentid=25348">5 Signs You Might Love a Loan</a>.';
    var messageLearnMoreLease = 'To learn more, please read <a href="/creditcenter/learn.jsp?contentid=25350">Leasing Basics</a> and <a href="/creditcenter/learn.jsp?contentid=25351">5 Signs You Might Love a Lease</a>.';
    // Messages for 1 B answer
    var messageB1 = 'Keep in mind, there are mileage restrictions associated with leases. ';
    var messageB2 = 'Keep in mind, there are maintenance requirements associated with leases. ';
    var messageB3 = 'Keep in mind, you may need to arrange for another car to replace the leased vehicle at the end of the lease period. ';
    var messageB4 = 'Keep in mind, vehicle customization is often very limited with most leases. ';
    var messageB5 = 'Keep in mind, at the end of the lease you will not own the car and must return or purchase it (if applicable) at that time. ';
    // Messages for 2 B answers
    var messageB1_2 = 'Keep in mind, with leases, there are mileage restrictions and scheduled maintenance is often required. ';
    var messageB1_3 = 'Keep in mind, with leases, there are mileage restrictions and you may need to arrange for another car to replace the leased vehicle at the end of the lease period. ';
    var messageB1_4 = 'Keep in mind, with leases, there are mileage restrictions and vehicle customization is often very limited. ';
    var messageB1_5 = 'Keep in mind, with leases, there are mileage restrictions. You also will not own the car at the end of the lease and must return or purchase it (if applicable) at that time. ';    
    var messageB2_3 = 'Keep in mind, with leases, scheduled maintenance is often required and you may need to arrange for another car to replace the leased vehicle at the end of the lease period. ';
    var messageB2_4 = 'Keep in mind, with leases, scheduled maintenance is often required and vehicle customization is often very limited. ';
    var messageB2_5 = 'Keep in mind, with leases, scheduled maintenance is often required. You also will not own the car at the end of the lease and must return or purchase it (if applicable) at that time. ';    
    var messageB3_4 = 'Keep in mind, with leases, you may need to arrange for another car to replace the leased vehicle at the end of the agreement and vehicle customization is often very limited. ';
    var messageB3_5 = 'Keep in mind, with leases, you may need to arrange for another car to replace the leased vehicle at the end of the agreement You also will not own the car at the end of the lease and must return or purchase it (if applicable) at that time. ';    
    var messageB4_5 = 'Keep in mind, with leases, vehicle customization is often very limited. You also will not own the car at the end of the lease and must return or purchase it (if applicable) at that time. ';
    
    
    if (total_B >= 3){
        resultMessage = messageStart + messageLoanBetter + messageLearnMoreLoan;
    } else if (total_A == 5){
        resultMessage = messageStart + messageLeaseBetter + messageLearnMoreLease;
    } else if (total_B == 1){
        if (answers[0] == 'B'){
            resultMessage = messageStart + messageLeaseBetter + messageB1 + messageLearnMoreLease;
        } else if (answers[1] == 'B'){
            resultMessage = messageStart + messageLeaseBetter + messageB2 + messageLearnMoreLease;
        } else if (answers[2] == 'B'){
            resultMessage = messageStart + messageLeaseBetter + messageB3 + messageLearnMoreLease;
        } else if (answers[3] == 'B'){
            resultMessage = messageStart + messageLeaseBetter + messageB4 + messageLearnMoreLease;
        } else if (answers[4] == 'B'){
            resultMessage = messageStart + messageLeaseBetter + messageB5 + messageLearnMoreLease;
        }
    } else if (total_B == 2){
        if (answers[0] == 'B' && answers[1] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB1_2 + messageLearnMoreBasic;
        } else if (answers[0] == 'B' && answers[2] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB1_3 + messageLearnMoreBasic;
        } else if (answers[0] == 'B' && answers[3] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB1_4 + messageLearnMoreBasic;
        } else if (answers[0] == 'B' && answers[4] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB1_5 + messageLearnMoreBasic;
        } else if (answers[1] == 'B' && answers[2] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB2_3 + messageLearnMoreBasic;
        } else if (answers[1] == 'B' && answers[3] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB2_4 + messageLearnMoreBasic;
        } else if (answers[1] == 'B' && answers[4] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB2_5 + messageLearnMoreBasic;
        } else if (answers[2] == 'B' && answers[3] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB3_4 + messageLearnMoreBasic;
        } else if (answers[2] == 'B' && answers[4] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB3_5 + messageLearnMoreBasic;
        } else if (answers[3] == 'B' && answers[4] == 'B' ){
            resultMessage = messageStart + messageEitherOr + messageB4_5 + messageLearnMoreBasic;
        }
    }
    return (resultMessage);
}

// Reset Calcs to default state
function resetQuiz(){    
    
    clearReturnedValue();
    
    var reqValsLen = reqVals.length;
    var eleID_A;
    var eleID_B;
    for (var i = 0; i < reqValsLen; i++) {
        var label = document.getElementById('label_' + reqVals[i]);
        var eleID_A = document.getElementById(reqVals[i] + 'a');
        var eleID_B = document.getElementById(reqVals[i] + 'b');
        eleID_A.checked = false;
        eleID_B.checked = false;
        label.style.color  = '#000';
        label.childNodes[0].setAttribute("src",errorWtImg);
    }
    
    if (document.getElementById('missingFields') != null){
        document.getElementById('missingFields').style.display  = 'none';
    }
 }

// Clear returned value on change
function clearReturnedValue(){
    var quizResult = document.getElementById('LvsL_result');
    if (quizResult.innerHTML){
        quizResult.innerHTML = '';
    }
 }

// -----------------------------------------------------------------------------------------------------------
// Functions for reporting
// -----------------------------------------------------------------------------------------------------------
 
//function for reporting, Calculate is being clicked on a Calculator
function reportCalculateClick(rdpagePram){
    // Create url mis code and rdpage parameter
    var misCode = "CCCTBDMGCC1051";
    var reportCode = "mis=" + misCode + "&rdpage=" + rdpagePram;
    var urlString = "/redirect/redirector_link.jsp?to_url=/img/blank_dot.gif&" + reportCode;
    // Set the location of the iFrame
    document.getElementById("reportFrame").src = urlString;
}
