var gOptionsNavigator = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=425, height=450, left=500"; 
var gWndChat;
  
function AJAXCreateRequest(aReqNum)
{
  var Result = null;
  
  if (aReqNum == 1)
  {
    if (window.XMLHttpRequest)
    {
      // -- use Native XMLHttpRequest
      Result = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
      // -- use IE/Windows ActiveX
      Result = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else if (aReqNum == 2)
  {
    if (window.XMLHttpRequest2)
    {
      // -- use Native XMLHttpRequest
      Result = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
      // -- use IE/Windows ActiveX
      Result = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return (Result);           
}

function AJAXPostRequest(aAJAXReq, aURL, aParams)
{
	aAJAXReq.open("POST", aURL, true);
	
	//Send the proper header infomation along with the request
	aAJAXReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	aAJAXReq.setRequestHeader("Content-length", aParams.length);
	aAJAXReq.setRequestHeader("Connection", "close");

	aAJAXReq.send(aParams);
  return true;
}
            
function PagePush(AMsg)
{
	var	Msgs	=	AMsg.split("|@|");
	var	I;
	var	Result	=	"";

	for (I = 0; (I < Msgs.length-1); I++)
	{
		if ((Msgs[I].split("|=|")[1].substring(0, 7) == "http://") ||
			(Msgs[I].split("|=|")[1].substring(0, 8) == "https://"))
		{
			Result = Msgs[I].split("|=|")[1];
			break;
		}					
	}
	return	(Result);
}

function FormatMsg(AMsg)
{
	var	Msgs	=	AMsg.split("|@|");
	var	I;
	var	Result	=	"";
  var DT = new Date();

	for (I = 0; (I < Msgs.length-1); I++)
	{
		if ((Msgs[I].split("|=|")[1] != "@CONNECTED") && (Msgs[I].split("|=|")[1] != "@DISCONNECTED"))
			Result = (Result +
					  "<tr><td><font style='color: black'><br>" + DT.toLocaleTimeString() + ": " + Msgs[I].split("|=|")[0] + "</font></td>" +
					  "<td><font style='color: blue'><br>&nbsp;&nbsp;&nbsp;" + Msgs[I].split("|=|")[1] + "</font></td></tr>");
	}
	return	(Result);
}

function FormatDebugMsg(aMsg)
{
	Result = "<tr><td><font style='color: green'><br>" + aMsg + "<br/></font></td></tr>";
  
	return	(Result);
}

function FormatErrorMsg(aMsg)
{
	Result = "<tr><td><font style='color: red'><br>" + aMsg + "<br/></font></td></tr>";
  
	return	(Result);
}

function FormatInfoMsg(aMsg)
{
	Result = "<tr><td><font style='color: brown'><br>" + aMsg + "<br/></font></td></tr>";
  
	return	(Result);
}

/**
 * Read the JavaScript cookies tutorial at:
 *   http://www.netspade.com/articles/javascript/cookies.xml
 */

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [multiple] Indica si el cookie contendrá multiple valores
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, multiple, expires, path, domain, secure)
{		
	if (multiple)
	{
		var	tmp = getCookie(name);		
		if (tmp != null)
			value = (tmp + "|@@@|" + value);
	}
	
	if (expires==null || expires=="")
	{
		expires = new Date(); 
 		expires.setTime(expires.getTime() + 3600000*24*1);
	}
    document.cookie= name + "=" + value +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);

    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) 
			return null;
    }
    else
    {
        begin += 2;
    }

    var end = document.cookie.indexOf(";", begin);

    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";	
    }
}

function EndSession()
{
	if (getCookie("ChatStatus") > "2")
	{
		setCookie("EndingSession", "END");
		top.open("/End.html","", gOptionsNavigator);
	}	
}		

function OpenURL(AURL)
{
	if (AURL != "")
		window.open(AURL,"","");
}


function Login(AHTTPServerName, AHTTPPortNumber, AACDGroup, AACDNum, AUserParams, AIDAccount)
{
	var	lErrorMsg	= "";
	var	lURL 		= "";
		
	if (AHTTPServerName == null || AHTTPServerName == "")
		lErrorMsg = "Invalid HTTP Server Name";
		
	if (AHTTPPortNumber == null || AHTTPPortNumber == "")
		lErrorMsg = ((lErrorMsg) ? lErrorMsg + " and " + "Invalid HTTP Port Number" : "Invalid HTTP Port Number");

	if (AACDGroup == null || AACDGroup == "")
		AACDGroup = "-1";
		
	if (AACDNum == null || AACDNum == "")
		AACDNum = "-1";

	if (AUserParams == null)
		AUserParams = "";

	if (AIDAccount == null)
		AIDAccount = "";
		
	if (lErrorMsg != "")
	{
		alert(lErrorMsg);
		return false;
	}

	lURL = 	("http://" + AHTTPServerName + ":" + AHTTPPortNumber + "/Login.html?&ACDGROUP=" + AACDGroup + "&ACDNUM=" + AACDNum + "&USERPARAMS=" + AUserParams + "&IDACCOUNT=" + AIDAccount);	

	gWndChat = top.open(lURL, "", gOptionsNavigator);
	
	return gWndChat;
}


function AddMessage(aMsg)
{
	setCookie("NewMessages", aMsg, true);
}

