/******************************************************************************
 * cshout.js
 * ----------------------------------------------------------------------------
 * DO NOT MODIFY OR REMOVE THIS COPYRIGHT SECTION
 * ----------------------------------------------------------------------------
 * Author       : Tien D. Tran (http://coolersport.info)
 * Copyright    : 2006 (c) Tien D. Tran
 * Application  : CShout
 * Version      : 2.0
 * Date Started : 2005/01/01
 * Last Modified: 2006/09/28 15:02:16
 *
 * This is a shoubox written in php and uses text file as database
 *
 * INSTALLATION:
 *             View installation.html
 * HOW TO USE THE SHOUTBOX:
 *             After installing successfully, click on ? button on the shoutbox
 *
 * Visit http://coolersport.info for any update of this shoutbox
 *
 ******************************************************************************/

function CShout() {
   /********************** CONFIGURATIONS **********************/
   this.url = '/cshout/cshout.php';
   this.inputentry = 'top'; // 'top' or 'bottom'
   this.width = 151;
   this.height = 300;
   this.layout = 0; // 0 or 1
   this.shouterlength = 12;
   this.adminColor = ['#415274', '#AA0000'];
   this.buttonColor = ['#000033', '#84D7FF'];
   /******************* END OF CONFIGURATIONS *******************/

   /********** DO NOT MODIFY THE CODE BELOW THIS LINE ***********/
   /****************** UNLESS YOU UNDERSTAND IT *****************/
   this.cache = new Array();
   this.instance = 0;
   this.isProcessing = false;
   this.page = 1;
   this.pages = 1;
   this.smileys = null;
   this.ajaxcs = ajaxCS.createInstance();
}

/*
 * Show shoutbox form entry
 */
CShout.prototype.showEntry = function() {
   document.write('<div class="sb_formarea">');
      document.write('<input type="button" id="shoutnum" class="sb_numofshouts" value="" >');
   document.write('<input type="text" id="name" size="16" value="Anonymous" maxlength="16" title="Name" class="sb_inputs" onkeydown="if(event.keyCode==13) document.getElementById(\'shout\').focus();" onfocus="if(this.value==\'Anonymous\') this.value=\'\';" onblur="if(this.value==\'\') this.value=\'Anonymous\';"><br>');
   
   document.write('<input type="text" id="shout" size="64" value="Message" maxlength="64" title="Message" class="sb_inputs" onkeydown="if(event.keyCode==13) cshout.shoutIt();" onfocus="if(this.value==\'Message\') this.value=\'\';" onblur="if(this.value==\'\') this.value=\'Message\';">');
   
   document.write('<input type="button" id="sb_show_smileys" title="Show smileys panel" value=":)" class="sb_buttons" onclick="cshout.showPanel(\'sb_smileylist\');">');
   document.write('<input type="button" id="shoutit" title="Send message" value=":::Send:::" class="sb_buttons" onclick="cshout.shoutIt();"><input type="button" title="Help" value="?" class="sb_buttons" onclick="cshout.showPanel(\'sb_help\');">');

   document.write('</div>');

   document.write('<div id="sb_smileylist" class="sb_panel"></div>');
   document.write('<div id="sb_help" class="sb_panel" style="text-align:left;">');
   document.write('<div id="sb_helpcaption">Make a shout</div>');
   document.write('<div id="sb_helptext">Enter your name or lieve Anonymous in the first text box and your message in the second one then click <b style="white-space:nowrap">:::Send:::</b> button.</div>');
   document.write('<div id="sb_helpcaption">Insert emoticons</div>');
   document.write('<div id="sb_helptext">Place the cursor in the second text box where you want to insert an emoticon then click <b>:)</b> button and click on appropriate icon.</div>');
   document.write('</div>');
   document.write('<div class="sb_formarea" style="color:transparent;font-size:0pt;clear:both;padding-top:5px;height:3px;overflow:hidden;">CShout</div>');
}

/*
 * Toggle panels
 */
