// AJAX CLASS
// COPYRIGHT D/SITE 2006

// main ajax class
function ajax() {
	
	// variables
	this.http_request 		= false;
	this.arrError			= new Array();
	this.arrParams			= new Array();
	this.arrEvent			= new Array();
	this.arrContent			= new Array();
	this.xml				= "";
	this.xmlError			= false;
	this.xmlEvent			= false;
	this.xmlContent			= false;
	
	
	// initializing
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	this.http_request = new XMLHttpRequest();
     	if (this.http_request.overrideMimeType) {
        	this.http_request.overrideMimeType('text/xml');
     	}
  	} else if (window.ActiveXObject) { // IE
     	try {
        	this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
     	} catch (e) {
        	try {
           		this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch (e) {}
     	}
  	}
  	
  	if (!this.http_request) {
     	this.arrError[this.arrError.length] = "Cannot create XMLHTTP instance";
  	}
  	
	// Post 
	this.post = function(treeId) {
		
		// making parameter string
		var tmpParam = "";
		
		for(key in this.arrParams) {
			tmpParam += key + "=" + this.arrParams[key] + "&";
		}
		
		// removing last &
		tmpParam = tmpParam.substr(0,(tmpParam.length - 1));
		
		this.http_request.open('POST', treeId, true);
      	this.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	
      	this.http_request.send(tmpParam);
      
	}
	
	// Post 
	this.get = function(treeId) {
		
		this.http_request.open('GET', treeId, true);
      	this.http_request.send(null);
      
	}
	
   	// adding parameter to post
   	this.addParam = function(name,val) {
   		
   		// adding param to array
   		this.arrParams[name] = escape(val);
   		
   	}
   	
   	// getting form
   	this.addForm = function(formName) {
   		
   		// getting form object
   		var objForm = document[formName];
   		
   		// looping through form elements
   		for(var i = 0;i < objForm.elements.length;i++) {
			
   			switch(objForm.elements[i].type) {
   				
   				case "text":
   				
   					this.addParam(objForm.elements[i].name,objForm.elements[i].value);
   				
   					break;
   				
   				case "hidden":
   				
   					this.addParam(objForm.elements[i].name,objForm.elements[i].value);
   				
   					break;
   					
   				case "password":
   				
   					this.addParam(objForm.elements[i].name,objForm.elements[i].value);
   				
   					break;
   					
   				case "textarea":
   				
   					this.addParam(objForm.elements[i].name,objForm.elements[i].value);
   				
   					break;
   					
   				case "checkbox":
   				
   					if(objForm.elements[i].checked) {
   				
   						this.addParam(objForm.elements[i].name,objForm.elements[i].value);
   				
   					}
   					break;
   					
   				case "select-one":
   					
   					this.addParam(objForm.elements[i].name,objForm.elements[i].options[objForm.elements[i].selectedIndex].value);
   					
   					break;
   			}
   			
   		}
   		
   	}
   	
   	// object reference for later use
   	var objRef = this;
   	
   	this.result = function () {
   		
   		if (objRef.http_request.readyState == 4) {
			
			if (objRef.http_request.status == 200) {
				
				objRef.xml = objRef.http_request.responseXML.documentElement;
				//alert(objRef.http_request.responseText);
				objRef.xml.normalize();
				
				// parsing XML
				objRef.parse();
				
				// executing XML
				objRef.execute();
				
			} else {
				objRef.arrError[objRef.arrError.length] = "There was a problem with the request";
			}
		}
	}
	
	this.http_request.onreadystatechange = this.result;
	
	// parsing the returndata
	this.parse = function () {
		
		// setting action
		this.xmlEvent = this.xml.getElementsByTagName('event');
		
		// setting content
		this.xmlContent = this.xml.getElementsByTagName('content');
	}
	
	// parsing the returndata
	this.execute = function () {
		
		this.getEvents();
		
		this.getContents();
		
		// Events
		this.runEvents();
		
		// content
		this.displayContent();
		
	}
	
	
	// setting errors
	this.getEvents = function () {
		
		// looping errors
		for(var i = 0; i < this.xmlEvent.length; i++) {
			
			// adding errors in array for later printout
			this.arrEvent[this.arrEvent.length] = this.xmlEvent[i].firstChild.nodeValue;
			
		}
	}
	
	// setting errors
	this.getContents = function () {
	
		// looping errors
		for(var i = 0; i < this.xmlContent.length; i++) {
			
			var tmpId = "";
			// default
			var tmpType = "add";
			var tmpValue = "";
			
			// getting id
			if(this.xmlContent[i].getAttribute('id')) {
				tmpId = this.xmlContent[i].getAttribute('id');
			}
			
			// getting type
			if(this.xmlContent[i].getAttribute('type')) {
				tmpType = this.xmlContent[i].getAttribute('type');
			}
			
			// getting value
			if(this.xmlContent[i].firstChild) {
				tmpValue = unescape(this.xmlContent[i].firstChild.nodeValue);
				
			}
			
			this.arrContent[this.arrContent.length] = new Array(tmpId,tmpType,tmpValue);
			
		}
	}
	
	// displaying errors
	this.displayContent = function () {
		
		// looping errors
		for(var i=0;i<this.arrContent.length;i++) {
			
			// making tmp reference to HTML object
			var tmpObj = document.getElementById(this.arrContent[i][0]);
			
			// only print if object exists
			if(tmpObj) {
				
				// should we add or set
				if(this.arrContent[i][1] == "set") {
					tmpObj.innerHTML = this.arrContent[i][2];
				} else if(this.arrContent[i][1] == "add") {
					tmpObj.innerHTML += this.arrContent[i][2];
				}
			}
			
		}
	}
	
	// running events
	this.runEvents = function () {
		
		// looping errors
		for(var i=0;i<this.arrEvent.length;i++) {
			
			eval(this.arrEvent[i]);
			
		}
	}
	
	return this;
}

function reload(val) {
	
	if(typeof(val) == "undefined") {

		window.location.reload();
	
	} else {
		
		document.location.href = "/" + val + "/";
	}
	
}
