/*
 *	It is of critical importance that this file is referenced inside the <body> tag of every page 
 * 	on your site.  For example: 
 *
 *	<body>
 *	<script src="/userplane/functions.js" type="text/javascript" language="javascript"></script>
 *	
 *	!!! WE HIGHLY RECOMMEND YOU LEAVE up_bDoPresence_CoachingSession TO true AND TO IMPLEMENT THE CODE IN ALL aspx 
 *	PAGES IN THIS FOLDER !!!
 *
 *	Set up_bDoPresence_CoachingSession to false if you have your own presence detection and will check for pending WM
 *	windows and call up_launchWM on your own.  If so, you will need to call up_launchWM for every 
 *	pending WM window every time the user changes pages.  Also, you will need to implement the logic 
 *	in the wmWindowOpened.aspx page to let you know when requests have been denied and also when windows 
 *	have successfully popped up
 */
var up_bDoPresence_CoachingSession = true; 
/* 
 *	If you have up_bDoPresence_CoachingSession set to true, this is how often (in seconds) we will connect to your 
 *	server via wmLauncher.aspx or win_ie_pd.aspx
 */
var up_iCheckSeconds_CoachingSession = 10; 

// PLEASE DO NOT CHANGE ANY CODE BELOW THIS LINE


function up_launchCoachingSession( userID, destinationUserID, destinationName, Modality,StartSessionPendingRequestID )
{
	up_localUserID_CoachingSession = userID;
	
	var popupWindowTest = null;
	
	//popupWindowTest = window.open( "/userplane/wm.aspx?strDestinationUserID=" + destinationUserID, "WMWindow_" + up_replaceAlpha(userID) + "_" + up_replaceAlpha(destinationUserID), "width=360,height=397,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1" );
	//popupWindowTest = window.open( "/userplane/wm.aspx?strDestinationUserID=" + destinationUserID, "WMWindow_" + up_replaceAlpha(userID) + "_" + up_replaceAlpha(destinationUserID), "width=360,height=397,toolbar=0,directories=0,menubar=0,dependent=1,status=0,location=0,scrollbars=0,resizable=1" );
	popupWindowTest = window.open( "/pop_client_session.aspx?StartSessionPendingRequestID=" + StartSessionPendingRequestID + "&Modality=" + Modality  + "&strDestinationUserID=" + destinationUserID, "WMWindow_" + userID + "_" + destinationUserID, "width=435,height=360,toolbar=0,directories=0,menubar=0,dependent=1,status=0,location=0,scrollbars=0,resizable=0" );

	if( popupWindowTest == null )
	{
		up_showWMNotify_CoachingSession(userID, destinationUserID, destinationName,Modality,StartSessionPendingRequestID );
	}
	else
	{
		up_clearWMNotify_CoachingSession(destinationUserID, false ); 
	}
	
	return false;
}

