/**
 * Formal Forms - Covidien Standard Code for Ajax Form Submission with Formalized UI
 *
 */

$(function() {
    //$('div#bmiForm').hide();
    $('div#bmiForm form.bmiForm').hide();
    $('div#bmiResults').hide();

    //intro
    $('div#bmiForm div.document div.qsubmit a.begin').click(function() {
        
        pageTracker._trackEvent('B4D PAT', 'Application Start', 'START');
        $('div#bmiForm div.Rich_Description').hide('fast');
        $('form.bmiForm1').show('slow');
    });

    //form 1
    $('div#bmiForm form.bmiForm1 div.qsubmit a.submit').click(function() {
        $('div#bmiForm form.bmiForm1').submit();
    });
    $('form.bmiForm1').validate({
        rules: {
            height_ft: { required: true, digits: true, min: 0 },
            height_in: { required: true, digits: true, min: 0, range: [0, 12] },
            weight: { required: true, digits: true, min: 0 },
            diabetes: { required: true },
            joint_pain: { required: true }
        },
        groups: {
            height: "height_ft height_in"
        },
        messages: {
            height_ft: "enter valid height",
            height_in: "enter valid height",
            weight: "enter valid weight",
            diabetes: "please select",
            joint_pain: "please select"
        },
        errorPlacement: function(error, element) {
            if (element.attr("name") == "height_ft" || element.attr("name") == "height_in") {
                error.insertAfter(".heightInLabel");
            } else if (element.attr("name") == "weight") {
                error.insertAfter(".weightInputLabel");
            } else if (element.hasClass("checkbox") || element.hasClass("radioInput")) {  //is a checkbox, add error label to parent .checkboxlabel
                error.appendTo(element.parent().prev());
            } else {    //is not a checkbox, add as normal
                error.appendTo(element.parent());
                //error.addClass
            }
        },
        submitHandler: function(form) {

            $('form.bmiForm1').hide('fast');
            $('form.bmiForm2').show('slow');
        }
    });

    //form 2
    $('div#bmiForm form.bmiForm2 div.qsubmit a.submit').click(function() {
        $('div#bmiForm form.bmiForm2').submit();
    });
    $('div#bmiForm form.bmiForm2 div.qsubmit a.previous').click(function() {
        $('form.bmiForm2').hide('fast');
        $('form.bmiForm1').show('slow');
    });
    $('form.bmiForm2').validate({
        submitHandler: function(form) {

            $('form.bmiForm2').hide('fast');
            $('form.bmiForm3').show('slow');
        }
    });

    //form 3
    $('div#bmiForm form.bmiForm3 div.qsubmit a.submit').click(function() {
        $('div#bmiForm form.bmiForm3').submit();
    });
    $('div#bmiForm form.bmiForm3 div.qsubmit a.previous').click(function() {
        $('form.bmiForm3').hide('fast');
        $('form.bmiForm2').show('slow');
    });

    $('form.bmiForm3').validate({
        rules: {
            weight_interfere: { required: true },
            frustrated_at_weight_loss_attempt: { required: true }
        },
        messages: {
            weight_interfere: "please select",
            frustrated_at_weight_loss_attempt: "please select"
        },
        errorPlacement: function(error, element) {
            if (element.hasClass("checkbox") || element.hasClass("radioInput")) {  //is a checkbox, add error label to parent .checkboxlabel
                error.appendTo(element.parent().prev());
            } else {    //is not a checkbox, add as normal
                error.appendTo(element.parent());
                //error.addClass
            }
        },
        submitHandler: function(form) {

            $('form.bmiForm3').hide('fast');
            $('form.bmiForm4').show('slow');
        }
    });

    //form 4
    $('div#bmiForm form.bmiForm4 div.qsubmit a.submit').click(function() {
        $('div#bmiForm form.bmiForm4').submit();
    });
    $('div#bmiForm form.bmiForm4 div.qsubmit a.previous').click(function() {
        $('form.bmiForm4').hide('fast');
        $('form.bmiForm3').show('slow');
    });
    $('form.bmiForm4').validate({
        rules: {
            insurance_type: { required: true }
        },
        messages: {
            insurance_type: "please select"
        },
        errorPlacement: function(error, element) {
            if (element.hasClass("checkbox") || element.hasClass("radioInput")) {  //is a checkbox, add error label to parent .checkboxlabel
                error.appendTo(element.parent().prev());
            } else {    //is not a checkbox, add as normal
                error.appendTo(element.parent());
                //error.addClass
            }
        },
        submitHandler: function(form) {

            $('div#bmiForm form.bmiForm4').hide('fast');
            $('div#bmiForm').hide('fast');
            $('div#bmiResults').show('slow');

            $('div#bmiResults div.learn').hide();
            $('div#bmiResults div.action').hide();
            $('div#bmiResults div.results').show('slow');

            $('div#bmiResults div.bmiNav ul li.item1 a').addClass('active');
            if ($('div#bmiResults div.bmiNav ul li.item1 a').attr('class', 'active')) {
                
                pageTracker._trackEvent('B4D PAT', 'Result View Change\: Your Results', 'YOUR RESULTS');
            }

            // START BMI LOGIC

            //get answer values
            var q1ft = $("input[name='height_ft']").val();
            var q1in = $("input[name='height_in']").val();
            var q1lbs = $("input[name='weight']").val();
            var q2 = $("input[name='diabetes']:checked").attr("value");
            var q3 = $("input[name='joint_pain']:checked").val();
            var q4 = $("input[name='health_problems']:checked").val();
            var q5 = $("input[name='weight_interfere']:checked").val();
            var q6 = $("input[name='frustrated_at_weight_loss_attempt']:checked").val();
            var q7 = $("select[name='insurance_type']").val();
            var q8 = $("input[name='enjoy_healthy_weight_activity']").val();

            //question 1
            var height_in = (parseInt(q1ft) * 12) + parseInt(q1in);
            var weight_lbs = parseInt(q1lbs);

            var bmi1 = weight_lbs * 703;
            var bmi2 = height_in * height_in;
            var bmi = parseInt(bmi1 / bmi2);


            
            pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q1', 'BMI\:' + bmi);

            if (bmi < 35) {
                $('div#bmiResults div.learn div.question1 div.40Plus').hide();
                $('div#bmiResults div.learn div.question1 div.35Plus').hide();
                $('div#bmiResults div.learn div.question1 div.35Minus').show();
            } else if (bmi > 40) {
                $('div#bmiResults div.learn div.question1 div.40Plus').show();
                $('div#bmiResults div.learn div.question1 div.35Plus').hide();
                $('div#bmiResults div.learn div.question1 div.35Minus').hide();
            } else {    //bmi is between 35 to 39
                $('div#bmiResults div.learn div.question1 div.40Plus').hide();
                $('div#bmiResults div.learn div.question1 div.35Plus').show();
                $('div#bmiResults div.learn div.question1 div.35Minus').hide();
            }

            //question 2
            //alert(q2);
            if (q2 == 'yes') {
                $('div#bmiResults div.learn div.question2 div.yes').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q2', 'HAVE DIABETES');
            } else if (q2 == 'no') {
                $('div#bmiResults div.learn div.question2 div.no').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q2', 'DON\'T HAVE DIABETES');
            } else if (q2 == 'pre-diabetes') {
                $('div#bmiResults div.learn div.question2 div.pre-diabetes').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q2', 'PRE DIABETES');
            } else {
                //error
            }


            //question 3
            //alert(q3);
            if (q3 == 'yes') {
                $('div#bmiResults div.learn div.question3 div.yes').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q3', 'HAVE JOINT PAIN');
            } else if (q3 == 'no') {
                $('div#bmiResults div.learn div.question3 div.no').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q3', 'DON\'T HAVE JOINT PAIN');
            } else {
                //error
            }

            //question 4
            //alert(q4);
            var checkValNum = $('input[@name=health_problems]:checked').length;
            var headerText = "4. You have ";
            var count = 0;
            $('input[@name=health_problems]:checked').each(function(i, val) {
                var checkVal = $(this).val();

                //alert(i);
                //alert(checkVal);

                if (i == 0) {
                    headerText = headerText + checkVal + " ";
                } else {
                    headerText = headerText + " and " + checkVal;
                }


                if (checkVal == 'Heart disease') {
                    $('div#bmiResults div.learn div.question4 ul.wlsResults li.heart-disease').show();
                }
                if (checkVal == 'High blood pressure') {
                    $('div#bmiResults div.learn div.question4 ul.wlsResults li.high-blood-pressure').show()
                }
                if (checkVal == 'High cholesterol') {
                    $('div#bmiResults div.learn div.question4 ul.wlsResults li.high-colesterol').show()
                }
                if (checkVal == 'Sleep apnea') {
                    $('div#bmiResults div.learn div.question4 ul.wlsResults li.sleep-apnea').show()
                }
                if (checkVal == 'Acid reflux disease') {
                    $('div#bmiResults div.learn div.question4 ul.wlsResults li.acid-reflux-disease').show()
                }
                if (checkVal == 'Severe urinary incontinence') {
                    $('div#bmiResults div.learn div.question4 ul.wlsResults li.severe-urinary-incontinence').show()
                }
                if (checkVal == 'Fertility problems') {
                    $('div#bmiResults div.learn div.question4 ul.wlsResults li.fertility-problems').show()
                }
                count++;
            });
            pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q4', 'COMORBIDITIES: '+count);

            if (checkValNum == 0) {
                $('div#bmiResults div.learn div.question4 h2.none').show();
                $('div#bmiResults div.learn div.question4 h2.one').hide();
                $('div#bmiResults div.learn div.question4 h2.two').hide();
                $('div#bmiResults div.learn div.question4 h2.multiple').hide();
                $('div#bmiResults div.learn div.question4 p.none').show();
                $('div#bmiResults div.learn div.question4 p.one').hide();
                $('div#bmiResults div.learn div.question4 p.two').hide();
                $('div#bmiResults div.learn div.question4 p.multiple').hide();
                //$('div#bmiResults div.learn div.question4 h2.one').html(headerText);
            } else if (checkValNum == 1) {
                $('div#bmiResults div.learn div.question4 h2.none').hide();
                $('div#bmiResults div.learn div.question4 h2.one').show();
                $('div#bmiResults div.learn div.question4 h2.two').hide();
                $('div#bmiResults div.learn div.question4 h2.multiple').hide();
                $('div#bmiResults div.learn div.question4 p.none').hide();
                $('div#bmiResults div.learn div.question4 p.one').show();
                $('div#bmiResults div.learn div.question4 p.two').hide();
                $('div#bmiResults div.learn div.question4 p.multiple').hide();
                $('div#bmiResults div.learn div.question4 h2.one').html(headerText);
            } else if (checkValNum == 2) {
                $('div#bmiResults div.learn div.question4 h2.none').hide();
                $('div#bmiResults div.learn div.question4 h2.one').hide();
                $('div#bmiResults div.learn div.question4 h2.two').show();
                $('div#bmiResults div.learn div.question4 h2.multiple').hide();
                $('div#bmiResults div.learn div.question4 p.none').hide();
                $('div#bmiResults div.learn div.question4 p.one').hide();
                $('div#bmiResults div.learn div.question4 p.two').show();
                $('div#bmiResults div.learn div.question4 p.multiple').hide();
                $('div#bmiResults div.learn div.question4 h2.one').html(headerText);
            } else if (checkValNum > 2) {
                $('div#bmiResults div.learn div.question4 h2.none').hide();
                $('div#bmiResults div.learn div.question4 h2.one').hide();
                $('div#bmiResults div.learn div.question4 h2.two').hide();
                $('div#bmiResults div.learn div.question4 h2.multiple').show();
                $('div#bmiResults div.learn div.question4 p.none').hide();
                $('div#bmiResults div.learn div.question4 p.one').hide();
                $('div#bmiResults div.learn div.question4 p.two').hide();
                $('div#bmiResults div.learn div.question4 p.multiple').show();
                $('div#bmiResults div.learn div.question4 h2.one').html(headerText);
            } else {
                //error
            }



            //question 5
            //alert(q5);
            if (q5 == 'yes') {
                $('div#bmiResults div.learn div.question5 div.yes').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q5', 'CAN\'T FUNCTION');
            } else if (q5 == 'no') {
                $('div#bmiResults div.learn div.question5 div.no').show(); ;
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q5', 'CAN FUNCTION');
            } else {
                //error
            }

            //question 6
            //alert(q6);
            if (q6 == 'yes') {
                $('div#bmiResults div.learn div.question6 div.yes').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q6', 'FRUSTRATED');
            } else if (q6 == 'no') {
                $('div#bmiResults div.learn div.question6 div.no').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q6', 'NOT FRUSTRATED');
            } else if (q6 == 'not made serious attempt') {
                $('div#bmiResults div.learn div.question6 div.no').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q6', 'NO SERIOUS ATTEMPTS');
            } else {
                //error
            }

            //question7
            //alert(q7);
            if (q7 == 'Private/Employer-provided') {
                $('div#bmiResults div.learn div.question7 div.private-employer-provided').show();
                $('div#bmiResults div.action div.insurance div.private-employer-provided').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q7', 'INSURANCE\: Private\/Employer\-provided')
            } else if (q7 == 'Medicare') {
                $('div#bmiResults div.learn div.question7 div.medicare').show();
                $('div#bmiResults div.action div.insurance div.medicare').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q7', 'INSURANCE\: Medicare')
            } else if (q7 == 'Medicaid') {
                $('div#bmiResults div.learn div.question7 div.medicaid').show();
                $('div#bmiResults div.action div.insurance div.medicaid').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q7', 'INSURANCE\: Medicaid')
            } else if (q7 == 'Other type') {
                $('div#bmiResults div.learn div.question7 div.other').show();
                $('div#bmiResults div.action div.insurance div.other').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q7', 'INSURANCE\: Other type')
            } else if (q7 == 'I do not have health insurance') {
                $('div#bmiResults div.learn div.question7 div.none').show();
                $('div#bmiResults div.action div.insurance div.none').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q7', 'INSURANCE\: I don\'t have health insurance')
            } else if (q7 == 'I\'m not sure') {
                $('div#bmiResults div.learn div.question7 div.unsure').show();
                $('div#bmiResults div.action div.insurance div.unsure').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q7', 'INSURANCE\: I\'m not sure')
            } else {
                //
            }

            //question8
            //alert(q8);
            if (q8 == '') {
                //do nothing
            } else {
                $('div#bmiResults div.learn div.question8 div.healthy-weight').show();
                
                pageTracker._trackEvent('B4D PAT', 'Results Screen\:Q8', 'ENJOY: ' + q8)
            }

            //results
            //alert(bmi);
            //alert(checkValNum);
            //alert(q5);
            //alert(q6);

            var status = "";

            if (bmi < 30 || (bmi >= 30 && bmi <= 34 && checkValNum == 0)) {
                pageTracker._trackEvent('B4D PAT', 'Results Screen', 'RESULT\: not a candidate');
                status = 'not candidate';
                $('div#bmiResults div.results div.not').show();
                $('div#bmiResults div.results div.possible').hide();
                $('div#bmiResults div.results div.yes').hide();
                $('div#bmiResults div.results div.strong').hide();
                //header
                $('div.bmiNav h2.not').show();
                $('div.bmiNav h2.possible').hide();
                $('div.bmiNav h2.yes').hide();
                $('div.bmiNav h2.strong').hide();
            } else if ((bmi >= 30 && bmi <= 34 && checkValNum >= 1) || (bmi >= 35 && bmi <= 40)) {
                pageTracker._trackEvent('B4D PAT', 'Results Screen', 'RESULT\: possible candidate');
                status = 'possible candidate';
                $('div#bmiResults div.results div.not').hide();
                $('div#bmiResults div.results div.possible').show();
                $('div#bmiResults div.results div.yes').hide();
                $('div#bmiResults div.results div.strong').hide();
                //header
                $('div.bmiNav h2.not').hide();
                $('div.bmiNav h2.possible').show();
                $('div.bmiNav h2.yes').hide();
                $('div.bmiNav h2.strong').hide();
            } else if ((bmi > 40 && checkValNum == 0 && (q5 == 'no' || q6 == 'no')) || (bmi >= 35 && bmi <= 40 && checkValNum >= 1 && q5 == 'no' && q6 == 'no')) {
                pageTracker._trackEvent('B4D PAT', 'Results Screen', 'RESULT\: candidate');
                status = 'candidate';
                $('div#bmiResults div.results div.not').hide();
                $('div#bmiResults div.results div.possible').hide();
                $('div#bmiResults div.results div.yes').show();
                $('div#bmiResults div.results div.strong').hide();
                //header
                $('div.bmiNav h2.not').hide();
                $('div.bmiNav h2.possible').hide();
                $('div.bmiNav h2.yes').show();
                $('div.bmiNav h2.strong').hide();
            } else if (((bmi > 40 && checkValNum >= 1) || (bmi > 40 && (q5 == 'yes' || q6 == 'yes'))) || (bmi >= 35 && bmi <= 40 && checkValNum >= 1 && (q5 == 'yes' || q6 == 'yes'))) {
                pageTracker._trackEvent('B4D PAT', 'Results Screen', 'RESULT\: strong candidate');
                status = 'strong candidate';
                $('div#bmiResults div.results div.not').hide();
                $('div#bmiResults div.results div.possible').hide();
                $('div#bmiResults div.results div.yes').hide();
                $('div#bmiResults div.results div.strong').show();
                //header
                $('div.bmiNav h2.not').hide();
                $('div.bmiNav h2.possible').hide();
                $('div.bmiNav h2.yes').hide();
                $('div.bmiNav h2.strong').show();
            } else {
                alert('error');
            }

            //action
            //alert(status);
            if (status == 'not candidate') {
                $('div#bmiResults div.action div.not').show();
                $('div#bmiResults div.action div.possible').hide();
                $('div#bmiResults div.action div.yes').hide();
                $('div#bmiResults div.action div.strong').hide();
            } else if (status == 'possible candidate') {
                $('div#bmiResults div.action div.not').hide();
                $('div#bmiResults div.action div.possible').show();
                $('div#bmiResults div.action div.yes').hide();
                $('div#bmiResults div.action div.strong').hide();
            } else if (status == 'candidate') {
                $('div#bmiResults div.action div.not').hide();
                $('div#bmiResults div.action div.possible').hide();
                $('div#bmiResults div.action div.yes').show();
                $('div#bmiResults div.action div.strong').hide();
            } else if (status == 'strong candidate') {
                $('div#bmiResults div.action div.not').hide();
                $('div#bmiResults div.action div.possible').hide();
                $('div#bmiResults div.action div.yes').hide();
                $('div#bmiResults div.action div.strong').show();
            } else {
                //error
            }



        }
    });

    //bmiNav
    $('div#bmiResults div.bmiResultTab div.bmiNav ul li.item1 a').click(function() {
        //$('div#bmiResults div.learn').hide('fast');
        //$('div#bmiResults div.action').hide('fast');
        //$('div#bmiResults div.results').show('fast');
        
        pageTracker._trackEvent('B4D PAT', 'Result View Change\: Your Results', 'YOUR RESULTS');
        $('div#bmiResults div.learn').hide();
        $('div#bmiResults div.action').hide();
        $('div#bmiResults div.results').show('fast');

        $('div#bmiResults div.bmiNav ul li a').removeClass('active');
        $('div#bmiResults div.bmiNav ul li.item1 a').addClass('active');
    });
    $('div#bmiResults div.bmiResultTab div.bmiNav ul li.item2 a').click(function() {
        //$('div#bmiResults div.results').hide('fast');
        //$('div#bmiResults div.action').hide('fast');
        //$('div#bmiResults div.learn').show('fast');

        
        pageTracker._trackEvent('B4D PAT', 'Result View Change\: Learn More', 'LEARN MORE');
        $('div#bmiResults div.results').hide();
        $('div#bmiResults div.action').hide();
        $('div#bmiResults div.learn').show('fast');

        $('div#bmiResults div.bmiNav ul li a').removeClass('active');
        $('div#bmiResults div.bmiNav ul li.item2 a').addClass('active');
    });
    $('div#bmiResults div.bmiResultTab div.bmiNav ul li.item3 a').click(function() {
        //$('div#bmiResults div.results').hide('fast');
        //$('div#bmiResults div.learn').hide('fast');
        //$('div#bmiResults div.action').show('fast');
        
        pageTracker._trackEvent('B4D PAT', 'Result View Change: Next Steps', 'NEXT STEPS');
        $('div#bmiResults div.results').hide();
        $('div#bmiResults div.learn').hide();
        $('div#bmiResults div.action').show('fast');

        $('div#bmiResults div.bmiNav ul li a').removeClass('active');
        $('div#bmiResults div.bmiNav ul li.item3 a').addClass('active');
    });


    $('div#formDiv div#bmiResults div.qrecalc a.recalc').click(function() {
        
        pageTracker._trackEvent('B4D PAT', 'Recalculate Eligibility', 'Recalculate');
        $('div#formDiv form').validate().resetForm();
        $('div#bmiResults').hide('fast');
        $('div#bmiForm').show('slow');
        $('div#bmiForm form.bmiForm1').show('slow');
        $('div#bmiResults div.bmiNav ul li.item a').removeClass('active');
    });


});
		

