<?php
require_once("localization.php"); 

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 $rma;
}

if (!isset($_GET['locale']) && strlen($_SERVER['QUERY_STRING']) == 0) {
    if (file_exists("ayt_" . str_replace(".", "_", server_remote_addr()) . ".tya")) {
      $suff=@file_get_contents("ayt_" . str_replace(".", "_", server_remote_addr()) . ".tya");
      header("Location: ./Ajax_yql_thesaurus.php" . $suff);
    } 
} else if (isset($_GET['locale'])) {
    file_put_contents("ayt_" . str_replace(".", "_", server_remote_addr()) . ".tya", "?locale=" . urldecode($_GET['locale']));
}

?>
<!doctype html>
<head>
<meta charset="utf-8">

<title><?php echo _("Thesaurus Search"); ?> <?php echo _("via"); ?> Yql - RJM Programming - 2014</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script type='text/javascript'>
window.onload = initAll;
var myxhr = false;
var url = "";
var dprefix = "http://www.thefreedictionary.com/";
var gt=null;
var mgt=null;
var iother=0;
var mifdone=0;

function miflater() {
 var qwt=document.getElementById('mydiv');
 if (qwt != null) {
 if (qwt.innerHTML.indexOf('>' + 'Similar Meaning' + '<') != -1) {
  qwt.innerHTML=qwt.innerHTML.replace(' ... ' + 'Thesaurus', ' ... ' + '<?php echo _("Thesaurus"); ?>').replace('>' + 'Similar Meaning' + '<', '>' + '<?php echo _("Similar Meaning"); ?> ' + '<').replace('>Opposite Meaning<', '>' + '<?php echo _("Opposite Meaning"); ?> ' + '<');
 } 
 }
 mgt=document.getElementById('mif');
 //alert(99);
 if (mgt != null) {
 //alert(98);
    var y = (mgt.contentWindow || mgt.contentDocument); 
 //alert(y.innerHTML);
    if (y.document) { y = y.document; } 
 //alert(y.body.innerHTML);
    if (y.body.innerHTML.indexOf("Similar Meaning") == -1) {
    setTimeout(miflater, 2000);
    } else {
    y.body.innerHTML=y.body.innerHTML.replace(' ... ' + 'Thesaurus', ' ... ' + '<?php echo _("Thesaurus"); ?>').replace('>' + 'Similar Meaning' + '<', '>' + '<?php echo _("Similar Meaning"); ?> ' + '<').replace('>Opposite Meaning<', '>' + '<?php echo _("Opposite Meaning"); ?> ' + '<');
    }
 //alert(95);
 }
 mgt=null;
 
}

function gtlook() {
 if (gt != null) {
    //var y = (gt.contentWindow || gt.contentDocument); 
    //if (y.document) { y = y.document; } 
    //alert(gt.document.body.innerHTML.length);
 }
 //gt.close();
 gt=null;
}


function onblurdo() {
 //url='./yql_thesaurus.php?language=' + document.getElementById('language').value + '&selname=' + this.value; 
 url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20thesaurus%20where%20key%3D%225r3uguAhHMoAQJIlShGL%22%20and%20word%3D%22" + document.getElementById('selname').value.replace(/ /g,'%20') + "%22%20and%20language%3D%22" + document.getElementById('language').value + "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
 if (document.getElementById('language').value == "fr_FR") dprefix = "http://www.systranet.com/dictionary/fr-en/";
 if (document.getElementById('language').value == "de_DE") dprefix = "http://www.dict.cc/?s=";
 if (document.getElementById('language').value == "es_ES") dprefix = "http://www.wordreference.com/es/en/translation.asp?spen=";
 if (document.getElementById('language').value == "it_IT") dprefix = "http://www.wordreference.com/iten/";
 //var omif=document.getElementById('mif'); 
 //omif.src = url; 
 //omif.style.display='block'; 
  if (myxhr) {
        //console.log("prehere");

      setTimeout(miflater, 2000);
    myxhr.onreadystatechange = setThesaurusWords;
    myxhr.open("GET", url, true);
    myxhr.send(null);
  } else {
    alert("Not able to create XMLHttpRequest ... sadly.");
  }
}

