function imgFit (img, maxImgWidth)
{
	if (typeof(img.naturalWidth) == 'undefined')
	{
		img.naturalHeight = img.height;
		img.naturalWidth  = img.width;
	}

	if (img.width > maxImgWidth)
	{
		img.height = Math.round((maxImgWidth/img.width)*img.height);
		img.width  = maxImgWidth;
		img.title  = 'Click image to view full size';
		img.style.cursor = 'move';
		return false;
	}
	else if (img.width == maxImgWidth && img.width < img.naturalWidth)
	{
		img.height = img.naturalHeight;
		img.width  = img.naturalWidth;
		img.title  = 'Click to fit in the browser window';
		return false;
	}
	return true;
}

function imgFit_Onload ()
{
	var images = document.images;

	for (i=0, len=images.length; i<len; i++)
	{
		var id = images[i].getAttribute('id');
		var wd = images[i].width;

		if (id && wd)
		{
			if (id == 'postImg' && wd > postImg_MaxWidth)
			{
				imgFit(images[i], postImg_MaxWidth);
			}
			else if (id == 'postImgAligned' && wd > postImgAligned_MaxWidth)
			{
				imgFit(images[i], postImgAligned_MaxWidth);
			}
			else if (id == 'attachImg' && wd > attachImg_MaxWidth)
			{
				imgFit(images[i], attachImg_MaxWidth);
			}
		}
	}
}

function toggle_block (id)
{
	var el = document.getElementById(id);
	el.style.display = (el.style.display == 'none') ? '' : 'none';
}

function toggle_disabled (id, val)
{
	document.getElementById(id).disabled = (val) ? 0 : 1;
}

// Cookie functions [from http://www.netspade.com/ ??]
/**
 * Sets a Cookie with the given name and value.
 *
 * name	   Name of the cookie
 * value	  Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]	 Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *			  (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *			  secure transmission
 */