function up_showWMNotify_CoachingSession( userID, destinationUserID, destinationName,Modality , StartSessionPendingRequestID )
{
	up_localUserID_CoachingSession = userID;
	
	var bAdd = true;
	for( var i = 0 ; i < up_launchArray_CoachingSession.length ; i++ )
	{
		if( up_launchArray_CoachingSession[i].destID == destinationUserID )
		{
			if( destinationName != undefined )
			{
				up_launchArray_CoachingSession[i].destName = "<strong>" + destinationName + "</strong>";
			}
			bAdd = false;
		}
	}
	
	destinationName = destinationName == undefined ? "A website member" : "<strong>" + destinationName + "</strong>";
	
	if( bAdd )
	{
		var userObj = new Object();
		userObj.destID = destinationUserID;
		userObj.destName = destinationName;
		userObj.Modality = Modality;
		userObj.StartSessionPendingRequestID = StartSessionPendingRequestID
		up_launchArray_CoachingSession.push( userObj );
	}
	
	up_showNotificationCoachingSession();
}
function up_clearWMNotify_CoachingSession( clearDestID, bForceClear )
{
	var tempArray = up_launchArray_CoachingSession;
	up_launchArray_CoachingSession = new Array();
	
	var bRemoved = false;
	
	while( tempArray.length > 0 )
	{
		var userObj = tempArray.pop();
		
		if( userObj.destID != clearDestID )
		{
			up_launchArray_CoachingSession.push( userObj );
		}
		else
		{
			bRemoved = true;
		}
	}
	
	if( bRemoved || !bForceClear )
	{
		if( bForceClear )
		{
			//frames['up_wmReject_CoachingSession'].location.href = "pop_client_session.aspx?strDestinationUserID=" + clearDestID + "&refresh=" + Math.floor( Math.random() * 100000000000 );
		}
		//frames['up_wmLauncher_CoachingSession'].location.href = "pop_client_session.aspx?destinationUserID=" + clearDestID + "&doPresence=" + ( up_bDoPresence && !up_is_win_CoachingSession_ie ? "true" : "false" ) + "&forceClear=" + ( bForceClear ? "true" : "false" ) + "&iRefreshInterval=" + up_iCheckSeconds + "&refresh=" + Math.floor( Math.random() * 100000000000 );
	}
	
	up_showNotificationCoachingSession();
}
function up_showNotificationCoachingSession()
{
	var elem = document.getElementById( "up_wmNotificationsStartSession" );
	//elem.innerHTML = '<div class="up_mbox"><div class="up_mbox1"><div class="up_mbox2"></div></div><div class="up_mboxgut" style="text-align:center">' + ( up_is_win_CoachingSession_ie ? '' : '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center">' ) + '<table border="0" cellpadding="2" cellspacing="5"><tr><td nowrap align="center"><strong style="font-size:larger;">Incoming IM Message</strong></td></tr><tr><td align="center">' + up_launchArray_CoachingSession[0].destName + ' wants to IM you.<br>Would you like to accept?</td></tr><tr><td nowrap align="center"><a style="font-size:larger;" href="" onClick="javascript: up_launchCoachingSession( \'' + up_localUserID_CoachingSession + '\', \'' + up_launchArray_CoachingSession[0].destName + '\' ); return false;">Yes</a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a style="font-size:larger;" href="" onClick="javascript: up_clearWMNotify_CoachingSession( \'' + up_launchArray_CoachingSession[0].destName + '\', true ); return false;">No</a></td></tr></table>' + ( up_is_win_CoachingSession_ie ? '' : '</td></tr></table>' ) + '</div><div class="up_mbox3"><div class="up_mbox4"></div></div></div>';
	//up_displayedNotificationID_CoachingSession = up_launchArray_CoachingSession[0].destID;
	//up_animateCoachingSession( 30 );
	
	if( up_launchArray_CoachingSession.length > 0 )
	{
		if( up_displayedNotificationID_CoachingSession != up_launchArray_CoachingSession[0].destID )
		{
			//elem.innerHTML = '<div class="up_mbox"><div class="up_mbox1"><div class="up_mbox2"></div></div><div class="up_mboxgut" style="text-align:center">' + ( up_is_win_CoachingSession_ie ? '' : '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center">' ) + '<table border="0" cellpadding="2" cellspacing="5"><tr><td nowrap align="center"><strong style="font-size:larger;">Incoming Popup Message</strong></td></tr><tr><td align="center">' + up_launchArray_CoachingSession[0].destName + ' wants to start coaching with you.<br> Please turn your popup blocker off to proceed.</td></tr><tr><td nowrap align="center"><a style="font-size:larger;" href="" onClick="javascript: up_launchCoachingSession( \'' + up_localUserID_CoachingSession + '\', \'' + up_launchArray_CoachingSession[0].destID + '\',\'\',up_launchArray_CoachingSession[0].Modality  ); return false;">Yes</a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a style="font-size:larger;" href="" onClick="javascript: up_clearWMNotify_CoachingSession( \'' + up_launchArray_CoachingSession[0].destID + '\', true ); return false;">No</a></td></tr></table>' + ( up_is_win_CoachingSession_ie ? '' : '</td></tr></table>' ) + '</div><div class="up_mbox3"><div class="up_mbox4"></div></div></div>';
			elem.innerHTML = '<div class="up_mbox"><div class="up_mbox1"><div class="up_mbox2"></div></div><div class="up_mboxgut" style="text-align:center">' + ( up_is_win_CoachingSession_ie ? '' : '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center">' ) + '<table border="0" cellpadding="2" cellspacing="5"><tr><td nowrap align="center"><strong style="font-size:larger;">Incoming Popup Message</strong></td></tr><tr><td align="center">' + up_launchArray_CoachingSession[0].destName + ' wants to start a coaching session with you.<br> Please turn your popup blocker off to proceed.</td></tr><tr><td nowrap align="center"><a style="font-size:larger;" href="" onClick="javascript: up_clearWMNotify_CoachingSession( \'' + up_launchArray_CoachingSession[0].destID + '\', true ); return false;">Close</a></td></tr></table>' + ( up_is_win_CoachingSession_ie ? '' : '</td></tr></table>' ) + '</div><div class="up_mbox3"><div class="up_mbox4"></div></div></div>';
			up_displayedNotificationID_CoachingSession = up_launchArray_CoachingSession[0].destID;
			up_animateCoachingSession( 30 );
		}
	}
	else
	{
		up_displayedNotificationID_CoachingSession = "";
		up_animateCoachingSession( -200 );
	}

}


