<?php
// xml_to_html.php
// RJM Programming
// September, 2015
// Via http://www.w3schools.com/php/php_ref_simplexml.asp ... thanks
// Via http://www.w3schools.com/php/php_xml_parser_expat.asp ... thanks
// Via http://www.w3schools.com/php/php_xml_dom.asp ... thanks

$lastkey="";
$thisattrib="";
$thisnonattrib="";
$firstone="";
$firstset="";
$data="";
$upto="";
$atstart=true;
$rest="";
$parser=null;
$ohtml="";
$inxml="";
$typeslist=",";
$attribslist=",";
$fbit=" if (1 == 2) { ";
$ebit=" } ";
$thismode="SimpleXML"; //"simplexml";
if (isset($_GET['smode']) || isset($_POST['smode'])) {
  $fbit="";
  $ebit="";
}
if (isset($_GET['smode'])) $thismode=$_GET['smode'];
if (isset($_POST['smode'])) $thismode=$_POST['smode'];

// Document DOM method
function do_dom($ixml) {
global $ohtml, $upto, $data, $typeslist, $attribslist, $ec, $en, $firstone, $firstset, $xenonattribbit, $xeattribbit, $nextbit;
 $outhtml=analyze($ixml,true);
 file_put_contents($firstone . ".xml", str_replace("&lt;", "<", str_replace("&gt;", ">", $ixml)));
 $xmlDoc = new DOMDocument();
 $xmlDoc->load($firstone . ".xml");
 $xml = $xmlDoc->documentElement;
 
 $tlist=explode(",", substr($typeslist,1));
 
 //$nextbit=">";
 $attribbit="";
 $nonattribbit="";
 $once=false;
 $jj=1;
 $kk=0;
 $ioff=1;
 foreach ($xml->childNodes AS $concept) {
    $bits=explode("    ", $concept->nodeValue);
    $attribbit="";
 $ioff=1;
    $nonattribbit="";
    for ($ii=$kk; $ii<sizeof($bits); $ii++) {
      if (strpos($tlist[$ii + $ioff], "/") !== false) $ioff++;
       if (strpos($firstset, $tlist[$ii + $ioff]) === false) {
         $once=true;
         if (strpos($outhtml, " data-" . $tlist[$ii + $ioff] . '="' . $bits[$ii]) === false) { 
          if (strpos($attribbit, " data-" . $tlist[$ii + $ioff] . '="') !== false) {
           $once=false;
          } else { 
           if (strpos($attribbit, " data-" . $tlist[$ii + $ioff] . '="' . $bits[$ii]) === false) {
            //$kk=$ii;
            $attribbit .= " data-" . $tlist[$ii + $ioff] . '="' . $bits[$ii] . '" ';
           }
           if (strpos($nonattribbit, " data-" . $tlist[$ii + $ioff] . '="' . $bits[$ii]) === false) {
            //$kk=$ii;
            $nonattribbit .= " data-" . $tlist[$ii + $ioff] . '="' . $bits[$ii] . '" ';
           }
          }
         }
         $jj++;
       } 
    }
    if (($attribbit != "" || $nonattribbit != "")) {
    eval($xeattribbit);
    eval($xenonattribbit);
    if (strpos($outhtml, ($attribbit . $nonattribbit)) === false && ($attribbit != "" || $nonattribbit != "")) {
      $outhtml=str_replace('</DIV>', ' ' . $firstset . ' ' . $nextbit . $attribbit . $nonattribbit . ' </div></DIV>', $outhtml);
    }
    }
  }
  $outhtml=str_replace("DIV", "div", $outhtml);

 return $outhtml;
}


