<?php
if (isset($_GET["tipo"])) $tipo=$_GET["tipo"];
else $tipo="192.168.22";
$inicial=1;
$final=254;
$cant=$final-$inicial+1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>VN Phone Scanner VoiceNET</title>
<script language="javascript">
var fondoIP="#E5E5E5";
function confExt(ip)
	{
	ext=document.getElementById("confext_"+ip).value;
	var xhttp = new XMLHttpRequest();
	//xhttp.timeout = 3000;
	xhttp.onreadystatechange = function() {
		if (this.readyState == 4 && this.status == 200) {
		document.getElementById("ext_"+ip).innerHTML=ext;
		document.getElementById("confext_"+ip).value="";
		}
	};
	xhttp.open("GET", "confext.php?ip="+ip+"&ext="+ext, true);
	xhttp.send();
	}
function updFW(ip)
	{
	document.getElementById("botupd_"+ip).style.display="none";
	document.getElementById("loading_"+ip).style.display="block";
	var xhttp = new XMLHttpRequest();
	//xhttp.timeout = 3000;
	xhttp.onreadystatechange = function() {
		if (this.readyState == 4 && this.status == 200) {
		document.getElementById("nuevo_"+ip).innerHTML="<div align='center'>ACTUALIZADO</div>";
		}
	};
	xhttp.open("GET", "fwupg.php?ip="+ip, true);
	xhttp.send();
	}
function updFW2(ip)
	{
	//document.getElementById("botupd_"+ip).style.display="none";
	//document.getElementById("loading_"+ip).style.display="block";
	var xhttp = new XMLHttpRequest();
	//xhttp.timeout = 3000;
	xhttp.onreadystatechange = function() {
		if (this.readyState == 4 && this.status == 200) {
		document.getElementById("nuevo_"+ip).innerHTML="<div align='center'>ACTUALIZADO</div>";
		}
	};
	xhttp.open("GET", "fwupg.php?ip="+ip, true);
	xhttp.send();
	}
function giveMeTime()
	{
	variable="Haz un poco de tiempo";
	}
function initScanSec(tipo, inicial, final)
	{
	fondoIP="#00CC99";
	initScan(tipo, inicial, final);
	}

function initScan(tipo, inicial, final)
	{
	document.getElementById("scan").style.display="none";
	document.getElementById("radar").style.display="block";
	//alert("tipo: "+tipo+" - Inicial: "+inicial+" - Final: "+final);
	for (i=inicial; i<=final; i++)
		{
		addr=tipo+"."+i;
		getStatus(addr, i, final);
		//setInterval(giveMeTime, 50);
		}
	}
function isNew(ip)
	{
	var xhttp = new XMLHttpRequest();
	xhttp.open("GET", "isnew.php?ip="+ip, false);
	xhttp.send();
	newph=xhttp.responseText;
	if(newph==1) return true;
	else return false;
	}
function getExt(ip)
	{
	var xhttp = new XMLHttpRequest();
	xhttp.open("GET", "getext.php?ip="+ip, false);
	xhttp.send();
	ext=xhttp.responseText;
	return ext;
	}
