    //--- HotComm Room Switching JS Utility
  //--- 7/3/08 changed ezpeer.net to crmsite.net due to vns move problems

//-- Timer
var ltms=(new Date()).valueOf();
function dtms() { var t=(new Date()).valueOf()-ltms; ltms+=t; return t;}
function tms()  { return (new Date()).valueOf();}

//-- Send debug output to DBMON (via debug version of IMC)

var dbTmr=0,dbMsg=null,dbflush=true;

function dbout()
{ var hsx,s;
    dbTmr=0;
    s=dbMsg; dbMsg=null;
    if (s)
      {
      if (hc_msie)
        hsx=new ActiveXObject("Microsoft.XMLHTTP");
      else
        hsx=new XMLHttpRequest();
      hsx.open("POST","/!db/",!dbflush);
      hsx.setRequestHeader("Content-Type","text/plain");      
      hsx.send(s);
      }    
}
function dsp(msg)
{ var t,dt,s="\n     ";

    t=(new Date()).valueOf(); dt=t-ltms; ltms=t;
    if (dt>99999)     s="\n";
    else if (dt>9999) s="\n ";
    else if (dt>999)  s="\n  ";
    else if (dt>99)   s="\n   ";
    else if (dt>9)    s="\n    ";
    s+=dt+" .. "+msg;
    if (dbMsg==null) dbMsg=s;
    else if (dbMsg.length<16000)
      dbMsg+=s;
    else
      {
      if (dbTmr) { clearTimeout(dbTmr); dbTmr=0;}
      dbMsg+=s;
      dbout();
      return;
      }
    if (dbflush) 
      {
      if (dbTmr) { clearTimeout(dbTmr); dbTmr=0;}
      dbout(); //dbflush=false;
      }
    else if (!dbTmr)
      dbTmr=setTimeout(dbout,2000);
}

function dsp(arg) {;}

//-- Get browser

var hc_tmo1=1000,hc_tmo2=300,hc_tmo3=3500,hc_tmo4=3000; // timeouts for loopback img, for retest, VNS image
var hc_brw=0,hc_iphone=false,hc_msie=false,hc_ie6=false,hc_moz=false,hc_ipod=false;
var hc_inst=0,hc_saf=false,hc_smobile=false,hc_opera=false;

function hc_getbrw()
{ var s=window.navigator.userAgent;
  
    hc_brw=0;  // unknown type
    //dsp("UserAgent="+s);
    if (s.indexOf("Safari")>=0)
      {
      hc_saf=true;
      if (s.indexOf("iPhone")>=0)
        hc_iphone=true,hc_smobile=true,hc_brw=4;
      else if (s.indexOf("iPod")>=0)
        hc_ipod=true,hc_smobile=true,hc_brw=7;
      else hc_brw=5;
      }
    else if (s.indexOf("MSIE")>=0)
      {
      if (s.indexOf("MSIE 6")>0 || s.indexOf("MSIE 5")>0)
        hc_ie6=true
      hc_msie=true; hc_brw=1;
      }
    else if (s.indexOf("Opera")>=0)
      hc_opera=true,hc_brw=6;
    else if (s.indexOf("Gecko")>=0 && s.indexOf("Mozilla")>=0)
      hc_moz=true, hc_brw=2; 
    /*if (hc_inst<1)
      { var i,c;
      i=s.indexOf(" hc");
      if (i>0)
        { 
        c=s.charCodeAt(i+3);
        if (c==0x20 || c==0x29 || c==0x3B)
          hc_inst=1;
        }
      }
      
    /*if (hc_inst<1)  // test mime types
      { var a,i,m,s;
      a=window.navigator.mimeTypes;
      s="Mime Types ("+a.length+")";
      for(i=0; i<a.length; ++i) 
        s+="\n"+i+"\tT="+a[i].type+" S="+a[i].suffixes+" D="+a[i].description;
      dsp(s);
      }*/
}

//-- Check for numeric IP

function HCNIP(x) 
{ var i,c;
  for (i=0; i<x.length; ++i)
    { 
    c=x.charCodeAt(0); 
    if (c==0x2E) continue; 
    if (c>=0x30 && c<=0x39) continue;
    return false;
    }
  return true;
}