function onblurd2() {
 if (1 == 1) {
  var lng=document.getElementById('language').value; 
  var si2=document.getElementById('selnam2').value; 
  var ln2=document.getElementById('languag2').value;  
  var sis=document.getElementById('selname').value; 
  if (si2 != '') { 
    if (27 == 78) { setTimeout(gtlook, 5000); gt=window.open('https://translate.google.com/#' + ln2.substring(0,2) + '/' + lng.substring(0,2) + '/' + encodeURIComponent(si2), si2, 'left=100,top=100,width=100,height=100'); } 
    if (23==23) { 
      document.getElementById('mif').style.display='block'; 
      document.getElementById('mif').src='./yql_thesaurus.php?language=' + lng + '&languag2=' + ln2 + '&selname=&selnam2=' + si2; 
      setTimeout(miflater, 1000);
    } 
  }  
  if (sis != '') { window.open('./yql_thesaurus.php?language=' + lng + '&selname=' + sis, 'mif'); setTimeout(miflater, 1000); } 
 } else if (1 == 7) {
 //url='./yql_thesaurus.php?language=' + document.getElementById('language').value + '&selname=' + this.value; 
 url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20thesaurus%20where%20key%3D%225r3uguAhHMoAQJIlShGL%22%20and%20word%3D%22" + document.getElementById('selnam2').value.replace(/ /g,'%20') + "%22%20and%20language%3D%22" + document.getElementById('languag2').value + "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
 if (document.getElementById('languag2').value == "fr_FR") dprefix = "http://www.systranet.com/dictionary/fr-en/";
 if (document.getElementById('languag2').value == "de_DE") dprefix = "http://www.dict.cc/?s=";
 if (document.getElementById('languag2').value == "es_ES") dprefix = "http://www.wordreference.com/es/en/translation.asp?spen=";
 if (document.getElementById('languag2').value == "it_IT") dprefix = "http://www.wordreference.com/iten/";
 //var omif=document.getElementById('mif'); 
 //omif.src = url; 
 //omif.style.display='block'; 
  if (myxhr) {
        //console.log("prehere");

    myxhr.onreadystatechange = setThesaurusWords;
    myxhr.open("GET", url, true);
    myxhr.send(null);
  } else {
    alert('<?php echo _("Not able to create XMLHttpRequest ... sadly."); ?>');
  }
  }
}

function initAll() {
  document.getElementById("selname").onblur = onblurdo;
  document.getElementById("selnam2").onblur = onblurd2;
  
  
      if (window.XMLHttpRequest) {
        myxhr = new window.XMLHttpRequest;
    }
    else {
  try {
    myxhr = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      myxhr = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      myxhr = false;
    }
  }
    }
    checkget();
}

function setThesaurusWords() {
  var i, ii, myline, mywords, preword, mylist, endbit="", secgotest;
  if (myxhr.readyState == 4) {
    if (myxhr.status == 200) {
      //console.log("here");
      if (myxhr.responseText) {


 mylist = myxhr.responseText.split('"synonyms":"');
 
 secgotest = document.body.innerHTML.split("<br><div ");
 if (secgotest.length > 1) {
   document.body.innerHTML = secgotest[0];
 }
 
 endbit = endbit + "<br><div align='center' id='mydiv'><table style='border:8px blue solid;' border=5 cellspacing=8 cellpadding=8><thead><tr><th>Similar Meaning</th><th>Opposite Meaning</th></tr><tbody>";
 for (i=1; i<mylist.length; i++) {
   myline = mylist[i].split('"');
   mywords = myline[0].split("|");
   for (ii=0; ii<mywords.length; ii++) {
    preword = mywords[ii].split(" (");
    if (mywords[ii].indexOf(" (antonym)") !== -1) {
	  endbit = endbit + "<tr><td style='background-color:white;'></td><td style='background-color:white;'><a style='text-decoration:none;' target=_blank title='" + mywords[ii].replace(" (antonym)", "") + "' href='" + dprefix + preword[0].replace(" (antonym)", "") + "'>" + mywords[ii].replace(" (antonym)", "") + "</a></td></tr>";
    } else {
	  endbit = endbit + "<tr><td style='background-color:white;'><a style='text-decoration:none;' target=_blank title='" + mywords[ii].replace(" (antonym)", "") + "' href='" + dprefix + preword[0].replace(" (antonym)", "") + "'>" + mywords[ii].replace(" (antonym)", "") + "</a></td><td style='background-color:white;'></td></tr>";
	}
   }
 }
 endbit = endbit + "</tbody></table></div>";
 document.body.innerHTML = document.body.innerHTML + endbit;


      }      
    } else {
      //console.log("Problem: " + myxhr.status);
      alert(myxhr.status);
    }
  }
}

function checkget() {
 var loc = location.search.split('locale=')[1] ? location.search.split('locale=')[1].split('&')[0] : "";
 if (loc != "") {
   document.getElementById('language').value=loc;
 }
 var startwith = location.search.split('startwith=')[1] ? location.search.split('startwith=')[1].split('&')[0] : "";
 if (startwith != "") {
   var workedstr=decodeURIComponent(startwith);
   var wstr=workedstr.toLowerCase().replace("+", " ").replace("+", " ").replace("+", " ").replace("+", " ").replace("+", " ").replace("+", " ").replace("+", " ").replace("+", " ").replace("+", " ").replace("+", " ");
   document.getElementById('selname').value=wstr;
   //document.getElementById('myform').submit();
   //document.body.click();
   onblurdo();
 }
 document.getElementById('selname').focus();
}

