function gbi(id) { return document.getElementById(id); }
function isnumeric(n) { numeric = /^[0-9]+$/; if (numeric.test(n)) return true; return false; }

function togglediv(divid) { var e=gbi(divid); if (e) { if (e.style.display=='block') { e.style.display='none'; return false; } else { e.style.display='block'; return true; } } }
function switchdiv(divid,divid2) { var e=gbi(divid); if (e) if (f=gbi(divid2)) { if (e.style.display=='block') { e.style.display='none'; f.style.display='block'; return false; } else { e.style.display='block'; f.style.display='none'; return true; } } }
function setdivdisplay(divid,v) { var e=gbi(divid); if (e) e.style.display=v; }
function setdivvis(divid,v) { var e=gbi(divid); if (e) e.style.visibility=v; }

function disablebutton(bid) { var b = gbi(bid); if (b) { b.disabled = true; } }

function redirect(url) { parent.location.href=url; }

function lower_me(obj) { var s = obj.value; s = s.toLowerCase(); obj.value = s; }

function remember() { /* obsolete */ }

/* cookies */

function deletecookie(cookieName) {
  var expireDate = new Date();
  expireDate.setTime(today.getTime() - 3600000*24*30);
  document.cookie = cookieName+"=pendingDelete;expires="+expireDate.toGMTString()+";path=/";
}

function readcookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ')
      c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0)
      return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// set cookie and length by milisecond, default cookie life = 'undefined'

var nSec	= 1000;
var nMin	= 60000;
var nHour	= 3600000;
var nDay	= 3600000*24;
var nMonth	= nDay*30;
var nYear	= nDay*365;

function setcookie(cookieName,cookieValue,mSec) {
  if (cookieName=='undefined' || cookieName=='' || cookieName==null) {
    alert('setcookie(); cookie name is undefined, cookie was not created');
    return;
  }
  if (mSec=='undefined' || mSec=='' || mSec==null) {
    alert('setcookie(); \'' + cookieName + '\' cookie lifetime is undefined, cookie was not created');
    return;
  }
  var today = new Date();
  var expire = new Date();
  expire.setTime(today.getTime() + mSec);
  document.cookie = cookieName+"="+cookieValue+";expires="+expire.toGMTString()+";path=/;";
}

/* css switcher */

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
      return a.getAttribute("title");
  }
  return null;
}

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
       // remember this style
       setcookie("style", title, nYear);
     }
   }
}


var t_id = 0;
var y_m = 0;
var y_d = 0;

function scroll_y ( ) {
  if (t_id)
    clearTimeout(t_id);
  var a = gbi('adjTable');
  if (a) {
    a.style.top = y_m + 'px';
//    y_d++;
    y_m+=20;
//    if (y_d >= 4) { 
      y_d = 0;
      clearTimeout(t_id);
      return;      
//    }
  }
  t_id = setTimeout("scroll_y()", 0); 
}

function t_start() { return;
  //t_stop();
  //scroll_y();
}

function t_stop() {
   if(t_id)
      clearTimeout(t_id);
   t_id  = 0;
   y_d   = 0;
}

function next_keyword ( ) {
  var a = gbi('adjTable');
  if (a) {
    
  }
}

