//	Common Javascript - Copyright (C) 2003-2010 Charles A Upsdell, All Rights Reserved; www.upsdell.com


if ( typeof(Array.indexOf) !== 'function' )
{
	Array.prototype.indexOf = function ( value, i )
	{
	var rv = -1;
	if ( i === undefined )
		i = 0;
	for ( ; (i < this.length) && (this[i] != value); ++i )
		;
	if ( i < this.length )
		rv = i;
	return rv;
	} 
}

Array.prototype.random = function ()
{
if ( this.length > 0 )
	return this[ Math.floor( this.length * Math.random() ) ];
else
	return undefined;
} 

function cModule ()
{
this.aModule = new Array();
this.aDependency = new Array();
}

cModule.prototype.alert = function ()
{
var sMessage = '';
for ( var i = 0; i < this.aDependency.length; ++i )
  {
	for ( var j = 1; j < this.aDependency[i].length; ++j )
	  {
		if ( this.aModule.indexOf(this.aDependency[i][j]) == -1 )
			sMessage += 'Module ' + this.aDependency[i][0] + '.js needs ' + this.aDependency[i][j] + '.js\n';
	  }
  }
if ( sMessage.length > 0 )
	alert( sMessage );
}

cModule.prototype.has = function ( module_list )
{
for ( var i = 0; i < arguments.length; ++i )
	this.aModule.push( arguments[i] );
}

cModule.prototype.uses = function ( this_module, module_list )
{
this.has( this_module );
var i = this.aDependency.length;
this.aDependency[i] = new Array();
this.aDependency[i][0] = this_module;
for ( var j = 1; j < arguments.length; ++j )
	this.aDependency[i].push( arguments[j] );
}

String.prototype.alert = function ( method, message, arglist )
{
var errmsg = this.valueOf();
errmsg += (method == '') ? '()' : ('.' + method + '()');
errmsg += ' error: ' + message;
if ( message.indexOf('%0') == -1 )
  {
	for ( var i = 2; i < arguments.length; ++i )
		errmsg += ((i == 2 ) ? ': ' : ', ' ) + arguments[i];
  }
else
  {
	errmsg = errmsg.blend( Array.prototype.slice.call( arguments, 2 ) );
  }
alert( errmsg );
}


String.prototype.blend = function ( arglist )
{
var rv = this.valueOf();
var n = 0;
for ( var i = 0; i < arguments.length; ++i )
  {
	if ( arguments[i] instanceof Array )
	  {
		for ( var j = 0; j < arguments[i].length; ++j )
		  {
			var regex = RegExp( '%{1}' + n + '((?=\\D)|$)', 'g' );
			rv = rv.replace( regex, arguments[i][j] );
			++n;
		  }
	  }
	else
	  {
		var regex = RegExp( '%{1}' + n + '((?=\\D)|$)', 'g' );
		rv = rv.replace( regex, arguments[i] );
		++n;
	  }
  }
return rv;
}


function getElement ( id )
{
var rv = null;
if ( (arguments.length == 0) || (id == "") )
	return rv;
else if ( document.getElementById )
	rv = document.getElementById( id );
else if ( document.all )
	rv = document.all[id];
return rv;
}


function isInRange ( a, b, c )
{
return (a <= b) && (b <= c);
}


function isMobileDevice ( width )
{
width = width || 480;
return (typeof(window.innerWidth) != 'undefined') && (window.innerWidth <= width);
}


var aModule = new cModule();


