// WARNING! This file is not parsed on every request, so you
// cannot use nHTML within this file.  --res 6/1/00

var gErrorUrl="";

function OpenHelpErrorHandler() 
{
    if (gErrorUrl != "")
    {
        location.href = gErrorUrl;
        return true;
    }
    else
    {
        return false;
    }
}


function OpenHelp(url)
{        
    window.onerror = OpenHelpErrorHandler;
    gErrorUrl = url;
    var hWnd = window.open(url, "NVOHelp", "menubar=yes,toolbar=yes,width=630,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 needed for IE 4.5 which doesn't run the onChange scripts
// until submit time (or unload time).  (SN 12/7/99)
function RemoveFocusAll()
{
	if (document.nvoForm)

	{
		var vers = navigator.appVersion;
		if (vers.indexOf("Mac") != -1 && vers.indexOf("IE") != -1)
		{
			var i = 0;

	    	for (i = document.nvoForm.elements.length - 1; i >= 0 ; i--)


			{
				if (document.nvoForm.elements[i].type == "text" || document.nvoForm.elements[i].type == "textarea")
				{
					document.nvoForm.elements[i].blur();
				}
			}
		}
	}
}


// this is the action string used to save the standard pages properties

function GetStdAction()
{
	var hidePage = (document.nvoForm.hidePage && document.nvoForm.hidePage.checked) ? "ON" : "OFF";
	var hideSignIn = (document.nvoForm.hideSignIn && document.nvoForm.hideSignIn.checked) ? "OFF" : "ON";

	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 
    			+"&.Sitemap.Hide-SignIn." + document.nvoForm.ObjectUID.value + "=" + hideSignIn +");";
}


var gSaveAndGoURL = "";
var gChanged = false;  // set to true if page has been modified



function JavaTextAreaErrorHandler()
{
    alert("Siden kan ikke gemmes, da Java-teksteditoren ikke blev indlæst. Accepter indstillingerne i dialogboksen Sikkerhed for denne side, og vent til siden er helt indlæst. Klik på knappen til genindlæsning. Hvis det ikke hjælper, kan du lukke browseren og genstarte den."); 
    return true;
}


var gHaveCalledUpdateTextAreas = false;


function UpdateTextAreas()
{
	var i = 0;
    var applet;
    var appletindex;

    if (!gHaveCalledUpdateTextAreas)
    {
        gHaveCalledUpdateTextAreas = true;
        
        if (document.nvoForm)
	    {
    	    window.onerror = JavaTextAreaErrorHandler;

    	    for (i = 0; i < document.nvoForm.elements.length; i++)
		    {        
        	    if (document.nvoForm.elements[i].name == "!minijane")
			    {
            	    appletindex = parseInt(GetField(document.nvoForm.elements[i].value, 1)) - 1;

            	    applet = document.applets[appletindex];

            	    if (!applet.forbiddenWordCheck())
            	    {
                	    window.onerror = null;
				        gHaveCalledUpdateTextAreas = false;
                	    return false;
            	    }

            	    if (applet.HTMLChanged())
                	    gChanged = true;

            	    // copy the contents of the miniJane editor into the hidden field
            	    document.nvoForm.elements[i + 1].value = applet.getHTML();

                    applet.destroy();
			    }
		    }
	    }

        window.onerror = null;
    }

    return true;
}


function CheckForTextChange()
{
	// If there are any miniJane applets on the page, check to see if text has changed
    var applet;
    var appletindex;

    if (document.nvoForm && document.nvoForm.elements)
    {
    for (var i = 0; i < document.nvoForm.elements.length; i++)
    {        
   	    if (document.nvoForm.elements[i].name == "!minijane")
	    {
       	    appletindex = parseInt(GetField(document.nvoForm.elements[i].value, 1)) - 1;

       	    // the appet name is the same as the define name
       	    applet = document.applets[appletindex];

       	    if (applet.HTMLChanged())
          	    gChanged = true;
	    }
	}
    }
}

function DoGoto(url, uid)
{
	CheckForTextChange();

	RemoveFocusAll();
    if (gChanged || uid == "")
    {
        gSaveAndGoURL = url;
        DoSave();
    }
    else
    {
        location.href = ReplaceString(url, "*", uid);
    }
}


// save the form data and goto the specified url

var hitreturn = false;

function DoDefault()
{
	if (!hitreturn) 
	{
 		hitreturn = true;
 		DoSave();
  	}
    return true;
}


function SaveAndGo(url)
{
	CheckForTextChange();

    RemoveFocusAll();
    if (gChanged)
    {
        gSaveAndGoURL = url;
        DoSave();
    }
    else
    {
        location.href = url;
    }
}


function Changed(url)
{
	gChanged = true;
}


function IsNetscape()
{
	return (navigator.appName.substring(0, 8) == "Netscape");
}


function WarnIfUsingOldBrowser()
{
	if (parseInt(navigator.appVersion) < 4)
	{
		if (IsNetscape())
			alert("For at redigere dit Websted skal du bruge en nyere version af Netscape Communicator. Besøg Netscapes Websted på adressen www.netscape.com for at få den nyeste version. Hvis du ikke gør det, kan det medføre upålidelig funktion.");
		else
			alert("For at redigere dit Websted skal du bruge en nyere version af Internet Explorer. Besøg Microsofts Websted på adressen www.microsoft.com for at få den nyeste version. Hvis du ikke gør det, kan det medføre upålidelig funktion.");
	}
} 


function NormalizeObjectName(name)
{
	var	newName = "";

	for (var i = 0; i < name.length; i++)

	{
		charCode = name.charCodeAt(i);
		if (charCode < 0)
			charCode += 256;
		
		// Remove the chars \ / : * ? \" < > | ' 
		// and handle only ascii values between 32 and 128
		
		if (charCode == 38			/*  &  */
			|| charCode == 47		/*  /  */
			|| charCode == 92		/*  \  */
			|| charCode == 58		/*  :  */
			|| charCode == 42		/*  *  */
			|| charCode == 63		/*  ?  */
			|| charCode == 34		/*  "  */
			|| charCode == 60		/*  <  */
			|| charCode == 62		/*  >  */
			|| charCode == 124		/*  |  */
			|| charCode == 39)		/*  '  */
		{
			// do nothing
		}
		else if (charCode > 32 && charCode < 128)
			newName += name.substr(i, 1);
	}
	
    // if we eliminated all chars, name the object "page" 
	if (newName == "")
		newName = "page";
		
	return newName;
	
}


function RemoveAmpersand(name)
{
	var	newName = "";

	for (var i = 0; i < name.length; i++)

	{
		charCode = name.charCodeAt(i);
		if (charCode < 0)
			charCode += 256;
		
		
		if (charCode == 38)		/*  &  */
		{
			/* do nothing */
		}
		else
			newName += name.substr(i, 1);
	}
	
	/* if we eliminated all chars, name the object "page" */
	if (newName == "")
		newName = "page";
		
	return newName;
	
}


function DoButton(buttonAction)
{
	if ((document.nvoForm.PageName) && (document.nvoForm.PageName.value == ""))
    {
    	alert("Du skal angive et sidenavn. Feltet kan ikke stå tomt. Angiv et gyldigt navn.");
    	document.nvoForm.PageName.focus();
    	document.nvoForm.PageName.select();
    	location.href = "#pagename";
    	return;
    }
    
	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("Vent, indtil siden er indlæst helt, og prøv derefter igen.");
    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("Du skal have den gratis Visitor Software for at bruge denne funktion. Hent og installer 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("Du skal aktivere Java i din browser for at bruge denne funktion.");
	    }
	    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("Du skal have den gratis Visitor Software for at bruge denne funktion. Hent og installer 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;
    }
    
    var temp = rec.substring(istart, iend);
    return temp;
}


function StdDelete(singularname, pluralname, profile, list, stdeditpage, cbName)
{
    var     action = "";
    var     count = 0;
	var		m1, m2;


	// cbName was added to allow multiple list per page, if null it is the origninal function
   	if (cbName == null)
		cbName = "cb";
  
    if (stdeditpage)
        action = GetStdAction();
    
    action += "UseProfile(Name=" + profile + ");Save()";

    for (var i = 0; i < document.nvoForm.elements.length; i++)
    {
		el = document.nvoForm.elements[i];
		if (el.name == cbName && el.checked)
        {
            uid = GetField(el.value, 0);
            action +=";Delete(List=" + list + "&UID=" + uid + ")";
            count++;
        }
    }

    if (count > 1)
    {
		m1 = "Er du sikker på, at du vil slette ^^^count^^^ ^^^pluralname^^^?";
		m2 = ReplaceString(m1, "^^^count^^^", count);
        if (confirm(ReplaceString(m2, "^^^pluralname^^^", pluralname)))
        {
			if (!UpdateTextAreas())
        		return;
            DoButton(action);
        }
    }
    else
    {
	    if (count == 1)
	    {
			m1 = "Er du sikker på, at du vil slette ^^^count^^^ ^^^singularname^^^?";
			m2 = ReplaceString(m1, "^^^count^^^", count);
	        if (confirm(ReplaceString(m2, "^^^singularname^^^", singularname)))
	        {
				if (!UpdateTextAreas())
    	    		return;
	            DoButton(action);
	        }
	    }
	    else
	    {
			m1 = "Marker afkrydsningsfelterne for at vælge et eller flere elementer, og klik derefter på Slet.";
	        alert(ReplaceString(m1, "^^^pluralname^^^", pluralname));
	    }
	}
}


function StdMove(name, moveurl, helpurl, profile, list, field, doneurl, query, uid, extraretparms, cbName)
{
    var     startuid = "";
    var     enduid = "";

	if (!uid)
		uid = "";
		
	// cbName was added to allow multiple list per page, if null it is the origninal function
   	if (cbName == null)
		cbName = "cb";

	for (var i = 0; i < document.nvoForm.elements.length; i++)
    {
		el = document.nvoForm.elements[i];
		if (el.name == cbName && el.checked)
        {
            if (startuid.length == 0)
                startuid = GetField(el.value, 0);
            else
                enduid = GetField(el.value, 0);
        }
    }

    if (startuid.length > 0)
    {
        if (enduid.length == 0)
            enduid = startuid;
		action = moveurl + "?startuid=" + startuid + "&enduid=" + enduid + "&profile=" + profile + "&list=" + list + "&field=" + field + 
				  "&name=" + escape(name) + "&doneurl=" + doneurl + "&uid=" + uid + "&helpurl=" + helpurl + query;
		if (extraretparms)
			action += "&extraretparms=" + extraretparms;
        SaveAndGo(action);
    }
    else
    {
		var   message = "Marker afkrydsningsfelterne for at vælge elementet, og klik derefter på Flyt.";
		alert(ReplaceString(message, "^^^name^^^", name));
    }
}


function StdEdit(name, editurl, editlisturl, query, cbName)
{
    var     uidlist = ",";
    var     count = 0;
    
   	// cbName was added to allow multiple list per page, if null it is the origninal function
   	// At this time cbName is not used because most 2nd list have there own edit function
   	// This was changed for future consistency reasons
   	if (cbName == null)
		cbName = "cb";

    for (var i = 0; i < document.nvoForm.elements.length; i++)
    {
		el = document.nvoForm.elements[i];
		if (el.name == cbName && el.checked)
        {
            uid = GetField(el.value, 0);
            uidlist += uid + ",";
            count++;
        }
    }

    if (count > 0)
    {
        if (count == 1)
            SaveAndGo(editurl + "?uid=" + uid + query);
        else
        {
            if (editlisturl == "")
                alert("Vælg kun ét element, der skal redigeres.")
            else
                SaveAndGo(editlisturl + "?count=" + count + "&uidlist=" + uidlist + query);
        }
    }
    else
    {
		var message = "Marker afkrydsningsfelterne for at vælge elementet, og klik derefter på Rediger.";
        alert(ReplaceString(message, "^^^name^^^", name));
    }
}


function ReplaceString(org, match, replace)
{
    var     dst = "";
    var     starti = 0;
    var     endi = 0;

    while ((endi = org.indexOf(match, starti)) >= 0)
    {
        dst += org.substring(starti, endi) + replace;
        starti = endi + match.length;
    }

    dst += org.substring(starti, org.length);

    return dst;
}


function IsTrue(value)
{
	upperCaseValue = value.toUpperCase();
	return (upperCaseValue    != ""
			&& upperCaseValue != "NO" 
			&& upperCaseValue != "FALSE"
			&& upperCaseValue != "OFF"
			&& upperCaseValue != "0");
}


function DoSelectAll(elementName, cbName)
{

	var originalElementName = "FALSE";
	
	// If both are null then this was an original single list
	// this requires both params: elementName & cbName if this is a 2nd list
	if (elementName == null && cbName == null)
	{	
		originalElementName = "TRUE";		
		cbName = "cb";
		elementName = document.nvoForm.allSelected.value;
	}
	
	for (var i = 0; i < document.nvoForm.elements.length; i++)
	{

		el = document.nvoForm.elements[i];
		
		if (el.name == cbName)
			el.checked = (elementName == "check");
			
	}

    // toggle values

	if (originalElementName == "TRUE")
	{
		if (document.nvoForm.allSelected.value == "check")
			document.nvoForm.allSelected.value = "uncheck";
		else
			document.nvoForm.allSelected.value = "check";
	}
	else
	{
		// This is returned and stored in the element name for 2nd or 3rd list.
		if (elementName == "check")
			return "uncheck";
		else
			return "check";
	}
}


function setStatusBar(msg)
{
	window.status = msg;
	return true;
}


function FindSelectedElement(name, buttonName, action, messageNone, messageTooMany)
{
	sel = null;

	numsel = 0;
	for (var i = 0; i < document.nvoForm.elements.length; i++)
	{
		el = document.nvoForm.elements[i];
		if (el.name == name && el.checked)
		{
			numsel++;
			sel = el;
		}
	}

	if (numsel == 0)
	{
		
		alert(ReplaceString(messageNone, "^^^buttonName^^^", buttonName));
	}
	else if (numsel > 1)
	{
		alert(ReplaceString(messageTooMany, "^^^action^^^", action));
		sel = null;
	}

	return sel;
}


function CheckURL(urlElement)
{
	if (urlElement.value.length > 0 && urlElement.value.indexOf("://") == -1)
	{
		urlElement.value = "http://" + urlElement.value
	}
	urlElement.value = ReplaceString(urlElement.value, " ", "%20");
	Changed();
}


function MakePositiveNumber(number)
{
	if (parseInt(number.value) > 0)
	{
        number.value = number.value;
	}
	else if (parseInt(number.value) < 0)
	{
		number.value = parseInt(number.value) * -1;
	}
	else
	{
		number.value = 1;
	}
	number.value = ReplaceString(number.value, " ", "%20");
	Changed();
}


// This is used for edit pages with 2 (or more) lists

function GotoSelectAll(CheckBoxName)
{
	if (!CheckBoxName)
		CheckBoxName = "cb";	

	switch(CheckBoxName)
	{
		case "cb":
			DoSelectAll();
			break;

		case "cb2":
			elementName = document.nvoForm.allSelected2.value
			document.nvoForm.allSelected2.value = DoSelectAll(elementName, "cb2");
			break;
	}
}

function OpenSetupWiz(theURL) {
  var wzWnd = window.open(theURL,'wizard','width=630,height=400');
    if (!wzWnd.opener) wzWnd.opener = self;
    if (wzWnd.focus != null) wzWnd.focus();
}


function AppendQuery(url, query)
{
	if (query != "")
	{
		if (url.indexOf("?") != -1) // if has ?
			url += "&";
		else
			url += "?";
		url += query;
	}
	return url;
}

function DoPictureGoto(useObject, isList, path, profile, displayBoxName, saveFileName, returnURL, fileName, uid)
{
	// useObject - indicates that the destination expects the profile to be passed as the "object" param
	// isList - indicates that we are dealing with a picture from a list
	// path - location of destination page
	// profile - the profile to save the picture into
	// displayBoxName - where to save state of "display picture" checkbox
	// saveFileName - where to save new image filename
	// returnURL - where to return after picking the picture
	// fileName - location of image
	// uid - if a list, uid of picture
	//
	var url = path;

    // Update gChanged flag if necessary, and check for forbidden words before going to picture page
	if (!UpdateTextAreas())
        return;
	
	if (useObject)
		url += "?object=";
	else
		url += "?profile=";
		
	url += profile;
	
	if (isList == "TRUE") // $ISTRUE() will return "TRUE" or "FALSE"
	{
		url += "&UID=*";
		url += "&baseName=";
		url += saveFileName;
		url += "&include=";
		url += displayBoxName;
		if (displayBoxName != "")
		    url += ".*";
		url += "&name=";
		url += saveFileName;
        if (saveFileName != "")
		    url += ".*";

        // add the UID to the returnURL if it does not already include it
        if ((returnURL.indexOf("uid=") == -1) && (returnURL.indexOf("UID=") == -1))
        {
            tmpVar = AppendQuery(returnURL, "UID=*")
            returnURL = tmpVar;
        }

	}
	else
	{
		url += "&include=";
		url += displayBoxName;
		url += "&name=";
		url += saveFileName;
	}
	
	url += "&url="
	url += escape(returnURL);
	url += "&initial=";
	url += escape(fileName);
	
	if (isList == "TRUE")
		DoGoto(url, uid);
	else
		SaveAndGo(url);
}


function NibbleToHexDigit (nibble)
{
    return "0123456789ABCDEF".charAt(nibble);
}

function Escape (name)
{
    var c;
	var	newName = "";

	for (var i = 0; i < name.length; i++)
	{
		c = name.charCodeAt(i);
		// Escape: space " # % & * + : < > ?
        if (c == 32 || c == 34 || c == 35 || c == 37 || c == 38 ||
            c == 42 || c == 43 || c == 58 || c == 60 || c == 62 || c == 63)
        {
            newName += "%";
            newName += NibbleToHexDigit((c & 0xf0) >> 4);
            newName += NibbleToHexDigit( c & 0x0f);
        }
        else
            newName += name.charAt(i);
    }
		
	return newName;
}

function SwitchMiniJane(isList, uid)
{
    var refreshURL = document.location.href;
    var tmpVar = "";
    
    if (refreshURL.indexOf("?") == -1)
    {
       tmpVar = AppendQuery(refreshURL, "post=TRUE")
       refreshURL = tmpVar
    }
	
	if (IsTrue(isList))
    {
        if ((refreshURL.indexOf("uid=") == -1) && (refreshURL.indexOf("UID=") == -1))
        {
            tmpVar = AppendQuery(refreshURL, "UID=*");
            refreshURL = tmpVar;
        }
    }

    if (!IsTrue(document.nvoForm.miniJaneFlag.value))
    {
        if (!IsTrue(document.nvoForm["Use-MiniJane"].value))
        {
            document.nvoForm["Use-MiniJane"].value = "ON"
        }
        else if (IsTrue(document.nvoForm["Use-MiniJane"].value))
        {
            document.nvoForm["Use-MiniJane"].value = "OFF"
        }
    }

    document.nvoForm.miniJaneFlag.value = "TRUE"
    Changed();
	if (IsTrue(isList))
        DoGoto(refreshURL, uid);
    else 
        SaveAndGo(refreshURL)
}
