Subversion Repositories bacoAlunos

Rev

Rev 381 | Rev 430 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
53 jmachado 1
/**
163 jmachado 2
 *
3
 * AJAX Functions
4
 *
5
 * This code is protected under GNU LGPL License
6
 *
7
 * @author Jorge Machado
8
 * @date April 2008
9
 **/
1 fvelez 10
 
53 jmachado 11
/**
12
 * Starts XMLHTTP Ajax request
13
 *
14
 * @author Jorge Machado
15
 * @date April 2008
16
 *
17
 * params:
18
 * @params HTTP GET Parameters for query string
19
 * @xmlHttpRequest request
20
 * @id target element
21
 * @stateChanged target function
22
 * @innerPhrase to put while waiting for response
23
 * @navjsp requested jsp
24
 */
25
function startRequest(xmlHttpRequest,params,id,stateChanged,innerPhrase,navjsp)
26
{
165 jmachado 27
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
53 jmachado 28
    if(innerPhrase != '')
29
        getObjectById(id).innerHTML = innerPhrase;
30
    if (xmlHttpRequest==null)
31
    {
32
        alert ("Browser does not support HTTP Request");
33
        return;
34
    }
165 jmachado 35
    var finalParams = "";
53 jmachado 36
    var url= navjsp;
37
    if(params != null && params.length > 0)
38
    {
165 jmachado 39
        //url=url+"?"+ params;
40
        //url=url+"&sid="+Math.random();
41
        url=url+"?sid="+Math.random();
42
        var paramsArray = params.split("&");
43
        var i;
44
        var union = '';
45
        for(i = 0; i < paramsArray.length; i++){
46
            var parameter = paramsArray[i].split("=");
47
            if(parameter.length == 2)
48
            {
49
                finalParams += union + parameter[0] + '=' + encodeURIComponent(parameter[1]);
50
                union = '&';
51
            }
52
        }
53 jmachado 53
    }
54
    else
55
    {
56
        url=url+"?sid="+Math.random();
57
    }
165 jmachado 58
 
53 jmachado 59
    xmlHttpRequest.onreadystatechange=stateChanged;
165 jmachado 60
    xmlHttpRequest.open("POST",url,true);
61
    xmlHttpRequest.setRequestHeader("Content-Type", contentType);
62
    xmlHttpRequest.send(finalParams);
53 jmachado 63
}
1 fvelez 64
 
53 jmachado 65
/**
66
 * Return an HTML element given ID
67
 *
68
 * @author Jorge Machado
69
 * @date April 2008
70
 *
71
 * params:
72
 * @objectId required object
73
 */
74
function getObjectById(objectId)
75
{
76
    // cross-browser function to get an object's style object given its id
77
    try
78
    {
79
        if(document.getElementById && document.getElementById(objectId))
1 fvelez 80
        {
81
            // W3C DOM
53 jmachado 82
            return document.getElementById(objectId);
83
        }
84
        else if (document.all(objectId))
85
        {
1 fvelez 86
            // MSIE 4 DOM
53 jmachado 87
            return document.all(objectId);
88
        }
89
        else if (document.layers && document.layers[objectId])
90
        {
1 fvelez 91
            // NN 4 DOM.. note: this won't find nested layers
53 jmachado 92
            return document.layers[objectId];
1 fvelez 93
        }
53 jmachado 94
        else
1 fvelez 95
        {
96
            return false;
97
        }
53 jmachado 98
    }
99
    catch(e)
100
    {
101
        return false;
102
    }
1 fvelez 103
}
104
 
406 jmachado 105
 
106
function getObjectByStartId(objectId)
107
{
108
    // cross-browser function to get an object's style object given its id
109
    try
110
    {
111
        for(var element in document.elements)
112
        {
113
            alert(element.value);
114
        }
115
//        if(document.getElementById && document.getElementById(objectId))
116
//        {
117
//            // W3C DOM
118
//            return document.getElementById(objectId);
119
//        }
120
//        else if (document.all(objectId))
121
//        {
122
//            // MSIE 4 DOM
123
//            return document.all(objectId);
124
//        }
125
//        else if (document.layers && document.layers[objectId])
126
//        {
127
//            // NN 4 DOM.. note: this won't find nested layers
128
//            return document.layers[objectId];
129
//        }
130
//        else
131
//        {
132
//            return false;
133
//        }
134
    }
135
    catch(e)
136
    {
137
        return false;
138
    }
139
}
140
 
