//****************************************************************************
// Copyright (c) 2005, Coveo Solutions Inc.
//****************************************************************************

//****************************************************************************
// Opens a SharePoint document for viewing.
// p_Uri    - The uri of the document to open.
// p_Window - The window that opens the document.
//****************************************************************************
function CES_SharePointItemUtilities_ViewDocument(p_Uri, p_Window)
{
    var res;
    var obj = CES_SharePointItemUtilities_GetOpenDocuments();
    if (obj != null) {
        try {
            res = obj.ViewDocument2(p_Window, p_Uri);
        } catch (e) {
            try {
                res = obj.ViewDocument(p_Uri);
            } catch (e) {
                // Older Office versions don't have ViewDocument.
                res = false;
            }
        }
    }

    // If the object isn't found, or that the call failed, tell the browser to
    // navigate to the document instead.
    if (obj == null || !res) {
        p_Window.location.href = p_Uri;
    }
}

//****************************************************************************
// Opens a SharePoint document for editing.
// p_Uri - The uri of the document to open.
// p_Window - The window that opens the document.
//****************************************************************************
function CES_SharePointItemUtilities_EditDocument(p_Uri, p_Window)
{
    var obj = CES_SharePointItemUtilities_GetOpenDocuments();
    if (obj != null) {
        try {
            obj.EditDocument2(p_Window, p_Uri);
        } catch (e) {
            obj.EditDocument(p_Uri);
        }
    } else {
        // Fallback on ViewDocuments, which will open the doc in the browser.
        CES_SharePointItemUtilities_ViewDocument(p_Uri, p_Window);
    }
}

//****************************************************************************
// Redirects the browser to the page that allows adding an item to a user's quick links.
// p_ID        - The id of the item.
// p_Title     - The title of the item.
// p_Uri       - The uri of the item.
// p_Type      - The type of the item.
// p_PageUri   - The uri of the QuickLinks.aspx page.
// p_ReturnUri - The return uri.
//****************************************************************************
function CES_SharePointItemUtilities_AddToQuickLinks(p_ID, p_Title, p_Uri, p_Type, p_PageUri, p_ReturnUri)
{
    var frm = document.createElement('form');
    frm.action = p_PageUri;
    frm.method = 'POST';
    document.body.appendChild(frm);

    frm.QuickLinksGUID.value = p_ID;
    frm.QuickLinksTitle.value = p_Title;
    frm.QuickLinksUrl.value = p_Uri;
    frm.QuickLinksType.value = p_Type;
    frm.ReturnUrl.value = p_ReturnUri;
    frm.submit();
}

//****************************************************************************
// Attempts to create an SharePoint.OpenDocuments object. 
// Returns the OpenDocuments object, or null if it is not installed.
//****************************************************************************
function CES_SharePointItemUtilities_GetOpenDocuments()
{
    // Attempt to create the object used to open documents
    var obj;
    try {
        obj = new ActiveXObject('SharePoint.OpenDocuments.2');
    } catch (e) {
        try {
            obj = new ActiveXObject('SharePoint.OpenDocuments.1');
        } catch (e) {
            try {
                obj = new ActiveXObject('SharePoint.OpenDocuments');
            } catch (e) {
                obj = null;
            }
        }
    }

    return obj;
}

