        var DefaultTSGateway = 'remote.tuiora.co.nz';
        var GatewayCredentialsSource = '4';
        window.onerror=fnErrTrap;
        var vbCritical = 16;
        var vbInformation = 64;
        var L_sTitle_Text = "Remote Desktop Connection";
                    
        function fnErrTrap(sMsg,sUrl,sLine) {
          var retval;
          var L_errMsg_Text = "To use this Web site, your computer must be running the Remote Desktop Connection (RDC) client. \n\nTo continue, install the latest RDC client and the most recent updates from the Microsoft Update Web site, and then try again.";
          if (sMsg.indexOf('is undefined') != -1) {
            retval = TSMsgBox(L_errMsg_Text, vbInformation, L_sTitle_Text);
          } else if (sMsg.indexOf('is null or not an object') != -1) {
            retval = TSMsgBox(L_errMsg_Text, vbInformation, L_sTitle_Text);
          } else if (sMsg.indexOf('canceled by the user') != -1) {
            return true;
          } else {
            var L_errMsg2_Text = "An application error was caught:\n\nError:%ErrorMessage%\nURL:%URL%\nLine:%ErrorLineNumber%" ; // {Placeholder="%ErrorMessage%","%URL%","%ErrorLineNumber%"}
            var errMsg2 = L_errMsg2_Text
            errMsg2 = errMsg2.replace("%ErrorMessage%",sMsg);
            errMsg2 = errMsg2.replace("%ErrorNumber%",sURL);
            errMsg2 = errMsg2.replace("%ErrorLineNumber%",sLine);

            retval = TSMsgBox(L_errMsg2_Text, vbInformation, L_sTitle_Text);
          }
          return true; 
        }
                    
        try {
          document.write("<object type=\"application/x-oleobject\" ");
          document.write("id=\"MsRdpClient\" name=\"MsRdpClient\" ");
          document.write("onerror=\"OnControlLoadError\" ");
          document.write("height=\"0\" width=\"0\" ");
          document.write("classid=\"CLSID:4eb89ff4-7f78-4a0f-8b8d-2bf02e94e4b2\">");
          document.write("</object>");
        } catch(e) {
          throw e;
        }
        var MsRdpClient = document.getElementById("MsRdpClient");

        function OnControlLoadError() {
          var L_errMsgLoad_Text = "A problem was detected while loading the ActiveX Control.";
          var retval = TSMsgBox(L_errMsgLoad_Text, vbInformation, L_sTitle_Text);
          return true;
        }
        MsRdpClient.MsRdpClientShell;
        var myExp = new Array(7);
        var myAr
        myAr = new Array("xDesktop_bg", "xFont_sm", "xDesktop_cm", "xWin_Drag", "xMenu_an", "xThemes_show", "xBitmap_cache"); 
        function setPerf() {
          var xLast = 0;
          switch(parseInt(document.getElementById("comboPerfomance").value)) {
            case 0:     // slow
              xLast = 6;
              break;
            case 1:     // modest
              xLast = 5;
              break;
            case 2:     // good
              xLast = 2;
              break;
            case 3:     // great
              xLast = 0;
              break;
            default:        
              break;
          }
          for (var i = 0; i < myAr.length-1; i++) {
            myExp[i] = 1;
            if (i < xLast) {
              myExp[i] = 0;
            }
          }
        }
        function setRes() {
          var retval;
          var iRes = GetParam("comboResolution", false, "0");
          var RDPstr1 = "screen mode:i:2\n";
          var RDPstr2 = "Desktop Size ID:i:" + iRes + "\n";

          if (iRes == "0") {
            retval =  RDPstr1;
          } else {
            retval =  RDPstr2;
          }
          return retval;
        }
        function flipBit (iVal) {
          return (iVal == 1) ? 0:1; 
        }
        function BtnConnect(){
          var RDPstr = "full address:s:" + GetParam("MachineName", true, "") + "\n";
          RDPstr += "authentication level:i:0\n"; /* 0=Connect and don't warn me, 1=Do not connect, 2=Warn me */
          RDPstr += "gatewayhostname:s:" +  DefaultTSGateway + "\n"; 
          RDPstr += "gatewaycredentialssource:i:" +  GatewayCredentialsSource + "\n";
          if ((DefaultTSGateway != null) && (DefaultTSGateway.length > 0)) {
            RDPstr += "gatewayusagemethod:i:2\n";
            RDPstr += "gatewayprofileusagemethod:i:1\n";
          } else {
            RDPstr += "gatewayusagemethod:i:2\n";
            RDPstr += "gatewayprofileusagemethod:i:0\n";
          }

          setPerf();
          RDPstr += "disable wallpaper:i:" + flipBit(myExp[0]).toString() + "\n";
          RDPstr += "allow font smoothing:i:" + myExp[1].toString() + "\n";
          RDPstr += "allow desktop composition:" + myExp[2].toString() + "\n";
          RDPstr += "disable full window drag:i:" + flipBit(myExp[3]).toString() + "\n";
          RDPstr += "disable menu anims:i:" + flipBit(myExp[4]).toString() + "\n";
          RDPstr += "disable themes:i:" + flipBit(myExp[5]).toString() + "\n";
          RDPstr += "bitmapcachepersistenable:i:1\n";

          RDPstr += setRes();
          RDPstr += "displayconnectionbar:i:1\n";
          RDPstr += "session bpp:i:32\n";

          RDPstr += "redirectclipboard:i:" + GetParam("xClipboard", false, "0") + "\n";
          if (GetParam("xDriveRedirection", false, "0") == 1) {
            RDPstr += "redirectdrives:i:1\n";
            RDPstr += "drivestoredirect:s:*\n";
          } else {
            RDPstr += "redirectdrives:i:0\n";
          }
          RDPstr += "redirectcomports:i:" + GetParam("xPortRedirection", false, "0") + "\n";
          RDPstr += "devicestoredirect:s:" + ((document.getElementById("xPnPRedirection").checked) ? "*" : "") + "\n"; 
          RDPstr += "keyboardhook:i:" + parseInt(document.getElementById("comboKeyboard").value) + "\n";
          RDPstr += "audiomode:i:" + parseInt(document.getElementById("comboAudio").value) + "\n";

          RDPstr += "redirectprinters:i:" + GetParam("xPrinterRedirection", false, "0") + "\n";
          RDPstr += "redirectsmartcards:i:1\n";
          RDPstr += "compression:i:1\n";
          RDPstr += "autoreconnection enabled:i:1\n";
          RDPstr += "";

          MsRdpClient.MsRdpClientShell.PublicMode = !(document.getElementById("PublicCheckbox").checked);
          MsRdpClient.MsRdpClientShell.RdpFileContents = RDPstr;
          if (TestTxt(document.getElementById("MachineName"))== false) {
            try {
              MsRdpClient.MsRdpClientShell.Launch();
            } catch(e) {
              if (e.number==-2147467259) {
                var L_ErrMsgLaunch_Text = "The security settings of your browser are preventing this website from launching the remote program. Please add this website to the Trusted sites or Local intranet zone of your browser and try again.";
                var retval = TSMsgBox(L_ErrMsgLaunch_Text, vbInformation, L_sTitle_Text);
              } else {throw e}
            }
          }
        }
        function GetParam(sParam, bReqd, vDefault) {
          var obj = document.getElementById(sParam);
          if(obj != null) {
            switch(obj.tagName) {
              case "SELECT":
                return obj.selectedIndex;
                break;
              case "INPUT":
                if (obj.type == "checkbox") return ((obj.checked) ? 1 : 0);
                if (obj.type == "hidden") return obj.value;
                if (obj.type == "text") return obj.value;
                break;
              default:
                break;
            }
          } else {
            if ((bReqd) && ((vDefault == "") || (vDefault == null) || (obj == null))) {
              var L_ErrMsgInvalid_Text = "%ParameterName% is not a valid or available parameter name.";  // {Placeholder="%ParameterName%"}
              var errMsgInvalid = sParam;
              errMsgInvalid = errMsgInvalid.replace("%ParameterName%", sParam);
              var retval = TSMsgBox(errMsgInvalid, vbInformation, L_sTitle_Text);
              return null;
            } else {
              return vDefault;
            }
          }
        }
        var L_gFQDN_Text = "ComputerName";
        function TestMN(s) {
          //-- Admins can set up their own validation here --//
          var r, re;
          re = new RegExp("[\b\f\n\r\t\x20\x22\x2A-\x2C\x3B-\x3F\x7C\xA6]{1,256}");
          r = s.match(re);
          var retval = (r == null)? false : true;
          return retval;
        }
        function TestTxt(obj) {
          var retval = false;
          if ((TestMN(obj.value)) || (obj.value == L_gFQDN_Text)) {
            obj.value = L_gFQDN_Text;
            obj.style.color="gray";
            document.getElementById("ButtonConnect").disabled = true;
            var L_errMsgServerInvalid_Text = "The server name specified is invalid.";
            var rmsg = TSMsgBox(L_errMsgServerInvalid_Text, vbCritical, L_sTitle_Text);
            retval = true;
          }
          return retval;
        }