92 jmachado 141
function hide(id,showId)
142
{
223 jmachado 143
    hideOne(id);
144
    showOne(showId);
92 jmachado 145
}
146
function show(id,hideId)
147
{
223 jmachado 148
    hideOne(hideId);
149
    showOne(id);
92 jmachado 150
}
223 jmachado 151
 
152
function hideOne(id)
159 jmachado 153
{
223 jmachado 154
//    getObjectById(id).style.visibility='hidden';
155
//    getObjectById(id).style.position='absolute';
156
    getObjectById(id).style.display='none';
159 jmachado 157
}
223 jmachado 158
 
159
function showOne(id)
159 jmachado 160
{
223 jmachado 161
//    getObjectById(id).style.visibility='visible';
162
//    getObjectById(id).style.position='relative';
163
    getObjectById(id).style.display='';
164
}
165
function showOrHideOne(id)
166
{
167
    if(getObjectById(id).style.display == 'none')
168
        showOne(id);
159 jmachado 169
    else
223 jmachado 170
        hideOne(id);
159 jmachado 171
}
163 jmachado 172
 
406 jmachado 173
function showOrHideOneWithStartId(id)
174
{
175
    for(var i = 1; getObjectById(id + i) != '' && getObjectById(id + i) != null && getObjectById(id + i) != 'undefined'; i++)
176
    {
177
        showOrHideOne(id + i);
178
    }
179
}
180
 
163 jmachado 181
function showOrHide(id,hideValue,showValue)
182
{
223 jmachado 183
    if(getObjectById(id).style.display == 'none')
163 jmachado 184
    {
223 jmachado 185
        showOne(id);
163 jmachado 186
        return showValue;
187
    }
188
    else
189
    {
223 jmachado 190
        hideOne(id);
163 jmachado 191
        return hideValue;
192
    }
193
}
92 jmachado 194
 
1 fvelez 195
/**
53 jmachado 196
 * Creates a new XmlHttpObject
197
 * @author Jorge Machado
198
 * @date April 2008
1 fvelez 199
 *
53 jmachado 200
 * params:
201
 * @handler target xmlHttpObject function
1 fvelez 202
 */
53 jmachado 203
function GetXmlHttpObject(handler)
1 fvelez 204
{
53 jmachado 205
    var objXmlHttp = null;
206
    if (navigator.userAgent.indexOf("Opera")>=0)
207
    {
208
        alert("This example doesn't work in Opera") ;
209
        return objXmlHttp;
210
    }
211
    if (navigator.userAgent.indexOf("MSIE")>=0)
212
    {
213
        var strName="Msxml2.XMLHTTP";
214
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
1 fvelez 215
        {
53 jmachado 216
            strName="Microsoft.XMLHTTP";
1 fvelez 217
        }
53 jmachado 218
        try
219
        {
220
            objXmlHttp=new ActiveXObject(strName);
221
            objXmlHttp.onreadystatechange=handler ;
222
            return objXmlHttp;
223
        }
224
        catch(e)
225
        {
226
            alert("Error. Scripting for ActiveX might be disabled") ;
227
            return objXmlHttp;
228
        }
229
    }
230
    if (navigator.userAgent.indexOf("Mozilla")>=0)
231
    {
232
        objXmlHttp=new XMLHttpRequest();
233
        objXmlHttp.onload=handler;
234
        objXmlHttp.onerror=handler;
235
        return objXmlHttp;
236
    }
1 fvelez 237
}
238
 
53 jmachado 239
/***************************************************
240
 TopFlashNews
163 jmachado 241
 ***************************************************/
53 jmachado 242
var xmlHttpTopFlashNews;
243
var getFlashNewsTimeout;
244
var getFlashNewsJsp;
381 jmachado 245
var semaphoreFlashNews = 0;
1 fvelez 246
 
