/**
 * AJAX object
 * 
 */
ajaxObject = {
	ajax_compat : true,
	IE			: true,
	error		: [],
	errors		: ['no error', 'valid', 'Not AJAX capable', 'No queue items to process'],
	queue		: [],
	xmlHttpReq	: false,
	ajax_method : 'GET',
	retFunction	: null,
	init		: function() {
					this.IE = (navigator.appName == "Microsoft Internet Explorer") ? true : false ;
					try {
						xmlHttp = new XMLHttpRequest();
					} catch (e) {
						try {
							xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
						} catch (e) {
							try {
								xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
							} catch (e) {
								this.ajax_compat = false
							}
						}
					}
				},
	process		: function() {
					if (this.ajax_compat == false) {
						this.setError(2, this.errors[2]); return false;
					} else if (this.queue.length == 0) {
						this.setError(3, this.errors[3]); return false;
					} else {
						queue_item = this.queue.shift()
						this.retFunction = queue_item[1]
						if (this.ajax_method == 'GET') {
							file = queue_item[0] + '&dummy=' + new Date().getTime()
							if (this.IE) {
								this.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
							} else {
								this.xmlHttpReq = new XMLHttpRequest();
							}
							this.xmlHttpReq.open('GET', file, true);
						} else {
							if (this.IE) {
								this.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
							} else {
								this.xmlHttpReq = new XMLHttpRequest();
							}
							this.xmlHttpReq.open('POST', queue_item[0] + '?dummy=' + new Date().getTime(), true);
							var params = queue_item[2];
							
							//Send the proper header information along with the request
							this.xmlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
							//this.xmlHttpReq.setRequestHeader("Content-length", params.length);
							//this.xmlHttpReq.setRequestHeader("Connection", "close");
						}
						this.xmlHttpReq.onreadystatechange = function() {
							if (ajaxObject.xmlHttpReq.readyState == 4) {
								if (ajaxObject.retFunction != '') {
									eval(ajaxObject.retFunction + '(\"' + escape(ajaxObject.xmlHttpReq.responseText) + '\")')
								}
								if (ajaxObject.xmlHttpReq.status == 200) {
									if (ajaxObject.queue.length == 0) {
										ajaxObject.setError(1, ajaxObject.errors[1]);
										document.getElementById('loading').style.display = 'none'
										return true
									} else {
										ajaxObject.process()
									}
								} else if (ajaxObject.xmlHttpReq.status != 0) {
									ajaxObject.setError(4, file + ' - ' + ajaxObject.xmlHttpReq.statusText)
									return false
								}
							}
						}
						if (this.ajax_method == 'GET') {
							this.xmlHttpReq.send(null);
						} else {
							this.xmlHttpReq.send(params);
						}
					}
				},
	setError	: function(errNum, errText) {
					while (this.error.length > 0) { discard = this.error.shift(); }
					this.error.push([errNum, errText])
				},
	getErrorNo	: function() { return this.error[0][0] },
	getErrorText: function() { return this.error[0][1] }
};

ajaxObject.init()

function toggle_input(obj, method, text)
{
	 if (method == 'focus') {
	 	if (obj.value == text) { obj.value = ''; }
	 	obj.style.color='black';
	 } else if (method == 'blur') {
	 	if (obj.value == '') { obj.value = text; }
	 	obj.style.color = 'gray';
	 }
}

function key_press_test(e)
	{
		var keynum;
		
		if (window.event) {
		  keynum = e.keyCode;
		} else if(e.which) {
		  keynum = e.which;
		}
		if (keynum == 13) {
			getPrices()
		}
	}

var sites = new Array
sites[0] = 'envirofone'
sites[1] = 'mazuma'
sites[2] = 'mobilephonexchange'
sites[3] = 'o2'

function getPrices()
{
	document.getElementById('results_article').style.display = 'none'
	document.getElementById('results').innerHTML = "<tr><th>Recycler</th><th>Product</th><th class='price_column'>Price</th><th>&#160;</th></tr>"
	document.getElementById('loading').style.display = 'block'
	
	for (s in sites) {
		ajaxObject.queue.push(['prices.php?site=' + escape(sites[s]) + '&make=' + escape(document.getElementById('make').value) + '&model=' + escape(document.getElementById('model').value), 'getPricesResponse'])
	}
	ajaxObject.process()
}
function getPricesResponse(response)
{
	document.getElementById('results_article').style.display = 'block'
	if (unescape(response) != '' && response.indexOf('Error') == -1) {
		response = eval('(' + unescape(response) + ')')
		if (response.length > 0) {
			table = document.getElementById('results')
			for (x = 0; x < response.length; x++) {
				table.innerHTML += "<tr><td>" + response[x].company + "</td><td>" + response[x].make + " " + response[x].model + "</td><td>&#163;" + response[x].price + "</td><td><span class=\"art-button-wrapper\"><span class=\"l\"> </span><span class=\"r\"> </span><a class=\"art-button\" href=\"go.php?src=" + response[x].link + "\" onclick=\"popup(this.href); return false\">Go &raquo;</a></span></td></tr>"
			}
		}
	} else {
		table.innerHTML += "<tr><td colspan='4'>" + response + "</td></tr>"
	}
}

function popup(href)
{
	window.open(href, 'win' + Math.floor(Math.random()*1000), 'height=600, width=1000, scrollbars=1, resizeable=1')
}