CShout.prototype.showPanel = function(id) {
   if (document.getElementById(id).style.display=='block') {
      document.getElementById(id).style.display = 'none';
   } else{ 
      document.getElementById('sb_smileylist').style.display = 'none';
      document.getElementById('sb_help').style.display = 'none';
      document.getElementById(id).style.display = 'block';
   }
}

/*
 * Show the shoutbox on the page
 */
CShout.prototype.show = function() {
   if (this.instance>0) return;
   if (this.ajaxcs.httpObj) {
      if (this.layout<0 || this.layout>1) this.layout = 0;
      var cstyle = ['',' style="width:'+this.width+'px;height:'+this.height+'px;overflow:auto;overflow-x:hidden;"', ' style="width:'+this.width+'px;overflow:auto;overflow-x:hidden;"'];
      document.write('<div style="width:'+this.width+'px;margin:0;padding:0;overflow-x:hidden;">');
      document.write('<div class="sb_shoutbox"'+cstyle[this.layout+1]+'>');
      if (this.inputentry=='top') this.showEntry();
      document.write('<div id="sb_shouts" class="shouts"'+cstyle[this.layout]+'></div>');
      if (this.inputentry=='bottom') this.showEntry();
      document.write('</div>');
      document.write('</div>');
      this.getSmileys();
      this.getPageContent(1);
      this.instance++;
   } else {
      var html = document.getElementById('nojs').innerHTML;
      html = html.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
      document.write(html);
      document.getElementById('nojsmessage').style.display = 'none';
   }
}

/*
 * Update page navigation panel
 */
CShout.prototype.updatePages = function() {
   var str = '';
   for (var i=1; i<this.pages+1; i++) {
      if(i==this.page)
         str += '<span>'+i+'</span> ';
      else
         str += '<a href="" onclick="cshout.getPageContent('+i+');return(false);">'+i+'</a> ';
   }
//   document.getElementById('sb_pagenav').innerHTML = str;
}

/*
 * Check for valid number
 */
CShout.prototype.isNumeric = function(val) {
   var numChars = "0123456789";
   var retVal = true;
   if (val == "") {
      return false;
   }
   for (i = 0; i < val.length && retVal == true; i++) {
      if (numChars.indexOf(val.charAt(i)) < 0) {
         retVal = false;
      }
   }
   return retVal;
}

/*
 * Show loading progress
 */
CShout.prototype.showProgress = function(status) {
   var disableit = false;
   if (status) {
      document.getElementById('shoutit').value = status;
      document.getElementById('shoutit').style.color = this.buttonColor[1];
      disableit = true;
   } else {
      document.getElementById('shoutit').value = ':::Send:::';
      document.getElementById('shoutit').style.color = this.buttonColor[0];
   }
   ids = ['shoutit','sb_show_smileys'];
   for (i=0; i<ids.length; i++) {
      document.getElementById(ids[i]).disabled = disableit;
   }
}

/*
 * Load a page from the server
 */
CShout.prototype.getPageContent = function(p) {
   if (this.isNumeric(p)){
      this.page = p;
   } else if (p=='prev') {
      this.page--;
   } else {
      this.page++;
   }
   if (this.page < 1) {
      this.page = 1;
   } else if (this.page > this.pages) {
      this.page = this.pages;
   }
   this.showProgress('Loading...');
   this.ajaxcs.load('get', this.url+'?page='+this.page, null, this.handleContent);
}

/*
 * Process data loaded from the server
 */
