/*############################################*/
// New Post Box
/*############################################*/
function new_post()
{
  post_box = my_getbyid('posts');

  new_post  = "<div id='new_post_remover'>";
  new_post += "<div id='new_titleBox' style='position:relative;left:-8px;padding:6px 0px 0px 8px;font-size:16px;font-weight:bold;background-image:url(../style/sss_new/images/newsTitleBar.gif);background-repeat:no-repeat;width:359px;height:29px;color:#006699;'>";
  new_post += "<div style='color:#000;float:left;'>&#0187;</div>";
  new_post += "<div id='new_title' style='float:left;padding:2px;background-color:#FFF;border:1px solid #9fa8bb;overflow:auto;width:200px;color:#000;font-size:12px;font-weight:lighter;' contenteditable='true'>Enter Title Here</div>";
  new_post += "</div>";
  new_post += "<div style='font-size:11px;position:relative;left:-5px;top:-5px;border-bottom:1px solid #b1b9ca;width:60%;'>Posted Date Will Automatically Be Set</div>";
  new_post += "<div id='button_bar' style='position:relative;left:15px;display:none;'></div>";
  new_post += "<div id='new_postBox' style='position:relative;left:15px;width:95%;'><span id='new_post' style='padding:2px;background-color:#FFF;border:1px solid #9fa8bb;overflow:auto;width:400px;height:200px;' contenteditable='true'>Enter Post Here</span><span id='save'><br /><div align='right'><div align='center' style='padding:3px;border:1px solid #9fa8bb;border-top:0px;background-color:#cfd9ea;width:50px;position:relative;left:-66px;' onClick=\"save_new_post();\">Save</div></div></span></div><br />";
  new_post += "</div>";

  post_box.innerHTML = new_post+post_box.innerHTML;
}

/*############################################*/
// Load MSG
/*############################################*/
function loadMsg(folder,msg,totalMsgs)
{

  currentMsg    = msg;
  currentFolder = folder;

  for (var i=1;i<=totalMsgs;i++)
  {
    if (my_getbyid('msg_'+i).getAttribute("class")!='msg_row_deleted')
    {
      my_getbyid('msg_'+i).setAttribute("class", "msg_row");
    }
    my_getbyid('msg_'+i).onclick = function()
    {
      loadMsg(folder,i,totalMsgs);
    }
  }

  thisMsg = my_getbyid('msg_'+msg);
  thisMsg.onclick = function()
  {
  }

  msgBody = my_getbyid('msg_body');
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support XMLHttpRequest!");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4 && xmlHttp.status==200)
    {
      thisMsg.setAttribute("class", "msg_row_selected");
      thisMsg.style.fontWeight = '';
      thisMsgStatus = my_getbyid('status_'+msg);
      thisMsgStatus.innerHTML = "<img src='" + base + "style/" + ourStyle + "/images/dot.png' />";
      msgBody.innerHTML = "<div id='msg'>" + xmlHttp.responseText + "</div>";
    }
    else
    {
      msgBody.innerHTML = "<div align='center'><img src='"+base+"style/"+ourStyle+"/images/ajax-loader.gif' /></div>";
    }
  }

  //xmlHttp.onreadystatechange();
  xmlHttp.open("POST",base + "pages/loadmsg.php?folder=" + folder + "&msg=" + msg + "&random=" + Math.random(),true);
  xmlHttp.send(null);

}

/*############################################*/
// Reply To Message
/*############################################*/
function reply()
{
  msgBody = my_getbyid('msg_body');
  if (reMsg = my_getbyid('msg'))
  {
    theMsg = reMsg.innerHTML;
  }
  else
  {
    return false;
  }

  replyHTML  = "<table border='0' width='100%'>";
  replyHTML += "<tr><td>To:</td><td><input name='to' value='test@test.com' size='30'></td></tr>";
  replyHTML += "<tr><td>Bcc:</td><td><input name='bcc' value='' size='30'></td></tr>";
  replyHTML += "<tr><td>Subject:</td><td><input name='subject' value='Re: ' size='45'></td></tr>";
  replyHTML += "<tr><td colspan='2' align='center'><textarea name='reply' cols='55' rows='8'>" + theMsg + "</textarea></td></tr>";
  replyHTML += "</table>";

  msgBody.innerHTML = replyHTML;
}

