﻿/*
	AjaxRequest v1.0 <http://www.jesperavot.nl/ar/>
    Copyright 2006 Jesper Avot
    This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
    
    TODO List for v1.5
    	- XML Support, allows php to return XML output instead of plain text/html.
    		This feature also allows multipli div's which can be filled with one request.
    	- Predefined functions for special stuff.
    	- Extra option to let the result be parsed by a custom function.
    	
    If you have a feature request, please email it to jesper@avot.nl
*/

var AR = {
	/*
		Predefined variablen
	*/
	Version: "1.0",
	XMLRequests: [],
	XMLIndex: 0,
	
	/*
		About the script function
	*/
	about: function()
	{
		alert( "AJAXRequest v" + AR.Version + "\nWritten by Jesper Avot\n\nFor more information, visit\nhttp://www.jesperavot.nl/ar/" );
	},
	
	/*
		doRequest function
			wrapper for AR.create
	*/
	doRequest: function( Input, Type, TimeOut )
	{
		AR.create( Input, Type, TimeOut );
	},
	
	/*
		Create function
	*/
	create: function( Input, PostString, TimeOut )
	{
		if( typeof Input != "object" )
		{
			Input = { URL: Input };
		}
		
		var XMLObjectID = AR.XMLIndex;
		AR.XMLIndex++;
		
		if( typeof TimeOut !== "number" )
		{
			TimeOut = 60;
		}
		
		AR.XMLRequests[XMLObjectID] = [];
		AR.XMLRequests[XMLObjectID]["Input"] = Input;
		AR.XMLRequests[XMLObjectID]["TimeOut"] = window.setTimeout( "AR.stop(" + XMLObjectID + ");", ( TimeOut * 1000 ) );
		AR.XMLRequests[XMLObjectID]["XMLObject"] = AR.createXMLHTTPRequest();
		
		if( AR.XMLRequests[XMLObjectID]["XMLObject"] === false )
		{
			// Request failed, stop the script.
			alert( "Request failed" );
			return false;
		}
		
		if( typeof PostString != "undefined" && PostString.length > 0 )
		{
			AR.XMLRequests[XMLObjectID]["XMLObject"].open( "POST", Input.URL, true );
			AR.XMLRequests[XMLObjectID]["XMLObject"].setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		}
		else
		{
			AR.XMLRequests[XMLObjectID]["XMLObject"].open ( "GET", Input.URL, true );
		}
		
		AR.XMLRequests[XMLObjectID]["XMLObject"].onreadystatechange = function()
		{
			if( AR.XMLRequests[XMLObjectID]["XMLObject"].readyState == 4 )
			{
				window.clearTimeout( AR.XMLRequests[XMLObjectID]["TimeOut"] );
				
				if( AR.XMLRequests[XMLObjectID]['XMLObject'].status == 200 )
				{
					AR.draw( XMLObjectID );
				}
				else
				{
					alert( "Data could not be fetched" );
				}
			}
		};
		
		AR.XMLRequests[XMLObjectID]["XMLObject"].send( ( typeof PostString != "undefined" && PostString.length > 0 ? PostString : null ) );
	},
	
	/*
		Stop function
	*/
	stop: function( XMLObjectID )
	{
		if( typeof AR.XMLRequests[XMLObjectID] == 'object' && AR.XMLRequests[XMLObjectID]['XMLObject'] !== true && AR.XMLRequests[XMLObjectID]['XMLObject'] !== false )
		{
			AR.XMLRequests[XMLObjectID]['XMLObject'].onreadystatechange = function() { };
			AR.XMLRequests[XMLObjectID]['XMLObject'].abort();
			AR.XMLRequests[XMLObjectID]['XMLObject'] = false;
			
			alert( "Timeout reached, please try again" );
		}
	},
	
	/*
		createXMLHTTPRequest function
	*/
	createXMLHTTPRequest: function()
	{
		var ActiveXTypes = [ 
		    "Microsoft.XMLHTTP", 
		    "MSXML2.XMLHTTP.5.0", 
		    "MSXML2.XMLHTTP.4.0", 
		    "MSXML2.XMLHTTP.3.0", 
		    "MSXML2.XMLHTTP" 
	   	]; 

		for( var i = 0; i < ActiveXTypes.length; i++ )
		{ 
			try
			{
				return new ActiveXObject( ActiveXTypes[i] ); 
			}
			catch( e )
			{ } 
		} 

		try
		{ 
			return new XMLHttpRequest(); 
		}
		catch( e )
		{ }
		
		return false;
	},
	
	/*
		Draw function
	*/
	draw: function( XMLObjectID )
	{
		var Object    = AR.XMLRequests[XMLObjectID];
		var Input     = Object["Input"]; // For feature use
		var XMLObject = Object["XMLObject"];
		var ResultDiv = Input.ResultDiv;
		
		if( typeof ResultDiv != "undefined" && document.getElementById( ResultDiv ) )
		{
			document.getElementById( ResultDiv ).innerHTML = unescape( XMLObject.responseText );
		}
	}
};

