/*
	Article count incrementor 
	===============================
	Written by Jason Salas, MBA, MCP
	On February 25, 2007
	http://www.jasonsalas.com
	jason@kuam.com
	
	*************
	This JavaScript manages the asynchronous processes that keep track of the number of times news stories are accessed on KUAM.com.
	*************
*/


/*
	Standard KUAM.com AJAX library
*/

var method = 'POST';
//var path = '/kuam/internaltools/storyincrementor/storyIncrementor.aspx';
var path = '/bookreviews/storyIncrementor.aspx';
var xmlhttp;

window.onload = incrementStoryCount;

function $(elem) {
	return document.getElementById(elem);
}

function createXMLHttpRequest() {
	if(typeof XMLHttpRequest != 'undefined') {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		var vers = [ 'MSXML2.XMLHttp.5.0','MSXML2.XMLHttp.4.0','MSXML2.XMLHttp.3.0','MSXML2.XMLHttp','Microsoft.XMLHTTP' ];
		
		for(var i=0;i<vers.length;i++) {
			try {
				return new ActiveXObject(vers[i]);
			} catch(e) {
				// do nothing 
			}
		}
		return null;
	}
}

function callRemoteScript() {
	xhr = createXMLHTTPRequest();
	if(xhr) {
		xhr.open(method,path,true);
		xhr.onreadystatechange = handleRemoteResponse;
		xhr.send(null);
	}		
}

function incrementStoryCount() {
	xmlhttp = createXMLHttpRequest();
	if(xmlhttp) {
		xmlhttp.open(method,path,true);
		xmlhttp.onreadystatechange = handleResponse;
		xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		xmlhttp.setRequestHeader('Content-length',document.URL.length);
		xmlhttp.send('storyID=' + encodeURIComponent(document.URL));		
	}
}

function handleResponse() {
	var resultDIV = $('results');
	if(resultDIV) {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				resultDIV.innerHTML = xmlhttp.responseText;
			}
		}
	}
}
