// ---- extend Number object with methods for converting degrees/radians

/** Converts numeric degrees to radians */
if (typeof (Number.prototype.toRad) === "undefined") {
    Number.prototype.toRad = function () {
        return this * Math.PI / 180;
    }
}

/** Converts radians to numeric (signed) degrees */
if (typeof (Number.prototype.toDeg) === "undefined") {
    Number.prototype.toDeg = function () {
        return this * 180 / Math.PI;
    }
}

$(document).ready(function () {
    try {
        $.watermark.options.useNative = false;
    }
    catch (e) { }

    $('.close a').click(function (e) {
        e.preventDefault();
        $(this).parent().parent().css('display', 'none');
    });

    $(".date-picker").datepicker();

    $('#dialogContent').dialog({
        autoOpen: false,
        modal: true,
        bgiframe: true,
        title: "MySql Membership Config Tool",
        width: 800,
        height: 600
    });

    $('.searchbox').watermark('search');
    $('.email-newsletter-signup').watermark('email address');
    $('.keywords').watermark('enter keyword(s)');
    $('.mindate, .maxdate').watermark('mm/dd/yyyy');

    $('.expandable .section-header a').click(function (e) {
        e.preventDefault();
        $(this).parent().next('.section').slideToggle();
        $(this).toggleClass('expanded');
    });

    $('.zipcodeinput').keypress(function (event) {
        if (event.which == '13') {
            event.preventDefault();
            $(this).siblings(':button').click();
        }
    });

    $('.daywrapper').hover(function () {
        $(this).attr('style', '');
    });
}); 
                     
        function rowAction(uniqueID) {             
            $('#dialogContent').dialog('option', 'buttons',                 
            {                    
                "OK": function() { __doPostBack(uniqueID, ''); $(this).dialog("close"); },                     
                "Cancel": function() { $(this).dialog("close"); }                 
            });  
                            
            $('#dialogContent').dialog('open');  
                         
            return false;
        }

        function checkInputtedTime(time) {  
            var indexColon = time.indexOf(':');
            if (indexColon == -1) {
                if (time.length == 3) {
                    return time.charAt(0) + ":" + time.charAt(1) + time.charAt(2)
                }

                else if (time.length == 4) {
                    return time.charAt(0) + time.charAt(1) + ":" + time.charAt(2) + time.charAt(3)
                }

                else {
                    alert("Invalid time");
                    return time;
                    
                }
            }
            return time;
        }



    function h12to24(time) {
        if (!(time && time != '')) {
            return '';
        }
        var time = time.toUpperCase();
        var indexColon = time.indexOf(':');

        var indexLetter = time.indexOf('P');
        var afternoon = (indexLetter != -1);
        if (indexLetter == -1)
            indexLetter = time.indexOf('A');

        var hour = new Number(time.substring(0, indexColon));

        if (indexColon > 0) {
            if (afternoon && hour < 12) {
                //after 1:xx pm is converted to (1+12):xx
                time = time.replace(hour, (hour + 12));
                indexLetter = time.indexOf('P');
            }
            if (!afternoon && hour == 12) {
                //12:xx am is converted 00:xx
                time = time.replace(hour, 0);
                indexLetter = time.indexOf('A');
            }
            if (indexLetter > 0) {
                time = time.substring(0, indexLetter);
                return time;
            }
        }
        alert('could not transfor time to 24H format');
    }

    function getDrivingDistance(obj, eventID) {
        $obj = $(obj);

        var zipcode = $obj.siblings('.zipcodeinput:first').val();

        $('#zipCodeLink' + eventID).html("Calculating...");
        $.ajax({

            type: "POST",
            url: "/GetDrivingDistance.aspx/ZipCode",
            data: "{ZipCode:'" + zipcode + "'}",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            success: function (response) {


                if (response.d == "") {
                    $('#zipCodeLink' + eventID).html("There was an error calculating your location, please try again. <a href=\"#\" onclick=\"zipCode(this); return false;\">Try again?</a>");
                }
                else {
                    //$('#zipCodeLink' + eventID).html(response.d);

                    array = response.d.split("_");
                    var R = 3959; // km
                    $('.zipcodedata').each(function (i, e) {
                        data = this.value.split("_");
                        var dLat = (data[1] - array[0]).toRad();
                        var dLon = (data[2] - array[1]).toRad();
                        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                        Math.cos(Number(data[1]).toRad()) * Math.cos(Number(array[0]).toRad()) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
                        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                        var d = R * c;

                        var dLat = (data[1] - array[0]).toRad();
                        var dLon = (data[2] - data[2]).toRad();
                        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                        Math.cos(Number(data[1]).toRad()) * Math.cos(Number(array[0]).toRad()) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
                        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                        var d1 = R * c;

                        var dLat = (data[1] - data[1]).toRad();
                        var dLon = (data[2] - array[1]).toRad();
                        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                        Math.cos(Number(data[1]).toRad()) * Math.cos(Number(data[1]).toRad()) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
                        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                        var d2 = R * c;

                        var rightangle = Number(d1 + d2);

                        if (rightangle < 10)
                            $('#zipCodeLink' + data[0]).html("Approx. 0-10 miles");
                        else if (rightangle < 25)
                            $('#zipCodeLink' + data[0]).html("Approx. 10-25 miles");
                        else
                            $('#zipCodeLink' + data[0]).html("Approx. " + Math.round(d / 10) * 10 + " - " + Math.round(rightangle / 10) * 10 + " miles");

                        $('#zipCodeLink' + data[0]).html($('#zipCodeLink' + data[0]).html() + "<span style=\"font-weight:normal;\"> | <a href=\"http://maps.google.com/maps?f=q&source=s_q&hl=en&daddr=" + data[1] + "," + data[2] + "\" style=\"display: inline;font-weight:normal\" target=\"_blank\">Get Directions</a> | <a href=\"#\" onclick=\"zipCode(this); return false;\" style=\"display: inline;font-weight:normal\">Change Zip Code?</a></span>");
                    });



                }
            },
            error: function (response) {
                $('#zipCodeLink' + eventID).html("There was an error calculating your location, please try again. <a href=\"#\" onclick=\"zipCode(this); return false;\">Try again?</a>");
            }
        });
        return false;
    }

    function zipCode(obj) {
        var x = jQuery(obj).position().left;
        var y = jQuery(obj).position().top - $(obj).parents('.zipcodelink:first').siblings(".zip-code-box").outerHeight();
        $(obj).parents('.zipcodelink:first').siblings('.zip-code-box').css({ 'display': 'block', 'top': y + 'px', 'left': x + 'px' });
        $(':text', $(obj).parents('.zipcodelink:first').siblings('.zip-code-box')).focus();
        $("#overlay").css('display', 'block');
        $("#overlay").css('height', $('body').outerHeight() + "px");
        $('#overlay').click(function () {
            closeDialog();
        });

    }


    function closeDialog() {
        $("#delete-box").css('display', 'none');
        $('.background').css('display', 'none');
    }

    function deleteDialog(obj, msg) {
        buttonObj = obj;
        var x = jQuery(obj).position().left + jQuery(obj).outerWidth();
        var y = jQuery(obj).position().top + jQuery(obj).outerHeight();
        if (x < 0)
            x = 0;

        if (x > $('body').outerWidth()) {
            x = $('body').outerWidth() - $("#delete-box").outerWidth();
        }

        $("#delete-box").css({ 'display': 'block', 'top': y + 'px', 'left': x + 'px' });
        $('.background').css('display', 'block');
        $('.background').css('height', $('body').outerHeight() + "px");

        if (msg) {
            $("#delete-box #msg").html(msg);
        }
        return false;
    }

    function confirmDelete() {
        var strReplaceAll = buttonObj.id;
        var intIndexOfMatch = strReplaceAll.indexOf("_");

        // Loop over the string value replacing out each matching
        // substring.
        while (intIndexOfMatch != -1) {
            // Relace out the current instance.
            strReplaceAll = strReplaceAll.replace("_", "$")

            // Get the index of any next matching substring.
            intIndexOfMatch = strReplaceAll.indexOf("_");
        }

        //alert(strReplaceAll);
        __doPostBack(strReplaceAll, '');
        closeDialog();
    }


