/* 
	Copyright (c) 2007 Vericia Internet
	Christiano Anderson <anderson@vericia.com>
	Created at 2007-11-14
	Version 0.0.3
*/

$(function() {

	var email_err = true;
	var password1_err = true;
	var password2_err = true;
	var name_err = true;
	var cidade_err = true;
	var estado_err = true;
	var msg_adicional = '';
	

	// Select city form
	$("select[@name=listEstados]").change(function() {
		
		$('select[@name=listCidades]').html('<option value= "0">Aguarde...</option>');
		
		$.post(url_inc + '/ws_get_city/',
			{ cityid: $(this).val() },
				function(resposta) {
					$('select[@name=listCidades]').html(resposta);
			});
		cidade_err = false;
		estado_err = false;
		//$('#listCidadesmsg').html("<img src='/static/img/ok.png'>");
		//$('#listEstadosmsg').html("<img src='/static/img/ok.png'>");
	});
		
	// console.log(inputs.join('&'));

	// Submit form

	$('#submitbutton').click(function() {
	
		if (email_err == true && password1_err == true && password2_err == true && name_err == true && cidade_err == true && estado_err == true) {
			alert("Por favor, complete todos os campos do formulario");
			return false;	
		} 
		
		
		
		else {
			$('#emailmsg').html("");
					
			//console.log(inputs.join('&'));
			//console.log(inputs);
			jQuery.ajax({
				type: 'POST',
				data: 'email=' + $('#emailcad').val() + 
					'&password1=' + $('#password1').val() + 
					'&password2=' + $('#password2').val() + 
					'&name=' + $('#name').val() +
					'&gender=' + $('#gender').val() +
					'&maritalstatus=' + $('#maritalstatus').val() +
					'&educationlevel=' + $('#educationlevel').val() +
					'&son=' + $('#son').val() +
					'&ano_nasc=' + $('#ano_nasc').val() +
					'&listEstados=' + $('#listEstados').val() +
					'&listCidades=' + $('#listCidades').val() +
					'&usertype=' + $('#usertype').val(),
				url: url_inc + '/signup3/',
				timeout: 2000,
				error: function() {
					console.log("Falhou");
				},
				success: function(r) {
					// alert(r);
					if (r == "ok") {
						$('#submitbutton').hide();
						$('#msg_err').html("Cadastro realizado com sucesso. <a href='/profile/'>Clique aqui para continuar</a>");
					} else {
						if (email_err == true) {
							msg_adicional = msg_adicional + "E-mail invalido, ";
						}
						if (password1_err == true || password2_err == true) {
							msg_adicional = msg_adicional + "Senhas invalidas, ";
						}
						if (name_err == true) {
							msg_adicional = msg_adicional + "Nome invalido, ";
						}
						if (cidade_err == true || estado_err == true) {
							msg_adicional = msg_adicional + "Por favor, selecione a cidade e estado onde mora, ";
						}
						alert("Erro no preenchimento do formulario - " + msg_adicional);
						
					}
				
				}
			})	
		return false;	
		}
		
		
	})

	// email field
	$('#emailcad').focus(function() {
		$('#emailmsg').html("&nbsp;");
	})
	
	$('#emailcad').blur(function() {
		jQuery.ajax({
			type: 'POST',
			data: 'email=' + $('#emailcad').val(),
			url: url_inc + '/ws_verify_signup_fields/',
			timeout: 2000,
			error: function(r) {
				console.log("Erro")
			}
			,
			success: function(r) {
				// $('#emailmsg').replaceWith("<img src='/static/img/ok.png'>");
				if (r == "ok") {
					email_err = false;
					//$('#emailmsg').html("<img src='/static/img/ok.png'>");
				} else {
					email_err = true;
					$('#msg_err').html(r);
				}
			}
		})
		return false;
	})
	// end email field
	

	// password1 field
	$('#password1').focus(function() {
		$('#password1msg').html("&nbsp;");
		//$('#password2').replaceWith('');
	})
	
	$('#password1').blur(function() {
		jQuery.ajax({
			type: 'POST',
			data: 'password1=' + $('#password1').val(),
			url: url_inc + '/ws_verify_signup_fields/',
			timeout: 2000,
			error: function(r) {
				console.log("Erro")
			}
			,
			success: function(r) {
				// $('#emailmsg').replaceWith("<img src='/static/img/ok.png'>");
				if (r == "ok") {
					password1_err = false;
					//$('#password1msg').html("<img src='/static/img/ok.png'>");
				} else {
					password1_err = true;
					$('#password1msg').html("<img src='/static/img/err.png'>" + " " + r);
				}
			}
		})
		return false;
	})
	// end password1 field

	// password2 field
	$('#password2').focus(function() {
		$('#password2msg').html("&nbsp;");
	})
	
	$('#password2').blur(function() {
		jQuery.ajax({
			type: 'POST',
			data: 'password2=' + $('#password2').val() + '&password1_ck=' + $('#password1').val(),
			url: url_inc + '/ws_verify_signup_fields/',
			timeout: 2000,
			error: function(r) {
				console.log("Erro")
			}
			,
			success: function(r) {
				// $('#emailmsg').replaceWith("<img src='/static/img/ok.png'>");
				if (r == "ok") {
					password2_err = false;
					//$('#password2msg').html("<img src='/static/img/ok.png'>");
				} else {
					password2_err = true;
					$('#password2msg').html("<img src='/static/img/err.png'>" + " " + r);
				}
			}
		})
		return false;
	})
	// end password2 field

	// name field
	$('#name').focus(function() {
		$('#namemsg').html("&nbsp;");
	})
	
	$('#name').blur(function() {
		jQuery.ajax({
			type: 'POST',
			data: 'name=' + $('#name').val(),
			url: url_inc + '/ws_verify_signup_fields/',
			timeout: 2000,
			error: function(r) {
				console.log("Erro")
			}
			,
			success: function(r) {
				// $('#emailmsg').replaceWith("<img src='/static/img/ok.png'>");
				if (r == "ok") {
					name_err = false;
					//$('#namemsg').html("<img src='/static/img/ok.png'>");
				} else {
					name_err = true;
					$('#namemsg').html("<img src='/static/img/err.png'>" + " " + r);
				}
			}
		})
		return false;
	})
	// end name field


})
