/******************************************************************************
 * 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 = base + 'cshout/cshout.php';
   this.inputentry = 'bottom'; // 'top' or 'bottom'
   this.width = 270;
   this.height = 205;
   this.layout = 0; // 0 or 1
   this.shouterlength = 30;
   this.adminColor = ['#000000', '#AA0000'];
   this.buttonColor = ['#000000', '#808080'];
   /******************* END OF CONFIGURATIONS *******************/

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

/*
 * Create an instance of XMLHttpRequest object
 */
CShout.prototype.createHttpRequestObject = function() {
   var httpObj;
   if(!document.all || navigator.appName == "Netscape" || navigator.appName == "Opera") {
      httpObj = new XMLHttpRequest();
   } else {
      var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','Microsoft.XMLHTTP','MSXML2.XMLHTTP');
      for (var i=0; i<XmlHttpVersions.length && !httpObj; i++) {
         try {
            httpObj = new ActiveXObject(XmlHttpVersions[i]);
         } catch (e) {}
      }
   }
   if (!httpObj) {
   	//alert("Error creating the XMLHttpRequest object. Your browser may not support this application.\nOld version of CShout will be used instead.");
   } else {
   	return httpObj;
   }
}

/*
 * Wrapper to make AJAX requests
 */
CShout.prototype.CDownloadUrl = function() {
   if (!this.httpObj) return;
   if (this.cache.length<4) {
      this.isProcessing = false;
      return;
   } else {
      this.isProcessing = true;
   }
   var method = this.cache.shift();
   var url = this.cache.shift();
   var parameters = this.cache.shift();
   var func = this.cache.shift();
   var ajaxvar = this.ajaxvar;
   this.httpObj.onreadystatechange = function() {
      if(cshout.httpObj.readyState == 4){
         if (cshout.httpObj.status == 200) {
            try {
               var contenttype = cshout.httpObj.getResponseHeader('Content-Type');
               if (contenttype.indexOf('xml')>-1) {
                  func(cshout.httpObj.responseXML);
				  if (ajaxvar != 'AJAX') {
				  	document.getElementById('shout').value = '';
				  }
               } else {
                  func(cshout.httpObj.responseText);
               }
            } catch(e) {
               //alert('Error retrieving response data: ' + e.message);
            }
         } else {
            func('Error: '+cshout.httpObj.status);
         }
         if (cshout.cache.length<4) {
            cshout.isProcessing = false;
         } else {
            cshout.isProcessing = true;
            setTimeout('cshout.CDownloadUrl()', 100);
         }
      }
   };
   try {
      this.httpObj.open(method, url, true);
      if (parameters!=null) {
         this.httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         this.httpObj.setRequestHeader("Content-length", parameters.length);
         this.httpObj.setRequestHeader("Connection", "close");
         this.httpObj.send(parameters);
      } else {
         this.httpObj.send(null);
      }
   } catch(e) {
      //alert('Error connecting to server: ' + e.message);
   }
}

/*
 * AJAX requests stack
 */
CShout.prototype.load = function(method, url, parameters, func) {
   this.cache.push(method, url, parameters, func);
   if (!this.isProcessing) this.CDownloadUrl();
}

/*
 * Show shoutbox form entry
 */
CShout.prototype.showEntry = function() {
   document.write('<div id="shoutboxantwort">');
   document.write('<textarea id="shout" maxlength="1024" style="width:246px;"></textarea><br>');
   document.write('<button id="shoutit" class="button" class="sb_buttons" onclick="cshout.shoutIt();"><img src="http://images.die-bundesmeister.de/bilder/buttons/' + this.locale + '/ab_in_die_shoutbox_damit.gif"></button>');
   document.write('</div>');

  // document.write('<div id="sb_pagenav" style="display:none;"></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_searchquery').style.display = 'none';
      document.getElementById('sb_loginform').style.display = 'none';
      //document.getElementById('sb_pagenav').style.display = 'none';
      document.getElementById('sb_help').style.display = 'none';
      document.getElementById(id).style.display = 'block';
   }
}

/*
 * Toggle login form's elements
 */
CShout.prototype.showLogin = function(isLogged) {
   var isIn = 'none', isOut = 'none';
   if (isLogged==1)
      isOut = 'inline';
   else
      isIn = 'inline';
   document.getElementById("u").style.display=isIn;
   document.getElementById("p").style.display=isIn;
   document.getElementById("login").style.display=isIn;
   document.getElementById("logout").style.display=isOut;
}

/*
 * Show the shoutbox on the page
 */
CShout.prototype.show = function() {
   if (this.instance>0) return;
   if (this.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>');
      document.write('<div class="sb_shoutbox"'+cstyle[this.layout]+'>');
      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';
   }
}

