// ***************************************
// iPrint.js
// Copyright 2000-2007, Novell Inc. All rights reserved
// ***************************************

// ***************************************
// CONSTANTS
// ***************************************

// Constants for the option type
DEF_TYPE_OPTION_NO = 0;    // Check box is not checked by default
DEF_TYPE_OPTION_YES = 1;   // Check box is checked by default
DEF_TYPE_OPTION_NONE = -1; // No check box is displayed

// Set this constant to change the default check box behavior
// (See "Constants for the option type" above.)

var defTypeGbl = DEF_TYPE_OPTION_NO;

// iPrint Client Current Version
IPRINT_CLIENT_MIN_VER    = 1.07;

// Debug Stuff
DEBUG = 0;

// Increase this value for slow computers (used with iPrint client install)
INSTALL_TIMEOUT_VALUE = 1000;
MODAL_TIME_OUT = 50;

// Operation Messages Constants
OP_CLIENT_INTERFACE_VERSION_MSG    = "op-client-interface-version";
OP_CLIENT_VERSION_INFO_MSG         = "op-client-version-info";
OP_CLIENT_IS_PRINTER_INSTALLED_MSG = "op-client-is-printer-installed";
OP_USER_GET_ROLE_MSG               = "op-user-get-role";
OP_PRINTER_INSTALL_MSG             = "op-printer-install";
OP_PRINTER_REMOVE_MSG              = "op-printer-remove";
OP_PRINTER_GET_STATUS_MSG          = "op-printer-get-status";
OP_PRINTER_GET_INFO_MSG            = "op-printer-get-info";
OP_PRINTER_PAUSE_MSG               = "op-printer-pause";
OP_PRINTER_RESUME_MSG              = "op-printer-resume";
OP_PRINTER_PURGE_JOBS_MSG          = "op-printer-purge-jobs";
OP_PRINTER_LIST_USERS_JOBS_MSG     = "op-printer-list-users-jobs";
OP_PRINTER_LIST_ALL_JOBS_MSG       = "op-printer-list-all-jobs";
OP_PRINTER_SEND_TEST_PAGE_MSG      = "op-printer-send-test-page";
OP_PRINTER_SEND_FILE_MSG           = "op-printer-send-file";
OP_JOB_GET_INFO_MSG                = "op-job-get-info"
OP_JOB_HOLD_MSG                    = "op-job-hold"
OP_JOB_RELEASE_HOLD_MSG            = "op-job-release-hold"
OP_JOB_CANCEL_MSG                  = "op-job-cancel"
OP_JOB_RESTART_MSG                 = "op-job-restart"
OP_DBG_PRINTER_GET_ALL_ATTRS_MSG   = "op-dbg-printer-get-all-attrs";
OP_LIST_LOCAL_PRINTERS_MSG         = "op-list-local-printers";
OP_PRINTER_UPLOAD_MSG              = "op-printer-upload";
OP_PRINTER_UPLOAD_TO_RMS_MSG       = "op-printer-upload-to-rms";

// Operation enumeration constants
OP_NONE                            = -1;
OP_CLIENT_INTERFACE_VERSION        =  0;
OP_CLIENT_VERSION_INFO             =  1;
OP_CLIENT_IS_PRINTER_INSTALLED     =  2;
OP_USER_GET_ROLE                   =  3;
OP_PRINTER_INSTALL                 =  4;
OP_PRINTER_REMOVE                  =  5;
OP_PRINTER_GET_STATUS              =  6;
OP_PRINTER_GET_INFO                =  7;
OP_PRINTER_PAUSE                   =  8;
OP_PRINTER_RESUME                  =  9;
OP_PRINTER_PURGE_JOBS              = 10;
OP_PRINTER_LIST_USERS_JOBS         = 11;
OP_PRINTER_LIST_ALL_JOBS           = 12;
OP_PRINTER_SEND_TEST_PAGE          = 13;
OP_PRINTER_SEND_FILE               = 14;
OP_JOB_GET_INFO                    = 15;
OP_JOB_HOLD                        = 16;
OP_JOB_RELEASE_HOLD                = 17;
OP_JOB_CANCEL                      = 18;
OP_JOB_RESTART                     = 19;
OP_DBG_PRINTER_GET_ALL_ATTRS       = 20;
OP_LIST_LOCAL_PRINTERS             = 21;
OP_PRINTER_UPLOAD                  = 22;
OP_PRINTER_UPLOAD_TO_RMS           = 23;

// iPrint Events for Installing/Re-installing, and Managing Printers
IPRINT_EVENT_UNKNOWN               = -1;
IPRINT_EVENT_ALERT                 =  0;
IPRINT_EVENT_STATUS                =  1;
IPRINT_EVENT_INSTALL               =  2;
IPRINT_EVENT_REINSTALL             =  3;
IPRINT_EVENT_UNINSTALL             =  4;
IPRINT_USER_JOB_MANAGEMENT         =  5;
IPRINT_ADMIN_JOB_MANAGEMENT        =  6;

// Error Code Constants
ERR_USER_CANCELLED_LOGIN           = 65552;
ERR_NO_PRINTER_DRIVER_ASSOCIATED   = 65550;
ERR_SECURE_PRINTER_OLD_CLIENT      = 1801;

// New Client constants
WIN_9X_NT4_INSTALLER               = "./clients/win-9x-nt4/nipp.exe";
WIN_2K_INSTALLER                   = "./clients/win-2k/nipp.exe";
WIN_XP_INSTALLER                   = "./clients/win-xp/nipp.exe";
WIN_VISTA_INSTALLER                = (navigator.userAgent.indexOf("Win64") > -1 ||
                                      navigator.userAgent.indexOf("WOW64") > -1 ||
                                      navigator.userAgent.indexOf("x64") > -1 ||
                                      navigator.userAgent.indexOf("x86_64") > -1) ?
                                     "./clients/win-vista/x86_64/nipp.exe" :
                                     "./clients/win-vista/x86/nipp.exe";

LINUX_INSTALLER                    = "JavaScript:ShowX11iPrintClientInstructions();";
LINUX_NLD9_INSTALLER_LINK          = (navigator.userAgent.indexOf("x86_64") > -1) ?
                                     "./clients/linux-nld9/x86_64/novell-iprint-xclient.x86_64.rpm" :
                                     "./clients/linux-nld9/x86/novell-iprint-xclient.i586.rpm";
LINUX_INSTALLER_LINK               = (navigator.userAgent.indexOf("x86_64") > -1) ?
                                     "./clients/linux/x86_64/novell-iprint-xclient.x86_64.rpm" :
                                     "./clients/linux/x86/novell-iprint-xclient.i586.rpm";
MAC_INSTALLER                      = "./clients/mac/iprint.dmg";
UNKNOWN_INSTALLER                  = "JavaScript:alert('Unsupported client type');";

// New Platform constants
PLATFORM_TYPE_OTHER                = -1; // Unknown
PLATFORM_TYPE_WINDOWS_9X_NT4       =  0; // Windows 95/98/NT
PLATFORM_TYPE_WINDOWS_2K           =  1; // Windows 2000
PLATFORM_TYPE_WINDOWS_XP           =  2; // Windows XP
PLATFORM_TYPE_WINDOWS_2003_SERVER  =  4; // Windows 2003 Server
PLATFORM_TYPE_WINDOWS_VISTA        =  8; // Windows Vista
PLATFORM_TYPE_LINUX_NLD9           =  20; // Linux NLD9 or earlier
PLATFORM_TYPE_LINUX                =  21; // Linux SLED10 or later
PLATFORM_TYPE_MAC                  =  40; // Mac OSX

// New Browser constants
BROWSER_TYPE_OTHER                 = -1; // Unknown
BROWSER_TYPE_IE                    = 0; // Microsoft Internet Explorer
BROWSER_TYPE_MOZILLA               = 1; // Mozilla based browsers (Includes Netscape, FireFox and IE for MAC)

