﻿COUNTRY = {
    US: 69,
    PH: 52
};

function InitCountries() {

    var $countryControl = $("#CountryId");
    var $stateControl = $("#StateId");
    var $provinceControl = $("#ProvinceId");
    var $cityControl = $("#CityId");

    

    //{infix:false,addEmphasis: true}
    $countryControl.ufd({ infix: false });
    $stateControl.ufd({ infix: false });
    $provinceControl.ufd({ infix: false });
    $cityControl.ufd({ infix: false });

    $countryControl.ufd("enable");

    var UFDprovince = $provinceControl.parents("span.ufd");
    var txtProvince = $("#ProvinceName").parents("div.textbox_div");

    var countryChange = function (event) {

        var coursemethod = $("#MethodID");

        if (coursemethod.length > 0) {
            if (coursemethod.val() == "2") {
                return;
            }
        }
        var selectedItem = $("#CountryId > option:selected");
        countryId = selectedItem.attr("value");
        if (countryId == COUNTRY.US /* USA */) {
            $.post("/Controls/States/List", null, function (data) {
                var items = "<option value='0'>Select State</option>";
                $.each(data, function (i, state) {
                    items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                });

                $cityControl.html("<option>Not Applicable</option>");
                $cityControl.removeAttr("disabled");
                $cityControl.attr("disabled", "true");

                $provinceControl.html("<option value='0'>Select County</option>");
                $provinceControl.removeAttr("disabled");
                $provinceControl.attr("disabled", "true");

                $stateControl.removeAttr("disabled");
                $stateControl.html(items);

                $stateControl.ufd("changeOptions");
                $provinceControl.ufd("changeOptions");
                $cityControl.ufd("changeOptions");

                UFDprovince.show();
                txtProvince.hide();
                $("th", $("#ProvinceId").parents("tr")).text("County");
            }, 'json');
        }
        else if (countryId == COUNTRY.PH /* Philippines */) {
            $.post("/Controls/Provinces/List/" + countryId + "/0", null, function (data) {
                var items = "<option value='0'>Select Province</option>";
                $.each(data, function (i, province) {
                    items += "<option value='" + province.Value + "'>" + province.Text + "</option>";
                });

                $cityControl.html("<option>Not Applicable</option>");
                $cityControl.attr("disabled", "true");

                $stateControl.html("<option>Not Applicable</option>");
                $stateControl.attr("disabled", "true");

                $provinceControl.removeAttr("disabled");
                $provinceControl.html(items);
                $provinceControl.show();

                $stateControl.ufd("changeOptions");
                $provinceControl.ufd("changeOptions");
                $cityControl.ufd("changeOptions");

                UFDprovince.show();
                txtProvince.hide();
                $("th", $("#ProvinceId").parents("tr")).text("Province");
            }, 'json');
        }
        else {

            var stateId = $("#ProvinceId > option:selected").attr("value");
            countryId = $("#CountryId > option:selected").attr("value");
            if (countryId > 0) {
                $.post("/Controls/Cities/List/" + countryId + "/0", null, function (data) {
                    var items = "<option value='0'>Select City</option>";
                    $.each(data, function (i, city) {
                        items += "<option value='" + city.Value + "'>" + city.Text + "</option>";
                    });

                    $cityControl.removeAttr("disabled");
                    $cityControl.html(items);
                    $cityControl.ufd("changeOptions");

                }, 'json');
            }
            else {
                $cityControl.html("<option>Not Applicable</option>");
                $cityControl.removeAttr("disabled");
                $cityControl.attr("disabled", "true");

                $cityControl.ufd("changeOptions");
            }

            $stateControl.html("<option>Not Applicable</option>");
            $stateControl.attr("disabled", "true");

            $provinceControl.html("<option>Not Applicable</option>");
            $provinceControl.attr("disabled", "true");


            $stateControl.ufd("changeOptions");
            $provinceControl.ufd("changeOptions");

            if (txtProvince.length > 0) {
                UFDprovince.hide();
                txtProvince.show();
            }

            $("th", $("#ProvinceId").parents("tr")).text("Province");
        }
    };

    var stateChange = function (event) {
        var selectedItem = $("#StateId > option:selected");
        var stateId = selectedItem.attr("value");
        countryId = $("#CountryId > option:selected").attr("value");
        $.post("/Controls/Provinces/List/" + countryId + "/" + stateId, null, function (data) {
            var items = "<option value='0'>Select County</option>";
            $.each(data, function (i, province) {
                items += "<option value='" + province.Value + "'>" + province.Text + "</option>";
            });

            $provinceControl.removeAttr("disabled");
            $provinceControl.html(items);
            $provinceControl.ufd("changeOptions");

        }, 'json');
    };

    var provinceChange = function (event) {
        var provinceId = $("#ProvinceId > option:selected").attr("value");
        countryId = $("#CountryId > option:selected").attr("value");
        if (provinceId > 0) {
            if (countryId == COUNTRY.US || countryId == COUNTRY.PH) {
                $.post("/Controls/Cities/List/" + countryId + "/" + provinceId, null, function (data) {
                    var items = "<option value='0'>Select City</option>";
                    $.each(data, function (i, city) {
                        items += "<option value='" + city.Value + "'>" + city.Text + "</option>";
                    });

                    $cityControl.removeAttr("disabled");
                    $cityControl.html(items);
                    $cityControl.ufd("changeOptions");
                }, 'json');
            }
        }
        else {
            $cityControl.html("<option>Not Applicable</option>");
            $cityControl.removeAttr("disabled");
            $cityControl.attr("disabled", "true");

            $cityControl.ufd("changeOptions");
        }
    };
    if ($.browser.msie) {
        addEvent($countryControl[0], "change", countryChange);
        addEvent($stateControl[0], "change", stateChange);
        addEvent($provinceControl[0], "change", provinceChange);
    }
    else {
        $countryControl.bind("change", countryChange);
        $stateControl.bind("change", stateChange);
        $provinceControl.bind("change", provinceChange);
    }

    var selectedItem = $("#CountryId > option:selected");
    countryId = selectedItem.attr("value");

    if (countryId == COUNTRY.US /* USA */ ||
        countryId == COUNTRY.PH /* Philippines */) {
        UFDprovince.show();
        txtProvince.hide();
    }
    else {
        UFDprovince.hide();
        txtProvince.show();
    }
    //$countryControl.change();
}

