//-------------------------------------------------------------
//	XML HTTP REQUEST
//-------------------------------------------------------------
//	This is where we set up our XMLHTTPREQUEST object and the
//	like..
//-------------------------------------------------------------

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(action) {
    http.open('get', 'requests.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function sndReqArg(action, value) {
    http.open('get', 'requests.php?action='+action+'&id='+value);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if (http.readyState == 4){
		var response = http.responseText;
        var update = new Array();

        if (response.indexOf('|' != -1)) {
            update = response.split('|');
			
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}

function readystate_ready_and_ok() {
	return (http.readyState == 4 && http.status == 200) ? true : false;
}

function readystate_not_ready() {
	return (this.xmlhandler.readyState && (this.xmlhandler.readyState < 4));
}

function process(url, type, post) {
	//------------------------------------------------
	// The 'post' variable needs to be in the following format:
	//
	// var=content&var2=content&var3=content
	//
	// All values need to be escaped with encodeURIComponent();
	//------------------------------------------------
	
	type = type == "POST" ? "POST" : "GET";
	
	//------------------------------------------------
	// Only go when ready
	//------------------------------------------------
	
	if (!readystate_not_ready() )
	{
		http.open(type, url, true);
		
		if ( type == "GET" ) {
			http.send(null);
			
		} else {
			if (typeof(http.setRequestHeader ) != "undefined") {
				http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			}
			
			http.send(post);
		}
		
		if (http.readyState == 4 && http.status == 200) {
			return true;
		}
	}
	
	return false;
}

/*function onreadystatechange( event ) {
	//------------------------------------------------
	// Make sure its a function event
	//------------------------------------------------
	
	if ( typeof(event) == 'function' )
	{
		http.onreadystatechange = event;
	}
}*/

var product_stats = [];
var product_on = 	"<img src='./gfx/misc/tickbox.png' alt='Enabled' />";
var product_off = 	"<img src='./gfx/misc/crossbox.png' alt='Disabled' />";

function product_status(this_id, status) {
	/*-------------------------------------------------
	// Toggles the status of one of our products
	//-----------------------------------------------*/
	
	if (!product_stats[this_id]) {
		// We haven't got an entry for this (yet..)
		product_stats[this_id] = status;
	} else {
		product_stats[this_id] = product_stats[this_id] == 'on' ? 'off' : 'on';
	}
	
	http.open('get', 'requests.php?type=product&id=' + this_id + '&status=' + product_stats[this_id]);
	//http.onreadystatechange = handleResponse;
	http.send(null);
	
	document.getElementById('product_stat_' + this_id).innerHTML = product_stats[this_id] == 'on' ? product_on : product_off;
}

var product_title_cache = [];

function product_edit_title(this_id) {
	/*-------------------------------------------------
	// Sets up to edit a title
	//-----------------------------------------------*/
	
	product_title_cache[this_id] = document.getElementById('product_title_' + this_id).innerHTML;
	
	var url = 'requests.php?type=product_start_edit&id= ' + this_id;
	
	request_function = function() {
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = http.responseText;
	
		if (html != 'error') {
			
			document.getElementById('product_title_' + this_id).innerHTML = html;
		}
	}
	
	http.onreadystatechange(request_function);
	
	process(url);
}
	
//-------------------------------------------------------------
// Set up our object
//-------------------------------------------------------------

function ajax() {
	
	this.xmlhttp = null;
	this.allowed = false;
	this.explorer = false;
}

//-------------------------------------------------------------
// Initialise...
//-------------------------------------------------------------

ajax.prototype.initialise = function() {
	
	try {
		//-------------------------------------------------------------
		// Gecko
		//-------------------------------------------------------------
		
		this.xmlhttp = new XMLHttpRequest();
		this.explorer = false;
		this.allowed = true;
		
		return true;
		
	} catch (e) {
		try {
			//-------------------------------------------------------------
			// Explorer
			//-------------------------------------------------------------
			
			this.xmlhttp = new ActiveXObject("Msxm12.XMLHTTP");
			this.explorer = true;
			this.allowed = true;
			
			return true;
			
		} catch (e) {
			try {
				//-------------------------------------------------------------
				// Explorer take-2
				//-------------------------------------------------------------
				
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				this.explorer = true;
				this.allowed = true;
				
				return true;
				
			} catch (e) {
				this.allowed = false;
				
				return false;
			}
		}
	}
}

//-------------------------------------------------------------
// Process...
//-------------------------------------------------------------

ajax.prototype.process = function (value) {
	
	value = escape(value);
	
	var uri = 'back.php';
	
	//-------------------------------------------------------------
	// Check for the object...
	//-------------------------------------------------------------
	
	if (!this.xmlhttp) {
		this.initialise();
	}
	
	//-------------------------------------------------------------
	// Go ahead
	//-------------------------------------------------------------
	
	this.xmlhttp.open("GET", uri + "?test=" + value);
	this.xmlhttp.onreadystatechange = this.respond();
	this.xmlhttp.send(null);
}

//-------------------------------------------------------------
// Respond...
//-------------------------------------------------------------

ajax.prototype.respond = function() {
	alert(this.xmlhttp.readyState);
	if (this.xmlhttp.readyState == 4) {
		
		var reponse = this.xmlhttp.responseText;
		var update = new array();
		
		if (response.indexOf('|' != -1)) {
			update = response.split('|');
			document.getElementById(update[0]).innerHTML = update[1];
		}
	}
}

/*
ajax.prototype.update(type, uri, value) {
	
	value = escape(value);
	
	//-------------------------------------------------------------
	// Check for the object...
	//-------------------------------------------------------------
	
	if (!this.xmlhttp) {
		this.initialise();
	}
	
	this.xmlhttp.open(type, 
*/