var sBasePath = "./";

function OnOCXError ()
{
  document.location.href = sRelPath + "OCXError.htm";
}

function OnWinCEError ()
{
  document.location.href = sRelPath + "PPCInstall.htm";
}

function GetNavigatorVersion()
{
  if (window.navigator.platform != "WinCE")
    return 0.0;
    
  var sVer = window.navigator.appMinorVersion;
  if (sVer == null) return 5.0;
  
  var fVer = parseFloat(sVer);
  if (fVer > 0.0)
    return fVer;
  
  sVer = window.navigator.appVersion;
  if (sVer.indexOf("Windows Phone 6.5") > 0)
    return 8.0;
  return 6.0;
}

// This function returns default WebSignature object
function GetWebSign()
{
  var oSD;
  oSD = WebSignature;
  if (oSD == null)
    oSD = document.getElementById("WebSignature")
  return oSD;
}

// Clear default WebSignature content
function OnClear ()
{
  GetWebSign().Clear();
}

// Submit the content of default WebSignature object to the specified URL
function OnSubmit (sURL, sPar)
{
  var ws;
  ws = GetWebSign();
  if (!ws.Valid)
  {
    alert("You must sign before hitting submit");  
    return false;
  }
  ws.Submit(sURL, sPar);
  return true;
}

// Function that shows the version of currently installed WebSignature control
function WS_ShowVersion() 
{
  try
  {
    alert("WebSignature: " + GetWebSign().Version + "\r\n\r\n" +
          "AppName: " + window.navigator.appName + "\r\n" +
          "AppVersion: " + window.navigator.appVersion + "\r\n" +
          "appMinorVersion: " + window.navigator.appMinorVersion + "\r\n" +
          "Platform: " + window.navigator.platform + " [" + window.navigator.cpuClass + "]\r\n" +
          "userAgent: " + window.navigator.userAgent ); 
  }
  catch(e)
  {
    alert("Error: " + e.description + " [" + e.number + "]");
  }
}

// We call this function as a test to verify control is properly loaded and working
function OnPageLoad ()
{
  var fNV = GetNavigatorVersion();
  if (fNV <= 0.0) 
  {
    // Do some extended code for Desktop IE version
    return true;
  }

  try
  {
    var oWS = GetWebSign();
    if (oWS.Version < 20000)
    {
      if (fNV > 6.99)
      {
        // Old version of WebSignature is already installed
        alert("Installation of new version 2.x is required.");
        document.location.href = sBasePath + "PPCUpdate.htm";
        return false;
      }
      // Otherwise use old 1.0 version on devices with WM 6.0
    }
    else if (oWS.Version < 20500)
    {
      if (fNV > 7.99)
      {
        // Older version of WebSignature (< 2.5) may work incorrectly under WM 6.5
        alert("You are using older version of the WebSignature control under your Windows Mobile 6.5 browser.\r\n"+
              "To avoid some possible problems with WebSignature you have to install its latest version (v2.5+).");
        document.location.href = sBasePath + "PPCUpdate.htm";
        return false;
      }
    }

    oWS.Clear();
    return true;
  }
  catch(e)
  {
    // If the control has not been loaded - we open ActiveX installation page
    alert("Error: " + e.description + " [" + e.number + "] \r\n" + "Try to reinstall the WebSignature control");
    document.location.href = sBasePath + "PPCInstall.htm";
  }
  return false;
}

// Use this function instead of OnPageLoad if you are using WebSignature v2.5
// to check the control version and assign the corresponding license key
function OnPageLoadEx (sLicVer25)
{
  if (!OnPageLoad())
    return false;

  var oWS;
  oWS = GetWebSign();
  if (oWS.Version < 20500 || sLicVer25 == null)
    return true;

  oWS.LicenseKey = sLicVer25;
  return true;
}

function InsertWebSign(sAttrs, sParams)
{
  document.writeln('<OBJECT id="WebSignature" name="WebSignature" ' + sAttrs);
  document.write('classid="clsid:4E534257-3000-4E22-83A5-AD614CB96642"');
  if (GetNavigatorVersion() > 0.0)
  {
    document.write('onerror="OnWinCEError()"');
    document.write('CODEBASE=' + sBasePath + 'WebSign.ARMV4.CAB#version=2,5,0">');
  }
  else
  {
    document.write('onerror="OnOCXError()"');
    try
    {
      if (window.navigator.platform == "Win64")
        document.write('CODEBASE=' + sBasePath + 'WebSign.x64.CAB#version=2,5,0">');
      else
        document.write('CODEBASE=' + sBasePath + 'WebSign.x86.CAB#version=2,5,0">');
    }
    catch(e)
    {
      document.write('CODEBASE=' + sBasePath + 'WebSign.i386.CAB#version=2,5,0">');
    }
  }
  document.write(sParams);
  document.writeln('</OBJECT>');
}

