function sendAjaxRequest(url, eventhandler) {
	if (window.ActiveXObject) {
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		var xhr = new XMLHttpRequest();
	}

	xhr.onreadystatechange = function() {
	if(xhr.readyState == 4)
		if(xhr.status == 200) {
			eventhandler(xhr);
		} else {
			//document.body.innerHTML = xhr.responseText;
		}
	}

	xhr.open("POST", url, true );
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("");
}

function getAjaxTwits(url, url2, itemCount) {
	var twitter = document.getElementById('AjaxTwits');
	var twitterload = document.getElementById('AjaxTwitsLoader');
	
	sendAjaxRequest(url, function(ajaxRequest) {
		if (ajaxRequest.responseText) {
					
			var ans = eval('(' + ajaxRequest.responseText + ')');
			
			if(ans['@attributes']['update'] == true) {
				updateTwitter(url2);
			}
			
			if (ans.item) {
				twitter.removeChild(twitterload);
				
				var itemCount = ans.item.length;
				
				for (var i = 0; i < itemCount; i++) {
					var li = document.createElement("LI");
					li.className = "twitter-item";
					
						var profilePic = document.createElement("A");
						profilePic.href = "http://www.twitter.com/" + ans.item[i].user;
						
							var img = document.createElement("IMG");
							img.src = ans.item[i].avatar;
							img.className = "avatar";
						
						profilePic.appendChild(img);
						
					li.appendChild(profilePic);
					
					var message = hyperlinks(ans.item[i].description);
					message = twitter_users(" " + message);

						var username = document.createElement("A");
						username.href = ans.item[i].url;
						username.className = "twitter-link";
						username.innerHTML = ans.item[i].user;

					li.appendChild(username);
					
					li.innerHTML += message;
					
						var br = document.createElement("BR");
					
					li.appendChild(br);
						
						/*
						var a = document.createElement("A");
						a.href = ans.item[i].url;
						a.className = "twitter-link";
						a.innerHTML = ans.item[i].date;
						*/
						var span = document.createElement("SPAN");
						span.className = "posted";
						span.innerHTML = "Posted " + ans.item[i].date;
					
					li.appendChild(span);
					
					twitter.appendChild(li);
				}
			}
		}
	});	
}

function updateTwitter(url) {
	sendAjaxRequest(url, function(ajaxRequest) {
		if (ajaxRequest.responseText) {
			//console.log("twitter has been updated with the new xml");
		}
	});
}

function hyperlinks(text) {
	text = text.replace(/\s(http:\/\/[a-z][a-zA-Z0-9\/\*\-\?\.\&\%\$]*)/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>");
	text = text.replace(/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>$2");
	// match www.something.domain/path/file.extension?some=variable&another=asf%
	text = text.replace(/\s(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"http://$1\" class=\"twitter-link\">$1</a>$2");      
    
	return text;
}

function twitter_users(text) {
    text = text.replace(/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/ig, "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\">@$2</a>$3 ");
    return text;
}