<!doctype html>
<html>
<head>
<title>Legend (of Map, perhaps) - RJM Programming - September, 2016 - Thanks to http://www.html5rocks.com/en/tutorials/file/dndfiles/</title>
<meta charset="utf-8"/>
<style>
#byte_content {
margin: 5px 0;
max-height: 100px;
overflow-y: auto;
overflow-x: hidden;
}
#byte_range { margin-top: 5px; }

</style>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

<script type='text/javascript'>
var rawurl=location.search.split('url=')[1] ? decodeURIComponent(location.search.split('url=')[1]).split('&')[0] : '';
var rawhtml=location.search.split('tarea=')[1] ? decodeURIComponent(location.search.split('tarea=')[1].split('&')[0]) : '';
var showlabel=location.search.split('showlabel=')[1] ? decodeURIComponent(location.search.split('showlabel=')[1]).split('&')[0] : 'off';
var widths=[], heights=[];
var tox=[], toy=[];
var href=[], onclick=[], names=[];
var filen = '', firstgo=true;
var mapimg='';
var xrawurl='';
var popup=null;
var yehbut='';
var ifh='600px';
var tablehtml='';
var idone=false;

var httpxml=null;

function createCORSRequest(method, url) { // thanks to http://jquery-howto.blogspot.com.au/2013/09/jquery-cross-domain-ajax-request.html
try {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
// XHR has 'withCredentials' property only if it supports CORS
xhr.open(method, url, true);
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET');
try {
xhr.overrideMimeType('text/xml'); // unsupported by IE
} catch(e) {}
} else if (typeof XDomainRequest != "undefined"){ // if IE use XDR
xhr = new XDomainRequest();
xhr.open(method, url);
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET');
try {
xhr.overrideMimeType('text/xml'); // unsupported by IE
} catch(e) {}
} else {
xhr = null;
}
} catch(ee) {
xhr = null;
}
if (!xhr) {
// Feed jQuery JSON
$.getJSON((url.replace('?','&') + "&format=json&jsoncallback=?").replace('&','?'), displayHtml);
}
setTimeout(checkrest, 2000);
return xhr;
}

function checkrest() {
if (rawhtml == '') {
document.getElementById('myif').src = 'http://www.rjmprogramming.com.au/HTMLCSS/legend_via_map.php?url=' + encodeURIComponent(xrawurl);
}
}


function displayHtml(data) {
rawhtml = data;
checkins();
}

function changed(othis) {
xrawhtml = othis;
if (!httpxml) ajaxFunction(othis);
if (httpxml) {
//httpxml.open("GET", othis, true);
httpxml.send(null);
}
}

function stateChanged() {
if (httpxml.readyState == 4) {
if (httpxml.status == 200) {
rawhtml = httpxml.responseText;
checkins();
}
}
}

