Subversion Repositories bacoAlunos

Rev

Rev 223 | Rev 406 | 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
 
92 jmachado 105
function hide(id,showId)
106
{
223 jmachado 107
    hideOne(id);
108
    showOne(showId);
92 jmachado 109
}
110
function show(id,hideId)
111
{
223 jmachado 112
    hideOne(hideId);
113
    showOne(id);
92 jmachado 114
}
223 jmachado 115
 
116
function hideOne(id)
159 jmachado 117
{
223 jmachado 118
//    getObjectById(id).style.visibility='hidden';
119
//    getObjectById(id).style.position='absolute';
120
    getObjectById(id).style.display='none';
159 jmachado 121
}
223 jmachado 122
 
123
function showOne(id)
159 jmachado 124
{
223 jmachado 125
//    getObjectById(id).style.visibility='visible';
126
//    getObjectById(id).style.position='relative';
127
    getObjectById(id).style.display='';
128
}
129
function showOrHideOne(id)
130
{
131
    if(getObjectById(id).style.display == 'none')
132
        showOne(id);
159 jmachado 133
    else
223 jmachado 134
        hideOne(id);
159 jmachado 135
}
163 jmachado 136
 
137
function showOrHide(id,hideValue,showValue)
138
{
223 jmachado 139
    if(getObjectById(id).style.display == 'none')
163 jmachado 140
    {
223 jmachado 141
        showOne(id);
163 jmachado 142
        return showValue;
143
    }
144
    else
145
    {
223 jmachado 146
        hideOne(id);
163 jmachado 147
        return hideValue;
148
    }
149
}
92 jmachado 150
 
1 fvelez 151
/**
53 jmachado 152
 * Creates a new XmlHttpObject
153
 * @author Jorge Machado
154
 * @date April 2008
1 fvelez 155
 *
53 jmachado 156
 * params:
157
 * @handler target xmlHttpObject function
1 fvelez 158
 */
53 jmachado 159
function GetXmlHttpObject(handler)
1 fvelez 160
{
53 jmachado 161
    var objXmlHttp = null;
162
    if (navigator.userAgent.indexOf("Opera")>=0)
163
    {
164
        alert("This example doesn't work in Opera") ;
165
        return objXmlHttp;
166
    }
167
    if (navigator.userAgent.indexOf("MSIE")>=0)
168
    {
169
        var strName="Msxml2.XMLHTTP";
170
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
1 fvelez 171
        {
53 jmachado 172
            strName="Microsoft.XMLHTTP";
1 fvelez 173
        }
53 jmachado 174
        try
175
        {
176
            objXmlHttp=new ActiveXObject(strName);
177
            objXmlHttp.onreadystatechange=handler ;
178
            return objXmlHttp;
179
        }
180
        catch(e)
181
        {
182
            alert("Error. Scripting for ActiveX might be disabled") ;
183
            return objXmlHttp;
184
        }
185
    }
186
    if (navigator.userAgent.indexOf("Mozilla")>=0)
187
    {
188
        objXmlHttp=new XMLHttpRequest();
189
        objXmlHttp.onload=handler;
190
        objXmlHttp.onerror=handler;
191
        return objXmlHttp;
192
    }
1 fvelez 193
}
194
 
53 jmachado 195
/***************************************************
196
 TopFlashNews
163 jmachado 197
 ***************************************************/
53 jmachado 198
var xmlHttpTopFlashNews;
199
var getFlashNewsTimeout;
200
var getFlashNewsJsp;
381 jmachado 201
var semaphoreFlashNews = 0;
1 fvelez 202
 
