/*
-------------------------------------------------------------------
 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="/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="/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. 




*/

//----------------------------------------------------------------
//  Usernames, Passwords & User Pages - These require configuration.
//----------------------------------------------------------------
var homepage = "index.html"
var successpage = "protected.html"; // The page users go to after login, if they have no personal page.
var loginpage = "/register.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("username1","password1","member.html"); // Change these two entries to valid logins.
users[1] = new Array("username2","password2","");          // Add addtional logins, straight after these, as
                                                           // required, followig the same format. Increment the 
											               // numbers in the square brackets, in new each one. Note:
											               // the 3rd parameter is the the page that user goes to
											               // after successful login. Ensure the paths are correct.
                                                           // Make this "" if user has no personal page.
//----------------------------------------------------------------
//  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);
  setCookie("ismember",1);
//  if (top.location.href != location.href){
//   location.href = successpage;           
//  }else{
//   top.location.href = successpage;  
//  }
 }else{
  alert('Registration Required To Access This Content');
 }  
}

function logout() {
 deleteCookie("login");
  deleteCookie("ismember");
  deleteCookie("item");
 if (top.location.href != location.href){
  location.href = homepage;           
 }else{
  top.location.href = homepage;  
 }
}

//----------------------------------------------------------------
// Cookie Handler
//----------------------------------------------------------------
var ckTemp = document.cookie;



//function setCookie(name, value) { 
// if (value != null && value != "")
//  document.cookie=name + "=" + escape(value) + ";";
// ckTemp = document.cookie;
// }
 
 function setCookie(name,value) {
		var date = new Date();
		date.setTime(date.getTime()+(2*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
	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('Registration Required To Access This Content'); 
 					 if(top.location.href != location.href){
  						 location.href = loginpage;           
 					 }
					 else{
   							top.location.href = loginpage;  
 						 }
 				} 
//				deleteCookie("item");
}


//----------------------------------------------------------------
// get the resource name and write to the form field also add field to display resource code in email
//----------------------------------------------------------------
var tcook = getCookie("item");
function ShowCookie() {
	document.write(tcook);
}

	var resource = getCookie("item");
function getResource() {	
if(resource != null){
			document.write('<input type="hidden" name="retURL"value="http://www.formfast.com/resources/'+resource+'/"> <input type="hidden" name="00N80000002zmL2" value='+resource+'>');
			}else{

	document.write('<input type="hidden" name="retURL" value="http://www.formfast.com/resources.html" > <input type="hidden" name="00N80000002zmL2" value='+resource+'>');
			}
}

//----------------------------------------------------------------
// check if registered already
//----------------------------------------------------------------
function checkReg(){
 var target = getCookie("item");
 var logincookie = getCookie("ismember");
 var targeturl = "http://www.formfast.com/resources/" + target + "/";


   if(logincookie==1){
 					 if(top.location.href != location.href){
  						 location.href = targeturl;           
 					 }
					 else{
   							top.location.href = targeturl;  
 						 }
 				}
 		 }

				
function showreg(){
	var regstat = getCookie("ismember");
	document.write('registered v8: ' + regstat);

	}
 
//----------------------------------------------------------------
// form validation
//----------------------------------------------------------------

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=args[i+1]; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) { alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
  } 
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

//----------------------------------------------------------------
// Login Panel
//----------------------------------------------------------------

function BuildPanel() {
document.write('<form name="logon"><table align="left" border="0"><tr><td align="right">');
document.write('<small><font face="Verdana">Username:</font></small></td>');
document.write('<td><small><font face="Verdana"><input type="text" name="username" size="20"></font></small></td></tr>');
document.write('<tr><td align="right"><small><font face="Verdana">Password:</font></small></td>');
document.write('<td><small><font face="Verdana"><input type="password" name="password" size="20"></font></small></td></tr>');
if(imgSubmit == ""){
 document.write('<tr><td align="center" colspan="2"><p><input type="button" value="Logon" name="Logon" onclick="login(username.value,password.value)">'); 
} else {
 document.write('<tr><td align="center" colspan="2"><p><input type="image" src="'+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="'+imgReset+'" name="Reset" onclick="logon.reset();">');
}
document.write('</p></td></tr></table></form>');
}

//----------------------------------------------------------------
// show the cookie contents
//----------------------------------------------------------------