//-- Dispatch code

function hc_seq() { var s=(new Date()).valueOf(); return s.toString(36); }

var hc_loaded=0,hc_testing=0,hc_img=null,hc_tid=0,hc_args=null,hc_ok=false,hc_srv=null;
var hc_retest=0,hc_uget=0,hc_wait=0,hc_wtid=0,hc_win2=1,hc_win=0,hc_run=0,hc_exe=0;
var hc_winhcu=null,hc_badsrv=null,hc_imgu=null,hc_gethcu=null,hc_relok=0;

//-- LINK SETTINGS

function HCSET(url,wt,win2,run,exe,badsrv)
{
  //if (hc_testing) return; // in progress
  if (hc_wtid) { clearTimeout(hc_wtid); hc_wtid=0;}
  hc_uget=url; hc_wait=wt;  hc_win2=win2; hc_run=run; hc_exe=exe;
  hc_badsrv=badsrv; hc_relok=0;
}

//-- Waiting callback for hc checks

function hc_wtck()
{   
   hc_wtid=0;
   dsp("Re-check HC.. testing="+hc_testing+" retest="+hc_retest+" ARG="+hc_args);
   if (hc_testing>0)  return;     // testing again, leave
   if (!hc_retest) return;      // retests disabled
   //hc_retest=0;
   if (hc_args) HCGO(hc_args);
   
}
//-- HC not found, download new one

/*function show_stat()
{
  dsp("STATUS: Installed="+hc_inst+" Loaded="+hc_loaded+" uget="+hc_uget); 
}*/

function hc_tmohcu()
{ var s;
  dbflush=1; dsp("hc_tmohcu(ok="+hc_relok+")");
  if (hc_relok) return;
  hc_relok=-1;
  s=hc_badsrv; 
  if (s)
    {
    hc_badsrv=null;
    window.location.href=s;
    }
}
function hc_erru()
{
  hc_tmohcu();
}
function hc_loadu()
{
  dbflush=1; dsp("hc_loadu(ok="+hc_relok+")");
  hc_badsrv=null; hc_relok=1;
}

function hc_testRel()
{
    hc_imgu=new Image();
    hc_imgu.onload=hc_loadu;
    hc_imgu.onerror=hc_erru;
    setTimeout(hc_tmohcu,2500);
    hc_imgu.src="http://"+hc_srv+".crmsite.net/_G_-1."+hc_seq()+"/";  
}
function hc_no()  // no running hotcomm found
{
  dsp("hc_NO: Installed="+hc_inst+" Rel="+hc_relok+" wtid="+(hc_wtid?1:0)+" wait="+(hc_wait?1:0)+" retest="+(hc_retest?1:0)+
      " win2="+(hc_win2?1:0)+" win="+(hc_win?1:0)+" Run="+hc_run+" uget="+hc_uget);
  
  if (!hc_inst && hc_run && (hc_ntest>0)) { setTimeout(hc_no,300); return;} // wait for hc-install detection to finish
  if (hc_wtid)  { clearTimeout(hc_wtid); hc_wtid=0;}
  if (hc_inst>0 && hc_run)  // hotComm is installed but not loaded - run it
    { var s=hc_args;
    
    hc_args=null; 
    if (hc_loaded<1)
      {
      hc_retest=2; setTimeout(hc_isLoad,2000);
      }    
    s="http://"+hc_srv+".crmsite.net/_G_U."+hc_seq()+".hcurl?"+s;
    dsp("HCURL Req=("+s+")");
    window.location.href=s;
    //window.location.href="http://192.168.2.10:8180/_G_U."+hc_seq()+".hcurl?"+hc_args;
    return;
    }
  if (hc_retest==1) // must wait for image
    {
    hc_wtid=setTimeout(hc_wtck,hc_tmo2);
    return;
    }
  if (hc_uget)
    { s=hc_uget;
    hc_uget=null;
    if (hc_wait) 
      { hc_retest=1; hc_wtid=setTimeout(hc_wtck,hc_tmo2);}
    if (!hc_win && hc_win2)
      hc_win=window.open(s,"_blank");
    else
      window.location.href=s;
    //setTimeout(show_stat,3000); //@for dbg
    return;
    }
  else if (hc_win) {hc_win.close(); hc_win=null;}
}

