var hInterval;

function showChat(){
   var arrived = new Date();
   writeSessionCookie("sessionInfo", "1::" + arrived);

   var d = document.getElementById("contactbar");

   d.style.visibility="hidden";
   window.open("/lindchat/gateway.cgi?type=start&src2chat=" + window.location, "_blank", "location=no,toolbar=no, resizeable=no, status=no, height=400, width=400");
}

function askChat(popped){
   var d = document.getElementById("contactbar");
   if (typeof d == "undefined"){
     window.setTimeout("askChat('" + popped + "')", 500); 
   }
   else
   {

   writeSessionCookie("sessionInfo", "2::" + timeStarted);
   var iWidth = document.body.clientWidth - 470;

   if (iWidth < 460)
      iWidth = 460;
/*
   d.style.posTop = 0;
   d.style.width = iWidth;
   d.style.visibility = "visible";
*/
   d.style.height = "0px";
   d.style.visibility = "visible";

   if (popped == "2"){
      //d.style.left = 460;
      d.style.height = "125px";
   }
   else{

      //d.style.left = document.body.clientWidth + iWidth + 10;
      hInterval = window.setInterval("scrollChat()", 30);
   }

   }
}

function scrollChat(){
   var d = document.getElementById("contactbar");
/*
   if (d.style.posLeft > 460){
      d.style.posLeft = d.style.posLeft - 10;
   }
   else{
      d.style.posLeft = 460;
      window.clearInterval(hInterval);
   }
*/
   if (d.style.posHeight < 125)
   {
      d.style.height = d.style.posHeight + 5;
   }
   else
   {
      d.style.height = "125px";
      window.clearInterval(hInterval);
   }

}


function noThanks(){
   var arrived = new Date();
   writeSessionCookie("sessionInfo", "1::" + arrived);

   var d = document.getElementById("contactbar");
   d.style.visibility = "hidden";
}

function neveragain(){
   var d = document.getElementById("contactbar");

   writePermCookie("neverAgain", "1");
   d.style.visibility="hidden";
}



function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}


function writeSessionCookie (cookieName, cookieValue) {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/;";
}



function writePermCookie (CookieName, CookieValue) {

  document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=Mon, 27 Dec 2027 08:10:00 UTC; path=/;";
}  

var sessionInfo = getCookieValue("sessionInfo");
var neverAgain = getCookieValue("neverAgain");
var dSTime = new Date();
var popTime = 60000; 
var timeStarted;

if (neverAgain != "1" && shouldShowPop() == true){
	if (sessionInfo != false){
		var ar = sessionInfo.split("::");
		var popped = ar[0];

		if (ar[1] = "NaN")
		{
			timeStarted = new Date();
		}
		else
		{
			timeStarted = new Date(ar[1]);
		}
		if (popped != 1){
			if (popped == 2)
			{
				askChat(popped);
			}
			else
			{
			if ((popTime - (dSTime - timeStarted)) < 2000){
				window.setTimeout("getChatStatus(popped);", 3000);
			}
			else{
				window.setTimeout("getChatStatus(popped)", popTime - Math.ceil(dSTime - timeStarted));
			}
			}
		}

	}
	else{
		var arrived = new Date();
		writeSessionCookie("sessionInfo", "0::" + arrived);
		window.setTimeout("getChatStatus()", popTime);
	}
}

