/***********************************************************************
* @name SuperGroups 1.2
* Created By Chris grVulture Michaelides
* http://www.axxis.gr
* @copyright  Copyright (C) 2009  AXXIS INTERNET SOLUTIONS / All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
************************************************************************/
var xmlhttp;
function loadXMLDoc(url, resp, myid, commentid, commentname, dirname)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Opera, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=
        function ()
            {
            if (xmlhttp.readyState==4)
              {// 4 = "loaded"
              if (xmlhttp.status==200)
                {// 200 = "OK"

//==============================================================================
                if (resp=='prepare_comment')
                  {
                var preparedcomment = xmlhttp.responseText;
                var url = dirname+'/components/com_supergroups/helpers/addcomment.php';
  loadXMLDoc(url+'?myid='+myid+'&commentid='+commentid+'&table='+encodeURIComponent(document.getElementById('table').value)+'&commentname='+encodeURIComponent(preparedcomment), 'save_comment', myid, commentid, preparedcomment, dirname);
                  }
//==============================================================================
                if (resp=='save_comment')
                  {
                          var enable_wall = document.getElementById('enable_wall').value;
                          var field = 'mydiv-'+commentid;
                          var button= 'buttondiv-'+commentid;
                          var inputdiv = 'comment-name'+commentid;
                  // process the time display...
                          var currentTime = new Date();
                          var hours = currentTime.getHours();
                          var minutes = currentTime.getMinutes();
                          var suffix = "am";
                          if (hours >= 12) {
                            suffix = "pm";
                            hours = hours - 12;
                          }
                          if (hours == 0) {
                            hours = 12;
                          }
                          if (minutes < 10) minutes = "0" + minutes;
                        
                          var minas = getDateStrWithDOW();
                          
                  // append my comment to the comments div
                          var data = xmlhttp.responseText;
                          var mycommenter = decodeURIComponent(document.getElementById('append-input'+commentid).value);
                          var superaddon_Write = document.getElementById('superaddon_Write').value;
                          now = new Date();
                          now = now.format("isoDateTime");
                        if (enable_wall==1) {
                          var olderdiv = document.getElementById('commentsdiv-'+commentid).innerHTML;
                          var newerdiv = "<div class=\"comments_div\" id=\""+data+"\" style=\"display:none;\">"+mycommenter+" <span class=\"supergroup_small_text\"><span class=\"timeago\" id=\""+now+"\">"+hours+":"+minutes+suffix+" "+minas+"</span></span><br />"+decodeURIComponent(commentname)+"<a style=\"float:right;\" href=\"javascript:delete_comment("+data+")\" title=\"remove this comment\"><img style=\"padding-left:3px;\" src=\""+dirname+"/components/com_supergroups/images/cancel.png\" width=\"10\" /></a></td></tr></table></div>";
                          document.getElementById('commentsdiv-'+commentid).innerHTML = newerdiv+olderdiv;
                        } else {
                          document.getElementById('commentsdiv-'+commentid).innerHTML += "<div class=\"comments_div\" id=\""+data+"\" style=\"display:none;\">"+mycommenter+" <span class=\"supergroup_small_text\"><span class=\"timeago\" id=\""+now+"\">"+hours+":"+minutes+suffix+" "+minas+"</span></span><br />"+decodeURIComponent(commentname)+"<a style=\"float:right;\" href=\"javascript:delete_comment("+data+")\" title=\"remove this comment\"><img style=\"padding-left:3px;\" src=\""+dirname+"/components/com_supergroups/images/cancel.png\" width=\"10\" /></a></td></tr></table></div>";
                        }
                          //$('commentsdiv-'+commentid).htmlText = $('commentsdiv-'+commentid).text;
                          document.getElementById(data).style.display = 'block';
                  // Re-initialize the comment-box for next comment...
                          document.getElementById(field).style.display = 'none';
                          document.getElementById(button).style.display = 'none';
                          document.getElementById(inputdiv).style.fontSize = "10px";
                          document.getElementById(inputdiv).style.color = "#999999";
                          document.getElementById(inputdiv).value = superaddon_Write;
                          document.getElementById(inputdiv).style.display = 'block';
                  }
//==============================================================================
                if (resp=='comment_update')
                  {
                  var enable_wall = document.getElementById('enable_wall').value;
                  var data = xmlhttp.responseText;
                  var already = document.getElementById('hidden-div').value;           
                  var appendtocomment = unescape(decodeURIComponent(data));
                  if (already != appendtocomment) {
                    document.getElementById('hidden-div').value = appendtocomment;               
                    var data = appendtocomment.split('-epomeno-');
                    var comment = data[0].split('-sxolia-');
                    var actid = data[1].split(','); 
                    var newtime = data[2];
                    var commentsid = data[3].split(','); 
                    if (newtime!='') {document.getElementById('pageopened').value = newtime;}
                    for(i=0;i<actid.length;i++) {
                      if (enable_wall==1) {
                        var olderdiv = document.getElementById('commentsdiv-'+actid[i]).innerHTML;
                        document.getElementById('commentsdiv-'+actid[i]).innerHTML = comment[i]+olderdiv; //alert(commentsid[i]);
                      } else {
                        document.getElementById('commentsdiv-'+actid[i]).innerHTML += comment[i]; //alert(commentsid[i]);
                      }
                        if (commentsid!='') document.getElementById(commentsid[i]).style.display = 'block';
                    }
                  }
                  setTimeout(commentUpdate, 15000);
                }
//==============================================================================              
                if (resp=='delete_comment')
                  {
                  // Re-initialize the comment-box for next comment...
                          document.getElementById(commentid).style.display = 'none';
                  }
//==============================================================================              
            //      document.getElementById('A1').innerHTML=xmlhttp.status;
              //    document.getElementById('A2').innerHTML=xmlhttp.statusText;
                //  document.getElementById('A3').innerHTML=xmlhttp.responseText;
                }
              else
                {
                alert("Problem retrieving XML data:" + xmlhttp.statusText);// + "|" + resp + "|" + url+'?myid='+myid+'&commentid='+commentid+'&commentname='+preparedcomment+'&dirname='+dirname);
                }
              }
            };
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