</script>
</head>
<body align='center' style=" background-color: cyan;" onclick=" if (iother == 0) { var lng=document.getElementById('language').value; var si2=document.getElementById('selnam2').value; var ln2=document.getElementById('languag2').value;  var sis=document.getElementById('selname').value; if (si2 != '') { if (27 == 78) { setTimeout(gtlook, 5000); gt=window.open('https://translate.google.com/#' + ln2.substring(0,2) + '/' + lng.substring(0,2) + '/' + encodeURIComponent(si2), si2, 'left=100,top=100,width=100,height=100'); } if (23==23) {  window.open('./yql_thesaurus.php?language=' + lng + '&languag2=' + ln2 + '&selname=&selnam2=' + si2, 'mif'); setTimeout(miflater, 1000);  } }  if (si2 != '') { sis=document.getElementById('selname').value; } if (sis != '') { window.open('./yql_thesaurus.php?language=' + lng + '&selname=' + sis, 'mif');  setTimeout(miflater, 1000); } } iother=0; ">
<br><h1 align='center'>&nbsp;<?php echo _("Thesaurus Search"); ?></h1>
<div align='center'>
<form id="myform" method="POST" style=" background-color: lightgray;" action="#">
<br>&nbsp;&nbsp;&nbsp;&nbsp;<select onclick=" iother=1; var ii=1; " onchange=" location.href='./Ajax_yql_thesaurus.php?locale=' + this.value;  document.getElementById('span2').innerHTML=this.value.replace('es_ES','<?php echo _("Spanish Word"); ?>').replace('fr_FR','<?php echo _("French Word"); ?>').replace('de_DE','German Word').replace('en_US','<?php echo _("English Word"); ?>').replace('it_IT','<?php echo _("Italian Word"); ?>') + ' ' + document.getElementById('language').value.replace('es_ES','<?php echo _("to Translate to Spanish Word"); ?>').replace('fr_FR','<?php echo _("to Translate to French Word"); ?>').replace('de_DE','<?php echo _("to Translate to German Word"); ?>').replace('en_US','<?php echo _("to Translate to English Word"); ?>').replace('it_IT','<?php echo _("to Translate to Italian Word"); ?>') + ':';   " onclick=" iother=3; var i=1;" id="language" name="language"><option value="en_US"><?php echo _("English"); ?></option><option value="es_ES"><?php echo _("Spanish"); ?></option><option value="de_DE"><?php echo _("German"); ?></option><option value="fr_FR"><?php echo _("French"); ?></option><option value="it_IT"><?php echo _("Italian"); ?></option></select>&nbsp;<i><?php echo _("Thesaurus"); ?></i> <?php echo _("Search"); ?>:&nbsp;<input style=" background-color: yellow;" type="text" onclick=" iother=5; this.value='';  document.getElementById('selnam2').value='';  document.getElementById('aselname').innerHTML=''; "  name="selname" id="selname" onblur=" onblurdo(); " />&nbsp;<a target=_blank href="#" title="lookup" id="aselname"></a><br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;<select onclick=" iother=2; var ii=1; " onchange=" document.getElementById('span2').style.display='inline';  document.getElementById('selnam2').style.display='inline';  document.getElementById('span2').innerHTML=this.value.replace('es_ES','<?php echo _("Spanish Word"); ?>').replace('fr_FR','<?php echo _("French Word"); ?>').replace('de_DE','German Word').replace('en_US','<?php echo _("English Word"); ?>').replace('it_IT','<?php echo _("Italian Word"); ?>') + ' ' + document.getElementById('language').value.replace('es_ES','<?php echo _("to Translate to Spanish Word"); ?>').replace('fr_FR','<?php echo _("to Translate to French Word"); ?>').replace('de_DE','<?php echo _("to Translate to German Word"); ?>').replace('en_US','<?php echo _("to Translate to English Word"); ?>').replace('it_IT','<?php echo _("to Translate to Italian Word"); ?>') + ':'; document.getElementById('selnam2').focus(); " onclick="iother=4; var i=1;" id="languag2" name="languag2"><option value=""><?php echo _("But know the word in ..."); ?></option><option value="en_US"><?php echo _("English"); ?></option><option value="es_ES"><?php echo _("Spanish"); ?></option><option value="de_DE"><?php echo _("German"); ?></option><option value="fr_FR"><?php echo _("French"); ?></option><option value="it_IT"><?php echo _("Italian"); ?></option></select>&nbsp;<span id='span2' style='display:none;'><i><?php echo _("Thesaurus"); ?></i> <?php echo _("Search"); ?>:</span>&nbsp;<input style="display:none; background-color: yellow;" type="text" onclick=" iother=6;  this.value='';  document.getElementById('aselname').innerHTML=''; document.getElementById('selname').value=''; "  name="selnam2" id="selnam2" onblur=" onblurd2(); " /><br><br>
&nbsp;<p name="info" id="info">... <?php echo _("Results go below"); ?> ...</p><br><iframe onload="setTimeout(miflater,1000);" id="mif" src="javascript:false;" style="display:none; width:100%; height:900px; background-color: lightgreen;"></iframe>
</form>
</div>
<script>
var ua = navigator.userAgent; 
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua); 
if (isiPad) {
  document.getElementById('myform').innerHTML = document.getElementById('myform').innerHTML.replace('<?php echo _("best to tab or click/touch out"); ?>', '<?php echo _("best to finish with keyboard button, not Go button"); ?>');
}
</script>
</body>

</html>
