1
|
|
|
var store=''; |
2
|
|
|
var recursive; |
3
|
|
|
var last_time='' ; |
4
|
|
|
var flag=0; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
// For updating sidebar and load conversation for first time |
10
|
|
|
|
11
|
|
|
function init(index) |
12
|
|
|
{ |
13
|
|
|
var q="q=total_messages"; |
14
|
|
|
var ele=document.getElementById("message-mob"); // Getting Div |
15
|
|
|
var xmlhttp = new XMLHttpRequest(); |
|
|
|
|
16
|
|
|
xmlhttp.onreadystatechange = function() // Ajax Call |
17
|
|
|
{ |
18
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) |
19
|
|
|
{ |
20
|
|
|
var arr=JSON.parse(xmlhttp.responseText); // Response From change.php |
21
|
|
|
// console.log(arr); |
22
|
|
|
if(arr!=null) |
23
|
|
|
{ |
24
|
|
|
var a=arr[arr.length-1].time; |
25
|
|
|
var b=arr[arr.length-1].username; |
26
|
|
|
var c=a+b; |
27
|
|
|
if (last_time!=c) |
28
|
|
|
{ |
29
|
|
|
last_time=c; |
30
|
|
|
$("#message-mob a").remove(); |
31
|
|
|
|
32
|
|
|
for (var i = arr.length - 1; i >= 0; i--) // organising content according to time |
33
|
|
|
{ |
34
|
|
|
// var sp=$("<span></span>").text(arr[i]['time']); // creating element span |
35
|
|
|
// sp.addClass('message-mob_time'); |
36
|
|
|
// var para=$("<a></a>").text(arr[i]['name']); //creating element a |
37
|
|
|
// para.append(sp); |
38
|
|
|
// $("#message-mob").append(para); |
39
|
|
|
// para.attr({'id':arr[i]['username'],'href':'message-mob.php#'+arr[i]['username'],'class':'message-mob','onclick':'chat(this,10)'}); |
40
|
|
|
|
41
|
|
|
var para=document.createElement("a"); //creating element a |
42
|
|
|
var node=document.createTextNode(arr[i]['name']); |
43
|
|
|
para.appendChild(node); |
44
|
|
|
para.setAttribute('id',arr[i]['username']); |
45
|
|
|
para.setAttribute('href','message.php#'+arr[i]['username']); |
46
|
|
|
para.setAttribute('class','message'); |
47
|
|
|
para.setAttribute('onclick','chat(this,10)'); |
48
|
|
|
ele.appendChild(para); |
49
|
|
|
|
50
|
|
|
var bre=document.createElement("span"); // creating element span for showing time |
51
|
|
|
var inp=document.createTextNode(arr[i]['time']); |
52
|
|
|
bre.appendChild(inp); |
53
|
|
|
bre.setAttribute('class','message_time'); |
54
|
|
|
para.appendChild(bre); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if(index==0) |
58
|
|
|
chat(document.getElementById(arr[arr.length-1].username),10); // Load messgage for the first conversation |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
}; |
63
|
|
|
xmlhttp.open("POST", "ajax/change.php", true); // ajax request post |
64
|
|
|
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
65
|
|
|
xmlhttp.send(q); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
// For loading conversation between two persons |
72
|
|
|
|
73
|
|
|
function chat(element,num) |
74
|
|
|
{ |
75
|
|
|
// console.log(num); |
76
|
|
|
$("#compose_selection").css("visibility","hidden"); |
77
|
|
|
last_time=''; |
78
|
|
|
flag=0; |
79
|
|
|
$("#compose_name").val(''); |
80
|
|
|
$("#search_item").val(''); |
81
|
|
|
$('#compose_text').hide(); |
82
|
|
|
|
83
|
|
|
stop(); // stopping previous setinterval call |
84
|
|
|
|
85
|
|
|
recursive =setInterval(repeat,1500); // refresh conversation |
86
|
|
|
function repeat() |
87
|
|
|
{ |
88
|
|
|
// console.log(element); |
89
|
|
|
init(1); |
90
|
|
|
|
91
|
|
|
var q={"username":element.id,"load":num}; |
92
|
|
|
q="q="+JSON.stringify(q); |
93
|
|
|
|
94
|
|
|
// console.log(q); |
95
|
|
|
|
96
|
|
|
var xmlhttp = new XMLHttpRequest(); |
|
|
|
|
97
|
|
|
var ele=document.getElementById("conversation"); // ajax call |
98
|
|
|
xmlhttp.onreadystatechange = function() |
99
|
|
|
{ |
100
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) |
101
|
|
|
{ |
102
|
|
|
var arr=xmlhttp.responseText; |
103
|
|
|
arr=JSON.parse(arr); |
104
|
|
|
// console.log(arr); |
105
|
|
|
|
106
|
|
|
if(arr!=null && flag==0) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
// console.log(1); |
109
|
|
|
if (arr[arr.length-1]==1) // Old User |
110
|
|
|
{ |
111
|
|
|
var a=arr[0].id; |
112
|
|
|
if(store!=a) |
113
|
|
|
{ |
114
|
|
|
store=a; |
115
|
|
|
// console.log(1); |
116
|
|
|
ele.innerHTML=""; |
117
|
|
|
|
118
|
|
|
if(arr[arr.length-2].load>10) // For showing previous message |
119
|
|
|
{ |
120
|
|
|
var txt=$("<a></a>").text("Show Previous Message!"); |
121
|
|
|
var te=$("<div></div>").append(txt); |
122
|
|
|
$("#conversation").append(te); |
123
|
|
|
$("#conversation div").addClass("previous"); |
124
|
|
|
$("#conversation div a").attr({"onclick":"previous(this)","id":arr[0].username,"name":arr[arr.length-2].load}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
for (var i = arr.length -3; i >= 0; i--) |
128
|
|
|
{ |
129
|
|
|
// create element |
130
|
|
|
var para=document.createElement("div"); |
131
|
|
|
|
132
|
|
|
if(arr[i]['sent_by']!=arr[i]['start']) |
133
|
|
|
para.setAttribute('class','sender'); |
|
|
|
|
134
|
|
|
else |
135
|
|
|
para.setAttribute('class','receiver'); |
136
|
|
|
|
137
|
|
|
ele.appendChild(para); |
138
|
|
|
var bre=document.createElement("br"); |
139
|
|
|
bre.setAttribute("style","clear:both;"); |
140
|
|
|
ele.appendChild(bre); |
141
|
|
|
|
142
|
|
|
var info=document.createElement("p"); |
143
|
|
|
var node=document.createTextNode(arr[i]['message']); |
144
|
|
|
info.appendChild(node); |
145
|
|
|
para.appendChild(info); |
146
|
|
|
|
147
|
|
|
var tt=document.createElement("h6"); |
148
|
|
|
var inp=document.createTextNode(arr[i]['time']); |
149
|
|
|
tt.appendChild(inp); |
150
|
|
|
tt.setAttribute('class','message_time'); |
151
|
|
|
info.appendChild(tt); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$("#chat_heading a").remove('a'); |
155
|
|
|
var txt=$("<a></a>").text(arr[0].name); |
|
|
|
|
156
|
|
|
$("#chat_heading").append(txt); |
157
|
|
|
$("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr[0].username}); |
158
|
|
|
$("#text_reply").attr({'name':arr[0]['identifier_message_number']}); |
159
|
|
|
ele.scrollTop = ele.scrollHeight; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
else if(arr['new']==0) // New User |
163
|
|
|
{ |
164
|
|
|
ele.innerHTML=""; |
165
|
|
|
$("#chat_heading a").remove('a'); |
166
|
|
|
var txt=$("<a></a>").text(arr.name); |
|
|
|
|
167
|
|
|
$("#chat_heading").append(txt); |
168
|
|
|
$("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr.username}); |
169
|
|
|
$("#text_reply").attr({'name':arr['identifier_message_number']}); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
}; |
174
|
|
|
xmlhttp.open("POST", "ajax/chat.php", true); |
175
|
|
|
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
176
|
|
|
xmlhttp.send(q); |
177
|
|
|
} |
178
|
|
|
function stop() |
179
|
|
|
{ |
180
|
|
|
clearInterval(recursive); |
181
|
|
|
// console.log("recursive"); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
|
188
|
|
|
// For reply to other messages |
189
|
|
|
|
190
|
|
|
function reply() |
191
|
|
|
{ |
192
|
|
|
var ele=[document.getElementById("text_reply").value]; |
193
|
|
|
var id=document.getElementById("text_reply").name; |
194
|
|
|
|
195
|
|
|
// console.log(ele); |
196
|
|
|
var p=''; |
|
|
|
|
197
|
|
|
var q={"name":id,"reply":ele}; |
198
|
|
|
q="q="+JSON.stringify(q); |
199
|
|
|
// console.log(q); |
200
|
|
|
var xmlhttp = new XMLHttpRequest(); |
|
|
|
|
201
|
|
|
xmlhttp.onreadystatechange = function() { |
202
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) |
203
|
|
|
{ |
204
|
|
|
arr=xmlhttp.responseText; |
|
|
|
|
205
|
|
|
// console.log(arr); |
206
|
|
|
if (arr=="Messages is sent") // Message is sent |
207
|
|
|
{ |
208
|
|
|
document.getElementById("text_reply").value=""; |
209
|
|
|
} |
210
|
|
|
else{ |
|
|
|
|
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
}; |
215
|
|
|
xmlhttp.open("POST", "ajax/reply.php", true); |
216
|
|
|
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
217
|
|
|
xmlhttp.send(q); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
|
222
|
|
|
// Compose new and direct message to anyone |
223
|
|
|
|
224
|
|
|
function compose() |
225
|
|
|
{ |
226
|
|
|
flag=1; |
227
|
|
|
$("#chat_heading a").remove('a'); |
228
|
|
|
document.getElementById("conversation").innerHTML=""; |
229
|
|
|
$('#compose_text').show(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
|
234
|
|
|
//compose messages |
235
|
|
|
|
236
|
|
|
function compose_message() |
237
|
|
|
{ |
238
|
|
|
var q=document.getElementById("compose_name").value; |
239
|
|
|
// console.log(q); |
240
|
|
|
var ele=document.getElementById("suggestion"); |
241
|
|
|
ele.innerHTML=""; |
242
|
|
|
var xmlhttp = new XMLHttpRequest(); |
|
|
|
|
243
|
|
|
xmlhttp.onreadystatechange = function() |
244
|
|
|
{ |
245
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) |
246
|
|
|
{ |
247
|
|
|
arr=xmlhttp.responseText; |
|
|
|
|
248
|
|
|
arr=JSON.parse(arr); |
249
|
|
|
// console.log(arr); |
250
|
|
|
|
251
|
|
|
if (arr!=[] && arr!="Not Found") |
252
|
|
|
{ |
253
|
|
|
for (var i = arr.length - 1; i >= 0; i--) |
254
|
|
|
{ |
255
|
|
|
var para=document.createElement("li"); |
256
|
|
|
var active=document.createElement("a"); |
257
|
|
|
var node=document.createTextNode(arr[i].name); |
258
|
|
|
active.appendChild(node); |
259
|
|
|
active.setAttribute("href","#"); |
260
|
|
|
active.setAttribute("onclick","chat(this,10)"); |
261
|
|
|
active.setAttribute("class","suggestion"); |
262
|
|
|
active.setAttribute("id",arr[i].username); |
263
|
|
|
para.appendChild(active); |
264
|
|
|
ele.appendChild(para); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
else if(arr=="Not Found") |
268
|
|
|
{ |
269
|
|
|
var txt=$("<a></a>").text('Not Found'); |
270
|
|
|
var l=$("<li></li>").append(txt); |
271
|
|
|
$("#suggestion").append(l); |
272
|
|
|
$("#suggestion li a").attr({"onclick":"myFunction()"}); |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
$("#compose_selection").css("visibility","visible"); |
277
|
|
|
}; |
278
|
|
|
if(q!="") |
279
|
|
|
{ |
280
|
|
|
xmlhttp.open("GET", "ajax/suggestion.php?q=" + q, true); |
281
|
|
|
xmlhttp.send(); |
282
|
|
|
} |
283
|
|
|
else |
284
|
|
|
$("#compose_selection").css("visibility","hidden"); //for hidding the suggestion |
|
|
|
|
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
|
290
|
|
|
// For Search |
291
|
|
|
|
292
|
|
|
function search_choose() |
293
|
|
|
{ |
294
|
|
|
// console.log(1); |
295
|
|
|
var q=$("#search_item").val(); |
296
|
|
|
var ele=document.getElementById("message"); |
297
|
|
|
|
298
|
|
|
var xmlhttp = new XMLHttpRequest(); |
|
|
|
|
299
|
|
|
xmlhttp.onreadystatechange = function() |
300
|
|
|
{ |
301
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { |
302
|
|
|
arr=JSON.parse(xmlhttp.responseText); // Response From change.php |
|
|
|
|
303
|
|
|
// console.log(arr); |
304
|
|
|
if($("#search_item").val()=='') |
305
|
|
|
last_time=''; |
|
|
|
|
306
|
|
|
if (arr!=null) |
307
|
|
|
{ |
308
|
|
|
ele.innerHTML=""; |
309
|
|
|
for (var i = arr.length - 1; i >= 0; i--) // organising content according to time |
310
|
|
|
{ |
311
|
|
|
var para=document.createElement("a"); //creating element a |
312
|
|
|
var node=document.createTextNode(arr[i]['name']); |
313
|
|
|
para.appendChild(node); |
314
|
|
|
para.setAttribute('id',arr[i]['username']); |
315
|
|
|
para.setAttribute('href','message.php#'+arr[i]['username']); |
316
|
|
|
para.setAttribute('class','message'); |
317
|
|
|
para.setAttribute('onclick','chat(this)'); |
318
|
|
|
ele.appendChild(para); |
319
|
|
|
|
320
|
|
|
var bre=document.createElement("span"); // creating element span for showing time |
321
|
|
|
var inp=document.createTextNode(arr[i]['time']); |
322
|
|
|
bre.appendChild(inp); |
323
|
|
|
bre.setAttribute('class','message_time'); |
324
|
|
|
para.appendChild(bre); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
else |
328
|
|
|
{ |
329
|
|
|
$("#message").text(''); |
330
|
|
|
// console.log("None"); |
331
|
|
|
var txt=$("<a></a>").text("Not Found"); |
332
|
|
|
$("#message").append(txt); |
333
|
|
|
$("#message a").addClass('message'); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
} |
337
|
|
|
}; |
338
|
|
|
xmlhttp.open("GET", "ajax/search_item.php?q=" + q, true); |
339
|
|
|
xmlhttp.send(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
window.ondblclick=myFunction; |
344
|
|
|
|
345
|
|
|
function myFunction() // Hidden compose message input |
346
|
|
|
{ |
347
|
|
|
$("#compose_selection").css("visibility","hidden"); |
348
|
|
|
last_time=''; |
349
|
|
|
flag=0; |
350
|
|
|
store=''; |
351
|
|
|
$("#compose_name").val(''); |
352
|
|
|
$("#search_item").val(''); |
353
|
|
|
$('#compose_text').hide(); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
|
357
|
|
|
function previous(element) // Load previous messages |
358
|
|
|
{ |
359
|
|
|
var user=element.id; |
|
|
|
|
360
|
|
|
var lo=element.name; |
361
|
|
|
chat(element,lo); |
362
|
|
|
store=''; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
console.log("Hello, Contact me at [email protected]"); |
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.