var oConn;
function assignXMLHTTP()
{
  //=====================================================\
  //
  // get an HTTPRequest object based on browser
  //
  //=====================================================/
    var a;
    var i;
    oConn = null;
    //alert(window.XMLHttpRequest);
    // typeof XMLHttpRequest != 'undefined'  // another site(http://code.jalenack.com/periodic/) uses this
    if (window.XMLHttpRequest) {
        try
        {
          oConn = new XMLHttpRequest(); // for Firefox, Opera & Safari...
        }
        catch(e)
        {
           alert("XMLHttpRequest() err:" + e.description);
        }
        }
    else
        {
        // branch for IE/Windows ActiveX version
        a = new Array();
        i = 0;
        a[i]      = "Microsoft.xmlHTTP";
        i++; a[i] = "Microsoft.xmlHTTP.1";
        i++; a[i] = "Microsoft.xmlHTTP.1.0";
        i++; a[i] = "Msxml2.xmlHTTP";
        i++; a[i] = "Msxml2.xmlHTTP.2.6";
        i++; a[i] = "Msxml2.xmlHTTP.3.0";
        i++; a[i] = "Msxml2.xmlHTTP.4.0";
        i++; a[i] = "Msxml2.xmlHTTP.5.0";
        i++; a[i] = "Msxml2.xmlHTTP.6.0";
        // search backwards thru array
        for (i=a.length-1; i>=0; i--)
        {
           try
           {
              oConn = new ActiveXObject(a[i]);
              break;
           }
           catch(e)
           {
              if (i==0)
              {
                 alert("client.js: xmlhttp assign err:\n proceed to download MS XML Service Pack\n then restart your browser(IE) and load page again");
                 // client needs to download this: MSXML (Microsoft XML Parser) 3.0 Service Pack 4 SDK
                 window.open("http://www.microsoft.com/downloads/details.aspx?FamilyId=B432CD45-B7ED-4C32-A443-EC56F10EE175&displaylang=en");
              }
              else
              {
                continue;
              }
           }
        }
        //alert("object found: " + i + "=" + a[i]);
    }
    return oConn;
}

function HideText()
  //=====================================================\
  //
  // designed to hide the "Please Wait..." message
  //
  //=====================================================/
{
    if (document.all) {
       /* alert("Internet Explorer Detected"); */
       PleaseWait.innerText= ' ';
    }
    else if (document.layers) {
            /* alert("Netscape Navigator Detected"); */
            document.layers['PleaseWait'].document.open();
            document.layers['PleaseWait'].document.write('');
            document.layers['PleaseWait'].document.close();
         }
         else if (document.getElementById) {
                 /* alert("Netscape 6 Detected"); */
                 document.getElementById("PleaseWait").innerHTML  = ' ';
              }
              else {
                 /* alert("Unrecognized Browser Detected"); */
              }
}

function showPopup(htmlSrc)
  //=====================================================\
  //
  // designed to show error messages from the server
  //
  //=====================================================/
{
    var host = window.document;
    var p = window.createPopup();
    var d = p.document;
    d.body.innerHTML = htmlSrc;
    with(d.body.style){
      border="window-inset 1px green";
      backgroundColor="lightyellow";
      color="blue";
      padding="1em";
    }
    // Measure the document
    p.show(0,0,200,200);
    w = d.body.scrollWidth;
    h = d.body.scrollHeight;
    //alert("host height=" + host.body.clientHeight + " err height=" + h);
    //p.show(window.bdy.clientWidth-w,window.bdy.clientHeight-h, w, h);
    p.show(host.body.clientWidth-w,host.body.clientHeight-h, w, h);
}

function PrimeToolTip ()
{
   //window.document.thisform.Coach.Title = 'xxx';//
   //window.document.forms[0].Coach.Title = 'xxx';    //
   document.forms[0].all("Coach").Title = 'xxx';
}

var oldrow;
var oldcolor;
oldrow = '';

function push(r)
  //=====================================================\
  //
  // assign color to a table row when radio is pushed
  // TABLE needs an ID='oTable'
  // TR needs an id='TRnnn'
  //
  //=====================================================/
{
  var row;
  var obj;
  var curr_row;
  var t;
  t = window.document.getElementById("oTable");
  //document.forms[0].GameId.value = r;
  window.document.getElementById("GameId").value = r;
  row = 'tr' + r;
  if(oldrow != '')
  {
    for (curr_row = 0; curr_row < t.rows.length; curr_row++)
    {
        if(t.rows[curr_row].id == oldrow)
        {
           t.rows[curr_row].style.backgroundColor = oldcolor;
           break;
        }
    }
  }
  else
  {
  }
  for (curr_row = 0; curr_row < t.rows.length; curr_row++)
  {
      if(t.rows[curr_row].id == 'tr' + r)
      {
         oldcolor = t.rows[curr_row].style.backgroundColor;
         t.rows[curr_row].style.backgroundColor = "lightyellow";
         break;
      }
  }
  oldrow = 'tr' + r;
}

