﻿/**
 * @projectDescription ajax implementation class ported from "Ajax Trucchi e Segreti" Hack #3 application by Matteo
 * @author vito.dituccio, matteo.salomone
 * Cross-Browser Ajax Toolkit. Tested with IE 6+ / Firefox 2+ / Chrome / Safari
 */

var Ajax = function(){};

Ajax.prototype = {
	/**
	 * Class constructor
	 */
	constructor: Ajax,
	/**
	 * Internal XMLHttpRequest Object
	 */
	request: null,
	/**
	 * initialize class instance
	 * @author matteo.salomone, vito.dituccio
	 * @return void
	 */
	init: function() {
		if(window.XMLHttpRequest) {
			this.request = new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			this.request = new ActiveXObject("Msxml2.XMLHTTP");
			if(!this.request) {
				this.request = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		else throw new Error("AJAX REQUEST ERROR: This browser doesn't support Ajax technology.");
	},
	/**
	 * generate random number to avoid browser caching the request
	 */
	random: function() { 
		return Math.round(Math.random() * 100000000000000);
	},
	/**
	 * Send a new request to the server
	 * @author matteo.salomone, vito.dituccio
	 * @param {String} reqType - type of HTTP request (GET or POST)
	 * @param {String} url - URL of the server side program
	 * @param {Boolean} [asynch] - optional, send an asynchronous request (default). set to false to force a synchronous request
	 * @param {Function} [respHandler] - optional, reference to the handler function that manage the response, otherwise it'll be used internal defaultRespHandler method
	 * @param {String} [postData] - optional, data to send for post request
	 * @param {Boolean} [codeIgniter] - optional, specifies if the request have to be done with a codeIgniter-like syntax
	 * @return void
	 */
	send: function(reqType, url, asynch, respHandler, postData, codeIgniter) {
		if(!url || url.constructor !== String) {
			throw new Error("AJAX SEND ERROR: Invalid url.");
			return false;
		}
		reqType = reqType.toLowerCase();
		if(reqType !== "post" && reqType !== "get") {
			throw new Error("AJAX SEND ERROR: Invalid HTTP request type.");
			return false;
		}
		this.init();
		if(!codeIgniter) {
			url += /\?/.test(url) ? "&" : "?";
			url += "rnd=" + this.random();
		} else {
			url += !/\/$/.test(url) ? "/" : "";
			url += this.random();
		}
		if(typeof asynch == "undefined" || asynch.constructor !== Boolean) asynch = true;
		try {
			if(respHandler && respHandler.constructor == Function) {
				this.request.onreadystatechange = respHandler;
			} else {
				var rqt = this.request;
				this.request.onreadystatechange = function() {
					//readyState 4 means "completed" (other values: 0=inizializing, 1=loading, 2=loaded, 3=interactive)
					if(rqt.readyState == 4) {
						//status 200 means "OK" (HTTP status)(other values: 404=Not Found)
						if(rqt.status == 200) {
							eval(rqt.responseText);
						} else {
							throw new Error("AJAX REQUEST ERROR: connection problem between XMLHttpRequest object and the server program.");
						}
					}
				};
			}
			this.request.open(reqType, url, asynch);
			if(reqType == "post") {
				this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				this.request.send(postData);
			} else {
				this.request.send(null);
			}
		} catch(err) {
			throw new Error("AJAX REQUEST ERROR: cannot reach the server, please try later.\nError Details: " + err.message);
			return false;
		}
	}
};