

$(document).ready(function() {	 
						  
// ---------------------------------------------------------------------
	
	// VALIDAZIONE FORM CHECKOUT - EVASIONE ORDINE
	var validator = $("#dati-fatturazione").validate({
		rules: {
			nome_cliente	 : "required",
			cognome_cliente  : "required",
			indirizzo_cliente: "required",
			citta_cliente    : "required",
			provincia_cliente: "required",
			flag_privacy     : "required",
			tipo_spedizione  : "required",
			tipo_pagamento   : "required",
			
			codice_fiscale_cliente: {
				required: true,
				minlength: 16
			},
			
			email_cliente: {
				required: true,
				email: true
			},
			
			cap_cliente: {
				required: true,
				maxlength: 5,
				number: true
			}	
		},
		messages: {
			flag_privacy:    privacy_err,
			tipo_spedizione: ship_err,
			tipo_pagamento: pay_err
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent() ); 
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else {
					error.appendTo( element.parent() ); 
			     }
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		/*submitHandler: function() {
			//document.getElementById('dati-fatturazione').submit();
			return true;
			jConfirm('<h4>'+title_label+'</h4><h4>'+total_label+$('#totale-ordine').text()+'</h4>Email: ' + '<span>' + $('#email_cliente').val() + '</span>', prompt_label, function(r) {	
			if(r == true) { //$('form#dati-fatturazione').submit( function(){alert('ciao');} );
				
				document.getElementById('dati-fatturazione').submit();
			}
		});
	    
		},*/
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.addClass('corretto');
		}
	});
	
	jQuery.validator.addMethod("spedizioneObbligatoria", function(value, element) {
		if ($("#flag_indirizzo_spedizione").is(":unchecked"))
			return $(element).parents("#indirizzo_alternativo").length;
		return !this.optional(element);
	}, "Campo obbligatorio.");

	jQuery.validator.addMethod("aziendaleObbligatoria", function(value, element) {
		if ($("#flag_azienda").is(":unchecked"))
			return $(element).parents("#dati_aziendali").length;
		return !this.optional(element);
	}, "Campo obbligatorio.");

	// VALIDAZIONE FORM CHECKOUT - LOGIN AREA RISERVATA
	var validator_login = $("#frm-login").validate({
		rules: {
			login_pwd  : "required",
			

			login_utente: {
				required: true,
				email: true
			}
			
		},
		messages: {
			login_pwd:    null,
			login_utente: null
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.appendTo( element.parent() ); 
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			
			var login_options = { 
				success:   login_user,  // post-submit callback 
				dataType:  'json',       // 'xml', 'script', or 'json' (expected server response type) 
				url: base_url + 'login'
			}; 
			
			$("#frm-login").ajaxSubmit(login_options);
			return false;
					   
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.addClass('corretto');
		}
	});
	
	// VALIDAZIONE FORM CHECKOUT - RECUPERA PASSWORD
	var validator_recover = $("#frm-recovery").validate({
		rules: {
			email_utente: {
				required: true,
				email: true
			}
		},
		messages: {
			email_utente:    null
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.appendTo( element.parent() ); 
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			
			var recover_options = { 
				success:   recover_pwd,  // post-submit callback 
				dataType:  'json',       // 'xml', 'script', or 'json' (expected server response type) 
				url: base_url + 'recupera_password'
			}; 
			
			$("#frm-recovery").ajaxSubmit(recover_options);
			return false;
					   
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.addClass('corretto');
		}
	});
	
	
	function login_user(data)
	{
		if(data.status_login == true)
		{
			top.location = base_url + 'carrello';
			return false;
		}
		
		$('#messaggio-login').html(data.error).css({'color' : '#ea5200'});
	}

	function recover_pwd(data)
	{
		if(data.status_pwd == true)
		{
			$('#messaggio-pwd').html(data.success).css({'color' : 'green'});
			$('#frm-recovery #dati-password').hide();
			$('#frm-recovery #invia-pwd').hide();
			return false;
		}
		
		$('#messaggio-pwd').html(data.error).css({'color' : '#ea5200'});
	}
}); // FINE DOM
