$(document).ready(function(){
	
	$('#regForm').submit(function(e) {
		register();
		e.preventDefault();
	});	
	$('#reg_editori_Form').submit(function(e) {
		register_editori();
		e.preventDefault();
	});
	$('#update_editori_Form').submit(function(e) {
		update_editori();
		e.preventDefault();
	});
	$('#upForm').submit(function(e) {
		update();
		e.preventDefault();
	});	
	$('#ebookForm').submit(function(e) {
		ebook();
		e.preventDefault();
	});	
	$('#ebook_editori_Form').submit(function(e) {
		ebook_editori();
		e.preventDefault();
	});
	$('#appuntiForm').submit(function(e) {
		appunti();
		e.preventDefault();
	});
	$('#bioForm').submit(function(e) {
		biografia();
		e.preventDefault();
	});	
	$("#next").click(function(){
			location.href = 'http://www.compraebook.it/home/conferma/ebook';
        });
});
function appunti()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/appunti",
		data: $('#appuntiForm').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}
function update_editori()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/update/editori",
		data: $('#update_editori_Form').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}
function update()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/update/utenti",
		data: $('#upForm').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}
function register_editori()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/reg_editori",
		data: $('#reg_editori_Form').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}
function register()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/reg",
		data: $('#regForm').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}
function ebook_editori()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/ebook_editori",
		data: $('#ebook_editori_Form').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}
			else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}
function ebook()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/ebook",
		data: $('#ebookForm').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}
			else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}
function biografia()
{
	hideshow('loading',1);
	error(0);
	$.ajax({
		type: "POST",
		url: "/index.php/ajax/db/biografia/utenti",
		data: $('#bioForm').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
				error(1,msg.txt);
			}
			else if(parseInt(msg.status)==2)
			{
				conferma(1,msg.txt);
			}
			hideshow('loading',0);
		}
	});
}

function error(act,txt)
{
	hideshow('messaggio',act);
	if(txt) $('#messaggio').addClass('error');
	if(txt) $('#messaggio').html(txt);
}
function conferma(act,txt)
{
	hideshow('messaggio',act);
	if(txt) $('#messaggio').addClass('conferma');
	if(txt) $('#messaggio').html(txt);
}
function hideshow(el,act)
{
	if(act) $('#'+el).css('visibility','visible');
	else $('#'+el).css('visibility','hidden');
}