﻿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");

    /*  $('#aut-banner').colorbox({ iframe: true, width: "500", height: "500", maxWidth: "800px", maxHeight: "800px", onComplete: function() {
    $(this).colorbox.resize();
    }
    });*/

    $('#aut-banner').colorbox({ inline: true, href: "#authtml", maxWidth: "500",
        onOpen: function() {
            $.fn.colorbox.init();
            $('#cboxOverlay').bind('click', function() { $.fn.colorbox.close(); });
        },
        onComplete: function() {
            $(this).colorbox.resize();
        }
    });
    $('.pp-modal').colorbox({ iframe: true,
        innerWidth: 920,
        innerHeight: 720,
        transition: 'fade',
        scrolling: false,
        onOpen: function() {
            $('html,body').animate({ scrollTop: 0 }, 0);
            //  $('#cboxTopLeft,#cboxTopCenter,#cboxTopRight,#cboxMiddleLeft,cboxMiddleRight').remove();
            $.fn.colorbox.init();
            $('#cboxOverlay').bind('click', function() { $.fn.colorbox.close(); });
            $('#cboxWrapper > div:eq(0),#cboxWrapper > div:eq(2)').remove();

            $('#cboxWrapper #cboxMiddleLeft, #cboxWrapper #cboxMiddleRight').remove();
            $('#cboxTitle, #cboxClose').remove();
            $('#cboxContent').css('background', 'none repeat scroll 0 0 Transparent');

        },
        OnClose: function() {

        }
    });

    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;
        }
    }

    /* $(document).ready(function() {
    $('.slideshow').cycle({
    fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    speed: 7000
    });
    });
    */

    $('#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;
    });



    /*  if ($.browser.msie) {
    $('.myperm input').click(function() {
            
    if ($('.myperm input').is(':checked')) {
    $.ajax({
    url: '/location/setlocation.aspx?usecurrent=1&perm=1',
    success: function(data) {
                        
    alert('Load was performed.');
    }
    });
                
    } else {
    $.get('/location/setlocation.aspx?remove=1');
    }
    });
    } else {*/

    // non ie browsers check box event notification for my carinos
    $('.myperm input').change(function() {

        if ($('.myperm input').is(':checked')) {
            $.ajax({
                url: '/location/setlocation.aspx?usecurrent=1&perm=1&id=2',
                success: function(data, textStatus, XMLHttpRequest){
              
                   // alert('Load was performed.');
                }
            });
        } else {
        $.ajax({
            url: '/location/setlocation.aspx?remove=1',
            success: function(data) {

              //  alert('Load was performed.');
            }
        });
        }
    });
    //}

    $('#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("");
                }
            });
        }
    });
});
