Subversion Repositories bacoAlunos

Rev

Rev 163 | Rev 166 | 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
 ***************************************************/
269
var xmlHttpReminder;
270
var newReminderId;
271
var reminderFormId;
272
var putMsgId;
273
var putOkMsg;
274
var putFailMsg;
1 fvelez 275
 
163 jmachado 276
function putReminder(startDate,expireDate,text,newReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
277
{
278
    newReminderId = newReminderDiv;
279
    reminderFormId = reminderFormDiv;
280
    putMsgId = msgDiv;
281
    putOkMsg = okMsg;
282
    putFailMsg = failMsg;
283
    getObjectById(putMsgId).innerHTML = waitMsg;
284
    xmlHttpReminder=GetXmlHttpObject(stateChangedPutReminderCall);
165 jmachado 285
    startRequest(xmlHttpReminder,"dispatch=save&startDate="+startDate+"&expireDate="+expireDate+"&reminderView.text="+ text,"",stateChangedPutReminderCall,"",action);
163 jmachado 286
    hide(reminderFormId);
287
    show(putMsgId);
288
}
289
function stateChangedPutReminderCall()
290
{
291
    if (xmlHttpReminder.readyState==4 || xmlHttpReminder.readyState=="complete")
292
    {
293
        if(xmlHttpReminder.responseText.indexOf("<div class=\"messages\">") >= 0)
294
        {
295
            getObjectById(putMsgId).innerHTML = xmlHttpReminder.responseText;
296
        }
297
        else
298
        {
299
            getObjectById(putMsgId).innerHTML = '<div class="statusOK">' + putOkMsg + '</div>';
165 jmachado 300
            show('activeReminders');
163 jmachado 301
            getObjectById(newReminderId).innerHTML = xmlHttpReminder.responseText + getObjectById(newReminderId).innerHTML;
302
        }
303
    }
165 jmachado 304
    setTimeout(cleanReminderStatus,3000);
163 jmachado 305
}
306
function cleanReminderStatus()
307
{
308
   getObjectById(putMsgId).innerHTML = '';
309
}
1 fvelez 310
 
311
 
165 jmachado 312
function deleteReminder(id,deleteReminderDiv,msgDiv,reminderFormDiv,action,waitMsg,okMsg,failMsg)
313
{
314
    newReminderId = deleteReminderDiv;
315
    reminderFormId = reminderFormDiv;
316
    putMsgId = msgDiv;
317
    putOkMsg = okMsg;
318
    putFailMsg = failMsg;
319
    getObjectById(putMsgId).innerHTML = waitMsg;
320
    xmlHttpReminder=GetXmlHttpObject(stateChangedDeleteReminderCall);
321
    startRequest(xmlHttpReminder,"dispatch=delete&id="+id,"",stateChangedDeleteReminderCall,"",action);
322
    hide(reminderFormId);
323
    show(putMsgId);
324
}
1 fvelez 325
 
165 jmachado 326
function stateChangedDeleteReminderCall()
327
{
328
    if (xmlHttpReminder.readyState==4 || xmlHttpReminder.readyState=="complete")
329
    {
330
        if(xmlHttpReminder.responseText.indexOf("<div class=\"messages\">") >= 0)
331
        {
332
            getObjectById(putMsgId).innerHTML = xmlHttpReminder.responseText;
333
        }
334
        else
335
        {
336
            getObjectById(putMsgId).innerHTML = '<div class="statusOK">' + putOkMsg + '</div>';
337
            getObjectById(newReminderId).innerHTML = '';
338
        }
339
    }
340
    setTimeout(cleanReminderStatus,3000);
341
}
1 fvelez 342
 
48 fvelez 343
 
344
 
345
 
346
 
92 jmachado 347
 
348
 
349
 
159 jmachado 350
 
351
 
352
 
163 jmachado 353
 
354
 
355
 
165 jmachado 356
 
357
 
358