var SITE_URL = "/routeone/ajax";

jQuery(document).ready(function() {

	jQuery.ajaxSetup( {
		contentType : "application/x-www-form-urlencoded;charset=iso-8859-1"
	});

	jQuery(function($) {
		$("#telefoneVisto").mask("(99)9999-9999");
	});

	var options = {
		url : SITE_URL + '?cmd=MailsSite', // override for form's 'action'
		// attribute
		beforeSubmit : beforeAjaxForm, // pre-submit callback
		success : successAjaxForm
	};

	$("#vistosForm").validate( {
		errorClass : "errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(options);
		},
		rules : {
			email : {
				required : true,
				email : true
			},
			mensagem : {
				required : true
			}
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});
	
	$("#franquias_informacoes_form").validate( {
		errorClass : "errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(options);
		},
		rules : {
			email : {
				required : true,
				email : true
			},
			name : {
				required : true
			},
			phone : {
				required : true
			},
			cep : {
				required : true
			},
			cidade : {
				required : true
			},
			uf : {
				required : true
			},
			local : {
				required : true
			},
			mobile : {
				required : true
			},
			pop : {
				required : true
			},
			profissao : {
				required : true
			},
			pop : {
				required : true
			},
			capital : {
				required : true
			}
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});

});

function beforeAjaxForm() {
	$("#waiting").show();
}

function successAjaxForm(responseText, statusText) {

	$("#success").hide();
	$("#error").hide();

	var params = {
		to : 'configuration',
		subject : '[ROUTE ONE] Solicitacao de Informacoes',
		html : responseText
	}

	$.post(SITE_URL + '?cmd=SendMail', params, function(data) {
		if (data == "true") {
			$("#waiting").hide();
			$("#success").show();

		} else {
			$("#waiting").hide();
			$("#error").show();
		}
	});
}

function normalizeForm(formId) {
	
	var text;
	var value;
	
	$("#" + formId + " :input").each(function(){
		
		value = $(this).val();
		text = normalizeText(value);
		
		$(this).attr("value", text);
		
	});
	
	$("#" + formId + " :textarea").each(function(){
		
		value = $(this).val();
		text = normalizeText(value);
		
		$(this).attr("value", text);
		
	});
	
}
	
function normalizeText(text) {
	
	if(text == undefined) {
		return undefined;
	}
	
	text = text.replace(new RegExp('[áãâã]','gi'), 'a'); 
	text = text.replace(new RegExp('[éèê]','gi'), 'e'); 
	text = text.replace(new RegExp('[íìî]','gi'), 'i'); 
	text = text.replace(new RegExp('[óòôõ]','gi'), 'o'); 
	text = text.replace(new RegExp('[úùû]','gi'), 'u'); 
	text = text.replace(new RegExp('[ç]','gi'), 'c');
	
	text = text.replace(new RegExp('[ÁÃÂÃ]','gi'), 'A'); 
	text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'E'); 
	text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'I'); 
	text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'O'); 
	text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'U'); 
	text = text.replace(new RegExp('[Ç]','gi'), 'C');

	return text;
};
