/// <reference path="bloodhound.min.js" />



$(document).ready(function () {



    var vm = {};
    var states = [];
    
    $.validator.addMethod("validCountry", function () {
        var id = $('input[name="Address.CountryId"]').val();
        return id && id !== 0;
    }, "Please select a valid Country from List.");

    
    //var countries = new Bloodhound({
    //    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
    //    queryTokenizer: Bloodhound.tokenizers.whitespace,
    //    remote: {
    //        url: '/Countries/GetAllCountries?query=%QUERY',
    //        wildcard: '%QUERY'
    //    }
    //});

    var countries = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch: {
            url: '/Countries/GetCountries'           
        }
    });

    $('#CountryId').typeahead({
        minLength: 1,
        highlight: true
    }, {
        name: 'countries',
        display: 'Name',
        source: countries
    }).on("typeahead:select", function (e, country) {
        $('input[name="Address.CountryId"]').val(country.Id);
        vm.countryId = country.Id;
    });

    $('#Location').typeahead({
        minLength: 2,
        highlight: true
    }, {
        name: 'countries',
        display: 'Name',
        source: countries
    }).on("typeahead:select", function (e, country) {
        $('input[name="Address.CountryId"]').val(country.Id);
        vm.countryId = country.Id;
    });

    var cities = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/Countries/GetAllCitiesByCountryId?query=%QUERY',
            wildcard: '%QUERY',
            replace: function (url, uriEncodedQuery) {
                var val = $('input[name="Address.CountryId"]').val();
                if (!val) return url.replace("%QUERY", uriEncodedQuery);
                return url.replace("%QUERY", uriEncodedQuery) + '&param=' + encodeURIComponent(val);
            }
        }
    });

    $('input[name="Address.CityId"]').typeahead({
        minLength: 2,
        highlight: true
    }, {
        name: 'cities',
        display: 'Name',
        source: cities
    }).on("typeahead:select", function (e, city) {
        $('input[name="Address.CityId"]').val(city.Name);

    });


    var Sendercities = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/Countries/GetAllCitiesByCountryId?query=%QUERY',
            wildcard: '%QUERY',
            replace: function (url, uriEncodedQuery) {
                var val = $('#SenderAddress_CountryId').val();
                if (!val) return url.replace("%QUERY", uriEncodedQuery);
                return url.replace("%QUERY", uriEncodedQuery) + '&param=' + encodeURIComponent(val);
            }
        }
    });

    $('#SenderAddress_CityId').typeahead({
        minLength: 2,
        highlight: true
    }, {
        name: 'Sendercities',
        display: 'Name',
        source: Sendercities
    }).on("typeahead:select", function (e, city) {
        $('#SenderAddress_CityId').val(city.Name);

    });

    var Receivercities = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/Countries/GetAllCitiesByCountryId?query=%QUERY',
            wildcard: '%QUERY',
            replace: function (url, uriEncodedQuery) {
                var val = $('#ReceiverAddress_CountryId').val();
                if (!val) return url.replace("%QUERY", uriEncodedQuery);
                return url.replace("%QUERY", uriEncodedQuery) + '&param=' + encodeURIComponent(val);
            }
        }
    });


    $('#ReceiverAddress_CityId').typeahead({
        minLength: 2,
        highlight: true
    }, {
        name: 'Receivercities',
        display: 'Name',
        source: Receivercities
    }).on("typeahead:select", function (e, city) {
        $('#ReceiverAddress_CityId').val(city.Name);

    });
    //$('input[name="Address.ZipCode"]').keypress(function (evt) {
    //	var charCode = (evt.which) ? evt.which : evt.keyCode;
    //	if (charCode > 31 && (charCode < 48 || charCode > 57))
    //		return false;
    //	return true;
    //});

    
   

    //var states = new Bloodhound({
    //    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
    //    queryTokenizer: Bloodhound.tokenizers.whitespace,
    //    remote: {
    //        url: '/Countries/GetAllStatesByCountryId?query=%QUERY',
    //        wildcard: '%QUERY',
    //        replace: function (url, uriEncodedQuery) {
    //            var val = $('#ReceiverAddress_CountryId').val();
    //            if (!val) return url.replace("%QUERY", uriEncodedQuery);
    //            return url.replace("%QUERY", uriEncodedQuery) + '&param=' + encodeURIComponent(val);
    //        }
    //    }
    //});


    //$('#ReceiverAddress_State').typeahead({
    //    minLength: 3,
    //    highlight: true,
    //    limit:8
    //}, {
    //    name: 'states',
    //    display: 'Name',
    //    source: states
    //});


});


//(function (window, document, $) {
//    'use strict';

//    // Date dd/mm/yyyy
//    $('.date-formatter').formatter({
//        'pattern': "{{99}}/{{99}}/{{9999}}"
//    });

//    //Phone mask
//    $('.phone-formatter').formatter({
//        'pattern': "({{999}}) {{999}}-{{9999}}"
//    });

//    // Another Date mm-dd-yyyy
//    $('.international-formatter').formatter({
//        'pattern': "+{{9}}({{999}}){{999}}-{{9999}}"
//    });

//    //Phone with extra
//    $('.xphone-formatter').formatter({
//        'pattern': "({{999}}) {{999}}-{{9999}} / {{a999999}}"
//    });

//    // Purchase Order
//    $('.purchase-formatter').formatter({
//        'pattern': "{{aaaa}} {{9999}}-{{****}}"
//    });

//    // Credit Card Number
//    $('.cc-formatter').formatter({
//        'pattern': "{{9999}} {{9999}} {{9999}} {{9999}}"
//    });

//    // SSN
//    $('.ssn-formatter').formatter({
//        'pattern': "{{999}}-{{99}}-{{9999}}"
//    });

//    // ISBN
//    $('.isbn-formatter').formatter({
//        'pattern': "{{999}}-{{99}}-{{999}}-{{9999}}-{{9}}"
//    });

//    // Currency in USD
//    $('.currency-usformatter').formatter({
//        'pattern': "${{9999}}"
//    });

//    $('.currency-pkformatter').formatter({
//        'pattern': "${{999999999}}"
//    });


//    // Percentage
//    $('.percentage-formatter').formatter({
//        'pattern': "{{99}}%"
//    });

//    // only Number
//    $('.number-formatter').formatter({
//        'pattern': "{{999999999}}"
//    });

//    $('.decimal-formatter').formatter({
//        'pattern': "{{999999999}}.{{99}}"
//    });

//})(window, document, jQuery);