File: 0.00.1a/js/ajax.js (View as HTML)

  1: /* -------------------------------------------------------------
  2: This file is part of FreeDESK
  3: 
  4: FreeDESK is (C) Copyright 2012 David Cutting
  5: 
  6: FreeDESK is free software: you can redistribute it and/or modify
  7: it under the terms of the GNU General Public License as published by
  8: the Free Software Foundation, either version 3 of the License, or
  9: (at your option) any later version.
 10: 
 11: FreeDESK is distributed in the hope that it will be useful,
 12: but WITHOUT ANY WARRANTY; without even the implied warranty of
 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14: GNU General Public License for more details.
 15: 
 16: You should have received a copy of the GNU General Public License
 17: along with FreeDESK.  If not, see www.gnu.org/licenses
 18: 
 19: For more information see www.purplepixie.org/freedesk/
 20: -------------------------------------------------------------- */
 21: 
 22: // This is the main AJAX functionality for server requests in FreeDESK
 23: // client-side
 24: 
 25: function ServerRequest()
 26: {
 27: 	this.callback = ""; // callback function
 28: 	this.url = ""; // URL
 29: 	this.additional = new Array(); // Additional data
 30: 	this.xmlhttp = false; // XML HTTP Request Object
 31: 	this.xmlrequest = true; // Is it an XML Request?
 32: 	this.async = true; // Is it asynchronous (not yet implemented)
 33: 	this.randomise = true; // Set a random string to query to avoid caching
 34: 	
 35: 	this.makeXmlHttp = function()
 36: 	{
 37: 		if (window.XMLHttpRequest) // all good browsers and indeed IE nowadays
 38: 		{
 39: 			this.xmlhttp = new XMLHttpRequest;
 40: 			if (this.xmlrequest && this.xmlhttp.overrideMimeType)
 41: 				this.xmlhttp.overrideMimeType("text/xml");
 42: 			else if (!this.xmlrequest && this.xmlhttp.overrideMimeType)
 43: 				this.xmlhttp.overrideMimeType("text/html");
 44: 		}
 45: 		else if (window.ActiveXObject) // Satan's Browser (old IE or Exploiter 7+ with XMLHttp Disabled)
 46: 		{
 47: 			try
 48: 			{
 49: 				this.xmlhttp = new ActiveXObject("Msxml2.HTTP"); // Newer
 50: 			}
 51: 			catch(e)
 52: 			{
 53: 				try
 54: 				{
 55: 					this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 56: 				}
 57: 				catch(ex)
 58: 				{
 59: 					// No, Microsoft WHAT HAVE YOU DONE??!?
 60: 					alert("Fatal Error: Cannot create ActiveXObject for xmlhttp");
 61: 				}
 62: 			}
 63: 		}
 64: 		else
 65: 		{
 66: 			// Dear lord are we running in lynx ??!?
 67: 			alert("Fatal Error: Cannot create xmlhttp object");
 68: 		}
 69: 		
 70: 		if (!this.xmlhttp)
 71: 			alert("Error: Cannot Create XMLHTTP Object");
 72: 	}
 73: 	
 74: 	this.randomString = function(len)
 75: 	{
 76: 		if (len == undefined)
 77: 			len = 32;
 78: 		var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
 79: 		var randomstring = "";
 80: 		for (var i=0; i<len; ++i)
 81: 		{
 82: 			var randchar = Math.floor(Math.random() * chars.length);
 83: 			randomstring += chars.substring(randchar, randchar+1);
 84: 		}
 85: 		return randomstring;
 86: 	}
 87: 	
 88: 	this.Get = function()
 89: 	{
 90: 		if (!this.xmlhttp)
 91: 			this.makeXmlHttp();
 92: 			
 93: 		var myurl = this.url;
 94: 		if (this.randomise)
 95: 		{
 96: 			if (myurl.indexOf("?") == -1)
 97: 				myurl += "?nocache="+this.randomString();
 98: 			else
 99: 				myurl += "&nocache="+this.randomString();
100: 		}
101: 		
102: 		this.xmlhttp.open('GET', myurl, true);
103: 		this.xmlhttp.ajax = this;
104: 		if (this.xmlrequest)
105: 		{
106: 			this.xmlhttp.onreadystatechange = function()
107: 			{
108: 				if (this.readyState == 4)
109: 				{
110: 					if (this.status == 200)
111: 					{
112: 						if (this.responseXML)
113: 						{
114: 							if (DESK.isError(this.responseXML))
115: 							{
116: 								if (DESK.getErrorCode(this.responseXML)==102) // expired session
117: 								{
118: 									DESK.show_login(DESK.getError(this.responseXML));
119: 									return;
120: 								}
121: 							} // Hand errors other than 102 through to the callback routine to handle
122: 							this.ajax.callback(this.responseXML, this.ajax.additional );
123: 						}
124: 						else
125: 						{
126: 							alert("AJAX XML Error: Invalid or Null\nBody:\n"+this.responseXML);
127: 						}
128: 					}
129: 					else
130: 					{
131: 						alert("AJAX Server Code: "+this.status+"\nURL: "+this.ajax.url);
132: 					}
133: 				}
134: 			}
135: 		}
136: 		else
137: 		{
138: 			// HTML Request
139: 			this.xmlhttp.onreadystatechange = function()
140: 			{
141: 				if (this.readyState == 4)
142: 				{
143: 					if (this.status == 200)
144: 					{
145: 						if (this.responseText)
146: 						{
147: 							this.ajax.callback(this.responseText, this.ajax.additional);
148: 						}
149: 						else
150: 						{
151: 							alert("AJAX Text Error: Invalid or Null Body\nBody:\n"+this.responseText);
152: 						}
153: 					}
154: 					else
155: 					{
156: 						alert("AJAX Server Code: "+this.status+"\nURL: "+this.ajax.url);
157: 					}
158: 				}
159: 			}
160: 		}
161: 		this.xmlhttp.send();
162: 	}
163: 	
164: 	
165: 	
166: 	this.Post = function(postdata)
167: 	{
168: 		if (!this.xmlhttp)
169: 			this.makeXmlHttp();
170: 		
171: 		this.xmlhttp.open('POST', this.url, true);
172: 		this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
173: 		this.xmlhttp.ajax = this;
174: 		if (this.xmlrequest)
175: 		{
176: 			this.xmlhttp.onreadystatechange = function()
177: 			{
178: 				if (this.readyState == 4)
179: 				{
180: 					if (this.status == 200)
181: 					{
182: 						if (this.responseXML)
183: 						{
184: 							if (DESK.isError(this.responseXML))
185: 							{
186: 								if (DESK.getErrorCode(this.responseXML)==102) // expired session
187: 								{
188: 									DESK.show_login(DESK.getError(this.responseXML));
189: 									return;
190: 								}
191: 							} // Hand errors other than 102 through to the callback routine to handle
192: 							this.ajax.callback(this.responseXML, this.ajax.additional );
193: 						}
194: 						else
195: 						{
196: 							alert("AJAX XML Error: Invalid or Null\nBody:\n"+this.responseXML);
197: 						}
198: 					}
199: 					else
200: 					{
201: 						alert("AJAX Server Code: "+this.status+"\nURL: "+this.ajax.url);
202: 					}
203: 				}
204: 			}
205: 		}
206: 		else
207: 		{
208: 			// HTML Request
209: 			this.xmlhttp.onreadystatechange = function()
210: 			{
211: 				if (this.readyState == 4)
212: 				{
213: 					if (this.status == 200)
214: 					{
215: 						if (this.responseText)
216: 						{
217: 							this.ajax.callback(this.responseText, this.ajax.additional);
218: 						}
219: 						else
220: 						{
221: 							alert("AJAX Text Error: Invalid or Null Body\nBody:\n"+this.responseText);
222: 						}
223: 					}
224: 					else
225: 					{
226: 						alert("AJAX Server Code: "+this.status+"\nURL: "+this.ajax.url);
227: 					}
228: 				}
229: 			}
230: 		}
231: 		this.xmlhttp.send(postdata);
232: 	}
233: 	
234: }
235: 
236: