

var gErrorUrl="";


function OpenHelpErrorHandler() 
{
    if (gErrorUrl != "")
    {
        location.href = gErrorUrl;
        return true;
    }
    else
    {
        return false;
    }
}


function OpenHelp(url)
{
    // special case for folders
    folderindex = location.href.indexOf("/nss-folder/");
    if (folderindex >= 0)
    {
        objectendindex = location.href.indexOf("/", folderindex + 12);
        url = location.href.substring(0, folderindex) + "/" + location.href.substring(folderindex + 12, objectendindex) + "/" + url;
    }
    
    window.onerror = OpenHelpErrorHandler;
    gErrorUrl = url;
    var hWnd = window.open(url, "NVOHelp", "menubar=yes,toolbar=yes,width=595,height=400,resizable=yes,scrollbars=yes");
	if (!hWnd.opener) hWnd.opener = self;
	if (hWnd.focus != null) hWnd.focus();
    gErrorUrl = "";
}


function FindElement(elementName)
{
	var i = 0;
    
    for (i = 0; i < document.nvoForm.elements.length; i++)
	{
		if (document.nvoForm.elements[i].name == elementName)
		{
			return document.nvoForm.elements[i];
		}
	}
    return null;
}


// this is the action string that is used to save the standard items on a page

function GetStdAction()
{
	var hidePage = document.nvoForm.hidePage.checked ? "ON" : "OFF";

	if (hidePage == "ON") // if hidden page, go back home
	{
		document.nvoForm.NVORedirect.value = document.nvoForm.hostAddressForHiddenPage.value;
	}
	
    return "Set(.Sitemap.Name." + document.nvoForm.ObjectUID.value + "=" + escape(document.nvoForm.PageName.value) 
    			+ "&.Sitemap.Title." + document.nvoForm.ObjectUID.value + "=" + escape(document.nvoForm.PageTitle.value) 
    			+ "&.Sitemap.Hide." + document.nvoForm.ObjectUID.value + "=" + hidePage +");";
}

function DoStdCancel()
{
	if (document.nvoForm.hidePage.checked) // if hidden page, go back home
	{
		window.open(document.nvoForm.hostAddressForHiddenPage.value, "_top");
	}
	else // page is visible
	{
		var	loc = location.href;
		
		startPos = loc.indexOf("/edit.nhtml");
		if (startPos >= 0)
			loc = loc.substring(0, startPos+1);
		else
			alert("ASSERT: DoStdCancel in global.js");

	    window.open(loc, "_top");
	}
}


var gSaveAndGoURL = "";
var gChanged = false;

// save the form data and goto the specified url

function SaveAndGo(url)
{
    if (gChanged && confirm("Would you like to save the change you made to this page?"))
    {
        gSaveAndGoURL = url;
        DoSave();
    }
    else
    {
        location.href = url;
    }
}

function Changed(url)
{
    gChanged = true;
}


function DoButton(buttonAction)
{
	if (gSaveAndGoURL != "")
    {
        document.nvoForm.NVORedirect.value = gSaveAndGoURL;
    }
    
    document.nvoForm.NVOAction.value = buttonAction;
	document.nvoForm.submit();
}


function DoMoveItem(where, uid)
{
    DoButton("MoveRecord(List=Sitemap_uid=" + uid + "_Where=" + where + ")");
}


window.errorLoadingApplet = false;


// This script is called when the user clicks on the client link before
// the browser has had time to launch the applet

function AppletErrorHandler() 
{
    alert("Please wait for the page to complete loading and then try again.");
    return true;
}


// This javascript is used launch a helper app

function StartHelper(host, service, hostAddress, extra)
{

    if (navigator.appVersion.indexOf("Macintosh") != -1)
    {
		if (navigator.mimeTypes)
		{
            mimetype = navigator.mimeTypes["application/netopia"];
            if (mimetype && mimetype.enabledPlugin)
			{
	            location.href = "http://" + hostAddress + "/pages/common/" + host + ".nhtml_service=" + service + "_back=" + escape(location.href) + "_extra=" + escape(extra);
    		}
			else
			{
				alert("You must have the FREE Visitor Software in order to use this feature. Please download and install the Visitor Software.");
				location.href = "http://" + hostAddress + "/bin/download.nhtml";
			}
		} // if we can check for mimeTypes supported
		else
		{
            location.href = "http://" + hostAddress + "/pages/common/" + host + ".nhtml_service=" + service + "_back=" + escape(location.href) + "_extra=" + escape(extra);
		} // else go to plugin start page 
    }
	else
	{
	    if (window.errorLoadingApplet)
	    {
	        alert("You must have Java enabled in your browser in order to use this feature.");
	    }
	    else
	    {
	        window.onerror = AppletErrorHandler;
	        
	        installed = document.cc.isClassInstalled("com.netopia.ns.version.client.VisitorClient");
	        if (installed)
	        {
	            location.href = "http://" + hostAddress + "/pages/common/" + host + ".netopia_service=" + service + "_extra=" + escape(extra);
	        }
	        else
	        {
	            alert("You must have the FREE Visitor Software in order to use this feature. Please download and install the Visitor Software.");
	            location.href = "http://" + hostAddress + "/bin/download.nhtml";
	        }
	    }
    } // else it's Windows
}

// pulls a parameter out of a comma delimeted string
// For example, given the string "a,b,c", passing
// index 0 would return "a", 1 would return "b", etc...
function GetField(rec, fieldindex)
{
    var     i;
    var     istart = 0;
    var     iend = 0;

    for (i = 0; i < fieldindex; i++)
    {
        istart = rec.indexOf(",", istart) + 1;
    }

    iend = rec.indexOf(",", istart);
    if (iend < 0)
    {
        iend = rec.length;
    }
    
    return rec.substring(istart, iend);
}