function setCookie (name, value, expires, path, domain, secure)
{
	document.cookie =
		name +'='+ escape(value)
	+	((expires) ? '; expires='+ expires.toGMTString() : '')
	+	((path) ? '; path='+ path : ((cookiePath) ? '; path='+ cookiePath : ''))
	+	((domain) ? '; domain='+ domain : ((cookieDomain) ? '; domain='+ cookieDomain : ''))
	+	((secure) ? '; secure' : ((cookieSecure) ? '; secure' : ''));
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie (name)
{
	var dc = document.cookie;
	var prefix = name + '=';
	var begin = dc.indexOf('; ' + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(';', begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name	  name of the cookie
 * [path]	path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie (name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + '=' +
			((path) ? '; path=' + path : '') +
			((domain) ? '; domain=' + domain : '') +
			'; expires=Thu, 01-Jan-70 00:00:01 GMT';
	}
}

/*
Simple Javascript Browser/OS detection (by Harald Hope, Tapio Markula, http://techpatterns.com)
Version 2.0.1
*/
var d, dom, ie, ie4, ie5x, moz, mac, win, lin, old, ie5mac, ie5xwin, op;

d = document;
n = navigator;
na = n.appVersion;
nua = n.userAgent;
win = ( na.indexOf( 'Win' ) != -1 );
mac = ( na.indexOf( 'Mac' ) != -1 );
lin = ( nua.indexOf( 'Linux' ) != -1 );

if ( !d.layers ){
	dom = ( d.getElementById );
	op = ( nua.indexOf( 'Opera' ) != -1 );
	konq = ( nua.indexOf( 'Konqueror' ) != -1 );
	saf = ( nua.indexOf( 'Safari' ) != -1 );
	moz = ( nua.indexOf( 'Gecko' ) != -1 && !saf && !konq);
	ie = ( d.all && !op );
	ie4 = ( ie && !dom );

	/*
	ie5x tests only for functionality. ( dom||ie5x ) would be default settings.
	Opera will register true in this test if set to identify as IE 5
	*/

	ie5x = ( d.all && dom );
	ie5mac = ( mac && ie5x );
	ie5xwin = ( win && ie5x );
}

// Copy text to clipboard. Originally got from decompiled `php_manual_en.chm`.
function copyText (from)
{
	if (!document.body.createTextRange) return false;
		BodyRange = document.body.createTextRange();
		if (!BodyRange.moveToElementText) return false;
		BodyRange.moveToElementText(from);
		if (!BodyRange.execCommand) return false;
		BodyRange.execCommand("Copy");
		return true;
}

/* Copyright (c) 2005 Scott S. McCoy
 * This was originally a non-object oriented interface
 * Function printf(format_string,arguments...)
 * Javascript emulation of the C printf function (modifiers and argument types
 *    "p" and "n" are not supported due to language restrictions)
 *
 * Copyright 2003 K&L Productions. All rights reserved
 * http://www.klproductions.com
 *
 * Terms of use: This function can be used free of charge IF this header is not
 *               modified and remains with the function code.
 *
 * Legal: Use this code at your own risk. K&L Productions assumes NO resposibility
 *        for anything.
 ********************************************************************************/

String.prototype.sprintf = function () {
  var fstring = this.toString();

  var pad = function(str,ch,len) { var ps='';
      for(var i=0; i<Math.abs(len); i++) {
		  ps+=ch;
	  }
      return len>0?str+ps:ps+str;
  };
  var processFlags = function(flags,width,rs,arg) {
      var pn = function(flags,arg,rs) {
          if(arg>=0) {
              if(flags.indexOf(' ')>=0) {
				  rs = ' ' + rs;
			  } else if(flags.indexOf('+')>=0) {
				  rs = '+' + rs;
			  }
          } else {
              rs = '-' + rs;
		  }
          return rs;
      };
      var iWidth = parseInt(width,10);
      if(width.charAt(0) == '0') {
          var ec=0;
          if(flags.indexOf(' ')>=0 || flags.indexOf('+')>=0) {
			  ec++;
		  }
          if(rs.length<(iWidth-ec)) {
			  rs = pad(rs,'0',rs.length-(iWidth-ec));
		  }
          return pn(flags,arg,rs);
      }
      rs = pn(flags,arg,rs);
      if(rs.length<iWidth) {
          if(flags.indexOf('-')<0) {
			  rs = pad(rs,' ',rs.length-iWidth);
		  } else {
			  rs = pad(rs,' ',iWidth - rs.length);
		  }
      }
      return rs;
  };
  var converters = [];
  converters.c = function(flags,width,precision,arg) {
      if (typeof(arg) == 'number') {
		  return String.fromCharCode(arg);
	  } else if (typeof(arg) == 'string') {
		  return arg.charAt(0);
	  } else {
		  return '';
	  }
  };
  converters.d = function(flags,width,precision,arg) {
      return converters.i(flags,width,precision,arg);
  };
  converters.u = function(flags,width,precision,arg) {
      return converters.i(flags,width,precision,Math.abs(arg));
  };
  converters.i =  function(flags,width,precision,arg) {
      var iPrecision=parseInt(precision, 10);
      var rs = ((Math.abs(arg)).toString().split('.'))[0];
      if(rs.length<iPrecision) {
		  rs=pad(rs,' ',iPrecision - rs.length);
	  }
      return processFlags(flags,width,rs,arg);
  };
  converters.E = function(flags,width,precision,arg) {
      return (converters.e(flags,width,precision,arg)).toUpperCase();
  };
  converters.e = function(flags,width,precision,arg) {
      iPrecision = parseInt(precision, 10);
      if(isNaN(iPrecision)) {
		  iPrecision = 6;
	  }
      rs = (Math.abs(arg)).toExponential(iPrecision);
      if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) {
		  rs = rs.replace(/^(.*)(e.*)$/,'$1.$2');
	  }
      return processFlags(flags,width,rs,arg);
  };
  converters.f = function(flags,width,precision,arg) {
      iPrecision = parseInt(precision, 10);
      if(isNaN(iPrecision)) {
		  iPrecision = 6;
	  }
      rs = (Math.abs(arg)).toFixed(iPrecision);
      if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) {
		  rs = rs + '.';
	  }
      return processFlags(flags,width,rs,arg);
  };
  converters.G = function(flags,width,precision,arg) {
      return (converters.g(flags,width,precision,arg)).toUpperCase();
  };
  converters.g = function(flags,width,precision,arg) {
      iPrecision = parseInt(precision, 10);
      absArg = Math.abs(arg);
      rse = absArg.toExponential();
      rsf = absArg.toFixed(6);
      if(!isNaN(iPrecision)) {
          rsep = absArg.toExponential(iPrecision);
          rse = rsep.length < rse.length ? rsep : rse;
          rsfp = absArg.toFixed(iPrecision);
          rsf = rsfp.length < rsf.length ? rsfp : rsf;
      }
      if(rse.indexOf('.')<0 && flags.indexOf('#')>=0) {
		  rse = rse.replace(/^(.*)(e.*)$/,'$1.$2');
	  }
      if(rsf.indexOf('.')<0 && flags.indexOf('#')>=0) {
		  rsf = rsf + '.';
	  }
      rs = rse.length<rsf.length ? rse : rsf;
      return processFlags(flags,width,rs,arg);
  };
  converters.o = function(flags,width,precision,arg) {
      var iPrecision=parseInt(precision, 10);
      var rs = Math.round(Math.abs(arg)).toString(8);
      if(rs.length<iPrecision) {
		  rs=pad(rs,' ',iPrecision - rs.length);
	  }
      if(flags.indexOf('#')>=0) {
		  rs='0'+rs;
	  }
      return processFlags(flags,width,rs,arg);
  };
  converters.X = function(flags,width,precision,arg) {
      return (converters.x(flags,width,precision,arg)).toUpperCase();
  };
  converters.x = function(flags,width,precision,arg) {
      var iPrecision=parseInt(precision, 10);
      arg = Math.abs(arg);
      var rs = Math.round(arg).toString(16);
      if(rs.length<iPrecision) {
		  rs=pad(rs,' ',iPrecision - rs.length);
	  }
      if(flags.indexOf('#')>=0) {
		  rs='0x'+rs;
	  }
      return processFlags(flags,width,rs,arg);
  };
  converters.s = function(flags,width,precision,arg) {
      var iPrecision=parseInt(precision, 10);
      var rs = arg;
      if(rs.length > iPrecision) {
		  rs = rs.substring(0,iPrecision);
	  }
      return processFlags(flags,width,rs,0);
  };

  farr = fstring.split('%');
  retstr = farr[0];
  fpRE = /^([-+ #]*)(?:(\d*)\$|)(\d*)\.?(\d*)([cdieEfFgGosuxX])(.*)$/;
  for(var i = 1; i<farr.length; i++) {
      fps=fpRE.exec(farr[i]);
      if(!fps) {
		  continue;
	  }
	  var my_i = fps[2] ? fps[2] : i;
      if(arguments[my_i-1]) {
          retstr+=converters[fps[5]](fps[1],fps[3],fps[4],arguments[my_i-1]);
      }
      retstr += fps[6];
  }
  return retstr;
};

