// Start members sign-in -->

var successpage = "members_section.html"; // The page users go to after login, if they have no personal page.
var loginpage = "login.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("Griffco","password","client_page-griffco.php");        // Change these two entries to valid logins.
users[1] = new Array("LuxVFX","escamo","client_page-luxvfx.php");


// 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);
  if (top.location.href != location.href){
   location.href = successpage;           
  }else{
   top.location.href = successpage;  
  }
 }else{
  alert('access denied');
 }  
}

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('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"><table align="left" border="0"><tr><td align="right" width="200">');
document.write('<b>Username:</b></td>');
document.write('<td><small><font face="Verdana"><input type="text" name="username" size="20" class="memorize"></font></small></td></tr>');
document.write('<tr><td align="right"><b>Password:</b></td>');
document.write('<td><input type="password" name="password" size="20" class="memorize"></td></tr>');
if(imgSubmit == ""){
 document.write('<tr><td align="center" colspan="2"><p align="right"><input type="button" value="Sign In" 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('');
} else {
 document.write('<input type="image" src="'+imgReset+'" name="Reset" onclick="logon.reset();">');
}
document.write('</p></td></tr></table></form>');
} 

// End members sign-in -->



//Time in days to save form fields values after last visit
//Set to different value to reset cookie (ie: "101 days" instead of "100 days"):
var memoryduration="100 days"

function setformobjects(){
var theforms=document.forms
memorizearray=new Array()
for (i=0; i< theforms.length; i++){
for (j=0; j< theforms[i].elements.length; j++){
if (theforms[i].elements[j].className.indexOf("memorize")!=-1 && theforms[i].elements[j].type=="text")
memorizearray[memorizearray.length]=theforms[i].elements[j]
}
}
var retrievedvalues=get_cookie("mvalue"+window.location.pathname)
if (retrievedvalues!=""){
retrievedvalues=retrievedvalues.split("|")
if (retrievedvalues[retrievedvalues.length-1]!=parseInt(memoryduration)) //reset cookie if var memoryduration has changed
resetcookie("mvalue"+window.location.pathname)
else{
for (i=0; i<memorizearray.length; i++){
if (retrievedvalues[i]!="empty_value")
memorizearray[i].value=retrievedvalues[i]
}
}
}
}

function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if cookie exists
      offset += search.length
      end = document.cookie.indexOf(";", offset);
      if (end == -1)
         end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function resetcookie(id){
var expireDate = new Date()
expireDate.setDate(expireDate.getDate()-10)
document.cookie = id+"=;path=/;expires=" + expireDate.toGMTString()
}

function saveformvalues(){
var formvalues=new Array(), temp
for (i=0; i<memorizearray.length; i++){
temp=memorizearray[i].value!=""? memorizearray[i].value : "empty_value"
formvalues[formvalues.length]=escape(temp)
}
formvalues[formvalues.length]=parseInt(memoryduration)
formvalues=formvalues.join("|")
var expireDate = new Date()
expireDate.setDate(expireDate.getDate()+parseInt(memoryduration))
document.cookie = "mvalue"+window.location.pathname+"="+formvalues+"; path=/;expires=" + expireDate.toGMTString()
}

if (window.addEventListener)
window.addEventListener("load", setformobjects, false)
else if (window.attachEvent)
window.attachEvent("onload", setformobjects)
else if (document.getElementById)
window.onload=setformobjects
if (document.getElementById)
window.onunload=saveformvalues