function getStatus(ip, act, final)
	{
	var xhttp = new XMLHttpRequest();
	//xhttp.timeout = 3000;
	xhttp.onreadystatechange = function() {
		if (this.readyState == 4 && this.status == 200) {
    		response = this.responseText;
			if (act==final) document.getElementById("scan").style.display="block";
			if (act==final) document.getElementById("radar").style.display="none";
			res=response.split("=");
			conf=res[0];
			mac=res[1];
			if (conf=="ok")
				{
				if (document.getElementById("tabla_"+ip).style.display=="none") document.getElementById("tabla_"+ip).style.backgroundColor=fondoIP;
				document.getElementById("tabla_"+ip).style.display="table-row";
				document.getElementById("ip_"+ip).innerHTML="<a href='http://"+ip+"/cgi-bin/sip' target='_blank'>"+ip+"</a>";
				document.getElementById("titulo_"+ip).innerHTML=mac;
				var extact=getExt(ip);
				document.getElementById("ext_"+ip).innerHTML=extact;
				/////////////////////////////////////////////////
				if (isNew(ip)) document.getElementById("nuevo_"+ip).innerHTML="<div align='center'><img id='loading_"+ip+"' style='display:none;' src='loading.gif' width='25' height='24' /><input id='botupd_"+ip+"' type='button' name='button' id='button' value='Actualizar' onclick=\"updFW('"+ip+"')\" /></div>";
				else document.getElementById("nuevo_"+ip).innerHTML="<div align='center'>ACTUALIZADO</div>";
				document.getElementById("conf_"+ip).innerHTML="<input value=\""+extact+"\" name=\"confext_"+ip+"\" type=\"text\" id=\"confext_"+ip+"\" size=\"5\" /><input type=\"submit\" name=\"button_ip\" id=\"button_ip\" value=\"Configurar\"  onclick=\"confExt('"+ip+"');\"/>";
				}
  		}
	};
	xhttp.open("GET", "scanport.php?ip="+ip, true);
	xhttp.send();
	}
</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Geneva, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
.Estilo1 {
	font-size: 14px;
	font-weight: bold;
}
body {
	background-color: #666666;
}
-->
</style></head>

<body onload="initScan('<?= $tipo ?>', '<?= $inicial ?>', '<?= $final ?>')">
<table width="909" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="610"><div align="center"><img src="logo_wp_jda.fw.png" width="302" height="122" /><br />
      <span class="Estilo1">SCANNER DE TELÉFONOS IP - JDA</span></div></td>
    <td height="65"><div align="right"><input type="button" name="scan" id="scan" value="Re-Escanear" onclick="initScanSec('<?= $tipo ?>', '<?= $inicial ?>', '<?= $final ?>')" style="display:block;" />
      <img src="radar.gif" name="radar" border="0" id="radar" style="display:none;" /></div></td>
  </tr>
</table>
<table width="909" border="0" align="center" cellpadding="2" cellspacing="4">
  <tr>
    <td width="156" bgcolor="#CCCCCC"><div align="center"><strong>IP</strong></div></td>
    <td width="176" bgcolor="#CCCCCC"><div align="center"><strong>MAC</strong></div></td>
    <td width="110" bgcolor="#CCCCCC"><div align="center">
      <p><strong>FIRMWARE</strong></p>
      </div></td>
    <td width="154" bgcolor="#CCCCCC"><div align="center"><strong>EXTENSION</strong></div></td>
    <td width="105" bgcolor="#CCCCCC"><div align="center"><strong>ACTUALIZAR</strong></div></td>
    <td width="194" bgcolor="#CCCCCC"><div align="center"><strong>CONFIGURAR</strong></div></td>
  </tr>
  <?php
  for ($i=$inicial;$i<=$final;$i++)
  	{
	$ip=$tipo.".".$i;
  ?>
  <tr id="tabla_<?= $ip ?>" style="display:<?= "none" ?>; background-color:#E5E5E5;">
    <td height="25" align="center" id="ip_<?= $ip ?>"></td>
    <td align="center" id="titulo_<?= $ip ?>"><div align="center"></div></td>
    <td id="nuevo_<?= $ip ?>"><div align="center"></div></td>
    <td id="ext_<?= $ip ?>" align="center">&nbsp;</td>
    <td id="act_<?= $ip ?>">&nbsp;</td>
    <td id="conf_<?= $ip ?>"><label>
      <input name="confext_<?= $ip ?>" type="text" id="confext_<?= $ip ?>" size="5" />
      <input type="submit" name="button_ip" id="button_ip" value="Configurar"  onclick="confExt(ip);"/>
    </label></td>
  </tr>
  <?php
  	}
  ?>
  <tr style="display:table-row;">
    <td colspan="6" bgcolor="#000000">&nbsp;</td>
  </tr>
</table>
</body>
</html>
