
// Array of all open windows
var windows = new Array();



function showChatButton(workgroup) {
    var d = new Date();
    var v1 = d.getSeconds() + '' + d.getDay();
    var base_url = "http://pbx.vocalnet.ro:9090/webchat";
    var img = "http://pbx.vocalnet.ro:9090/webchat/live?action=isAvailable&workgroup=" + workgroup;
    var gotoURL = "http://pbx.vocalnet.ro:9090/webchat/start.jsp?workgroup=" + workgroup + "&location=" + window.location.href;
    // document.write("<a href=\""+base_url+"/#\" onclick=\"launchWin(\'framemain\','"+gotoURL+"',500, 400);return false;\"><img border=\"0\" src=\""+img+"\"></a>");
    document.write("<a href=\"#\" onclick=\"launchWin(\'framemain\','"+gotoURL+"',500, 400);return false;\"><img border=\"0\" src=\""+img+"\"></a>");

}

// Open a window given its unique name, url, width and height.

function launchWin(name, url, width, height) {
  var defaultOptions = "location=no,status=no,toolbar=no,personalbar=no,menubar=no,directories=no,";
  var winleft = (screen.width - width) / 2;
  var winUp = (screen.height - height) / 2;

  defaultOptions += "scrollbars=no,resizable=yes,top=" + winUp + ",left=" + winleft + ",";
  defaultOptions += "width=" + width + ",height=" + height;

  launchWinWithOptions(name, url, defaultOptions);

}

// Open a window with given name, url, and options list

function launchWinWithOptions(name, url, options) {
  if (! windowExists(name)) {
    var winVar = window.open(url, name, options);
    windows[windows.length] = winVar;
    return winVar;
  }
  else{
    var theWin = getWindow(name);
    theWin.focus();
  }

}

// Returns the window object - returns nothing if not found.

function getWindow(name) {
  for (var i = 0; i < windows.length; i++) {
    try {
      if (windows[i].name == name) {
        return windows[i];
      }
    }
    catch (exception) {
    }
  }
}

// Checks to see if a window exists

function windowExists(name) {
  for (var i = 0; i < windows.length; i++) {
    // IE needs a try/catch here for to avoid an access violation on windows[i].name
    // in some cases.
    try {
      if (windows[i].name == name) {
        return true;
      }
    }
    catch (exception) {
    }
  }
  return false;
}