// ajax http request stuff
var ajax = { 

  newRequest:function() {
    var msProgID = new Array ( "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP" );
    if ( typeof window.ActiveXObject == 'undefined' )
      return new XMLHttpRequest();
    else {
      for ( i=0; i<4; i++ ) {
//      r = new ActiveXObject(msProgID[i]);
  //      if (r)
    //      return r;
          return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    alert("AJAX error: Cannot create HTTP request object.");
  },
  
  httpAsyncRequest:function( how, href, params ) {
    return ajax.httpRequest( how, href, params, true );
  },
  
  handleResult:function() {
    //setdivdisplay('cmnt_loading', 'none');
    //setdivdisplay('cmnt_typing', 'none');
    //setdivdisplay('cmnt_posted', 'block');

//    disablebutton('cmnt_btn1');
//    disablebutton('cmnt_btn2');
//    var cb = gbi('CommentBox');
//    cb.readOnly = true;
    //cb.className = 'disabledTextarea';
    
    
  },
  
  httpRequest:function( how, href, params, async ) {
    r = null;
    if (!href || href == "") // this function requires a url
      return;
    if (how) {
      how = how.toUpperCase(); // make sure request type is in upper case
      if (r = ajax.newRequest()) {
        if (r.overrideMimeType)
          r.overrideMimeType('text/xml');
        r.onreadystatechange = function() {
          switch (r.readyState) {
            case 4: // COMPLETE
              switch (r.status) {
                case 200: // page returned Ok

                  var placeholder = document.getElementById("newComment");
                    if (placeholder)
                      placeholder.innerHTML = r.responseText;
                      
                break;
                case 404: // page not found
                  alert("404, page not found");
                break;
                case 500: // Internal Server Error
                  alert("404, internal server error");
                default:
                  alert("uncaught http error"); // something else is wrong
              };
              break;
              default:
            };
          }
        r.open(how, href, async);
        if (how == 'POST') // we need this, otherwise the server will discard posted data
          r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        r.send(params);
        
        //document.title += '<' + r.responseText + '>';
        
        return r;
      } else { /* could not create http request */ }
    } else { /* request method not specified */ }
  }
  
}; // var ajax end

// automatic profile comment saver
var autosave_sleep = false;
var autosave_timer = 0;
var autosave_shown = 0;
var autosave_oldct = 0;
var allow_comments = true;
var last_comment_id = null;

var autosave = {

  last_is_current:function() {
    return false;
  },
  
  stillTyping:function() { // check if the user had stopped typing
  
   if (allow_comments) {
  
    if (autosave_oldct == autosave_timer && document.getElementById('cmnt_saving').innerHTML != 'Saved....') { // user stopped typing


        if (autosave_timer > 2) {
          if (js_bioid && js_qsuser && js_anonymous) {
          
            var commentbox = gbi('CommentBox').value;
            var params1 = 'a=' + js_bioid + '&b=' + js_qsuser + '&c=' + js_anonymous + '&d=' + commentbox;
            var params2 = 'a=' + last_comment_id + '&b=' + commentbox;
            var urlAjaxPostComment = 'http://www.authenticsociety.com/ajax/postcomment.php';
            var urlAjaxUpdateComment = 'http://www.authenticsociety.com/ajax/updatecomment.php';
        
            var ret = null;
            
            //document.title += '[' + last_comment_id + ']';
            
            if (isnumeric(last_comment_id))
              ret = ajax.httpAsyncRequest( 'POST', urlAjaxUpdateComment, params2);
            else
              ret = ajax.httpAsyncRequest( 'POST', urlAjaxPostComment, params1);
        
            //document.title += '[' + ret.responseText + ']';
        
            if (ret) {
              //document.title += "-posted ok ( last id was " + last_comment_id + " ) -";
              fader.doFade('cmnt_status_border', -1); 
              document.getElementById('cmnt_saving').innerHTML = 'Saved....';
              //document.title += "-saved-";
            }
            else {
              //document.title += "-we're fucked-";
              document.getElementById('cmnt_saving').innerHTML = 'Ooops, our server cannot post your comment at this time, try again later.';
              allow_comments = false;
            }
          }
        }

    }
    autosave_oldct = autosave_timer;
    setTimeout(autosave.stillTyping, 3000);
    }
  },

  startIncTimer:function() { // save comment every 5 seconds and only if it has been changed
    autosave_timer++;
    document.getElementById('cmnt_saving').innerHTML = '<span class="faded">Typing...</span>';
  }
 
};

var fadeid = null;
var fadestate = 'none';
var fadeframe = 0;
var fader = {

  focusOn:function( id ) {
    return gbi(id);
  },
  
  doFade:function( id, how ) {
  
    fadeid = fader.focusOn ( id );
  
    if (fadeid == null)
      return;

    if ( how == -1 ) {
      fader.reset(10);
      fader.startFadeOut();
      
      //alert(fadestate + ',' + fadeframe);
      
    }
    else {
      fader.reset(0);
      fader.startFadeIn();
    }
  },
  
  reset:function( ff ) {
    fadestate = 'none';
    fadeframe = ff;
  },
  
  fadein:function() { // assuming mozff is reset to 0
    //document.title = 'start fadein() with fadeframe = ' + fadeframe + ', fadestate = ' + fadestate;  

    fadeframe = fadeframe + 1;

    if (fadeframe <= 10 && fadestate != 'none') {    
    
      var newclasstag = 'mof'+fadeframe;
      
      //document.title += fadeframe + ',';
    
      if (fadeid)    
        fadeid.className = newclasstag;
      else
        document.title = "{{{FATAL JS ERROR}}} fadeid is not defined";
      
    setTimeout(fader.fadein, 1);    
    } else { fadestate = 'none'; return; }
  },
  
  fadeout:function() { // assuming mozff is reset to 9
   // document.title = 'start fadeout() with fadeframe = ' + fadeframe + ', fadestate = ' + fadestate;
    
    fadeframe = fadeframe - 1;
    
    if (fadeframe >= 0 && fadestate != 'none') {
    
      var newclasstag = 'mof'+fadeframe;
      
      //document.title += fadeframe + ',';
    
      if (fadeid)
        fadeid.className = newclasstag;
      else
        document.title = "{{{FATAL JS ERROR}}} fadeid is not defined";
      
      setTimeout(fader.fadeout, 1);
    } else { fadestate = 'none'; return; }
  },
  
  startFadeOut:function() {
    if (fadeid == null) { document.title = '{{FATAL JS ERROR}} fadeid = null';
      return; }
    if (fadestate == 'none') {
      fadestate = 'fading out';
      fader.fadeout();
    }
  },
  
  startFadeIn:function() {
    if (fadeid == null) {  document.title = '{{FATAL JS ERROR}} fadeid = null';
      return; }
    if (fadestate == 'none') {
      fadestate = 'fading in';
      fader.fadein();    
    }
  }
};

var Stopped = 0;
var Paused  = 2;
var Playing = 4;
var Lecture = {
  
};