$.validator.addMethod("validatecountry", ValidateCountry, "Please select a country");

$.validator.addMethod("validatestate", ValidateState, "Please select a state");

$.validator.addMethod("validateprovince", ValidateProvince, "Please select a province");

$.validator.addMethod("validatecity", ValidateCity, "Please select a city");

function ValidateCountry() {
    var ddl = $("select[name='CountryId']");
    var err = ddl.parents("tr").find('div.error');

    ddl.hide();
    if (ddl.val() <= 0) {
        return false;
    }
    else {
        err.html("");
        return true;
    }
}

function ValidateState() {
    var ddl = $("select[name='StateId']");
    var err = ddl.parents("tr").find('div.error');

    ddl.hide();
    var country = $("select[id='CountryId']");
    if (ddl.val() == 0 && country.val() == COUNTRY.US) {
        return false;
    }
    else {
        err.html("");
        return true;
    }
}

function ValidateProvince() {
    var ddl = $("select[name='ProvinceId']");
    var err = ddl.parents("tr").find('div.error');

    ddl.hide();
    var country = $("select[id='CountryId']");
    if (ddl.val() == 0 && (country.val() == COUNTRY.US || country.val() == COUNTRY.PH)) {
        switch (country.val()) {
            case COUNTRY.US:
                err.html("Please select a county");
                break;
            case COUNTRY.PH:
                err.html("Please select a province");
                break;
        }
        return false;
    }
    else {
        err.html("");
        return true;
    }
}

function ValidateCity() {
    var ddl = $("select[name='CityId']");
    var cityId = ddl.val();
    var err = ddl.parents("tr").find('div.error');
    var countryId = $("select[id='CountryId']").val();
    var provinceId = $("select[id='ProvinceId']").val();
    if (countryId == COUNTRY.US || countryId == COUNTRY.PH) {
        if (provinceId > 0 && cityId <= 0) {
            return false;
        }
    }
    else {
        if (countryId > 0 && cityId <= 0) {
            return false;
        }
    }

    err.html("");
    return true;
}


var addEvent = function (obj, type, fn) {
    if (obj.attachEvent) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function () { obj['e' + type + fn](window.event); }
        obj.attachEvent('on' + type, obj[type + fn]);
    } else
        obj.addEventListener(type, fn, false);
};
