/*** 
	Simple jQuery Slideshow Script
	Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {
	var $active = $('#slideshow IMG.active');

	if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

	// use this to pull the images in the order they appear in the markup
	var $next =  $active.next().length ? $active.next()
		: $('#slideshow IMG:first');
	// uncomment the 3 lines below to pull the images in random order
	// var $sibs  = $active.siblings();
	// var rndNum = Math.floor(Math.random() * $sibs.length );
	// var $next  = $( $sibs[ rndNum ] );
	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
}

$(function() {
		setInterval( "slideSwitch()",  2000 );
});
		
/*
funzione per l'apertura di una nuova pagina su preventivi.it con il form già compilato
*/
function cercoClienti(){
	tipo = document.getElementById('tipo_utente');
	if(tipo.value == 'NuoviClienti' || tipo.value == 'Operaio'){
	var categoriaValue = document.getElementById("impianto").value;
	var cognomeValue = document.getElementById("cognome").value;
	var emailValue = document.getElementById("email").value;
	var telefonoValue = document.getElementById("telefono").value;
	var provinciaValue = document.getElementById("provincia").value;
	//	var comuneValue = document.getElementById("comune").value;
	//	alert("Questa pagina è dedicata ad utenti in cerca di preventivi, <br />per trovare nuovi clienti sarai reindirizzato alla pagina"); 
	var url = "http://www.preventivi.it/aziende.php?categoria=" + categoriaValue + "&cognome=" + cognomeValue + "&email=" + emailValue + "&telefono=" + telefonoValue + "&provincia=" + provinciaValue;
// 	window.open("http://www.preventivi.it/index.php?categoria=" + categoriaValue + "&cognome=" + cognomeValue + "&email=" + emailValue + "&telefono=" + telefonoValue + "&provincia=" + provinciaValue, '_blank');
	if (document.getElementById("marker").value != '') {
		url += 	"marker=" + document.getElementById("marker").value;
	}
	location.href=  url;
	}
}

// impostazioni personalizzate per il validator
$.ketchup
.createErrorContainer(function(form, el) {
  return $('<p/>', {
           'class': 'ketchup-custom'
         }).insertAfter(el);
})

.addErrorMessages(function(form, el, container, messages) {
  container.html('');
  $('<span/>', {
      text: messages[0]
    }).css("margin-left", el.css("margin-left")).appendTo(container);
})


.showErrorContainer(function(form, el, container) {
	el.css("border-color", "red").css("border-radius", "0px");
	container.slideDown('fast');
})

.hideErrorContainer(function(form, el, container) {
	el.css("border-color", "").css("border-radius", "");
	container.slideUp('fast');
});


// Validiazione del form e conseguente chiamata ajax
// Nella chiamata ajax il parametro "data" del form preleva solo i campi del form che hanno l'attributo "name" !!!
$(document).ready(function() { 

	if(validator == 2){
		$('#modulo').ketchup({		
			  validateEvents: 'click keyup blur change'
		});
	} else if(validator == null){
		var validator = 1;
		$('#modulo').ketchup({		
			validateEvents: 'submit'
		});
	}

	$("#modulo").submit(function() {
		if(validator == 1){
			validator = 2;
			$('#modulo').ketchup({		
				  validateEvents: 'click keyup blur change'
			});
		}
		if( $('#modulo').ketchup("isValid") ) { 
			document.getElementById("pulsante").disabled=true;
			document.getElementById("progress").style.visibility = "visible";
			document.getElementById("pulsante").style.visibility = "hidden";
			$.ajax({
				type: "POST",
				url: "./confirm_user.php",
				datatype: "json",
				data: $("form").serialize(),
				async:true,
				success:function() {
					try{
			
	//					location.href = paginaGrazieGet + ".php?cognome1="+document.getElementById('cognome').value+"&email1="+document.getElementById('email').value+"&telefono1="+document.getElementById('telefono').value+"&provincia1="+document.getElementById('provincia').value+"&comune1="+document.getElementById('comune').value+"&tipo_utente1="+document.getElementById('tipo_utente').value+"";
//						location.href = paginaGrazieGet + ".php?marker="+document.getElementById('marker').value +"&cognome="+document.getElementById('cognome').value+"&email="+document.getElementById('email').value+"&telefono="+document.getElementById('telefono').value+"&provincia="+document.getElementById('provincia').value+"&comune="+document.getElementById('comune').value+"&tipo_utente="+document.getElementById('tipo_utente').value+"";
						location.href = "grazieGETpreventivo.php?marker="+document.getElementById('marker').value +"&cognome="+document.getElementById('cognome').value+"&email="+document.getElementById('email').value+"&telefono="+document.getElementById('telefono').value+"&provincia="+document.getElementById('provincia').value+"&comune="+document.getElementById('comune').value+"&tipo_utente="+document.getElementById('tipo_utente').value+"";
					}catch (e){
						alert("Errore durante il redirect alla pagina grazieGETpreventivo.php !!! " + e.message);
					} 
				},
				error:function(msg){
            		$('#msg').text('Connection error').addClass('err');
        		}
		  });
		}
	});
});