// X11 iPrint Client Installation Dialog constants
X11_DIALOG_WIDTH                   = 600;
X11_DIALOG_HEIGHT                  = 550;

// ***************************************
// GLOBAL VARIABLES
// ***************************************

var pNameGbl = "";
var persistenceGbl = "";
var defaultOpGbl = "";
var onInstalledActionGbl = "";
var onNotInstalledActionGbl = "";
var browserLangGbl = "";
var browserTypeGbl;
var platformTypeGbl;
var serverName = "";
var printerInstalled = "Not Checked";
var clientInstallerGbl;
var clientInstallerLinkGbl;
var backGbl = -1;
var paInfoWinGbl;
var setAsDefaultGbl = false;
var dialogWinGbl = new Object();
var linksGbl;
var isNav4Gbl = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4));;

// ***************************************
// FUNCTIONS
// ***************************************

// ***************************************
// Function: ShowX11iPrintClientInstructions
// ***************************************
function ShowX11iPrintClientInstructions()
{
  var x11Instructions = window.open("./x11_inst.htm",
                                    "xllinst",
                                    "width=" + X11_DIALOG_WIDTH +
                                    ",height=" + X11_DIALOG_HEIGHT +
                                    ",left=" + ((screen.width - X11_DIALOG_WIDTH) / 2) +
                                    ",top=" + ((screen.height - X11_DIALOG_HEIGHT) / 2) +
                                    ",toolbar=no");

  if(x11Instructions)
  {
    x11Instructions.focus();
  }
  else
  {
    alert(POPUPS_DISABLED_MSG);
  }
}

// ***************************************
// Function: IsiPrintClientInstalled
// Purpose:  Returns 
// (DONE)
// ***************************************
function IsiPrintClientInstalled()
{
  if((platformTypeGbl == PLATFORM_TYPE_WINDOWS_9X_NT4 || 
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2K ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_XP || 
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2003_SERVER ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_VISTA)
      && browserTypeGbl == BROWSER_TYPE_IE)
  {
    // Internet Explorer for Windows
    var activeXOk = false;
    eval('try{var xObj = new ActiveXObject("ienipp.Novell iPrint Control"); if(xObj)activeXOk = true;}catch(e){}');
    return activeXOk;
   }
  else
  {
    // All other browsers assume Mozilla based (including IE for MAC)
    if(navigator.plugins)
    {
      for(var i = 0; i < navigator.plugins.length; i++)
      {
        if((navigator.plugins[i].name.toLowerCase().indexOf("iprint") > -1) ||
           (navigator.plugins[i].name.toLowerCase().indexOf("npnipp") > -1))
          return true;
      }
    }
  }
  return false;
}

// ***************************************
// Function: IsiPrintClientCurrent
// Purpose:  Returns
// ***************************************
function IsiPrintClientCurrent()
{
  var interfaceVersionString;
  var interfaceVersion;

  if((platformTypeGbl == PLATFORM_TYPE_WINDOWS_9X_NT4 || 
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2K ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_XP ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2003_SERVER ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_VISTA)
      && browserTypeGbl == BROWSER_TYPE_IE)
  {
    interfaceVersionString = iPrintAccess.ExecuteRequest("op-client-interface-version",
                                                         "&result-type=object"); 
    interfaceVersion = parseFloat(GetValue("interfaceVersion", interfaceVersionString));
    return (interfaceVersion >= IPRINT_CLIENT_MIN_VER);
  }
  else
  {
    // Get the interface version for Mozilla
    // Return true for now until this function is complete...
    return true;
  }
}

// ***************************************
// Function: ParseServerName
// Purpose:  Get the server name from the printer address
// ***************************************
function ParseServerName(printerName)
{
  var retVal = "";
  var colonSlashSlashPos = printerName.indexOf("://");
  var nextColonPos = 0;
  var nextSlashPos = 0;
  var startPos = 0;
  var endPos = 0;

  if(colonSlashSlashPos > -1)
  {
    startPos = colonSlashSlashPos + "://".length;
    nextColonPos = printerName.indexOf(":", startPos);
	nextSlashPos = printerName.indexOf("/", startPos);
    
	if(nextColonPos > -1 && nextSlashPos > -1 && nextColonPos < nextSlashPos)
	{
	  endPos = nextColonPos;
	}
	else if(nextSlashPos > -1)
	{
	  endPos = nextSlashPos;
	}
	else
	{
	  endPos = printerName.length;
	}
    retVal = printerName.substring(startPos, endPos);
  }

  return retVal;
}

// ***************************************
// Function: IsHTTPSSupported
// Purpose:  If using an older server (NW65SP2 or older),
//           or the client is older then return true
// Note:  This function is only used with Internet Explorer
// ***************************************
function IsHTTPSSupported(printerNameStr)
{
  var retVal = true;
  var ippSrvrAddressStr = "";
  var serverNameStr = ParseServerName(printerNameStr);
  var serverVerStr = "";
  
  if(serverNameStr != "")
  {
    ippSrvrAddressStr = "http://" + serverNameStr + "/ipp";    
  }  

  if(ippSrvrAddressStr != "")
  {
    if((platformTypeGbl == PLATFORM_TYPE_WINDOWS_9X_NT4 || 
        platformTypeGbl == PLATFORM_TYPE_WINDOWS_2K ||
        platformTypeGbl == PLATFORM_TYPE_WINDOWS_XP ||
        platformTypeGbl == PLATFORM_TYPE_WINDOWS_2003_SERVER ||
        platformTypeGbl == PLATFORM_TYPE_WINDOWS_VISTA)
        && browserTypeGbl == BROWSER_TYPE_IE)
  	{
      if(typeof iPrintAccess.GetServerVersion != "undefined")
	  {
	    serverVerStr = iPrintAccess.GetServerVersion(ippSrvrAddressStr);
		if(serverVerStr != "")
		{
		  retVal = !((serverVerStr.indexOf("Linux") > -1) || (serverVerStr.indexOf("iprintserverversion") > -1));
		}
	  }
	}
  }
  return retVal;
}


