﻿
var IsUploading = false;
var LastClick = "";

function SubmitClick(CheckUploadSize) {
    LastClick = "submit";
    UploadStarted();
    if (CheckUploadSize) setTimeout("checkUploadedFilesSize()", 5000);
}

function UploadStarted() {
    IsUploading = true;
}

function CancelUploadCatch()
{
  IsUploading = false;
  LastClick = "";
}

window.onbeforeunload = function () {
    // Clicking the submit button triggers an unload. 
    if (LastClick == "submit") {
        LastClick = "";
        return;
    }
    
    if (IsUploading) {
        return ("You are in the middle of sending a file. Leaving the page will abort the upload!");
    }
}


var WaitingForRedirect = false;
 
var SizeLimit = 2147483648; // 2 gigs

 function SetSizeLimit(NewSizeLimit)
 {
   SizeLimit = NewSizeLimit;
 }
 
 
 function checkUploadedFilesSize()
 {
     if (WaitingForRedirect) return;

     if (SizeLimit == 0) {
         return;
     }
     
    if (SizeLimit < 0) {
        WaitingForRedirect = true;
        CancelUploadCatch();
        var message = "You must upgrade your plan to send this file.\n";
        message += "\n";
        message += "Quota Left/Limit for this file: " + (SizeLimit / (1024 * 1024)).toFixed(2) + " MB\n";
        message += "\n";
        message += "Click OK\n";
        alert(message);

        //window.location = "/MyAccount/Upgrade/";
        return;
    }

     
    var x = document.getElementById("RadProgressArea1_Panel_PrimaryTotal");
    if (x == null) x = document.getElementById("ctl00_ContentPlaceHolder1_RadProgressArea1_Panel_PrimaryTotal");
    if (x == null) x = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_RadProgressArea1_Panel_PrimaryTotal");
    
    var sizestring = "";
    var multiplier = 1;
    
    
    if (x != null) sizestring = x.innerHTML

    if (sizestring == null || sizestring == "")
    {
        setTimeout("checkUploadedFilesSize()", 5000);
    } 
    else
    {
        //HideElement("divHideDuringUpload"); 
        sizestring = sizestring.toUpperCase();
        
        
        if (sizestring.indexOf("KB") != -1) 
        {
            sizestring = sizestring.replace("KB", "");
            multiplier = 1024;
        }
        
        if (sizestring.indexOf("MB") != -1) 
        {
            sizestring = sizestring.replace("MB", "");
            multiplier = 1024*1024;
        }
        
        if (sizestring.indexOf("GB") != -1) 
        {
            sizestring = sizestring.replace("GB", "");
            multiplier = 1024*1024*1024;
        }
        
        var size = parseFloat(sizestring);
        if (isNaN(size)) size = 1;

        size = size * multiplier;
        if (SizeLimit != 0) {
            if (size > SizeLimit) {
                //progressArea.cancelRequest();
                WaitingForRedirect = true;
                CancelUploadCatch();
                var message = "You must upgrade your plan to send this file.\n";
                message += "\n";
                message += "Quota Left/Limit for this file: " + (SizeLimit / (1024 * 1024)).toFixed(2) + " MB\n";
                message += "Size of file being uploaded: " + (size / (1024 * 1024)).toFixed(2) + " MB\n";
                message += "\n";
                message += "Click OK\n";
                alert(message);

                //window.location="/MyAccount/Upgrade/";
                return;

                var tempwin = GetRadWindow();
                //var oArea = tempwin.$find('RadProgressArea1');        
                //oArea.cancelRequest(); 
                //tempwin.close();  

                //var parentwin = window.parent;
                //if (parentwin == null) parentwin = window;


                //parentwin.location="/MyAccount/Upgrade/";
            }
        }
    }
 }
 

function ForceShowRadProgressArea()
{
    var ar = document.getElementById("RadProgressArea1");
    if (ar == null) ar = document.getElementById("ctl00_ContentPlaceHolder1_RadProgressArea1");
     ar.style.display = "block";
     ar.style.visibility="visible";
     ar.style.width="430px";
}

function HideRadProgressAreaBorder()
{     
     var pan = document.getElementById("RadProgressArea1_Panel");
     if (pan == null) pan = document.getElementById("ctl00_ContentPlaceHolder1_RadProgressArea1_Panel");
     
     if (pan == null) return;
     
     var ullist = pan.getElementsByTagName("ul"); 
     for (var i = 0; i < ullist.length; i++) 
     { 
        var current = ullist[i]; 
        if (current.className == "ruProgress") 
        {
            if(document.all)//IE browser  
            {
                current.style.borderWidth = 0;
            } else {
             current.style.border="0px none";
            }
            
        }
    }
}