function ajaxFunction(inu) {
//var httpxml;
try {
// Firefox, Opera 8.0+, Safari
httpxml=createCORSRequest( "get", inu); //.replace('//','@@').split('/')[0].replace('@@','//') ); // new XMLHttpRequest();
httpxml.onreadystatechange=stateChanged;
//alert(95);
} catch (e) {
// Internet Explorer
try {
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
httpxml.onreadystatechange=stateChanged;
} catch (e) {
try {
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
httpxml.onreadystatechange=stateChanged;
} catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
}

function receiveMessage(event) {
// Do we trust the sender of this message? (might be
// different from what we originally opened, for example).
if (event.origin !== "http://members.ozemail.com.au") {
return;
}

// event.source is popup
// event.data is document.body.innerHTML + " Hello RJM Programming! the secret response is: av" + "-a-car-do!"
if (event.source == popup) {
if (event.data.indexOf('av-a-' + 'car-do!') != -1) {
rawhtml=event.data.split('</html>')[0] + '</html>';
checkins();
}
}
}

function crossdomainiframe() {

var thisurl=xrawurl;
if (popup) {
// var popup = window.open(thisurl, 'myif');

// When the popup has fully loaded, if not blocked by a popup blocker:

try {
// This does nothing, assuming the window hasn't changed its location.
popup.postMessage("The user is 'admin' and the password is 'av-a-c" + "ar-do!'",
"http://members.ozemail.com.au");

// This will successfully queue a message to be sent to the popup, assuming
// the window hasn't changed its location.
popup.postMessage("hello there!", "http://members.ozemail.com.au");

if (window.addEventListener) {
window.addEventListener("message", receiveMessage, false);
} else if (window.attachEvent) {
window.attachEvent("onmessage", receiveMessage);
}
} catch(eee) { }
} else {
setTimeout(crossdomainiframe, 2000);
}
}


function placeit(whattoleft) {
document.getElementById('placemap').innerHTML="<iframe onload=' if (!idone) { idone=true; ifdone(this); }' id=ifplacemap src='" + whattoleft.href + "' style='overflow:scroll;height:" + ifh + ";width:600px;' width=600 height=" + ifh + "></iframe>";
}

function checkins() {
var sparer='', i, j, k, bits, maphtml='', postbits, hrefbits, onclickbits, minw, maxw, minh, maxh,mybits='',delim='';
//tablehtml='';
if (rawhtml != '' && mapimg == '') {
//alert(rawhtml);
bits=rawhtml.replace('<IMG','<img').split('<img ');
if (bits.length > 1) {
//alert(bits[1]);
postbits=bits[1].replace(' SRC=',' src=').split('src=');
if (postbits.length > 1) {
//alert(postbits[1]);
mapimg=postbits[1].replace('>',' ').split(' ')[0].replace('"','').replace('"','').replace("'","").replace("'","");
//alert('mApimg=' + mapimg + ' xrawurl=' + xrawurl);
if (mapimg != '' && mapimg.indexOf('/') == -1 && xrawurl.indexOf('/') != -1) {
//alert(xrawurl);
var qbits=xrawurl.split('/');
if (qbits[eval(-1 + qbits.length)].indexOf('.') != -1) {
mapimg=xrawurl.replace('/' + qbits[eval(-1 + qbits.length)], '/' + mapimg);
//alert('mapimg=' + mapimg);
} else if (qbits[eval(-1 + qbits.length)] == '') {
mapimg=xrawurl + mapimg;
//alert('mapImg=' + mapimg);
} else {
mapimg=xrawurl + '/' + mapimg;
//alert('mapiMg=' + mapimg);
}
}
var ioh = new Image();
ioh.src = mapimg;
ifh = eval(("" + ioh.width).replace('px',''));
if (eval("0" + ifh) == 0) ifh=600;
ifh += 200;
//document.title='iFh=' + ifh;
}
}
}
if (firstgo && rawhtml != '') {
document.getElementById('tarea').value = rawhtml;
}
if (document.getElementById('showlabel').checked && showlabel != 'on') {
if (firstgo) {
document.getElementById('showlabel').checked = false;
} else {
showlabel = 'on';
}
} else if (!document.getElementById('showlabel').checked && showlabel == 'on') {
if (firstgo) {
document.getElementById('showlabel').checked = true;
} else {
showlabel = 'off';
}
}
firstgo = false;
if (rawurl) {
if (rawurl != '') {
sparer=rawurl;
xrawurl=rawurl;
rawurl='';
filen = "<a id=aplace target=_blank onclick=placeit(this); title=MapHTML href='" + sparer + "'>" + sparer + "</a>";
document.getElementById('url').value = sparer;
if (sparer.indexOf('rjmprogramming.com.au') == -1) {
if (sparer.indexOf('members.ozemail.com.au/~mkuulma') != -1) {
if (sparer.indexOf('livingroom_corner.') != -1) {
changed(sparer);
} else {
popup = window.open(sparer, 'myif');
}
} else if (1 == 2) {
popup = window.open(sparer, '_blank', 'top=30,left=0,width=600,height=800');
setTimeout(crossdomainiframe,3000);
} else {
changed(sparer);
popup = window.open(sparer, '_blank', 'top=30,left=0,width=600,height=800');
}
} else {
document.getElementById('myif').src = sparer;
}
if (mapimg != '' && mapimg.indexOf('/') == -1 && sparer.indexOf('/') != -1) {
//alert(sparer);
var xbits=sparer.split('/');
if (xbits[eval(-1 + xbits.length)].indexOf('.') != -1) {
mapimg=sparer.replace('/' + xbits[eval(-1 + xbits.length)], '/' + mapimg);
//alert('mapimg=' + mapimg);
} else if (xbits[eval(-1 + xbits.length)] == '') {
mapimg=sparer + mapimg;
//alert('mapImg=' + mapimg);
} else {
mapimg=sparer + '/' + mapimg;
//alert('mapiMg=' + mapimg);
}
}
if (mapimg != '') {
var ioh = new Image();
ioh.src = mapimg;
ifh = eval(("" + ioh.width).replace('px',''));
if (eval("0" + ifh) == 0) ifh=600;
ifh += 200;
//document.title='iFH=' + ifh;
}
return;
}
}
if (rawhtml) {
if (rawhtml != '') {
// if ((location.search.split('url=')[1] ? decodeURIComponent(location.search.split('url=')[1]).split('&')[0] : '') != '') document.getElementById('tarea').value = rawhtml; //tablehtml;
names=[];
widths=[];
heights=[];
tox=[];
toy=[];
href=[];
onclick=[];
bits=rawhtml.split('<!-- Region ');
if (bits.length > 1) {
for (j=1; j<bits.length; j++) {
postbits=bits[j].split(' ');
if (postbits.length > 1) {
if (showlabel == 'on') {
names.push(bits[j].replace(postbits[0] + ' ','').split(' -->')[0]);
} else {
names.push('<span id=span' + j + ' style=display:none;>' + bits[j].replace(postbits[0] + ' ','').split(' -->')[0] + '</span>');
}
}
}
}
bits=rawhtml.replace('</scr' + 'ipt','<scr' + 'ipt').split('<scr' + 'ipt');
if (bits.length > 1) {
document.getElementById('forscripts').innerHTML = '<scr' + 'ipt' + bits[1] + '</scr' + 'ipt>';
}
rawhtml=rawhtml.replace(/onClick=/g, "onclick=");
if (rawhtml.indexOf('coords="') != -1) {
bits=rawhtml.split('coords="');
//if ((location.search.split('url=')[1] ? decodeURIComponent(location.search.split('url=')[1]).split('&')[0] : '') != '') alert(bits.length);
for (j=1; j<bits.length; j++) {
postbits=bits[j].split('"')[0].split(',');
hrefbits=bits[j].split('href="');
onclickbits=bits[j].split('onclick="');
for (k=0; k<postbits.length; k+=2) {
if (k == 0) {
minw=eval(postbits[k]);
maxw=eval(postbits[k]);
minh=eval(postbits[eval(1 + k)]);
maxh=eval(postbits[eval(1 + k)]);
//names[eval(-1 + j)]=names[eval(-1 + j)] + ' ' + bits[j].split(' ')[0];
} else {
if (eval(postbits[k]) < eval(minw)) minw=eval(postbits[k]);
if (eval(postbits[k]) > eval(maxw)) maxw=eval(postbits[k]);
if (eval(postbits[eval(1 + k)]) < eval(minh)) minh=eval(postbits[eval(1 + k)]);
if (eval(postbits[eval(1 + k)]) > eval(maxh)) maxh=eval(postbits[eval(1 + k)]);
}
}
widths.push(eval(maxw - minw));
heights.push(eval(maxh - minh));
tox.push(eval(minw) - 5);
toy.push(eval(minh) - 5);
//tox.push(Math.round(eval(minw)));
//if (eval(j) != 1) {
//toy.push(Math.round(eval(minh) + eval(heights[eval(-2 + j)] * 0 / 2)));
//} else {
//alert(minh); // + ' ' + heights[eval(-1 + j)] + ' ' + Math.round(eval(minh) - eval(heights[eval(-1 + j)])));
//toy.push(Math.round(eval(minh) - eval(heights[eval(-1 + j)] * 0)));
//alert(toy[eval(-1 + j)]);
//}
if (names.length < heights.length) names.push("");
//names[eval(-1 + j)]=names[eval(-1 + j)] + ' ' + tox[eval(-1 + j)] + ' ' + toy[eval(-1 + j)] + 'background:url(' + "'" + mapimg + "'" + '); background-position: -' + tox[eval(-1 + j)] + 'px -' + toy[eval(-1 + j)] + 'px; background-clip: inherit; ';
//alert(names[eval(-1 + j)]);
if (hrefbits.length > 1) href.push(' href="' + hrefbits[1].split('"')[0] + '" ');
if (onclickbits.length > 1) onclick.push(' onclick="' + onclickbits[1].split('"')[0] + '" ');
mybits='';
delim='';
for (k=0; k<postbits.length; k+=2) {
mybits+=delim + eval(postbits[k] - minw) + ',' + eval(postbits[eval(1 + k)] - minh);
delim=',';
}
rawhtml=rawhtml.replace(bits[j].split('"')[0], mybits);
}
} else {
bits=rawhtml.split("coords='");
// if ((location.search.split('url=')[1] ? decodeURIComponent(location.search.split('url=')[1]).split('&')[0] : '') != '') alert('+' + bits.length);
for (j=1; j<bits.length; j++) {
postbits=bits[j].split("'")[0].split(',');
hrefbits=bits[j].split("href='");
onclickbits=bits[j].split("onclick='");
for (k=0; k<postbits.length; k+=2) {
if (k == 0) {
minw=eval(postbits[k]);
maxw=eval(postbits[k]);
minh=eval(postbits[eval(1 + k)]);
maxh=eval(postbits[eval(1 + k)]);
} else {
if (eval(postbits[k]) < eval(minw)) minw=eval(postbits[k]);
if (eval(postbits[k]) > eval(maxw)) maxw=eval(postbits[k]);
if (eval(postbits[eval(1 + k)]) < eval(minh)) minh=eval(postbits[eval(1 + k)]);
if (eval(postbits[eval(1 + k)]) > eval(maxh)) maxh=eval(postbits[eval(1 + k)]);
}
}
widths.push(eval(maxw - minw));
heights.push(eval(maxh - minh));
tox.push(eval(minw) - 5);
toy.push(eval(minh) - 5);
if (names.length < heights.length) names.push("");
if (hrefbits.length > 1) href.push(" href='" + hrefbits[1].split("'")[0] + "' ");
if (onclickbits.length > 1) onclick.push(" onclick='" + onclickbits[1].split("'")[0] + "' ");
mybits='';
delim='';
for (k=0; k<postbits.length; k+=2) {
mybits+=delim + eval(postbits[k] - minw) + ',' + eval(postbits[eval(1 + k)] - minh);
delim=',';
}
rawhtml=rawhtml.replace(bits[j].split('"')[0], mybits);
}
}
bits=rawhtml.replace('</MAP>','<map ').replace('<MAP ','<map ').replace('</map>','<map ').replace(/AREA /g,'area ').split('<map ');
if (bits.length >= 3) {
if (bits[1].indexOf('">') != -1) bits[1]=bits[1].replace('">', '" >');
tablehtml=('<table border=3 cellspacing=5 cellpadding=5 ' + bits[1].replace(/">/g,'"></polygon></svg></td></tr>').replace(/ \/>/g,'</polygon></svg></td></tr').replace(/\/area/g,'/polygon>/svg>/td></tr').replace(/area /g,'tr><td id=td1 ').replace(/coords=/g,'><svg xmlns="http://www.w3.org/2000/svg" height="height1" width="width1" ><polygon style="fill:transparent;stroke:purple;stroke-width:5;fill-rule:nonzero;opacity:0.4;" points=') + '</table>').replace('<tr','<tr><th id=myth style=text-align:left;>Legend' + (' for ' + filen + '</').replace(' for </','</') + 'th></tr><tr');
//alert(tablehtml + ' w.l=' + widths.length + ' n.l=' + names.length + ' tox.l=' + tox.length + ' toy.l=' + toy.length + ' tox.0=' + tox[0] + ' toy.0=' + toy[0]);
for (i=0; i<widths.length; i++) {
if (mapimg != '') tablehtml=tablehtml.replace('=td1 ', '=td1 style="background:url(' + "'" + mapimg + "'" + '); background-position: -' + Math.max(0,tox[i]) + 'px -' + Math.max(0,toy[i]) + 'px; " ');
tablehtml=tablehtml.replace('=td1 ', '=TD' + eval(1 + i) + ' ');
if (onclick.length > 0 && href.length > 0) {
//alert('<a target=_blank ' + onclick[i] + href[i] + '><svg');
tablehtml=tablehtml.replace('<svg', '<a target=_blank ' + onclick[i] + href[i] + '><SVG').replace('</svg>', '</SVG>' + names[i] + '</a>');
} else if (onclick.length > 0) {
//alert('<a target=_blank ' + onclick[i] + '><svg');
tablehtml=tablehtml.replace('<svg', '<a target=_blank ' + onclick[i] + '><SVG').replace('</svg>', '</SVG>' + names[i] + '</a>');
} else if (href.length > 0) {
//alert('<a target=_blank ' + href[i] + '><svg');
tablehtml=tablehtml.replace('<svg', '<a target=_blank ' + href[i] + '><SVG').replace('</svg>', '</SVG>' + names[i] + '</a>');
}
tablehtml=tablehtml.replace('height1', heights[i]).replace('width1', widths[i]);
// if (i == 0 || i == 1) alert(tablehtml + ' w.l=' + widths.length + ' n.l=' + names.length + ' tox.l=' + tox.length + ' toy.l=' + toy.length + ' tox.0=' + tox[0] + ' toy.0=' + toy[0]);
}
maphtml='<map ' + bits[1] + '</map>';
//if ((location.search.split('url=')[1] ? decodeURIComponent(location.search.split('url=')[1]).split('&')[0] : '') != '') document.getElementById('tarea').value = tablehtml;
document.getElementById('table_content').innerHTML = document.getElementById('forscripts').innerHTML + tablehtml.replace(/ shape="poly" /g,' ').replace(/ shape="rect" /g,' ').replace(/ shape="circle" /g,' ');
if (mapimg != '' && document.getElementById('aplace')) {
placeit(document.getElementById('aplace'));
}
//setTimeout(andthendo, 2000);
//document.getElementById('table_content').innerHTML = document.getElementById('forscripts').innerHTML + tablehtml.replace(/ shape="poly" /g,' ').replace(/ shape="rect" /g,' ').replace(/ shape="circle" /g,' ');
}
}
}
}

function andthendo() {
//document.getElementById('table_content').innerHTML = document.getElementById('forscripts').innerHTML + tablehtml.replace(/ shape="poly" /g,' ').replace(/ shape="rect" /g,' ').replace(/ shape="circle" /g,' ');
if (mapimg != '' && document.getElementById('aplace')) {
placeit(document.getElementById('aplace'));
}
}

function readBlob(opt_startByte, opt_stopByte) {

var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}

file = files[0];
filen = file.name;
var start = parseInt(opt_startByte) || 0;
var stop = parseInt(opt_stopByte) || file.size - 1;

var reader = new FileReader();

// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
document.getElementById('byte_content').textContent = evt.target.result;
document.getElementById('byte_range').textContent =
['Read bytes: ', start + 1, ' - ', stop + 1,
' of ', file.size, ' byte file'].join('');
rawhtml = evt.target.result;
checkins();
}
};

var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);
}



