function ajaxObject(id) {

	this.id = id;
	window["ajaxObject"+id] = this;
	
	ajaxObject.prototype.ua = navigator.userAgent.toLowerCase();
	if ((this.ua.indexOf("msie") != -1) && (this.ua.indexOf("opera") == -1) && (this.ua.indexOf("webtv") == -1)) this.isIE = true; 
}

ajaxObject.prototype.id = '';
ajaxObject.prototype.isIE = false;
ajaxObject.prototype.ua = '';

ajaxObject.prototype.http_request = false;
ajaxObject.prototype.js_method = 'updateObjects';
ajaxObject.prototype.object_count = 0;

ajaxObject.prototype.script_url = '';
ajaxObject.prototype.script_params = new Array();

ajaxObject.prototype.target_div = false;
ajaxObject.prototype.numnavi = false;
ajaxObject.prototype.navistr = false;
ajaxObject.prototype.showallnavi = false; // careers only hack - needs tidying up
ajaxObject.prototype.loadindicator = false;

ajaxObject.prototype.loadindicator_image = new Array('/media/indicator.gif','/media/blank.gif');

ajaxObject.prototype.snum_adjust = 1;
ajaxObject.prototype.snum_label = "snum";
ajaxObject.prototype.viewnum = "0";

ajaxObject.prototype.getXMLHttpObject = function() {

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		this.http_request = new XMLHttpRequest();
		if (this.http_request.overrideMimeType) {
			this.http_request.overrideMimeType('application/xml');
		}
		
		return true;
		
	} else if (window.ActiveXObject) { // IE
		try {
			this.http_request = new ActiveXObject("Msxml2.XMLHTTP");

			return true;

		} catch (e) {
			try {
				this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				
				return true;

			} catch (e) { this.http_request = false; }
		}
	}

}

ajaxObject.prototype.init = function() {
	
	if (this.getXMLHttpObject) {
		
		if (typeof this.js_method === 'function') {
// alert(this.js_method);
			this.addOnClickHandlers();
		}
		else { alert(typeof this.js_method); }
	}
}


ajaxObject.prototype.setScriptParam = function(key,param) {
	this.script_params[this.script_params.length] = new Array(key,param);
}

ajaxObject.prototype.getValue = function(tagname,data) {
	return this.getTextFromXMLNode(data.getElementsByTagName(tagname)[0]);
}

ajaxObject.prototype.setScriptParams = function(params) {
	this.script_params = params;
}

ajaxObject.prototype.setScriptParamForKey = function(key,param) {
	
	var index = -1;
	
	for (var i=0;i<this.script_params.length;i++) {
		
		if (this.script_params[i][0] == key) {
			index = i;
		}
	}

	if (index > -1) {
		this.script_params[index][1] = param;
	}
	else {
		this.setScriptParam(key,param);
	}
}

ajaxObject.prototype.scriptParamForKey = function(key) {
	
	var index = -1;
	
	for (var i=0;i<this.script_params.length;i++) {
		
		if (this.script_params[i][0] == key) {
			index = i;
		}
	}

	if (index > -1) {
		return this.script_params[index][1];
	}
	else {
		return '';
	}
}

ajaxObject.prototype.setScriptUrl = function(url) {
	this.script_url = url;
}

ajaxObject.prototype.buildScriptUrl = function(params) {
	var url = this.script_url;
	var c = params.length;
	
	if (c > 0) {
		
		url += "?" + params[0][0] + "=" + params[0][1];
		
		for (var i=1;i<c;i++) {
			url += "&" + params[i][0] + "=" + params[i][1];
		}
	}

	return url;
}

ajaxObject.prototype.makeXMLRequest = function(url,input) {
	if (!this.getXMLHttpObject()) {
		// force normal browser reload
		return true;
	}

	if (this.loadindicator) {
		for (var i=0; i<this.loadindicator.length; i++) {
			this.loadindicator[i].src = this.loadindicator_image[0];
		}
	}
	// alert(this.js_method);
	//this.js_method = func;
	tmp_this = this;

	this.http_request.onreadystatechange = function () { return tmp_this.doUpdate(input); }

	this.http_request.open('GET', url, true);
	this.http_request.send(null);
	
	return false; // stop href from being loaded

}