window.onload = commentUpdate;

	function commentUpdate(){
     var dirname = document.getElementById('dirname').value;
     var url = dirname+'/components/com_supergroups/helpers/commentupdate.php';

           var updatedata= document.getElementById('updatedata').value;
           var appendarray= encodeURIComponent(document.getElementById('appendarray').value);
           var pageopened= encodeURIComponent(document.getElementById('pageopened').value);

    loadXMLDoc(url+'?updatedata='+updatedata+'&appendarray='+appendarray+'&table='+encodeURIComponent(document.getElementById('table').value)+'&pageopened='+pageopened, 'comment_update', 0, 0, 0, dirname);
    //myRequest.send('updatedata='+updatedata+'&appendarray='+appendarray+'&pageopened='+pageopened);
  }
// END OF LOADING PAGE FUNCTIONS
// ***************************************************************************************

  function get_commentid(id){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var superaddon_Login = document.getElementById('superaddon_Login').value;
      var superaddon_Write = document.getElementById('superaddon_Write').value;
      var url = dirname+'/components/com_supergroups/helpers/preparecomment.php';
      var commentname = encodeURIComponent(document.getElementById('comment-name'+commentid).value);
  
      var field = 'mydiv-'+commentid;
      var button= 'buttondiv-'+commentid;
      var inputdiv = 'comment-name'+commentid;
      var login = 'login'+commentid;
      
  // if not logged in user...
/*      if (commentname != superaddon_Login) {
        $(login).style.display = 'none';
      }*/
      
  // if no comment yet...
      if (commentname=='') {
        var browser=navigator.appName;
        var commentshow = document.getElementById('commentshow-'+commentid).value;
        document.getElementById(field).style.display = 'none';
        document.getElementById(button).style.display = "none"; // let's go for this instead
        document.getElementById(inputdiv).style.fontSize = "10px";
        document.getElementById(inputdiv).style.color = "#999999";
        document.getElementById(inputdiv).value = superaddon_Write;
        //alert (document.activeElement.focus);
        if (commentshow=="block") {
          document.getElementById(inputdiv).style.display = 'block';
          if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "none";
        } else {
          document.getElementById(inputdiv).style.display = 'none';
          if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "block";
        }
      } else {
        var pressed = document.getElementById('button-pressed'+commentid).value;
  
  //if button clicked...      
        if (pressed=='PRESSED') {
  
  // prepare elements...
          document.getElementById('button-pressed'+commentid).value = "button-pressed"+commentid;    
  
  // make the AJAX call...
            var myid = document.getElementById('myid').value;

          loadXMLDoc(url+'?commentname='+commentname, 'prepare_comment', myid, commentid, commentname, dirname);
          //myRequest.send('myid='+myid+'&commentid='+commentid+'&commentname='+commentname);

        }
      }
  }

  function showfield(id){
      var browser=navigator.appName;
      var commentid = id;
      var superaddon_Login = document.getElementById('superaddon_Login').value;
      var superaddon_Write = document.getElementById('superaddon_Write').value;
      var commentname = document.getElementById('comment-name'+commentid).value;
      var field = 'mydiv-'+commentid;
      var button= 'buttondiv-'+commentid;
      var inputdiv = 'comment-name'+commentid;
      var login = 'login'+commentid;
      if (document.getElementById(inputdiv).style.display=='none') {
        if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "block";
        if (commentname != superaddon_Login) {
          document.getElementById(field).style.display = 'block';
          document.getElementById(button).style.display = 'block';
          document.getElementById(inputdiv).style.fontSize = "14px";
          document.getElementById(inputdiv).style.color = "#000000";
          document.getElementById(inputdiv).value = "";
          document.getElementById(inputdiv).style.display = 'block';
          var xx = document.getElementById(inputdiv).focus();
        } else {
          document.getElementById(login).style.display = 'block';
    // calling an anonymous function that calls the function you need with your desired parameters! SWEEEET, but head-banger to figure out!
        	setTimeout(function(){get_commentid(commentid)}, 4000);
        }
      } else {
        if (commentname == superaddon_Write) {
          if (browser=="Microsoft Internet Explorer") document.getElementById('commentshow-'+commentid).value = "block";
          document.getElementById(field).style.display = 'block';
          document.getElementById(button).style.display = 'block';
          document.getElementById(inputdiv).style.fontSize = "14px";
          document.getElementById(inputdiv).style.color = "#000000";
          document.getElementById(inputdiv).value = "";
          document.getElementById(inputdiv).style.display = 'block';
          var xx = document.getElementById(inputdiv).focus();
        }
      }
  }
  
  function showmore(id){
      var commentid = id;
      var more = 'more'+commentid;
      var show = 'show'+commentid;
      var hide = 'hidecomments'+commentid;
      document.getElementById(show).style.display = 'none';
      document.getElementById(more).style.display = 'block';
      document.getElementById(hide).style.display = 'block';
  }
  function hidemore(id){
      var commentid = id;
      var more = 'more'+commentid;
      var show = 'show'+commentid;
      var hide = 'hidecomments'+commentid;
      document.getElementById(hide).style.display = 'none';
      document.getElementById(more).style.display = 'none';
      document.getElementById(show).style.display = 'block';
  }
  
  function delete_comment(id){
      var dirname = document.getElementById('dirname').value;
      var commentid = id;
      var url = dirname+'/components/com_supergroups/helpers/deletecomment.php';
          
        loadXMLDoc(url+'?commentid='+commentid+'&table='+encodeURIComponent(document.getElementById('table').value), 'delete_comment', 0, commentid, 0, dirname);
        //myRequest.send('commentid='+commentid);
  }
  
  function get_ready(id){
      var commentid = id;
      var field = 'mydiv-'+commentid;
          document.getElementById(field).style.display = 'none';
      var imagediv = 'imagediv-'+commentid;
      var inputdiv = 'button-pressed'+commentid;
      document.getElementById(inputdiv).value = "PRESSED";
      get_commentid(commentid);
  }
  
  function GetDay(intDay){
      var DayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
                           "Thursday", "Friday", "Saturday");
      return DayArray[intDay];
  }
  
  function GetMonth(intMonth){
      var MonthArray = new Array("January", "February", "March",
                                 "April", "May", "June",
                                 "July", "August", "September",
                                 "October", "November", "December"); 
      return MonthArray[intMonth]; 	  	 
  }
  
  function getDateStrWithDOW(){
      var today = new Date();
      var year = today.getYear();
      if(year<1000) year+=1900;
      var todayStr = GetMonth(today.getMonth()) + " " + today.getDate();
      todayStr +=  ", " + GetDay(today.getDay());
      return todayStr;
  }