53 jmachado 203
function getFlashNews(jsp,timeout)
48 fvelez 204
{
53 jmachado 205
    getFlashNewsJsp = jsp;
206
    getFlashNewsTimeout = timeout;
381 jmachado 207
    semaphoreFlashNews = 1;
53 jmachado 208
    getFlashNewsTimeoutCall();
48 fvelez 209
}
53 jmachado 210
function getFlashNewsTimeoutCall()
48 fvelez 211
{
53 jmachado 212
    xmlHttpTopFlashNews=GetXmlHttpObject(stateChangedGetFlashNews);
381 jmachado 213
    semaphoreFlashNews = 1;
53 jmachado 214
    startRequest(xmlHttpTopFlashNews,"","flashTopNews",stateChangedGetFlashNews,"",getFlashNewsJsp)
48 fvelez 215
}
216
function stateChangedGetFlashNews()
217
{
381 jmachado 218
    if (semaphoreFlashNews == 1 && (xmlHttpTopFlashNews.readyState==4 || xmlHttpTopFlashNews.readyState=="complete"))
53 jmachado 219
    {
220
        getObjectById("flashTopNews").innerHTML=xmlHttpTopFlashNews.responseText;
221
        setTimeout(getFlashNewsTimeoutCall,getFlashNewsTimeout);
381 jmachado 222
        semaphoreFlashNews = 0;
53 jmachado 223
    }
48 fvelez 224
}
1 fvelez 225
 
226
 
92 jmachado 227
/***************************************************
228
 Search
163 jmachado 229
 ***************************************************/
92 jmachado 230
var xmlHttpSearch;
231
var searchResultsDiv;
1 fvelez 232
 
92 jmachado 233
function searchCall(div,query,searchType,page,module,action)
234
{
235
    searchResultsDiv = div;
236
    xmlHttpSearch=GetXmlHttpObject(stateChangedSearchCall);
237
    startRequest(xmlHttpSearch,"dispatch=searchModule&query=" + query + "&searchType=" + searchType + "&page=" + page + "&module=" + module,"",stateChangedSearchCall,"",action);
238
}
239
function stateChangedSearchCall()
240
{
241
    if (xmlHttpSearch.readyState==4 || xmlHttpSearch.readyState=="complete")
242
    {
243
        getObjectById(searchResultsDiv).innerHTML=xmlHttpSearch.responseText;
244
    }
245
}
1 fvelez 246
 
247
 
159 jmachado 248
/***************************************************
249
 Pop
163 jmachado 250
 ***************************************************/
159 jmachado 251
var xmlHttpPop;
252
var popDiv;
1 fvelez 253
 
159 jmachado 254
function loadPop(div,action)
255
{
256
    popDiv = div;
257
    xmlHttpPop=GetXmlHttpObject(stateChangedPopCall);
258
    startRequest(xmlHttpPop,"","",stateChangedPopCall,"",action);
259
}
260
function stateChangedPopCall()
261
{
262
    if (xmlHttpPop.readyState==4 || xmlHttpPop.readyState=="complete")
263
    {
264
        getObjectById(popDiv).innerHTML=xmlHttpPop.responseText;
265
    }
266
}
1 fvelez 267
 
163 jmachado 268
/***************************************************
269
 Reminders
270
 ***************************************************/
198 jmachado 271
 
272
var activeRemindersCounter = 0;
273
var expiredRemindersCounter = 0;
381 jmachado 274
var semaphoreReminder = 0;
198 jmachado 275
 
276
function setActiveReminders(active)
277
{
278
    activeRemindersCounter = active;
279
}
280
function setExpiredReminders(expired)
281
{
282
    expiredRemindersCounter = expired;
283
}
284
 
163 jmachado 285
var xmlHttpReminder;
286
var newReminderId;
287
var reminderFormId;
288
var putMsgId;
289
var putOkMsg;
290
var putFailMsg;
1 fvelez 291
 
