/**
 * Project:     PHPSitePro: the PHP site engine
 * File:        ajaxManager.js
 *
 * @author		Bensussán, Ariel Martín
 * @package		ajax
 * @version		1.0.0
 */

//document.domain = 'billiken.com.ar' ;

var ifrIDs = new Array();
var sources = new Array();

/**
 * @desc	Get the data of a dynamic page usin the AJAX methodology by GET method
 * @param	string pageURL
 * @param	string divID
 * @param	boolean pageURL
 * @return	void
 * @access	Public
 */
function obtainPageDataByGetMethod(pageURL, divID) {
	var xmlHTTPReq = createXMLHTTPReq();

	if(xmlHTTPReq) {
		pageURL = addTimeParameter(pageURL);
		xmlHTTPReq.open("GET", pageURL);
		processPageRequest(divID, xmlHTTPReq);
		try {
			xmlHTTPReq.send(null);
		} catch(e) {
			document.write("getData: xmlHTTPReq.onreadystatechange Error");
		}
	}
}

/**
 * @desc	Creates the XMLHTTPReq object in agreement of the client browser
 * @param	void
 * @return	XMLHTTPReq xmlHTTPReq
 * @access	Public
 */
function createXMLHTTPReq() {
	var xmlHTTPReq = false;

	try {
		xmlHTTPReq = new XMLHttpRequest();
	} catch(e) {
		var activeXObjectXMLHTTP = new Array("MSXML2.XMLHTTP", "Microsoft.XMLHTTP", "MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0");

		for(var i = 0; i < activeXObjectXMLHTTP.length && !xmlHTTPReq; i++) {
			try {
				xmlHTTPReq = new ActiveXObject(activeXObjectXMLHTTP[i]);
			} catch (e) {
				//DO NOTHING
			}
		}
	}
	if(!xmlHTTPReq) {
		alert("Error: It has not been possible to create object XMLHTTPReq.");
	}
	return xmlHTTPReq;
}

/**
 * @desc	Insert the time parameter to the page URL
 * @param	string pageURL
 * @return	void
 * @access	Public
 */
function addTimeParameter(pageURL) {
	var separator = '&';

	if(pageURL.indexOf('?') == -1) {
		separator = '?';
	}
	return pageURL + separator + "time=" + new Date().getTime();
}

/**
 * @desc	Process the page request
 * @param	string divID
 * @param	XMLHTTPReq xmlHTTPReq
 * @return	void
 * @access	Public
 */
function processPageRequest(divID, xmlHTTPReq) {
	xmlHTTPReq.onreadystatechange = function() {
		try {
			if(xmlHTTPReq.readyState == 4 && xmlHTTPReq.status == 200) {
				var divObject = document.getElementById(divID);
				if(divObject) {
					var responseText = xmlHTTPReq.responseText;
					divObject.innerHTML = responseText;
				}
			}
		} catch(e) {
			document.write("Error: An error has occured in xmlHTTPReq.readyState.");
		}
	}
}

/**
 * @desc	Get the data of a dynamic page usin the AJAX methodology by POST method
 * @param	string pageURL
 * @param	string divID
 * @param	boolean pageURL
 * @return	void
 * @access	Public
 */
function obtainPageDataByPostMethod(pageURL, divID) {
	var xmlHTTPReq = createXMLHTTPReq();

	if(xmlHTTPReq) {
		xmlHTTPReq.open("POST", pageURL);
		xmlHTTPReq.setRequestHeader("Method", "POST " + pageURL + " HTTP/1.1");
		xmlHTTPReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		processPageRequest(divID, xmlHTTPReq);
		pageURL = addTimeParameter(pageURL);
		try {
			xmlHTTPReq.send(pageURL);
		} catch(e) {
			document.write("Error: An error has occured in xmlHTTPReq.onreadystatechange.");
		}
	}
}


/**
 * @desc	Get the data of a dynamic page usin the AJAX methodology by POST method
 * @param	string pageURL
 * @param	string divID
 * @param	boolean pageURL
 * @return	void
 * @access	Public
 */
function pasteHTML(content, divID) {
	var divObject = document.getElementById(divID);
	if(divObject) {
		divObject.innerHTML = content;
		divObject.style.visibility = 'visible' ;
	}
}


/**
 * @desc	Get the data of a dynamic page usin the AJAX methodology by POST method
 * @param	string pageURL
 * @param	string divID
 * @param	boolean pageURL
 * @return	void
 * @access	Public
 */
function reloadIframe(ifrID, source) {
	var ifrObject = document.getElementById(ifrID);
	if(ifrObject) {
		ifrObject.src = source;
	}
}

/**
 * @desc	Get the data of a dynamic page usin the AJAX methodology by POST method
 * @param	string pageURL
 * @param	string divID
 * @param	boolean pageURL
 * @return	void
 * @access	Public
 */
function reloadBlocks() {
	for(var i=0; i < ifrIDs.length; i++) {
		reloadIframe(ifrIDs[i], sources[i]);
	}
}

/**
 * @desc	Get the data of a dynamic page usin the AJAX methodology by POST method
 * @param	string pageURL
 * @param	string divID
 * @param	boolean pageURL
 * @return	void
 * @access	Public
 */
function registerBlock(ifrID, source) {
	ifrIDs[ifrIDs.length] = ifrID;
	sources[sources.length] = source;
}
