(function(){
var urlPrefix = '/svc/';
$(document).ready(function () {
$.ajax({
'type': 'POST',
'url': urlPrefix + 'list_problems',
'accepts': 'application/json',
'contentType': 'application/json; charset=UTF-8',
'data': JSON.stringify({
'language': 'prolog'
}),
'processData': false,
'dataType': 'json',
'error': function loginErrorHandler(jqXHR, textStatus, errorThrown) {
$('#disabled').css('cursor', '');
alert('Request to obtain list of problems failed: ' + (errorThrown || textStatus));
},
'success': function loginSuccessHandler(data, textStatus, jqXHR) {
var i, groups, group, problems, problem, first_group,
jqGroup = $('#problem_group'),
jqProblems = $('#problems'),
html = [],
mapping = {}
onGroupChange = function () {
var problems = mapping[jqGroup.val()],
html = [],
i, p;
if (problems) {
for (i = 0; i < problems.length; i++) {
p = problems[i];
html.push('\n')
}
}
jqProblems.html(html.join(''));
};
if (data && (data.code === 0)) {
$('#disabled').css('display', 'none');
groups = data.problems;
for (i = 0; i < groups.length; i++) {
group = groups[i];
html.push('\n');
mapping[group.identifier] = group.problems;
}
jqGroup.html(html.join(''));
first_group = html[1];
html = null;
jqGroup.on('click', onGroupChange);
jqGroup.val(first_group);
onGroupChange();
$("#submit").on('click', function () {
var group = $('#problem_group').val(),
problem = $('#problems').val();
if (!group) alert('Choose a problem group');
else if (!problem) alert('Choose a problem');
else {
$.ajax({
'type': 'POST',
'url': urlPrefix + 'login',
'accepts': 'application/json',
'contentType': 'application/json; charset=UTF-8',
'data': JSON.stringify({
'username': $('#username').val(),
'password': $('#password').val()
}),
'processData': false,
'dataType': 'json',
'error': function loginErrorHandler(jqXHR, textStatus, errorThrown) {
alert('Request for login failed: ' + (errorThrown || textStatus));
},
'success': function loginSuccessHandler(data, textStatus, jqXHR) {
if (data && (data.code === 0)) {
window.location = 'prolog.html#sid=' + data.sid + '/grp=' + group + '/prb=' + problem;
}
else {
alert('Login failed: code=' + data.code + ', reason=' + data.message);
}
}
});
}
});
}
else {
$('#disabled').css('cursor', '');
alert('Obtaining list of problems failed: code=' + data.code + ', reason=' + data.message);
}
}
});
});
})();