// JavaScript Document
$(document).ready(function(e) {
	
	var check = new Array(false);
	
	/*   SUBMIT CHECK SIGNUP FORM   */

	//Controllo del campo cognome
	$('form#signup input[name="cognome"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Cognome').css('border-color', '#C00');
			check[0] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[0] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Cognome') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo nome
	$('form#signup input[name="nome"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Nome').css('border-color', '#C00');
			check[1] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[1] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Nome') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo qualifica
	$('form#signup input[name="qualifica"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Qualifica').css('border-color', '#C00');
			check[2] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[2] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Qualifica') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo amministrazione
	$('form#signup input[name="amministrazione"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Amministrazione').css('border-color', '#C00');
			check[3] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[3] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Amministrazione') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo CAP
	$('form#signup input[name="cap"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'CAP').css('border-color', '#C00');
			check[4] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[4] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'CAP') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo email
	$('form#signup input[name="email"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Email').css('border-color', '#C00');
			check[5] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[5] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Email') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo telefono
	$('form#signup input[name="telefono"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Telefono').css('border-color', '#C00');
			check[6] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[6] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Telefono') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo domicilio
	$('form#signup input[name="domicilio"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Domicilio').css('border-color', '#C00');
			check[7] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[7] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Domicilio') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo luogo_nascita
	$('form#signup input[name="luogo_nascita"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Luogo di Nascita').css('border-color', '#C00');
			check[8] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[8] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Luogo di Nascita') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo data_nascita
	$('form#signup input[name="data_nascita"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Data di Nascita').css('border-color', '#C00');
			check[9] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[9] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Data di Nascita') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo cf
	$('form#signup input[name="cf"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Codice Fiscale').css('border-color', '#C00');
			check[10] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[10] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Codice Fiscale') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	
	/*   SUBMIT CHECK CONTACT FORM   */
	
	//Controllo del campo cognome
	$('form#contact input[name="cognome"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Cognome').css('border-color', '#C00');
			check[0] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[0] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Cognome') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo nome
	$('form#contact input[name="nome"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Nome').css('border-color', '#C00');
			check[1] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[1] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Nome') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo email
	$('form#contact input[name="email"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Email').css('border-color', '#C00');
			check[2] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[2] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Email') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo telefono
	$('form#contact input[name="telefono"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Telefono').css('border-color', '#C00');
			check[3] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[3] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Telefono') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo oggetto
	$('form#contact input[name="oggetto"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Oggetto').css('border-color', '#C00');
			check[4] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[4] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Oggetto') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	//Controllo del campo messaggio
	$('form#contact textarea[name="text"]').bind('blur', function(){
		var value = $.trim($(this).attr('value'));
		if (value == '')
		{
			$(this).attr('value', 'Messaggio').css('border-color', '#C00');
			check[5] = false; 
		} else {
			$(this).css('border-color', '#090');
			check[5] = true;
		}
	}).bind('focus', function(){
		if (this.value == 'Messaggio') { $(this).attr('value', '').css('border-color', '#999'); }
	});
	
	//Controllo dell'invio del form
	$('form#contact input[type="submit"], form#signup input[type="submit"]').bind('click', function(){
		var section = $('form input[type="hidden"]').attr('value');
		var check2 = true;
		for (var i = 0; i < check.length; i++)
		{
			if (check[i] == false)
			{
				alert('Errore, non tutti i campi sembrano completati correttamente. Correggi e riprova!');
				check2 = false;
				break;
			}
		}
		if (check2)
		{
			if(section == 'contact')
			{
				var cognome = $('form#contact input[name="cognome"]').attr('value');
				var nome = $('form#contact input[name="nome"]').attr('value');
				var email = $('form#contact input[name="email"]').attr('value');
				var telefono = $('form#contact input[name="telefono"]').attr('value');
				var oggetto = $('form#contact input[name="oggetto"]').attr('value');
				var testo = $('form#contact textarea[name="testo"]').attr('value');
				$.ajax({
    				url: 'core/send.php',
    				type: "POST",
    				data: "cognome="+cognome+"&nome="+nome+"&email="+email+"&telefono="+telefono+"&oggetto="+oggetto+"&testo="+testo+"&section=contact",
    				success: function(data) {
						alert(data);
						location.reload();
        			}
				});
				return false;
			} 
			else if (section == 'signup')
			{
				var cognome = $('form#signup input[name="cognome"]').attr('value');
				var nome = $('form#signup input[name="nome"]').attr('value');
				var email = $('form#signup input[name="email"]').attr('value');
				var telefono = $('form#signup input[name="telefono"]').attr('value');
				var domicilio = $('form#signup input[name="domicilio"]').attr('value');
				var cap = $('form#signup input[name="cap"]').attr('value');
				var qualifica = $('form#signup input[name="qualifica"]').attr('value');
				var amministrazione = $('form#signup input[name="amministrazione"]').attr('value');
				var luogo_nascita = $('form#signup input[name="luogo_nascita"]').attr('value');
				var data_nascita = $('form#signup input[name="data_nascita"]').attr('value');
				var cf = $('form#signup input[name="cf"]').attr('value');
				$.ajax({
    				url: 'core/send.php',
    				type: "POST",
    				data: "cognome="+cognome+"&nome="+nome+"&email="+email+"&telefono="+telefono+"&domicilio="+domicilio+"&cap="+cap+"&qualifica="+qualifica+"&amministrazione="+amministrazione+"&luogo_nascita="+luogo_nascita+"&data_nascita="+data_nascita+"&cf="+cf+"&section=signup",
    				success: function(data) {
						alert(data);
						location.reload();
        			}
				});
				return false;
			}
			else
			{
				alert('Errore!');
				return false;
			}
		}
		return false;
	});
	
});
