$(document).ready(function () {
    $("#add-policy-step1").submit(function (e) {
        e.preventDefault();
        $("#next-step2").text("Please wait...").attr('disabled', true).css('backgroundColor', 'gray');

        var result;
        if($('#client-list').val() == ""){
            result = 'Please Select a Client to Proceed';
        }else if($('#insured-name').val() == ""){
            result = 'Please Enter Insured Name to Proceed';
        }else{
            result = 'Good';
        }

        if(result != 'Good'){
            alert(result);
            $("#next-step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');

        }else {
            var data1 = $('#add-policy-step1').serializeArray();
            $.ajax({
                method: 'POST',
                url: '/add-policy',
                data: data1,
                success: function (res) {
                    alert(res);
                    $("#next-step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    /*if(res == 'error adding policy'){
                        alert('An Error has Occurred while adding Policy. Please Try again!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                        location.reload();
                    }else if(res == 'empty fields'){
                        alert('Please fill out all fields marked in Red Asterisk');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }else if(res == 'empty client name'){
                        alert('Please fill out Client Name(s)!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }else if(res == 'empty id_no'){
                        alert('Please fill out ID/Passport Field!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }else if(res == 'empty kra'){
                        alert('Please fill out KRA PIN details!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }else if(res == 'empty phone'){
                        alert('Please fill out the Phone Number Field!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    } else if(res == 'empty email'){
                        alert('Please fill out the Email Address Field!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    } else if(res == 'client exists'){
                        alert('Client Already exists! Check Email, ID No. or KRA PIN Entered');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    } else if(res == 'stamp error'){
                        alert('System is Unable to add this Client. Please Try Again!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    } else if(res == 'incomplete at step1'){
                        alert('Client has an Incomplete Policy at Step 1. Please us it or Delete it to Debit a new one!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    } else if(res == 'error adding client'){
                        alert('System encountered and error while adding Client. Please Try Again!');
                        $("#next_step2").text("Next Step >>").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }
                    else{
                        $('#step1').hide();
                        $('#step2').show();
                        $('#steps2').addClass('active');
                        $('#step3').hide();

                        //we update the url with the new inserted value
                        history.pushState({res}, null, '?id='+ res);
                    }*/
                    return false;
                }
            });
        }




    });
});
