﻿function ValidateDeleteAction(url, redirect) {
    if (confirm("Are you sure you want to delete this?")) {
        $("fieldset").slideUp('slow');
        $.post(url, "", function(data) {
            location.href = redirect;
        });
    }
    return false;
}
function UpdateStatus(e, url) {
    $.post(url, "", function(data) {
        $('#' + e).fadeOut();
    });
    return false;
}
function UpdateStatusMultipleApprove(url) {
    // #checkbox-selectors:checked doesn't work on ie8
    //$.each($("#checkbox-selectors:checked"), function(i, item) {
    if (UpdateStatusValidateCheckbox("To approve records, tick one or more checkboxes.")) {
        $.each($("input:checkbox:checked", $(".grid-wrapper")), function(i, item) {
            $.post(url + item.value, { approve: "true" }, function(data) {
                item.checked = false;
                $(item).parent().parent().hide();
                $('#status-icon' + item.value).attr("src", "/content/images/icons/check2.png");
                $(item).parent().parent().fadeIn("slow");
            });
        });
    }
    return false;
}
function UpdateStatusMultipleUnapprove(url) {
    if (UpdateStatusValidateCheckbox("To unapprove records, tick one or more checkboxes.")) {
        $.each($("input:checkbox:checked", $(".grid-wrapper")), function(i, item) {
            $.post(url + item.value, { approve: "false" }, function(data) {
                item.checked = false;
                $(item).parent().parent().hide();
                $('#status-icon' + item.value).attr("src", "/content/images/icons/warning.png");
                $(item).parent().parent().fadeIn("slow");
            });
        });
    }
    return false;
}
function UpdateStatusMultipleArchive(url) {
    if (UpdateStatusValidateCheckbox("To archive records, tick one or more checkboxes.")) {
        $.each($("input:checkbox:checked", $(".grid-wrapper")), function(i, item) {
            $.post(url + item.value, { archive: "true" }, function(data) {
                item.checked = false;
                $(item).parent().parent().fadeOut("slow");
            });
        });
    }
    return false;
}
function UpdateStatusMultipleUnarchive(url) {
    if (UpdateStatusValidateCheckbox("To unarchive records, tick one or more checkboxes.")) {
        $.each($("input:checkbox:checked", $(".grid-wrapper")), function(i, item) {
            $.post(url + item.value, { archive: "false" }, function(data) {
                item.checked = false;
                $(item).parent().parent().fadeOut("slow");
            });
        });
    }
    return false;
}
function UpdateStatusMultipleDelete(url) {
    if (UpdateStatusValidateCheckbox("To delete records, tick one or more checkboxes.")) {
        if (confirm("Are you sure you want to delete the selected records?")) {
            $.each($("input:checkbox:checked", $(".grid-wrapper")), function(i, item) {
                $.post(url + item.value, { remove: "true" }, function(data) {
                    item.checked = false;
                    $(item).parent().parent().fadeOut("slow");
                });
            });
        }
    }
    return false;
}

function UpdateStatusValidateCheckbox(msg) {
    var result = false;
    if ($("input:checkbox:checked", $(".grid-wrapper")).length > 0) {
        result = true;
    } else {
        alert(msg);
    }
    return result;
}

/* supports cancelling of file uploads */
var uploadchangedtimeout = 0;
function uploadfileChanged(index) {
    uploadchangedtimeout = setTimeout('showcancelfile(' + index + ');', 700);
}
function showcancelfile(index) {
    clearTimeout(uploadchangedtimeout);
    $('#cancelfile' + index).fadeIn();
}
function cancelupload(index, title) {
    $('#uploadfile' + index).replaceWith('<input type="file" id="uploadfile' + index + '" name="uploadfile' + index + '" size="34" style="width:100%;" title="' + title + '" onchange="uploadfileChanged(' + index + ');" />');
    $('#cancelfile' + index).fadeOut();
}


/* supports time picker */
var timepickerTimer = 0;
function timepickerSetup() {
    if (timepickerTimer > 0) {
        clearTimeout(timepickerTimer);
    }
    timepickerDisable(true);
    timepickerTimer = setTimeout('timepickerCallback();', 2500);
}
function timepickerCallback() {
    clearTimeout(timepickerTimer);
    timepickerDisable(false);
    timepickerInitialize();
}
function timepickerInitialize() {
    $("#startdatetime").timePicker({ show24Hours: false });
    $("#enddatetime").timePicker({ show24Hours: false });
    if ($("#allday:checked").length > 0) {
        $("#startdatetime").fadeOut();
        $("#enddatetime").fadeOut();
    }
}
function toggleSDT() {
    $("#startdatetime").toggle();
    $("#enddatetime").toggle();    
}
function timepickerDisable(toggle) {
    if (toggle) {
        $("#startdatetime").attr("disabled", "true");
        $("#enddatetime").attr("disabled", "true");
    } else {
        $("#startdatetime").removeAttr("disabled");
        $("#enddatetime").removeAttr("disabled");
    }
}

