var req = null;
var req2 = null;

function aleatorio(inferior,superior){ 
	numPosibilidades = superior - inferior;
	aleat = Math.random() * numPosibilidades; 
	aleat = Math.round(aleat);
	return parseInt(inferior) + aleat;
} 


function parserURL(vText){
	vLink="";
	arrText=vText.split(" ");
	//alert(arrText.length);
	for(i=0;i<arrText.length;i++){
			//alert(arrText[i]);
			if(arrText[i].substr(0,7)=="http://"){
				arrText[i]="<a href='"+arrText[i]+"' target='_blank'>[enjoy-it]</a>";
			}
	}
	return arrText.join(" ");
}

function getXMLContent() {
  if (req.readyState == 4 && req.status == 200 && req.responseXML) {
    var dobj = document.getElementById('twitterlastmsg');
	//alert('ok');
	var nl = req.responseXML.getElementsByTagName('status');
	//dobj.innerHTML = nl.item(0).getElementsByTagName('text').item(0).text;
  	var vTxt = nl.item(0).getElementsByTagName('text').item(0).firstChild.nodeValue;
  	dobj.innerHTML = parserURL(vTxt);
  }
}

function getXMLContentPHP() {
  if (req2.readyState == 4 && req2.status == 200) {
  	//alert('ok');
    var dobj2 = document.getElementById('twitterlastmsgtime');
	//var nl = req.responseXML.getElementsByTagName('status');
	//dobj.innerHTML = nl.item(0).getElementsByTagName('text').item(0).text;
  	//var vTxt = nl.item(0).getElementsByTagName('text').item(0).firstChild.nodeValue;
  	//dobj2.innerHTML = req2.responseText;
  	dobj2.innerHTML = req2.responseText;
  }
}



function simpleLoadXMLDoc( url ) {

  if(window.XMLHttpRequest) {
    try { req = new XMLHttpRequest();
    } catch(e) { req = false; }
  } else if(window.ActiveXObject) {
    try { req = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
    try { req = new ActiveXObject('Microsoft.XMLHTTP');
    } catch(e) { req = false; }
  } }
  if(req) {
  	randomnumber=aleatorio(100,10000);
    req.onreadystatechange = getXMLContent;
    req.open('GET', '../includes/getXML.php?url='+url+'&koo='+randomnumber, true);
    req.send('');
  }
}


function simpleLoadResultPHP( url ) {
  if(window.XMLHttpRequest) {
    try { req2 = new XMLHttpRequest();
    } catch(e) { req2 = false; }
  } else if(window.ActiveXObject) {
    try { req2 = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
    try { req2 = new ActiveXObject('Microsoft.XMLHTTP');
    } catch(e) { req2 = false; }
  } }


  if(req2) {
  	randomnumber=aleatorio(100,10000);
    req2.onreadystatechange = getXMLContentPHP;
    req2.open('GET', '../includes/getXML.php?url='+url+'&result=ok&koo='+randomnumber, true);
    req2.send('');
  }
}








function init(){
	simpleLoadXMLDoc('http://twitter.com/statuses/user_timeline/joanjimenez.xml');
	simpleLoadResultPHP('http://twitter.com/statuses/user_timeline/joanjimenez.xml');
}

function go(){
	init();
	setInterval("init()",30000);
}