
/*
-------------------------------------------------------------------
 Login Script Version 1.9
 By H G Laughland 
 http://www.laughland.biz

This script can be used free of charge and may only be
redistributed the same way. The disclaimer must remain
intact.

DISCLAIMER: Use this script with caution. This script is not 
the most secure method available, for protecting material. Its
main purpose is to demonstrate the javascript techniques used.
The author takes no responsibility for data loss
resulting from the use of this script.
-------------------------------------------------------------------

INSTRUCTIONS - Read Before Using Script

If you are using frames, the code referred to in steps 2 - 5 must be put in the 
pages displayed in the frames and NOT in the parent document.

Step 1
In the Usernames & Passwords section, configure the variables as
indicated by the comments.
 
Step 2:
Add the following code to the <head> section of your login page: 
 <script src="../test-login/scripts/test/scripts/login.js"></script> 
Change "scripts/login.js" to reflect the correct path to this script
file on your server. 
 
Step 3:
Add this code to the login page, at the place you want the login
panel to show:
 
 <script language="JavaScript">
  BuildPanel();
 </script>
 
Step 4:
Add the following code to the <head> section of each page procteded
by this script:
 
 <script src="../test-login/scripts/test/scripts/login.js"></script>
 <script language="JavaScript">
  checkCookie();
 </script>

Change "scripts/login.js" to reflect the correct path to the script
file on your server.
 
Step 5: 
On the page that is to have the logout button, paste this code where you
want the button to be:

 <form action="" name="frmLogoff">
  <input type="button" name="btLogoff" value="log out" onclick="logout();">
 </form>
 
 To use your own image instead of the grey button change the type from button to image
 and add src="myimage.gif" where myimage.gif is the image (including the path to it if
 needed, you want to use.
 
Step 6:
Upload this script and your html pages to the relevant directories
on your server. 

*/



var rootfolder2 = sPath.substring(sPath.indexOf("/") +1);
var rootfolder3 = rootfolder2.substring(rootfolder2.indexOf("/") );

if (rootfolder == "")
{
 rootfolder = rootfolder2.replace(rootfolder3, "");
}

else
{
	var rootfolder = rootfolder;
}

if (cookiename =="")
{
	cookiename = rootfolder;
}
else
{
	cookiename = cookiename;
}


var rootfolderpath =  sPath.substring(sPath.indexOf(rootfolder)); //leave as is
var sPagelogin1 = rootfolderpath.replace(rootfolder, ""); //leave as is
var sPagelogin = sPagelogin1.replace("/", ""); //leave as is


var loginpagepath = sPath.replace(sPagelogin, ""); //use this in loginpage variable if you need to use absolute path

	
var loginpage = loginpagepath + "login.html?" + sPagelogin;//Change this to the page the login panel is on.



var imgSubmit = ""; //Change to the path to your login image,if you don't want the standard button, otherwise do not change.
var imgReset = "";  //Change to the path to your reset image,if you don't want the standard button, otherwise do not change.

//insert image. Image name must be the same as successpage

var loginimage = successpage.replace("/index.html", "");

if(loginimage == "index.html")
{
	loginimage = rootfolder;
}
else
{
	loginimage = loginimage
}

//insert page title
var pagetitle = loginimage.replace(/-/gi, " ");
pagetitle = pagetitle.replace(/_/gi, " ");
 

var slashposition = pagetitle.indexOf('/') ;
var trimpagetitle = pagetitle.substring(pagetitle.indexOf('/') );

if (slashposition !== -1)
{

 var pagetitle = pagetitle.replace(trimpagetitle, "")
};

//change page title to tile case
String.prototype.titleCase = function () {
	var str = "";
	var wrds = this.split(" ");
	for(keyvar in wrds)
	{
	str += ' ' + wrds[keyvar].substr(0,1).toUpperCase()
	 + wrds[keyvar].substr(1,wrds[keyvar].length);
	}
   return str;
}
var pagetitle = pagetitle.titleCase();

//exceptions for page titles

if (loginimage == "globalcasino") {pagetitle = "The Global Casino"}
else if (loginimage == "worldeconomy") {pagetitle = "Geography of the World Economy, 5th edition"}
else if (loginimage == "letstalkgerman") {pagetitle = "Let's Talk German"}
else if (loginimage == "pas-de-probleme") {pagetitle = "Pas de Problème"}