function hc_clr(Ok)
{ 
  dsp("CLR Ok="+Ok+" Test="+hc_testing+" Inst="+hc_inst+" LD="+hc_loaded+" retest="+hc_retest+" A=("+hc_args+")");
  if (hc_tid) { clearTimeout(hc_tid); hc_tid=0;}
  //hc_img=null;
  if (hc_testing>0) --hc_testing;
  if (Ok<=0) 
    { 
    if (hc_loaded==0)
      hc_loaded=-1;
    if (hc_loaded>0) hc_retest=0;
    if (!hc_args)
      {
      if (hc_retest>1 && hc_testing<=0 && hc_msie) 
        {
        hc_testing=0;
        setTimeout(hc_isLoad,2500);
        }
      }
    else hc_no(); 
    }
  else
    {
    hc_loaded=1; hc_inst=1; hc_retest=0; hc_args=null;
    if (hc_wtid) { clearTimeout(hc_wtid); hc_wtid=0;}
    if (hc_win)  { hc_win.close(); hc_win=null;}
    if (hc_msie && !hc_ie6) hc_loaded=-1;
    }
}
function hc_imgtmo()
{
  dsp("HC Image Load timeout. LD="+hc_loaded);// IMG="+hc_img?1:0);
  hc_tid=0; hc_clr(0);
}
function hc_loaderr()
{
  dsp("HC Image load Error. LD="+hc_loaded);// IMG="+hc_img?1:0);
  hc_clr(-1);
}
function hc_loadok()
{
  hc_inst=1; hc_loaded=1;
  dsp("HC Image load OK.");// IMG="+hc_img?1:0);
  hc_clr(1);
}

function hc_isLoad()
{    
    if (!hc_args && hc_loaded>0) 
      { hc_retest=0; return; }
    ++hc_testing;
    hc_img=new Image();
    hc_img.onload=hc_loadok;
    hc_img.onerror=hc_loaderr;         
    hc_tid=setTimeout(hc_imgtmo,hc_tmo1);
    hc_img.src="http://localhost:9190/_G_r."+hc_seq()+(hc_args?("?"+hc_args):"");    
}
function HCX(vSrv,vRoom,vUname,vPsw,vAsk,vPannel,vEmail,vEntryCode,vButtons,vOptArgs)
{ var a,s;
    if (!vSrv) return null; // missing required arguments
    hc_srv=vSrv;
    if (!hc_relok)  // test relay
      hc_testRel();
    a="s="+escape(vSrv);
    if (vRoom) a+="&r="+escape(vRoom);
    if (vUname) a+="&u="+escape(vUname);
    if (vPsw) a+="&p="+escape(vPsw);
    if (vAsk) a+="&a=1"; // ask for username/psw
    if (vPannel) a+="&P=1";
    if (vEmail) a+="&e="+escape(vEmail);
    if (vEntryCode) a+="&c="+escape(vEntryCode);
    if (vOptArgs) a+="&o="+escape(vOptArgs);
    if (vButtons) a+="&b="+escape(vButtons);
    s=document.location.href;
    if (s.indexOf("http://")==0)
      s=s.substr(7);
    a+="&f="+escape(s); // add referer page
    hc_wtarg=a;
    if (hc_wtid) { clearTimeout(hc_wtid); hc_wtid=0;}
    if (hc_win) { hc_win.close(); hc_win=null;}
    hc_retest=0; hc_args=null; 
    HCGO(a);
}

function HCGO(urlArgs)
{
    dsp("HCGO Test="+hc_testing+" Inst="+hc_inst+" LD="+hc_loaded+" retest="+hc_retest+" A=("+urlArgs+")");
    if (!urlArgs) return false;
    hc_args=urlArgs;
    hc_testing=0; // disable retests
    if (hc_loaded>0 || !hc_msie)      
      {
      hc_loaded=-1;
      hc_isLoad(); return true;
      }
    //setTimeout(hc_switch,10);
    if (hc_loaded<=0)
      {
      if (hc_inst>0)  // not loaded but installed -- run it
        hc_no();
      else            // not installed or loaded, go download it
        setTimeout(hc_isLoad,20); // do retest & dispatch in separate thread
      }
    return true;
}