function check_if(iois) {
if (iois != null) {
var aconto = (iois.contentWindow || iois.contentDocument);
if (aconto != null) {
if (rawurl.indexOf('rjmprogramming.com.au') == -1 && xrawurl.indexOf('/~mkuulma/livingroom_corner.htm') == -1 && rawurl.indexOf('/~mkuulma/livingroom_corner.htm') == -1) {
//alert(97);
//if (rawurl.indexOf('/~mkuulma/livingroom_corner.htm') == -1) {
popup=aconto;
//alert(970);
crossdomainiframe();
//}
} else {
if (aconto.document) { aconto = aconto.document; }
if (aconto.body != null) {
rawhtml=aconto.body.innerHTML;
checkins();
//aconto.scrollTo(200, 200);
//document.getElementById('mytable').style.top='200px;'
//document.getElementById('mytable').style.left='200px;'
}
}
}
}
}

function ifdone(iois) {
if (iois != null) {
var aconto = (iois.contentWindow || iois.contentDocument);
if (aconto != null) {
if (aconto.document) { aconto = aconto.document; }
if (aconto.body != null) {
document.getElementById('table_content').innerHTML = document.getElementById('forscripts').innerHTML + tablehtml.replace(/ shape="poly" /g,' ').replace(/ shape="rect" /g,' ').replace(/ shape="circle" /g,' ');
}
}
}
}