53 jmachado 247
function getFlashNews(jsp,timeout)
48 fvelez 248
{
53 jmachado 249
    getFlashNewsJsp = jsp;
250
    getFlashNewsTimeout = timeout;
381 jmachado 251
    semaphoreFlashNews = 1;
53 jmachado 252
    getFlashNewsTimeoutCall();
48 fvelez 253
}
53 jmachado 254
function getFlashNewsTimeoutCall()
48 fvelez 255
{
53 jmachado 256
    xmlHttpTopFlashNews=GetXmlHttpObject(stateChangedGetFlashNews);
381 jmachado 257
    semaphoreFlashNews = 1;
53 jmachado 258
    startRequest(xmlHttpTopFlashNews,"","flashTopNews",stateChangedGetFlashNews,"",getFlashNewsJsp)
48 fvelez 259
}
260
function stateChangedGetFlashNews()
261
{
381 jmachado 262
    if (semaphoreFlashNews == 1 && (xmlHttpTopFlashNews.readyState==4 || xmlHttpTopFlashNews.readyState=="complete"))
53 jmachado 263
    {
264
        getObjectById("flashTopNews").innerHTML=xmlHttpTopFlashNews.responseText;
265
        setTimeout(getFlashNewsTimeoutCall,getFlashNewsTimeout);
381 jmachado 266
        semaphoreFlashNews = 0;
53 jmachado 267
    }
48 fvelez 268
}
1 fvelez 269
 
270
 
92 jmachado 271
/***************************************************
272
 Search
163 jmachado 273
 ***************************************************/
92 jmachado 274
var xmlHttpSearch;
275
var searchResultsDiv;
1 fvelez 276
 
92 jmachado 277
function searchCall(div,query,searchType,page,module,action)
278
{
279
    searchResultsDiv = div;
280
    xmlHttpSearch=GetXmlHttpObject(stateChangedSearchCall);
281
    startRequest(xmlHttpSearch,"dispatch=searchModule&query=" + query + "&searchType=" + searchType + "&page=" + page + "&module=" + module,"",stateChangedSearchCall,"",action);
282
}
283
function stateChangedSearchCall()
284
{
285
    if (xmlHttpSearch.readyState==4 || xmlHttpSearch.readyState=="complete")
286
    {
287
        getObjectById(searchResultsDiv).innerHTML=xmlHttpSearch.responseText;
288
    }
289
}
1 fvelez 290
 
291
 
159 jmachado 292
/***************************************************
293
 Pop
163 jmachado 294
 ***************************************************/
159 jmachado 295
var xmlHttpPop;
296
var popDiv;
1 fvelez 297
 
159 jmachado 298
function loadPop(div,action)
299
{
300
    popDiv = div;
301
    xmlHttpPop=GetXmlHttpObject(stateChangedPopCall);
302
    startRequest(xmlHttpPop,"","",stateChangedPopCall,"",action);
303
}
304
function stateChangedPopCall()
305
{
306
    if (xmlHttpPop.readyState==4 || xmlHttpPop.readyState=="complete")
307
    {
308
        getObjectById(popDiv).innerHTML=xmlHttpPop.responseText;
309
    }
310
}
1 fvelez 311
 
163 jmachado 312
/***************************************************
313
 Reminders
314
 ***************************************************/
198 jmachado 315
 
316
var activeRemindersCounter = 0;
317
var expiredRemindersCounter = 0;
381 jmachado 318
var semaphoreReminder = 0;
198 jmachado 319
 
320
function setActiveReminders(active)
321
{
322
    activeRemindersCounter = active;
323
}
324
function setExpiredReminders(expired)
325
{
326
    expiredRemindersCounter = expired;
327
}
328
 
163 jmachado 329
var xmlHttpReminder;
330
var newReminderId;
331
var reminderFormId;
332
var putMsgId;
333
var putOkMsg;
334
var putFailMsg;
1 fvelez 335
 
