globalCookieName = window.globalCookieName || "blocks";
refreshLocation = window.refreshLocation || "members.functions.php";
resetLocation = window.resetLocation || "members.php";
cookiePath = window.cookiePath || "/";

// alert("cookiePath - "+cookiePath+'\nglobalCookieName - '+globalCookieName+'\nrefreshLocation - '+refreshLocation+'\nresetLocation - '+resetLocation);

function shblock(id,dowhat){
  var p=document.getElementById(id+'_content');
  if (dowhat){
    var img;
    if (dowhat=='show'){
      if (img=document.getElementById('img_plus.gif_'+id)){
        p.style.display='block';
        img.setAttribute("src","/img/icons/minus.gif");
        img.setAttribute("id","img_minus.gif_"+id);
        img.setAttribute("title","Collapse Module Contents");
        img.setAttribute("alt","Collapse Module Contents");
        storeincookie(id,1,1);
      }
    }
    if (dowhat=='hide'){
      if (img=document.getElementById('img_minus.gif_'+id)){
        p.style.display='none';
        img.setAttribute("src","/img/icons/plus.gif");
        img.setAttribute("id","img_plus.gif_"+id);
        img.setAttribute("title","Expand Module Contents");
        img.setAttribute("alt","Expand Module Contents");
        storeincookie(id,1,0);
      }
    }
  } else {
    if (p.style.display=='none'){
      var img=document.getElementById('img_plus.gif_'+id);
      p.style.display='block';
      img.setAttribute("src","/img/icons/minus.gif");
      img.setAttribute("id","img_minus.gif_"+id);
      img.setAttribute("title","Collapse Module Contents");
      img.setAttribute("alt","Collapse Module Contents");
      storeincookie(id,1,1);
    } else {
      var img=document.getElementById('img_minus.gif_'+id);
      p.style.display='none';
      img.setAttribute("src","/img/icons/plus.gif");
      img.setAttribute("id","img_plus.gif_"+id);
      img.setAttribute("title","Expand Module Contents");
      img.setAttribute("alt","Expand Module Contents");
      storeincookie(id,1,0);
    }
  }
}


function dropdown(block){
  var dd=document.createElement("form");
  dd.setAttribute("name","dd_"+block);
  dd.setAttribute("id","dd_"+block);
  dd.style.cssFloat="right";
  dd.style.styleFloat="right";
  dd.style.paddingRight="2px";
  dd.style.margin="0px";
  dd.innerHTML="<select style='border:1px solid #cccccc;font:normal 9px Arial,sans-serif;margin:0px;'>";
  dd.innerHTML+="<option value=1>Last 1 hour</option>";
  dd.innerHTML+="<option value=2>Last 24 hours</option>";
  dd.innerHTML+="<option value=3>Last 72 hours</option>";
  dd.innerHTML+="<option value=4>Last 7 days</option>";
  dd.innerHTML+="</select>";

  return dd;

}




function showcontrols(id,on,disablehide,disableorder,firstcell){
//  alert(id+'-'+on+'-'+disablehide+'-'+disableorder+'---'+firstcell)
  if (on==1) var start=1;
  else var start=0;

  var td=document.getElementById(id);
  var dtd=td.getElementsByTagName('div');
  for(var i=0;i<dtd.length;i++){
    if(dtd[i].className.indexOf('blocktitle')!=-1){
      var d=dtd[i];
    }
  }
  var dt=d.firstChild;

//if (id=='dp'){
//  var refbutton=dropdown("dp");
//  d.insertBefore(refbutton,dt);
//  dt=refbutton;
//}

  if (disableorder!=1){
    var rf=controlButton("refresh.gif","refresh('"+id+"'"+(id=="ch"?",'loc'":"")+")",id,"Refresh Module Contents");
    d.insertBefore(rf,dt);
    var au=controlButton("up.gif","move('"+id+"','up')",id,"Move Module UP");
    d.insertBefore(au,rf);
    if (firstcell==1 || firstcell==3 || firstcell==4) document.getElementById("img_up.gif_"+id).className+=" dontshow";
    var ad=controlButton("down.gif","move('"+id+"','down')",id,"Move Module DOWN");
    d.insertBefore(ad,au);
    if (firstcell==2 || firstcell==3 || firstcell==4) document.getElementById("img_down.gif_"+id).className+=" dontshow";
  }

// show/hide button
  if (start!=1) {
    var a=controlButton("plus.gif","shblock('"+id+"')",id,"Expand Module Contents");
  } else {
    var a=controlButton("minus.gif","shblock('"+id+"')",id,"Collapse Module Contents");
  }


  if (ad!=undefined){
    d.insertBefore(a,ad);
  } else  {
    d.insertBefore(a,dt);
  }



  if (disablehide!=1){
  var ax=controlButton("x.gif","delblock('"+id+"')",id,"Remove Module");
  d.insertBefore(ax,a);
  if (firstcell==4) document.getElementById("img_x.gif_"+id).className+=" dontshow";
  }

// these are cool but need more work - ie doesnt seem to understand it
//  d.setAttribute("onClick","javascript:shblock('"+id+"')");
//  d.setAttribute("onmouseover","style='cursor:pointer'");

  if (start!=1) {
  document.getElementById(id+'_content').style.display='none';
  }
  firstcell=0;
}

