function permutationGenerator(nNumElements) {
   this.nNumElements = nNumElements;
   this.antranspositions = new Array;
   var k = 0;
   for (i = 0; i < nNumElements - 1; i++)
   for (j = i + 1; j < nNumElements; j++)
   this.antranspositions[ k++ ] = ( i << 8 ) | j;
   // keep two positions as lo and hi byte!
   this.nNumtranspositions = k;
   this.fromCycle = permutationGenerator_fromCycle;
}
function permutationGenerator_fromCycle(anCycle) {
   var anpermutation = new Array(this.nNumElements);
   for (var i = 0; i < this.nNumElements; i++) anpermutation[i] = i;
   for (var i = 0; i < anCycle.length; i++) {
      var nT = this.antranspositions[anCycle[i]];
      var n1 = nT & 255;
      var n2 = (nT >> 8) & 255;
      nT = anpermutation[n1];
      anpermutation[n1] = anpermutation[n2];
      anpermutation[n2] = nT;
   }
   return anpermutation;
}
function password(strpasswd) {
   this.strpasswd = strpasswd;
   this.getHashValue = password_getHashValue;
   this.getpermutation = password_getpermutation;
}
function password_getHashValue() {
   var m = 907633409;
   var a = 65599;
   var h = 0;
   for (var i = 0; i < this.strpasswd.length; i++) 
   h = (h % m) * a + this.strpasswd.charCodeAt(i);
   return h;
}
function password_getpermutation() {
   var nNUMELEMENTS = 13;
   var nCYCLELENGTH = 21;
   pg = new permutationGenerator(nNUMELEMENTS);
   var anCycle = new Array(nCYCLELENGTH);
   var npred   = this.getHashValue();
   for (var i = 0; i < nCYCLELENGTH; i++) {
      npred = 314159269 * npred + 907633409;
      anCycle[i] = npred % pg.nNumtranspositions;
   }
   return pg.fromCycle(anCycle);
}
function SecureContext(strText, strSignature, bEscape) {
   this.strSIGNATURE = strSignature || '';
   this.bESCApE = bEscape || false;
   this.strText = strText;
   this.escape = SecureContext_escape;
   this.unescape = SecureContext_unescape;
   this.transliterate = SecureContext_transliterate;
   this.encypher = SecureContext_encypher;
   this.decypher = SecureContext_decypher;
   this.sign = SecureContext_sign;
   this.unsign = SecureContext_unsign;
   this.secure = SecureContext_secure;
   this.unsecure = SecureContext_unsecure;
}
function SecureContext_escape(strToEscape) {
   var strEscaped = '';
   for (var i = 0; i < strToEscape.length; i++) {
      var chT = strToEscape.charAt( i );
      switch(chT) {
         case '\r': strEscaped += '\\r'; break;
         case '\n': strEscaped += '\\n'; break;
         case '\\': strEscaped += '\\\\'; break;
         default: strEscaped += chT;
      }
   }
   return strEscaped;
}
function SecureContext_unescape(strToUnescape) {
   var strUnescaped = '';
   var i = 0;
   while (i < strToUnescape.length) {
      var chT = strToUnescape.charAt(i++);
      if ('\\' == chT) {
         chT = strToUnescape.charAt( i++ );
         switch( chT ) {
            case 'r': strUnescaped += '\r'; break;
            case 'n': strUnescaped += '\n'; break;
            case '\\': strUnescaped += '\\'; break;
            default: // not possible
         }
      } else strUnescaped += chT;
   }
   return strUnescaped;
}
function SecureContext_transliterate(btransliterate) {
   var strDest = '';
   var nTextIter = 0;
   var nTexttrail = 0;
   while (nTextIter < this.strText.length) {
      var strRun = '';
      var cSkipped = 0;
      while (cSkipped < 7 && nTextIter < this.strText.length) {
         var chT = this.strText.charAt(nTextIter++);
         if (-1 == strRun.indexOf(chT)) {
            strRun += chT;
            cSkipped = 0;
         } else cSkipped++;
      }
      while (nTexttrail < nTextIter) {
         var nRunIdx = strRun.indexOf(this.strText.charAt(nTexttrail++));
         if (btransliterate) {
            nRunIdx++
            if (nRunIdx == strRun.length) nRunIdx = 0;
         } else {
            nRunIdx--;
            if (nRunIdx == -1) nRunIdx += strRun.length;
         } strDest += strRun.charAt(nRunIdx);
      }
   }
   this.strText = strDest;
}
function SecureContext_encypher(anperm) {
   var strEncyph = '';
   var nCols = anperm.length;
   var nRows = this.strText.length / nCols;
   for (var i = 0; i < nCols; i++) {
      var k = anperm[ i ];
      for (var j = 0; j < nRows; j++) {
         strEncyph += this.strText.charAt(k);
         k         += nCols;
      }
   }
   this.strText = strEncyph;
}
function SecureContext_decypher(anperm) {
   var nRows = anperm.length;
   var nCols = this.strText.length / nRows;
   var anRowOfs = new Array;
   for (var i = 0 ; i < nRows; i++) anRowOfs[ anperm[ i ] ] = i * nCols;
   var strplain = '';
   for (var i = 0; i < nCols; i++) {
      for (var j = 0; j < nRows; j++)
         strplain += this.strText.charAt(anRowOfs[ j ] + i);
   }
   this.strText = strplain;
}
function SecureContext_sign(nCols) {
   if (this.bESCApE) {
      this.strText = this.escape(this.strText);
      this.strSIGNATURE = this.escape(this.strSIGNATURE);
   }
   var nTextLen = this.strText.length + this.strSIGNATURE.length;
   var nMissingCols = nCols - (nTextLen % nCols);
   var strpadding = '';
   if (nMissingCols < nCols)
   for (var i = 0; i < nMissingCols; i++) strpadding += ' ';
   var x = this.strText.length;
   this.strText += strpadding + this.strSIGNATURE;
}
function SecureContext_unsign(nCols) {
   if (this.bESCApE) {
      this.strText = this.unescape(this.strText);
      this.strSIGNATURE = this.unescape(this.strSIGNATURE);
   }
   if ('' == this.strSIGNATURE) return true;
   var nTextLen = this.strText.lastIndexOf(this.strSIGNATURE);
   if (-1 == nTextLen) return false;
   this.strText = this.strText.substr(0, nTextLen);
   return true;
}
function SecureContext_secure(strpasswd) {
   var passwd = new password(strpasswd);
   var anperm = passwd.getpermutation()
   this.sign(anperm.length);
   this.transliterate(true);
   this.encypher(anperm);
}
function SecureContext_unsecure(strpasswd) {
   var passwd = new password(strpasswd);
   var anperm = passwd.getpermutation()
   this.decypher(anperm);
   this.transliterate(false);
   return this.unsign(anperm.length);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function doSecure(text) {
   var sc = new SecureContext(text, 'd2', '');
   sc.secure('r2');
   return sc.strText;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var uid = new Array(
            "benny", "christer", 
            "hedberg", "hofman", "lilljegren", "josefsson", "pettersson", 
            "sandberg", "jansson", "leo", "jeansson", "wihlborg", 
            "krantz", "almqvist", "lidstrom", "eskesen", "comstedt", 
            "backgard"
         );
var name = new Array(
            "Benny Ong", "Christer Hedberg", 
            "Christer Hedberg", "Peter Hofman", "Kjell Lilljegren", "Urban Josefsson", "Nils-Owe Pettersson", 
            "Torgny Sandberg", "Björn Jansson", "Robert Leo", " Bo Jeansson", "Stefan Wihlborg", 
            "Fredrik Krantz", "Björn Almqvist", "Bernt Lidström", "Bo Eskesen", "Tommy Comstedt",
            "Olle Backgård"
         );
var pwd = new Array(
            "2dd3w3de1dd q", "2dd g ds0dddb", 
            "2dd g ds0dddb", "4ddbg ds0ddd2", "5ddbg ds0ddd2", "6ddbg ds0ddd2", "8ddbg ds0ddd2", 
            "3ddbg ds1ddd2", "5ddbg ds1ddd2", "6ddbg ds1ddd2", "1dd1g ds2dddb", " dd g ds2dddb", 
            "3dd3g ds2dddb", "2dd g ds3dddb", "1ddbg ds5ddd2", "1ddbg ds6ddd2", "1ddbg ds7ddd2", 
            "2dd g ds6dddb"
         );
         
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

function countdown(yr, m, d) {
   theyear = yr;
   themonth = m;
   theday = d;
   var today = new Date();
   var todayy = today.getYear();
   if (todayy < 1000) {
      todayy+=1900
   }
   var todaym = today.getMonth();
   var todayd = today.getDate();
   var todayh = today.getHours();
   var todaymin = today.getMinutes();
   var todaysec = today.getSeconds();
   
   var todaystring = montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
   futurestring = montharray[m-1]+" "+d+", "+yr;
   dd = Date.parse(futurestring)-Date.parse(todaystring);
   dday = Math.floor(dd/(60*60*1000*24)*1) + 1;
   dhour = Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
   dmin = Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
   dsec = Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
   
   if(dday==0) { // && dhour==0 && dmin==0 && dsec==1) {
      return 'See You at Cookvention 2010!';
   } else if(dday < 0) {
      return '';
   } else {
      return (dday + ' Days and Counting Down to Cookvention 2010!');
   }
   setTimeout("countdown(theyear,themonth,theday)",1000);
}

function delCookie() {
   var c_name = 'bgsname';
   var cookie_date = new Date();
   if( confirm('Once logged out, you will no longer able to access the Members Only section.\nAre you sure you want to log out?') ) {
      cookie_date.setTime ( cookie_date.getTime() - 1 );
      document.cookie = c_name += "=; expires=" + cookie_date.toGMTString();
      window.location.reload();
   }
}

function setCookie(c_name, value, expiredays) {
   var exdate = new Date();
   exdate.setDate(exdate.getDate()+expiredays);
   document.cookie = c_name + "=" + escape(value) + ( expiredays==null?"":";expires="+exdate.toGMTString() );
}

function getCookie(c_name) {
   if(document.cookie.length > 0) {
      c_start = document.cookie.indexOf(c_name + "=");
      if(c_start != -1) {
         c_start = c_start + c_name.length+1;
         c_end = document.cookie.indexOf(";", c_start);
         if (c_end == -1) c_end=document.cookie.length;
         return unescape( document.cookie.substring(c_start, c_end) );
      }
   }
   return "";
}

function valCookie() {
   var html = "<p>";
   var username = getCookie('bgsname');
   if(username != null && username != '') {
      html = "<center><table cellpadding='0' cellspacing='0' border='0' bgcolor='#CFC4E8' width='78%'>" + 
             "<tr><td width='30%'><font face='Arial Narrow, Arial' size='2'>" + 
             " &nbsp; &nbsp; &nbsp; <b>Logged In</b>: " + username + "</font></td>" + 
             "<td width='40%' align='center'><font face='Arial' size='2' color='#0d0d99'><b>" + 
             countdown(2010, 10, 2) + "</b></font></td>" + 
             "<td width='30%'align='right'><font face='Arial' size='2'>" + 
             "[ <a href='#' onclick='delCookie();' title='Logout'>Logout</a> ] &nbsp; &nbsp; &nbsp; </font></td></tr>" + 
             "<tr><td valign='bottom' align='left' colspan='2'><img src='img/corner_bl.gif'></td>" + 
             "<td valign='bottom' align='right'><img src='img/corner_br.gif'></td></tr></table></center><p>";
   } 
   document.getElementById("member").innerHTML = html;
}

function checkCookie(page) {
   var html = "<p>";
   var username = getCookie('bgsname');
   var file = location.href;
   if(file.lastIndexOf("/") != -1) {
      file = file.substring(file.lastIndexOf("/")+1, file.length); 
   } 
   if(username != null && username != '') {
      if(file != page) {
         move(page);
      }
      html = "<center><table cellpadding='0' cellspacing='0' border='0' bgcolor='#CFC4E8' width='78%'>" + 
             "<tr><td width='30%'><font face='Arial Narrow, Arial' size='2'>" + 
             " &nbsp; &nbsp; &nbsp; <b>Logged In</b>: " + username + "</font></td>" + 
             "<td width='40%' align='center'><font face='Arial' size='2' color='#0d0d99'><b>" + 
             countdown(2010, 10, 2) + "</b></font></td>" + 
             "<td width='30%'align='right'><font face='Arial' size='2'>" + 
             "[ <a href='#' onclick='delCookie();' title='Logout'>Logout</a> ] &nbsp; &nbsp; &nbsp; </font></td></tr>" + 
             "<tr><td valign='bottom' align='left' colspan='2'><img src='img/corner_bl.gif'></td>" + 
             "<td valign='bottom' align='right'><img src='img/corner_br.gif'></td></tr></table></center><p>";
   } else {
      if(file != 'members.html') {
         move('members.html');
      }
   }
   document.getElementById("member").innerHTML = html;
}

function move(page) {
   window.location = page;
}

function validatePwd(username, password) {
   var encrypted = doSecure(password);
   var validUID = false;
   for(var i=0; i<uid.length; ++i) {
      if(username.toLowerCase() == uid[i]) {
         validUID = true;
         if(encrypted.toLowerCase() == pwd[i]) {
            setCookie('bgsname', name[i], 1);
            checkCookie();
         } else {
            alert("INVALID PASSWORD");
            return false;
         }
         break;
      } 
   }
   if(!validUID) {
      alert("USER '" + username + "' IS NOT FOUND.");
      return false;
   }
}
