function save(action){
if(action == 1) {

var p = document.forms['newuser'];



if( Object.isNumber(parseInt(p['fiv'].value)) && Object.isNumber(parseInt(p['idTessera'].value)))
{
	var user  = {nome: p['nome'].value, 
		     cognome: p['cognome'].value,
		     fiv: p['fiv'].value,
	  	     idTessera: p['idTessera'].value,
		     dob: isValidDate(p['dob'].value),
		     idZona: p['idZona'].value
		    };
	var jsonUser=Object.toJSON(user);

	var params='action='+action+'&val='+jsonUser;
	var url='http://www.470.it/cgi-bin/save.php';

	new Ajax.Request(url, {
		method: 'post',
		parameters: params,
		onSuccess: function(transport) {
			if(transport.responseText=='OK')
			{
				$('notice').update('<h1>Done</h1><p>Aggiunto nuovo utente</p>');
				$('idTessera').style.backgroundColor="white";
				$('fiv').style.backgroundColor="white";
			}
			else
			$('notice').update('<h1>Error</h1><p>'+transport.responseText+'</p>');
		}
	});

}
else
{
	$('idTessera').style.backgroundColor="red";
	$('fiv').style.backgroundColor="red";
	$('notice').update('<h1>Error</h1><p>Il campo FIV o il numero della tessera sono sbagliati</p>');
}

} else if(action == 2) 
{
	var p = document.forms['addnews'];
	var news= {title: p['title'].value,
		   text: p['text'].value,
 	           img: p['img'].value,
		   idZona: p['idZona'].value,
  	           author:  p['author'].value};
	var jsonNews = Object.toJSON(news);
	var url='http://www.470.it/cgi-bin/save.php';
	var params='action='+action+'&val='+jsonNews;

	new Ajax.Request(url, {
		method: 'post',
		parameters: params,
		onSuccess: function(transport) {
		if(transport.responseText=='OK')
			$('notice').update('<h1>Done</h1><p>Aggiunta news ricarca la pagina home per visualizzarne il contenuto</p>');
		else
			$('notice').update('<h1>Error</h1><p>'+transport.responseText+'</p>');
		}
	});

} else if(action ==3)
{
	var p = document.forms['addregata'];
	
	var startDate   = isValidDate(p['startDate'].value);
	var subDeadLine = isValidDate(p['subDeadline'].value);

	if(startDate && subDeadLine) {	
		var news= {nome: p['nome'].value,
			   startDate: startDate,
 		           subDeadline: subDeadLine,
			   idZona: p['idZona'].value,
  	       		   orgCommittee:  p['orgCommittee'].value};
		var jsonNews = Object.toJSON(news);
		var url='http://www.470.it/cgi-bin/save.php';
		var params='action='+action+'&val='+jsonNews;

		new Ajax.Request(url, {
			method: 'post',
			parameters: params,
			onSuccess: function(transport) {
			if(transport.responseText=='OK')
				$('notice').update('<h1>Done</h1><p>Aggiunta nuova regata ricarica il calendario</p>');
			else
				$('notice').update('<h1>Error</h1><p>'+transport.responseText+'</p>');
			}
		});
	}
	else
	{
		$('idTessera').style.backgroundColor="red";
		$('fiv').style.backgroundColor="red";
		$('notice').update('<h1>Error</h1><p>Le date inserite non sono valide o non sono nel formato corretto. Reinserire i dati facendo attenzione al formato lo script si aspetta prima il giorno e poi il mese (gg-mm)</p>');
	}

} else if(action == 9)
{
	var p = document.forms['addclassifica'];
	var regata={title: p['title'].value,
		   text: p['text'].value,
 	           img: p['img'].value,
		   idZona: p['idZona'].value,
  	           author:  p['author'].value};
	var jsonNews = Object.toJSON(news);
	var url='http://www.470.it/cgi-bin/save.php';
	var params='action='+action+'&val='+jsonNews;

	new Ajax.Request(url, {
		method: 'post',
		parameters: params,
		onSuccess: function(transport) {
		if(transport.responseText=='OK')
			$('notice').update('<h1>Done</h1><p>Aggiunta news ricarca la pagina home per visualizzarne il contenuto</p>');
		else
			$('notice').update('<h1>Error</h1><p>'+transport.responseText+'</p>');
		}
	});

	
}

}

function isValidDate(value)
{
	var tmpDate = value.split("-");
	var gg=parseInt(tmpDate[0]);
	var mm=parseInt(tmpDate[1]);
	var aa=parseInt(tmpDate[2]);
	if(Object.isNumber(gg) && Object.isNumber(mm))
	{
		var theDate = new Date(aa,(mm-1),gg);
		return theDate;
	}
	else
	{
		return false;
	}
			
}
function viewUser()
{
$('regata').hide();
$('classifica').hide();
$('news').hide();
$('user').show(); 
return false;
}

function viewNews()
{
$('user').hide();
$('classifica').hide();
$('regata').hide();
$('news').show(); 
return false;
}
function viewRegata()
{
$('user').hide();
$('classifica').hide();
$('news').hide();
$('regata').show(); 
return false;
}

function viewContents(val)
{
$('user').hide();
$('regata').hide();
$('news').hide();
$('classifica').show();
document.getElementById('tipo').value = val;
var title = document.getElementById('title');
if(val == 1) title.innerHTML = 'Aggiungi comunicato';
else        title.innerHTML = 'Aggiungi classifica';
return false;
}

function checkAuth()
{


Dialog.confirm($('login').innerHTML, {className:"alphacube", width:400, 
                                      okLabel: "login", cancelLabel: "cancel",
                                      onOk:function(win){
					params = 'name='+$('name').value+'&passwd='+$('passwd').value;
					url = "http://www.470.it/cgi-bin/login.php";
					new Ajax.Request(url, {
					method: 'post',
					parameters: params,
						onSuccess: function(transport) {
						if(transport.responseText=='OK')
						    location.href="http://www.470.it/private/private.php?id_zona=1";	
						else
						{
                                    		    $('login_error_msg').innerHTML='Login or password inccorect';
                                     		    $('login_error_msg').show(); 
                                     		    Windows.focusedWindow.updateHeight();
                                      		    new Effect.Shake(Windows.focusedWindow.getId()); return false;
						}
					}});

					}});
          

}
