// Ajax Object
// Written by Alexandr Gumbar on 08-29-2008

/*Last Modified:
09-03-2008 - Added fix for captureData function that captures the correct value of the checkbox and group of the radiobuttons
09-11-2008 - Added feature of Popup box (where the respond can be displayed - reqires fade.js) if the captureData function has a value passed into it.
10-23-2008 - Fixed bug with passing URL query in the link

*/

// OBJECT Hijax
function Hijax() {
	//Declaring variables
 var container,url,canvas,data,loading,callback,request, popup;
 	//container - is the element on the page that contains the trigger mechanism for the Ajax request: form or links...
 	// url - path to the file
	// canvas - part of the page that gets updated
	// data  - null or string of name/value pairs to be sent to the server
	// loading - function to be triggered when the Ajax request begins
	// callback - fuction to be triggered when the Ajax request ends
	// request - an instance of XMLHttpRequest
	// popup - show popup or not (if you pass any value to the capturedata function the popup will appear)
	
//Creating methods
 this.setContainer = function(value) {
  container = value;
 };
 this.setUrl = function(value) {
  url = value;
 };
 this.setCanvas = function(value) {
  canvas = value;
 };
 this.setLoading = function(value) {
  loading = value;
 };
 this.setCallback = function(value) {
  callback = value;
 };
 
//Method that captures data from the form / links and disables browser sumbit/link and gives control to the JavaScript 
 this.captureData = function(value) {
	 popup = value;
  if (container.nodeName.toLowerCase() == "form") {
   container.onsubmit = function() {
    var query = "";
    for (var i=0; i<this.elements.length; i++) {
			//Makes sure that the correct value of the checkbox get send
			if (this.elements[i].type == 'checkbox' && this.elements[i].checked == true)
			//Send value=true for checked checkboxes
			{this.elements[i].value = true;}
			if (this.elements[i].type == 'checkbox' && this.elements[i].checked == false)
			//Send value=false for unchecked checkboxes
			{this.elements[i].value = false;}
			//Makes sure that the correct and only one value of the radiobutton group get send
			if (this.elements[i].type == 'radio' && this.elements[i].checked == false)
			//for not checked radiobuttons send nothing
			{query+= "";}
	else {		
     query+= this.elements[i].name;
     query+= "=";
     query+= escape(this.elements[i].value);
     query+= "&";
		}
    }
    data = query;
	//MODIFICATION FOR THIS PARTICULAR OBJECT 
	if (popup && popup!="none")
	{$('#'+popup).fadeIn();}
	else{/*do nothing*/}
    return !start();
   };
  } else {
   var links = container.getElementsByTagName("a");
   for (var i=0; i<links.length; i++) {
    links[i].onclick = function() {
			var file = this.getAttribute("href").split("?")[0]
			var query = this.getAttribute("href").split("?")[1];
			url =file+"?"+query;
			
			/*url += "?"+query;*/
			
	//MODIFICATION FOR THIS PARTICULAR OBJECT 
	if (popup && popup!="none")
	{$('#'+popup).fadeIn();}
	else{/*do nothing*/}
     return !start();
    };

   }
   links = null;
   
  }
 };
 
 

//Initialing the Ajax request
 var start = function() {
  request = getHTTPObject();
  if (!request || !url) {
   return false;
  } else {
   initiateRequest();
   return true;
  }
 };
 
 	//Main method, which checks the browser ajax capability 
 var getHTTPObject = function() {
  var xmlhttp = false;
  if (window.XMLHttpRequest) {
   xmlhttp = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
     xmlhttp = false;
    }
   }
  }
  return xmlhttp;
 };

//This method is the engine that drives the object
 var initiateRequest = function() {
  if (loading) {
   loading();
  }
  request.onreadystatechange = completeRequest;
  if (data) {
   request.open("POST", url, true);
   request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   request.send(data);
  } else {
   request.open("GET", url, true);
   request.send(null);
  }
 };

	//This method is attached to the "onreadystatechange" event handler, to see if the readyState property has reached 4 and if the server is sending a status of 200 or 304: (everything went well).
 var completeRequest = function() {
  if (request.readyState == 4) {
   if (request.status == 200 || request.status == 304) {
	   // If a canvas property of the current Hijax object has been specified that part of the page is updates with the contents of responseText;
    if (canvas) {
     canvas.innerHTML = request.responseText;
    }
    if (callback) {
     callback();
    }
   }
  }
 };

}
//END OF THE OBJECT

