function createCookie(name,value,days) 
{
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

$(function()
{
	var pause 		= 1000,
		year 		= $('#year'),
		thanks		= $('#thanks'),
		hideYear	= readCookie('hideYear');

	if (!hideYear){
		setTimeout(
			function(){
				year.animate({bottom: '0'}, 700);
			}, pause
		);
		
		$('a#hide').click(
			function()
			{
				year.animate({bottom: '-200px'}, 200);
				return false;
			}
		);
		
		$('a#kill').click(
			function()
			{
				createCookie( 'hideYear', 0, 365 );
				year.animate({bottom: '-200px'}, 200);
				return false;
			}
		);
		
		$('#register').click(
			function()
			{
				var nome 	= $('#nome').val(),
					mail 	= $('#email').val(),
					error	= false,
					messaggio = '',
					filter 	= /^[A-Z0-9+_.-]+@[A-Z0-9.-]+$/i;

				if( !nome ){
					error = true;
					messaggio = 'Compilare correttamente il campo Nome\n';
				}
				else{
					messaggio = '';
				}
				
				if( !filter.test(mail) ){
					error = true;
					messaggio += 'Controlla di aver inserito un indirizzo email valido';
				}
				
				if( error ){
					alert(messaggio);
				}
				else{
				
					year.animate({bottom: '-200px'}, 200);
					thanks.animate({bottom: '0'}, 700);
					//createCookie( 'hideYear', 0, 365 );
					
					$.post(
						'/list/?p=subscribe&id=2', 
						{email: mail, attribute1: nome, htmlemail: '1', 'list[2]': 'signup', 'listname[2]': 'Your Inspiration Web', subscribe: 'Iscriviti'}
					);	
				}
				
				return false;
				
			}
		);
		
	}
	
});

