//    ***********
//    SITE.INC.JS
//    ***********
//    Our Javascript Functions!

var xmlHttp;

//       ********
function postData(caction,cid,commentText,url)
//       ********
//       Post our data via HTTP
{
   xmlHttp=GetXmlHttpObject();
   if (xmlHttp==null)
   {
      alert ("Browser does not support HTTP Request");
      return;
   }
   postStr="cid="+cid+"&text="+commentText+"&action="+caction;
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", postStr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(postStr);
   document.getElementById("txtResponse").innerHTML="Updating the database...";
}

//       ************
function stateChanged()
//       ************
//       let us know when our HTTP Request is complete
{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
      document.getElementById("txtResponse").innerHTML=xmlHttp.responseText;
      editing = false;
   }
}

//       ****************
function GetXmlHttpObject()
//       ****************
//       Retrieve our HTTP request.
{
   var objXMLHttp=null;
   if (window.XMLHttpRequest)
   {
      objXMLHttp=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
      return objXMLHttp;
   }

   var editing  = false;

   if (document.getElementById && document.createElement)
   {
      var butt = document.createElement('BUTTON');
      var buttext = document.createTextNode('Edit!');
      butt.appendChild(buttext);
      butt.onclick = saveEdit;
      var editpostbutt = document.createElement('BUTTON');
      var editpostbuttext = document.createTextNode('Edit!');
      editpostbutt.appendChild(editpostbuttext);
      editpostbutt.onclick = saveEditPost;
      var subjectbutt = document.createElement('BUTTON');
      var subjectbuttext = document.createTextNode('Edit!');
      subjectbutt.appendChild(subjectbuttext);
      subjectbutt.onclick = saveEditSubject;
   }

//       *******
function catchIt(e)
//       *******
//       catch our clicks
{
   if (editing) return;
   if (!document.getElementById || !document.createElement) return;
   if (!e) var obj = window.event.srcElement
      else var obj = e.target;
   while (obj.nodeType != 1)
   {
      obj = obj.parentNode;
   }
   if (obj.tagName == 'TEXTAREA' || obj.tagName == 'A') return;
   while (obj.nodeName != 'DIV' && obj.nodeName != 'HTML')
   {
      obj = obj.parentNode;
   }
   if (obj.nodeName == 'HTML') return;
   if (!obj.getAttribute('id')) return;
   var grabbedID = obj.getAttribute('id');
      // if (!grabbedID.match('editable_content') || !grabbedID.) return;
   if (grabbedID.match('editable_content'))
   {
   var commentID = grabbedID.substr(16); // grab our number from this.
   var x = obj.innerHTML;
   var y = document.createElement('TEXTAREA');
      y.setAttribute('cols','80');
      y.setAttribute('id',commentID);
      y.setAttribute('rows','8');
      y.setAttribute('wrap','soft');
      y.style.whiteSpace = 'pre';
   // this will put the button below the textarea, but needs to be removed....
   var yBR = document.createElement('SPAN');
      yBR.setAttribute('id','remove_me');
      yBR.innerHTML = '<br />Useable tags: <b>&lt;b&gt;&lt;/b&gt;</b>, <i>&lt;i&gt;&lt;/i&gt;</i>, &lt;pre&gt;&lt;/pre&gt;, &lt;a href=\'http://link.here\'&gt;&lt;/a&gt;<br />';
   var z = obj.parentNode;
      z.insertBefore(y,obj);
      z.insertBefore(yBR,obj);
      z.insertBefore(butt,obj);
      z.removeChild(obj);
   // ok, this next part took FOREVER to sort out.
   // y.value takes <br>'s (and <br />'s -- even thought I don't tell it to... ? ah well..)
   // it takes them and replaces <br>\n (a line break) and turns it into just a line break.
   // but in IE, the textareas don't recognise linebreaks from the source for some reason
   // so a second command is needed to replace the <br> as if there was no \n after it
   // so <br> is replaced by \r, which isn't used by FF or Opera, but is by IE for some reason
   // so this is almost browser-specific code, without really being such. 'Cause without that,
   // FF/Opera would have double line breaks, or IE would have no line breaks. Both unacceptable.
   // I hope this is all that ever has to be done to fix this!
   y.value = x.replace(/<br>\n/gi, '\r\n');
   y.value = x.replace(/<br>/gi, '\r');
   y.focus();
   editing = true;
   }
   //  EDIT POSTS on FORUM
   //  *******************
   if (grabbedID.match('editable_postbody'))
   {
   var postID = grabbedID.substr(17); // grab our number from this.
   var x = obj.innerHTML;
   var y = document.createElement('TEXTAREA');
      y.setAttribute('cols','90');
      y.setAttribute('id',postID);
      y.setAttribute('rows','10');
      y.setAttribute('wrap','soft');
      y.style.whiteSpace = 'pre';
   // this will put the button below the textarea, but needs to be removed....
   var yBR = document.createElement('SPAN');
      yBR.setAttribute('id','remove_me');
      yBR.innerHTML = '<br />Some text here<br />';
   var z = obj.parentNode;
      z.insertBefore(y,obj);
      z.insertBefore(yBR,obj);
      z.insertBefore(editpostbutt,obj);
      z.removeChild(obj);
   // ok, this next part took FOREVER to sort out.
   // y.value takes <br>'s (and <br />'s -- even thought I don't tell it to... ? ah well..)
   // it takes them and replaces <br>\n (a line break) and turns it into just a line break.
   // but in IE, the textareas don't recognise linebreaks from the source for some reason
   // so a second command is needed to replace the <br> as if there was no \n after it
   // so <br> is replaced by \r, which isn't used by FF or Opera, but is by IE for some reason
   // so this is almost browser-specific code, without really being such. 'Cause without that,
   // FF/Opera would have double line breaks, or IE would have no line breaks. Both unacceptable.
   // I hope this is all that ever has to be done to fix this!
   y.value = x.replace(/<br>\n/gi, '\r\n');
   y.value = x.replace(/<br>/gi, '\r');
   y.focus();
   editing = true;
   }
   // EDITING SUBJECTS in FORUMS
   // **************************
      if (grabbedID.match('editable_subject'))
   {
   var postID = grabbedID.substr(16); // grab our number from this.
   var x = obj.innerHTML;
   var y = document.createElement('INPUT');
      y.setAttribute('type','text');
      y.setAttribute('id',postID);
      y.setAttribute('size','95');
      y.setAttribute('maxlength', '150');
      y.style.fontSize = '10pt';
   var z = obj.parentNode;
      z.insertBefore(y,obj);
      z.insertBefore(subjectbutt,obj);
      z.removeChild(obj);
   // ok, this next part took FOREVER to sort out.
   // y.value takes <br>'s (and <br />'s -- even thought I don't tell it to... ? ah well..)
   // it takes them and replaces <br>\n (a line break) and turns it into just a line break.
   // but in IE, the textareas don't recognise linebreaks from the source for some reason
   // so a second command is needed to replace the <br> as if there was no \n after it
   // so <br> is replaced by \r, which isn't used by FF or Opera, but is by IE for some reason
   // so this is almost browser-specific code, without really being such. 'Cause without that,
   // FF/Opera would have double line breaks, or IE would have no line breaks. Both unacceptable.
   // I hope this is all that ever has to be done to fix this!
   y.value = x;
   y.focus();
   editing = true;
   }
}


//       ********
function saveEdit()
//       ********
//       save our edited comments
{
   var area = document.getElementsByTagName('TEXTAREA')[0];
   var commentText = area.value;
         commentTextBrowser = commentText.replace(/\n/gi,'<br />\n');
   var commentID = area.getAttribute('id');
   var y = document.createElement('DIV');
      y.setAttribute('className','editable_content'); // for IE...
      y.setAttribute('class','editable_content');
      y.setAttribute('id','editable_content'+commentID);
      y.innerHTML = commentTextBrowser;
   var notify = document.createElement('DIV');
      notify.setAttribute('id','txtResponse');
      notify.setAttribute('class','edited_content');
      notify.setAttribute('className','edited_content');
   var z = area.parentNode;
      z.insertBefore(notify,area)
      z.insertBefore(y,area);
      z.removeChild(area);
      z.removeChild(document.getElementsByTagName('button')[0]);
      // removes our BR tag
      z.removeChild(document.getElementById('remove_me'));
   postData('updatecomment', commentID, encodeURIComponent(commentText), 'ajax_update.php');
}

//       ********
function saveEditPost()
//       ********
//       save our edited comments
{
   var area = document.getElementsByTagName('TEXTAREA')[0];
   var commentText = area.value;
         commentTextBrowser = commentText.replace(/\n/gi,'<br />\n');
   var commentID = area.getAttribute('id');
   var y = document.createElement('DIV');
      y.setAttribute('className','editable_content'); // for IE...
      y.setAttribute('class','editable_content');
      y.setAttribute('id','editable_postbody'+commentID);
      y.innerHTML = commentTextBrowser;
   var notify = document.createElement('DIV');
      notify.setAttribute('id','txtResponse');
      notify.setAttribute('class','edited_content');
      notify.setAttribute('className','edited_content');
   var z = area.parentNode;
      z.insertBefore(notify,area)
      z.insertBefore(y,area);
      z.removeChild(area);
      z.removeChild(document.getElementsByTagName('button')[0]);
      // removes our BR tag
      z.removeChild(document.getElementById('remove_me'));
   postData('updatepostbody', commentID, encodeURIComponent(commentText), 'ajax_update.php');
}

//       ********
function saveEditSubject()
//       ********
//       save our edited comments
{
   var area = document.getElementsByTagName('INPUT')[1];
   var commentText = area.value;
   var commentID = area.getAttribute('id');
   var y = document.createElement('DIV');
      y.setAttribute('className','editable_content'); // for IE...
      y.setAttribute('class','editable_content');
      y.setAttribute('id','editable_subject'+commentID);
      y.innerHTML = commentText;
   var notify = document.createElement('DIV');
      notify.setAttribute('id','txtResponse');
      notify.setAttribute('class','edited_content');
      notify.setAttribute('className','edited_content');
   var z = area.parentNode;
      z.insertBefore(notify,area)
      z.insertBefore(y,area);
      z.removeChild(area);
      z.removeChild(document.getElementsByTagName('button')[0]);
      // removes our BR tag
   postData('updatepostsubject', commentID, encodeURIComponent(commentText), 'ajax_update.php');
}



document.onclick = catchIt;
window.onload=show;

//       ****
function show(id)
//       ****
//       show our menu item
{
   var d = document.getElementById(id);
   for (var i = 1; i<=10; i++)
   {
      if (document.getElementById('smenu'+i))
      {
         document.getElementById('smenu'+i).style.display='none';
      }
   }
   if (d)
   {
      d.style.display='block';
   }
}


//       ****
function hide(id)
//       ****
//       hide our menu item.
{
   var d = document.getElementById(id);
   for (var i = 1; i<=10; i++)
   {
      if (document.getElementById('smenu'+i))
      {
         document.getElementById('smenu'+i).style.display='none';
      }
   }
   if (d)
   {
      d.style.display='none';
   }
}