// Expat method
function do_expat($ixml) {
global $ohtml, $parser, $rest, $atstart, $fp, $typeslist, $attribslist, $lastkey, $data;
//echo $ixml;
//return "";
$ohtml=analyze($ixml, true);
$tlist=explode(",", substr($typeslist,1));
$lastkey=$tlist[0];
$atstart=true;
$rest="";
//Initialize the XML parser
$parser=xml_parser_create();

// Specify element handler
xml_set_element_handler($parser,"start","stop");

// Specify data handler
xml_set_character_data_handler($parser,"char");

// Open XML file
file_put_contents($tlist[0] . ".xml", str_replace(" <", "<", str_replace("\n ", "\n", str_replace("> ", ">\n ", $ixml))));
$fp=fopen($tlist[0] . ".xml","r");

// Read data
while ($data=fread($fp,4096)) {
  xml_parse($parser,$data,feof($fp)) or
  die (sprintf("XML Error: %s at line %d %s",
  xml_error_string(xml_get_error_code($parser)),
  xml_get_current_line_number($parser),strtoupper(substr($typeslist[0],0,1)) . strtolower(substr($typeslist[0],1))));
}

// Free the XML parser
xml_parser_free($parser);


 return str_replace("DIV", "div", $ohtml); //"<p>expat</p>";
}

// Function to use at the start of an element
function start($parser,$element_name,$element_attrs) {
global $ohtml, $upto, $data, $typeslist, $attribslist, $ec, $en;
  $ec=$element_attrs;
  $en=$element_name;
  $tlist=explode(",", substr($typeslist,1));
  $j=0;
  for ($i=0; $i<sizeof($tlist); $i++) {
    if (strtoupper($tlist[$i]) == strtoupper($element_name)) $j=$i;
  }
  $atts="";
  switch($element_name) {
    case strtoupper($tlist[0]): //"NOTE":
    //echo "-- Note --<br>";
    if (1 == 2) $ohtml.="-- " . strtoupper(substr($tlist[0],0,1)) . strtolower(substr($tlist[0],1)) . " --" . $atts . "<br>";
    break;
    case strtoupper($tlist[$j]): //"NOTE":
    if (1 == 9) {
    if (1 == 2) $ohtml.=strtoupper(substr($tlist[$j],0,1)) . strtolower(substr($tlist[$j],1)) . ":: " . $atts;
    $xone=strpos(strtolower($data), "<" . strtolower($tlist[$j]) . " ");
    $one=strpos(strtolower($data), "<" . strtolower($tlist[$j]) . ">");
    $two=strpos(strtolower($data), "</" . strtolower($tlist[$j]) . ">");
    if ($one !== false && $two !== false) {
      $one++;
      $two--;
      if (strpos(substr($data, $one, ($two - $one)), "<") !== false) {
  $ohtml=str_replace('</DIV>', ' ' . $firstset . ' <div data-subtype="' . strtolower($tlist[$j]) . '"' . $thisattrib . '> ' . $thisnonattrib . '  </div></DIV>', $ohtml);
  $thisattrib="";
  $thisnonattrib="";
  $lastkey=$tlist[$j];
      }
    } else if ($xone !== false && $two !== false) {
      $xone++;
      $two--;
      if (strpos(substr($data, $xone, ($two - $xone)), "<") !== false) {
  $ohtml=str_replace('</DIV>', ' ' . $firstset . ' <div data-subtype="' . strtolower($tlist[$j]) . '"' . $thisattrib . '> ' . $thisnonattrib . '  </div></DIV>', $ohtml);
  $thisattrib="";
  $thisnonattrib="";
  $lastkey=$tlist[$j];
      }
    }
    }
    break;
  }
}

