var newip = "";
var tvwidth = "";
var t;
var polling = false;

function setBounds() {

	var box1 = document.getElementById('tv1');
	var tvwidth = box1.offsetWidth;
	document.getElementById('tv1overlay').style.width = tvwidth - 2;
	document.getElementById('tv2overlay').style.width = tvwidth - 2;
}

function parseResult(response) {
   var td = document.getElementById('log');
   td.innerHTML = response;
}

// does the polling for log and new url
function poll(id) {
   var dataSource = 'poll.php?id=' + id;
	queryHandler(id,dataSource,"poll");

	// restart poll
	t = setTimeout('poll('+id+')',1000);
}

// give status up
function up(ip,id) {
	// if(newip != "") ip = newip;
	var dataSource = 'poll.php?ip=' + ip + '&id=' + id + '&status=up';
	queryHandler(id,dataSource,"");
}

// give status down
function down(ip,id) {
	// if(newip != "") ip = newip;
	var dataSource = 'poll.php?ip=' + ip + '&id=' + id + '&status=down';
	queryHandler(id,dataSource,"");
}

// ajax request
function queryHandler(id,dataSource,what) {

   var XMLHttpRequestObject = false;

   if(window.XMLHttpRequest && !(window.ActiveXObject)) {
      XMLHttpRequestObject = new XMLHttpRequest();
      XMLHttpRequestObject.overrideMimeType('text/xml');
   } else if(window.ActiveXObject) {
      XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
   }

   if(XMLHttpRequestObject) {
      XMLHttpRequestObject.open('GET',dataSource,true);
      XMLHttpRequestObject.onreadystatechange = function() {
         if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				if(what == "poll") {
					var match = XMLHttpRequestObject.responseText.match(/Found port 80 on ([\d|\.]+?) while going ([^\s]+?), the ip resolved to ([^<]+?) <br\/>[^a-zA-Z]*$/);
					if(match) {
						if(newip != match[1]) {
							newip = match[1];
							var host = match[3];
							if(match[2] == "up") {
								document.getElementById('ip3').innerHTML = newip;
								document.getElementById('tv3').src = "http://"+newip;
								document.getElementById('host3').innerHTML = host;
								clearTimeout(t);
							}
							if(match[2] == "down") {
								document.getElementById('ip1').innerHTML = newip;
								document.getElementById('tv1').src = "http://"+newip;
								document.getElementById('host1').innerHTML = host;
								clearTimeout(t);
							}
							polling = false;
						}
					}
					parseResult(XMLHttpRequestObject.responseText); // gets back log data

				}
         }
      };
      XMLHttpRequestObject.send(null);
   }
}
function emptyForm() {
	document.getElementById('input').value = '';
}
function fillForm() {
	document.getElementById('input').value = 'Enter an IP or URL here';
}
function shift(where,id) {
	if(polling) return;
	polling = true;
	clearTimeout(t);
	poll(document.getElementById('pollId').childNodes[0].nodeValue);
	var tv1 = document.getElementById('tv1');
	var tv2 = document.getElementById('tv2');
	var tv3 = document.getElementById('tv3');
	var ip1 = document.getElementById('ip1');
	var ip2 = document.getElementById('ip2');
	var ip3 = document.getElementById('ip3');
	var host1 = document.getElementById('host1');
	var host2 = document.getElementById('host2');
	var host3 = document.getElementById('host3');
	var ip = "";
	if(where == 'left') {
		if(tv2.src != '') {
			tv1.src = tv2.src;
			tv2.src = tv3.src;
			tv3.src = '';

			ip = ip3.innerHTML;
			ip1.innerHTML = ip2.innerHTML;
			ip2.innerHTML = ip3.innerHTML;
			ip3.innerHTML = '<img src="images/loading.gif">';

			host1.innerHTML = host2.innerHTML;
			host2.innerHTML = host3.innerHTML;
			host3.innerHTML = '<img src="images/loading.gif">';

			up(ip,id);
		}
	} else if(where == 'right') {
		if(tv2.src != '') {
			tv3.src = tv2.src;
			tv2.src = tv1.src;
			tv1.src = '';

			ip = ip1.innerHTML;
			ip3.innerHTML = ip2.innerHTML;
			ip2.innerHTML = ip1.innerHTML;
			ip1.innerHTML = '<img src="images/loading.gif">';

			host3.innerHTML = host2.innerHTML;
			host2.innerHTML = host1.innerHTML;
			host1.innerHTML = '<img src="images/loading.gif">';

			down(ip,id);
		}
	}
}
