function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			   /* alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." ); */
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	
	return encoded;
};

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
	return plaintext;
};

function popup(str,strHostName){
	/* var popupSrc="<html><head><title>AccessBlue Logout</title>" +
					 "<script language='javascript'>setTimeout('win=top; win.opener = top; win.close();',3000);</script>"+
					 "</head><body bgcolor='#ffffff'><center><table width='100%' border='0' cellspacing='0' cellpadding='0'>" +
					 "<tr><td width='100%'>&nbsp;</td></tr><tr><td width='100%' align='center'><b>"+str+"</b></td></tr>"+
					 "<tr><td width='100%'>&nbsp;</td></tr>"+
					 "<tr><td width='100%' align='center'><img name='bar' src='/web/Images/Loading.gif' style='display:block'></td>"+
					 "</tr></table></center></body></html>" ; */
  
  
  if (window.showModalDialog) {
		window.showModalDialog("/web/includes/LogoutNew.asp?message=" + 
			URLEncode(str),"w"+(new Date()).getTime(),"width=425,height=125");
    
    //check the hostname
		if (strHostName != null)
		{
			//alert(strHostName);
			//if (strHostName.toLowerCase().substr(0,5) == "d3743") {
			if (strHostName.toLowerCase().substr(0,9) == "citrixsrv") {
			//if (strHostName.toLowerCase().substr(0,5) == "d2947") {
				document.execCommand("ClearAuthenticationCache");
				reDirectNow();
			}
			else {
			//	alert('closing');
				closeNow();
			}
		}
		else {
			//alert(strHostName);
			//alert("Null!");
			closeNow();
		}
  }
  
  else {
    /* var wndObject = window.open("/web/includes/LogoutNew.asp?message=" + URLEncode(str),"w"+(new Date()).getTime(),
        "width=400,height=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes");
   
	//check the hostname
	
    wndObject.addEventListener("unload", alertNow, false);*/
    
    alertNow();
  }
  //Navigate back to the page here
}

function reDirectNow()
{
     /*count += 1;
    
     if ((navigator.appName.indexOf('Microsoft') == -1) && count > 1) {
        // alert("yay");
         window.location = "/web/RedDotCM/HTML/default.asp";
     }
     else
     { */
        
		window.location = "/web/RedDotCM/HTML/default.asp";
     // }
     
}

function closeNow()
{
	setTimeout('win=top; win.opener = top; win.close();',50);
}

function alertNow()
{
	alert ("You must close your web browser to completely end your session and logout.\n\nThe information screens that you viewed during this session will remain in your browser's memory until the browser is closed.");	  
}
function cheatCloseWinAccessBlue(str,strHostName) { 
	
	//popup();
	
	//if (navigator.appName.indexOf('Microsoft') != -1)
//	{
		popup(str,strHostName);
		//setTimeout('win=top; win.opener = top; win.close();',3000);
	//}
	//else
	//{
	//	alert ("You must close your web browser to completely end your session and logout.\n\nThe information screens that you viewed during this session will remain in your browser's memory until the browser is closed.");
	//}
}