var doshowcontrols=1;

function allcontrols(){
  var d=document.getElementsByTagName('img');
  for(var i=0;i<d.length;i++){
    if(d[i].className.indexOf('controlbutton')!=-1 && d[i].className.indexOf('dontshow')==-1  && d[i].id.indexOf('plus')==-1  && d[i].id.indexOf('minus')==-1){
      if (doshowcontrols){
        d[i].style.display="none";
      } else {
        d[i].style.display="inline";
      }
    }
  }
  if (doshowcontrols)doshowcontrols=0;
  else doshowcontrols=1;
}

function delblock(id){
  if(confirm("This will remove the module. To restore it, use the \"Page settings\" link in the top right corner of the page.\n\nAre you sure you want to remove the module?")){
    var d=document.getElementById(id);
    var storage=document.getElementById('hiddenblocks');
    var p=d.parentNode;
    storage.innerHTML+="<div id='restore_"+id+"'>&raquo;&nbsp;<a href=\"javascript:restore('"+id+"','"+p.id+"')\">"+(id=="dp"?"Detected Problems":blocksnames[id])+"</a></div>";
    document.getElementById('nohidden').style.display='none';
    p.removeChild(d);
    storeincookie(id,0,0);
    normalize(p.id);
  }
}

function removefromrestorelist(id){
  var c=document.getElementById(id);
  var p=document.getElementById('hiddenblocks');
  p.removeChild(c);
  var cnt=0;
  var rest=p.getElementsByTagName('div');
  for(var i=0;i<rest.length;i++){
    if(rest[i].id.indexOf('restore_')!=-1){
      cnt++;
    }
  }
  if (!cnt) document.getElementById('nohidden').style.display='block';
}


function move(id,direction){
  var d=document.getElementById(id);
  var p=d.parentNode;
  var b=p.getElementsByTagName('div');
  var blocks=new Array();
  var pos=1;
  var mypos=blockssettings[id].substring(4);
  var mycol=blockssettings[id][3];

  for(var i=0;i<b.length;i++){
  if(b[i].className.indexOf('blocks')!=-1){
  blocks[pos]=b[i];
  if (b[i].id==id){
  mypos=pos;
  }
  pos++;
  }
  }

  if (direction=="up" && mypos>1){
    var oldid=blocks[(mypos-1)];
    p.insertBefore(d,blocks[(mypos-1)]);
    blocks[(mypos-1)]=d;
    blocks[mypos]=oldid;
    storeincookie(id,4,mypos-1);
    storeincookie(oldid.id,4,mypos);
  } else if (direction=="down" && blocks[(mypos+1)]){
    var oldid=p.replaceChild(d,blocks[(mypos+1)]);
    p.insertBefore(oldid,d);
    blocks[(mypos+1)]=d;
    blocks[mypos]=oldid;
    storeincookie(id,4,mypos+1);
    storeincookie(oldid.id,4,mypos);
  }


  for(var i=1;i<blocks.length;i++){
    var up=document.getElementById("img_up.gif_"+blocks[i].id);
    var dn=document.getElementById("img_down.gif_"+blocks[i].id);
    if (i==1){
    up.className+=" dontshow";
    dn.className="controlbutton";
    } else if (i==(blocks.length-1)){
    up.className="controlbutton";
    dn.className+=" dontshow";
    } else {
    up.className="controlbutton";
    dn.className="controlbutton";
    }
  }
}

