summaryrefslogtreecommitdiffstats
path: root/modules/kajax/ajax.js
blob: 4a3e105ac6dd0365e5d3f7aa7b677dfda9e7d057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Ampache.org
// All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License v2
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// uid is an array of uids that need to be replaced		
function ajaxPut(url,source) {

	if (document.getElementById(source)) { 
		Event.stopObserving(source,'click',function(){ajaxPut(url,source);});
	} 

	if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
                	try {
       			        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                	} 
			catch (e) {}
		}
        }
	else { // Mozilla
		IE = false;
		http_request = new XMLHttpRequest();	
	}
        if (!http_request) {
            return false;
        }
        http_request.onreadystatechange = function() { getContents(http_request); };
        http_request.open('GET', url, true);
       	http_request.send(null);	
}

// uid is an array of uids that need to be replaced             
function ajaxState(url,input) {

        var data = document.getElementById(input).value

        var post_data = input + '=' + encodeURI(data); 

        var http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
                http_request = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
                try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                try {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
                }
        }
        if (!http_request) {
                return false;
        }
        http_request.onreadystatechange = function() { getContents(http_request); };
        http_request.open('POST', url, true);
        http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http_request.setRequestHeader("Content-length", post_data.length);
        http_request.setRequestHeader("Connection", "close");
        http_request.send(post_data);

} // ajaxState


///
// getContents
// This is the nitty gritty wait for a good xml document and then decode it
function getContents(http_request) {
	
	// Display the loading doodly
	document.getElementById('ajax-loading').style.display = 'block';

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var data = http_request.responseXML;
			var newContent = http_request.responseXML.getElementsByTagName('content'); 
	               

			for(var i=0; i < newContent.length; i++) {  
				var newID = newContent[i].getAttribute('div'); 				
				if (document.getElementById(newID)) { 
					$(newID).update(newContent[i].firstChild.nodeValue); 
				} 
			}
			document.getElementById('ajax-loading').style.display = 'none';
		} 
        }
}

function ajaxPost(url,input,source) { 
    
	if (document.getElementById(source)) { 
		Event.stopObserving(source,'click',function(){ajaxPost(url,input,source);}); 
	} 

        var post_data = 'a=0';
	var data = document.getElementById(input).elements;

	// For the post data we recieved
	for(i=0;i<data.length;i++) { 
		var frm_field = data[i];

		// This makes the value of the checkbox the checked status, more usefull
		if (frm_field.type == 'checkbox') { 
			frm_field.value = frm_field.checked; 
		} 
		post_data = post_data +'&' + frm_field.name + '=' + encodeURI(frm_field.value);
	}
	
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
        	http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
        	try {
	        	http_request = new ActiveXObject("Msxml2.XMLHTTP");
	        } catch (e) {
	        try {
		        http_request = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (e) {}
	        }
	}
	if (!http_request) {
		return false;
	}
	http_request.onreadystatechange = function() { getContents(http_request); };
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", post_data.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(post_data);

}