//the global page number is calculated in memberSession snipet

$(document).ready(function(){

    $(".jsErrors").hide();
    
    $(".page_1").append('<div class="navigation"><div class="left"><span></span></div><div class="right"><span class="go_next_1 goto_change">Next</span></div></div>');
    
    if (page_no == 2) { //hide previous button when the default state is page 2
        $(".page_2").append('<div class="navigation"><div class="left"><span class="go_previous goto_change"></span></div><div class="right"><span class="go_next_2 goto_change">Next</span></div></div>');
    }
    else {
        $(".page_2").append('<div class="navigation"><div class="left"><span class="go_previous goto_change">Previous</span></div><div class="right"><span class="go_next_2 goto_change">Next</span></div></div>');
    }
    $(".page_3").append('<div class="navigation"><div class="left"><span class="go_previous goto_change">Previous</span></div><div class="right"><span class="go_next_3 goto_change">Next</span></div></div>');
    $(".page_4").append('<div class="navigation"><div class="left"><span class="go_previous goto_change">Previous</span></div><div class="right"><span class="go_next_4 goto_change">Next</span></div></div>');
    $(".page_5").append('<div class="navigation"><div class="left"><span class="go_previous goto_change">Previous</span></div><div class="right"><span class="go_next_5 goto_change">Next</span></div></div>');
    $(".page_6").append('<div class="navigation"><div class="left"><span class="go_previous goto_change">Previous</span></div><div class="right"><span></span></div></div>');
    
    //PAGE 1
    $(".go_next_1").click(function(){
        resultStep1 = validateStep1($("input[name='member']:checked").val());
        
        if (resultStep1 == "") {
            page_no++;
            hideAllPages();
            $(".jsErrors").empty().hide();
            $(".page_" + page_no).show();
            $("#formContent").hide();//hide main content text
        }
        else {
            $(".jsErrors").show();
            $(".errors").hide();//hide php errors in case they show
            $(".jsErrors").html(errorMainMessage + resultStep1);
        }
    });
    
    //PAGE 2
    $(".go_next_2").click(function(){
        resultStep2 = validateStep2();
        
        if (resultStep2 == "") {
            page_no++;
            hideAllPages();
            $(".jsErrors").empty().hide();
            $(".page_" + page_no).show();
            $("#formContent").hide();//hide main content text
        }
        else {
            $(".jsErrors").show();
            $(".errors").hide();//hide php errors in case they show
            $(".jsErrors").html(errorMainMessage + resultStep2);
        }
    });
    
    $(".go_next_3").click(function(){
        resultStep3 = validateStep3();
        
        if (resultStep3 == "") {
            page_no++;
            hideAllPages();
            $(".jsErrors").empty().hide();
            $(".page_" + page_no).show();
            $("#formContent").hide();//hide main content text
        }
        else {
            $(".jsErrors").show();
            $(".errors").hide();//hide php errors in case they show
            $(".jsErrors").html(errorMainMessage + resultStep3);
        }
    });
    
    $(".go_next_4").click(function(){
        resultStep4 = validateStep4();
        
        if (resultStep4 == "") {
            page_no++;
            hideAllPages();
            $(".jsErrors").empty().hide();
            $(".page_" + page_no).show();
            $("#formContent").hide();//hide main content text
        }
        else {
            $(".jsErrors").show();
            $(".errors").hide();//hide php errors in case they show
            $(".jsErrors").html(errorMainMessage + resultStep4);
        }
    });
    
    
    $(".go_next_5").click(function(){
        resultStep5 = validateStep5();
        
        if (resultStep5 == "") {
            page_no++;
            hideAllPages();
            $(".jsErrors").empty().hide();
            $(".page_" + page_no).show();
            $("#formContent").hide();//hide main content text
        }
        else {
            $(".jsErrors").show();
            $(".errors").hide();//hide php errors in case they show
            $(".jsErrors").html(errorMainMessage + resultStep5);
        }
    });
    //PAGE 6 - Agreeement
    $("input.submitApp").click(function(){
        resultStep6 = validateStep6();
        $('form').submit(function(){
            if (resultStep6 == "") {
                return true;
            }
            else {
                $(".jsErrors").show();
                $(".errors").hide();//hide php errors in case they show
                $(".jsErrors").html(errorMainMessage + resultStep6);
                return false;
            }
        });
    });
    
    $(".go_previous").click(function(){
        page_no--;
        hideAllPages();
        $(".jsErrors").empty().hide();
        $(".page_" + page_no).show();
        if(page_no == 1) {
            $("#formContent").show();//hide main content text
        }
    });
});


function validateStep2(){
    var validStep = "";
    if (!something($("input[name='howLongAddress']").val())) {
        validStep += "- Please enter the length of time you have resided at your current address<br/>";
    }
    if (!something($("input[name='reason']").val())) {
        validStep += "- Please enter your reason for applying for a mortgage/loan<br/>";
    }
    if (!something($("input[name='amount']").val())) {
        validStep += "- Please enter the mortgage/loan amount you are applying for<br/>";
    }
    if (!something($("input[name='lenghtEmployment']").val())) {
        validStep += "- Please enter your length of employment<br/>";
    }
    if (!something($("input[name='salary']").val())) {
        validStep += "- Please enter your salary<br/>";
    }
    if (!something($("input[name='maritalStatus']").val())) {
        validStep += "- Please enter your marital status<br/>";
    }
    return validStep;
}
function validateStep3(){
    var validStep = "";
    return validStep;
}
function validateStep4(){
    var validStep = "";
    return validStep;
}
function validateStep5(){
    var validStep = "";
    return validStep;
}
function validateStep6(){
    var validStep = "";
    
    if (!something( $("input[name='bankruptcy']:checked").val() )) {
        validStep += "- Please indicate whether or not you have filed for bankruptcy or consumer proposal<br/>";
    }
    
    if($("input[name='bankruptcy']:checked").val() == 'Yes' && $("input[name='dischargeDate']").val() == ''){
        validStep += "- Please enter the discharge date<br/>";
    }
    
    if (!$("input[name='acknowledge']").attr('checked')) {
        validStep += " - Please accept our agreement<br/>";
    }
    
    return validStep;
}