CShout.prototype.handleContent = function(xmldoc) {
   try {
      var ferror = xmldoc.documentElement.getElementsByTagName('fe'); // fatal error
      if (ferror.length==0) {
         var error = xmldoc.documentElement.getElementsByTagName('e');
         var shouts = xmldoc.documentElement.getElementsByTagName('s');
		 //var snum = xmldoc.documentElement.getElementsByTagName('snum');
         var info = xmldoc.documentElement.getElementsByTagName('info')[0];
         var isadmin = (info.getAttribute('islogged')=='1')?true:false;
         var buttons = '';
         var rowidx = 0;
         var html = '';

         for (i=0; i<error.length; i++) {
            html += '<div id="sb_error">'+error[i].childNodes[0].nodeValue+'</div>';
         }
		 
         cshout.pages = parseInt(info.getAttribute('pages'));
		 var sn = info.getAttribute('sn');
//         document.getElementById('sb_pageno').innerHTML = cshout.page;
         document.getElementById('shoutnum').value = "Sent shouts: "+sn;
		 document.getElementById('shoutit').value = status;
         for (i=0; i<shouts.length; i++) {
            k = shouts[i].getAttribute('k');
            n = shouts[i].getAttribute('n');
            d = shouts[i].getAttribute('d');
            t = shouts[i].getAttribute('t');
            ip = shouts[i].getAttribute('ip');
            s = shouts[i].childNodes[0].nodeValue;
            html += '<div class="sb_shouter">'+buttons+n;
            html += '<a href="#" onclick="alert(\'Shouter: '+n.unquote()+'\\nWhen: '+t+' '+d+'\');return(false)">on '+d.substring(0,5)+' at '+t.substring(0,5)+' GMT '+'</a></div>';
            html += '<div class="sb_row'+rowidx+'" title="'+t+' '+d+' '+ip+'">'+s+'</div>';
            rowidx=(rowidx==0)?1:0;
         }
         document.getElementById('sb_shouts').innerHTML = html;
         cshout.updatePages();
      } else {
         alert(ferror[0].childNodes[0].nodeValue);
      }
      cshout.showProgress();
   } catch(e) {
      alert('Error occurred while processing data.\nError message:\n'+e.message+'\n'+xmldoc);
   }
}

/*
 * Get and print smileys panel
 */
CShout.prototype.getSmileys = function() {
   document.getElementById('sb_smileylist').innerHTML = '<div style="text-align:center;margin-top:10px;">Loading smileys...</div>';
   this.ajaxcs.load('get', this.url+'?act=smileys', null, function(xmldoc) {
      try {
         if (!xmldoc) return;
         var xmlsmileys = xmldoc.documentElement.getElementsByTagName('sm');
         var html = '';
         cshout.smileys = new Array();
         for (i=0; i<xmlsmileys.length; i++) {
            src = xmlsmileys[i].getAttribute('s');
            text = xmlsmileys[i].getAttribute('t').replace(/\'/,'\\u0027').replace(/\"/,'\\u0022');
            if (src) {
               str = '<div title="'+text+'" class="sb_smiley" style="background-image:url('+src+');" onclick="cshout.addSmiley(\''+text+'\');"></div>';
            } else {
               str = '<span title="'+text+'" onclick="cshout.addSmiley(\''+text+'\');">'+text+'</span> ';
            }
            html += str;
            cshout.smileys.push(new Array(text, str));
         }
         document.getElementById('sb_smileylist').innerHTML = '<div><table align="center" border="0"><tr><td>'+html+'</td></tr></table></div>';
      } catch(e) {
         alert('Error occurred while getting smileys.\nError message:\n'+e.message+'\n'+xmldoc);
      }
   });
}

/*
 * Submit a shout
 */
CShout.prototype.shoutIt = function() {
   cshout.showProgress('Shouting...');
   this.ajaxcs.load('get', this.url+'?act=shout&name='+Escape(document.getElementById('name').value)+'&shout='+Escape(document.getElementById('shout').value), null, this.handleContent);
}

/*
 * Add a smiley into current message
 */
CShout.prototype.addSmiley = function(s) {
   if (document.getElementById('shout').value=='Message')
      document.getElementById('shout').value = s;
   else
      document.getElementById('shout').value += s;
}

/*
 * Escape a string before submitting
 */
function Escape(s) {
   return escape(s).replace(/\+/,'%2B');
}

/*
 * Singleton instance
 */
cshout = new CShout();
