/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2008 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 170 2008-12-04 19:03:12Z emartin24 $
 *
 */
//link para host do ajax
var link = 'http://www.bbprevidencia.com.br/portal_novo_bbp/';


$(document).ready(function () {
	$('#contactForm input.contact, #contactForm span.contact').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("contact.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'contact-overlay',
				containerId: 'contact-container',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});

	// preload images
	var img = ['SPCBtnCancelar.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'SPCBtnEnviar.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = '../img/contact/' + this;
	});
});


//Verifica os dados do usuario que esqueceu a senha esta sendo usado no altSenha.php do portal_novo
var contact = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#contact-container .contact-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#contact-container .contact-input').css({
				'font-size': '.9em'
			});
		}

		// dynamically determine height
		var h = 280;
		if ($('#contact-subject').length) {
			h += 26;
		}
		if ($('#contact-cc').length) {
			h += 22;
		}

		var title = $('#contact-container .contact-title').html();
		$('#contact-container .contact-title').html('Carregando...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#contact-container .contact-content').animate({
						height: h
					}, function () {
						$('#contact-container .contact-title').html(title);
						$('#contact-container form').fadeIn(200, function () {
							$('#contact-container #contact-cpf').focus();

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#contact-container .contact-button').each(function () {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#contact-container .contact-send').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#contact-container .contact-title').html('to aki');
				$('#contact-container .contact-message').fadeOut(function () {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				$('#contact-container .contact-title').html('Enviando...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '150px'
				}, function () {
					$('#contact-container .contact-loading').fadeIn(200, function () {
						$.ajax({
							url: 'http://www.bbprevidencia.com.br/portal_novo_bbp/portal_novo/contact.php',
							data: $('#contact-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (xhr) {
								$('#contact-container .contact-loading').fadeOut(200, function () {
									$('#contact-container .contact-title').html('Obrigado!');
									$('#contact-container .contact-message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#contact-container .contact-message:visible').length > 0) {
					var msg = $('#contact-container .contact-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						contact.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#contact-container .contact-message').animate({
						height: '80px'
					}, contact.showError);
				}
				
			}
		});
		
		$("#contact-cpf").mask("999.999.999-99");
		$("#contact-dtNascimento").mask("99/99/9999");
	},
	close: function (dialog) {
		$('#contact-container .contact-message').fadeOut();
		$('#contact-container .contact-title').html('Obrigado...');
		$('#contact-container form').fadeOut(200);
		$('#contact-container .contact-content').animate({
			height: 80
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		contact.message = '';
		var cpf = $('#contact-container #contact-cpf').val();
		if (!cpf) {
			contact.message += 'Cpf &eacute; obrigat&oacute;rio.<br />';
			return false;
		}
		else {
			if (!contact.validateCpf(cpf)) {
				contact.message += 'Cpf inv&aacute;lido. ';
				return false;
			}
		}
		
		var email = $('#contact-container #contact-email').val();
		if( !email ){
			contact.message += 'E-mail &eacute; obrigat&oacute;rio.<br />'
			return false
		}else{
			if( !contact.validateEmail( email )){
				contact.message += 'E-mail inv&aacute;lido.<br />'
				return false;
			}
		}
		
		if (!$('#contact-container #contact-dtNascimento').val()) {
			contact.message += 'A data de nascimento &eacute; obrigat&oacute;rio.<br />';
			return false;
		}
		
		if (!$('#contact-container #contact-sexo').val()) {
			contact.message += 'Selecione uma op&ccedil;&atilde;o para sexo.<br />';
			return false
		}
		return true;

	},
	
	remove: function(str, sub) {
		  i = str.indexOf(sub);
		  r = "";
		  if (i == -1) return str;
		  r += str.substring(0,i) + contact.remove(str.substring(i + sub.length), sub);
		  return r;
	},
	validateCpf: function (cpf) {
  
	   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	   if(!filtro.test(cpf)){
		 return false;
	   }
	   
	   cpf = this.remove(cpf, ".");
	   cpf = this.remove(cpf, "-");
		
	   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
		  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
		  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
		  cpf == "88888888888" || cpf == "99999999999"){
		  return false;
	   }
	
	   soma = 0;
	   for(i = 0; i < 9; i++){
		 soma += parseInt(cpf.charAt(i)) * (10 - i);
	   }
	   
	   resto = 11 - (soma % 11);
	   
	   if(resto == 10 || resto == 11) {
		 resto = 0;
	   }
	   
	   if(resto != parseInt(cpf.charAt(9))){
		 return false;
	   }
	   
	   soma = 0;
	   
	   for(i = 0; i < 10; i ++){
		 soma += parseInt(cpf.charAt(i)) * (11 - i);
	   }
	   
	   resto = 11 - (soma % 11);
	   
	   if(resto == 10 || resto == 11){
		 resto = 0;
	   }
	   
	   if(resto != parseInt(cpf.charAt(10))){
		 return false;
	   }
	   return true;
	 },
	 
	 validateEmail: function ( email ){
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(email)) {
			return false;
		}
		return true;
	 },
	
	showError: function () {
		$('#contact-container .contact-message')
			.html($('<div class="contact-error">').append(contact.message))
			.fadeIn(200);
	}
};