//var ajax_compatible = 1;
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);

function fetch_object(idname) {
 if (document.getElementById) {
  return document.getElementById(idname);
 } else if (document.all) {
  return document.all[idname];
 } else if (document.layers) {
  return document.layers[idname];
 } else {
  return null;
 }
}

function fetch_tags(parentobj, tag) {
 if (typeof parentobj.getElementsByTagName != 'undefined') {
  return parentobj.getElementsByTagName(tag);
 } else if (parentobj.all && parentobj.all.tags) {
  return parentobj.all.tags(tag);
 } else {
  return null;
 }
}

// ############
// AJAX_Handler
// ############

function AJAX_Handler_wBB(async) {
	this.async = async ? true : false;
}

AJAX_Handler_wBB.prototype.init = function() {
 try {
  this.handler = new XMLHttpRequest();
  return (this.handler.setRequestHeader ? true : false);
 }
 catch(e) {
  try {
   this.handler = new ActiveXObject('Microsoft.XMLHTTP');
   return true;
  }
  catch(e) {
   return false;
  }
 }
}

AJAX_Handler_wBB.prototype.is_compatible = function() {
 if (is_ie && !is_ie4) { return true; }
  else if (XMLHttpRequest) {
   try { return XMLHttpRequest.prototype.setRequestHeader ? true : false; }
   catch(e) {
    try { var tester = new XMLHttpRequest(); return tester.setRequestHeader ? true : false; }
     catch(e) { return false; }
    }
  }	else { return false; }
}

AJAX_Handler_wBB.prototype.not_ready = function() {
 return (this.handler.readyState && (this.handler.readyState < 4));
}

AJAX_Handler_wBB.prototype.onreadystatechange = function(event) {
 if (!this.handler)	{
  this.init();
 }
 if (typeof event == 'function') {
  this.handler.onreadystatechange = event;
 } else {
  alert('XML Sender OnReadyState event is not a function');
 }
}

AJAX_Handler_wBB.prototype.send = function(desturl, datastream) {
 if (!this.handler) {
  this.init();
 }
 if (!this.not_ready()) {
  this.handler.open('POST', desturl, this.async);
  this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  this.handler.send(datastream);

  if (!this.async && this.handler.readyState == 4 && this.handler.status == 200) {
   return true;
  }
 }
 return false;
}

// we can check this variable to see if browser is AJAX compatible
var ajax_compatible = AJAX_Handler_wBB.prototype.is_compatible();

// ##################
// PHP_Emulator class
// ##################
function PHP_Emulator_wBB() {
}

PHP_Emulator_wBB.prototype.stripos = function(haystack, needle, offset) {
 if (typeof offset == 'undefined') {
  offset = 0;
 }
 index = haystack.toLowerCase().indexOf(needle.toLowerCase(), offset);
 return (index == -1 ? false : index);
}

PHP_Emulator_wBB.prototype.ltrim = function(str) {
 return str.replace(/^\s+/g, '');
}

PHP_Emulator_wBB.prototype.rtrim = function(str) {
 return str.replace(/(\s+)$/g, '');
}

PHP_Emulator_wBB.prototype.trim = function(str) {
 return this.ltrim(this.rtrim(str));
}

PHP_Emulator_wBB.prototype.preg_quote = function(str) {
 // replace + { } ( ) [ ] | / ? ^ $ \ . = ! < > : * with backslash+character
 return str.replace(/(\+|\{|\}|\(|\)|\[|\]|\||\/|\?|\^|\$|\\|\.|\=|\!|\<|\>|\:|\*)/g, "\\$1");
}

PHP_Emulator_wBB.prototype.unhtmlspecialchars = function(str) {
 f = new Array(/&lt;/g, /&gt;/g, /&quot;/g, /&amp;/g);
 r = new Array('<', '>', '"', '&');
 for (var i in f) {
  str = str.replace(f[i], r[i]);
 }
 return str;
}

PHP_Emulator_wBB.prototype.htmlspecialchars = function(str) {
 //var f = new Array(/&(?!#[0-9]+;)/g, /</g, />/g, /"/g);
 var f = new Array(
  (is_mac && is_ie ? new RegExp('&', 'g') : new RegExp('&(?!#[0-9]+;)', 'g')),
  new RegExp('<', 'g'),
  new RegExp('>', 'g'),
  new RegExp('"', 'g')
 );
 var r = new Array(
  '&amp;',
  '&lt;',
  '&gt;',
  '&quot;'
 );

 for (var i = 0; i < f.length; i++) {
  str = str.replace(f[i], r[i]);
 }

 return str;
}

PHP_Emulator_wBB.prototype.in_array = function(ineedle, haystack, caseinsensitive) {
 var needle = new String(ineedle);

 if (caseinsensitive) {
  needle = needle.toLowerCase();
  for (var i in haystack) {
   if (haystack[i].toLowerCase() == needle) {
    return i;
   }
  }
 } else {
  for (var i in haystack) {
   if (haystack[i] == needle) {
    return i;
   }
  }
 }
 return -1;
}

PHP_Emulator_wBB.prototype.str_pad = function(text, length, padstring) {
 text = new String(text);
 padstring = new String(padstring);

 if (text.length < length) {
  padtext = new String(padstring);

  while (padtext.length < (length - text.length)) {
   padtext += padstring;
  }
  text = padtext.substr(0, (length - text.length)) + text;
 }
 return text;
}

PHP_Emulator_wBB.prototype.urlencode = function(text) {
 return escape(text).replace(/\+/g, "%2B");
}

PHP_Emulator_wBB.prototype.ucfirst = function(str, cutoff) {
 if (typeof cutoff != 'undefined') {
  var cutpos = str.indexOf(cutoff);
  if (cutpos > 0) {
   str = str.substr(0, cutpos);
  }
 }

 str = str.split(' ');
 for (var i = 0; i < str.length; i++) {
  str[i] = str[i].substr(0, 1).toUpperCase() + str[i].substr(1);
 }
	return str.join(' ');
}

var AJAX_PHP = new PHP_Emulator_wBB();

var wBB_DblClick_Timer = null;
var wBB_DblClick_Obj = null;
var wBB_DblClick_E = null;

function wBB_DblClick_Handler(obj, callback) {	
 if (typeof obj.onclick == 'function') {
  obj.singleclick = obj.onclick;
  obj.onclick = function(e) {			
   clearTimeout(wBB_DblClick_Timer);
   wBB_DblClick_Obj = this;
   wBB_DblClick_E = e;
   wBB_DblClick_Timer = setTimeout('wBB_DblClick_Obj.singleclick(wBB_DblClick_E)', 500);
   return false;
  };
 } else if (typeof obj.href != 'undefined') {
  obj.onclick = function(e) {
   clearTimeout(wBB_DblClick_Timer);
   wBB_DblClick_Timer = setTimeout("window.location = '" + this.href + "';", 500);
   return false;
  };
 }
 obj.dblclick_callback = callback;
 obj.ondblclick = function(e) {
  e = do_an_e(e);

  clearTimeout(wBB_DblClick_Timer);
  this.dblclick_callback(e);
		
  return false;
 }
}

function do_an_e(eventobj) {
 if (!eventobj || is_ie) {
  window.event.returnValue = false;
  window.event.cancelBubble = true;
  return window.event;
 } else {
  eventobj.stopPropagation();
  eventobj.preventDefault();
  return eventobj;
 }
}

