//Summary: This script prevents multiple sessions from being created by first assigning a unique name to the window based on page name and date/time combination.
//The window name is then stored within a cookie that is set to expire every 5 seconds, but is written every 3 seconds as long as the page is still open.

//Arguements: pageName-the name of page that is calling the script(i.e. default.asp)

function BlockMultipleSessions(pageName)
{
//1. Get the cookie with window name
 var currentWindowName;
 currentWindowName = getCookie(pageName);

//2. check to see if cookie exist, if it doesn't create the unique window name.
 if(window.name == '' && !currentWindowName)
 { 
    var d;
    d = new Date();
    
    window.name = pageName + d.getDay() + '-' + d.getMonth() + '-' + d.getYear() + '-' + d.getHours() + '-' + d.getMinutes() + '-'+ d.getSeconds() ;
 }
 
 //3. if the cookie exists but has a different value then block session
 
 if(window.name == ''&& currentWindowName != '')
  {

  location.href=('multipleSessions.asp');
  }
 //4. If cookie does not exist then create it.
  createSessionTrackCookie(window.name,pageName);
}



function createSessionTrackCookie(theSessionID,pageName)
{
//This fucntion creates the cookie and sets it to expire in 5 seconds then recreates the cookie every 3 seconds
var sid;
sid = theSessionID;

var cookieName;
cookieName = pageName;

var CookieExpirationTime; //<---Seconds the cookie will stay alive. Change this value if you want the cookie to expire sooner or later.
CookieExpirationTime = 5;

var d;
d = new Date();
d.setSeconds(d.getSeconds() + CookieExpirationTime);

document.cookie = cookieName + ' = ' + sid + '; expires = ' + d + 'path=/';

setTimeout('createSessionTrackCookie((\"'+sid+'\"),(\"'+cookieName+'\"))',3000);


}



function getCookie(name)
{//This function gets the cookie
	var cookies = document.cookie;
	if (cookies.indexOf(name) != -1)
	{
		var startpos = cookies.indexOf(name)+name.length+1;
		var endpos = cookies.indexOf(";",startpos);
		if (endpos == -2) endpos = cookies.length;
		return unescape(cookies.substring(startpos,endpos));
	}
	else
	{
		return false;
	}
}



function CheckForCookies()
{
//This function checks to see if cookies are enabled.
if(navigator.cookieEnabled == false)
{

location.href=('cookiesNotEnabled.asp');
}

}