﻿//Handle the DialogCallback callback 
function EditDialogCallback(dialogResult, returnValue) {
    window.location.href = window.location.href;
}

//Open the Dialog

function OpenListEditDialog(listName, itemId) {
    var urlBase = GetUrlBase(window.location.href);
    var options = {
        url: urlBase + "/Lists/" + listName + "/EditForm.aspx?ID=" + itemId + "&IsDlg=1",
        width: 700,
        height: 700,
        dialogReturnValueCallback: EditDialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function OpenListViewDialog(listName, itemId) {
    var urlBase = GetUrlBase(window.location.href);
    var options = {
        url: urlBase + "/Lists/" + listName + "/DispForm.aspx?ID=" + itemId + "&IsDlg=1",
        width: 700,
        height: 700,
        dialogReturnValueCallback: EditDialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function OpenDocumentViewDialog(listName, itemId) {
    var urlBase = GetUrlBase(window.location.href);
    var options = {
        url: urlBase + "/" + listName + "/Forms/DispForm.aspx?ID=" + itemId + "&IsDlg=1",
        width: 700,
        height: 700
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function GetUrlBase(url) {
    var firstSlash;

    if (url.substring(0, 5).toLowerCase() == "https") {
        firstSlash = url.indexOf("/", 8);
    }
    else {
        firstSlash = url.indexOf("/", 7);
    }

    var secondSlash = url.indexOf("/", firstSlash + 1);
    return url.slice(0, secondSlash);
}

function GetGlobalUrl(url) {
    var firstSlash;

    if (url.substring(0, 5).toLowerCase() == "https") {
        firstSlash = url.indexOf("/", 8);
    }
    else {
        firstSlash = url.indexOf("/", 7);
    }

    var host = url.slice(0, firstSlash);

    var globalUrl = host + "/" + "GlobalAssets";
    return globalUrl;
}

function OpenImageUploadDialog(listId, stockNumber) {
    var urlBase = GetUrlBase(window.location.href);
    var options = {
        title: 'Upload File',
        url: urlBase + "/_layouts/Upload.aspx?List=" + listId + "&RootFolder=" + stockNumber + "&IsDlg=1",
        dialogReturnValueCallback: EditDialogCallback
    };

    SP.UI.ModalDialog.showModalDialog(options);
}

function OpenNewItemDialog(listName) {
    var urlBase = GetUrlBase(window.location.href);
    var options = {
        url: urlBase + "/Lists/" + listName + "/NewForm.aspx?&IsDlg=1",
        width: 700,
        height: 700,
        dialogReturnValueCallback: EditDialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function OpenDocumentEditDialog(docLibName, itemId) {
    var urlBase = GetUrlBase(window.location.href);
    var options = {
        url: urlBase + "/" + docLibName + "/Forms/" + "/EditForm.aspx?ID=" + itemId + "&IsDlg=1",
        width: 700,
        height: 700,
        dialogReturnValueCallback: EditDialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function OpenGlobalDocumentEditDialog(docLibName, itemId) {
    var urlBase = GetGlobalUrl(window.location.href);
    var options = {
        url: urlBase + "/" + docLibName + "/Forms/" + "/EditForm.aspx?ID=" + itemId + "&IsDlg=1",
        width: 700,
        height: 700,
        dialogReturnValueCallback: EditDialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function OpenGlobalAssetUploadDialog(listId) {
    var urlBase = GetGlobalUrl(window.location.href);
    var options = {
        title: 'Upload File',
        url: urlBase + "/_layouts/Upload.aspx?List=" + listId + "&IsDlg=1",
        dialogReturnValueCallback: EditDialogCallback
    };

    SP.UI.ModalDialog.showModalDialog(options);
}







