// Core JavaScript methods


var undefined = undefined  ||  (typeof void 0);




var core =
{




css: function
(
	url
, attr
)
		{
	
		for ( var s = 0;  s < document.styleSheets.length;  s++ )
			if ( document.styleSheets[s].href  &&  document.styleSheets[s].href.indexOf( url ) > -1 )	return;	// stylesheet already loaded

		var css = document.createElement( 'link' );
		css.rel = 'stylesheet';
		css.type = 'text/css';
		css.href = url;

		for ( var a in attr )
			css[a] = attr[a];

		document.getElementsByTagName('head')[0].appendChild( css );
		}	// End method; loadCSS





, trim: function
(
  str
)
		{
		return	( (typeof str) == 'undefined' || str == null  ?  ''  :  str.replace( /^\s+/, '' ).replace( /\s+$/, '' )  );
		}




, zFill: function 
(
  val
, fill
)
		{
		var zip = '000000000000000000000000000000' + val;

		return	zip.substring( zip.length-fill, zip.length );
		}	// End method; zFill( val, siz )





// Why this isn't in Math....
, sign: function
(
  value
)
		{
		if ( value < 0 )	return	-1;
		if ( value > 0 )	return 1;

		return	0;
		}	// End method; sign()






, script: function
( 
	url
, type
)
		{
		document.writeln( '<' + 'script type="' + ( (typeof type) != 'undefined'  ?  type  :  'text/javascript' ) + '" src="' + url + '"></script>' );
		}	// End method; script( url [, type] )




// JSON: JavaScript Object Notation
, json: function
( 
	url
, node
)
		{
		node = ( (typeof node) != 'undefined'  ?  node  :  document.getElementsByTagName('head')[0] );

		var scriptID = 'coreJSON_' + (new Date().getTime());
		var script = document.createElement( 'script' );
		script.id = scriptID;
		script.type = 'text/javascript';
		script.src = url;

		node.appendChild( script );

		return		script;
		}	// End method; json

, asynchJSON: this.json	// backward compatibility




, jsonEnc: function 
(
  itm
)
		{
		if ( itm == null )	return	null;
		var iType = (typeof itm);
		var json;
		var val;

		if ( itm instanceof Array )
			{
			json = '[';

			for ( var x = 0;  x < itm.length;  x++ )
				{
				val = this.jsonEnc( itm[x] );
				json += ( x > 0  ?  ','  :  '' ) + ( val != null  ?  val  :  'null' );
				}
			json += ']';
			}

		else if ( (typeof itm) == 'object' )
			{
			json = '{';

			for ( var attr in itm )
				{
				val = this.jsonEnc( itm[attr] );
				json += ( json != '{'  ?  ','  :  '' ) + attr + ':' + ( val != null  ?  val  :  'null' );
				}

			json += '}';
			}

		else if ( iType == 'function' )
			json = null;

		else if ( iType == 'string' )
			json = '\'' + itm.replace( /\'/g, "\\'" ) + '\'';

		else
				json = ''+itm;


		return	json;
		}	// End method; jsonEnc




, cookie: function
(
  name
, opts
)
		{
		this.name = name;
		this.EMPTY = '';
		this.EMPTY_JAR = {};

		this.DEFAULTS = { document: window.document,  path: '/',  domain: window.location.hostname,  secure: false };

		this.opts = ( (typeof opts) != undefined  &&  opts != null  ?  opts  :  this.DEFAULTS );
		this.opts.document = ( (typeof this.opts.document) != undefined  ?  this.opts.document  :  this.DEFAULTS.document );	// MUST have a document

		// remaining options (path, domain, secure) must be set explicitly by application code.

		}	// End 'constructor' cookie

}	// End object; core




core.cookie.prototype.store = function
(
  obj
)
		{
		if ( obj != null )
			{
			this.opts.document.cookie = this.name + '=' + escape( core.jsonEnc( obj ) )
				+  ( (typeof this.opts.expiry) != undefined  ?  '; expires=' + this.opts.expiry.toGMTString()  :  this.EMPTY )
				+  ( (typeof this.opts.path) != undefined  ?  '; path=' + this.opts.path  :  this.EMPTY )
				+  ( (typeof this.opts.domain) != undefined  ?  '; domain=' + this.opts.domain  :  this.EMPTY )
				+  ( (typeof this.opts.secure) != undefined  &&  this.opts.secure  ?  '; secure'  :  this.EMPTY )
				;
			}
		else
			{		// null object means force the cookie to expire
			this.opts.document.cookie = this.name + '='
				+  ( (typeof this.opts.path) != undefined  ?  '; path=' + this.opts.path  :  this.EMPTY )
				+  ( (typeof this.opts.domain) != undefined  ?  '; domain=' + this.opts.domain  :  this.EMPTY )
				+ '; expires=Fri, 02-Jan-1970 00:00:00 GMT'
				;
			}

		}	// End method; core.cookie.store




core.cookie.prototype.value = function ()
		{
		var cookies = this.opts.document.cookie.split( ';' );
		var nm = this.name + '=';

		for ( var c = 0;  c < cookies.length;  c++ )
			if ( cookies[c].indexOf( nm ) == 0 )
				{
				eval( 'var _ = ' +unescape( cookies[c].split( '=' )[1] ) );
				return	_;
				}

		return	this.EMPTY_JAR;
		}	// End method; core.cookie.value