</script>

</head>
<body onload="checkins(); " style="width:100%;overflow:scroll;">

<table id='mytable' style='width:100%;position:absolute;top:0;left:0;'><tr><td id='tdplacemap' style='vertical-align:top;width:600px;overflow:scroll;'><div id='placemap' style='overflow:scroll;width:600px;'></div><div id='lhs' style=background-color:yellow;width:600px;>
<h1>Legend of HTML Map Element</h1>
<h2>RJM Programming - September, 2016</h2>
<h3>Thanks to <a target=_blank title='HTML5 Rocks' href='http://www.html5rocks.com/en/tutorials/file/dndfiles/'>http://www.html5rocks.com/en/tutorials/file/dndfiles/</a></h3>

HTML File: <input style='background-color:orange;' type="file" id="files" name="file" />
<span class="readBytesButtons">
<button style='display:none;' data-startbyte="0" data-endbyte="4">1-5</button>
<button style='display:none;' data-startbyte="5" data-endbyte="14">6-15</button>
<button style='display:none;' data-startbyte="6" data-endbyte="7">7-8</button>
<button style='background-color:pink;'>Process File Legend for HTML Map Data</button>
</span>
<div style='display:none;' id="byte_range"></div>
<div style='display:none;' id="byte_content"></div>

<br>
<form action='./legend_via_map.htm' method='GET'>
HTML Url: <input type='text' id='url' name='url' value='' style='width:80%;'></input><br><br>
HTML Itself: <textarea rows=20 cols=76 name='tarea' id='tarea' value=''></textarea><br><br><br>
<input type='submit' name="submit" id="submit" value="Create Legend for HTML Map Data" style="display:inline;background-color:pink;"></input> Showing Labels <input type='checkbox' id='showlabel' name='showlabel' checked></input>
</form>
</div></td><td>
<div id="table_content"></div>
</td></tr></table>

<script type='text/javascript'>

document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
if (evt.target.tagName.toLowerCase() == 'button') {
var startByte = evt.target.getAttribute('data-startbyte');
var endByte = evt.target.getAttribute('data-endbyte');
readBlob(startByte, endByte);
}
}, false);

</script>

<div id='forscripts'>
</div>
<iframe id='myif' name='myif' onload='check_if(this);' src='' style='display:none;'></iframe>
</body>
</html>