var DEBUG = false;
var QueryParams = window.location.href.toString().toQueryParams();
if (QueryParams.debug) DEBUG = true;

/*
Element.addMethods(
{
	getOuterHTML: function(element)
	{		if (element.outerHTML)
			return element.outerHTML;		else			return element.parentNode.innerHTML;	}
});
*/

function $S(value) { return new String(value); }

Object.extend(String.prototype, 
{
	parseFloatF: function (currencySymbol)
	{
		if (!currencySymbol) currencySymbol = '$';
		
		value = this;
		if (currencySymbol != '')
			value = value.replace(currencySymbol, '');
		value = value.gsub(/\(/g, '-').gsub(/[,)\s+]/g, '');
		
		return value;
	},

	formatNumber: function (decimalPlaces, includeLeadingDigit, useParensForNegativeNumbers, groupDigit, currencySymbol)
	{
		if (decimalPlaces == null) decimalPlaces = 0;
		if (useParensForNegativeNumbers == null) useParensForNegativeNumbers = false;
		if (includeLeadingDigit == null) includeLeadingDigit = true;
		if (groupDigit == null) groupDigit = true;
		if (currencySymbol == null) currencySymbol = '';
	
		value = this;
		if (currencySymbol != '')
			value = value.replace(currencySymbol, '');
		
		value = value.gsub(/\(/g, '-').gsub(/[,)\s+]/g, '');
		
		if (isNaN(parseFloat(value))) return;
		var isNegative = (value < 0);
		value = parseFloat(value.gsub(/-/, ""));

		var zeros = '';
		for (var i = 0; i < decimalPlaces; i++)
			zeros += '0';

		value += parseFloat('.' + zeros + '5');
		value = $S(value);
		var decimalStop = value.indexOf('.');

		if (groupDigit)
		{
			var formatted = "";
			for (var j = 0, i = decimalStop % 3; i < decimalStop; i += 3)
			{
				if (i > j)
					formatted += value.substring(j, i) + ",";
					
				j = i;
			}
			formatted += value.substring(j, value.length);
			value = formatted;
		}

		value = value.substr(0, value.indexOf(".") + (decimalPlaces == 0 ? 0 : decimalPlaces + 1));
		
		if (isNegative)
		{
			if (useParensForNegativeNumbers)
				value = '(' + value + ')';
			else
				value = '-' + value;
		}
		
		if (!includeLeadingDigit && value.substr(0, 1) == '0')
			value = value.substr(1, value.length - 1);
		
		return currencySymbol + value;
	}
});

Form.Methods.load = function(element, url, onComplete, parameters, dataType)
{
	element.disable();
	element.status = 'loading';
	element.exception = null;
	if (dataType == null) dataType = 'JSON';

	new Ajax.Request(url,
	{
		onException: function(request, exception)
		{
			element.status = 'failed';
			element.exception = exception;
		}.bind(element),
		onFailure: function(request)
		{
			element.status = 'failed';
			element.exception = 'HTTP Response ' + request.status;
		}.bind(element),
		onSuccess: function(request)
		{
			try
			{
				if (dataType == 'JSON')
				{
					var data = request.responseText.evalJSON();
					if (data.exception)
					{
						element.status = 'failed';
						element.exception = data.exception;
					}
					else
					{
						for (var property in data)
						{
							if (element[property] && typeof data[property] != 'undefined')
								element[property].value = data[property];
						}						element.status = 'loaded';
						element.enable();
					}
				}
			}
			catch(e)
			{
				element.status = 'error';
				element.exception = e;
			}
		}.bind(element),
		onComplete: onComplete.bind(element),			
		parameters: parameters
	});
}

Form.Element.Methods.loadFromObjArray = function(element, array, valueProp, textProp, startIndex)
{
	if (element.tagName.toLowerCase() != 'select') return;
	
	element = $(element);

	var html = '';
	if(Prototype.Browser.IE)
	{
		var optionIndex = element.outerHTML.toLowerCase().indexOf('<option');
		html += element.outerHTML.substr(0, optionIndex).toString().strip();
	}
	
	$A(element.options).each(function(opt)
	{
		if (opt.index >= startIndex) throw $break;
		html += '<option value="' + opt.value + '">' + opt.text + '</option>';
	});

	$A(array).each(function(obj)
	{
		html += '<option value="' + eval('obj.' + valueProp) + '">' + eval('obj.' + textProp) + '</option>';
	});
	
	if(Prototype.Browser.IE)
	{
		html += '</select>';
		element.replace(html);
	}
	else
		element.update(html);
}
Element.addMethods();

var Data = Class.create();
Data.prototype = 
{
	initialize: function(url, onComplete, parameters, load, dataType, asynchronous)
	{
		this.ready = false;
		this.status = 'initialized';
		this.exception = null;
		this.value = null;
		
		this.url = url;
		this.onComplete = onComplete;
		this.parameters = parameters;
		if (load == null) load = true;
		if (dataType == null) dataType = 'JSON';
		this.dataType = dataType;
		if (asynchronous == null) asynchronous = true;
		this.asynchronous = asynchronous;
		
		if (load) this.load();
	},
	
	load: function(defaultValue)
	{
		this.ready = false;
		this.status = 'loading';
		this.exception = null;
		this.defaultValue = defaultValue;
	
		new Ajax.Request(this.url,
		{
		    asynchronous: this.asynchronous,
			onException: function(request, exception)
			{
				this.status = 'failed';
				this.exception = exception;
			}.bind(this),
			onFailure: function(request)
			{
				this.status = 'failed';
				this.exception = 'HTTP Response ' + request.status;
			}.bind(this),
			onSuccess: function(request)
			{
				try
				{
					this.value = request.responseText;		
					if (this.exception || this.value == "error")
					{
						this.status = 'failed';
						if (!this.exception)
							this.exception = "unknown error";
					}
					else
					{
						if (this.dataType == 'JSON')
						{
							var data = request.responseText.evalJSON();
							Object.extend(this, data);
						}
						this.ready = true;
						this.status = 'loaded';
					}
				}
				catch(e)
				{
					this.status = 'error';
					this.exception = e;
				}
			}.bind(this),
			onComplete: this.onComplete.bind(this),			
			parameters: this.parameters
		});
	}
}