/*


INSTRUCTIONS 

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. 


*/

//----------------------------------------------------------------
//  Usernames, Passwords & User Pages - These require configuration.
//----------------------------------------------------------------
var successpage = "eI_Partner_Demo.html"; // The page users go to after login, if they have no personal page.
var loginpage = "index.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("jas","1234","eI_Partner_Demo.html"); // Change these two entries to valid logins.
users[1] = new Array("ss","1234","eI_Partner_Demo.html");          // Add addtional logins, straight after these, as
users[2] = new Array("wv","123","eI_Partner_Demo_edu.html");
users[3] = new Array("schools","123","eI_Partner_Demo_edu.html");                            // required, followig the same format. Increment the 
users[4] = new Array("itgtrial","123","CA_form.php");             // numbers in the square brackets, in new each one. Note:
users[5] = new Array("depdemo","123","eI_Partner_dep.html");       // the 3rd parameter is the the page that user goes to
users[6] = new Array("wvdemo","123","CA_form_edu.php");
users[7] = new Array("gotocalc","123","eI Calculators.htm");
users[8] = new Array("PAdemo","123","eI_Partner_Demo_PA.html");
users[9] = new Array("wvtest","123","eI_Partner_itg.html");
users[10] = new Array("online","123","registration.php");
users[11] = new Array("itgwv","123","eI_Partner_itgwv.html");

  											               
//----------------------------------------------------------------
//  Login Functions
//----------------------------------------------------------------
function login(username,password){
 if(username.substr(0,2)=="eI"){
	 //var enusername = btoa(username);
	 //var enpassword = btoa(password);
	 var up = "?uid="+username+"&pwd="+password;
	 var uri = "logins.php"+up;
 	location.href = encodeURI(uri);
	//exit();
 }
else if(username.toLowerCase() == "eiadmin"){
	up = "login4admin.php?uname="+username+"&upass="+password;
	location.href = encodeURI(up);
}
 else{
 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('Sorry, access denied - please contact enviroIndex if you would like to know more');
 }  
 }
}

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('Sorry, access denied - please contact enviroIndex if you would like to know more'); 
  if(top.location.href != location.href){
   location.href = loginpage;           
  }else{
   top.location.href = loginpage;  
  }
 }
}

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


function BuildPanel() {
document.write('<form name="logon" action="login.php"><table align="left" border="0"><tr><td align="left">');
document.write('<medium><font face="verdana"><font color="#000000">User ID:</font></medium></td>');
document.write('<td><medium><font face="verdana"><input type="text" name="username" size="12"></font></medium></td></tr>');
document.write('<tr><td align="left"><medium><font face="arial"><font color="#000000">Password:</font></medium></td>');
document.write('<td><medium><font face="arial"><input type="password" name="password" size="12"></font></medium></td></tr>');
if(imgSubmit == ""){
 document.write('<tr><td align="center"></td>');	
} else {
 document.write('<tr><td align="center"><input type="image" src="'+imgReset+'" name="Reset" onclick="logon.reset();"></td>');
}
if(imgReset == ""){
  document.write('<td align="center"><input class="button" type="button" value="Log in" name="Logon" onclick="login(username.value,password.value)"></td>'); 
} else {

  document.write('<td align="center"><input type="image" src="'+imgSubmit+'" name="Logon" onclick="login(username.value,password.value)"></td>');
}
document.write('</tr></table></form>');
}


