//
//	HtmlRequestObject
// Ver 2.0
//
// Author: Himanshu Shrivastava
//
// Description:
// 	Re-wrote HtmlRequestObject to work with one connection at a time
//		Done for making more than one connection by instantiating more than
//		one object and executing.
//
// Copyright (c) Oklahoma Climate Survey
//

function HtmlRequestObject( inLoadType, inLoadVal )
{
	var lThisObject = this;
	var lRequestingPage = false;
	this.runAfterProcessingRequest = function() { }

	var lLoadHtml = '';

	switch ( inLoadType )
	{
		case 1: case 'text':
			lLoadHtml = inLoadVal;
			break;
		case 2: case 'image':
			lLoadHtml = "Loading ... <br><img src='"+inLoadVal+"' />";
			break;
		default:
	}

	this.ProcessRequest = function( inLayerId, inUrl, inHtmlVar, inMethod )
	{
		var inLayer = document.getElementById( inLayerId );
		if( ! inMethod ) inMethod = 'GET';
		if( lRequestingPage == true ) { return false; }
		
		lRequestingPage = true;
		
		var lHttpObj = null;

		if( window.XMLHttpRequest ) { lHttpObj = new XMLHttpRequest(); }
		else { lHttpObj = new ActiveXObject( "Microsoft.XMLHTTP" ); }

		if( lHttpObj == null )
		{
			alert( "Browser does not support AJAX" );
			return false;
		}
		else
		{
			lHttpObj.onreadystatechange = function()
			{
				if( lHttpObj.readyState == 4 || lHttpObj.readyState == 'complete' )
				{
					if( lHttpObj.status == 200 )
					{
						inLayer.innerHTML = lHttpObj.responseText;
						
						delete lHttpObj;
						lRequestingPage = false;

						lThisObject.runAfterProcessingRequest();
					}
					else
					{
						inLayer.innerHTML = 'URL not found';
						delete lHttpObj;
						lRequestingPage = false;
					}
				}
				else
				{
					if( lLoadHtml != '' ) inLayer.innerHTML = "<center>" + lLoadHtml + "</center>";
				}
			}

			if( inMethod.toLowerCase() == 'post' )
			{
				lHttpObj.open (inMethod, inUrl, true );
				lHttpObj.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
				lHttpObj.send( inHtmlVar );
			}
			else
			{
				var ltempUrl = inUrl;
				if( inHtmlVar )	{	ltempUrl += "?"+inHtmlVar;	}
				lHttpObj.open( inMethod, ltempUrl, true );
				lHttpObj.send( null );
			}
		}
	}
}
