Statistics
| Branch: | Revision:

root / evote / listCandidates.js @ f52a1778

History | View | Annotate | Download (11.6 kB)

1
/****************
2
Page JQuery Code
3
Author Konstantinos Trifonopoulos
4
*****************/
5

    
6
function loadEditForm(candidateCode) {
7
    $.ajax({
8
        type: 'POST',
9
        data: {
10
            candidateCode: candidateCode
11
        },
12
        url: 'data/getCandidate.php',         
13
        dataType: "JSON",
14
        success: function(json) { 
15
            if (json.success =="true") {
16
                $.each(json.results, function() {
17
                    $('#editCandidateCode').val(this.candidateCode);
18
                    $('#editCandidateFirstName').val(this.firstName);
19
                    $('#editCandidateLastName').val(this.lastName);
20
                    $('#editCandidateFatherName').val(this.fatherName);
21
                    $('#editSchoolCode').val(this.schoolCode);
22
                });
23
                                                  
24
                $('#dvEditCandidate').dialog("open");
25
            } else {
26
                msgBox("error","Σφάλμα", json.message);
27
            }
28
        }
29
    })
30
         
31
}
32
 
33
function loadCandidates() { 
34
    $('.listRow').remove();
35
    $.getJSON("data/selCandidates.php", 
36
        function (data) {
37
                                                 
38
            $.each(data, function() {
39
                $('#listOfCandidates').append("<tr class='listRow' id='id"+  this.CAN_Code +"'><td><b>" + this.CAN_LastName + "</b></td><td><b>" + this.CAN_FirstName + "</b></td><td><b>" + this.CAN_FatherName + "</b></td><td><b>" + this.SCH_Name + "</b></td><td><img class='editIcon pointme'  editId="+  this.CAN_Code +" src='images/edit-icon.png' height=16 width=16 original-title='επεξεργασία'><img class='deleteIcon pointme'  delId="+  this.CAN_Code +"  src='images/delete-icon.png' height=16 width=16 original-title='διαγραφή'></td></tr>");
40
            });
41
            
42
            if (finished !=1) {
43
                $('.deleteIcon').tipsy({
44
                    gravity:'w'
45
                });
46
                $('.editIcon').tipsy({
47
                    gravity:'e'
48
                });                                                          
49
                $('.editIcon').click(function() { 
50
                    loadEditForm($(this).attr('editId'));
51
                });
52
                                                  
53
                                                  
54
                $('.deleteIcon').click(function() {
55
                    myDelId = $(this).attr('delId');
56
                    msgBox("question","Προσοχή!", "Θέλετε να διαγράψετε τον συγκεκριμένο υποψήφιο;",  function() {
57
                        deleteCandidate(myDelId);
58
                    }
59
                    );
60

    
61
                });
62
            } else {
63
               $('.editIcon').hide();
64
               $('.deleteIcon').hide(); 
65
               
66
            }
67
        }); // getJSON
68
} // loadCandidates
69

    
70
function loadSchools() {
71
    // Γεμίζει το dropdown με τις σχολές
72
        
73
    $('.opAthlima').remove();
74
    $.getJSON("data/selSchools.php", 
75
        function (json) {
76
                                                 
77
            $.each(json, function() {
78
                $('#schoolCode').append("<option class='opSchool' value="+ this.SCH_Code +">" + this.SCH_Name + "</option>"); 
79
                $('#editSchoolCode').append("<option class='opSchool' value="+ this.SCH_Code +">" + this.SCH_Name + "</option>"); 
80
            });
81
        });
82
}; // getJSON
83

    
84
function checkFields() {
85
        
86
    var result = true;
87
    $('#errorMessage ol  li').remove();
88
    //trim values
89
    $('#newCandidateFirstName').val($.trim($('#newCandidateFirstName').val()));
90

    
91
    if ($('#newCandidateFirstName').val().length == 0) {
92
        $('#errorMessage ol').append('<li>Δεν έχετε δώσει όνομα υποψηφίου</li>');
93
        result = false;
94
    } 
95
    if ($('#newCandidateFirstName').val().length < 2) {
96
        $('#errorMessage ol').append('<li>Το περιεχόμενο πρέπει να έχει μήκος τουλάχιστον 2 χαρακτήρων</li>');
97
        result = false;
98
    }         
99
    if ($('#newCandidateFirstName').val().length > 50) {
100
        $('#errorMessage ol').append('<li>Το περιεχόμενο πρέπει να έχει μήκος το πολύ 50 χαρακτήρες</li>');
101
        result = false;
102
    } 
103

    
104

    
105
        $('#newCandidateLastName').val($.trim($('#newCandidateLastName').val()));
106
    if ($('#newCandidateLastName').val().length == 0) {
107
        $('#errorMessage ol').append('<li>Δεν έχετε δώσει επώνυμο υποψηφίου</li>');
108
        result = false;
109
    }
110
    if ($('#newCandidateFirstName').val().length < 2) {
111
        $('#errorMessage ol').append('<li>Το περιεχόμενο πρέπει να έχει μήκος τουλάχιστον 2 χαρακτήρων</li>');
112
        result = false;
113
    }                 
114
    if ($('#newCandidateFirstName').val().length > 50) {
115
        $('#errorMessage ol').append('<li>Το περιεχόμενο πρέπει να έχει μήκος το πολύ 50 χαρακτήρες</li>');
116
        result = false;
117
    }         
118

    
119
        $('#newCandidateFatherName').val($.trim($('#newCandidateFatherName').val()));
120
    if ($('#newCandidateFatherName').val().length == 0) {
121
        $('#errorMessage ol').append('<li>Δεν έχετε δώσει όνομα πατρός του υποψηφίου</li>');
122
        result = false;
123
    }
124
    if ($('#newCandidateFatherName').val().length < 2) {
125
        $('#errorMessage ol').append('<li>Το περιεχόμενο πρέπει να έχει μήκος τουλάχιστον 2 χαρακτήρων</li>');
126
        result = false;
127
    }                 
128
    if ($('#newCandidateFatherName').val().length > 50) {
129
        $('#errorMessage ol').append('<li>Το περιεχόμενο πρέπει να έχει μήκος το πολύ 50 χαρακτήρες</li>');
130
        result = false;
131
    }         
132
        
133
    if (jQuery.trim($('#schoolCode').val()).length == 0) {
134
        $('#errorMessage ol').append('<li>Δεν έχετε δώσει τη σχολή στην οποία ανήκει ο υποψήφιος</li>');
135
        result = false;
136
    } 
137

    
138
    return result;
139
}
140

    
141

    
142
function clearAndCloseNew() {
143
    //clear error messages
144
    $('#errorMessage ol  li').remove();
145
    //Clear everything in the form and hide
146
    $('#newCandidateFirstName').val("");
147
    $('#newCandidateLastName').val("");
148
    $('#newCandidateFatherName').val("");
149

    
150
    $('#schoolCode').val("");
151
    $('#dvNewCandidate').hide();
152
    $('#dvMessageNewCandidate').show();
153
        
154
}
155

    
156
function insertCandidate() {
157
    $.ajax({
158
        type: 'POST',
159
        url: 'data/insCandidate.php',
160
        data:  $('#frmNewCandidate').serialize() ,
161
        dataType: "JSON",
162
        success: function(json) {   
163
            if (json.success == "false") { 
164
                msgBox("error","Σφάλμα", json.message); 
165
            } else {
166
                loadCandidates();
167
                clearAndCloseNew();                
168
            }
169
        },
170
        error: function(data) {
171
            msgBox("error","Σφάλμα", data);
172
        }
173
    });
174
        
175
}
176

    
177
function updateCandidate() {
178
        
179
    $.ajax({
180
        type: 'POST',
181
        url: 'data/updCandidate.php',
182
        data:  $('#frmEditCandidate').serialize() ,
183
        dataType: "JSON",
184
        success: function(json) {   
185
            if (json.success == "false") { 
186
                msgBox("error","Σφάλμα", json.message);
187
            } else {
188
                loadCandidates();
189
                clearAndCloseNew();
190
                $('#dvEditCandidate').dialog("close");                
191
            }; 
192
        },
193
        error: function(data) {
194
            msgBox("error","Σφάλμα", data);
195
        }
196
    });
197

    
198
}
199

    
200
function deleteCandidate(candidateCode) {
201
    
202
    myBallotCode = $('#ballotCode').val();
203
        
204
    $.ajax({
205
        type: 'POST',
206
        data: {
207
            candidateCode:candidateCode
208
        },
209
        url: 'data/delCandidate.php',         
210
        dataType: "JSON",
211
        success: function(json) { 
212
            if (json.success =="true") {
213
                msgBox("info", "Μήνυμα", "Επιτυχής Διαγραφή");
214
                loadCandidates();
215
            } else {
216
                msgBox("error","Σφάλμα", json.message);
217
            }
218
        }
219
    });        //Ajax
220
}
221
function updateTips( t ) {
222
    $('.validateTips')
223
    .text( t )
224
    .addClass( "ui-state-highlight" );
225
    setTimeout(function() {
226
        $('.validateTips').removeClass( "ui-state-highlight", 1500 );
227
    }, 500 );
228
}
229
        
