/* *************************** REGISTRATION ********************** */
function user_register_show()
{
	if (!document.sending) {
		document.sending = true;
		jQuery.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/user_register_show.php",
			data: { },
			success: function(response) {
				document.sending = false;
				$('#container_popup').html(response);
				$('#container_popup').dialog({
					modal: true,
                    open: function(){$("#pap_datepicker").datepicker({
						changeMonth: true,
						changeYear: true,
                                                yearRange: '1900:2011',
                                                dateFormat: 'dd/mm/yy'
					});}
				});
			},
			error: function(obj, what_happen, other_obj) {
                document.sending = false;
			}
		});
	}
}


function user_register_hide()
{
	$('#container_popup').dialog('close');
	$('#container_popup').dialog('destroy');
}
function user_register_send()
{
   
	if (!document.sending && user_register_valid()) {
		document.sending = true;
		$('#user_reg_err').html(MSG_SENDING);
		jQuery.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/user_register_send.php",
			data: { name: $('#user_reg_name').val().trim(),
					lastname: $('#user_reg_lastname').val().trim(),
					country: $('#user_reg_country').val().trim(),
					city: $('#user_reg_city').val().trim(),
					email: $('#user_reg_email').val().trim(),
                                        birthyear: $('#pap_datepicker').val().trim(),
                                        language: $('#user_reg_language').val().trim(),
                                        gender: $('#user_reg_gender').val().trim(),
                                        ocupation: $('#user_reg_ocupation').val().trim(),
                                        poloRelation: $('#user_reg_polo_relation').val().trim(),
                                        countriesVisit: $('#user_reg_countries_visit').val().trim(),
					passw: $('#user_reg_passw1').val()
				},
			success: function(response) {
				document.sending = false;
				if (response == '0') {
					$('#user_reg_err').removeClass('error');
					$('#user_reg_err').html(MSG_DATA_SENT_OK_VALIDATE);
					setTimeout(function(){
						user_register_hide();
					}, 5000);
				} else if (response == '1') {
					$('#user_reg_err').html(MSG_EMAIL_EXIST);
				} else {
					$('#user_reg_err').html(MSG_DATA_SENT_ERROR);
				}
			},
			error: function(obj, what_happen, other_obj) {
                document.sending = false;
			}
		});
	}
}
function user_register_valid()
{
	var valid = false;
	$('#user_reg_err').html('');
	if ($('#user_reg_name').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURNAME_MISSING);
	}
	else if ($('#user_reg_lastname').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURLAST_MISSING);
	}
	else if ($('#user_reg_country').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURCOUNTRY_MISSING);
	}
	else if ($('#user_reg_city').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURCITY_MISSING);
	}
        else if ($('#user_reg_language').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOULANGUAGE_MISSING);
	}
        else if ($('#pap_datepicker').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURBIRTHYEAR_MISSING);
	}
        else if ($('#user_reg_gender').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURGENDER_MISSING);
	}
        else if ($('#user_reg_ocupation').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOUROCUPATION_MISSING);
	}
        else if ($('#user_reg_polo_relation').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURPOLORELATION_MISSING);
	}
        else if ($('#user_reg_countries_visit').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOURCOUNTRIESVISIT_MISSING);
	}
	else if ($('#user_reg_email').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_YOUREMAIL_MISSING);
	}
	else if ($('#user_reg_passw1').val() != $('#user_reg_passw2').val())
	{
		$('#user_reg_err').html(MSG_PASSWDS_NO_MATCH);
	}
	else if ($('#user_reg_passw1').val().trim() == '')
	{
		$('#user_reg_err').html(MSG_BLOG_EDIT_PASSWORD_MISSING);
	}
	else
	{
		valid = true;
	}
	return valid;
}

/**
 * inicializa el datepicker con fechas anteriores a hoy, generalmente usado para fecha de nacimiento
 *
 */
function init_datePicker_past(){
 	$("#pap_datepicker").datepicker();
}

function eval_form_pap(){
	//var form = $('#form_agenda_pap');
	if (inputIsEmpty('pap_datepicker')) {
		$('#msg_error_pap').html(MSG_COMPLETAR_FECHA);
		$('#pap_datepicker').focus();
	}else if ($('#pap_anexo').val() < 1) {
		$('#msg_error_pap').html(MSG_COMPLETAR_ANEXO);
		$('#pap_anexo').focus();
	}else{
		return true;
	}
	return false;
}


/* *************************** LOGIN ********************** */
function user_login_show()
{
	if (!document.sending) {
		document.sending = true;
		jQuery.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/user_login_show.php",
			data: { },
			success: function(response) {
				document.sending = false;
				$('#container_popup').html(response);
				$('#container_popup').dialog({
					modal: true
				});
			},
			error: function(obj, what_happen, other_obj) {
                document.sending = false;
			}
		});
	}
}
function user_login_hide()
{
	$('#container_popup').dialog('close');
	$('#container_popup').dialog('destroy');
}
function user_login_send()
{
	if (!document.sending) {
		document.sending = true;
		jQuery.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/user_login_send.php",
			data: { email: $('#user_login_email').val().trim(), passw: $('#user_login_passw').val() },
			success: function(response) {
				document.sending = false;
				if (response == '0') {
					// Redireccionar (server || dev):
					if (URI.substr(0, 9) == 'register/' || URI.substr(0, 26) == 'living_polo_2010/register/')
						window.location.href = WEB_PATH;
					else
						window.location.href = WEB_PATH + URI;
				} else {
					$('#user_login_err').html(MSG_LOGIN_ERROR);
				}
			},
			error: function(obj, what_happen, other_obj) {
                document.sending = false;
			}
		});
	}
}
function user_logout()
{
	if (!document.sending) {
		document.sending = true;
		jQuery.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/user_logout.php",
			data: {  },
			success: function(response) {
				document.sending = false;
				window.location.href = WEB_PATH + URI;
			},
			error: function(obj, what_happen, other_obj) {
                document.sending = false;
			}
		});
	}
}

/* *************************** FORGOT PASSWORD ********************** */
function user_forgotpassw_show()
{
	if (!document.sending) {
		document.sending = true;
		jQuery.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/user_forgotpassw_show.php",
			data: { },
			success: function(response) {
				document.sending = false;
				$('#container_popup').html(response);
				$('#container_popup').dialog({
					modal: true
				});
			},
			error: function(obj, what_happen, other_obj) {
                document.sending = false;
			}
		});
	}
}
function user_forgotpassw_hide()
{
	$('#container_popup').dialog('close');
	$('#container_popup').dialog('destroy');
}
function user_forgotpassw_send()
{
	if (!document.sending) {
		document.sending = true;
		jQuery.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/user_forgotpassw_send.php",
			data: { email: $('#user_forgotpassw_email').val().trim() },
			success: function(response) {
				document.sending = false;
				if (response == '0') {
					$('#user_forgotpassw_err').html(MSG_DATA_SENT_FORGOTPASSW);
					setTimeout(function(){
						user_forgotpassw_hide();
					}, 5000);
				} else {
					$('#user_forgotpassw_err').html(MSG_EMAIL_NOT_VALID);
				}
			},
			error: function(obj, what_happen, other_obj) {
                document.sending = false;
			}
		});
	}
}
