//File: user.js
var bn_user = {
favourites: new Array(),
user: null,
nickname: null,
recently_viewed: {length: 0},
avatar: 'nobody',
public_profile: 0,
//do_after_login: null,
//arguments_after_login: null,
//Overridable functions for what to do on each page after login or slay
after_login: function() {},
after_slay: function() {},
after_logout: function() {},
/**
* Destroy the session then slay the user
*/
logout: function() {
// Reason for the next bit of code:
//We need to make sure that no matter what all privledges are taken away
//that is why we use the slay. Inside the slay is a call to an overridable
//function called after_slay, and here is a call to after_logout which is
//also overridable. This means that each page can set what these actions
//do. We need to do both after_slay and after_logout because the page should
//never be redirected until the stax logout has completed because if the
//next page loads and the user is still logged in(the stax call has not yet
//completed) the user will appear to be logged in on the next page but then
//the stax will complete destroying the session. Bad.
//The solution is to continue with the slay no matter what, but only logout
//after the stax call is finished. HENCE: IF YOUR LOGOUT ON A PAGE REQUIRES
//A REDIRECT TO A NEW PAGE, YOU MUST ___MUST___ USE AFTER LOGOUT!! THIS IS
//YOUR WARNING. IF YOU HAVE SIMILAR ISSUES, YOU DID NOT READ THIS FIRST OR
//DID NOT PROPERLY HEED MY WARNING TO REMEMBER THIS WARNING!
_logout(function(ret){
bn_user.after_logout();
data = JSON.parse(ret);
if (data.success) {
}
});
bn_user.slay();
},
/**
* Takes away user priveleges in javascript
* @return void
*/
slay: function(){
bn_user.user = null;
bn_user.nickname = null;
bn_user.recently_viewed = {};
bn_user.avatar = 'nobody';
bn_user.public_profile = 0;
bn_user.generate_greeting();
this.favourites = '';
return bn_user.after_slay();
},
/**
* Checks if a user is logged in and logs the user in
* @param recall boolean: recall the calling function on successful login
* @return boolean
*/
check_login: function(recall, cb) {
if (this.user == null) {
if (recall) {
do_after_login = arguments.callee.caller;
arguments_after_login = arguments.callee.caller.arguments;
} else {
$("#LOGIN_ERROR").empty();
}
$('#LOGIN_FORM').submit(function(){
$("#LOGIN_ERROR").empty();
if ($('#LOGIN_USERNAME').val() == '' || $('#LOGIN_PASSWORD').val() == '') {
$("#LOGIN_ERROR").html('You must enter a username and a password. ');
$("#LOGIN_ERROR").show();
return false;
}
_do_login($('#LOGIN_USERNAME').val(), $('#LOGIN_PASSWORD').val(), $('#LOGIN_REMEMBER').attr('checked'), function(ret){
data = JSON.parse(ret);
if (data.success) {
$("#LOGIN_WINDOW").jqmHide();
bn_user.user = data.user;
bn_user.nickname = data.nickname;
bn_user.public_profile = data.public;
bn_user.avatar = data.avatar;
//Build the top link area
//$('#GREETING').empty();
//$('#GREETING').append('Welcome, ' + bn_user.nickname + '. Sign Out');
//$('#MY_SAVED_NAMES_TOPLINK').show();
bn_user.favourites = data.favourites;
if (typeof(do_after_login) != 'undefined' && do_after_login != null) {
do_after_login.apply(this, arguments_after_login);
do_after_login = null;
}
bn_user.recently_viewed = data.recent;
bn_user.generate_greeting();
bn_user.after_login();
if($('input#LOGIN_REMEMBER').attr('checked')) {
$.cookie('REMEMBER_USER', $('#LOGIN_USERNAME').val(), {expires: 365});
}
} else {
if (data.auth_code == 'LOCKED') {
$("#LOGIN_ERROR").html('Your account has been lock please try again in 20 minutes. ');
} else {
$("#LOGIN_ERROR").html('Could not log you in. Please check your credentials. ');
}
$("#LOGIN_ERROR").show();
}
});
return false;
});
//cookie for remembering user login id
if($.cookie('REMEMBER_USER'))
$('#LOGIN_USERNAME').val($.cookie('REMEMBER_USER'));
else
$('#LOGIN_USERNAME').val('');
$('#LOGIN_PASSWORD').val('');
$('#LOGIN_WINDOW').jqmShow();
if($.cookie('REMEMBER_USER')) {
$('input#LOGIN_REMEMBER').attr('checked', true);
if($('#LOGIN_PASSWORD').val() != '')
$('input#LOGIN_SUBMIT').focus();
else
$('input#LOGIN_PASSWORD').focus();
}
return false;
} else {
return true;
}
},
/**
* Generates the top link area for logout/login and recently viewed
*/
generate_greeting: function() {
$('#GREETING').empty();
$('#RECENT').empty();
$('#RECENT').append('
Recently Viewed
');
if (bn_user.recently_viewed.length) {
$('#RECENT').append('
');
for (var x = 0; x < bn_user.recently_viewed.length; x++) {
$('#bcrumbs').append('
' + bn_user.recently_viewed[x].NAME + '
');
favourite = false;
for (var i = 0; i < bn_user.favourites.length; i++) {
if (bn_user.favourites[i].NAME == bn_user.recently_viewed[x].ID && bn_user.favourites[i].CATEGORY == 1) {
favourite = true;
break;
}
}
recent_names_content = '';
recent_names_content += '
';
if($('#MY_SAVED_NAMES_TOPLINK').data("qtip"))
$('#MY_SAVED_NAMES_TOPLINK').qtip("destroy");
//$('#MY_SAVED_NAMES_TOPLINK').qtip({style: {border: '0px', background: 'none'}, content: saved_names_content, position: {adjust: {x: 0, y: -12}, corner: { tooltip: 'topRight'}}, hide: { fixed: true }});
$('#GREETING').qtip({style: {border: '0px', background: 'none'}, content: saved_names_content, position: {adjust: {x: 26, y: -7}, corner: { tooltip: 'topRight'}}, hide: { fixed: true }});
},
send_support: function() {
$('#SUCCESS').hide();
$('#FAIL').hide();
data = {
'NICKNAME': $('#NICKNAME').val(),
'NAME': $('#NAME').val(),
'EMAIL': $('#EMAIL').val(),
'COMMENT': $('#COMMENT').val(),
'SECURITY': $('#CAP_CONFIRM').val()
};
_send_support(JSON.stringify(data), function(ret) {
data = JSON.parse(ret);
if (data.success) {
$('#SUCCESS').html(data.message);
$('#SUCCESS').show();
$('#contact_form').hide();
} else {
$('#FAIL').html(data.message);
$('#FAIL').show();
}
return true;
});
}
};
$(document).ready(function(){
$('#LOGIN_USERNAME').watermark('Your Email', 'watermark');
$('#FORGOT_PASSWORD_USERNAME').watermark('Your Email', 'watermark');
$('#FORGOT_PASSWORD_FORM').submit(function(){
$("#FORGOT_PASSWORD_WINDOW").jqmHide();
_send_password($('#FORGOT_PASSWORD_USERNAME').val(), function(ret) {
data = JSON.parse(ret);
if (data.success) {
bn_global.display_modal_message('Your password has been sent.');
} else {
bn_global.display_modal_message('Your password could not be sent.');
}
return false;
});
return false;
});
bn_user.generate_greeting();
var saved_names_content = '';
});
//File: favourite.js
var bn_favourite = {
favimg: '/images/favourite.png',
nonfavimg: '/images/nonfav.png',
confirm_add_delete: true,
/**
* Stax call to delete a favourite if viewing it within a users profile(i.e. not search or detail page)
*/
delete_favourite_by_id: function(id, name_id, category, confirm) {
if (bn_favourite.confirm_add_delete && typeof(confirm) == 'undefined'){
$('#DELETE_FAVOURITE').jqmShow();
$('#FAVOURITE_DELETE_YES').unbind('click').click(function(){$('#DELETE_FAVOURITE').jqmHide(); bn_favourite.delete_favourite_by_id(id, name_id, category, true);return false;});
return;
}
_delete_favourite_by_id(id, function(ret) {
data = JSON.parse(ret);
bn_global.stax_fail(data);
//Remove li from MSN
if (data.success && typeof(bn_mysavednames) != 'undefined') {
bn_user.favourites = data.favourites;
$("li#name_div_" + id).remove();
//if the user removed this name but still has another somewhere
still_exists = false;
for (var z in bn_user.favourites) {
if (bn_user.favourites[z].NAME == name_id) {
still_exists = true;
break;
}
}
if (!still_exists)
$('#RECENT_ADD_' + name_id).text('Save Name');
if (category == 2) {
bn_mysavednames.build_middle_names_list();
if (!$('#FAVOURITE_MIDDLE_NAMES li').size())
$('#MIDDLE_NAME_CONTAINER').hide();
}
$('select[id^=MIDDLE_NAME_SELECT_] option:selected[value="' + id + '"]').parents('span').empty();
}
});
},
/*
* Stax call to delete a favourite from the database based on the name(for when outside of profile views)
*/
delete_favourite: function (id, confirm) {
if (bn_favourite.confirm_add_delete && typeof(confirm) == 'undefined'){
$('#DELETE_FAVOURITE').jqmShow();
$('#FAVOURITE_DELETE_YES').unbind('click').click(function(){$('#DELETE_FAVOURITE').jqmHide(); bn_favourite.delete_favourite(id, true);});
return;
}
if (confirm == true) {
_delete_favourite(id, function(ret){
var data = JSON.parse(ret);
bn_global.stax_fail(data);
if (data.success) {
if (typeof(bn_search) != 'undefined') {
$('#FAVIMG_' + id).attr('src', bn_favourite.nonfavimg);
$('#EXACT_FAVIMG_' + id).attr('src', bn_favourite.nonfavimg);
}
$('#RECENT_ADD_' + id).text('Save Name');
bn_user.favourites = data.favourites;
if (typeof(bn_mysavednames) != 'undefined') {
bn_mysavednames.build_middle_names_list();
bn_mysavednames.list_mysavednames();
$('#FAVOURITE_NAMES select option:selected[name="' + id + '"]');
}
bn_user.generate_my_saved_names();
if (typeof(bn_detail) != 'undefined') {
bn_detail.set_favourite_action();
}
}
});
}
},
/**
* Add a favourite to the database
*/
add_favourite: function(id, comment, delete_if_exists, category) {
if ($('#ADD_NAME_STATUS')) {
$('#ADD_NAME_STATUS').removeClass('ADD_NAME_SUCCESS','ADD_NAME_FAIL');
$('#ADD_NAME_STATUS').html('');
}
if (id.length == 0) {
$('#ADD_NAME_STATUS').html('Please enter a name.');
$('#ADD_NAME_STATUS').addClass('ADD_NAME_FAIL');
$('#ADD_NAME_STATUS').show();
return false;
}
if (category == 2) {
for(var i in bn_user.favourites) {
if (bn_user.favourites[i]['CATEGORY'] == 2 && bn_user.favourites[i]['NAME_TEXT'].toLowerCase() == id.toLowerCase()) {
bn_global.display_modal_message('
You cannot have duplicate middle names.', true);
return;
}
}
}
if (!bn_user.check_login(true))
return;
//if the name already exists, user is calling to delete the name
if(typeof(delete_if_exists) != 'undefined' && delete_if_exists)
for (var x in bn_user.favourites){
if (id == bn_user.favourites[x].NAME) {
bn_favourite.delete_favourite(id);
return;
}
}
// if (bn_favourite.confirm_add_delete && typeof(comment) == 'undefined') {
//
// $('#FAVOURITE_ERROR').hide();
//
// $('#FAVOURITE_SUBMIT').unbind('click').click(function(){bn_favourite.add_favourite(id, $('#FAVOURITES_COMMENT').val());});
//
// $('#ADD_FAVOURITE').jqmShow();
//
// return;
// }
if (typeof(category) == 'undefined')
category = 1;
if (typeof(comment) == 'undefined')
comment = '';
_add_favourite(id, comment, category, function(ret) {
var data = JSON.parse(ret);
bn_global.stax_fail(data);
if (data.success) {
//fewafwea
$('#ADD_FAVOURITE').jqmHide();
bn_user.favourites = data.favourites;
for (var z in data.added) {
$('#RECENT_ADD_' + data.added[z].NAME).text('Delete name');
}
var new_fav_count = parseInt($('#FAV_COUNT').html()) + 1;
$('#FAV_COUNT').empty();
$('#FAV_COUNT').append(new_fav_count);
bn_user.generate_my_saved_names();
if (typeof(bn_detail) != 'undefined') {
bn_detail.set_favourite_action();
}
if (typeof(bn_search) != 'undefined') {
$('#FAVIMG_' + id).attr('src', bn_favourite.favimg);
$('#EXACT_FAVIMG_' + id).attr('src', bn_favourite.favimg);
}
if (typeof(bn_mysavednames) != 'undefined') {
if (category == 2) {
bn_mysavednames.build_middle_names_list();
$('#MIDDLE_NAME_CONTAINER').show();
}
bn_mysavednames.list_mysavednames();
if (isNaN(data.added)) {
// names = new Array();
// names = id.split(',');
name_feedback = new Array();
for (var i in data.added) {
name_feedback.push(data.added[i].NAME.replace(/^\s+|\s+$/g,"") + ' added.');
}
$('#ADD_NAME_FEEDBACK').html(name_feedback.join(' '));
setTimeout(function(){$('#ADD_NAME_FEEDBACK').empty();}, 5000);
}
}
if ($('#ADD_NAME_STATUS')) {
$('#ADD_NAME_STATUS').addClass('ADD_NAME_SUCCESS');
$('#ADD_NAME_STATUS').html(''+ id +' added.');
$('#ADD_NAME_STATUS').show();
$('#ADD_NAME_NAME').val('');
}
} else {
if (typeof(data.user_logged_in) != 'undefined' && data.user_logged_in == false) {
bn_user.slay();
bn_user.check_login(true);
} else if (typeof(data.error_message) != 'undefined') {
$('#FAVOURITE_ERROR').empty();
$('#FAVOURITE_ERROR').append(data.error_message);
$('#FAVOURITE_ERROR').show();
}
}
});
}
};
//File: search.js
var bn_search = {
//Globals
page: 1,
search_term: '',
meaning: '',
/**
* Stax call to find names matching filters and search criteria
* @return
*/
search_names: function(search_term) {
//if ((typeof(search_term) == 'undefined' || search_term == '') && this.search_term == '')
//if ((typeof(search_term) == 'undefined') && this.search_term == '')
// return;
$('#pre_message').hide();
//Show a loading message in the search
$("#SEARCH_EXACT_RESULTS, #SEARCH_RESULTS").empty().hide();
$("#SEARCH_RESULTS").append('
Loading...').show();
if (typeof(search_term) == 'undefined') {
search_term = this.search_term;
}
//Check if the search term has changed
if (this.search_term != search_term) {
if (search_term.length != 1) {
$('#FILTER_CELEBRITY_NAME').attr('checked', false);
$('#SEARCH_CELEBRITY').hide();
}
//If there is only one letter in the search term, start a begins with
//search and set the begins with filter appropriately
if (search_term.length == 1) {
$('#FILTER_BEGINS_WITH option[value="' + search_term + '"]').attr('selected', 'selected');
} else {
$('#FILTER_BEGINS_WITH option[value=""]').attr('selected', 'selected');
}
$('#FILTER_NOT_BEGINS_WITH').val('');
$('#FILTER_ENDS_WITH').val('');
$('#FILTER_NOT_ENDS_WITH').val('');
this.search_term = search_term;
}
//Synchronize the search text box
$('#SEARCH').val(this.search_term);
//Prepare the data for JSON conversion
var filter = {
'FILTER_BEGINS_WITH': $('#FILTER_BEGINS_WITH').val(),
'FILTER_NOT_BEGINS_WITH': $('#FILTER_NOT_BEGINS_WITH').val(),
'FILTER_ENDS_WITH': $('#FILTER_ENDS_WITH').val(),
'FILTER_NOT_ENDS_WITH': $('#FILTER_NOT_ENDS_WITH').val(),
'FILTER_ORIGIN': $('#FILTER_ORIGIN').val(),
'FILTER_MEANING': $('#SEARCH_MEANING').val(),
'FILTER_BOY': $('#FILTER_BOY').is(':checked'),
'FILTER_GIRL': $('#FILTER_GIRL').is(':checked'),
'FILTER_UNISEX': $('#FILTER_UNISEX').is(':checked'),
'FILTER_CELEBRITY_NAME': $('#FILTER_CELEBRITY_NAME').is(':checked')
};
$('#LETTER_LIST [id^=letter_]').attr('class', 'letters');
if (bn_search.search_term.length == 1) {
$('#LETTER_LIST #letter_' + bn_search.search_term.toUpperCase()).attr('class', 'letter_selected');
}
//Dispatch Stax AJAX call
_search_names(this.search_term, this.page, JSON.stringify(filter), function (ret) {
$('#SEARCH_RESULTS').empty();
$('#SEARCH_NOTIFIER').empty();
//Parse the JSON string
var data = JSON.parse(ret);
if (data.total == 0) {
$('#SEARCH_NOTIFIER').append(' No results. Try to broaden your search critera.');
return;
}
$('#PREVIEW').show();
if (typeof(data.exact) != 'undefined' && typeof(data.exact['ID']) != 'undefined') {
// exact_match_html = '
';
exact_match_html = '';
var favourite = false;
if (bn_user.favourites != null)
for (var x = 0; x < bn_user.favourites.length; x++) {
if (data.exact['ID'] == bn_user.favourites[x].NAME) {
favourite = true;
break;
}
}
exact_match_html += '
Exact Match for "' + data.exact['NAME']['NAME'] + '"
';
$('#RANDOM_POLL').html(poll_html);
},
/**
* Stax call to get a random poll
*/
get_random_poll: function( ) {
_get_random_poll(function(ret){
data = JSON.parse(ret);
bn_global.stax_fail(data);
if (data.success) {
bn_global.random_poll=data.POLL_DATA;
bn_global.display_random_poll();
}
});
},
stax_fail: function(data) {
if (!data.success && typeof(data.user_logged_in) != 'undefined' && !data.user_logged_in) {
bn_user.after_slay = function() {};
bn_user.slay();
$('#MESSAGE_WINDOW').jqm({onHide:function(){location.href='/search';}});
bn_global.display_modal_message('You have been logged out. Please login again.');
} else if (!data.success) {
bn_global.display_modal_message();
}
},
/**
* Checks if the user is logged in, then redirects to the poll creation page
*/
create_new_poll: function() {
if (!bn_user.check_login(true)) {
return;
}
return(location.href='/poll');
},
/**
* Generate alphabetical listings on the page
* @return void
*/
letter_list: function() {
if (typeof(bn_search) != 'undefined')
for (var x = 65; x < 91; x++) {
char = String.fromCharCode(x);
css_style = 'letters';
if ($('#SEARCH').val().length == 1 && char == $('#SEARCH').val()) {
css_style = 'letter_selected';
}
$('#LETTER_LIST').append('
'+ char +'
');
$('#FILTER_BEGINS_WITH').append('');
$('#FILTER_NOT_BEGINS_WITH').append('');
$('#FILTER_ENDS_WITH').append('');
$('#FILTER_NOT_ENDS_WITH').append('');
}
else
for ( var x = 65; x < 91; x++) {
$('#LETTER_LIST').append('
' + String.fromCharCode(x) + '
');
}
},
/**
* Standard message display for the site
*/
display_modal_message: function(message, error) {
var error_colour = '#f69679';
var message_div = $('#MESSAGE_WINDOW');
var message_html = '';
if (typeof(message) == 'undefined') {
message = bn_global.unknown_error_message;
error = true;
}
if (typeof(error) != 'undefined') {
message_div.removeClass('std_message_modal');
message_div.addClass('error_message_modal');
//message_html += '