if (!window.unemail)
	window.unemail = '';

avail = new Image();
avail.src = "http://admin.instantservice.com/resources/smartbutton/6152/"+departmentid+"/available.gif?"+Math.floor(Math.random()*10001);
avail.onload = function() {
	agents_available(departmentid);
}
avail.onerror = function() {
	if (window.departmentid2) {
		avail2 = new Image();
		avail2.src = "http://admin.instantservice.com/resources/smartbutton/6152/"+departmentid2+"/available.gif?"+Math.floor(Math.random()*10001);
		avail2.onload = function() {
			agents_available(departmentid2);
			departmentid = departmentid2;
		}
		avail2.onerror = function() {
			agents_not_available(departmentid2, unemail);
		}
	} else {
		agents_not_available(departmentid, unemail);
	}
}
function agents_available(departmentid) {
	if (window.custom_element && document.getElementById(custom_element)) {
		document.getElementById(custom_element).style.cursor = 'pointer';
		document.getElementById(custom_element).onclick = function() {
			window.open('https://admin.instantservice.com/links/6152/'+departmentid, 'custclient', 'width=600,height=160,scrollbars=0');
			return false;
		}
	} else {
		document.getElementById('smartbutton').innerHTML = '<a href="" onClick="window.open(\'https://admin.instantservice.com/links/6152/'+departmentid+'\',' + 
		'\'custclient\',\'width=600,height=160,scrollbars=0\');return false;">' + 
		'<img src="http://northwindsoftware.com/assets/statichelp_large.gif" border="0"></a>';
	}
	return true;
}
function agents_not_available(departmentid, unemail) {
	if (window.unemail) {
		if (unemail.indexOf('@',unemail) >= 0) {
			var mt = 'mailto:';
		} else {
			var mt = '';
		}
		if (window.custom_element && document.getElementById(custom_element)) {
		  wrapNode(document.getElementById(custom_element), 'a', mt+unemail);
		  if (window.ce_offline) {
        document.getElementById(custom_element).setAttribute('src', ce_offline);
      }
		} else {
      document.getElementById('smartbutton').innerHTML = '<a href="'+mt+unemail+'"><img src="http://northwindsoftware.com/assets/unavailable.gif" border="0"></a>';
    }
	} else {
    if (window.custom_element && document.getElementById(custom_element)) {
      if (window.ce_offline) {
        document.getElementById(custom_element).setAttribute('src', ce_offline);
      }
    } else {
      document.getElementById('smartbutton').innerHTML = '<img src="http://northwindsoftware.com/assets/unavailable.gif" border="0">';
    }
	}
	return true;
}
function wrapNode(el, wrapper, url) { 
  var newNode = document.createElement(wrapper); 
  var parent = el.parentNode; 
  newNode.appendChild(el.cloneNode(true));
  newNode.setAttribute('id','northwind_ce_wrapper');
  if (wrapper == 'a' && url)
    newNode.setAttribute('href', url);
  parent.replaceChild(newNode, el);  
} 
document.write('<div id="smartbutton"></div>');