163 jmachado 292
function putReminder(startDate,expireDate,text,newReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
293
{
381 jmachado 294
    if(semaphoreReminder == 1)
295
    {
296
        getObjectById(putMsgId).innerHTML = "<div class=\"messages\">busy...</div>";
297
    }
298
    else
299
    {
300
        semaphoreReminder = 1;
301
        newReminderId = newReminderDiv;
302
        reminderFormId = reminderFormDiv;
303
        putMsgId = msgDiv;
304
        putOkMsg = okMsg;
305
        putFailMsg = failMsg;
306
        getObjectById(putMsgId).innerHTML = waitMsg;
307
        xmlHttpReminder=GetXmlHttpObject(stateChangedPutReminderCall);
308
        startRequest(xmlHttpReminder,"dispatch=save&startDate="+startDate+"&expireDate="+expireDate+"&reminderView.text="+ text,"",stateChangedPutReminderCall,"",action);
223 jmachado 309
//    hideOne(reminderFormId);
381 jmachado 310
        showOne(putMsgId);
311
    }
163 jmachado 312
}
313
function stateChangedPutReminderCall()
314
{
315
    if (xmlHttpReminder.readyState==4 || xmlHttpReminder.readyState=="complete")
316
    {
317
        if(xmlHttpReminder.responseText.indexOf("<div class=\"messages\">") >= 0)
318
        {
319
            getObjectById(putMsgId).innerHTML = xmlHttpReminder.responseText;
320
        }
381 jmachado 321
        else if(semaphoreReminder == 1)
163 jmachado 322
        {
381 jmachado 323
            semaphoreReminder = 0;
163 jmachado 324
            getObjectById(putMsgId).innerHTML = '<div class="statusOK">' + putOkMsg + '</div>';
223 jmachado 325
            activeRemindersCounter = activeRemindersCounter + 1;
326
            showOne('activeReminders');
163 jmachado 327
            getObjectById(newReminderId).innerHTML = xmlHttpReminder.responseText + getObjectById(newReminderId).innerHTML;
328
        }
329
    }
165 jmachado 330
    setTimeout(cleanReminderStatus,3000);
163 jmachado 331
}
332
function cleanReminderStatus()
333
{
334
   getObjectById(putMsgId).innerHTML = '';
335
}
1 fvelez 336
 
198 jmachado 337
function deleteExpiredReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
165 jmachado 338
{
198 jmachado 339
    deleteReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg,stateChangedDeleteExpiredReminderCall);
340
}
341
function deleteActiveReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
342
{
343
    deleteReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg,stateChangedDeleteActiveReminderCall);
344
}
345
function deleteReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg,callBack)
346
{
165 jmachado 347
    newReminderId = deleteReminderDiv;
348
    reminderFormId = reminderFormDiv;
349
    putMsgId = msgDiv;
350
    putOkMsg = okMsg;
351
    putFailMsg = failMsg;
352
    getObjectById(putMsgId).innerHTML = waitMsg;
198 jmachado 353
    xmlHttpReminder=GetXmlHttpObject(callBack);
354
    startRequest(xmlHttpReminder,"dispatch=delete&id="+id,"",callBack,"",action);
223 jmachado 355
    //hideOne(reminderFormId);
356
    showOne(putMsgId);
165 jmachado 357
}
1 fvelez 358
 
198 jmachado 359
function stateChangedDeleteExpiredReminderCall()
360
{
223 jmachado 361
 
362
    stateChangedDeleteReminderCall("expired");
198 jmachado 363
    if(expiredRemindersCounter <= 0)
364
    {
223 jmachado 365
        hideOne('expiredReminders');
198 jmachado 366
    }
367
    if(expiredRemindersCounter < 0)
368
        expiredRemindersCounter = 0;
369
}
370
function stateChangedDeleteActiveReminderCall()
371
{
223 jmachado 372
    stateChangedDeleteReminderCall("active");
198 jmachado 373
 
374
    if(activeRemindersCounter <= 0)
375
    {
223 jmachado 376
        hideOne('activeReminders');
198 jmachado 377
    }
378
    if(activeRemindersCounter < 0)
379
        activeRemindersCounter = 0;
380
}
223 jmachado 381
function stateChangedDeleteReminderCall(type)
165 jmachado 382
{
383
    if (xmlHttpReminder.readyState==4 || xmlHttpReminder.readyState=="complete")
384
    {
223 jmachado 385
        if(type == "active")
386
            activeRemindersCounter--;
387
        else
388
            expiredRemindersCounter--;
389
 
165 jmachado 390
        if(xmlHttpReminder.responseText.indexOf("<div class=\"messages\">") >= 0)
391
        {
392
            getObjectById(putMsgId).innerHTML = xmlHttpReminder.responseText;
393
        }
394
        else
395
        {
396
            getObjectById(putMsgId).innerHTML = '<div class="statusOK">' + putOkMsg + '</div>';
397
            getObjectById(newReminderId).innerHTML = '';
398
        }
399
    }
400
    setTimeout(cleanReminderStatus,3000);
401
}
1 fvelez 402
 
48 fvelez 403
 
404
 
405
 
406
 
92 jmachado 407
 
408
 
409
 
159 jmachado 410
 
411
 
412
 
163 jmachado 413
 
414
 
415
 
165 jmachado 416
 
417
 
418