function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}

   function sleepy(naptime){
      naptime = naptime * 1000;
      var sleeping = true;
      var now = new Date();
      var alarm;
      var startingMSeconds = now.getTime();
      //alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
      while(sleeping){
         alarm = new Date();
         alarmMSeconds = alarm.getTime();
         if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
      }      
      //alert("Wakeup!");
   } 

function focusimage(id) {
  var image = document.getElementById('remove1'+id).innerHTML;
  document.getElementById('removeimage'+id).src = image;
}
function blurimage(id) {
  var image = document.getElementById('remove0'+id).innerHTML;
  document.getElementById('removeimage'+id).src = image;
}

function rotatepage(page) {
  var totalpages = document.getElementById('pagecounter').innerHTML;
  document.getElementById('pagelimitlink'+(page-1)).style.display = 'inline';
  document.getElementById('pagelimitdiv'+(page-1)).style.display = 'none';
  document.getElementById('page'+(page-1)).style.display = 'none';
  if (page>totalpages) page=1;
  document.getElementById('page'+page).style.display = 'inline';
}

function groupsperpage(page) {
  document.getElementById('pagelimitlink'+page).style.display = 'none';
  document.getElementById('pagelimitdiv'+page).style.display = 'inline';
  document.getElementById('pagelimit'+page).focus();
}

function confirm_it(url) {
  var confirm_text = document.getElementById('confirm_text').innerHTML;
  var answer = confirm(confirm_text);
  if (answer)
    window.location=url;
}