function mail2(c,ema)
{
  var rv = true;
  if(ema.indexOf("@") == -1 || ema.indexOf(".") == -1)
  {
    rv = false;
  }
  else
  {
    window.clipboardData.setData("Text",ema);
    c.href = "mailto:" + ema;
  }
  return rv;
}

function CWDate2JSDateO (cwDate)
{
    // accepts a CW long value for a DATE e.g. 75432
    // returns a JS DATE object
    var dd = 61730;     //date difference in days from Jan 1 1970
    var newJSDate;
    var rv;
    if (cwDate == null || cwDate == "null" || cwDate == "")
    {
       newJSDate = 0;
    }
    else
    {
       newJSDate = (cwDate - dd + 1) * 24 * 60 * 60 * 1000;
    }
    rv = new Date(newJSDate);
    return rv;
}

function CWTime2JSTimeO (cwTime)
{
    // accepts a CW long value for a TIME e.g. 63300001
    // returns a JS DATE object
    var newJSTime;
    var rv;
    if (cwTime == null || cwTime == "null" || cwTime == "")
    {
          newJSTime = 0; // set to zero...no time specified
    }
    else
    {
       if (cwTime == 1)
       {
          newJSTime = 0; // set to zero...its midnight
       }
       else
       {
          newJSTime = (cwTime-1)*10;
       }
    }
    rv = new Date(newJSTime);
    return rv;
}

function CWDate2VBDate (cwDate)
{
    // for testing 74831 = Mon 11/14/2005
    var newVBDate;      //new VB Date
    var dd = 61730;     //date difference in days from Jan 1 1970
    var rv;

    if (cwDate == null || cwDate == "null" || cwDate == "")
    {
       newVBDate      = "";   // so that we can display a blank date
    }
    else
    {

       newVBDate      = (cwDate - dd + 1) * 24 * 60 * 60 * 1000;
    }
                                //and finally return the VB style Date
    rv  = newVBDate;
    newVBDate  = null;
    dd         = null;
    return rv;
}

function VBDate2CWDate (vbDate)
{
    var jsdate;
    var newCWDate;      //new CW Date
    var dd = 61730;     //date difference in days from Jan 1 1970
    var rv;

    if (vbDate == null || vbDate == "null" || vbDate == "")
    {
       newCWDate      = null;
    }
    else
    {
       jsdate = new Date(vbDate);
       newCWDate = parseInt(jsdate.valueOf() / (24 * 60 * 60 * 1000)) + dd;
    }
    // and finally return the CW style Date
    rv  = newCWDate;
    newCWDate  = null;
    return rv;

}


function CWTime2VBTime (cwTime)
{
    var rv;
    var newVBTime;
    if (cwTime == null || cwTime == "null" || cwTime == "")
    {
       newVBTime  = "";   // so that we can display a blank time
    }
    else
    {
       if (cwTime == 1)
       {
          newVBTime = 0;
       }
       else
       {
          //newVBTime  = (cwTime-1)/(24*60*60*100);
          newVBTime  = (cwTime-1)*10;
       }
    }
    rv = newVBTime;
    return rv;
}

function VBTime2CWTime (vbTime)
{
    var NoOfSecs;
    var rv;
    var jstime;
    var newVBTime;

    if (vbTime == null || vbTime == "null" || vbTime == "")
    {
       newVBTime  = null;
    }
    else
    {
       //jstime = new Date(vbTime);  // NaN
       jstime = new Date("1/1/1970, " + vbTime); //works I think!
       //jstime = new Date(1970,1,1,11,29); //works I think!
       NoOfSecs = ((jstime.getHours()*60) + jstime.getMinutes()) * 60;
       newVBTime = (NoOfSecs*100)+1;
    }
    rv = newVBTime;
    NoOfSecs  = null;
    return rv;

}


function WeekDayName (d, abbrev)
{
    var rv;
    switch (d % 7)
    {
    case   0: rv = "Sunday"; break;
    case   1: rv = "Monday"; break;
    case   2: rv = "Tuesday"; break;
    case   3: rv = "Wednesday"; break;
    case   4: rv = "Thursday"; break;
    case   5: rv = "Friday"; break;
    case   6: rv = "Saturday"; break;
    }
    switch (abbrev)
    {
    case   false:  WeekDayName = rv; break;
    case   0:      WeekDayName = rv; break;
    case   1:      WeekDayName = rv.substr(0,2); break;
    default:       WeekDayName = rv.substr(0,abbrev); break;
    }
}

function FormatDateTime4Me(dt,f)
{
    var rv;
    if (dt == null || dt == "null" || dt == "")
    {
       rv = "";
    }
    else
    {
       var mydt = new Date(dt);
       switch (f)
       {
       case 2:   rv = (mydt.getMonth()+1) + "/" + mydt.getDate() + "/" + mydt.getYear(); break;
       case 4:   rv = mydt.getUTCHours() + ":" + getUTCMinutes(); break;
       default:  rv = FormatDateTime(dt,f); break; // standard vbs function
       }
    }
    return rv;
}