// Function to use at the end of an element
function stop($parser,$element_name) {
global $ohtml, $data, $typeslist, $attribslist, $firstone, $firstset, $thisattrib, $thisnonattrib, $lastkey;
  $tlist=explode(",", substr($typeslist,1));
  $j=0;
  for ($i=0; $i<sizeof($tlist); $i++) {
    if (strtoupper($tlist[$i]) == strtoupper($element_name)) $j=$i;
  }
  //echo "<br>";
  switch($element_name) {
    case strtoupper($tlist[0]): //"NOTE":
  if (($thisattrib != "" || $thisnonattrib != "")) $ohtml=str_replace('</DIV>', ' ' . $firstset . ' <br><div data-subtype="' . strtolower($tlist[0]) . '"' . $thisattrib . '> ' . $thisnonattrib . '  </div></DIV>', $ohtml);
  $thisattrib="";
  $thisnonattrib="";
  //$lastkey=$tlist[0];
    break;
    case strtoupper($tlist[$j]): //"NOTE":
    if (1 == 1) {
    $xone=strpos(strtolower($data), "<" . strtolower($tlist[$j]) . " ");
    $one=strpos(strtolower($data), "<" . strtolower($tlist[$j]) . ">");
    $two=strpos(strtolower($data), "</" . strtolower($tlist[$j]) . ">");
    if ($one !== false && $two !== false) {
      $one++;
      $two--;
      if (strpos(substr($data, $one, ($two - $one)), "<") !== false && ($thisattrib != "" || $thisnonattrib != "")) {
  $ohtml=str_replace('</DIV>', ' ' . $firstset . ' <br><div data-subtype="' . strtolower($tlist[$j]) . '"' . $thisattrib . '> ' . $thisnonattrib . '  </div></DIV>', $ohtml);
  $thisattrib="";
  $thisnonattrib="";
  //$lastkey=$tlist[$j];
      }
    } else if ($xone !== false && $two !== false) {
      $xone++;
      $two--;
      if (strpos(substr($data, $xone, ($two - $xone)), "<") !== false && ($thisattrib != "" || $thisnonattrib != "")) {
  $ohtml=str_replace('</DIV>', ' ' . $firstset . ' <br><div data-subtype="' . strtolower($tlist[$j]) . '"' . $thisattrib . '> ' . $thisnonattrib . '  </div></DIV>', $ohtml);
  $thisattrib="";
  $thisnonattrib="";
  //$lastkey=$tlist[$j];
      }
    }
    }
  if (1 == 2) $ohtml.="<br>";
    break;
  }
}

// Function to use when finding character data
function char($parser,$data) {
global $ohtml, $ec, $typeslist, $attribslist, $en, $thisattrib, $thisnonattrib, $lastkey, $firstset, $firstone;
  $tlist=explode(",", substr($typeslist,1));
    //echo $data;
  if (1 == 2) $ohtml.=$data;
  if (strtolower($en) != strtolower($lastkey) && strpos(strtolower($firstset), "/" . strtolower($en)) === false && strpos(strtolower($firstone), strtolower($en)) === false) {
    $thisnonattrib.="  data-" .  strtolower($en) . '="' . $data . '" ';
    $thisattrib.="  data-" .  strtolower($en) . '="' . $data . '"';
  }
  $lastkey=$en;

  if (is_array($ec)) {
      while(list($key,$val) = each($ec)) {
        if (strtolower($key) != "id" && strtolower($key) != "title" && strtolower($key) != "alt" && strtolower($key) != "style") {
       if (strtolower($key) != strtolower($lastkey)) {
         //$ohtml.=" data-" . strtolower($key) . '="' . $val . '"';
         $thisattrib.=" data-" . strtolower($key) . '="' . $val . '"';
         $thisnonattrib.=" data-" . strtolower($key) . '="' . $val . '"';
         $lastkey=$key;
         }
        } else {
         //$ohtml.=" " . $key . '="' . $val . '"';
         $thisattrib.=" " . $key . '="' . $val . '"';
        }
       }
  }
}


