﻿//----------------------------------------------------------------
// Zflow AJAX

Zflow.Ajax = function(){}

Zflow.Ajax.createXmlHttp = function()
{
	var xmlHttp = null;
	
	try 
	{
		xmlHttp = new XMLHttpRequest();
	} 
	catch (trymicrosoft) 
	{
		try 
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (othermicrosoft) 
		{
			try 
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed) 
			{
				xmlHttp = null;
			}
		}
	}
	
	return xmlHttp;
}

Zflow.Ajax.getResponse = function(url, resultHandler, context, errorHandler)
{
	var _xmlHttp = Zflow.Ajax.createXmlHttp();
	
	if (_xmlHttp)
	{
		_xmlHttp.open("GET", url, true);
		_xmlHttp.onreadystatechange = function ()
		{
			if (_xmlHttp.readyState == 4)
			{
				if (_xmlHttp.status == 200)
				{
					var _response = _xmlHttp.responseText;
					
					if (resultHandler != null)
					{
						resultHandler(_response, context);
					}
				}
				else
				{
					if (errorHandler != null)
					{
						errorHandler(_xmlHttp.status, _xmlHttp.statusText, context);
					}
					else
					{
						alert("Request Failure - (" + _xmlHttp.statusText + ")");
					}
				}
			}
		}
		_xmlHttp.send(null);
	}
}

Zflow.Ajax.postResponse = function(url, postData, resultHandler, context, errorHandler)
{
	var _xmlHttp = Zflow.Ajax.createXmlHttp();

	if (_xmlHttp)
	{
		_resultHandler = resultHandler;
		
		_xmlHttp.open("POST", url, true);
		_xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		_xmlHttp.onreadystatechange = function ()
		{
			if (_xmlHttp.readyState == 4)
			{
				if (_xmlHttp.status == 200)
				{
					var _response = _xmlHttp.responseText;
					
					if (resultHandler != null)
					{
						resultHandler(_response, context);
					}
				}
				else
				{
					if (errorHandler != null)
					{
						errorHandler(_xmlHttp.status, _xmlHttp.statusText, context);
					}
					else
					{
						alert("Request Failure - (" + _xmlHttp.statusText + ") [" + _xmlHttp.status + "]");
					}
				}
			}
		}
		_xmlHttp.send(postData);
	}
}

Zflow.Ajax.getXmlResponse = function(url, resultHandler, context, errorHandler, errorHandler)
{
	var _xmlHttp = Zflow.Ajax.createXmlHttp();
	
	if (_xmlHttp)
	{
		_xmlHttp.open("GET", url, true);
		_xmlHttp.onreadystatechange = function ()
		{
			if (_xmlHttp.readyState == 4)
			{
				if (_xmlHttp.status == 200)
				{
					var _response = _xmlHttp.responseXML;
					
					if (resultHandler != null)
					{
						resultHandler(_response, context);
					}
				}
				else
				{
					if (errorHandler != null)
					{
						errorHandler(_xmlHttp.status, _xmlHttp.statusText, context);
					}
					else
					{
						alert("Request Failure - (" + _xmlHttp.statusText + ") [" + _xmlHttp.status + "]");
					}
				}
			}
		}
		_xmlHttp.send(null);
	}
}

Zflow.Ajax.postXmlResponse = function(url, postData, resultHandler, context, errorHandler)
{
	var _xmlHttp = Zflow.Ajax.createXmlHttp();

	if (_xmlHttp)
	{
		_resultHandler = resultHandler;
		
		_xmlHttp.open("POST", url, true);
		_xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		_xmlHttp.onreadystatechange = function ()
		{
			if (_xmlHttp.readyState == 4)
			{
				if (_xmlHttp.status == 200)
				{
					var _response = _xmlHttp.responseXML;
					
					if (resultHandler != null)
					{
						resultHandler(_response, context);
					}
				}
				else
				{
					if (errorHandler != null)
					{
						errorHandler(_xmlHttp.status, _xmlHttp.statusText, context);
					}
					else
					{
						alert("Request Failure - (" + _xmlHttp.statusText + ") [" + _xmlHttp.status + "]");
					}
				}
			}
		}
		_xmlHttp.send(postData);
	}
}