//-- Installation check

var hc_vns=0,hc_vnstid=0,hc_ntest=3;  // installation check
var hc_vns5=0,hc_vns2=0,hc_vnstid5=0,hc_vnstid2=0;

function hc_clrvns(Ok)
{  
  if (Ok>0)   // installed
    { 
    hc_inst=Ok; 
    if (hc_vnstid2) {clearTimeout(hc_vnstid2); hc_vnstid2=0;}
    if (hc_vnstid5) {clearTimeout(hc_vnstid5); hc_vnstid5=0;}
    if (hc_vnstid) {clearTimeout(hc_vnstid); hc_vnstid=0;}
    }
   else
    {
    if (--hc_ntest>0) // still some tests to go
      return;
    if (hc_inst<=0 && !hc_loaded) 
      hc_inst=Ok;
    }
    
}

//-- NEW TEST with /!!G

function hc_vnsloadok()
{  
  if (hc_vnstid) {clearTimeout(hc_vnstid); hc_vnstid=0;}
  dsp("Loaded VNS image "+this.src);
  hc_clrvns(-1);
}
function hc_vnsloaderr()
{
  hc_clrvns(1);
  dsp("Error loading VNS image.");
}
function hc_vnstmo()
{
  hc_vnstid=0; hc_clrvns(-1);
  dsp("Timeout loading VNS image.");
}

//-- Original general check

function hc_vnsloadok2()
{  
  dsp("Loaded VNS image2 "+this.src);
  hc_clrvns(1);
}
function hc_vnsloaderr2()
{
  if (hc_vnstid2) {clearTimeout(hc_vnstid2); hc_vnstid2=0;}
  hc_clrvns(-1);
  dsp("Error loading VNS image2.");
}
function hc_vnstmo2()
{
  hc_vnstid2=0; hc_clrvns(-1);
  dsp("Timeout loading VNS image2.");
}

//-- CL check
function hc_vnsloadok5()
{  
  if (hc_vnstid5) {clearTimeout(hc_vnstid5); hc_vnstid5=0;}
  dsp("Loaded VNS image5 "+this.src);
  hc_clrvns(-1);
}
function hc_vnsloaderr5()
{
  if (hc_vnstid5) hc_clrvns(1);
  dsp("Error loading VNS5 image.");
}
function hc_vnstmo5()
{
  hc_vnstid5=0; hc_clrvns(-1);
  dsp("Timeout loading VNS5 image.");
}

function hc_isInst()
{
    dsp("Starting VNS Image test... inst="+hc_inst+" loaded="+hc_loaded);
    if (hc_inst>0 || hc_loaded>0) return;  // test already done
    
    hc_vns=new Image(); // new test
    hc_vns.onload=hc_vnsloadok;
    hc_vns.onerror=hc_vnsloaderr;        
    hc_vnstid=setTimeout(hc_vnstmo,hc_tmo4);  
    hc_vns.src="http://vnsjs1.1stworks.com/!!G";
    
    hc_vns5=new Image();  // CL test
    hc_vns5.onload=hc_vnsloadok5;
    hc_vns5.onerror=hc_vnsloaderr5;        
    hc_vnstid5=setTimeout(hc_vnstmo5,hc_tmo2);    
    hc_vns5.src="http://hcurltest5/!!ditest";

    hc_vns2=new Image();  // old test
    hc_vns2.onload=hc_vnsloadok2;
    hc_vns2.onerror=hc_vnsloaderr2;        
    hc_vnstid2=setTimeout(hc_vnstmo2,hc_tmo4);
    hc_vns2.src="http://hcurltest2/!!ditek1";
}

//-- MAIN INIT FOR HC Room Switching

  hc_getbrw();  // get browser type
  hc_ntest=3;
  setTimeout(hc_isLoad,100);
  setTimeout(hc_isInst,200);
  dsp("Starting HCGO Library -- Browser="+hc_brw+"="+navigator.userAgent);


