var form_copy = null;

function openForm(obj) {
	stopAllAnimations();
	
	if(obj == null) {
		obj = new Object();
	}
	if(obj.title == null) {
		obj.title = "Richiedi un preventivo";
	}
	$('#form_title').text(obj.title);
	$('#email_redazione').val(obj.email_redazione);
	if(obj.email_operatore != null) {
		$('#email_operatore').val(obj.email_operatore);
	}
	//*/
	var btop = '10%';
	//var btop = ($(window).width() - 445) / 2;
	var ww = $(window).width();
	var bleft = (ww - 685) / 2;
	
	var original_form = $('#form');
	form_copy = original_form.clone();
	form_copy.attr('id', 'compiled_form');
	form_copy.find('#form_title').attr('id', 'form_copy_title');
	$(document.body).append(form_copy);
	
	if(obj.sid != null) {
		var s = form_copy.find("#servizio");
		s.val(obj.sid);
		s.css('display', '');
	} else {
		form_copy.find("#servizio").css('display', 'none');
	}
	
	form_copy.find("input[type=text]").each(function () {
		var ival = this.value;
		this.defaultValue = ival;
		$(this).focus(function() {
			if(this.value == ival) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = ival;
			}
		});
	});
	form_copy.find("textarea").each(function () {
		var ival = this.value;
		$(this).focus(function() {
			if(this.value == ival) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = ival;
			}
		});
	});
	
	//alert($(window).height());
	$.blockUI({
		message: form_copy,
		css: {
			width: '685px',
			height: '445px',
			border: 0,
			top: btop,
			left: bleft,
			cursor: 'normal',
			background: 'transparent'
		}
	});
	
	image_xmtr($('#form_copy_title')[0]);
	
	$(document.body).bind('xmtr_loaded', function() {
		setTimeout(function() { form_copy.find("#nome")[0].blur(); }, 100)
		$(document.body).unbind('xmtr_loaded');
	});
	//*/
	
	$(window).resize(function() {
		var ww = $(window).width();
		var bleft = (ww - 685) / 2;
		$('.blockMsg').css('left', bleft);
	});
}

function sendForm() {
	var post = new Object();
	//var compiled_form = $("#compiled_form");
	var compiled_form = form_copy;
	compiled_form.find(".default_value").each(function() {
		if(this.value == this.defaultValue) {
			this.value = 'Non specificato';
		}
	});
	post.nome = compiled_form.find("#nome").val();
	post.cognome = compiled_form.find("#cognome").val();
	post.telefono = compiled_form.find("#telefono").val();
	post.fax = compiled_form.find("#fax").val();
	post.email = compiled_form.find("#email").val();
	post.servizio = compiled_form.find("#servizio").val();
	post.richiesta = compiled_form.find("#richiesta").val();
	//post.email_redazione = compiled_form.find("#email_redazione").val();
	//post.email_operatore = compiled_form.find("#email_operatore").val();
	
	var required = ['nome', 'cognome', 'email', 'richiesta'];
	for(var idx in required) {
		var field = required[idx];
		if(post[field] == null || post[field].toString().length <= 0 || post[field] == compiled_form.find("#"+field)[0].defaultValue) {
			alert("Riempire tutti i cambi obbligatori");
			return false;
		}
	}
	
	//Email validation
	var regexp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(!regexp.test(post.email)) {
		alert("Inserire una email valida");
		return false;
	}
	
	if(!compiled_form.find("#privacy")[0].checked) {
		alert("Accettare il trattamento dei dati");
		return false;
	}
	
	compiled_form.find("#form_content").fadeOut('slow', function() {
		compiled_form.find("#form_loading_msg").fadeIn(function() {
			$.post('actions/sendmail.php', post, function(res) {
				if(res < 0) {
					//Errore
					//alert("Errore durante l'invio, riprovare ("+res+")");
					compiled_form.find("#form_loading_msg").text("Errore durante l'invio del messaggio, riprovare");
				} else {
					//alert("Richiesta inoltrata con successo");
					compiled_form.find("#form_loading_msg").text("Richiesta inoltrata con successo");
					setTimeout(closeForm, 3000);
				}
			});
		});
	});
}

function closeForm() {
	$.unblockUI();
	form_copy.html('');
	form_copy.remove();
	form_copy = null;
	$(window).unbind('resize');
	restartAllAnimation();
}

