// base on js.js 1.10 and env.inc 1.38.1.19

var callMeSchedule = new Array(7);
for (i = 0; i < 7; i++)
{
  callMeSchedule[i] = new Array(2);
  callMeSchedule[i][0] = "0:00";
  callMeSchedule[i][1] = "0:00";
}
var chatSchedule = new Array(7);
for (i = 0; i < 7; i++)
{
  chatSchedule[i] = new Array(2);
  chatSchedule[i][0] = "0:00";
  chatSchedule[i][1] = "0:00";
}
var autoContactSchedule = new Array(7);
for (i = 0; i < 7; i++)
{
  autoContactSchedule[i] = new Array(2);
  autoContactSchedule[i][0] = "0:00";
  autoContactSchedule[i][1] = "0:00";
}
var errorAutoContactSchedule = new Array(7);
for (i = 0; i < 7; i++)
{
  errorAutoContactSchedule[i] = new Array(2);
  errorAutoContactSchedule[i][0] = "0:00";
  errorAutoContactSchedule[i][1] = "0:00";
}

function toMinutes(time)
{
  var seperatorIndex = time.indexOf(":");
  var hours = parseInt(time.substring(0, seperatorIndex));
  var minutes = parseInt(time.substring(seperatorIndex + 1, time.length));

  return hours * 60 + minutes;
}

function isCallCenterOpen(schedule)
{
  var value = false;
  var date = new Date();
  var dow = date.getDay();
  var openTime, closeTime;
  var currentTime = date.getHours() * 60 + date.getMinutes() + date.getTimezoneOffset();
  
  if (currentTime < 0)
  {
    currentTime = 1440 + currentTime;
  }
  else if (currentTime >= 1440)
  {
    currentTime = currentTime - 1440;
  }
   
  openTime = toMinutes(schedule[dow][0]);
  closeTime = toMinutes(schedule[dow][1]);
  if (((openTime <= closeTime) && ((openTime <= currentTime) && (currentTime <= closeTime))) ||
      ((openTime > closeTime) && ((openTime <= currentTime) || (currentTime <= closeTime))))
  {
    value = true;
  }

  return value;
}


function minimizeContactModule()
{
  if (document.bottomForm)
  {
    for (var i = 0; i < document.bottomForm.bottomSelect.length; i++)
    {
      if (document.bottomForm.bottomSelect.options[i].value == "javascript:popUpContactModule(chatUrl, 'chat')")
      {
        swapImage("stab2Image", "//www.ibm.com/i/c.gif");
        for (var j = 0; j < document.links.length; j++)
        {
          if (document.links[j].href == "javascript:popUpContactModule(chatUrl, 'chat')")
          {
            document.links[j].onmouseover = null;
            document.links[j].onmouseout = null;
            document.links[j].href = "#";
          }
        }
        document.bottomForm.bottomSelect.options[i] = null;
      }
      if (document.bottomForm.bottomSelect.options[i].value == "javascript:popUpContactModule(callMeUrl, 'callMe')")
      {
        swapImage("stab3Image", "//www.ibm.com/i/c.gif");
        for (var j = 0; j < document.links.length; j++)
        {
          if (document.links[j].href == "javascript:popUpContactModule(callMeUrl, 'callMe')")
          {
            document.links[j].onmouseover = null;
            document.links[j].onmouseout = null;
            document.links[j].href = "#";
          }
        }
        document.bottomForm.bottomSelect.options[i] = null;
      }
    }
  }
  else if ((document.all) && (document.all.contactModule))
  {
    document.all.contactModule.style.height = 1;
    document.all.contactModule.style.width = 1;
  }
  else if ((document.getElementById) && (document.getElementById("contactModule")))
  {
    document.getElementById("contactModule").style.height = 0;
    document.getElementById("contactModule").style.width = 0;
  }
}

function showContactModule()
{
  if (isCallCenterOpen(callMeSchedule) || isCallCenterOpen(chatSchedule))
  {
    if (document.bottomForm)
    {
    }
    else if ((document.layers) && (document.contactModule))
    {
      document.contactModule.visibility = "show";
    }
    else if ((document.all) && (document.all.contactModule))
    {
      document.all.contactModule.style.visibility = "visible";
    }
    else if ((document.getElementById) && (document.getElementById("contactModule")))
    {
      document.getElementById("contactModule").style.visibility = "visible";
    }
  }
  else
  {
    minimizeContactModule();
  }
}

function popUpContactModule(url, name)
{
  var sourceId = getCookie("sourceid0");
  var navCode = getSubCookie("commerce", "NavCode");

  url += "&callUsSrcPage=http%3A%2F%2F" + window.location.host + pageUrl;
  if ((sourceId != null) && (trackBeFreeTeleWeb == true))
  {
    url += "&sourceid0=" + sourceId;
  }
  if (navCode != null)
  {
    url += "&navcode=" + navCode;
  }
  popup(url, 506, 382, name);
}

function popUpAutoContactModule(schedule, url, name)
{
  if (isCallCenterOpen(schedule))
  {
    popUpContactModule(url, name);
    setSubCookie("commerce", "contactModule", "off");
  }
}

function startAutoContact()
{
  if ((getSubCookie("commerce", "contactModule") != "off") && (isCallCenterOpen(autoContactSchedule)))
  {
    window.setTimeout("popUpAutoContactModule(autoContactSchedule, autoContactUrl, 'auto')", autoContactTimeOutValue);
  }
}

function startErrorAutoContact()
{
  if ((getSubCookie("commerce", "contactModule") != "off") && (isCallCenterOpen(errorAutoContactSchedule)))
  {
    window.setTimeout("popUpAutoContactModule(errorAutoContactSchedule, errorAutoContactUrl, 'autoError')", errorAutoContactTimeOutValue);
  }
}
