// JavaScript Document

//////////////////////////////////////////////////////////////////
// RDA's Ajax Requester Class
function RdAjaxRequest(objUpdater,strUrl,strMethod,strPostData) {
	var requestHeaders = ["X-RDAjax-Version", "0.2" ];
	
	if ( strMethod == "post" )
	{
        requestHeaders.push( "Content-type", "text/xml" );
    }

	this.objUpdater = objUpdater;
    this.ajax = new Ajax.Request ( strUrl,
					  {
						  method: strMethod,
						  postBody: strPostData,
						  requestHeaders: requestHeaders,
						  onComplete: this.onComplete.bind(this)
					  }
					  );
};

RdAjaxRequest.prototype.onComplete=function(request) {
	if(!request)
		return;
// User can set an onFailure option - which will be called by prototype
	if (request.status != 200)
	{
		alert('Request returned: ' + request.status);
		return;
	}
	this.objUpdater.ajaxUpdate(request.responseXML.documentElement);
};
//////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////

function GetAjaxQuotation ( iIndex, bWantPrev ) 
{
	var strTheUrl;

	//strTheUrl = "/RDJavaWeb/QuoteServlet";
	//strTheUrl = "/QuoteServlet";
	strTheUrl = "/getquote.php?asxml";
	//strTheUrl = "/rdweb/getquote.php?asxml";
	
	if ( iIndex > 0 )
	{
		strTheUrl += "&quoteindex=" + iIndex;
		if ( bWantPrev )
			strTheUrl += "&previous";
	}	
	
	new RdAjaxRequest( new ResponseParser('divQuoteBox'), 
						strTheUrl, 'get', '' ); 
};


//////////////////////////////////////////////////////////////////

function ResponseParser(divId) {
	this.divId = divId;
	//this.DomHelper = new RdDomHelper ( divId );
	//this.DomHelper.setDivText("");
}

ResponseParser.prototype.ajaxUpdate=function(ajaxResponse) 
{
	// Look for the response
	var QuoteName = this.getElementTagContent ( ajaxResponse, 'quoteName' );
	var Quotation = this.getElementTagContent ( ajaxResponse, 'quotation' );
	var QuoteIndex = this.getElementTagContent ( ajaxResponse, 'quoteIndex' );
	
	if ( QuoteName != "" && Quotation != "" )
	{
		// Got something.  Stick it in the div.
		
		// First, look at the index and calc the next/prev ones.
		var iIndex = parseInt(QuoteIndex);
		var iPrev = ( iIndex > 0 ? iIndex - 1 : 0 );
		var iNext = ( iIndex > 0 ? iIndex + 1 : 0 );
		
		var strTheText = "";
		
		strTheText +=
			"<p>" +
				"<b>Quick Quote:</b>" +
				"&nbsp;&nbsp;" +
				"&nbsp;&nbsp;" +
				"<img src='images/arrowsmlf.gif' style='cursor:pointer;' " +
		             "onclick='GetAjaxQuotation(" + iPrev + ",1)'>" +
				"&nbsp;&nbsp;" +
				"<img src='images/arrowsmrt.gif' style='cursor:pointer;' " +
					 "onclick='GetAjaxQuotation(" + iNext + ",0)'>" +
			"</p>" +
			"<p>" +
				Quotation +
				"<br /><br />" +
				"<i>" + QuoteName + "</i>" +
			"</p>";
		
		// Now go do the div thang
		//this.DomHelper.clearDiv();
		//this.DomHelper.setDivText ( strTheText );
		var theDiv=document.getElementById(this.divId);
		if (theDiv)
		{
			theDiv.innerHTML = strTheText;
		}
		
		
	}
}


ResponseParser.prototype.getElementTagContent = function ( ajaxResponse, strTag )
{
	var strReturn = "";
	var el = ajaxResponse.getElementsByTagName ( strTag );
	
	// Did we get something?
	if ( el )
	{
		// Does it contain something?
		if ( el.length > 0 )
		{
			strReturn = this.getElementContent ( el[0] );
		}
	}
	
	return strReturn;
}

ResponseParser.prototype.getElementContent = function ( el ) 
{
	// From "Ajax In Action", p. 357.
	// Apparently, IE uses ".text" and W3C says ".textContent".
	return ( el.text != undefined ? el.text : el.textContent );
}

ResponseParser.prototype.getAttributeValue = function ( el, strName, dflValue ) 
{
	// If the attribute node exists, return its value.
	// Else return the given default value.
	return ( el.getAttributeNode(strName) ? el.getAttributeNode(strName).value : dflValue );
}



