$(document).ready(function() {
	$('#rapide1').click(function(){ $('div#opened1').slideDown('slow'); } );
	$('#part1').mouseleave(function(){ $('div#opened1').slideUp('slow'); } );
	$('#rapide2').click(function(){ $('div#opened2').slideDown('slow'); } );
	$('#part2').mouseleave(function(){ $('div#opened2').slideUp('slow'); } );
	$('#rapide3').click(function(){ $('div#opened3').slideDown('slow'); } );
	$('#part3').mouseleave(function(){ $('div#opened3').slideUp('slow'); } );
	$('#rapide4').click(function(){ $('div#opened4').slideDown('slow'); } );
	$('#part4').mouseleave(function(){ $('div#opened4').slideUp('slow'); } );
	
	function sanitize(s) {
		return s.replace(/[^a-zA-Z 0-9.@-_?![]]+/g, ""); 
	};

	$('#contact_nome').blur(function () { validate('contact', 'nome'); });
	$('#contact_endereco').blur(function () { validate('contact', 'endereco'); });
	$('#contact_email').blur(function () { validateEmail('contact', 'email'); });
	$('#contact_indic').blur(function () { validate('contact', 'indic'); });
	$('#contact_tel').blur(function () { validate('contact', 'tel'); });
	$('#contact_estado').blur(function () { validate('contact', 'estado'); });
	$('#contact_cidade').blur(function () { validate('contact', 'cidade'); });
	$('#contact_mensagem').blur(function () { validate('contact', 'mensagem'); });
	$('#form_contact').submit(function() {
		return validContact();
	});
});

function valid (evt, type) {
	if      (type == "alphanum")	var interdit = '\'&\t#~"^%$£²¤§*°¨µ{}<>|\\`';
	else if (type == "alphanum2")	var interdit = '+&*?!:;,\t#~"^%$£²¤§*@°¨µ_.()[]{}<>|\\/`';
	else if (type == "alphanum3")	var interdit = '+\'&*;,\t#~"^%$£²¤§*@°¨µ_.{}<>|\\/`';
	else if (type == "alpha")		var interdit = '1234567890+&*?!:;,\t#~"^%$£?²¤§*@°¨µ-_.()[]{}<>|\\/`\'';
	else if (type == "area")		var interdit = '+äãçëìïòöõüñ\t#~"%$£²¤§°¨(){}<>|\\/`\''; 
    else if (type == "site")		var interdit = '+*!;,\t#"^$£²¤§%*@°¨µ()[]{}<>|\`\''; 
	else if (type == "num")			var interdit = '+azertyuiopqsdfgh mnbvcxwAZERTYUIOPMLKJHGFDSQWXCVBN&*?@_ !-:;,\t#~"^$£?²¤§%*°¨µ()[]{}<>|\\`\''; 
	else if (type == "email")		var interdit = '+ &*?!:;,\t#~"^%$£?²¤§*°¨µ()[]{}<>|\\/`\''; 
	else							var interdit = '+&*?!:;,\t#~"^%$£?²¤§*°¨µ@_.()[]{}<>|\\/`\''; 
	var keyCode = evt.which ? evt.which : evt.keyCode;
	if (keyCode == 9 || keyCode == 71 || keyCode == 123)
		return true;
	if (interdit.indexOf(String.fromCharCode(keyCode)) >= 0)
	{
		return false;
	}
}

function validate(type, input) {
	if ($('#' + type + '_' + input).val() == '')
	{
		bad(type, input);
		return 'bad';
	}
	else
	{
		good (type, input);
		return 'good';
	}
}

function validateEmail(type, input) {
	_email = escape($('#' + type + '_' + input).val());
	var arobase = _email.indexOf("@")
	var point = _email.lastIndexOf(".")

	if ((arobase < 3)||(point + 2 > _email.length) ||(point < arobase+3)) 
	{
		bad(type, input);
		return 'bad';
	}
	else
	{
		good(type, input);
		return 'good';
	}	
}

function validContact() {
	$('.error').fadeOut();
	var error = 0
	var errormsg = ''
	$('#confirm_msg').fadeOut("slow");
	$('.confirm').fadeOut("slow");
	$('#error_msg').html('')
	$('.error').fadeOut(1000)
	
	if (validate ('contact', 'nome') == 'bad')
	{
		error = 1;
	}
	if (validate ('contact', 'endereco') == 'bad')
	{
		error = 1;
	}
	if (validateEmail ('contact', 'email') == 'bad')
	{
		error = 1;
	}
	if (validate ('contact', 'indic') == 'bad')
	{
		error = 1;
	}
	if (validate ('contact', 'tel') == 'bad')
	{
		error = 1;
	}
	if (validate ('contact', 'estado') == 'bad')
	{
		error = 1;
	}
	if (validate ('contact', 'cidade') == 'bad')
	{
		error = 1;
	}
	if (validate ('contact', 'mensagem') == 'bad')
	{
		error = 1;
	}	

	if (error == 0)
	{
		send_msg();
	}
	return false;
}

function good (type, input) {
	$('#' + type + '_' + input).css('border-color', '#00cc00');
}

function bad (type, input) {
	$('#' + type + '_' + input).css('border-color', '#CD0A0A');
}

function reset (type, input) {
	$('#' + type + '_' + input).css('border-color', '#666');
}

$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

function send_msg() {
	$.ajax({
		type: "GET",
		url: "include/send_contact.php",
		data: "nome=" + $('#contact_nome').val() + "&endereco=" + $('#contact_endereco').val() + "&email=" + $('#contact_email').val() + "&indic=" + $('#contact_indic').val() + "&tel=" + $('#contact_tel').val() + "&estado=" + $('#contact_estado').val() + "&cidade=" + $('#contact_cidade').val() + "&mesagem=" + $('#contact_mensagem').val(),
		success: function() {
			reset ('contact', 'nome');
			reset ('contact', 'endereco');
			reset ('contact', 'email');
			reset ('contact', 'indic');
			reset ('contact', 'tel');
			reset ('contact', 'estado');
			reset ('contact', 'cidade');
			reset ('contact', 'mesagem');
			$('form').clearForm();
			alert ('Contato pedido salva com sucesso. Ele será processado o mais rapidamente possível...');
			$('#confirm_msg').fadeIn("slow");
		}
	});
}

function setCursor (el, st, end) {
	if (el.setSelectionRange)
	{
		el.focus();
		el.setSelectionRange (st, end);
	}
	else
	{
		if (el.createTextRange)
		{
			range = el.createTextRange();
			range.collapse(true);
			range.moveEnd('character', end);
			range.moveStart('character', st);
			range.select();
		}
	}
}
