// see http://www.wssdemo.com/Pages/TipsTricks.aspx
// Office 2003 Components
/*
SharePoint.SpreadsheetLauncher.2
SharePoint.SpreadsheetLauncher
Sharepoint.OpenXMLDocuments.1
Sharepoint.OpenXMLDocuments
SharePoint.OpenDocuments.2 // client support 
SharePoint.OpenDocuments  
SharePoint.SpreadsheetLauncher.1
SharePoint.SpreadsheetLauncher
SharePoint.StssyncHandler.2
SharePoint.StssyncHandler
*/
var REFRESH = false;

function trim(s)
{ 
	return s.replace(/^\s(\s)*|(\s)*\s$/g,"");
}

function RefreshOnFocus()
{
    window.location.href = window.location;	
}


function newDocument(folderName, templateName) {
    try   {
		// Try to open using the Office 2003 components
		if (folderName==null) {
			alert("A document cannot be created without a target folder.");
		}
		if (templateName==null) {
			alert("A document cannot be created without a template.");
		}
		
	    templateName = trim(templateName); 
		folderName = trim(folderName);
		//alert("Template: " +templateName + "\nFolder:     " + folderName);
		
        var docManager = new ActiveXObject("SharePoint.OpenDocuments.2"); 
		if (!docManager) {
			throw "Office 11 components not installed";
        } else {
			if (!docManager.CreateNewDocument(templateName, folderName)) {
			//if (!docManager.CreateNewDocument2(window, templateName, folderName)) {
				alert("Cannot create document.  The template or the folder may not be available.");
			}
            window.onfocus = RefreshOnFocus;	
			return false;
		}
    } catch (e) {
		try  {
			// Nope, Try to open using the Office 2000 components
			docManager = new ActiveXObject("SharePoint.OpenDocuments.1");
			if (!docManager) {
				throw "Office 10 components not installed";
			}		
			if (!docManager.CreateNewDocument(templateName, folderName)) {
				alert(genErr);
			}
            window.onfocus = RefreshOnFocus;	
			return false;
		} catch (e) {
			; // fall into bottom
		}
    }
    
    var altErr = "This feature requires Internet Explorer 5.0+ and Office 2003+."
    altErr += "\nYou can still add documents to the folder using the attachments feature to upload it."
    altErr += "\nTo create the document edit it, save it locally, then use the attachment system to upload it to this request."
    alert(altErr);
    
    return true; // follow the link
}


function editDocument(documentPath)	{
   var docManager = new ActiveXObject("SharePoint.OpenDocuments.1"); 

   if (docManager)	{
	  docManager.EditDocument(documentPath);	
	  return false;
	} else {
		var altErr = "This feature requires Internet Explorer 5.0+ and Office 2003+."
		altErr += "\nYou can still add documents to the folder using the attachments feature to upload it."
		altErr += "\nTo create the document edit it, save it locally, then use the attachment system to upload it to this request."
		alert(altErr);
	}
	return true;
}


