var agentName;
var iebody;
var objref;
var showonscrollvar;
var relpConnected;
var relpLanguage = 'ns';

if (relpConnected != true)
{
	//create the divs
	document.write('<script language="JavaScript" type="text/javascript" src="http://www.realelivepeople.com/relp_swfobject.js"></script>');
	document.write('<div id=\'ReLPTrafficHook\' name=\'ReLPTrafficHook\' >&nbsp</div>');
	document.write('<div id=\'chatInvitation\' name=\'chatInvitation\' style="z-index: 500">&nbsp</div>');
}
	
//ReLP 2.x
function CenterChatInvitation()
{
	
	var ie=document.all && !window.opera	
	var scroll_top=(ie)? document.body.scrollTop : window.pageYOffset
	
	
	objref.style.top=scroll_top+docheight/2-objheight/2+"px"
	
	
}

//ReLP 2.x
function ShowReLPInvite(guestID, chatRequestID)
{
	//alert('Chat request received!');
	
	
	//check for a preexisting invite
	var existingInvite = document.getElementById("ReLPChatInvitation");
	if (existingInvite != null)
	{
		existingInvite.parentNode.removeChild(existingInvite);
	}
	
	var ReLPChatInvitation = new relp_SWFObject("http://realelivepeople.com/chatInvitation.swf?guestID=" + guestID + "&invitationID=" + chatRequestID + "&ISO639=" + GetISO639() , "ReLPChatInvitation", "400", "380", "7", "#FFFFFF");
	ReLPChatInvitation.addParam("allowScriptAccess", "always");
	ReLPChatInvitation.addParam("quality", "high");
	ReLPChatInvitation.write("chatInvitation");
		
	document.getElementById("chatInvitation").style.visibility="visible";
	

	var ie=document.all && !window.opera
	var dom=document.getElementById
	
	
	iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
	objref=(dom)? document.getElementById("chatInvitation") : document.all.chatInvitation
	docheight=(ie)? iebody.clientHeight: window.innerHeight
	var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset
	var docwidth=(ie)? iebody.clientWidth : window.innerWidth
	
	var objwidth=objref.offsetWidth
	objheight=objref.offsetHeight
	objref.style.left=docwidth/2-objwidth/2+"px"
	objref.style.top=scroll_top+docheight/2-objheight/2+"px"
	
	showonscrollvar=setInterval("CenterChatInvitation()", 50)


}

//Relp 2.x
function HideChatInvite()
{
	
	//ReLP 2.x
	document.getElementById("chatInvitation").innerHTML = "";
	document.getElementById("chatInvitation").style.visibility="hidden";
}

//ReLP 2.x
function AcceptReLPChat(guestID, chatID, failsafeHTML, openChatMessage, closeChatMessage)
{	
	var mywindow = window.open ("http://realelivepeople.com/guest.php?chatID=" + chatID + "&guestID=" + guestID + "&ISO639=" + GetISO639(), 
							"ReLPGuestChat",
							"location=0,status=0,scrollbars=0,width=580,height=505");
							
	if (mywindow == null || mywindow == undefined) //if window was intercepted
	{
		var chatCode = 'window.open("http://realelivepeople.com/guest.php?chatID=' + chatID + '&guestID=' + guestID + '&ISO639=' + GetISO639() + '", "ReLPGuestChat", "location=0,status=0,scrollbars=0,width=580,height=505")';
		ShowRelPFailsafe(failsafeHTML, openChatMessage, closeChatMessage, chatCode);
	}	
	else //if window was allowed
	{	
		HideChatInvite();
	}
}

//Added 18/Feb/2008 to avoid issue with firefox popup blocker
function OpenNewReLPChat(guestID, requestedHost, failsafeHTML, openChatMessage, closeChatMessage)
{		
	var mywindow = window.open("http://realelivepeople.com/guest.php?requestedHost=" + requestedHost + "&guestID=" + guestID + "&ISO639=" + GetISO639() + "&auto=true", 
							"ReLPGuestChat",
							"location=0,status=0,scrollbars=0,width=580,height=505");
	
	if (mywindow == null || mywindow == undefined) //if window was intercepted
	{
		var chatCode = 'window.open("http://realelivepeople.com/guest.php?requestedHost=' + requestedHost + '&guestID=' + guestID + '&ISO639=' + GetISO639() + '&auto=true", "ReLPGuestChat", "location=0,status=0,scrollbars=0,width=580,height=505")';
		ShowRelPFailsafe(failsafeHTML, openChatMessage, closeChatMessage, chatCode);
	}	
	else //if window was allowed
	{	
		HideChatInvite();
	}
}