function up_animateCoachingSession( iWhereTo )
{
	var elem = document.getElementById( "up_wmNotificationsStartSession" );
	if( up_iDivCurrentY_CoachingSession != iWhereTo || up_launchArray_CoachingSession.length > 0 )
	{
		if( up_iDivCurrentY_CoachingSession != iWhereTo )
		{
			up_iDivCurrentY_CoachingSession += iWhereTo < up_iDivCurrentY_CoachingSession ? -10 : 10;
		}
		
		var px = up_iDivCurrentY_CoachingSession + document.body.scrollTop + 'px';
		elem.style.top = px;
		
		clearTimeout( up_animateCoachingSession_timeoutID_CoachingSession );
		up_animateCoachingSession_timeoutID_CoachingSession = setTimeout( "up_animateCoachingSession(" + iWhereTo + ")", 33 );
	}
	else
	{
		elem.style.top = iWhereTo;
	}
}

function up_replaceAlpha( strIn )
{
	var strOut = "";
	for( var i = 0 ; i < strIn.length ; i++ )
	{
		var cChar = strIn.charAt(i);
		if( ( cChar >= 'A' && cChar <= 'Z' )
			|| ( cChar >= 'a' && cChar <= 'z' )
			|| ( cChar >= '0' && cChar <= '9' ) )
		{
			strOut += cChar;
		}
		else
		{
			strOut += "_";
		}
	}
	
	return strOut;
}



document.write( '<style type="text/css">' );
document.write( '.up_mbox { background: #ffffff url(/userplane/images/fbox.gif) repeat-y top right; border: 0; margin: 2px 0 6px 0; padding: 0; }' );
document.write( '.up_mbox1 { background: url(/userplane/images/fbox1.gif) no-repeat top right; height: 8px; font: normal 0px/0px Sans-serif; margin: 0; padding: 0; }' );
document.write( '.up_mbox2 { display: block; background: url(/userplane/images/fbox2.gif) no-repeat top left; height: 8px; width: 8px; font: normal 0px/0px Sans-serif; margin: 0; padding: 0; }' );
document.write( '.up_mbox3 { background: url(/userplane/images/fbox3.gif) no-repeat bottom right; height: 8px; font: normal 0px/0px Sans-serif; margin: 0; padding: 0; }' );
document.write( '.up_mbox4 { display: block; background: url(/userplane/images/fbox4.gif) no-repeat bottom left; height: 8px; width: 8px; font: normal 0px/0px Sans-serif; margin: 0; padding: 0; }' );
document.write( '.up_mboxgut { background: url(/userplane/images/fboxgut.gif) repeat-y top left; font: 11px/18px Verdana, Geneva, Arial, Helvetica, Helve, Sans-serif; margin: 0; padding: 0 10px 0 10px; word-wrap: break-word; }' );
document.write( '</style>' );
document.write( '<iframe name="up_wmLauncher_CoachingSession" id="up_wmLauncher_CoachingSession" style="position:absolute; top: -200px; z-index:9998; width:100px; height:100px; border: 0px" src=""></iframe>' );
document.write( '<iframe name="up_wmReject_CoachingSession" id="up_wmReject_CoachingSession" style="position:absolute; top: -200px; z-index:9999; width:100px; height:100px; border: 0px" src=""></iframe>' );
document.write( '<div id="up_wmNotificationsStartSession" style="position:absolute; width:350px; z-index:9997; left: 30px; top: -200px;"></div>' );
var up_iDivCurrentY_CoachingSession = -200;

var up_launchArray_CoachingSession = new Array();
var up_localUserID_CoachingSession = "";
var up_displayedNotificationID_CoachingSession = "";

var up_wmCheckImage_CoachingSession = null;
var up_checkWM_timeoutID_CoachingSession = null;
var up_onImageLoad_timeoutID_CoachingSession = null;
var up_animateCoachingSession_timeoutID_CoachingSession = null;

// determine if is Windows IE (up_is_win_CoachingSession_ie)
var up_agt_CoachingSession 			= navigator.userAgent.toLowerCase();
var up_appVer_CoachingSession 		= navigator.appVersion.toLowerCase();
var up_is_mac_CoachingSession 		= up_agt_CoachingSession.indexOf('mac') != -1;
var up_is_safari_CoachingSession 	= up_agt_CoachingSession.indexOf('safari') != -1 && up_is_mac_CoachingSession;
var up_is_khtml_CoachingSession  	= up_is_safari_CoachingSession || up_agt_CoachingSession.indexOf('konqueror') != -1;
var up_is_ie_CoachingSession  	 	= up_appVer_CoachingSession.indexOf('msie') != -1 && up_agt_CoachingSession.indexOf("opera") == -1 && !up_is_khtml_CoachingSession;
var up_is_win_CoachingSession   	= up_is_mac_CoachingSession ? false : (up_agt_CoachingSession.indexOf("win") != -1 || up_agt_CoachingSession.indexOf("16bit") != -1);
var up_is_win_CoachingSession_ie 	= up_is_win_CoachingSession && up_is_ie_CoachingSession; 