163 jmachado 336
function putReminder(startDate,expireDate,text,newReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
337
{
381 jmachado 338
    if(semaphoreReminder == 1)
339
    {
340
        getObjectById(putMsgId).innerHTML = "<div class=\"messages\">busy...</div>";
341
    }
342
    else
343
    {
344
        semaphoreReminder = 1;
345
        newReminderId = newReminderDiv;
346
        reminderFormId = reminderFormDiv;
347
        putMsgId = msgDiv;
348
        putOkMsg = okMsg;
349
        putFailMsg = failMsg;
350
        getObjectById(putMsgId).innerHTML = waitMsg;
351
        xmlHttpReminder=GetXmlHttpObject(stateChangedPutReminderCall);
352
        startRequest(xmlHttpReminder,"dispatch=save&startDate="+startDate+"&expireDate="+expireDate+"&reminderView.text="+ text,"",stateChangedPutReminderCall,"",action);
223 jmachado 353
//    hideOne(reminderFormId);
381 jmachado 354
        showOne(putMsgId);
355
    }
163 jmachado 356
}
357
function stateChangedPutReminderCall()
358
{
359
    if (xmlHttpReminder.readyState==4 || xmlHttpReminder.readyState=="complete")
360
    {
361
        if(xmlHttpReminder.responseText.indexOf("<div class=\"messages\">") >= 0)
362
        {
363
            getObjectById(putMsgId).innerHTML = xmlHttpReminder.responseText;
364
        }
381 jmachado 365
        else if(semaphoreReminder == 1)
163 jmachado 366
        {
381 jmachado 367
            semaphoreReminder = 0;
163 jmachado 368
            getObjectById(putMsgId).innerHTML = '<div class="statusOK">' + putOkMsg + '</div>';
223 jmachado 369
            activeRemindersCounter = activeRemindersCounter + 1;
370
            showOne('activeReminders');
163 jmachado 371
            getObjectById(newReminderId).innerHTML = xmlHttpReminder.responseText + getObjectById(newReminderId).innerHTML;
372
        }
373
    }
165 jmachado 374
    setTimeout(cleanReminderStatus,3000);
163 jmachado 375
}
376
function cleanReminderStatus()
377
{
378
   getObjectById(putMsgId).innerHTML = '';
379
}
1 fvelez 380
 
198 jmachado 381
function deleteExpiredReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
165 jmachado 382
{
198 jmachado 383
    deleteReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg,stateChangedDeleteExpiredReminderCall);
384
}
385
function deleteActiveReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
386
{
387
    deleteReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg,stateChangedDeleteActiveReminderCall);
388
}
389
function deleteReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg,callBack)
390
{
165 jmachado 391
    newReminderId = deleteReminderDiv;
392
    reminderFormId = reminderFormDiv;
393
    putMsgId = msgDiv;
394
    putOkMsg = okMsg;
395
    putFailMsg = failMsg;
396
    getObjectById(putMsgId).innerHTML = waitMsg;
198 jmachado 397
    xmlHttpReminder=GetXmlHttpObject(callBack);
398
    startRequest(xmlHttpReminder,"dispatch=delete&id="+id,"",callBack,"",action);
223 jmachado 399
    //hideOne(reminderFormId);
400
    showOne(putMsgId);
165 jmachado 401
}
1 fvelez 402
 
198 jmachado 403
function stateChangedDeleteExpiredReminderCall()
404
{
223 jmachado 405
 
406
    stateChangedDeleteReminderCall("expired");
198 jmachado 407
    if(expiredRemindersCounter <= 0)
408
    {
223 jmachado 409
        hideOne('expiredReminders');
198 jmachado 410
    }
411
    if(expiredRemindersCounter < 0)
412
        expiredRemindersCounter = 0;
413
}
414
function stateChangedDeleteActiveReminderCall()
415
{
223 jmachado 416
    stateChangedDeleteReminderCall("active");
198 jmachado 417
 
418
    if(activeRemindersCounter <= 0)
419
    {
223 jmachado 420
        hideOne('activeReminders');
198 jmachado 421
    }
422
    if(activeRemindersCounter < 0)
423
        activeRemindersCounter = 0;
424
}
223 jmachado 425
function stateChangedDeleteReminderCall(type)
165 jmachado 426
{
427
    if (xmlHttpReminder.readyState==4 || xmlHttpReminder.readyState=="complete")
428
    {
223 jmachado 429
        if(type == "active")
430
            activeRemindersCounter--;
431
        else
432
            expiredRemindersCounter--;
433
 
165 jmachado 434
        if(xmlHttpReminder.responseText.indexOf("<div class=\"messages\">") >= 0)
435
        {
436
            getObjectById(putMsgId).innerHTML = xmlHttpReminder.responseText;
437
        }
438
        else
439
        {
440
            getObjectById(putMsgId).innerHTML = '<div class="statusOK">' + putOkMsg + '</div>';
441
            getObjectById(newReminderId).innerHTML = '';
442
        }
443
    }
444
    setTimeout(cleanReminderStatus,3000);
445
}
1 fvelez 446
 
48 fvelez 447
 
448
 
449
 
450
 
92 jmachado 451
 
452
 
453
 
159 jmachado 454
 
455
 
456
 
163 jmachado 457
 
458
 
459
 
165 jmachado 460
 
461
 
462