//<![CDATA[

"use strict";

//	Copyright: Well-connected Ltd (http://www.well-connected.net/), 2012. All rights reserved.

var a_images = new Array;

function addEvent ( o, e, f ) {
	if ( o.addEventListener ) {
		o.addEventListener ( e, f, true);
		return true;
	}
	else if ( o.attachEvent ) {
		return o.attachEvent ( "on" + e, f );
	}
	else {
		return false;
	}
}

function ltrim ( str, chars ) {
	chars = chars || "\\s";
	return str.replace ( new RegExp ( "^[" + chars + "]+", "g" ), "" );
}

function rtrim ( str, chars ) {
	chars = chars || "\\s";
	return str.replace ( new RegExp ( "[" + chars + "]+$", "g" ), "" );
}

function trim ( str, chars ) {
	return ltrim(rtrim(str, chars), chars);
}

function toggle_on_off ( obj )
{
	obj.value = ( obj.checked ? 1 : 0 );
}

function select_table ( s_url )
{
	var s_href = validate_input ( s_url );
	if ( s_href > " " )
	{
		location.href = s_href;
	}
}

function show_local_date ( i_days )
{
	var today = new Date ();
	if ( i_days > 0 )
		today.setDate ( today.getDay () + i_days );

	var s_date = today.toLocaleFormat ( "%A, %e %B %Y" );
	document.write ( s_date );
	return s_date;
}

function show_local_datetime ( i_seconds )
{
	var today = new Date ();
	if ( i_seconds > 0 )
		today.setSeconds ( today.getSeconds () + i_seconds );

	var s_date = today.toLocaleFormat ( "%A, %e %B %Y, %H:%M" );
	document.write ( s_date );
	return s_date;
}

function debug_object ( obj )
{
	var s_debug = "";
	for ( i = 0; i < obj.length; i++ )
	{
		var fld = obj[i];
		s_debug += " id:" + fld.id + "  value:" + fld.value + "\n";
	}
	alert ( "Action:\n" + obj.action + "\n\nFields:\n" + s_debug );
}

function resize_window ( w, i_width, i_height )
{
	if ( i_width > 0 )
	{
		if ( i_width > screen.availWidth )
			i_width = screen.availWidth;
	}
	else
		i_width = screen.availWidth;

	if ( i_height > 0 )
	{
		if ( i_height > screen.availHeight )
			i_height = screen.availHeight;
	}
	else
		i_height = screen.availHeight;

	w.moveTo ( 0, 0 );
	w.resizeTo ( i_width, i_height );
}

function open_window ( s_url, s_name, i_width, i_height, s_param )
{
	var s_options = "directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes";
	if ( s_param > " " )
		s_options += "," + s_param;	//	append parameters so they overwrite options

	s_url = s_url.replace( /\s+/gi, "+" );

	var w = window.open ( s_url, s_name, s_options );
	resize_window ( w, i_width, i_height );
	w.focus();
}

function preload_images ( a_filenames ) {
//	preload_images ( array ( "/alias/first.gif", "/alias/second.gif", "/alias/third.gif" ) );
	for ( var i = 0; i < a_filenames.length; i++ )	{
		a_images[i] = new Image();
		a_images[i].src = a_filenames[i];
	}
}

function open_image ( s_url, s_img_url, s_param )
{
	s_options = "directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes";

	img = new Image();
	img.src = s_img_url;
	s_options += ",width=" + ( img.width + 50 ) + ",height=" + ( img.height + 90 );

	if ( s_param > " " )
		s_options += "," + s_param;	//	append parameters to overwrite defaults

	window.open ( s_url + s_img_url, 'image', s_options );
}

function goto_location ( o_target, s_url )
{
	s_url = s_url.replace( /\s+/gi, "+" );
	o_target.location.href = s_url;
}

//]]>

