Subversion Repositories bacoAlunos

Rev

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