/*
-------------------------------------------------------------------
 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.

Additions and minor changes were made to improve this script by Steven Dolen (SBD).
(SBD referenced throughout for copyright purposes to protect H G Laughland.)
Last update 10/10/2005.

-------------------------------------------------------------------

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="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="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.

Step 7 (optional):
Additional step added by SBD.
Use CSS in to define the look of the form. The classes are:
form.loginscript
font.loginscript_username, font.loginscript_password, input.loginscript_username, input.loginscript_password
input.loginscript_login, input.loginscript_reset

For example, you could place this into the HTML header:

<style type="text/css">
<!--
form.loginscript { margin: 0px }
font.loginscript_username { color: #FFFFFF }
font.loginscript_password { color: #FFFFFF }
input.loginscript_username { height: 18px; width: 105px; }
input.loginscript_password { height: 18px; width: 105px; }
input.loginscript_login { height: 20px; font-size: 8pt; }
input.loginscript_reset { height: 20px; font-size: 8pt; }
-->
</style>

This would make the text white, set a specific size for the fields, and make the
input buttons smaller than they are by default.


*/

//----------------------------------------------------------------
//  Usernames, Passwords & User Pages - These require configuration.
//----------------------------------------------------------------
var successpage = "price/pricing.html"; // The page users go to after login, if they have no personal page.
var loginpage = "/pricelogin.html"; //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.

var users = new Array();

users[0] = new Array("Gotcha!","Photography",""); // Change these two entries to valid logins.
users[1] = new Array("Gotcha","Photography","");
users[2] = new Array("gotcha!","photography","");
users[3] = new Array("gotcha","photography","");          // Add addtional logins, straight after these, as

users[4] = new Array("Gotcha","gotcha99","");
users[5] = new Array("Gotcha","Gotcha99","");// required, followig the same format. Increment the
					             // numbers in the square brackets, in new each one. Note:
					             // the 3rd parameter is the page that user goes to
                          		             // after successful login. Ensure the paths are correct.
                                                     // Make this "" if user has no personal page.

var text_logon = "Login";                            // This is what the Login button will read. Addition by SBD.
var text_reset = "Reset";                            // This is what the Reset button will read. Addition by SBD.

//----------------------------------------------------------------
//  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("login",1);
  if (top.location.href != location.href){
   location.href = successpage;
  }else{
   top.location.href = successpage;
  }
 }else{
  alert('Invalid username or password.');
  // Alert changed from 'Please Login' to something more appropriate by SBD.
 }
}

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) {
 if (value != null && value != "")
  document.cookie=name + "=" + escape(value) + ";";
 ckTemp = document.cookie;
 }

function deleteCookie(name) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    "; 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("login");
 if(!temp==1) {
  alert('Please Login');
  if(top.location.href != location.href){
   location.href = loginpage;
  }else{
   top.location.href = loginpage;
  }
 }
}

//----------------------------------------------------------------
// Login Panel
//----------------------------------------------------------------

function BuildPanel() {
document.write('<form name="logon" class=loginscript><table align="left" border="0">');
//document.write('<tr><td align="right"><small><font face="Verdana" class=loginscript_username>Username:</font></small></td>');
//document.write('<td><small><font face="Verdana"><input type="text" name="username" size="20" class=loginscript_username></font></small></td></tr>');


document.write('<tr><td align="right"><small><font face="Verdana" class=loginscript_username></font></small></td>');
document.write('<td><small><font face="Verdana"><input type="hidden" name="username" size="20" value="Gotcha" class=loginscript_username></font></small></td></tr>');




document.write('<tr><td align="right"><small><font face="Verdana" class=loginscript_password>Password:</font></small></td>');
document.write('<td><small><font face="Verdana"><input type="password" name="password" size="20" class=loginscript_password></font></small></td></tr>');
if(imgSubmit == ""){
 document.write('<tr><td align="center" colspan="2"><p><input type="button" value="' + text_logon + '" name="Logon" onclick="login(username.value,password.value)" class=loginscript_login>');
} else {
 document.write('<tr><td align="center" colspan="2"><p><input type="image" src="'+imgSubmit+'" name="Logon" onclick="login(username.value,password.value)" class=loginscript_login>');
}
if(imgReset == ""){
 document.write('<input type="reset" value="' + text_reset + '" name="Reset" class=loginscript_reset>');
} else {
 document.write('<input type="image" src="'+imgReset+'" name="Reset" onclick="logon.reset();" class=loginscript_reset>');
}
document.write('</p></td></tr></table></form>');
}

