﻿/// <reference path="~/Javascript/Utilities/jquery.js" />
function getIsLoggedIn() {
    return $("#ac_auth a[href=/Logout]").length !== 0;
}
$(function() {
    //non-constant url
    var quoteImmSearch = '/health-insurance/quote/search';
    var quoteMedicareSearch = '/medicare/quote/search';
    var quoteDentalSearch = '/dental-insurance/quote/search';

    //TODO: Vision Update
    var quoteVisionSearch = '/products/vision'//'/vision-insurance/quote/search';

    $('#txtZipCode').numeric();
    $('#txtNumberPeople').numeric();

    if (getIsLoggedIn() || $('input[name=insuranceType]:checked').val() !== "Imm") {
        $('div[id*=PeopleCoverDiv]').hide();
    }

    var dte = new Date().getFullYear();
    $("#CurrentYear").html(dte);
    $("#NextYear").html(dte + 1);

    $('input[name=insuranceType]').click(function() {
        if ($(this).val() == "Imm" && !getIsLoggedIn())
            $('div[id*=PeopleCoverDiv]').show();
        else {
            $('div[id*=PeopleCoverDiv]').hide();
        }

        if ($('div[id^=errorSummary]:visible').length > 0) {
            $("form").valid();
        }
    });

    $.validator.addMethod("zipcode", function(param, element) {
        return !this.optional(element); //|| $("#selectCounty")[0].options.length != 0;
    }, "");
    //    $.validator.addMethod("county", function(param, element) {
    //        return ($('#selectCounty').parent().is(':visible')) ? $("#selectCounty")[0].selectedIndex != 0 : true;
    //    }, "");

    $('form').validate({
        rules: {
            "locality.ZipCode": { "required": true, "zipcode": true },
            "NumberOfPeople": { required: "div[id*=PeopleCoverDiv]:visible" }
            //            "SelectedCountyName": { "county": true }
        },
        messages: {
            "locality.ZipCode": { "required": "InvalidZipCode", "zipcode": "InvalidZipCode" },
            "NumberOfPeople": "NumberOfPeopleRequired"
            //            "SelectedCountyName": "InvalidCounty"
        },
        unhighlight: function(element, errorClass) {
            $(element).removeClass(errorClass);
            $(element.form).find("label[for=" + element.id + "]").removeClass("errorLabel");
        },
        onfocusout: false,
        onkeyup: false,
        showErrors: function(errorMap, errorList) {
            if (errorList.length > 0) {
                $('#errorBody ul li').hide();
                for (var i = 0, elements = this.validElements(); elements[i]; i++) {
                    this.settings.unhighlight.call(this, elements[i], this.settings.errorClass);
                }
                for (var i = 0; i < errorList.length; i++) {
                    var error = errorList[i];
                    $(error.element).addClass("error");
                    if (error.message == "NumberOfPeopleRequired") {
                        $("label[for=" + error.element.id + "]").addClass("errorLabel");
                        $('#liNumberOfPeople').show();
                    }
                    //                    else if (error.message == "InvalidCounty") {
                    //                        $('label[for="selectCounty"]').addClass("errorLabel");
                    //                        $('#liSelectCounty').show();
                    //                    }
                    else {
                        $('label[for="txtZipCode"]').addClass("errorLabel");
                        $('#liIncompleteError').show();
                    }
                }
                $('div[id^=errorSummary]').show();
                $('input:text.error:first').focus();
            }
        }
    });
    $('#submitImm').click(function() {
        if ($('form').valid()) {
            var zipCodeValue = $('input#txtZipCode').val();
            var numPeopleValue = $('#txtNumberPeople').val();
//            var countyIndex = $('#selectCounty')[0].selectedIndex;
            location.replace("https://" + location.host + quoteImmSearch + "?zipcode=" + zipCodeValue + "&countyindex=" + countyIndex + "&pcount=" + numPeopleValue);
        } else {
            $('.error:first').focus();
        }
    });
    $('#submitImmMedicare').click(function() {
        if ($('form').valid()) {
            var zipCodeValue = $('input#txtZipCode').val();
            var numPeopleValue = $('#txtNumberPeople').val();
//            var countyIndex = $('#selectCounty')[0].selectedIndex;
//            if (countyIndex > 0) { countyIndex = countyIndex - 1; }
            if ($('input[value=Imm]').is(':checked'))
                location.replace("https://" + location.host + quoteImmSearch + "?zipcode=" + zipCodeValue + "&pcount=" + numPeopleValue); //"&countyindex=" + countyIndex + 
            else
                location.replace("https://" + location.host + quoteMedicareSearch + "?zipcode=" + zipCodeValue); // + "&countyindex=" + countyIndex
        } else {
            $('.error:first').focus();
        }
    });
    $('#submitMedicare').live("click", function() {
        if ($('form').valid()) {
            var zipCodeValue = $('input#txtZipCode').val();
//            var countyIndex = $('#selectCounty')[0].selectedIndex;
            location.replace("https://" + location.host + quoteMedicareSearch + "?zipcode=" + zipCodeValue); //+ "&countyindex=" + countyIndex
        } else {
            $('.error:first').focus();
        }
    });

    $('#submitDental').live("click", function() {
        if ($('form').valid()) {
            var zipCodeValue = $('input#txtZipCode').val();
//            var countyIndex = $('#selectCounty')[0].selectedIndex;
            location.replace("https://" + location.host + quoteDentalSearch + "?zipcode=" + zipCodeValue); //+ "&countyindex=" + countyIndex);
        } else {
            $('.error:first').focus();
        }
    });

    $('#submitVision').live("click", function() {
        if ($('form').valid()) {
            var zipCodeValue = $('input#txtZipCode').val();
//            var countyIndex = $('#selectCounty')[0].selectedIndex;
            location.replace("https://" + location.host + quoteVisionSearch + "?zipcode=" + zipCodeValue); // + "&countyindex=" + countyIndex);
        } else {
            $('.error:first').focus();
        }
    });
});