function normalize(col){

  var blocks=new Array();
  var pos=1;

  var center=document.getElementById('centercolumn');
  var contp=center.parentNode;

  var cont=document.getElementById(col);
  var b=cont.getElementsByTagName('div');

  for(var i=0;i<b.length;i++){
    if(b[i].className.indexOf('blocks')!=-1){
      blocks[pos]=b[i];
      pos++;
    }
  }

  for (i=1;i<blocks.length;i++){
    storeincookie(blocks[i].id,4,i);
    var cls=document.getElementById("img_x.gif_"+blocks[i].id);
    var up=document.getElementById("img_up.gif_"+blocks[i].id);
    var dn=document.getElementById("img_down.gif_"+blocks[i].id);
    if (pos==2 && cls && col=="centercolumn"){
      cls.className+=" dontshow";
    } else if (pos>2 && cls && col=="centercolumn"){
      cls.className="controlbutton";
    }
    if (up){
      if (pos==2) {
      up.className+=" dontshow";
      } else if (i==1) {
      up.className+=" dontshow";
      } else {
      up.className="controlbutton";
      }
      if (up.style.display=="inline" && up.className.indexOf('dontshow')!=-1) up.style.display="none";
    }
    if (dn){
      if (pos==2) {
      dn.className+=" dontshow";
      } else if (i==1) {
      dn.className="controlbutton";
      } else if (i==(blocks.length-1)) {
      dn.className+=" dontshow";
      } else{
      dn.className="controlbutton";
      }
    }
  }
  if (col=="rightcolumn" && pos==1 && !document.getElementById('pb')){
    contp.removeChild(cont);
    center.style.paddingRight="0px";
    center.style.display="block";
  }
}

function storeincookie(id,pos,val){
  var str=blockssettings[id];
  if(pos==4) {
    str=str.substring(0,pos)+val;
  }else{
    str=str.substring(0,pos)+val+str.substring(pos+1);
  }
  blockssettings[id]=str;
  var toCookie = blockssettings.toJSONString();
  createCookie(globalCookieName,toCookie,30);
  checksaved(blockssettings);
}

function checksaved(blockcookie){
  var check=0;
  for (var i in blocksnames){
    if(blockcookie[i]!=savedblocks[i]){
      check=1;
      break;
    }
  }
  if (document.getElementById('savechanges')){
    onoff('savechanges',check);
  }
  if (document.getElementById('savetop')){
    onoff('savetop',check);
  }
}




function refresh(id,optionals){
  if (document.getElementById(id+"_content")) {
    if (optionals=="loc"){
      loc=document.f.loc.value;
    }
    document.getElementById(id+"_content").innerHTML = '<div style="text-align:center"><img src="/img/wait20trans.gif" width=20 height=20 border="0"></div>';
    doRefresh(refreshLocation+(refreshLocation.indexOf("?")!=-1?'&refresh=':'?refresh=')+id+(optionals=="loc"?"&loc="+loc:""),id+"_content",0);
    shblock(id,'show');
  }
}

function restore(b,col){
  removefromrestorelist("restore_"+b);
  if (col=="rightcolumn" && !document.getElementById(col)){
    var contp=document.getElementById('centercolumn').parentNode;
    var right=document.createElement("TD");
    right.setAttribute("id",col);
    contp.appendChild(right);
    right.style.width="25%";
    right.style.borderLeft="1px dotted #B7B7B7";
    document.getElementById('centercolumn').style.paddingRight="15px";
    document.getElementById('centercolumn').style.width="auto";
  }



  column=document.getElementById(col);

  var c=document.createElement("DIV");
  c.setAttribute("id",b);
  c.className="blocks";
  c.innerHTML="<div class='blocktitle left'><img src='/img/icons/"+blocksicons[b]+".png' width='16' height='16' border='0' alt=''> "+blocksnames[b]+"</div><div id='"+b+"_content' class='in "+(b=='st'?'center':'left')+"'><img align=center src='/img/wait20trans.gif' width=20 height=20 border=0></div>";

  var fb=returnFirstBlock(column);
  if (fb){
    column.insertBefore(c,fb);
    showcontrols(b,1,0,0,1);
  }
  else {
  column.appendChild(c);
  showcontrols(b,1,0,0,2);
  }

  pumpittimeout=0;
  doRefresh(refreshLocation+(refreshLocation.indexOf("?")!=-1?'&refresh=':'?refresh=')+b,b+"_content",0);
  storeincookie(b,0,1);
  storeincookie(b,1,1);
  normalize(col);
  if ((b=="st" || b=="dp" || b=="la") && globalCookieName=="blocks") _autorefresh();
  if (!doshowcontrols){
    doshowcontrols=1;
    allcontrols();
  }

}

