/** 
 ** @author PS
 ** @version 1.0.0
***/

var AJAX = {
	createRequest :	function (method, url, params, code) {
		var xmlHttp = null;
		try {
			
			// Firefox, Opera 8.0+, Safari
    	xmlHttp = new XMLHttpRequest();
		} catch(e) {			
			try {
				
				// Internet Explorer
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
				
				// AJAX is not supported by browser
				return false;
			}
		}
		
		// on ready state change function -- occurs every time state changes
		xmlHttp.onreadystatechange = function () {
			// call function when request has finished successfully
			if(xmlHttp.readyState == 4) {
				if(typeof code == 'function') code(xmlHttp.responseText != null ? xmlHttp.responseText : '');
				else alert('Currently, this code format is not supported.');
			}
		}		
		
		// setting up method
		if(typeof method != 'string' || (method = method.toUpperCase()) != 'POST') method = 'GET';
		
		// parsing correct url
		//if(typeof url != 'string') url = '/js/ajax/error/blank.html';
		if(method == 'GET') {
			url += '?';
			if(typeof params == 'string') url += params;
		}
		
		// opening connection
		xmlHttp.open(method,url,true);
		
		// sending request
    if(method == 'GET') {
			// send no params since params are embeded in url
			xmlHttp.send(null);
		} else if(method == 'POST') {
			// add necessary request headers
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			// send the params
			xmlHttp.send(params);
		}
		
		// return successful AJAX operation
		return true;
	}
}