// -------------------------------------------------------------------

/**
*	HighlightActiveInput
*
*	Adds event handlers to all inputs with class 'text', that adds 
*	the class 'active-input'to them onfocus and removes it onblur.
**/
function HighlightActiveInput()
{
	$('input.text, select.text, textarea.text').each(function()
	{
		$(this).focus(function()
		{
			$(this).addClass('active-input');
		});
		$(this).blur(function()
		{
			$(this).removeClass('active-input');
		});
	});
}

// -------------------------------------------------------------------

/**
*	ClearForm
*
*	Clears all form elements that occur 
*	within the given object (id).
*
*	- obj: ID of object that contains the form elements
**/
function ClearForm(obj)
{
	$('#' + obj + ' input').each(function()
	{
		if (this.type != 'submit')
		{
			$(this).val('');
		}
	});

	$('#' + obj + ' select').each(function()
	{
		this.selectedIndex = 0;
	});

	$('#' + obj + ' textarea').each(function()
	{
		$(this).val('');
	});

}

// -------------------------------------------------------------------

/**
*	GetFormData
*
*	Creates an array with all data from a form.
*
*	- obj: ID of object that contains the form elements
**/
function GetFormData(obj)
{
	var data = new Object();
	
	$('#' + obj + ' input, #' + obj + ' textarea').each(function()
	{
		// Collect values from text inputs and selects
		if (this.type != 'submit' && this.type != 'checkbox')
		{
			data[this.name] = $(this).val();
		}
		
		// Collect values from checkboxes
		if (this.type == 'checkbox' && this.checked)
		{
			var actual_val = this.id.replace(this.name + '_', '');
			if (data[this.name])
			{
				data[this.name].push(actual_val);
			}
			else
			{
				data[this.name] = new Array();
				data[this.name].push(actual_val);
			}
		}
	});

	$('#' + obj + ' select').each(function()
	{
		data[this.name] = $(this).val();
	});
	return data;
}

// -------------------------------------------------------------------

/**
*	$$
*
*	Returns an object reference
*	for the given object id
**/
function $$(obj_id)
{
	return document.getElementById(obj_id);
}

// -------------------------------------------------------------------

/**
*	redirect
*
*	Redirects to the given page, with WEB_ROOT
**/
function redirect(url)
{
	window.location = url;
}

// -------------------------------------------------------------------

/**
*	log
*
*	Logs to the console
**/
function log(str, to_div)
{
	if (console)
	{
		console.log(str);
	}
}

// -------------------------------------------------------------------

/**
*	ParseDate
*
*	Converts a string date (DD-MM-YYYY or YYYY-MM-DD) to Date object.
**/
function ParseDate(str)
{
	var parts = str.split('-');
	var d = new Date;
	d.setSeconds(0);
	d.setMinutes(0);
	d.setHours(0);
	
	if (parts[0].length == 4)
	{
		// YYYY-MM-DD 
		d.setDate(parts[2]);
		d.setMonth((parts[1] - 1));
		d.setFullYear(parts[0]);
	}
	else
	{
		// DD-MM-YYYY
		d.setDate(parts[0]);
		d.setMonth((parts[1] - 1));
		d.setFullYear(parts[2]);
	}
	return d;
}

// -------------------------------------------------------------------

/**
*	GetIsoDate
*
*	Converts a Date object to an ISO date string (YYYY-MM-DD).
**/
function GetIsoDate(somedate)
{
	var month = somedate.getMonth() + 1;
	if (month < 10)
	{
		month = '0' + month;
	}
	var day = somedate.getDate();
	if (day < 10)
	{
		day = '0' + day;
	}
	return somedate.getFullYear() + '-' + month + '-' + day;
}

// ----------------------------------------------

/**
*	Add in_array function to array
**/
Array.prototype.hasValue = function(value) 
{
	var len = this.length;
	for (var x = 0; x <= len; x++) 
	{
		if (this[x] == value) 
		{
			return true;
		}
	}
	return false;
};