function InitSearchBar()
{
	$('#words').keypress(function(event) {
		if (event.keyCode == '13' || event.keyCode == '10')
		{
			event.preventDefault();
			$('#search_button').click();
		}
	});
}

function ShowSearchBar()
{
	$('#search_bar').animate({'top': '0'}, 'fast', function() {
		$(this).find('input#words').focus();
	});
}

function HideSearchBar()
{
	setTimeout("$('#search_bar').animate({'top': '-40px'}, 'fast')", 100);
}

function Search()
{
	searchText = $('#words').val();

	if (! searchText)
	{
		alert("Devi inserire il testo da cercare");
		return;
	}

	destAddr = "/disegni.php/search/" + escape(searchText);

	if ($('#author_only:checked').length == 1)
		destAddr += "/autonly/1";

	if ($('#srcedizioni:checked').length == 1)
		destAddr += "/srcedizioni/1";

	window.location = destAddr;

/* 	HideSearchBar(); */
}

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 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)
{
	var query = {};
	var selectors;

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

	$form.find(selectors)
		 .each(function(i)
				{
					if (! this.name || this.type == "button")
						return;

					if (! this.value && include_empty)
					{
							// Se richiesto dal chiamante imposto a NULL il valore corrispondente agli input vuoti
						query[this.name] = 'NULL';
					}
					else if (this.type == "checkbox" && ! this.checked)
					{
							// Se l'input e' un check, e non e' checked, imposto il valore a zero
						query[this.name] = 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
						query[this.name] = this.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;
}