/*############################################*/
// Delete Message
/*############################################*/
function deleteMsg()
{

  /*var answer = confirm('Are You Sure You Want To Delete This Message?');
 	if (answer)
  {*/
    thisMsg = my_getbyid('msg_'+currentMsg);
    //thisMsg.style.textDecoration = 'line-through';
    //thisMsg.style.color = '#999';
    //thisMsg.style.display = "none";
    msgBody = my_getbyid('msg_body');
    var xmlHttp;
    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
      // Internet Explorer
      try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        try
        {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
          alert("Your browser does not support XMLHttpRequest!");
          return false;
        }
      }
    }

    xmlHttp.onreadystatechange=function()
    {
      if(xmlHttp.readyState==4 && xmlHttp.status==200)
      {
        thisMsg.setAttribute("class", "msg_row_deleted");
        thisMsgStatus = my_getbyid('status_'+currentMsg);
        thisMsgStatus.innerHTML = "<img src='" + base + "style/" + ourStyle + "/images/deleted.png' />";
        msgBody.innerHTML = "<div align='center'>"+xmlHttp.responseText+"</div>";
      }
      else
      {
        msgBody.innerHTML = "<div align='center'><img src='"+base+"style/"+ourStyle+"/images/ajax-loader.gif' /></div>";
      }
    }

    //xmlHttp.onreadystatechange();
    xmlHttp.open("POST",base + "pages/loadmsg.php?folder=" + currentFolder + "&delete=" + currentMsg + "&random=" + Math.random(),true);
    xmlHttp.send(null);
 	/*}
 	else
  {
    return false;
 	}*/
}

/*############################################*/
// Print Message
/*############################################*/
function printMsg()
{
  // Print currentMsg
}

/*############################################*/
// Save The New Post
/*############################################*/
function save_new_post()
{

  newTitle      = my_getbyid('new_title');
  newTitle_data = newTitle.innerHTML;

  newPost      = my_getbyid('new_post');
  newPost_data = newPost.innerHTML;
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support XMLHttpRequest!");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4 && xmlHttp.status==200)
    {
      post_box = my_getbyid('posts');
      post_box.removeChild(my_getbyid('new_post_remover'));

      if (0<xmlHttp.responseText)
      {
        time     = new Date();
        month    = time.getMonth();
        day      = time.getDate();
        year     = time.getYear();
        hour     = time.getHours();
        minute   = time.getMinutes();
        second   = time.getSeconds();
        //timezone = time.getTimezone();
        theTime  = "" + ((hour > 12) ? hour - 12 : hour);
        theTime += ((minute < 10) ? ":0" : ":") + minute;
        theTime += (hour >= 12) ? " PM" : " AM";

        monthFull     = new Array;
        monthFull[0]  = 'January';
        monthFull[1]  = 'Febuary';
        monthFull[2]  = 'March';
        monthFull[3]  = 'April';
        monthFull[4]  = 'May';
        monthFull[5]  = 'June';
        monthFull[6]  = 'July';
        monthFull[7]  = 'August';
        monthFull[8]  = 'September';
        monthFull[9]  = 'October';
        monthFull[10] = 'November';
        monthFull[11] = 'December';

        if (day==1 || day==21 || day==31) suffix = 'st';
        else if (day==2 || day==22) suffix = 'nd';
        else if (day==3 || day==23) suffix = 'rd';
        else suffix = 'th';

        date = monthFull[month]+" "+day+", "+year+" @ "+theTime;

        new_post  = "<div id='new_titleBox' style='position:relative;left:-8px;padding:6px 0px 0px 8px;font-size:16px;font-weight:bold;background-image:url(../style/sss_new/images/newsTitleBar.gif);background-repeat:no-repeat;width:359px;height:29px;color:#006699;'>";
        new_post += "<div style='color:#000;float:left;'>&#0187;</div>";
        new_post += "<div id='new_title'>"+newTitle_data+"</div>";
        new_post += "</div>";
        new_post += "<div style='font-size:11px;position:relative;left:-5px;top:-5px;border-bottom:1px solid #b1b9ca;width:60%;'>Posted on <strong>"+monthFull[month]+" "+day+suffix+", "+year+"</strong> @ <strong>"+theTime+"</strong></div>";
        new_post += "<div id='new_postBox' style='position:relative;left:15px;width:95%;'><span id='new_post'>"+newPost_data+"</span><span id='save'></span></div><br />";
        post_box.innerHTML = new_post+post_box.innerHTML;
      }
    }
    else
    {
      //itm.innerHTML             = "Saving...";
    }
  }

  //xmlHttp.onreadystatechange();
  xmlHttp.open("POST",base + "frame/class_news.php?new_post=1&title=" + newTitle_data + "&post=" + newPost_data + "&random=" + Math.random(),true);
  xmlHttp.send(null);

}

