<?php
   function pc_html2ascii($s) { 
            // convert links 
            $s = preg_replace('/<a\\s+.*?href="?([^\\" >]*)"?[^>]*>(.*?)<\\/a>/i', 
                              '$2 ($1)', $s); 
            
            // convert <br>, <hr>, <p>, <div> to line breaks 
            $s = preg_replace('@<(b|h)r[^>]*>@i',"\\n",$s); 
            $s = preg_replace('@<p[^>]*>@i',"\\n\\n",$s); 
            $s = preg_replace('@<div[^>]*>(.*)</div>@i',"\\n".'$1'."\\n",$s); 
            $s = preg_replace('@<pre[^>]*>(.*)</pre>@i',"\\n".'$1'."\\n",$s); 
            
            // convert bold and italic 
            $s = preg_replace('@<b[^>]*>(.*?)</b>@i','*$1*',$s); 
            $s = preg_replace('@<i[^>]*>(.*?)</i>@i','/$1/',$s); 
            
            // decode named entities 
            $s = strtr($s,array_flip(get_html_translation_table(HTML_ENTITIES))); 
            
            // decode numbered entities 
            // $s = preg_replace('//e','chr(\\\\1)',$s); 
            
            // remove any remaining tags 
            $s = strip_tags($s); 
            
            return $s; 
        }         
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $_GET['tittle']?> - POS - WebSafiERP</title>
</head>
<body>
<script type="text/javascript">
      var sleepCounter = 0;
      var seletedPrinter = "POS";
      var hasSelected = false;
   
      function findPrinter() {
         var applet = document.jZebra;
         if (applet != null) {
            // Searches for locally installed printer with "zebra" in the name
            // applet.findPrinter("zebra");
            applet.findPrinter(seletedPrinter);
 
         }
	 
         monitorFinding();
      }

      function findPrinters() {
         var applet = document.jZebra;
         if (applet != null) {
            // Searches for locally installed printer with "zebra" in the name
            applet.findPrinter("\\{dummy printer name for listing\\}");
         }

         monitorFinding2();
      }

      function monitorFinding() {
        var applet = document.jZebra;
        if (applet != null) {
           if (!applet.isDoneFinding()) {
              window.setTimeout('monitorFinding()', 100);
           } else {
              var printer = applet.getPrinterName();
              alert(printer == null ? "Impresora no encontrada" : "Impresora \"" + printer + "\" seleccionada.");
           }
        } else {
            alert("¡Applet no cargado!");
        }
      }

      function print() {
      	 if (!hasSelected) {
      	   alert("Debe seleccionar una impresora");


      	 }
      	 else {
      	 findPrinter();
      	 
         var applet = document.jZebra;
         
         if (applet != null) {
            // Send characters/raw commands to applet using "append"
            // Hint:  Carriage Return = \r, New Line = \n, Escape Double Quotes=\"
           // applet.setEncoding("UTF-8");
	    
            applet.append("<?php echo pc_html2ascii(''.$_POST['commands']); ?>\n\n\n");
            // Send characters/raw commands to printer
            applet.print();
         }
         
         monitorPrinting();
         applet = null;
         }
      }
      
      function monitorPrinting() {
        var applet = document.jZebra;
        if (applet != null) {
           if (!applet.isDonePrinting()) {
              window.setTimeout('monitorPrinting()', 100);
           } else {
              var e = applet.getException();
              alert(e == null ? "Impreso de forma exitosa." : "Ocurri&otilde; una excepci&otilde;n: " + e.getLocalizedMessage());
           }
        } else {
            alert("¡Applet no cargado!");
        }
      }
      
      function monitorFinding2() {
		var applet = document.jZebra;
		if (applet != null) {
	   		if (!applet.isDoneFinding()) {
	      		window.setTimeout('monitorFinding2()', 100);
	   		} else {
              var printersCSV = applet.getPrinters();
              var printers = printersCSV.split(",");
              var newHTML = '<select name="impresoras" multiple="multiple" onblur="getMultiple(document.myForm.impresoras);">';              
              for (p in printers) {
                  newHTML = newHTML + '<option>'+ printers[p] + '</option>';
              }
              newHTML= newHTML + '</select>';
              myForm.innerHTML = newHTML;
	   		}
		} else {
            alert("¡Applet no cargado!");
        }
      }
      
function getMultiple(ob)
{
    if (ob.selectedIndex != -1) {
       seletedPrinter = ob.options[ob.selectedIndex].value;
       hasSelected = true;
    }
    // You can use the arSelected array for further processing.
}
</script>
<?php  echo $_POST['commands']; ?>
<br/><br/>
   <!-- <input type=button onClick="findPrinter()" value="Buscar impresora"><br /> -->
   <p>Por favor, liste las impresoras y luego seleccione la impresora deseada antes de imprimir.</p>
   <input type=button onClick="findPrinters()" value="Listar impresoras."><br />
    <form name="myForm">
      <select multiple>
      </select>
    </form>
   <input type=button onClick="print()" value="Imprimir factura"><br>

<applet name="jZebra" code="jzebra.PrintApplet.class" archive="./jzebra.jar" width="100" height="100">

Su navegador no soporta el plugin de java o este no está instalado.
Por favor informe al área de soporte técnico.

<param name="printer" value="zebra">
<!-- <param name="sleep" value="200"> -->
</applet>
</body>
</html>