230
function checkRegexp( o, regexp, n ) {
231
    if ( !( regexp.test( o.val() ) ) ) {
232
        o.addClass( "ui-state-error" );
233
        updateTips( n );
234
        return false;
235
    } else {
236
        return true;
237
    }
238
}
239

    
240
function checkLength( o, n, min, max ) {
241
                
242
    if ( o.val().length > max || o.val().length < min ) {
243
        o.addClass( "ui-state-error" );
244
        updateTips( "Το μήκος του πεδίου '" + n + "' πρέπει να είναι μεταξύ " +
245
            min + " και " + max + "." );
246
        return false;
247
    } else {
248
        return true;
249
    }
250
}
251

    
252
function removeAllFormErrors() {
253
        $('span.vanadium-advice').remove();
254
        $('.vanadium-invalid').removeClass('vanadium-invalid');
255
        $('.vanadium-valid').removeClass('vanadium-valid');  
256
         
257
}
258

    
259
$(document).ready(function() { 
260

    
261
    $( "#dvEditCandidate" ).dialog({
262
        autoOpen: false,
263
        height: 250,
264
        width: 450,
265
        modal: true,
266
        buttons: { 
267
            "Ενημέρωση" : function() {
268
                /*var bValid=true;
269
                $('#editCandidateFirstName').removeClass("ui-state-error");
270
                $('#editCandidateFirstName').val($.trim($('#editCandidateFirstName').val()));
271
                bValid = bValid && checkLength($('#editCandidateFirstName'), "όνομα", 4, 50);
272
                                                        
273
                $('#editCandidateLastName').val($.trim($('#editCandidateLastName').val()));
274
                $('#editCandidateLastName').removeClass("ui-state-error");
275
                bValid = bValid && checkLength($('#editCandidateLastName'), "επώνυμο", 4, 50);
276
                                                        
277
                $('#editCandidateFatherName').val($.trim($('#editCandidateFatherName').val()));
278
                $('#editCandidateFatherName').removeClass("ui-state-error");
279
                bValid = bValid && checkLength($('#editCandidateFatherName'), "όν. πατρός", 4, 50);
280
                                                        
281
                $('#editSchoolCode').removeClass("ui-state-error");
282
                bValid = bValid && checkLength($('#editSchoolCode'), "Σχολή/Τμήμα", 1, 50);
283
                */
284
                                Vanadium.validateAndDecorate();
285

    
286
                if($('#frmEditCandidate .vanadium-invalid').length==0) {
287
                                        updateCandidate();
288
                                        //$(this).dialog('close');
289
        }
290
                if (bValid) {
291
                    // Στείλε update        
292
                    //updateCandidate();
293
                }
294
            },
295
            "Ακύρωση" : function() {
296
                $(this).dialog('close');
297
            } 
298
        },
299
        open: function() { 
300
                        removeAllFormErrors();
301
                 },
302
        close: function() {
303
            $('#editCandidateFirstName').removeClass("ui-state-error");
304
            $('#editCandidateLastName').removeClass("ui-state-error");
305
            $('#editCandidateFatherName').removeClass("ui-state-error");
306
            $('#editSchoolCode').removeClass("ui-state-error");
307
            $('.validateTips').removeClass( "ui-state-highlight");
308
        }
309
    });
310

    
311

    
312
    $('#dvNewCandidate').hide();
313
    $('#imgNewEllection').click(function () {
314
        $('#dvNewCandidate').show();
315
        $('#dvMessageNewCandidate').hide();
316
        $('#newCandidateFirstName').focus();
317
    });
318
    $('#btnCancelInsertVoting').click(function () {
319
        clearAndCloseNew();
320
    });
321

    
322
    $('#btnInsertCandidate').click(function () {
323
        if (checkFields()) {
324
            insertCandidate();
325
        }
326
    });
327

    
328
    if (currentElectionCode ==0) {
329
        $('#dvMessageNewCandidate').hide();        
330
        $('#dvNewCandidate').hide();        
331
        $('#dvCandidates').hide();
332
    } else {
333
        if (finished==1) {
334
            $('#dvMessageNewCandidate').hide();        
335
        }
336
        loadCandidates();
337
        loadSchools();
338
    };
339

    
340

    
341

    
342
}); //ready