﻿
function UploadValidateFileCount(isSilent) {

    var x = $('.uploadifyQueueItem').size();

    if (x == 0) {
        if (!isSilent)
            alert("No files Selected!");

        return false;
    }

    return true;
}


function UploadClear() {

    UploadifyCurrentSize = undefined;
    UploadInstance.Clear();
    ClearPackageName();
}


function UploadReset() {
    UploadClear();
}


function UploadStart() {

    IsUploadActive = true;

    if (UploadInstance)
        UploadInstance.Start();

    UploadProgressVisible = false; //Stub for old version

    UploadifyStartTime = new Date();
    UploadifyTotalCount = UploadifyRemainingCount = UploadifyGetTotalCount();
}


// ===================  Upload Progress  =================================================

function UploadProgressCancelClick() {

    var f = confirm("Are you sure you want to cancel upload?");
    if (f == true) {
        UploadReset();
        ProgressHide();

        IsUploadActive = false;

        UploadCancel(GetPackageServiceUrl(), UploadInstance.UploadID);

        var x = GetGuid();
        UploadifySetUploadID(x);
    }

    return !f;
}

function GetGuid() {
    return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    }).toUpperCase();
}

function ProgressHide() {
    SubmitEnable();
    UploadProgressHide();
}


//  #########################################################################################

function RegistrationUpload_UpdatePageState() {
    if (typeof (NMenu_SetSignedIn) != 'undefined')
        NMenu_SetSignedIn(1);

    if (typeof (TopMenu_SetSignedIn) != 'undefined')
        TopMenu_SetSignedIn(1);

    if (typeof (IsLoggedIn) != 'undefined')
        IsLoggedIn = true;

}

function RegistrationUpload_OnComplete(sender, isKeepPageState) {

    if (!isKeepPageState)
        RegistrationUpload_UpdatePageState();

    if (UploadifyCurrentSize == 0) {
        alert('Files size cannot be 0.');
        return;
    }

    // test package size
    if (Uploadify_IsFileSizeValid())
        UploadCreateLink(GetPackageServiceUrl(), UploadStart);
    else {
        UpgradeAccountDo(UploadifyCurrentSize, Upgrade_OnComplete);
    }
}

function RegistrationUpload_OnCancel(sender) {

}

function Upgrade_OnComplete(newTariff) {

    if (typeof (SetTariffText) != 'undefined')
        SetTariffText(newTariff)

    var s = undefined;
    if (typeof (GetRegistrationEmail) != 'undefined')
        s = GetRegistrationEmail();

    RegistrationDo(GuestID, s, RegistrationUpload_OnComplete);
}



//  -------------------------------------------  Uploadify  --------------------------

var UploadifyCurrentSize;
var UploadifyStartTime;
var UploadifyRemainingCount;
var UploadifyTotalCount;

function UploadifyAddFile(event, data) {

    UploadifyCurrentSize = data.allBytesTotal;

    UploadifyUpdatePreItem(data.fileCount);
}

function UploadifyRemoveFile(event, ID, fileObj, data) {

    UploadifyCurrentSize = data.allBytesTotal;

    UploadifyUpdatePreItem(data.fileCount);
}

function Uploadify_IsFileSizeValid() {

    if (!UploadifyCurrentSize) {

        if (UploadifyCurrentSize == 0)
            return true;
        return false;
    }

    if (UploadLimit == 0)
        return true;

    return UploadifyCurrentSize < (UploadLimit * 1024 * 1024);
}


//---------------------------  GetExtraUploadParams  ----------------------

function GetExtraUploadParams() {

    var url = document.location.href;
    var userAgent = navigator.userAgent;
    var parentUrl = parent.document.location.href;

    var flashVersion = '';
    if (typeof (GetSwfVersion) != 'undefined')
        flashVersion = GetSwfVersion();

    var fileCount = '';
    if (typeof (UploadifyGetTotalCount) != 'undefined')
        fileCount = UploadifyGetTotalCount();

    var fileSize = '';
    if (typeof (UploadifyCurrentSize) != 'undefined')
        fileSize = UploadifyCurrentSize;

    return "',Url: '" + url + "',UserAgent: '" + userAgent + "',ParentUrl: '" + parentUrl + "',FlashVersion: '" + flashVersion + "',UploaderType: '" + '2' + "',FileCount: '" + fileCount + "',FileSize: '" + fileSize;
}
