var indirizzo = 'http://areariservata.demaplast.com/';

$(function() {
	$('#load').dialog({
		modal: true,
		overlay: {
			opacity: 0.5,
			background: "black"
		},
		autoOpen: false,
		height: '120px',
		width: '260px',
		title: 'Caricamento dati',
		resizable: false,
		draggable: false
	});

	$('.elencoCodici').change(function() {

		var idCodice = $(this).val();

		if(idCodice != '') {
			$('#thTutti').hide();
			$('#thRicerca').show();
			$('.trCliente').hide();
			$('#tr'+idCodice).show();
			$('#tr'+idCodice).css("background-color","#FDFDFD");
		}
		else {
			$('#thTutti').show();
			$('#thRicerca').hide();
			$('.trCliente').show();
			$('.trCliente:odd').css("background-color","#D3D7DC");
		}
		return false;
	});

	$('.elencoSigilli').change(function() {

		$.ajax({
			type: "post",
			url: 'index/ajax/showSigilli',
			data: 'valueQ='+$(this).val(),
			async: false,
			beforeSend: function() {
				$('#load').show();
				$('#load').dialog('open');
			},
			success: function(data) {
				$('#nomeCliente').html(data.nomeCodice);
				$('#elenco').html(data.contenuto);
				$('.trCliente:odd').css("background-color","#D3D7DC");
				$('#load').dialog('close');
			},
			dataType: 'json'
		});
		return false;
	});

	$('#elencoCodiciSigilli').change(function() {

		$.ajax({
			type: "post",
			url: 'index/ajax/showSigilli',
			data: 'valueQ='+$(this).val(),
			async: false,
			beforeSend: function() {
				$('#load').show();
				$('#load').dialog('open');
			},
			success: function(data) {
				$('#nomeCliente').html(data.nomeCodice);
				$('#elenco').html(data.contenuto);
				$('.trCliente:odd').css("background-color","#D3D7DC");
				$('#load').dialog('close');
			},
			dataType: 'json'
		});
		return false;
	});
});


function erase() {
	$.ajax({
		url: 'index/agente/erase',
		async: false,
		success: function(data) {
			if(data == 'ok')
				window.location.href = indirizzo+'index/logout.html';
		}
	});
}

window.onbeforeunload = function (event) {

	$.ajax({
		url: 'index/agente/erase',
		async: false
	});
};

function trim(stringa){
    while (stringa.substring(0,1) == ' ') {
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' ') {
        stringa = stringa.substring(0,stringa.length-1);
    }

    return stringa;
}

function validEmail(text) {

	if(text.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/))
		return true;

	return false;
}

function validDouble(text) {

	if(text.match(/^\+?[0-9\.,]+$/))
		return true;

	return false;
}

function validPhone(text) {

	if(text.match(/^[0-9\.-\/ ]+$/))
		return true;

	return false;
}


function validNumeric(text) {
	if(text.match(/^[0-9]+$/))
		return true;

	return false;
}

// Verifico se il campo è solo letterale (a-zA-Z)
function validString(text) {
	if(text.match(/^[a-zA-Z]+$/))
		return true;

	return false;
}