/* ********************************************************************************
*
* Author: Aouse Al-Rawi
* Library: Ajax
* Date: 2008
*
* ********************************************************************************/






/* ********************************************************************************
*
* Class: Ajax
* Description: Ajax class definition
*
* ********************************************************************************/
function ajaxObject(url,callBack,parameters,method,postParams){
this.url = url;
this.date = new Date();
this.timestamp = this.date.getTime();
this.xhrObj;
this.callBack = 'this.'+callBack;
this.callBackParams = parameters;
this.method = method;
this.postParams = postParams;
}
/* ************************** End *************************************************/




/* ********************************************************************************
*
* Method: StartAjax
* Description: Get the XHR Object and set the callback function
*
* ********************************************************************************/
ajaxObject.prototype.startAjax = function(){
if(window.XMLHttpRequest)
  {
	this.xhrObj = new XMLHttpRequest();
	this.xhrObj.onreadystatechange = eval(this.callBack);
	
	if(this.method=='GET')
	  {
	    this.xhrObj.open(this.method,this.url+'&timestamp='+this.timestamp,true);
	    this.xhrObj.send(null);
	  }
	else
      {
		this.xhrObj.open(this.method,this.url,true);
	    this.xhrObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		this.xhrObj.setRequestHeader("Content-length", this.postParams.length);
		this.xhrObj.setRequestHeader("Connection", "close");
		this.xhrObj.send(this.postParams);
	  }  
  }
else if(window.ActiveXObject)
  {
    
	this.xhrObj = new ActiveXObject("Microsoft.XMLHTTP")
	this.xhrObj.onreadystatechange = eval(this.callBack);
	this.xhrObj.open(this.method,this.url+'&timestamp='+this.timestamp,true);
	this.xhrObj.send();
	
  }

}
/* ************************** End *************************************************/




/* ********************************************************************************
*
* Method: getFeed
* Description: Get an xml feed, display it as HTML using the parameters given
*
* ********************************************************************************/
ajaxObject.prototype.getFeed = function(){
var feedContainer = document.getElementById('nuhc_homeBlogPosts');
  if(ajaxHandle.xhrObj.readyState==4)
    {
	  if(ajaxHandle.xhrObj.status == 200)
      {		
		 var blogXml = ajaxHandle.xhrObj.responseXML;
		 var length = blogXml.getElementsByTagName(ajaxHandle.callBackParams[0]);
		 
		 for(var i=0;i<length.length;i++)
		   {
			 var title = length[i].getElementsByTagName(ajaxHandle.callBackParams[1]);
			 var link = length[i].getElementsByTagName(ajaxHandle.callBackParams[2]);
			 var description = length[i].getElementsByTagName(ajaxHandle.callBackParams[3]);
			 
   			 var container = document.createElement('div');
			 var heading = document.createElement('h2');
			 var headingLink = document.createElement('a');
             var descriptionTextContainer = document.createElement('p');
			 
			 var headingLinkText = document.createTextNode(title.item(0).firstChild.nodeValue);
			 
			 var cutDownText = description.item(0).firstChild.nodeValue.substring(0,100);
			 var limit;
			 for(var j=cutDownText.length;j>0;j--)
			  {
			    if(cutDownText[j]==" ")
				 {
				   limit = j;
				   break;
				 }
			  }
			  
			 cutDownText = cutDownText.substring(0,limit);
			 cutDownText += " ...";

			 
			 var descriptionText = document.createTextNode(cutDownText); 
			 
			 headingLink.setAttribute("href",link.item(0).firstChild.nodeValue);
			 headingLink.setAttribute('title','Read the "'+title.item(0).firstChild.nodeValue+'" post');
			 
			 var footerContainer = document.createElement('div');
			 footerContainer.setAttribute("class","nuhc_homeFooter");
			 
			 var footerContainerTextContainer = document.createElement('p');
			 footerContainerTextContainer.setAttribute("class","nuhc_readMore");
			 
			 var readMoreLink = document.createElement('a');
			 readMoreLink.setAttribute("href",link.item(0).firstChild.nodeValue);
			 readMoreLink.setAttribute('title','Read the "'+title.item(0).firstChild.nodeValue+'" post');
			 
			 var readMoreImage = document.createElement("img");
			 readMoreImage.setAttribute("src","_images/event-next.jpg");
			 readMoreImage.setAttribute("width","11");
			 readMoreImage.setAttribute("height","11");
			 readMoreImage.setAttribute("alt","read more button");
			 readMoreImage.setAttribute("border","0");
			 
			 var footerContainerLinkText = document.createTextNode("Read more");
			 
			 headingLink.appendChild(headingLinkText);
			 heading.appendChild(headingLink);
			 descriptionTextContainer.appendChild(descriptionText);
			 container.appendChild(heading);
			 container.appendChild(descriptionTextContainer);
			 readMoreLink.appendChild(footerContainerLinkText);
			 readMoreLink.appendChild(readMoreImage);
			 footerContainerTextContainer.appendChild(readMoreLink);
			 //footerContainerTextContainer.appendChild(readMoreImage);
			 footerContainer.appendChild(footerContainerTextContainer);			 
			 container.appendChild(footerContainer);
			 feedContainer.appendChild(container);
		   }
		}
    }
 }
/* ************************** End *************************************************/



/* ********************************************************************************
*
* Method: displayAll
* Description: display the entire response of an ajax call
*
* ********************************************************************************/
ajaxObject.prototype.displayAll = function(){

  if(ajaxHandle.xhrObj.readyState==4)
    {
	  if(ajaxHandle.xhrObj.status == 200)
      {
		document.getElementById(ajaxHandle.callBackParams[0]).innerHTML = ajaxHandle.xhrObj.responseText;
	  }
    }
 }

/* ************************** End *************************************************/


/* ********************************************************************************
*
* Method: nothing
* Description: dummy function
*
* ********************************************************************************/
ajaxObject.prototype.nothing = function(){
  if(ajaxHandleSecond.xhrObj.readyState==4)
    {
	  if(ajaxHandleSecond.xhrObj.status == 200)
      {
		//Do nothing...
	  }
    }
 }

/* ************************** End *************************************************/