// Used by SimpleXML and Expat and Document DOM methods
function analyze($ixml, $just) {
 global $typeslist, $attribslist, $firstone, $firstset, $thismode, $xeattribbit, $xenonattribbit, $nextbit;
 $outhtml="";
 $nextbit='<div data-subtype="">';
 $eattribbit='$attribbit = ' . "'';";
 $xeattribbit='$attribbit .= ' . "'';";
 $attribbit="";
 $enonattribbit='$nonattribbit = ' . "'';";
 $xenonattribbit='$nonattribbit .= ' . "'';";
 $nonattribbit="";
 $trythese=false;
 $firstone="";
 $firstset="";
 $typeslist=",";
 $attribslist=",";
 $xbits=explode("<?xml", $ixml);
 if (sizeof($xbits) > 1) {
   $prexbits=explode("?>", $xbits[1]);
   $ixml=str_replace("<?xml" . $prexbits[0] . "?>", "", $ixml);
 }
 $parts=explode("<", $ixml);
 for ($i=1; $i<sizeof($parts); $i++) {
   $presubparts=explode(" ", $parts[$i]);
   $subpart=explode(" ", str_replace(">", " ", $parts[$i]));
   if (strpos($typeslist, "," . $subpart[0] . ",") === false) {
     if ($trythese) {
       if (strpos($ixml, "<" . $subpart[0] . ">") !== false && strpos($ixml, "</" . $subpart[0] . ">") !== false) {
         $one=strpos($ixml, "<" . $subpart[0] . ">");
         $one++;
         $two=strpos($ixml, "</" . $subpart[0] . ">");
         $two--;
         if (strpos(substr($ixml, $one, ($two - $one)), "<") !== false) {
           $firstset .= "/" . $subpart[0];
           $nextbit=str_replace('">', $subpart[0] . '">', $nextbit);
         } else {
           $trythese=false;
         }
       } else if (strpos($ixml, "<" . $subpart[0] . " ") !== false && strpos($ixml, "</" . $subpart[0] . ">") !== false) {
         $one=strpos($ixml, "<" . $subpart[0] . " ");
         $one++;
         $two=strpos($ixml, "</" . $subpart[0] . ">");
         $two--;
         if (strpos(substr($ixml, $one, ($two - $one)), "<") !== false) {
           $firstset .= "/" . $subpart[0];
           $nextbit=str_replace('">', $subpart[0] . '">', $nextbit);
         } else {
           $trythese=false;
         }
       }
     }
     if ($firstone == "") {
       $firstone=$subpart[0];
       $firstset=$firstone;
       $trythese=true;
     } else if ($trythese == false && strpos($subpart[0], "/") === false) {
       //if (strpos($enonattribbit, $subpart[0]) === false) $enonattribbit=str_replace(" = ", " = ' data-" . $subpart[0] . '="' . "'" . ' . $concept->' . $subpart[0] . " . " . "'" . '"' . "'" . ' . ', $enonattribbit);
       if (strpos($enonattribbit, $subpart[0]) === false) $enonattribbit=str_replace(";", " . ' data-" . $subpart[0] . '="' . "'" . ' . $concept->' . $subpart[0] . " . " . "'" . '"' . "'" . ' . ' . "'';", $enonattribbit);

       if (strpos($eattribbit, $subpart[0]) === false && strpos($attribbit, $subpart[0]) === false) {
          if ($attribbit == "") {
            $eattribbit='$attribbit = ' . "'>';";
            $xeattribbit='$attribbit .= ' . "'>';";
            $attribbit=">";
          }
        $eattribbit=str_replace("= ", "= ' data-" . $subpart[0] . '="' . "'" . ' . $concept->' . $subpart[0] . " . " . "'" . '"' . "'" . ' . ', $eattribbit);
       }
       
       //if (strpos($xenonattribbit, $subpart[0]) === false) $xenonattribbit=str_replace(";", " . ' data-" . $subpart[0] . '="' . "'" . ' . $concept->' . $subpart[0] . " . " . "'" . '"' . "'" . ' . ' . "'';", $xenonattribbit);
     }
     $typeslist .= $subpart[0] . ",";
   }
   $ok=true;
   for ($j=0; $j<sizeof($presubparts); $j++) {
     if ($ok) {
       if (strpos($presubparts[$j], "=") !== false) {
        $pep=explode("=", $presubparts[$j]);
        if (strpos($attribslist, "," . $subpart[0] . ":" . $pep[0] . ",") === false) {
          $attribslist .= $subpart[0] . ":" . $pep[0] . ",";
          if (strpos($attribbit, $pep[0]) === false) {
           if (strtolower($pep[0]) != "id" && strtolower($pep[0]) != "title" && strtolower($pep[0]) != "alt" && strtolower($pep[0]) != "style") {
            if (strpos($enonattribbit, $pep[0]) === false) $enonattribbit=str_replace(";", " . ' data-" . $pep[0] . '="' . "'" . ' . $concept[' . "'" . $pep[0] . "']" . " . " . "'" . '"' . "'" . ' . ' . "'';", $enonattribbit);
            if (strpos($xenonattribbit, $pep[0]) === false) $xenonattribbit=str_replace(";", " . ' data-" . $pep[0] . '="' . "'" . ' . $concept->getAttribute(' . "'" . $pep[0] . "')" . " . " . "'" . '"' . "'" . ' . ' . "'';", $xenonattribbit);
           } else {
          if ($attribbit == "") {
            $eattribbit='$attribbit = ' . "'>';";
            $xeattribbit='$attribbit .= ' . "'>';";
            $attribbit=">";
          }
            $eattribbit=str_replace("= ", "= '" . $pep[0] . '="' . "'" . ' . $concept[' . "'" . $pep[0] . "']" . " . " . "'" . '"' . "'" . ' . ', $eattribbit);
            $xeattribbit=str_replace("= ", "= '" . $pep[0] . '="' . "'" . ' . $concept->getAttribute(' . "'" . $pep[0] . "')" . " . " . "'" . '"' . "'" . ' . ', $xeattribbit);
            if (strpos($nextbit, '"' . $subpart[0] . '"') !== false) $nextbit=str_replace('">', '" ', $nextbit);
           }
          }
        }
       }
       if (strpos($presubparts[$j], ">") !== false) {
        $ok=false;
       } 
     }
   }
 }
 
if (!$just) {  // SimpleXML method
 file_put_contents($firstone . ".xml", str_replace("&lt;", "<", str_replace("&gt;", ">", $ixml)));
 $xml=simplexml_load_file($firstone . ".xml") or die("Error: Cannot create object");
 $outhtml='<DIV  id="' . $firstone . '1" data-type="' . $firstset . '"></DIV>';
 foreach($xml->children() as $concept) {
    eval($eattribbit);
    eval($enonattribbit);
    $outhtml=str_replace('</DIV>', ' ' . $firstset . ' ' . $nextbit . $attribbit . $nonattribbit . '  </div></DIV>', $outhtml);
 }
 $outhtml=str_replace("DIV", "div", $outhtml);
 } else {
 $outhtml='<DIV  id="' . $firstone . '1" data-type="' . $firstset . '"></DIV>';
 }
 return $outhtml;
}

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return str_replace(":", "_", $rma);
}

