/*
 * Basic wrapper for WebGains code.  *ALL* pages should include, and ALL javascript should be a child of this object.
 */

// this is handy all over the place
String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}
// ensure we only ever do this once, no matter how many times we're included for whatever reason.
if (typeof(WEBGAINS) == "undefined")
{
	WEBGAINS = {};
	WG = WEBGAINS;
}

if (typeof(WG.GUI) == 'undefined')
{
	WG.GUI = {};
}


WG.GUI.displayError = function (error)
{
	log('Failure: ' + repr(error));
	log(repr(typeof(error)));
	var messageText = '';

	WG.GUI.lastError = error;
	switch (typeof(error))
	{
		case 'undefined':
			messageText = 'An error has occured.  Please refresh your browser and try again, or submit a support request.';
			break;

		case 'string':
			messageText = error;
			break; 

		default:
			if (typeof(error.message) != 'undefined')
			{
				messageText = error.message;
			}
			else
			{
				messageText = 'An error has occured.  Please refresh your browser and try again, or submit a support request.';
			}
			break;
	}

	var newMessage = MochiKit.DOM.P({'class':'errormessage'}, messageText);

    var errorDiv = $('webgains-error');
    if (errorDiv === null)
    {
        // we want to add an error div. 
		errorDiv = DIV({'class':'confirmationmessage', 'id':'webgains-error'});
		var main = $('main');
		if (main.hasChildNodes())
		{
			main.insertBefore(errorDiv, main.childNodes[0]);
		}
		else		// pathalogical case
		{
			main.appendChild(errorDiv);
		}
    }
	errorDiv.appendChild(newMessage);
}

WG.GUI.frameSize = function()
{
	// culled (legally) from the almighty QuirksMode: http://www.quirksmode.org/viewport/compatibility.html
	var x,y;
	if (window.innerHeight) // all except Explorer
	{
		x = window.innerWidth;
		y = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
}

WG.changeLang = function (elem)
{
	var locale = elem.options[elem.selectedIndex].value;
	var dest   = window.location;
	window.location = '/lc.html?locale='
					  +escape(locale)
					  +'&dest='+escape(dest);
	return false;
}

WG.parseFloat = function(val)
{
	val = val.replace(DECIMAL_POINT, 'DEC');
	val = val.replace(THOUSAND_SEPARATOR, '');
	return parseFloat(val.replace('DEC', '.'));
}

WG.stringToFloat = WG.parseFloat;

WG.numberFormat = function(val, decimals)
{
	if (decimals == undefined || decimals == 0) {
		return WG.thousands(Math.round(val));
	}

	// round to decimal places
	val = new String(val);
	val = val.replace(DECIMAL_POINT, '.');
	val = new String(Math.round(parseFloat(val) * Math.pow(10, decimals + 1)) / Math.pow(10, decimals + 1));

	// work out how many '0' need to add'
	if (val.indexOf('.') == -1) {
		val += '.';
	} else {
		decimals -= val.length - val.indexOf('.') - 1;
	}

	// add the '0'
	for (var i = 0; i < decimals; i++) {
		val += '0';
	}

	// add thousands
	return WG.thousands(val.replace('.', DECIMAL_POINT));
}

WG.thousands = function(val)
{
	var negative = false;
	if (val < 0) {
		negative = true;
		val = Math.abs(val);
	}

	val = new String(val); // make sure it's a string

	var parts = val.split(DECIMAL_POINT);
	var result = '';
	for (var i = 0; i < parts[0].length; i++) {
		result += parts[0].charAt(i);
		if ((parts[0].length - i - 1) % 3 == 0 && i < parts[0].length - 1) {
			result += THOUSAND_SEPARATOR;
		}
	}
	parts[0] = '';
	if (parts.length > 1) {
		result += DECIMAL_POINT + parts.join('');
	}

	if (negative) {
		result = '-' + result;
	}

	return result;
}