else {pagetitle = pagetitle};	



//----------------------------------------------------------------
//  Login Functions
//----------------------------------------------------------------




function login(username,password){
 var member = null;
 var loggedin = 0;
 var members = users.length;
 for(x=0;x<members && !loggedin; x++){
 if((username==users[x][0])&&(password==users[x][1])){
    loggedin = 1;
    member = x;
	break; // User validated, terminate the for loop.
   }
 } 
 
 if(loggedin==1){
  if(users[member][2] != "") {
   successpage = users[member][2];
  }
  setCookie(cookiename,1, '', '/', '', '' );
   setCookie(cookiename2,1, '', '/', '', '' );
  if (top.location.href != location.href){
   location.href = successpage;           
  }else{
   top.location.href = successpage;  
  }
 }else{
  alert('Your user name and password are invalid, please check and try again');
 }  
}




//new logout so you can set a different logout destination to the login page if you wish

function logout() {
 deleteCookie(cookiename, '/'); 
 deleteCookie(cookiename2, '/'); 
 
 if (top.location.href != location.href && logout == ""){


  location.href = loginpage;           
 }
 
 else if (top.location.href != location.href && logout !== "") 
 {
  location.href = logoutpage;       
 }
 
 else{
if(logoutpage == ""){	 
	 
  top.location.href = loginpage;  }
  else
  {
	  top.location.href = logoutpage;
  }  
 } 
}


/*old logout

function logout() {
deleteCookie("login");
if (top.location.href != location.href){
 location.href = loginpage;          
}else{
 top.location.href = loginpage;  
}
}
*/

//----------------------------------------------------------------
// Cookie Handler
//----------------------------------------------------------------
var ckTemp = document.cookie;

function setCookie(name, value, expires, path, domain, secure) { 
 if (value != null && value != "")
  document.cookie=name + "=" + escape(value) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );

 ckTemp = document.cookie;
 }
 
function deleteCookie(name, path) {
  if (getCookie(name)) {
    document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "" ) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function getCookie(name) { 
 var index = ckTemp.indexOf(name + "=");
 if(index == -1) return null;
  index = ckTemp.indexOf("=", index) + 1;
 var endstr = ckTemp.indexOf(";", index);
 if (endstr == -1) endstr = ckTemp.length;
 return unescape(ckTemp.substring(index, endstr));
 }
  
function checkCookie() {
 var temp = getCookie(cookiename);
 if(!temp==1) {
//  alert('access denied'); 
  if(top.location.href != location.href){
   location.href = loginpage;           
  }else{
   top.location.href = loginpage;  
  }
 }
}


function checkCookie2() {
 var temp = getCookie(cookiename2);
 if(!temp==1) {
//  alert('access denied'); 
  if(top.location.href != location.href){
   location.href = loginpage;           
  }else{
   top.location.href = loginpage;  
  }
 } 
}

//----------------------------------------------------------------
// Login Panel
//----------------------------------------------------------------

function BuildPanel() {
document.write('<form name="logon" class="loginform"><table align="left" border="0" cellspacing="5"><tr><td align="right">');
document.write('Username:</td>');
document.write('<td><input type="text" name="username" size="30"></td></tr>');
document.write('<tr><td align="right">Password:</td>');
document.write('<td><input type="password" name="password" size="30" onkeydown="javascript:if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {login(username.value,password.value)}};"></td></tr>');

if(imgSubmit == ""){
document.write('<tr><td></td><td><p><input type="button" class="loginbutton" value="Log in" name="Logon" onclick="login(username.value,password.value)">');
}

else {
document.write('<tr><td align="center" colspan="2"><p><input type="image" src="../test-login/scripts/test/'+imgSubmit+'" name="Logon" onclick="login(username.value,password.value)">');
}

/*else {
document.write('<tr><td></td><td><p><input type="image" src="../test-login/scripts/test/'+imgSubmit+'" name="Logon" onclick="login(username.value,password.value)">');
}
if(imgReset == ""){
document.write('<input type="reset" value="Reset" name="Reset">');
} else {
document.write('<input type="image" src="../test-login/scripts/test/'+imgReset+'" name="Reset" onclick="logon.reset();">');
}
*/
document.write('</p></td></tr></table></form>');
}



