﻿var BasePath="";
function ShowMessageOnOverlay(m) {
    if ($("#Overlay").id == undefined) EnableOverlay();
    $("#Overlay").append($("<div></div>").css({
        position: "fixed",
        left: ($(document).width() - 357) / 2 + "px",
        top: ($(window).height() - 139 - 100) / 2 + "px",
        zIndex: "1000",
        backgroundColor: "Transparent",
        backgroundImage: "url(" + BasePath + "images/MsgBg.png)",
        backgroundRepeat: "no-repeat",
        width: "280px",
        height: "85px",
        fontSize: "25px",
        paddingTop: "55px",
        paddingLeft: "80px"
    }).html("<img src='" + BasePath + "images/loading.gif' align='absmiddle' hspace='10' />" + m));
};

function EnableOverlay() {
    if ($("#Overlay").length > 0) return;
    $("#aspnetForm").prepend($("<div></div>").attr("id", "Overlay").css({
        position: "absolute",
        left: $(document).width() / 2 + "px",
        top: $(window).height() / 2 - 20 + "px",
        zIndex: "999",
        width: "0px",
        height: "0px"
    }).animate({
        left: "0px",
        top: "0px",
        width: "100%",
        height: $(document).height() + "px"
    }, 0));
    
    if ($.browser.version == "6.0")
        $("#Overlay").css({ filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + BasePath + "images/blank.png,sizingmethod=scale)" });
    else
        $("#Overlay").css({ backgroundImage: "url(" + BasePath + "images/blank.png)" });
}
function DisableOverlay() {
    $("#Overlay").empty().remove();
    return false;
}
function ShowResult(data) {
    try {
        data = eval('(' + data + ')');
        if (data.Msg){
			DisableOverlay();
            alert(data.Msg);
		}
		if (data.RedirectURL){
            document.location.href = data.RedirectURL;
        } 
    } catch (e) {
	    DisableOverlay();
        alert(data + "\n\n" + e);
    }
}
function SetUploader(InputFile, UploadImage) {
    $("#" + UploadImage).click(function() {
        var UploadedFile = $("#" + InputFile).val();
        if (UploadedFile == "") {
            alert("Please select a file to upload");
            $("#" + InputFile).focus();
            return false;
        } else {
            var Extension = UploadedFile.substring(UploadedFile.lastIndexOf(".")).toLowerCase();
            if (Extension != ".jpg" && Extension != ".jpeg" && Extension != ".gif" && Extension != ".tiff") {
                alert("Invalid file selected. Currently we only support jpg, gif and tif files.");
                return false;
            }
        }
        $("form").attr("target", "hiddenFrame");
        ShowFileProgress();
        return true;
    });
}
function ShowFileProgress() {
    if (jQuery("#Overlay").id == undefined) EnableOverlay();
    var WaitingBox = jQuery("<div></div>").attr("id", "WaitingBox").css({
        width: "400px",
        height: "106px",
        left: (jQuery(document).width() - 400) / 2 + "px",
        top: (jQuery(window).height() - 106 - 100) / 2 + "px"
    });
    if ($.browser.version == "6.0") {
        var offset = jQuery(window).width() - jQuery(".MainContainer").width();
        WaitingBox.css({ position: "absolute", left: (jQuery(window).width() - 400 + offset) / 2 + "px" });
    }
    jQuery("#Overlay").append(WaitingBox);
    WaitingBox.append("<br/><br/>");
    var ProgressBarContainer = jQuery("<div></div>").addClass("ProgressBarContainer");
    ProgressBarContainer.html("<div id='ProgressBarText'>Please wait...</div><div id='ProgressBar'></div>");
    WaitingBox.append(ProgressBarContainer);

    CheckFileProgress();
}
function CheckFileProgress() {
    $.get("UploadProgress.ashx", {
        DJUploadStatus: UploadKey,
        ts: new Date().getTime()
    }, function(data) {
        var status = $(data).find("status");
        var empty = status.attr('empty');
        if (!empty) {
            var percent = status.attr('progress');
            var file = status.attr('file');
            var kbs = Math.round(parseInt(status.attr('bytes')) / 1024);
            var size = Math.round(parseInt(status.attr('size')) / 1024);
            //var data = "\n\n" + 'Uploading ' + file + ' ... ' + kbs + ' of ' + size + ' KB (' + percent + "%)" + "\n\n" + $(data).find("*").text();
            $("#ProgressBarText").html(percent + "%");
            $("#ProgressBar").css({ width: percent + "%" });
            if (percent == 100) {
               
            }
            else CheckFileProgress();
        } else {
            CheckFileProgress();
        }
    }, "xml");
}