// ***************************************
// Function: CheckPrinterInstall
// Purpose:  Returns 
// ***************************************
function CheckPrinterInstall()
{
  var installed;
  var error;

  // Check to see if the iPrint printer is already installed
  if((platformTypeGbl == PLATFORM_TYPE_WINDOWS_9X_NT4 || 
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2K ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_XP ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2003_SERVER ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_VISTA)
      && browserTypeGbl == BROWSER_TYPE_IE)
  {
    installed = iPrintAccess.ExecuteRequest("op-client-is-printer-installed", 
//                                          "printer-url=" + DecodeUri(pNameGbl) +
                                            "printer-url=" + pNameGbl +
                                            "&result-type=object");

    error = GetValue("Programmatic Error Info", installed);
    installed = GetValue("printerInstalled", installed).toLowerCase();

	if(error != "")
	{
      if((error.indexOf("1801") > -1 || error.indexOf("SSL") > -1) && IsHTTPSSupported(pNameGbl))
      {
        pNameGbl= pNameGbl.replace(/^ipp:/gi, "https:").replace(/\/ipp\//gi,"/ipps/");
        installed = iPrintAccess.ExecuteRequest("op-client-is-printer-installed", 
//                                                "printer-url=" + DecodeUri(pNameGbl) +
                                                "printer-url=" + pNameGbl +
                                                "&result-type=object");

        error = "";
        error = GetValue("Programmatic Error Info", installed);
        installed = GetValue("printerInstalled", installed).toLowerCase();

        if(error != "")
	{
	  alert(error);
	  GoBack();
	}
	else if(installed != "true")
	  PrinterIsNotInstalledAction();
	else
	  PrinterIsInstalledAction();
      }
      else if(error.indexOf("65552") > -1 || error.indexOf("65561") > -1)
      {
	    // Do nothing
		GoBack();
      }
      else
	  {
        alert(error);
		GoBack();
	  }
    }
	else if(installed != "true")
	  PrinterIsNotInstalledAction();
    else
      PrinterIsInstalledAction();
  }
  else if(platformTypeGbl == PLATFORM_TYPE_MAC)
  {
    // Need to determine how to check if the printer is installed using a Mac
    PrinterIsNotInstalledAction();
  }
  else
  {
    backGbl--;
    request.document.writeln("<" + "HTML>");
    request.document.writeln("<" + "BODY>");
    request.document.writeln("<EMBED TYPE=\"application/x-Novell-ipp\"" +
                              " printer-url=\"" + pNameGbl + "\"" +
                              " operation=\"op-client-is-printer-installed\"" +
                              " HIDDEN=\"true\"" +
                              " result-type=\"url\"" +
                              " target-frame=\"response\"" +
                              " call-back-url=\"response.htm\">" + 
                              "</EMBED>");
    request.document.writeln("</" + "BODY>");
    request.document.writeln("</" + "HTML>");
    request.document.close();
  }
}

// ***************************************
// Function: PrinterIsInstalledAction
// Purpose:  Returns 
// ***************************************
function PrinterIsInstalledAction()
{
    alert(PRT_ALREADY_INSTALLED_MSG);
    GoBack();
}

// ***************************************
// Function: GetPrinterAgentURL
// ***************************************
function GetPrinterAgentURL(ippUrl)
{
  var tempUrl = ippUrl.replace(/^ipp:|^ipps:|^https:/gi, "http:").replace(/\/ipps\//gi, "/ipp/").replace(/:443/gi, ":631");

  if(tempUrl.indexOf(":631") == -1)
  {
    tempUrl = tempUrl.replace(/\/ipp\//gi, ":631/ipp/");
  }

  return tempUrl;
}

// ***************************************
// Function: InstalliPrintClient
// Purpose:  Returns 
// ***************************************
function InstalliPrintClient()
{
  // Install the iPrint Client
  if(clientInstallerGbl == WIN_9X_NT4_INSTALLER ||
     clientInstallerGbl == WIN_2K_INSTALLER ||
     clientInstallerGbl == WIN_XP_INSTALLER ||
     clientInstallerGbl == WIN_VISTA_INSTALLER)
  {
    parent.document.location.replace("./win_inst.htm");
  }
  else if(clientInstallerGbl == LINUX_INSTALLER)
  {
    parent.document.location.replace("./x11_inst.htm");
  }
  else
  {
    request.location.replace(clientInstallerGbl);
  }
}

// ***************************************
// Function: DisableEvents
// Purpose:  Disables events while a cross-browser
//           modal dialog is displayed.
// ***************************************
function DisableEvents()
{
  if(isNav4Gbl)
  {
    window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
	window.onclick = NullEvent;
  }
  else
  {
    DisableLinks();
  }
  window.onfocus = CheckModal;  
}

// ***************************************
// Function: EnableEvents
// Purpose:  Enables events that were previously disabled
//           using the DisableEvents function
// ***************************************
function EnableEvents()
{
  if(isNav4Gbl)
  {
    window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
	window.onclick = null;
	window.onfocus = null;
  }
  else
  {
    EnableLinks();
  }
}

// ***************************************
// Function: DisableLinks
// Purpose:  Disables links while a cross-browser
//           modal dialog is displayed
// ***************************************
function DisableLinks()
{
  linksGbl = new Array();
  var i, j;

  for(i = 0; i < document.forms.length; i++)
  {
    for(j = 0; j < document.forms[i].elements.length; j++)
    {
      document.forms[i].elements[j].disable = true;
    }
  }

  for(i = 0; i < document.links.length; i++)
  {
    linksGbl[i] = {click:document.links[i].onclick, up:null};
    linksGbl[i].up = document.links[i].onmouseup;
    document.links[i].onclick = NullEvent;
    document.links[i].onmouseup = NullEvent;
    document.links[i].disabled = true;
  }
  window.onfocus = CheckModal;
  document.onclick = CheckModal;
}

// ***************************************
// Function: EnableLinks
// Purpose:  Enables links that were previously disabled
//           using the DisableLinks function
// ***************************************
function EnableLinks()
{
  var i, j;

  for(i = 0; i < document.forms.length; i++)
  {
    for(j = 0; j < document.forms[i].elements.length; j++)
    {
      document.forms[i].elements[j].disabled = false;
    }
  }  

  for(i = 0; i < document.links.length; i++)
  {
    document.links[i].onclick = linksGbl[i].click;
    document.links[i].omouseup = linksGbl[i].up;
    document.links[i].disabled = false;
  }
}


// ***************************************
// Function: NullEvent
// Purpose:  Sets the focus back to the cross-browser
//           modal dialog
// ***************************************
function NullEvent()
{
  // Set Focus to the Open Dialog
  if(dialogWinGbl.win && !dialogWinGbl.closed)
  {
    dialogWinGbl.win.focus();
    return false;
  }
}

// ***************************************
// Function: CheckModal
// Purpose:
// ***************************************
function CheckModal()
{
  setTimeout("IsDialogModal()", MODAL_TIME_OUT);
  return true;
}

// ***************************************
// Function: IsDialogModal
// Purpose:
// ***************************************
function IsDialogModal()
{
  if(dialogWinGbl.win && !dialogWinGbl.win.closed)
  {
    dialogWinGbl.win.focus();
  }
}

// ***************************************
// Function: ModalDialog
// Purpose:  Simulates a cross-browser modal dialog
// ***************************************
function ModalDialog(url, width, height, returnFunc)
{
  if(!dialogWinGbl.win || (dialogWinGbl.win && dialogWinGbl.win.closed))
  {
    var topLeft = "";
    dialogWinGbl.url = url;
    dialogWinGbl.width = width;
    dialogWinGbl.height = height;
    dialogWinGbl.returnFunc = returnFunc;
    dialogWinGbl.name = (new Date()).getSeconds().toString(); // Unique Dialog Name

    if(window.screenX)
    {
      // Netscape 4+

      // Center the main window
      topLeft = "screenX=" + (window.screenX + ((window.outerWidth - dialogWinGbl.width)/2)) +
                ",screenY=" + (window.screenY + ((window.outerHeight - dialogWinGbl.height)/2));
    }
    else if(window.screenLeft)
    {
	  // IE 5+

      // Center the main window
	  var CSSCompat = (document.compatMode && document.compatMode != "BackCompat");

	  window.outerWidth = (CSSCompat) ? document.body.parentElement.clientWidth :
	                       document.body.clientWidth;
	  window.outerHeight = (CSSCompat) ? document.body.parentElement.clientHeight :
	                       document.body.clientHeight;
      window.outerHeight -= 80;

      dialogWinGbl.left = parseInt(window.screenLeft + ((window.outerWidth - dialogWinGbl.width) / 2));
      dialogWinGbl.top = parseInt(window.screenTop + ((window.outerHeight - dialogWinGbl.height) / 2));
      topLeft = "left=" + dialogWinGbl.left + ",top=" + dialogWinGbl.top;
    }
    else
    {
      // All other browsers
      topLeft = "left=" + ((screen.width - dialogWinGbl.width)/2) +
                ",top=" + ((screen.height - dialogWinGbl.height)/2);
    }

    // Generate the Dialog and make sure it has focus
    dialogWinGbl.win = window.open(dialogWinGbl.url, dialogWinGbl.name, topLeft +
                                ",resizable=no,width=" + dialogWinGbl.width +
                                ",height=" + dialogWinGbl.height + ",status=no");
  }

  if(dialogWinGbl.win)
  {
    dialogWinGbl.win.focus();
  }

  return dialogWinGbl.win;
}

// ***************************************
// Function: PrinterIsNotInstalledAction
// Purpose:  Returns 
// ***************************************
function PrinterIsNotInstalledAction()
{
  if(onNotInstalledActionGbl.toLowerCase() == "install")
  {
     if(platformTypeGbl != PLATFORM_TYPE_MAC)
     {
        PromptForInstall();
     }
     else 
     {
        // Mac client asks if you want to install the printer
        InstallPrinter();
        GoBack();
     }
  }
  else
  {
    parent.location.replace(GetPrinterAgentURL(pNameGbl));
  }
}

// ***************************************
// Function: HideDialog
// Purpose:  Hides the Dialog
// ***************************************
function HideDialog()
{
  document.getElementById("installdiv").style.visibility = "hidden";
  document.getElementById("statustext").style.visibility = "visible";
  document.getElementById("statustext").innerHTML = "Installing " + DecodeUri(pNameGbl) + "...";
}


// ***************************************
// Function: InstallPrinter
// Purpose:  Installs an iPrint Printer
// ***************************************
function InstallPrinter()
{
//  HideDialog();
//  document.getElementById("installdiv").style.visibility = "hidden";
//  document.getElementById("statustext").style.visibility = "visible";
//  document.getElementById("statustext").innerHTML = "Installing " + DecodeUri(pNameGbl) + "...";

  var tempUri = pNameGbl;
  
  if((platformTypeGbl == PLATFORM_TYPE_WINDOWS_9X_NT4 || 
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2K ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_XP ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_2003_SERVER ||
      platformTypeGbl == PLATFORM_TYPE_WINDOWS_VISTA)
      && browserTypeGbl == BROWSER_TYPE_IE)
  {

//     iPrintAccess.ExecuteRequest("op-printer-install", "printer-url=" + DecodeUri(pNameGbl) + 
     iPrintAccess.ExecuteRequest("op-printer-install", "printer-url=" + pNameGbl + 
                                 (setAsDefaultGbl ? "&driver-options=default:yes;" : "") +
                                 (persistenceGbl ? "&persistence=" + persistenceGbl : "") +
                                 "&result-type=msgBox");
     document.getElementById("statustext").style.visibility = "visible";
     GoBack();
  }
  else
  {
     if(platformTypeGbl == PLATFORM_TYPE_MAC)
     {
         // Note: Mac client needs a way to set the default printer...
         tempUri = DecodeUri(pNameGbl);
//         tempUri = pNameGbl;
     }

     backGbl = backGbl - 2;
     request.document.writeln("<" + "HTML>");
     request.document.writeln("<" + "BODY>");
     request.document.writeln("<EMBED TYPE=\"application/x-Novell-ipp\"" +
                              " printer-url=\"" + tempUri + "\"" +
                              " operation=\"op-printer-install\"" +
                              " HIDDEN=\"true\"" +
                              " result-type=\"url\"" +
                              " target-frame=\"response\"" +
                              (setAsDefaultGbl ? " driver-options=default:yes;" : "") +
                              (persistenceGbl ? " persistence=" + persistenceGbl : "") +
                              " call-back-url=\"response.htm\">" +
                              "</EMBED>");
     request.document.writeln("</" + "BODY>");
     request.document.writeln("</" + "HTML>");
     request.document.close();
  }
}

// ***************************************
// Function: OnResponseLoad
// Purpose:  Parses the response coming back from Mozilla/Mac based plugins
// ***************************************
function OnResponseLoad()
{
  var responseStr = response.document.location.search.substr(1);

  if(responseStr)
  {
    ProcessResponse(GetOperationFromResponse(responseStr), responseStr);
  }
}

// ***************************************
// Function GetOperationFromResponse
// ***************************************
function GetOperationFromResponse(decodeResponse)
{
  var dResponse = decodeResponse.toLowerCase();
  if(dResponse.indexOf(OP_CLIENT_INTERFACE_VERSION_MSG) > -1) return OP_CLIENT_INTERFACE_VERSION;
  else if(dResponse.indexOf(OP_CLIENT_VERSION_INFO_MSG) > -1) return OP_CLIENT_VERSION_INFO;
  else if(dResponse.indexOf(OP_CLIENT_IS_PRINTER_INSTALLED_MSG) > -1) return OP_CLIENT_IS_PRINTER_INSTALLED;
  else if(dResponse.indexOf(OP_USER_GET_ROLE_MSG) > -1) return OP_USER_GET_ROLE;
  else if(dResponse.indexOf(OP_PRINTER_INSTALL_MSG) > -1) return OP_PRINTER_INSTALL;
  else if(dResponse.indexOf(OP_PRINTER_REMOVE_MSG) > -1) return OP_PRINTER_REMOVE;
  else if(dResponse.indexOf(OP_PRINTER_GET_STATUS_MSG) > -1) return OP_PRINTER_GET_STATUS;
  else if(dResponse.indexOf(OP_PRINTER_GET_INFO_MSG) > -1) return OP_PRINTER_GET_INFO;
  else if(dResponse.indexOf(OP_PRINTER_PAUSE_MSG) > -1) return OP_PRINTER_PAUSE;
  else if(dResponse.indexOf(OP_PRINTER_RESUME_MSG) > -1) return OP_PRINTER_RESUME;
  else if(dResponse.indexOf(OP_PRINTER_PURGE_JOBS_MSG) > -1) return OP_PRINTER_PURGE_JOBS;
  else if(dResponse.indexOf(OP_PRINTER_LIST_USERS_JOBS_MSG) > -1) return OP_PRINTER_LIST_USERS_JOBS;
  else if(dResponse.indexOf(OP_PRINTER_LIST_ALL_JOBS_MSG) > -1) return OP_PRINTER_LIST_ALL_JOBS;
  else if(dResponse.indexOf(OP_PRINTER_SEND_TEST_PAGE_MSG) > -1) return OP_PRINTER_SEND_TEST_PAGE;
  else if(dResponse.indexOf(OP_PRINTER_SEND_FILE_MSG) > -1) return OP_PRINTER_SEND_FILE;
  else if(dResponse.indexOf(OP_JOB_GET_INFO_MSG) > -1) return OP_JOB_GET_INFO;
  else if(dResponse.indexOf(OP_JOB_HOLD_MSG) > -1) return OP_JOB_HOLD;
  else if(dResponse.indexOf(OP_JOB_RELEASE_HOLD_MSG) > -1) return OP_JOB_RELEASE_HOLD;
  else if(dResponse.indexOf(OP_JOB_CANCEL_MSG) > -1) return OP_JOB_CANCEL;
  else if(dResponse.indexOf(OP_JOB_RESTART_MSG) > -1) return OP_JOB_RESTART;
  else if(dResponse.indexOf(OP_DBG_PRINTER_GET_ALL_ATTRS_MSG) > -1) return OP_DBG_PRINTER_GET_ALL_ATTRS;
  else if(dResponse.indexOf(OP_LIST_LOCAL_PRINTERS_MSG) > -1) return OP_LIST_LOCAL_PRINTERS;
  else if(dResponse.indexOf(OP_PRINTER_UPLOAD_MSG) > -1) return OP_PRINTER_UPLOAD;
  else if(dResponse.indexOf(OP_PRINTER_UPLOAD_TO_RMS_MSG) > -1) return OP_PRINTER_UPLOAD_TO_RMS;
  else return OP_NONE;
}

// ***************************************
// Function: DisplayError
// ***************************************
function DisplayError(responseStr)
{
  var errGroup = GetValue("ErrorGroup", responseStr);
  var errCode = parseInt(GetValue("ErrorCode", responseStr));
  var errMsg = GetValue("ErrorMessage", responseStr);
  
  switch(errCode)
  {
    case ERR_USER_CANCELLED_LOGIN:
      return;
    case ERR_NO_PRINTER_DRIVER_ASSOCIATED:
    default:
    {
      if(errMsg != "")
            alert(DecodeUri(errMsg));
	  else
        alert(ERROR_GROUP_MSG + " " + errGroup + "\n" +
              ERROR_CODE_MSG + " " + errCode + "\n" +
              ERROR_MESSAGE_MSG + " " + UNKNOWN_ERROR_MSG);
    }
  }
}

// ***************************************
// Function: ProcessResponse
// ***************************************
function ProcessResponse(operationType, responseStr)
{
  // Determine if the response has any errors
  if(responseStr.toLowerCase().indexOf("success") == 0)
  {
    switch(operationType)
    {
      case OP_CLIENT_IS_PRINTER_INSTALLED:
      {
        if(GetValue("printerInstalled", responseStr) == "true")
          PrinterIsInstalledAction();
        else
          PrinterIsNotInstalledAction();
      }
      break;
      case OP_PRINTER_INSTALL:
      {
         if(GetValue("printerInstalled", responseStr) == "false")
         {
            alert(PRINTER_FAILED_TO_INSTALL_MSG);
         }
         else
         {
            alert(jsprintf(PRINTER_INSTALL_SUCCESS_MSG, unescape(pNameGbl)));
         }
         GoBack();
      }
      break;

      case OP_CLIENT_INTERFACE_VERSION:
      case OP_CLIENT_VERSION_INFO:
      case OP_PRINTER_GET_STATUS:
      case OP_PRINTER_GET_INFO:
      case OP_USER_GET_ROLE:
      case OP_PRINTER_REMOVE:
      case OP_PRINTER_PAUSE:
      case OP_PRINTER_RESUME:
      case OP_PRINTER_PURGE_JOBS:
      case OP_PRINTER_LIST_USERS_JOBS:
      case OP_PRINTER_LIST_ALL_JOBS:
      case OP_PRINTER_SEND_TEST_PAGE:
      case OP_PRINTER_SEND_FILE:
      case OP_JOB_GET_INFO:
      case OP_JOB_HOLD:
      case OP_JOB_RELEASE_HOLD:
      case OP_JOB_CANCEL:
      case OP_JOB_RESTART:
      case OP_DBG_PRINTER_GET_ALL_ATTRS:
      case OP_LIST_LOCAL_PRINTERS:
      case OP_PRINTER_UPLOAD:
      case OP_PRINTER_UPLOAD_TO_RMS:
      case OP_NONE:
      default:
      // The Default Page does not use any of the above operations
      break;
    }
  }
  else
  {
    DisplayError(responseStr);
    GoBack();
  }
}

// ***************************************
// Function: jsprintf
// Purpose:  Scaled down Javascript printf which only supports %s.
// Returns:  String with inserted params.
// Params:   First parameter is the string with the %s tags.
//           All subsequent parameters are the substitutions 
//           for the %s tags in the string from the first parameter.
// ***************************************
function jsprintf()
{
  if(!arguments || arguments.length < 1)
    return;

  var str = arguments[0];
  var i = 1;
  var sPos;

  while((sPos = str.indexOf("%s")) > -1 && i < arguments.length)
  {
    if(sPos)
    {
      str = str.substr(0, sPos) + arguments[i] + str.substr(sPos + 2);
    }
    else
    {
      str = arguments[i] + str.substr(2);
    }
    i++;
  }
  return str;
}

// ***************************************
// Function: DecodeUri(value)
// Purpose:  Decodes a URI
// (DONE)
// ***************************************
function DecodeUri(value)
{
  var result;

  if(decodeURI != "undefined" && decodeURI)
    result = decodeURI(value);
  else
    result = unescape(value);

  return result;
}

// ***************************************
// Event: OnIsinstfLoad
// Purpose:  
// ***************************************
function OnIsinstfLoad()
{
  // Localize the dialog
  LocalizeIsinstfPage();

  var deflink = document.getElementById("setasdeflink");
  var deftag = document.getElementById("setasdefaultatag");

  switch(defTypeGbl)
  {
    case 0:
      deflink.checked = false;
      setAsDefaultGbl = false;
      break;
    case 1:
      deflink.checked = true;
      setAsDefaultGbl = true;
      break;
    default:
      deflink.style.visibility = "hidden";
      deftag.style.visibility ="hidden";
      setAsDefaultGbl = false;
  }


    // 1.  Check To see if iPrint Client is installed
  if(IsiPrintClientInstalled())
  {
    // 2.  If installed then check to see if it is current
    if(IsiPrintClientCurrent())
       CheckPrinterInstall();
  }
  else
  {
    // 2. If iPrint Client is not installed, prompt to install
    if(confirm(CLIENT_NOT_INSTALLED_INSTALL_MSG))
      InstalliPrintClient();

    // Must set a timeout to so that we have a chance to install
    // the iPrint client before we go back.  If users have a really
    // slow computer, this value may need to be increased.  The default
    // is 500 miliseconds (1/2 second).
    setTimeout("GoBack();", INSTALL_TIMEOUT_VALUE);
  }
}


// ***************************************
// NOTE: THIS IS A FUTURE FUNCTION
// Function: iPrintPrinterFunc()
// Parameters:
//             1 - IPP Printer URL
//             2 - Printer Settings
//             3 - On Installed Behavior
//             4 - On Not Installed Behavior
// ***************************************
function iPrintPrinterFunc(ippPrinterURL, printerSettings, onInstalled, onNotInstalled, persistence)
{
  var onInstalledType = GeEventType(onInstalled);
  var onNotInstalledType = GetEventType(onNotInstalled);
  // 1. Is the iPrint Client installed?
  //    IsiPrintClientInstalled()
  //   (YES) 2. Is the iPrint Client current?
  //            IsiPrintClientCurrent()
  //            (YES) 3.  Is the iPrint Printer Installed?
  //               		  IsiPrintPrinterInstalled() - NEW
  //                      (YES) 4. Do the OnInstalled Action
  //                       switch(onInstalledType)
  //                       {
  //                         case IPRINT_EVENT_ALERT:
  //                           // alert('The Printer is already installed'); 
  //                           break;
  //                         case IPRINT_EVENT_STATUS:
  //                           // 1. Open the window and pass the status...
  //                           break;
  //                         case IPRINT_EVENT_USER_JOB_MANAGEMENT:	 // Future
  //                         case IPRINT_EVENT_ADMIN_JOB_MANAGEMENT: // Future
  //                         case IPRINT_EVENT_UNINSTALL:            // Future
  //                         case IPRINT_EVENT_REINSTALL:            // Future
  //                         case IPRINT_EVENT_INSTALL:              // Future (See IPRINT_EVENT_REINSTALL)
  //                         case IPRINT_EVENT_UNKNOWN:              // Not supported
  //                         default:
  //                           // Do nothing until these types are supported
  //                           // alert('The operation is not supported');
  //                           break;
  //                       }
  //                      (NO)  4. Do the OnNotInstalled Action
  //                       switch(onNotInstalledType)
  //                       {
  //                           // alert('The Printer is already installed'); 
  //                           break;
  //                         case IPRINT_EVENT_INSTALL:              // See IPRINT_EVENT_REINSTALL
  //                         case IPRINT_EVENT_STATUS:
  //                           // 1. Open the window and pass the status...
  //                           break;
  //                         case IPRINT_EVENT_ADMIN_JOB_MANAGEMENT: // Future
  //                         case IPRINT_EVENT_ALERT:                // Not supported
  //                         case IPRINT_EVENT_REINSTALL:            // Not supported
  //                         case IPRINT_EVENT_UNINSTALL:            // Not supported
  //                         case IPRINT_EVENT_USER_JOB_MANAGEMENT:	 // Not supported
  //                         case IPRINT_EVENT_UNKNOWN:              // Not supported 
  //                         default:
  //                           // Do nothing until these types are supported
  //                           // alert('The operation is not supported');
  //                           break;
  //                       }
  //                  5. Return to the calling page (if necessary)
  //             (NO) 3.  Prompt the user to install that latest iPrint Client Do they want to install?
  //                      (YES) 4. Install the iPrint Client
  //                      (NO)  4. Abort the iPrint Client Install
  //                  5. Return to the calling page (if necessary)    
  //   (NO)  2. Prompt the user to install the iPrint Client.  Do they want to install?
  //         (YES) 3.  Install the iPrint Client
  //         (NO)  3.  Abort the iPrint Client Install
  //         4.  Return to the calling page (if necessary)
}

// ***************************************
// NOTE: THIS IS A FUTURE FUNCTION
// Function: GetEventType
// Purpose:  Gets the event enumeration based on the string passed to the function
// Returns:  An enumeration representing one of the known iPrint event types
// ***************************************
function GetEventType(eventString)
{
  var lcEventString = eventString.toLowerCase();

  if(lcEventString == "alert")
    return IPRINT_EVENT_ALERT;
  else if(lcEventString == "status" ||
          lcEventString == "info")
    return IPRINT_EVENT_STATUS;
  else if(lcEventString == "install")
    return IPRINT_EVENT_INSTALL;
  else if(lcEventString == "re-install" ||
          lcEventString == "reinstall")
    return IPRINT_EVENT_REINSTALL;
  else if(lcEventString == "un-install" ||
          lcEventString == "uninstall" ||
          lcEvent == "remove")
	return IPRINT_EVENT_UNINSTALL;
  else if(lcEventString == "user-job-mgmt")
    return IPRINT_USER_JOB_MANAGEMENT;
  else if(lcEventString == "admin-job-mgmt")
    return IPRINT_ADMIN_JOB_MANAGEMENT;
  else
    return IPRINT_EVENT_UNKNOWN;
}

// ***************************************
// Function: GoBack
// Purpose:  Go back to the calling page
// ***************************************
function GoBack()
{
  if(browserTypeGbl == BROWSER_TYPE_IE || platformTypeGbl == PLATFORM_TYPE_MAC)
    window.history.back();
  else
  {
    window.history.go(backGbl);
    backGbl = -1;
  }
}

// ***************************************
// Function: GetValue
// Purpose:  Returns 
// ***************************************
function GetValue(paramName, string)
{
  var position;
  var start;
  var end;
  var end1;
  var end2;
  var params;

  params = string;
  position = params.toLowerCase().indexOf(paramName.toLowerCase());
  if(position != -1)
  {
    start = position + paramName.length + 1;
    end1 = params.indexOf("&", start);
    if(end1 == -1)
      end1 = params.length;

    end2 = params.indexOf("+", start);
    if(end2 == -1)
      end2 = params.length;

    if(end1 < end2)
      end = end1;
    else
      end = end2;

    if(start != end)
      return(params.substring(start, end));
  }
  return("");
}

// ***************************************
// Function: GetiPrintClientInstaller
// Purpose:  Returns the iPrint client installation
//           file based upon the client's Operating System
// ***************************************
function GetiPrintClientInstaller()
{
  switch(platformTypeGbl)
  {
    case PLATFORM_TYPE_WINDOWS_9X_NT4:
      return(WIN_9X_NT4_INSTALLER);  
	case PLATFORM_TYPE_WINDOWS_2K:
	  return (WIN_2K_INSTALLER);
	case PLATFORM_TYPE_WINDOWS_XP:
	case PLATFORM_TYPE_WINDOWS_2003_SERVER:
	  return (WIN_XP_INSTALLER);
  case PLATFORM_TYPE_WINDOWS_VISTA:
    return (WIN_VISTA_INSTALLER);
    case PLATFORM_TYPE_LINUX_NLD9:
	case PLATFORM_TYPE_LINUX:
      return(LINUX_INSTALLER);
    case PLATFORM_TYPE_MAC:
      return(MAC_INSTALLER);
    default:
      return(UNKNOWN_INSTALLER);
  }
}

// ***************************************
// Function: GetiPrintClientInstallerLink
// Purpose:  Returns the iPrint client installation
//           file based upon the client's Operating System
// ***************************************
function GetiPrintClientInstallerLink()
{
  switch(platformTypeGbl)
  {
    case PLATFORM_TYPE_WINDOWS_9X_NT4:
      return(WIN_9X_NT4_INSTALLER);  
    case PLATFORM_TYPE_WINDOWS_2K:
      return (WIN_2K_INSTALLER);
    case PLATFORM_TYPE_WINDOWS_XP:
    case PLATFORM_TYPE_WINDOWS_2003_SERVER:
      return (WIN_XP_INSTALLER);
    case PLATFORM_TYPE_WINDOWS_VISTA:
      return (WIN_VISTA_INSTALLER)
    case PLATFORM_TYPE_LINUX_NLD9:
      return (LINUX_NLD9_INSTALLER_LINK);
    case PLATFORM_TYPE_LINUX:
      return(LINUX_INSTALLER_LINK);
    case PLATFORM_TYPE_MAC:
    default:
      return "";
  }
}

// ***************************************
// Function: GetClientPlatform()
// Purpose:  Returns the client's Operating System (platform)
// ***************************************
function GetClientPlatform()
{
  var clientPlatform = navigator.userAgent.toLowerCase();
  
  if(clientPlatform.indexOf("windows 95") > -1 ||
     clientPlatform.indexOf("windows 98") > -1 ||
	 clientPlatform.indexOf("windows nt 4.0") > -1)
	return (PLATFORM_TYPE_WINDOWS_9X_NT4);
  if(clientPlatform.indexOf("windows nt 5.0") > -1)
    return (PLATFORM_TYPE_WINDOWS_2K);
  if(clientPlatform.indexOf("windows nt 5.1") > -1)
    return (PLATFORM_TYPE_WINDOWS_XP);
  if(clientPlatform.indexOf("windows nt 5.2") > -1)
    return (PLATFORM_TYPE_WINDOWS_2003_SERVER);
  if(clientPlatform.indexOf("windows nt 6.0") > -1)
    return (PLATFORM_TYPE_WINDOWS_VISTA);
  if(clientPlatform.indexOf("x11") > -1 ||
     clientPlatform.indexOf("linux") > -1)
  {
	// If nld is in the userAgent string, then assume nld9 or earlier
	// If Konqueror, and version is 3.2 also assume nld9
	// otherwise assume sled10 or later...

	var konqIndex = clientPlatform.indexOf("konqueror/");
	var konqVer = (konqIndex > -1) ? parseFloat(clientPlatform.substr(konqIndex + "konqueror/".length)): 0.0;

	if(clientPlatform.indexOf("nld") > -1 || (konqVer <= 3.2 && konqVer > 0.0))
	  return (PLATFORM_TYPE_LINUX_NLD9);
	else
	  return (PLATFORM_TYPE_LINUX);
  }

  if(clientPlatform.indexOf("mac") > -1)
    return (PLATFORM_TYPE_MAC);

  return (PLATFORM_TYPE_OTHER);
}

// ***************************************
// Function: GetClientBrowser()
// Purpose:  Returns the browser type
// ***************************************
function GetClientBrowser()
{
  var clientBrowser = navigator.appName.toLowerCase();

  if(clientBrowser.indexOf("microsoft") > -1) return BROWSER_TYPE_IE;
  else if(clientBrowser.indexOf("netscape") > -1 ||
          clientBrowser.indexOf("konqueror") > -1) return BROWSER_TYPE_MOZILLA;
  else return BROWSER_TYPE_OTHER;
}

// ***************************************
// MAIN
// ***************************************

browserTypeGbl = GetClientBrowser();
platformTypeGbl = GetClientPlatform();
browserLangGbl = GetBrowserLanguage();
pNameGbl = GetValue("ippPrinterURL", window.location.search);
persistenceGbl = GetValue("persistence", window.location.search);
defaultOpGbl = GetValue("operation", window.location.search);
printerInstalled = GetValue("installed", window.location.search);
onInstalledActionGbl = GetValue("onInstalled", window.location.search);
onNotInstalledActionGbl = GetValue("onNotInstalled", window.location.search);
clientInstallerGbl = GetiPrintClientInstaller();
clientInstallerLinkGbl = GetiPrintClientInstallerLink();


if(defaultOpGbl == "")
{
  defaultOpGbl = "op-printer-list-all-jobs";
}

document.writeln("<SCRIPT" + " SRC=\"" + "../ippdocs/nls/" + (browserLangGbl ? browserLangGbl : "en") + "/iprntmsg.js\"></SCRIPT>");

// ***************************************
// FUNCTION: myClose
// PURPOSE:  Closes the information window
// ***************************************
function myClose()
{
  parent.window.close();
}

// ***************************************
// SETUP FUNCTIONS
// ***************************************

// ***************************************
// FUNCTION: SetupHeaderPage()
// PURPOSE:
// ***************************************
function SetupHeaderPage()
{
  var pDisplayFrame = parent.frames["control2"];

  if(!pDisplayFrame)
    pDisplayFrame = parent.control2;

  LocalizeHeaderPage();
  // Switch the iPrint client install link based upon the client operating system as follows:
  document.getElementById("installiprintclientlink").href = clientInstallerGbl;

  if(pDisplayFrame)
  {
    document.getElementById("printerlistheader").style.display = "";
  }
  else
  {
    document.getElementById("printerlistheader").style.display = "none";
  }
}

// ***************************************
// LOCALIZATION FUNCTIONS
// ***************************************

// ***************************************
// FUNCTION: LocalizeHeaderPage()
// PURPOSE:
// ***************************************
function LocalizeHeaderPage()
{
   SetLocalizedString(document.getElementById("aboutiprinthint"), headerAboutiPrint);
   SetLocalizedString(document.getElementById("iprinthomehint"), refreshStr);
   SetLocalizedString(document.getElementById("selectedprinter"), selectedPrinterPromptMsg + " " + unescape(parent.pNameGbl)); 
   SetLocalizedString(document.getElementById("iprintprintersonserver"), ippsrvrPrinterListTitle + " " + parent.serverName);
   SetLocalizedString(document.getElementById("installiprintclient"), pmenuInstalliPrintClientString);
   SetLocalizedString(document.getElementById("installprinter"), ptitleInstallPrinter);
   SetLocalizedString(document.getElementById("printerlocation"), ptitlePrinterLocation);
   parent.paLocationSorted = 0;
   ShowPaListSortArrow(1);
}

// ***************************************
// Function: LocalizeX11Installer()
// Purpose:  Localize the Linux Installer Dialog based on the 
//           locale of the browser
// Returns:  None
// ***************************************

function LocalizeX11Installer()
{
  SetLocalizedString(document.getElementById("x11title"), IPRINT_CLIENT_INSTALL_TITLE_MSG);
  SetLocalizedString(document.getElementById("x11installer"), X11_INSTALLER_MSG);
  SetLocalizedString(document.getElementById("cniandsti"), STEPS_TO_INSTALL_MSG);
  SetLocalizedString(document.getElementById("clicklinkbelow"), CLICK_THE_LINK_BELOW_MSG);
  SetLocalizedString(document.getElementById("downloadiprintclient"), DOWNLOAD_IPRINT_CLIENT_MSG);
  SetLocalizedString(document.getElementById("exitbrowser"), EXIT_BROWSER_BEFORE_INSTALL_MSG);
  SetLocalizedString(document.getElementById("konquerornote"), KONQUEROR_NOTE_MSG);

  document.getElementById("downloadiprintclient").href = clientInstallerLinkGbl;
}

// ***************************************
// Function: LocalizeWinInstaller()
// Purpose:  Localize the Windows Installer Dialog based on the 
//           locale of the browser
// Returns:  None
// ***************************************

function LocalizeWinInstaller()
{
  // Localize the strings...
  SetLocalizedString(document.getElementById("wintitle"), IPRINT_CLIENT_INSTALL_TITLE_MSG);
  SetLocalizedString(document.getElementById("wininstaller"), WIN_INSTALLER_MSG);
  SetLocalizedString(document.getElementById("cniandsti"), STEPS_TO_INSTALL_MSG);
  SetLocalizedString(document.getElementById("clicklinkbelow"), CLICK_THE_LINK_BELOW_WO_ASSOC_MSG);
  SetLocalizedString(document.getElementById("downloadiprintclient"), DOWNLOAD_IPRINT_CLIENT_MSG);
  SetLocalizedString(document.getElementById("exitbrowser"), EXIT_BROWSER_BEFORE_INSTALL_MSG);

  document.getElementById("downloadiprintclient").href = clientInstallerLinkGbl;
}



// ***************************************
// FUNCTION: LocalizeIsinstfPage()
// PURPOSE:
// ***************************************
function LocalizeIsinstfPage()
{
  SetLocalizedString(document.getElementById("printernotinstalled"), IPD_PRINTER_NOT_INSTALLED);
  SetLocalizedString(document.getElementById("selectedprinter"), selectedPrinterPromptMsg + " " + ExtractPAName(DecodeUri(pNameGbl)));
//  SetLocalizedString(document.getElementById("actualprinter"), DecodeUri(pNameGbl));
  SetLocalizedString(document.getElementById("installnow"), IPD_DO_YOU_WANT_TO_INSTALL); 
  SetLocalizedString(document.getElementById("yesbutton"), ippsrvrPrinterInfoPrinterYes);
  SetLocalizedString(document.getElementById("nobutton"), ippsrvrPrinterInfoPrinterNo);
  SetLocalizedString(document.getElementById("setasdefaultatag"), IPD_SET_PRINTER_AS_DEFAULT);
}

// ***************************************
// FUNCTION: LocalizeSetPrinterDefaultPage()
// PURPOSE:
// ***************************************
function LocalizeSetPrinterDefaultPage()
{
  if(ptitleInstallPrinter != "")
    document.title = ptitleInstallPrinter;

  SetLocalizedString(document.getElementById("printernotinstalled"), IPD_PRINTER_NOT_INSTALLED);
  SetLocalizedString(document.getElementById("installnow"), IPD_DO_YOU_WANT_TO_INSTALL); 
  SetLocalizedString(document.getElementById("setasdeflink"), IPD_SET_PRINTER_AS_DEFAULT);
  SetLocalizedString(document.getElementById("yesbutton"), ippsrvrPrinterInfoPrinterYes);
  SetLocalizedString(document.getElementById("nobutton"), ippsrvrPrinterInfoPrinterNo);
}


// ***************************************
// FUNCTION: ShowPaListSortArrow()
// PURPOSE:
// ***************************************
function ShowPaListSortArrow(sortLocation)
{
   // Save the sorted state in a variable in case the page is refreshed.
   var oldLocation = parent.paLocationSorted & 3;

   if (oldLocation == sortLocation)
   {
     if(parent.paLocationSorted & 4)
	 {
       parent.paLocationSorted = sortLocation;
	 }
	 else
	 {
	   parent.paLocationSorted = sortLocation | 4;
	 }
   }
   else
   {
     parent.paLocationSorted = sortLocation;
   }

   var installObj = document.getElementById("installprintertitle");
   var installObjDir = document.getElementById("installPrinterNameDir");
   var locationObj = document.getElementById("printerlocationtitle");

   var locationObjDir = document.getElementById("printerlocationDir");

   if(parent.paLocationSorted & 2)
   {
     if(parent.paLocationSorted & 4)
	 {
	   locationObj.src = '/ippdocs/images/up_arrow.gif';
	   locationObjDir.href = '/ipp/pdisplay.htm?locationup';
	 }
	 else
	 {
	   locationObj.src = '/ippdocs/images/dn_arrow.gif';
	   locationObjDir.href = '/ipp/pdisplay.htm?location';
	 }

     installObj.style.visibility = "hidden";
     locationObj.style.visibility = "visible";
   }
   else
   {
      if(parent.paLocationSorted & 4)
	  {
	    installObj.src = '/ippdocs/images/up_arrow.gif';
		installObjDir.href = '/ipp/pdisplay.htm?nameup';
	  }
	  else
	  {
	    installObj.src = '/ippdocs/images/dn_arrow.gif';
		installObjDir.href = '/ipp/pdisplay.htm';
	  }
      installObj.style.visibility = "visible";
      locationObj.style.visibility = "hidden";
   }
}

// ***************************************
// Function: SetLocalizedString
// Purpose:  Validates that the string and object are valid,
//           then sets the object to the string
// Inputs:
//           localObj - The object to be localized
//           localStr = the localized string variable
// ***************************************
function SetLocalizedString(localObj, localStr)
{
  if(!localObj || !localStr)
  {
    return;
  }

  if(localObj.tagName == "INPUT" && localObj.type == "button")
  {
	localObj.value = localStr;
  }
  else if(localObj.tagName == "TITLE")
  {
    document.title = localStr;
  }
  else
  {
    localObj.innerHTML = localStr;  
  }
}

// ***************************************
// Function: LocalizePControlPage
// Purpose:  
// ***************************************

function LocalizePcontrolPage()
{
  document.writeln("<TITLE>" + ippsrvrPrinterListPageTitle + "</TITLE>");
}

// ***************************************
// USER INTERFACE FUNCTIONS
// ***************************************

// ***************************************
// Function: ShowHint
// Purpose:  Reveal tool tip hints
// ***************************************
function ShowHint(tipObject)
{
  var tipObj = document.getElementById(tipObject);

  if(tipObj)
  {
    tipObj.style.visibility = "visible";
  }
}

// ***************************************
// Function: HideHint
// Purpose:  Hide tool tip hints
// ***************************************
function HideHint(tipObject)
{
  var tipObj = document.getElementById(tipObject);

  if(tipObj)
  {
    tipObj.style.visibility = "hidden";
  }
}

// **********************************
//  Function: GetBrowserLanguage
//  Purpose:  Gets the maptool.js for the language on the workstation
// **********************************
function GetBrowserLanguage()
{
   var lang = "";

   if(navigator.appName == "Microsoft Internet Explorer")
   {
      lang = navigator.userLanguage.toLowerCase();
      if(lang.indexOf("zh") != 0)
      {
        lang = lang.substring(0, 2);
      }
   }
   else if(navigator.appName == "Netscape")
   {
      lang = navigator.language.toLowerCase();
      if(lang.indexOf("zh") != 0)
      {
        lang = lang.substring(0, 2);
      }
      else
      {
        if(lang.indexOf("tw") > -1)
        {
          lang = "zh-tw";
        }
        else if(lang.indexOf("cn") > -1)
        {
          lang = "zh-cn";
        }
      }
   }

   // If we can't determine which language,  
   // or the language is not supported,
   // then use English.

   if(lang != "en" &&    // English
      lang != "de" &&    // German
      lang != "es" &&    // Spanish
      lang != "fr" &&    // French
      lang != "it" &&    // Italian
      lang != "pt" &&    // Portuguese
      lang != "ru" &&    // Russian
      lang != "hu" &&    // Hungarian
      lang != "sk" &&    // Slovak
      lang != "cs" &&    // Czech
      lang != "pl" &&    // Polish
      lang != "zh-tw" && // Chinese Traditional
      lang != "zh-cn" && // Chinese Simplified
      lang != "ja")      // Japanese
   {
     lang = "en";
   }

   // For Japanese use the jp directory
   if(lang == "ja")
     lang = "jp";

   return lang;
}

// **********************************
// Function: escapeURI
// Inputs in the format:  <http_or_https>://<IP_Address_or_DNS>/<ipp_or/PAName
// Outputs in the format: <http_or_https>://<IP_Address_or_DNS>/ipp/<Escaped_PAName>
// **********************************
function escapeURI(paUri)
{
  var tempPaUri = paUri;
  var lcPaUri = paUri.toLowerCase();
  var colonSSPos = lcPaUri.indexOf("://");

  if(colonSSPos > -1)
  {
    var ippPos = lcPaUri.indexOf("/ipp", colonSSPos + "://".length);

    if(ippPos > -1)
    {
      var nextSlashPos = lcPaUri.indexOf("/", ippPos + 1);

      if(nextSlashPos > -1)
      {
        tempPaUri = paUri.substring(0, colonSSPos + "://".length) + 
                    escape(paUri.substring(colonSSPos + "://".length, ippPos)) +
                    paUri.substring(ippPos, nextSlashPos + 1) +
                    escape(paUri.substring(nextSlashPos + 1, paUri.length));
      }
    }
  }

  return tempPaUri;
}

// **********************************
// Function: DisplayPAInfo
// Inputs in format: http://xxx.xxx.xxx.xxx/ipp/PAName
// **********************************
function DisplayPAInfo(paInfo)
{
  var encodedPaInfo = paInfo;

  if(browserTypeGbl == BROWSER_TYPE_IE)
    encodedPaInfo = escapeURI(paInfo).replace(/%3A/g, ":");

  paInfoWinGbl = window.open("","PrinterInfo","width=600, height=600, resizable=yes");

  if(paInfoWinGbl)
  {
    paInfoWinGbl.document.writeln("<HTML>\n" +
                                  "<HEAD>\n" +
                                  "<TITLE>"+ parent.ippsrvrPrinterInfoPageTitle + "</TITLE>\n" +
                                  "</HEAD>\n" +
                                  "<FRAMESET ROWS=\"90, *\" BORDER=\"0\">\n" +
				          "  <FRAME SRC=\"../ippdocs/header.htm\" FRAMEBORDER=\"NO\" SCROLLING=\"NO\" NORESIZE>\n" +
                                  "  <FRAME ID=\"painfo\" NAME=\"painfo\" SRC=\"" + encodedPaInfo + "\">\n" +
                                  "</FRAMESET>\n" +
                                  "<" + "/HTML>");

    paInfoWinGbl.document.close();
    paInfoWinGbl.focus();
  }
  else
  {
    // Popup windows have been disabled...
    alert(POPUPS_DISABLED_MSG);
  }
}

// **********************************
// Function: ReloadDoc
// Purpose:
// **********************************
function ReloadDoc()
{
  if(parent.control2)
  {
    parent.control2.location.reload(true);
  }
  else if(parent.painfo)
  {
    parent.painfo.location.reload(true);
  }
}

// **********************************
// Function: PromptForInstall
// Purpose:
// **********************************

function PromptForInstall()
{
  var installDiv = window.document.getElementById("installdiv");

  if(installDiv)
  {
    installDiv.style.visibility = "visible";

  }
}

// Function: InstPrinter
// Purpose: Install the printer if necessary

function InstPrinter()
{
  if(opener && opener.EnableEvents)
  {
    opener.EnableEvents();
  }

  if(installPrinterGbl)
  {
    if(opener && !opener.closed && opener.dialogWinGbl)
    {
      if(defaultCheckBoxGbl && (cbRowGbl && cbRowGbl.style.visibility != "hidden"))
            opener.setAsDefaultGbl = defaultCheckBoxGbl.checked;

      opener.dialogWinGbl.returnFunc();
    }
  }
  else
  {
    if(opener && opener.GoBack)
      opener.GoBack();
  }
}

// **********************************
// Function: ExtractPAName
// Purpose:  Get the printer agent name from the printer URL
// **********************************

function ExtractPAName(printerURL)
{
  return printerURL.substr(printerURL.indexOf("/", printerURL.toLowerCase().indexOf("/ipp") + 1) + 1);
}
