function ShowAlertMsg($divob, title, msg, cback)
{
	$divob.dialog("destroy")
		  .find("p.msg")
		  .html(msg)
		  .end()
		  .dialog({
				resizable: false,
				modal: true,
				title: title,
				buttons: {
					"OK": function() {
						cback();
					}
				}
		  });
}

function ShowConfirmDialog($divob, title, msg, actionBtn, cback)
{
	$divob.dialog("destroy")
		  .find("p.msg")
		  .html(msg)
		  .end()
		  .dialog({
				resizable: false,
				modal: true,
				title: title,
				buttons: {
					'Cancel': function() {
						$(this).dialog('close');
					},
					actionBtn: function() {
						cback();
					}
				}
		  });
}

function ValidEmailAddress(addr)
{
	return addr.match(/[^@\s]+@[^@\s]+\.[^@\s]+/) != null;
}

function SiteVisChanged(value)
{
	$.ajax({
		data: { css: value },
		dataType: 'html',
		type: 'get',
		url: '/change-site-css.php',
		success: function(data, textStatus)
		{
			window.location.reload();
		},
		error: function (XMLHttpRequest, textStatus, errorThrown)
		{
		},
		complete: function(XMLHttpRequest, textStatus)
		{
		}
	});
}

function FormatSQLDate(dateString)
{
	var chunks = dateString.split('-');
	return chunks[2] + "/" + chunks[1] + "/" + chunks[0];
}

function FormatSQLDateUSA(dateString)
{
	var chunks = dateString.split('-');
	return chunks[1] + "-" + chunks[2] + "-" + chunks[0];
}

function UpdateResultsPP(value)
{
	createCookie('respp', value, 365 * 10);
	window.location = (typeof(window.location) == 'object' ? window.location.href : window.location).replace(/\/page\/[^\/]+/, '/page/1');
}

function FormToObject($form, visible_only, include_empty, cback)
{
	var selectors;

	if (visible_only)
		selectors = "input:visible,select:visible,textarea:visible";
	else
		selectors = "input,select,textarea";

	return FormElementsToObject($form.find(selectors), include_empty, cback);
}

function FormElementsToObject($elems, include_empty, cback)
{
	var query = {};

	$elems.each(function(i) {

		if (! this.name || this.type == "button")
			return;

		var value = null;

		if (! this.value && include_empty)
		{
				// Se richiesto dal chiamante imposto a NULL il valore corrispondente agli input vuoti
			value = 'NULL';
		}
		else if (this.type == "checkbox" && ! this.checked)
		{
				// Se l'input e' un check, e non e' checked, imposto il valore a zero

			var unckvalue = $(this).attr("unckvalue");

			value = unckvalue ? unckvalue : "0";
		}
		else if (this.value && (this.type != "radio" || this.checked))
		{
				// Includo il valore solo se esiste e, nel caso dei radio button, se il button e' checked
			value = this.value;
		}

		if (value && (! cback || cback(this, query)))
			query[this.name] = value;
	});

	return query;
}

function ObjectToForm(data, $form, cback)
{
	$form.find("input,textarea,select").each(function(i) {

		if (! data[this.name])
			return;

		if (! cback || ! cback(this, data[this.name]))
		{
			if (this.type == 'checkbox')
			{
				if (parseInt(data[this.name]))
					$(this).attr('checked','checked');
				else
					$(this).removeAttr('checked');
			}
			else if (this.type == 'radio')
			{
				if (parseInt(data[this.name]) && $(this).val() == data[this.name])
					$(this).attr('checked','checked');
				else
					$(this).removeAttr('checked');
			}
			else
			{
				$(this).val(data[this.name]);
			}
		}
	});
}

function GetRandomNumber(range)
{
	return Math.floor(Math.random() * range);
}

function GetRandomChar()
{
	var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";

	return chars.substr(GetRandomNumber(62), 1);
}

function RandomID(size)
{
	var str = "";

	for(var i = 0; i < size; i++)
	{
		str += getRandomChar();
	}

	return str;
}

