﻿var Page_IsValid = true;
var first = true;
var target;

 function confirmBlockUI(msg, id) {
    $("#" + id).click(function (e) {
        if (first) {
            target = $(e.target);
            e.preventDefault();
            if (Page_IsValid) {
                $('#question').html('<h3 style="font-family:verdana">' + msg + '</h3><input type="button" id="' + id + 'yes" value="Yes" /> <input type="button" id="' + id + 'no" value="No" />');
                $.blockUI({ message: $('#question'), css: { width: '350px'} });
                $('#' + id + 'yes').click(function () {
                    $.unblockUI();
                    first = false;
                    target.click();
                    first = true;
                });

                $('#' + id + 'no').click(function () {
                    $.unblockUI();
                });
            }
        }
    });
}

 function confirmBlockUILink(msg, id) {
    $("#" + id).click(function (e) {
        e.preventDefault();
        if (Page_IsValid) {
            $('#question').html('<h3 style="font-family:verdana">' + msg + '</h3><input type="button" id="' + id + 'yes" value="Yes" /> <input type="button" id="' + id + 'no" value="No" />');
            $.blockUI({ message: $('#question'), css: { width: '350px'} });
            $('#' + id + 'yes').click(function () {
                $.unblockUI({
                    onUnblock: function () {
                        eval($('#' + id).attr('href'));
                    }
                }); 
            });

            $('#' + id + 'no').click(function () {
                $.unblockUI();
            });
        }
    });
}

function alertBlockUI(msg, id) {
    $('#question').html('<h3 style="font-family:verdana">' + msg + '</h3><input type="button" id="' + id + 'yes" value="OK" />');
    $.blockUI({ message: $('#question'), css: { width: '350px'} });
    $('#' + id + 'yes').click(function () {
        $.unblockUI();
        if (id != '') {
            $("#" + id).focus();
        }
    });
}

function confirmSession() {
    $('#question').html('<h3 style="font-family:verdana">This session will expire in two minutes. Click OK to refresh the session and prevent data loss.</h3><input type="button" id="yes" value="OK" /> <input type="button" id="no" value="Cancel" />');
    $.blockUI({ message: $('#question'), css: { width: '350px'} });
    $('#yes').click(function () {
        $.unblockUI();
        var img = new Image(1,1);
        img.src = 'Fitness.aspx?xxx='+escape(new Date());
        sessionTimeout = window.setTimeout('confirmSession()', resetTime);
    });

    $('#no').click(function () {
        $.unblockUI();
    });
}

function confirmBuild() {
    $('#question').html('<h3 style="font-family:verdana">Would you like to create a workout using the Workout Wizard or Workout Editor now?</h3><input type="button" id="ww" value="Workout Wizard" /> <input type="button" id="we" value="Workout Editor" /> <input type="button" id="no" value="Cancel" />');
    $.blockUI({ message: $('#question'), css: { width: '350px'} });
    $('#ww').click(function () {
        $.unblockUI();
        location.href = "protected/workoutwizard.aspx";
    });

    $('#we').click(function () {
        $.unblockUI();
        location.href = "protected/workouteditor.aspx";
    });

    $('#no').click(function () {
        $.unblockUI();
    });
}

function WaterMark(txt, evt, defaultText) {
    if (txt.value.length == 0 && evt.type == "blur") {
        txt.style.color = "gray";
        txt.value = defaultText;
    }
    if (txt.value == defaultText && evt.type == "focus") {
        txt.style.color = "black";
        txt.value = "";
    }
}

function Over1HourAlert(txt) {
    var time = txt.value.split(":");
    var hours = 0
    var minutes = 0
    var seconds = 0.0
    switch (time.length) {
        case 3:
            if (IsNumber(time[0])) hours = parseInt(time[0]);
            if (IsNumber(time[1])) minutes = parseInt(time[1]);
            if (IsNumber(time[2])) seconds = parseFloat(time[2]);
            seconds += hours * 3600 + minutes * 60;
            break;
        case 2:
            if (IsNumber(time[0])) minutes = parseInt(time[0]);
            if (IsNumber(time[1])) seconds = parseFloat(time[1]);
            seconds += minutes * 60;
            break;
        case 1:
            if (IsNumber(time[0])) seconds = parseInt(time[0]) * 60;
            break;
    }

    if (seconds > 3600)
        alertBlockUI("You entered over 1 hour for this exercise. Please change the value if this is not correct.");
}

function IsNumber(n) {  
    return !isNaN(parseFloat(n)) && isFinite(n);
}