/*############################################*/
// Edit News
/*############################################*/
function edit(id,edit_type,boxWidth,boxHeight)
{
  box  = my_getbyid(id+'_'+edit_type+'Box');
  box.ondblclick               = "";

  if ('post'==edit_type)
  {
    edited = my_getbyid(id+'_edited');
    buttons  = my_getbyid('button_bar');
    buttons.style.display = "";
    save_button  = my_getbyid('save');
    save_button.innerHTML = "<br /><div align='right'><div align='center' style='padding:3px;border:1px solid #9fa8bb;border-top:0px;background-color:#cfd9ea;width:50px;position:relative;left:-66px;'>Save</div></div>";
  }

  itm  = my_getbyid(id+'_'+edit_type);
  if ('title'==edit_type)
  {
    itm.style.fontWeight      = "lighter";
    itm.style.color           = "#000000";
    itm.style.fontSize        = "12px";
  }
  itm.style.padding         = "2px";
  itm.style.backgroundColor = "#FFFFFF";
  itm.style.border          = "1px solid #9fa8bb";
  itm.style.width           = boxWidth;
  itm.style.height          = boxHeight;
  itm.style.overflow        = 'auto';
  itm.contentEditable       = true;
  //newitm                    = itm.innerHTML;
  if ('post'!=edit_type)
  {
    itm.onblur = function ()
    {
      itm.style.padding         = "";
      itm.style.border          = "";
      itm.style.backgroundColor = "";
      itm.style.width           = "";
      itm.style.height          = "";
      itm.style.fontWeight      = "";
      itm.style.color           = "";
      itm.style.fontSize        = "";
      itm.contentEditable       = false;
      save(id,edit_type,boxWidth,boxHeight);
    }
  }
  else if ('post'==edit_type)
  {
    save_button.onclick = function ()
    {
      buttons.style.display = "none";
      save_button.innerHTML = "";
      itm.style.padding         = "";
      itm.style.border          = "";
      itm.style.backgroundColor = "";
      itm.style.width           = "";
      itm.style.height          = "";
      itm.style.fontWeight      = "";
      itm.style.color           = "";
      itm.style.fontSize        = "";
      itm.contentEditable       = false;

      time     = new Date();
      month    = time.getMonth();
      day      = time.getDate();
      year     = time.getYear();
      hour     = time.getHours();
      minute   = time.getMinutes();
      second   = time.getSeconds();
      theTime  = "" + ((hour > 12) ? hour - 12 : hour);
      theTime += ((minute < 10) ? ":0" : ":") + minute;
      theTime += (hour >= 12) ? " PM" : " AM";

      monthFull     = new Array;
      monthFull[0]  = 'January';
      monthFull[1]  = 'Febuary';
      monthFull[2]  = 'March';
      monthFull[3]  = 'April';
      monthFull[4]  = 'May';
      monthFull[5]  = 'June';
      monthFull[6]  = 'July';
      monthFull[7]  = 'August';
      monthFull[8]  = 'September';
      monthFull[9]  = 'October';
      monthFull[10] = 'November';
      monthFull[11] = 'December';

      date = monthFull[month]+" "+day+", "+year+" @ "+theTime;
      edited.innerHTML = "<strong>Edited:</strong> "+date;


      save_edit(id,edit_type,boxWidth,boxHeight);
    }
  }
}

/*############################################*/
// Save Edited Post
/*############################################*/
function save_edit(id,edit_type,boxWidth,boxHeight)
{
  itm  = my_getbyid(id+'_'+edit_type);
  data = itm.innerHTML;

  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support XMLHttpRequest!");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4 && xmlHttp.status==200)
    {
      box  = my_getbyid(id+'_'+edit_type+'Box');
      box.ondblclick = function () {
        edit(id,edit_type,boxWidth,boxHeight);
      }

      itm  = my_getbyid(id+'_'+edit_type);
      itm.onblur                = "";
      itm.innerHTML             = xmlHttp.responseText;
    }
    else
    {
      //itm.innerHTML             = "Saving...";
    }
  }

  //xmlHttp.onreadystatechange();
  xmlHttp.open("POST",base + "frame/class_news.php?id="+id+"&"+edit_type+"=" + data + "&random=" + Math.random(),true);
  xmlHttp.send(null);
}

/*############################################*/
// Do Bold Button
/*############################################*/
function do_bold()
{
  b = my_getbyid('but_b');
  if ('#cfd9ea'==b.style.backgroundColor)
  {
    b.style.backgroundColor = "#DA0";
  }
  else
  {
    b.style.backgroundColor = "#cfd9ea";
  }
}