if (isset($_GET['inxml']) && file_exists("xml_to_html.html")) {
  $inxml=urldecode($_GET['inxml']);
if (strpos(strtolower($thismode), "simplexml") !== false) {
  if (strpos($_SERVER['HTTP_REFERER'], "xml_to_html.html") !== false && strpos($_SERVER['HTTP_REFERER'], "rjmprogramming.com.au") !== false) {
   header("Location: ./xml_to_html.html?inxml=" . $_GET['inxml']);
   exit;
  }
} else if (strpos(strtolower($thismode), "expat") !== false) {
$outhtml=do_expat($inxml);
} else if (strpos(strtolower($thismode), "dom") !== false) {
$outhtml=do_dom($inxml);
}
} else if (isset($_POST['inxml']) && file_exists("xml_to_html.html")) {
  $inxml=urldecode($_POST['inxml']);
if (strpos(strtolower($thismode), "simplexml") !== false) {
  if (strpos($_SERVER['HTTP_REFERER'], "xml_to_html.html") !== false && strpos($_SERVER['HTTP_REFERER'], "rjmprogramming.com.au") !== false) {
  $pinxml=$_POST['inxml'];
  $xbits=explode("<?xml", $pinxml);
  if (sizeof($xbits) > 1) {
   $prexbits=explode("?>", $xbits[1]);
   $pinxml=str_replace("<?xml" . $prexbits[0] . "?>", "", $pinxml);
  }
  $okhtml=@file_get_contents("xml_to_html.html");
  $bits=explode('var preinxml="', $okhtml);
  if (sizeof($bits) > 1) {
    $subbits=explode('"', $bits[1]);
    $newhtml=str_replace($subbits[0], str_replace("\n", " ", str_replace("\r", " ", str_replace('"', '\"', str_replace("<","&lt;",str_replace(">","&gt;",urldecode($pinxml)))))), $okhtml);
    file_put_contents("xmltohtml" . server_remote_addr() . ".html", $newhtml);
    header("Location: xmltohtml" . server_remote_addr() . ".html");
    exit;
  } else {
    header("Location: ./xml_to_html.html?inxml=" . $_POST['inxml']);
    exit;
  }
} else if (strpos(strtolower($thismode), "expat") !== false) {
$outhtml=do_expat($inxml);
} else if (strpos(strtolower($thismode), "dom") !== false) {
$outhtml=do_dom($inxml);
}
  }
} else if (isset($_POST['inxml'])) {
  $inxml=urldecode($_POST['inxml']);
  if (1 == 2) {
  $pinxml=$_POST['inxml'];
  $xbits=explode("<?xml", $pinxml);
  if (sizeof($xbits) > 1) {
   $prexbits=explode("?>", $xbits[1]);
   $pinxml=str_replace("<?xml" . $prexbits[0] . "?>", "", $pinxml);
  }
  }
}