function returnFirstBlock(c){
  var b=c.getElementsByTagName('div');
  for(var i=0;i<b.length;i++){
    if(b[i].className.indexOf('blocks')!=-1){
      return b[i];
    }
  }
  return false;
}


function doRefresh(url,container,docontrols){
//  alert("refreshing to "+url);
      var lcontainer=document.getElementById(container);
//          lcontainer.innerHTML = '<img align=center src="/img/wait20trans.gif" width=20 height=20 border="0">';
  var xmlHttp = createXmlHttpRequestObject();
  if (xmlHttp){
    try{
      var sep = (-1 < url.indexOf("?")) ? "&" : "?";
      url = url + sep + "__=" + encodeURIComponent((new Date()).getTime());
      xmlHttp.open("GET", url, true);

      xmlHttp.onreadystatechange = function(){
//                alert(xmlHttp.readyState);
        if (xmlHttp.readyState == 4){
          if (xmlHttp.status == 200){
            try {
              var theresponse=xmlHttp.responseText;
              lcontainer.innerHTML=theresponse
              var tobeparsed=theresponse.match(/<script>var (thetime=new Date.*?)<\/script>/);

              if (docontrols)showcontrols(lcontainer.id,1,0,0,2);
              if (container=='ti_content') {
                if (pumpittimeout)
                {
                  clearTimeout(pumpittimeout);
                }
                eval(tobeparsed[1]);
                pumpit(0); //makes the : ticking
              }
              if (container=='ch_content')  loc=document.f.loc.value; //makes the : ticking
            }
            catch(e) {
              alert("Error reading the response: " + e.toString());
            }
          } else {
            alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
          }
        }
      };
  	  xmlHttp.send(null);
    }
    catch (e){
      alert("Can't connect to server:\n" + e.toString());
    }
    delete xmlHttp;
  }
}

function resetpage(){
  eraseCookie(globalCookieName);
  window.location.href=((resetLocation.indexOf("?")!=-1)?resetLocation+"&resetpage=1":resetLocation+"?resetpage=1");
}

function resettodefault() {
  eraseCookie(globalCookieName);
  window.location.href=((resetLocation.indexOf("?")!=-1)?resetLocation+"&resetdef=1":resetLocation+"?resetdef=1");
}

function enablesuspendconf(){
  eraseCookie("conf");
  window.location.href=resetLocation;
}

function oldinterface() {
  createCookie("o","1",360);
  window.location.href="members.v2.php?switch=1";
}

function todashboard() {
  createCookie('o','',-1);
  window.location.href='members.php?switch=1';
}

function dismissMessage(id) {
  if (parseInt(id.substr(1))==id.substr(1)) {
    _dismissM(id);
  }
 document.getElementById(id).style.display="none";
 _checkMessage();
}

function _checkMessage() {
  var o = document.getElementById("messages_content");
  if (o!="undefined") {
    var e = o.childNodes;
    for (i = 0; i < e.length; i++) {
      if (e[i].id.substr(0,1)=="m" && e[i].style.display!="none") return;
    }
    document.getElementById("messages").style.display="none";
  }
}

function _dismissM(id) {
  var id=id.substr(1);
  var xmlHttp = createXmlHttpRequestObject();
  if (xmlHttp){
    xmlHttp.open ('GET', "/account/messages.php?d="+id, true);
    xmlHttp.onreadystatechange = function(){
      if (xmlHttp.readyState == 4){
        return 1;
      }
    }
    xmlHttp.send ('');
  } else {
    return 0;
  }
}