CShout.prototype.showAgain = function() {
   if (this.httpObj) {
   	  this.ajaxvar = 'AJAX';
      this.getPageContentDyn(1);
   } 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 (var 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 = /*unescape("%BB") + */ '>> ABSCHICKEN';
      document.getElementById('shoutit').style.color = this.buttonColor[0];
   }
   ids = ['shoutit'];
   for (var 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.load('get', this.url+'?page='+this.page, null, this.handleContent);
}
CShout.prototype.getPageContentDyn = function() {
   this.page = 1;
   this.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 info = xmldoc.documentElement.getElementsByTagName('info')[0];
         var isadmin = (info.getAttribute('islogged')=='1')?true:false;
         var buttons = '';
         var rowidx = 0;
         var html = '';

         for (var i=0; i<error.length; i++) {
            html += '<div id="sb_error">'+error[i].childNodes[0].nodeValue+'</div>';
         }
         cshout.pages = parseInt(info.getAttribute('pages'));
         for (var 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;
            if (isadmin) buttons = '<a class="sb_delete" href="" onclick="cshout.deleteIt(\''+k+'\');return(false);" title="Delete this shout">x</a><a class="sb_delete" href="" onclick="cshout.checkIP(\''+ip+'\');return(false);" title="Check ip location">ip</a>';
			html += '<div style="margin-bottom:10px; ">';
            if (n.length>cshout.shouterlength)
               html += '<span class="title">['+t.slice(0, 5)+'] <span class="fett"><a href="community/profil/benutzer/'+n+'" title="Zum Profil von '+n+'">'+buttons+n.substring(0,cshout.shouterlength-3)+'</a>:</span></span>: ';
            else
               html += '<span class="title" title="'+n+'">['+t.slice(0, 5)+'] <span class="fett"><a href="community/profil/benutzer/'+n+'" title="Zum Profil von '+n+'">'+buttons+n+'</a></span></span>: ';
            html += '<span class="sb_row sb_row'+rowidx+'" title="'+t+' '+d+'">'+s+'</span></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.load('get', this.url+'?act=smileys', null, function(xmldoc) {
      try {
         var xmlsmileys = xmldoc.documentElement.getElementsByTagName('sm');
         var html = '';
         cshout.smileys = new Array();
         for (var 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() {
   this.ajaxvar = null;
   if (document.getElementById('flashobj')) {
   	if(navigator.appVersion.indexOf("MSIE")>-1) {
   		document.getElementById('flashobj').focus();
	} else {
		document.getElementById('flashemb').focus();
	}
   }
   cshout.showProgress('Shouting...');
   this.load('get', this.url+'?act=shout&shout='+Escape(document.getElementById('shout').value), null, this.handleContent);
}

/*
 * Perform a search
 */
CShout.prototype.searchIt = function() {
   this.showProgress('Searching...');
   this.load('get', this.url+'?act=search&q='+Escape(document.getElementById('q').value), null, this.handleContent);
}

/*
 * Login procedure
 */
CShout.prototype.doLogin = function() {
   this.showProgress('Logging in...');
   this.load('post', this.url, 'act=login&u='+hex_sha1(document.getElementById('u').value)+'&p='+hex_sha1(document.getElementById('p').value), function(xmldoc) {
      document.getElementById('p').value = 'pass';
      var ferror = xmldoc.documentElement.getElementsByTagName('fe'); // fatal error
      if (ferror.length>0) {
         if (ferror[0].childNodes[0].nodeValue=='ok') {
            cshout.getPageContent(cshout.page);
            document.getElementById('sb_show_login').style.color = cshout.adminColor[0];
            cshout.showPanel('sb_loginform');
         } else {
            //alert(ferror[0].childNodes[0].nodeValue);
         }
      } else {
         //alert('Unexpected error.');
      }
      cshout.showProgress();
   });
}

/*
 * Logout procedure
 */
CShout.prototype.doLogout = function() {
   this.showProgress('Logging out...');
   this.load('post', this.url, 'act=logout', function(xmldoc) {
      var ferror = xmldoc.documentElement.getElementsByTagName('fe'); // fatal error
      if (ferror.length>0) {
         if (ferror[0].childNodes[0].nodeValue=='ok') {
            cshout.getPageContent(cshout.page);
            document.getElementById('sb_show_login').style.color = cshout.adminColor[1];
            cshout.showPanel('sb_loginform');
         } else {
            //alert(ferror[0].childNodes[0].nodeValue);
         }
      } else {
         //alert('Unexpected error.');
      }
      cshout.showProgress();
   });
}

/*
 * Delete a shout
 */
CShout.prototype.deleteIt = function(k) {
   this.showProgress('Deleting...');
   this.load('get', this.url+'?act=del&k='+k, null, function(data) {
      cshout.getPageContent(cshout.page);
      cshout.showProgress();
   });
}

/*
 * Check location of an ip address
 */
CShout.prototype.checkIP = function(ip) {
   window.open('http://www.geobytes.com/IpLocator.htm?GetLocation&ipaddress='+ip);
}

/*
 * Add a smiley into current message
 */
CShout.prototype.addSmiley = function(s) {
   if (document.getElementById('shout').value=='Nachricht')
      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();