if ($inxml != "") {
if (strpos(strtolower($thismode), "simplexml") !== false) {
$outhtml=analyze($inxml,false);
} else if (strpos(strtolower($thismode), "expat") !== false) {
$outhtml=do_expat($inxml);
} else if (strpos(strtolower($thismode), "dom") !== false) {
$outhtml=do_dom($inxml);
}
} else {
$inxml="&lt;messages&gt;  &lt;note id='501'&gt;    &lt;to&gt;Tove&lt;/to&gt;    &lt;from&gt;Jani&lt;/from&gt;    &lt;heading&gt;Reminder&lt;/heading&gt;    &lt;body&gt;Don't forget me this weekend!&lt;/body&gt;  &lt;/note&gt;  &lt;note id='502'&gt;    &lt;to&gt;Jani&lt;/to&gt;    &lt;from&gt;Tove&lt;/from&gt;    &lt;heading&gt;Re: Reminder&lt;/heading&gt;    &lt;body&gt;I will not&lt;/body&gt;  &lt;/note&gt;&lt;/messages&gt;";
file_put_contents("messages.xml", str_replace("&lt;", "<", str_replace("&gt;", ">", $inxml)));
if (strpos(strtolower($thismode), "simplexml") !== false) {
$xml=simplexml_load_file("messages.xml") or die("Error: Cannot create object");
$outhtml='<DIV id="messages1" data-type="messages/note"></DIV>';
foreach($xml->children() as $note) {
    $outhtml=str_replace('</DIV>', ' messages/note <div data-subtype="note" data-to="' . $note->to . '" data-from="' . $note->from . '" data-heading="' . $note->heading . '" data-body="' . $note->body . ' id="' . $note['id'] . '"> data-to="' . $note->to . '" data-from="' . $note->from . '" data-heading="' . $note->heading . '" data-body="' . $note->body . '"  </div></DIV>', $outhtml);
}
$outhtml=str_replace("DIV", "div", $outhtml);
} else if (strpos(strtolower($thismode), "expat") !== false) {
$outhtml=do_expat($inxml);
} else if (strpos(strtolower($thismode), "dom") !== false) {
$outhtml=do_dom($inxml);
}
}

$thehtml="<!doctype html>
<html>
<head>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<title>XML to HTML - PHP via SimpleXML or Expat or Document DOM - RJM Programming - September, 2015 - via http://www.w3schools.com/xml/xml_attributes.asp</title>
<scrip" . "t type='text/javascript'>

var preqs=location.search.split('addressbook=')[1] ? location.search.split('addressbook=')[1].split('&')[0] : 'y';
var qs='?addressbook=' + (location.search.split('noaddressbook=')[1] ? location.search.split('noaddressbook=')[1].split('&')[0].toLowerCase().replace('y','n') : preqs);

