﻿function isNumeric(form_value) {
    if (form_value.match(/^\d+$/) == null)
        return false;
    else
        return true;
}

var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

$(document).ready(function() {

    textboxes = $(".zip");
    getdir = $(".getaddress");
    catering = $(".catering");

    if ($.browser.mozilla) {
        $(textboxes).keypress(checkForEnter);
        $(getdir).keypress(checkForEnterDir);
        $(catering).keypress(checkForEnterCat);
    } else {
        $(textboxes).keydown(checkForEnter);
        $(getdir).keydown(checkForEnterDir);
        $(catering).keypress(checkForEnterCat);
    }

    function checkForEnterDir(event) {
        if (event.keyCode == 13) {
            $('#getdirections').click();
            event.preventDefault();
            return false;
        }
    }

    function checkForEnterCat(event) {
        if (event.keyCode == 13) {
            $('#btnjoincatering').click();
            event.preventDefault();
            return false;
        }
    }

    function checkForEnter(event) {
        if (event.keyCode == 13) {
            $('#btnzip').click();
            event.preventDefault();
            return false;
        }
    }

    $('#gototop').click(function() {
        $('html,body').animate({ scrollTop: 0 }, 1000);
    });

    $('#btnzip').click(function() {
        var z = "";
        z = $('.zip').val();
        if ($('#btnzip').hasClass('home')) {
            location.href = '/location/home/ziplookup/' + Url.encode(z);

        } else {
            location.href = '/location/home/ziplookup/' + Url.encode(z);
        }
        /*
        if (z.length == 5 && isNumeric(z)) {
        location.href = '/location/ziplookup/' + Url.encode(z);
        }
        else if ((z.length >= 5) && (z.indexOf(",") > 0)) {
        location.href = '/location/ziplookup/' + Url.encode(z);
        } else {
        alert("Please enter a valid zip code or city, state");

        } */

        return false;
    });

    $('.myperm input').change(function() {

        if ($('.myperm input').is(':checked')) {

            $.get('/location/setlocation.aspx?usecurrent=1&perm=1');
        } else {
            $.get('/location/setlocation.aspx?remove=1');
        }
    });

    $('#getdirections').click(function() {

        var sadd = $('.getaddress').val();
        var ddr = $('#lat').text() + "," + $('#long').text();

        window.open("http://maps.google.com/maps?daddr=" + ddr + "&saddr=" + sadd + "&f=l&hl=en&sll=30,-96&sspn=0.10228,0.216465&ie=UTF8&z=9&om=1");


    });
    $("input,textarea").each(function(i) {


        var obj = $('#' + this.id);

        if (obj.hasClass('defaulttext')) {

            if (obj.val() == '') {
                obj.val(obj.attr('title'));
            }

            obj.focus(function() {
                if ($(this).val() == $(this).attr("title")) {
                    $(this).val("");
                }
            });

        }
    });

});