ajaxObject.prototype.doUpdate = function(input) {

	try {
		if (this.http_request.readyState == 4) {

			if (this.http_request.status == 200) {
				
				if (this.loadindicator) {
					for (var i=0; i<this.loadindicator.length; i++) {
						this.loadindicator[i].src = this.loadindicator_image[1];
					}
				}
				
				if (this.updateObjects(this.http_request.responseXML)) {
					return false;
				}
				else {
					if (input) {
						window.location = input.href;
					}						
					return true;
				}

			
			} else {
				for (var i=0; i<this.loadindicator.length; i++) {
					this.loadindicator[i].src = this.loadindicator_image[1];
				}
				
				if (input) {
					window.location = input.href;
				}
				return true;
			}
		}
	}
	catch(e) {
		// alert('Caught Exception: ' + e.description);
		if (this.loadindicator) {
			for (var i=0; i<this.loadindicator.length; i++) {
				this.loadindicator[i].src = this.loadindicator_image[1];
			}
		}
		if (input) {
			window.location = input.href;
		}
		
		return true;
	}
}

ajaxObject.prototype.getTextFromXMLNode = function(node) {

	if (this.isIE) {
		return node.text;
	}
	else {
		if (node.hasChildNodes()) {
			return node.childNodes[0].nodeValue;
		}
		else {
			return "";
		}
	}
}

ajaxObject.prototype.addOnClickHandlers = function() {
    
	if (this.numnavi) {

		var regExp = new RegExp('\\b' + this.snum_label + '=([0-9]{1,})\\b');
		
		// can't copy the params array as the elements will be by reference
		var params = new Array(); 

		for (var i=0;i<this.script_params.length;i++) {
			params[i] = this.script_params[i];
		}

		tmp_this = this;

		for (var j=0; j<this.numnavi.length; j++) {

			for (var i = 0;i < this.numnavi[j].childNodes.length; i++) {
	
				if (this.numnavi[j].childNodes[i].tagName == 'a' || this.numnavi[j].childNodes[i].tagName == 'A') {

					this.numnavi[j].childNodes[i].onclick = function () { 

						var result = this.href.match(regExp);
				
						if (result != null) {
							var tmp_result = result[1];
							if (tmp_this.snum_adjust != 0) {
								tmp_result -= tmp_this.snum_adjust;
							}
							
							params[params.length] = new Array(tmp_this.snum_label,tmp_result);
							
							return tmp_this.makeXMLRequest(tmp_this.buildScriptUrl(params),this); // MUST return the value to stop the href - otherwise js objects will have gone by the time the http_request is ready
						}
						else {
							return true;
						}
					}
				}
			}
		}
	}
	
	// careers job search
    
	if (this.showallnavi) {
	
		// can't copy the params array as the elements will be by reference
		var params2 = new Array(); 

		for (var i=0;i<this.script_params.length;i++) {
			if (this.script_params[i][0] != 'show_all') {
				params2[i] = this.script_params[i];
			}
		}

		tmp_this = this;

		for (var i = 0;i < this.showallnavi.childNodes.length; i++) {
			
			if (this.showallnavi.childNodes[i].tagName == 'a' || this.showallnavi.childNodes[i].tagName == 'A') {
				
				this.showallnavi.childNodes[i].onclick = function () { 
					
					this.style.textDecoration = "underline";
					
					if (this.id == 'jobnavishowall') {
						params2[params2.length] = new Array('show_all',1);
						document.getElementById('jobnavihideall').style.textDecoration = "none";
						
					}
					else {
						params2[params2.length] = new Array('show_all',0);
						document.getElementById('jobnavishowall').style.textDecoration = "none";
					}
					
					this.blur();

					return tmp_this.makeXMLRequest(tmp_this.buildScriptUrl(params2),this); // MUST return the value to stop the href - otherwise js objects will have gone by the time the http_request is ready
				}
			}
		}
	}
}

ajaxObject.prototype.doSearch = function() {
	alert('dosearch');
}
// default js method

ajaxObject.prototype.updateObjects = function (responseXML) {

	var objectarr = responseXML.getElementsByTagName('object');
	
	var tmpstr = "";
	
	this.object_count = objectarr.length;
	
	if (this.object_count) {
	
		for (var i=0;i<objectarr.length;i++) {
			tmpstr += this.js_method(i,objectarr[i]);
		}
	}
	else tmpstr += "No results were found.";
	
    if (this.target_div) {
   		for (var i=0; i<this.target_div.length; i++) {
 			this.target_div[i].innerHTML = tmpstr;
		}
	}

    if (this.navistr) {
   		for (var i=0; i<this.navistr.length; i++) {
 			this.navistr[i].innerHTML = this.getTextFromXMLNode(responseXML.getElementsByTagName('navistr')[0]);
    	}
    }

    if (this.numnavi) {
    	for (var i=0; i<this.numnavi.length; i++) {
			this.numnavi[i].innerHTML = this.getTextFromXMLNode(responseXML.getElementsByTagName('numnavi')[0]);
		}
	}
	this.addOnClickHandlers();
	
	return true;

}