var httpReq = {'default': null};

function sendRequest(page, queryString, postData, callback, channel, stopOnOverride) {
	if(!page)
    	page = 'view_controller.php';
	if(!channel)
		channel = 'default';
	
	if(stopOnOverride && httpReq[channel] && httpReq[channel].readyState) {
		return;
	} else if(httpReq[channel]) {
		httpReq[channel].abort();
	} else {
		httpReq[channel] = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
	}
    
    if(httpReq[channel]) {
        httpReq[channel].onreadystatechange = function() {processRequest(channel, callback);};		
		// setup to handle GET or a POST, based on the data
		httpReq[channel].open((postData == null ? "GET" : "POST"), page+'?'+queryString+'&ajax=yes', true);
		if(postData && postData.length > 0) {			
			httpReq[channel].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
		}
		
		httpReq[channel].send(postData);
    }
}

function processRequest(channel, callback) {
	if(httpReq[channel] && httpReq[channel].readyState == 4) {
		if(httpReq[channel].status == 200) {
			eval(callback);
		} else if(httpReq[channel].status != 0 && typeof console != 'undefined' && console.log) {
			console.log('HTTP error: '+httpReq[channel].status);
			console.log(httpReq[channel].responseText);
		}
	}
}

function processSlip(slip, skipShowSlip, slideTo, channel) {
	if(!slip)
		slip = 'detail';
	if(!channel)
		channel = 'default';

	if(httpReq[channel].responseText.indexOf('--skipInnerHtml-->') == -1) {
		if(slideTo && httpReq[channel].responseText.indexOf('--skipSlide-->') == -1) {
			slideSlip(slip, slideTo, httpReq[channel].responseText);
		} else {
			var el = document.getElementById(slip+'SlipContent');
			
			el.innerHTML = httpReq[channel].responseText.replace(/\<script.+?\<\/script\>/g, '');
			
			if(skipShowSlip) {
				var theSlip = $(slip+'SlipContainer');
				setSlipScrollers(theSlip, 'auto');
			}
		}
	}
	
	if(!skipShowSlip) {
		showSlip(slip);
	}
}