//Added 8/Apr/2008 to add an HTML failsafe chat link when popup blocker intercepts chat window
function ShowRelPFailsafe(failsafeHTML, openChatMessage, closeChatMessage, chatCode)
{
	//alert('Showing ReLP Failsafe: \n' + failsafeHTML + '\n' + chatCode);
	var chatInvitation = document.getElementById("chatInvitation");
	var failsafeContent;

	
	if (failsafeHTML == null || failsafeHTML == undefined || failsafeHTML == 'undefined' || failsafeHTML == "")
	{
		//use default failsafe content when none is specified
		failsafeContent = '<div style=\'background-color: #cccccc;\'>';		
		failsafeContent += '<h2>Your popup blocker has prevented the chat window from opening automatically. Click the "Open Chat" button below to open the window directly.</h2>';							
	}
	else
	{
		failsafeContent = failsafeHTML;
	}
	
			failsafeContent += '<div>';
				failsafeContent += '<input type=\'button\' value=\'' + openChatMessage + '\' onClick=\'' + chatCode + ';HideChatInvite();\' />';
				failsafeContent += '<input type=\'button\' value=\'' + closeChatMessage + '\' onClick=\'HideChatInvite()\' />';
			failsafeContent += '</div><br />';
		failsafeContent += '</div>';
	
	chatInvitation.innerHTML = failsafeContent;	
	//alert("showed failsafe \n\n" + chatInvitation.innerHTML)
}

//ReLP 2.x
function ConnectReLP(RELSiteName, language)
{
	
	
	relpConnected = true;
	
	if (typeof(language) != 'undefined')
	{
		relpLanguage = language;
		//alert(language);
	}
	
	
	var theBody = document.getElementsByTagName('body')[0];
	
	//***Create ReLP 2.x viewerhook***
	var so2 = new relp_SWFObject("http://64.114.146.250/ReLPTrafficHook.swf?siteName=" + RELSiteName + "&ISO639=" + GetISO639() + "&referrer=" + escape(document.referrer) + "&browserInfo=" + GetReLPBrowserInfo() + "&pageTitle=" + document.title, "ReLPTrafficHook", "1", "1", "8", "#FFFFFF");

	var ReLPTrafficHook = document.getElementById('ReLPTrafficHook');
	ReLPTrafficHook.style.position = "absolute";
	ReLPTrafficHook.style.float = "none";
	ReLPTrafficHook.style.top = "-500px";	
	so2.addParam("allowScriptAccess", "always");
	so2.addParam("quality", "high");
	so2.write("ReLPTrafficHook");
	
	
	//Create Chat Invitation (ReLP 2.x)
	var chatInvitation = document.getElementById('chatInvitation');
	chatInvitation.style.position = "absolute";
	chatInvitation.style.float = "absolute";
	chatInvitation.style.visibility = "hidden";
	chatInvitation.align = "center";
	chatInvitation.style.width = "300px";
	chatInvitation.top = "200px";
	chatInvitation.zindex = "100";

	
	
}

function GetISO639()
{
	if (relpLanguage != 'ns') //specified to relp explicitly
	{
		return relpLanguage;
	}
	else if (typeof(_ISO639) != 'undefined') //defined in the page implicitly
	{
		return _ISO639;
	}
	else //default to english
	{
		return 'eng';
	}
}

function GetReLPBrowserInfo()
{
	var nVer = navigator.appVersion;
	var nAgt = navigator.userAgent;
	var browserName  = '';
	var fullVersion  = 0; 

	
	if ((verOffset=nAgt.indexOf("MSIE"))!=-1) { // In Internet Explorer, the true version is after "MSIE" in userAgent
		browserName  = "Internet Explorer";
		fullVersion  = parseFloat(nAgt.substring(verOffset+5));	
	}
	else if ((verOffset=nAgt.indexOf("Opera"))!=-1) 	// In Opera, the true version is after "Opera" 
	{
		browserName  = "Opera";
		fullVersion  = parseFloat(nAgt.substring(verOffset+6));
	}
	else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) 	// In Firefox, the true version is after "Firefox" 
	{
		browserName  = "Firefox";
		fullVersion  = parseFloat(nAgt.substring(verOffset+8));		
	}
	else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 	// In most other browsers, "name/version" is at the end of userAgent 
	{
		browserName  = nAgt.substring(nameOffset,verOffset);
		fullVersion  = parseFloat(nAgt.substring(verOffset+1));
		if (!isNaN(fullVersion)) 
		{
			fullVersion = parseInt(''+fullVersion);
		}
		else 
		{
			fullVersion  = 0; majorVersion = 0;
		}
	}
	
	// Finally, if no name and/or no version detected from userAgent...
	if (browserName.toLowerCase() == browserName.toUpperCase() || fullVersion==0)
	{
		browserName  = navigator.appName;
		fullVersion  = parseFloat(nVer);		
	}
	
	return browserName + ' ' + fullVersion;
}