function changemode(so, si, sv) {
  document.getElementById('imode').value=sv;
}
/" . "/ A business logic idea ... messages/note XML with defined id= are user interactively emailable 
function checkforemail() { " . $fbit . " 
  var ee, eo, preiu, iu, predivs,divs=[],cdivs;
  var ecnt=0;
  var oto='', ofrom='', osubject='', obody='';
  
  predivs=document.body.innerHTML.toLowerCase().split(' id=');
  
  for (preiu=1; preiu<predivs.length; preiu++) {
    cdivs=predivs[preiu].substring(1).split(predivs[preiu].substring(0,1));
    iu=0;
    divs[iu]=document.getElementById(cdivs[0]);
    try {
     oto='';
     ofrom='';
     oheading='';
     obody='';
     try {
       oto=divs[iu].getAttribute('data-to');
       if (oto == null) oto='';
     } catch (ee) {
       oto='';
     }
     try {
       ofrom=divs[iu].getAttribute('data-from');
       if (ofrom == null) ofrom='';
     } catch (ee) {
       ofrom='';
     }
     try {
       obody=divs[iu].getAttribute('data-body');
       if (obody == null) obody='';
     } catch (ee) {
       obody='';
     }
     try {
       osubject=divs[iu].getAttribute('data-heading');
       if (osubject == null) {
         osubject='';
         osubject=divs[iu].getAttribute('data-subject');
         if (osubject == null) osubject='';
       } 
     } catch (ee) {
       osubject='';
       try {
         osubject=divs[iu].getAttribute('data-subject');
         if (osubject == null) osubject='';
       } catch (ee) {
         osubject='';
       }
     }
     
    } catch (ee) {
    }
    if (oto != '' || obody != '' || osubject != '') {
      if (document.getElementById('elinksplace').innerHTML.replace(/&amp;/g,\"&\").indexOf(('mailto:' + oto + '?subject=' + encodeURIComponent(osubject) + '&body=' + encodeURIComponent(obody)).replace(/&amp;/g,\"&\")) == -1) {
       document.getElementById('elinksplace').innerHTML+='<a href=\"mailto:' + oto + '?subject=' + encodeURIComponent(osubject) + '&body=' + encodeURIComponent(obody) + '\" id=\"email' + ecnt + '\" style=\"display:none;\">From ' + ofrom + \"</a>\";
       ecnt++;
      }
    }
  }
  if (ecnt > 0) {
    for (var iuc=eval(-1 + ecnt); iuc>=0; iuc--) {
      eo=document.getElementById('email' + iuc);
      eo.click();
    }
  } " . $ebit . " 
}
/" . "/ A business logic idea ... client pre-emptive iframe ... address book
function getaddressbook(iois) {
  var qbits, ie, je, edelim='\"', bdone=false;
  var idx=['&lt;to&gt;','<to>','<TO>','<To>','data-to=\"','&lt;from&gt;','<from>','<FROM>','<From>','data-from=\"','&lt;cc&gt;','<cc>','<CC>','<Cc>','data-cc=\"','&lt;bcc&gt;','<bcc>','<BCC>','<Bcc>','data-bcc=\"'];
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument); 
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
      addresscont=aconto.body.innerHTML.replace(/\\n/g, ',');
      var emailinfo=addresscont.split(',');
      for (ie=1; ie<eval(-1 + emailinfo.length); ie+=2) {
       if (emailinfo[eval(ie - 1)] != \"\" && emailinfo[ie].indexOf('@') != -1) {
       for (je=0; je<idx.length; je++) {
        edelim='&lt;';
        if (idx[je].indexOf(edelim) == -1) edelim='\"';
        if (idx[je].indexOf(edelim) == -1) edelim='<';
        if (document.body.innerHTML.toLowerCase().indexOf(idx[je] + emailinfo[eval(ie - 1)].toLowerCase() + edelim) != -1) {
         if (document.body.innerHTML.indexOf(idx[je] + emailinfo[eval(ie - 1)] + edelim) != -1) {
         if (je < 4 && bdone == false) {
           bdone=true;
           document.getElementById('bemail').style.display='block';
         }
          while (document.body.innerHTML.indexOf(idx[je] + emailinfo[eval(ie - 1)] + edelim) != -1) {
           document.body.innerHTML=document.body.innerHTML.replace(idx[je] + emailinfo[eval(ie - 1)] + edelim,idx[je] + emailinfo[eval(ie - 1)] + \" [\" + emailinfo[ie] + \"]\" + edelim);
          }
         }
         if (document.body.innerHTML.indexOf(idx[je] + emailinfo[eval(ie - 1)].toLowerCase() + edelim) != -1) {
         if (je < 4 && bdone == false) {
           bdone=true;
           document.getElementById('bemail').style.display='block';
         }
          while (document.body.innerHTML.indexOf(idx[je] + emailinfo[eval(ie - 1)].toLowerCase() + edelim) != -1) {
           document.body.innerHTML=document.body.innerHTML.replace(idx[je] + emailinfo[eval(ie - 1)].toLowerCase() + edelim,idx[je] + emailinfo[eval(ie - 1)].toLowerCase() + \" [\" + emailinfo[ie] + \"]\" + edelim);
          }
         }
         if (document.body.innerHTML.indexOf(idx[je] + emailinfo[eval(ie - 1)].toUpperCase() + edelim) != -1) {
         if (je < 4 && bdone == false) {
           bdone=true;
           document.getElementById('bemail').style.display='block';
         }
          while (document.body.innerHTML.indexOf(idx[je] + emailinfo[eval(ie - 1)].toUpperCase() + edelim) != -1) {
           document.body.innerHTML=document.body.innerHTML.replace(idx[je] + emailinfo[eval(ie - 1)].toUpperCase() + edelim,idx[je] + emailinfo[eval(ie - 1)] + \" [\" + emailinfo[ie].toUpperCase() + \"]\" + edelim);
          }
         }
        }
       }
      }
      }
     }
    }
  }
}
/" . "/ A business logic idea ... email via XML instructions
function emailit() {
  var fs=document.getElementsByTagName('form');
  var tas=document.getElementsByTagName('textarea');
  if (tas.length >= 3) {
    tas[2].innerHTML=tas[0].innerHTML;
    fs[0].submit();
  }
}
</scr" . "ipt>
</head>
<body style='background-color:teal;' onload=" . '"' . " document.getElementById('imode').value='" . strtolower($thismode) . "'; document.getElementById('smode').value='" . strtolower($thismode) . "'; checkforemail(); " . '"' . ">
<h1 align='center'>XML to HTML - PHP via <select id='smode' onchange='changemode(this,this.id,this.value);'><option value='simplexml'>SimpleXML</option><option value='expat'>Expat</option><option value='dom'>Document DOM</option></select> - RJM Programming - September, 2015</h1>
<div align='center'><table><tr><th>XML</th><th> <a target=_blank title='Where the XML on left came from ... http://www.w3schools.com/xml/xml_attributes.asp ... thanks' href='http://www.w3schools.com/xml/xml_attributes.asp'>----</a>&gt; Display <a target=_blank title='PHP simplexml functionality information from w3schools' href='http://www.w3schools.com/php/php_ref_simplexml.asp'>----</a>&gt; </th><th>HTML<font size=0>5</font></th></tr><tr><td><textarea style='background-color:silver;' rows=10 cols=80>" . str_replace("&lt;", "<", str_replace("&gt;", ">", $inxml)) . "</textarea></td><td> ----&gt; " . $outhtml . " ----&gt; </td><td><textarea style='background-color:lightblue;' rows=10 cols=80>" . str_replace("<","&lt;", str_replace(">","&gt;", $outhtml)) . "</textarea></td></tr></table></div>" . "
<br><br><br><br><br><form method='POST' action='./xml_to_html.php' id='myform'><input id='imode' name='smode' type='hidden' value='simplexml'></input><input type='submit' value='Translate XML to HTML' style='valign:top;'><br><textarea cols='120' rows='50' name='inxml'></textarea></form>
<div id='elinksplace'><iframe src='addressbook.html' id='eabook' onload=" . '"' . "  if (qs.indexOf('=n') == -1) {  getaddressbook(this); } " . '"' . " style='display:none;'></iframe><input onclick=' emailit();' type='button' value='Email' style='display:none;position:absolute;top:280px;left:30px;z-index:7;' id='bemail'></input></div>
</body>
</html>
";

echo $thehtml;
?> 