// Global variables
var queue = new Object;
var $dialog = $('#loginpopup')
.dialog({
autoOpen: false,
title: 'Login Dialog'
});
//var $dialog = $('#loginpopup')
// .dialog({
// autoOpen: false,
// title: 'Login Dialog'
// });
var $problemId = $('#theProblemId', '#loginpopup');
$("#newprofile").click(function ()
{
$("#login_div").hide();
$("#newprofileform").show();
});
/*
* Global function for adding problems
*/
var add_problem = function( )
{
problem_name = queue.problem_name;
problem_description = queue.problem_description;
log = queue.log;
dataString = queue.dataString;
cat_ids = queue.cat_ids;
//var dataString = 'problem_name=' + problem_name + '&problem_description=' + problem_description + '&log=' + log + '&cat_ids=' + cat_ids;
$.ajax({
type: "POST",
url: "/problems/add_problem_ajax.php",
data: dataString,
success: function(html)
{
// Hide she wait animation
$("#loading").hide();
error_message = '';
if ( html == 'no_problem_name' )
{
//alert ("no problem name");
error_message = 'You did not enter a problem name.';
$('.success').fadeOut(200).hide();
$('.error').fadeIn(200).show();
}
else
if ( html == 'no_problem_description' )
{
//alert ("no problem description");
error_message = 'You did not enter a problem description.';
$('.success').fadeOut(200).hide();
$('.error').fadeIn(200).show();
}
else
if ( html == 'not_logged_in' )
{
//alert ("not logged in");
error_message = 'You are not logged in.';
$('.success').fadeOut(200).hide();
$('.error').fadeIn(200).show();
}
else
if ( html == 'no_categories' )
{
//alert ("no categories");
error_message = 'You did not enter any problem categories.';
$('.success').fadeOut(200).hide();
$('.error').fadeIn(200).show();
}
else
{
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
// $('#recent_problems').html(html);
//alert ("add comment error, data: " + html);
// Here can update the right side of the screen with the newly entered information
//$('#create_problem')[0].reset();
//$(':text, :password, :file, SELECT', '#create_problem').val('');
// THIS WORKED.
$("#create_problem").get(0).reset();
window.location = "http://www.problemio.com/problems/problem.php?problem_id="+html;
}
//$(':input','#create_problem')
//.not(':button, :submit, :reset, :hidden')
//.val('')
//.removeAttr('checked')
//.removeAttr('selected');
} // End of success case
,
error: function(html)
{
// $('.success').fadeIn(200).show();
// $('.error').fadeOut(200).hide();
// alert ("error " + html );
}
}); // End of AJAX call
}
$(function()
{
$("#header_search").submit(function(e)
{
e.preventDefault();
$("#loading").show();
var search = $("#txtSearch").val();
var dataString = 'search=' + search;
// alert("searching: " + search);
// Send me email informing me that someone searched.
var alert_message = "Someone is trying to search for: " + search;
var alertString = 'message=' + alert_message;
// Now send me alert email
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
// Check if search not empty
if( !search )
{
// Show dialog to say that search string can not be empty.
$("
Search string can not be empty.
").dialog( {title: 'Empty Search String'} );
// Showing the wait image
$("#loading").hide();
// Send me email informing me that someone searched.
var alert_message = "Someone is trying to search but they ran into a validation error for: " + search;
var alertString = 'message=' + alert_message;
// Now send me alert email
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
}
else
{
// Showing the wait image
$("#loading").hide();
// redirect to the search results page.
window.location = "http://www.problemio.com/problems/search.php?search="+search;
}
$("#loading").hide();
return false;
});
$("#login_div input[type=submit]").click(function()
{
// Showing the wait image
$("#loading").show();
var email = $("#email").val();
var password = $("#user_pass").val();
var dataString = 'email='+ email + '&password=' + password;
if( !email )
{
$('.login_success_email_empty').fadeOut(200).hide();
$('.login_error_email_empty').fadeOut(200).show();
// Showing the wait image
$("#loading").hide();
}
else
if( !password || password.length < 5)
{
$('.password_success').fadeOut(200).hide();
$('.password_error').fadeOut(200).show();
// Showing the wait image
$("#loading").hide();
}
else
{
$.ajax({
type: "POST",
url: "../auth/login_ajax.php",
dataType: "json",
data: dataString,
success: function(json)
{
// Showing the wait image
$("#loading").hide();
$('.password_error').fadeOut(200).hide();
$('.no_such_user').fadeOut(200).hide();
$('.login_success_email_empty').fadeOut(200).hide();
$('.login_success').fadeIn(200).show();
// Closing the dialog box
$('#loginpopup').dialog('close');
// Swapping out the header div
$('#header_logged_out').hide();
$('#header_logged_in').show();
queue.login = true;
if (queue.voteUp)
{
vote(queue.voteUp , 1);
}
else
if ( queue.voteDown )
{
vote(queue.voteDown , 1);
}
else
if ( queue.add_problem )
{
// Lets call the add problem function
add_problem ();
queue.problem_name = name;
queue.problem_description = problem_blurb;
}
},
error : function(json)
{
// Showing the wait image
$("#loading").hide();
queue.login = false;
// Output the result.
errorMessage = json.responseText;
if ( errorMessage == 'no_such_user' )
{
$('.no_such_user').fadeOut(200).hide();
$('.no_such_user').fadeOut(200).show();
}
}
});
}
return false;
});
});
$(document).ready(function()
{
// Called right away after someone clicks on the vote up link
$('.vote_up').click(function(e)
{
e.preventDefault();
$("#loading").show();
var problem_id = $(this).attr("data-problem_id");
vote(problem_id , 1);
//Return false to prevent page navigation
return false;
});
$('.vote_down').click(function(e)
{
e.preventDefault();
$("#loading").show();
problem_id = $(this).attr("data-problem_id");
vote ( problem_id , -1 );
//Return false to prevent page navigation
return false;
});
// Global function
var vote = function(problem_id , vote)
{
var dataString = 'problem_id=' + problem_id + '&vote=' + vote;
// Email me that this error happened!!
var alert_message = "Someone is trying to vote on a problem: " + problem_id + " with vote: " + vote;
var alertString = 'message= ' + alert_message;
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
// The person is actually logged in so lets have him vote
$.ajax({
type: "POST",
url: "/auth/check_login.php",
dataType: "json",
success: function(data)
{
$.ajax({
type: "POST",
url: "/problems/vote.php",
data: dataString,
success: function(html)
{
$("#loading").hide();
if ( html == "not_logged_in" )
{
queue.login = false;
//set the current problem id to the one within the dialog
$problemId.val(problem_id);
// Email me that this error happened!!
var alert_message = "Someone tried to vote on a problem but was not logged in. Problem id: " + problem_id + " with vote: " + vote;
var alertString = 'message= ' + alert_message;
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
// Try to create the popup that asks user to log in.
$("#loginpopup").dialog( {title: 'Please Login To Vote'} ); // title: 'Login Dialog'
// prevent the default action, e.g., following a link
return false;
}
else
if ( html == "already_voted" )
{
// Display a dialog box saying that the user already voted
$('You already voted this way on this problem.
').dialog( {title: 'Already Voted'});
// show div which says un-important, hide div which says important
//$("#support").hide();
//$("#dont_support").show();
// Email me that this error happened!!
var alert_message = "Someone is trying to vote on a problem but had already voted that way: " + problem_id + " with vote: " + vote;
var alertString = 'message= ' + alert_message;
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
return false;
}
else
if ( html == "error_getting_vote" )
{
$('').html('Error getting existing votes.').dialog();
// Email me that this error happened!!
var alert_message = "Someone is trying to vote on a problem but encountered an error: " + html + " for problem_id: " + problem_id + " with vote: " + vote;
var alertString = 'message= ' + alert_message;
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
}
else
{
//if ( vote == -1 )
//{
// $("#support").show();
// $("#dont_support").show();
//}
//else
//{
// $("#support").hide();
// $("#dont_support").show();
//}
// Email me that this error happened!!
var alert_message = "Someone voted successfully a problem: " + problem_id + " with vote: " + vote;
var alertString = 'message= ' + alert_message;
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
// Now make a call to AJAX to get the count of votes
$.ajax({
type: "POST",
url: "/problems/get_vote_count.php",
data: dataString,
success: function(html)
{
var class_name = ".votes_"+problem_id;
if ( !html)
{
html = "0";
}
$(class_name).text(html);
return false;
}
});
return false;
}
},
error: function(html)
{
$("#loading").hide();
return false;
} // End of error case
}); // Closing inner AJAX call.
},
error: function(data)
{
$("#loading").hide();
$("#loginpopup").dialog( {title: 'Please Login To Vote'} );
// Email me that this error happened!!
var alert_message = "Someone tried to vote but was not logged in for problem: " + problem_id + " with vote: " + vote;
var alertString = 'message= ' + alert_message;
$.ajax({
type: "POST",
url: "/problems/send_email_alert_ajax.php",
data: alertString ,
success: function(data)
{
},
error: function(data)
{}
});
return false;
} // End of error
}); // End of outer AJAX.
return false;
};
}); // End of document.ready() check
//////////////////////////////////////////////
// Stuff for adding categories
//////////////////////////////////////////////
$(function()
{
function log( message )
{
$( "" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#category_field" ).autocomplete({
source: "/problems/get_categories_ajax.php",
minLength: 2,
select: function( event, ui )
{
log( ui.item ?
ui.item.value :
"Nothing selected, input was " + this.value );
}
});
$('#add_category').click(function()
{
//alert ("in add category");
// Now have to get value of the text entry area
var category = $("#category_field").val(); // Works
var text_area = $("#log").val();
//alert ("text_area: " + text_area);
// Should append to value of textarea what is in the category
if ( text_area )
{
//alert ("text area NOT EMPTY: " + text_area + " and category: " + category );
text_area = text_area + " , " + category;
}
else
{
//alert ("text area EMPTY: " + text_area + " and category: " + category );
text_area = category;
}
// Now replace old text_area with new one
$("#log").val(text_area);
// Now reset the text field
$("#category_field").val("");
// Make the categories box visible
document.getElementById("categories_box").style.display = 'block';
return false;
});
$('#category_form').bind('submit',function()
{
// Get the variables
var problem_id = 1;
var categories = $("#log").val();
var dataString = 'problem_id='+ problem_id + '&categories=' + categories;
//alert ("Data string: " + dataString);
// Now check if the problem_id or solution are empty
if( !categories )
{
$("#loading").hide();
$('#categories_success').fadeIn(200).hide();
$('#categories_error').fadeOut(200).show();
}
else
{
// Now check if the user is logged in
$.ajax({
type: "POST",
url: "/auth/check_login.php",
dataType: "json",
success: function(data)
{
// If we are logged in, now can make a call to add the categories
$.ajax({
type: "POST",
url: "/problems/add_categories_ajax.php",
data: dataString ,
success: function(data)
{
$('#categories_success').fadeIn(200).show();
$('#categories_error').fadeOut(200).hide();
$("#log").val("");
},
error: function(data)
{
$("#loading").hide();
$("Something went wrong. Please let us know that this happened so we can fix it right away.
").dialog( );
//alert ("error");
//if ( data.status == 200 )
//{
// $('.add_message_success').fadeIn(200).show();
// $('.add_message_error').fadeOut(200).hide();
// $('#comments').html(data);
// }
//alert ("could not add attempted solution to the database");
}
}); // Closes the innter AJAX
} ,
error: function(data)
{
// Error case for checking if user is not logged in
$("#loginpopup").dialog( {title: 'Please Login'} );
return false;
} // End of error
}); // End of the outer AJAX call.
} // End of else
return false;
}); // End of function
$('#unfollow_problem').click(function()
{
$("#loading").show();
// Now have to get value of the text entry area
var problem_id = $("#problem_id").val();
var member_id = $("#member_id").val();
//alert ("problem_id: " + problem_id + " and member_id: " + member_id);
var dataString = 'problem_id='+ problem_id + '&member_id=' + problem_id;
if( problem_id == '' )
{
//$('.success').fadeOut(200).hide();
//$('.error').fadeOut(200).show();
$("#loading").hide();
$("Something went wrong. Please let us know that this happened so we can fix it right away.
").dialog( );
}
else
{
// First check if user is logged in
$.ajax({
type: "POST",
url: "/auth/check_login.php",
dataType: "json",
success: function(json)
{
// Now do the next AJAX to follow the problem.
$.ajax({
type: "POST",
url: "/problems/unfollow_problem_ajax.php",
data: dataString ,
success: function(data)
{
$("#loading").hide();
// See what is returned
if ( data == 'success')
{
// alert ("success");
// Now change the display on the links to follow/unfollow problem.
$("#unfollow_problem").hide();
$("#follow_problem").show();
//$("#successfull_follow").hide();
//$("#successfull_unfollow").show();
// Print a dialog box with a success message.
$("You successfully un-followed this problem.
").dialog( { title: 'Success'} );
}
else
if ( data == 'db_error')
{
//alert ("db error");
}
else
if ( data == 'not_logged_in')
{
//alert ("not_logged_in");
}
else
if ( data == 'not_problem_id')
{
//alert ("no problem id");
}
return false;
},
error: function(json)
{
// Hide she wait animation
$("#loading").hide();
// Open the login dialog box.
$("#loginpopup").dialog( {title: 'Please Login To Un-follow'} );
return false;
}
});
},
error: function(json)
{
// Hide she wait animation
$("#loading").hide();
// Open the login dialog box.
$("#loginpopup").dialog();
return false;
}
});
}
$("#loading").hide();
return false;
});
$('#follow_problem').click(function()
{
$("#loading").show();
// Now have to get value of the text entry area
var problem_id = $("#problem_id").val();
var member_id = $("#member_id").val();
//alert ("problem_id: " + problem_id + " and member_id: " + member_id);
var dataString = 'problem_id='+ problem_id + '&member_id=' + problem_id;
if( problem_id == '' )
{
//$('.success').fadeOut(200).hide();
//$('.error').fadeOut(200).show();
// Not sure what to do here.
$("#loading").hide();
$("Something went wrong. Please let us know that this happened so we can fix it right away.
").dialog( );
}
else
{
// First check if user is logged in
$.ajax({
type: "POST",
url: "/auth/check_login.php",
dataType: "json",
success: function(json)
{
// Now do the next AJAX to follow the problem.
$.ajax({
type: "POST",
url: "/problems/follow_problem_ajax.php",
data: dataString ,
success: function(data)
{
$("#loading").hide();
if ( data == 'already_follows_problem' )
{
$("You already follow this problem :)
").dialog( { title: 'Success'} );
}
// See what is returned
if ( data == 'success')
{
//alert ("success");
// Now change the display on the links to follow/unfollow problem.
$("#follow_problem").hide();
$("#unfollow_problem").show();
//$("#successfull_follow").show();
//$("#successful_unfollow").hide();
$("You successfully followed this problem.
").dialog( { title: 'Success'} );
}
else
if ( data == 'db_error')
{
//alert ("db error");
}
else
if ( data == 'already_follows_problem')
{
//alert ("already_follows_problem");
}
else
if ( data == 'not_logged_in')
{
//alert ("not_logged_in");
}
else
if ( data == 'not_problem_id')
{
//alert ("no problem id");
}
return false;
},
error: function(json)
{
// Hide she wait animation
$("#loading").hide();
// Open the login dialog box.
//$("#loginpopup").dialog( );
return false;
}
});
},
error: function(json)
{
// Hide she wait animation
$("#loading").hide();
// Open the login dialog box.
$("#loginpopup").dialog( {title: 'Login To Follow Problem'} );
return false;
}
});
}
$("#loading").hide();
return false;
});
//$('#header_search').bind('submit',function()
//{
// var search_string = $("#search_string").val();
// var dataString = 'search='+ search_string;
// Now check if the problem_id or solution are empty
// if( !search_string )
// {
//$('#categories_success').fadeIn(200).hide();
//$('#categories_error').fadeOut(200).show();
// }
// else
// {
// If we are logged in, now can make a call to add the categories
// $.ajax({
// type: "POST",
// url: "/problems/search_ajax.php",
// data: dataString ,
// success: function(data)
// {
//$('#categories_success').fadeIn(200).show();
//$('#categories_error').fadeOut(200).hide();
// $("#log").val("");
// },
// error: function(data)
// {
//if ( data.status == 200 )
//{
// $('.add_message_success').fadeIn(200).show();
// $('.add_message_error').fadeOut(200).hide();
// $('#comments').html(data);
// }
//alert ("could not add attempted solution to the database");
// }
// }); // End of the outer AJAX call.
// } // End of else
// return false;
//}); // End of function
$('#create_account_form').live('submit',function()
{
// Showing the wait image
$("#loading").show();
// First set all the validation error divs to not display
$('#create_profile_error').hide();
$('#create_profile_bottom_error').hide();
$('#first_name_error').hide();
$('#email_error').hide();
$('#email_confirm_error').hide();
$('#password_error').hide();
$('#password_confirm_error').hide();
$('#terms_and_conditions_error').hide();
//$('#zip_error').hide();
//$('#country_error').hide();
var first_name = $("#first_name").val();
//var last_name = $("#last_name").val();
var password = $("#create_pass").val();
var pass_confirm = $("#user_pass_check").val();
var user_email = $("#user_email").val();
var user_email_confirm = $("#user_email_confirm").val();
var terms_and_conditions = $('#terms_and_conditions').attr('checked');
//var zip = $("#zip").val();
//var country = $("#country").val();
var dataString = 'first_name='+ first_name + '&user_pass=' + password + '&user_pass_check=' + pass_confirm + '&user_email=' + user_email + '&user_email_confirm=' + user_email_confirm + '&terms_and_conditions=' + terms_and_conditions;
// '&zip=' + zip + '&country=' + country
var validation_errors = false;
//alert ("Data string " + dataString);
// Validate and check if they are empty
if( !first_name )
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#first_name_error').show();
}
if( !user_email )
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#email_error').show();
}
if( !user_email_confirm )
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#email_confirm_error').show();
}
if( !password )
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#password_error').show();
}
if( !pass_confirm )
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#password_confirm_error').show();
}
if ( password != pass_confirm )
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#password_confirm_error').show();
}
if ( password.length < 5 )
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#password_confirm_error').show();
}
//if( !zip )
//{
// validation_errors = true;
// $('#create_profile_error').show();
// $('#create_profile_bottom_error').show();
// $('#zip_error').show();
//}
//if( !country || country.indexOf("---") != -1 )
//{
// validation_errors = true;
// $('#create_profile_error').show();
// $('#create_profile_bottom_error').show();
// $('#country_error').show();
//}
if ($('#terms_and_conditions').attr('checked'))
{
// alert ("CHECKKEED");
// Not sure what to do here yet :)
}
else
{
validation_errors = true;
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
$('#terms_and_conditions_error').show();
}
if ( validation_errors )
{
// Not sure what to do here yet :)
}
else
{
// Lets call the AJAX to create the account!!
$.ajax({
type: "POST",
url: "/auth/create_profile_ajax.php",
data: dataString ,
success: function(data)
{
// Showing the wait image
$("#loading").hide();
if ( data.indexOf("Error") != -1)
{
//alert(data);
$('#create_profile_error').html(data);
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
}
else
{
$("#createprofilepopup").dialog( 'close' );
$("You have successfully created an account and are now logged in. Just close this window and resume what you were doing.").dialog();
}
},
error: function(data)
{
// Showing the wait image
$("#loading").hide();
$('#create_profile_error').html("Server error while creating your profile. Please let us know about this.
");
$('#create_profile_error').show();
$('#create_profile_bottom_error').show();
}
});
} // End of validation checking else statement.
return false;
});
function showLoading()
{
$("#loading").show();
}
function hideLoading()
{
$("#loading").hide();
}
$('#login').live('click' , function( event )
{
event.preventDefault();
// Make the existing suggestions visible
$("#loginpopup").dialog();
$("#createprofilepopup").dialog( 'close' );
return false;
});
$('#signup').live('click' , function( event )
{
event.preventDefault();
$("#loginpopup").dialog( 'close' );
$("#createprofilepopup").dialog( { title: 'Create Profile' , height: 490, width: 580 } );
return false;
});
// Now create JS for clicking on diff categories to expand the sub-categories.
$("#health_cat").change(function()
{
// Set the health sub-categories to be displayed.
$("#health_sub_cat").show();
});
$("#local_cat").change(function()
{
// Set the health sub-categories to be displayed.
$("#local_sub_cat").show();
});
$("#society_cat").change(function()
{
// Set the health sub-categories to be displayed.
$("#society_sub_cat").show();
});
$("#environment_cat").change(function()
{
// Set the health sub-categories to be displayed.
$("#environment_sub_cat").show();
});
$("#politics_cat").change(function()
{
// Set the health sub-categories to be displayed.
$("#politics_sub_cat").show();
});
$("#economics_cat").change(function()
{
// Set the health sub-categories to be displayed.
$("#economics_sub_cat").show();
});
$("#education_cat").change(function()
{
// Set the health sub-categories to be displayed.
$("#education_sub_cat").show();
});
}); // End of document.ready