<?php
if (!defined('IS_DEDI')) die();
include_once($cfg_dedi['dedi_path'].'inc/inc.init_external.php'); ?>
<HTML id=dlgFind STYLE="width: 385px; height: 165px; ">
<HEAD>
 <?php echo '<meta http-equiv="content-type" content="text/html; charset='.$lang_charset.'">'."\n"; ?>
<TITLE>Find & Replace</TITLE>
<style>
html, body, button, div, input, select, fieldset, td { font-family: MS Shell Dlg; font-size: 8pt;};
</style>
<script>
window.setInterval(checkInputString, 500);
opener = window.dialogArguments;
var editor_obj = opener.document.all["_" + opener.getGlobalVar('_editor_field') + "_editor"];
function checkInputString() {
// close when switched to textedit
   if (opener.getGlobalVar('_editor_field') == "_editor_disabled") {
      window.close();
   }
 }
</script>
<SCRIPT defer>
var TxtRange;
TxtRange = dialogArguments.document.selection.createRange();

function _CloseOnEsc() {
  if (event.keyCode == 27) { window.close(); return; }
}
window.onerror = HandleError

function HandleError(message, url, line) {
  var str = "An error has occurred in this dialog." + "\n\n"
  + "Error: " + line + "\n" + message;
	  alert(str);
	  window.close();
	  return true;
}

// event handlers
function Init() {document.body.onkeypress = _CloseOnEsc;}

//Match Case and Whole Words
function matchtype(){
    var retval = 0;
    var matchcase = 0;
    var matchword = 0;
    if (document.formSearch.MatchCase.checked) matchcase = 4;
    if (document.formSearch.MatchWholeWord.checked) matchword = 2;
    retval = matchcase + matchword;
    return(retval);
}

//Checks for Find field not empty
function checkInputString(){
    if (document.formSearch.stringSearch.value.length < 1) {
        alert("<?php echo $dedi_lang['wys_pop_find_noText'] ?>");
        return false;
    } else {
        return true;
    }
}

// Finds the entered text
function FindTxt(){
    if (checkInputString()) {
        var searchval = document.formSearch.stringSearch.value;
        TxtRange.collapse(false);
        if (TxtRange.findText(searchval, 1000000000, matchtype())) {TxtRange.select();}
			else {
            var startAgain = confirm("<?php echo $dedi_lang['wys_pop_find_finish'] ?>");
            if (startAgain) {
                TxtRange.expand("textedit");
                TxtRange.collapse();
                TxtRange.select();
                FindTxt();
            }
        }
    }
}

// replaces the first found instance of the entered textwith the desired text
function ReplaceTxt(){
    if (checkInputString()) {
        if (document.formSearch.MatchCase.checked){
            if (TxtRange.text == document.formSearch.stringSearch.value) TxtRange.text = document.formSearch.stringReplace.value
        }
		else {
            if (TxtRange.text.toLowerCase() == document.formSearch.stringSearch.value.toLowerCase())
			TxtRange.text = document.formSearch.stringReplace.value
			}
        	FindTxt();
    	}
	}

// replaces all instances of the entered text with the desired text
function ReplaceAllTxt(){
    if (checkInputString()) {
        var searchval = document.formSearch.stringSearch.value;
        var wordcount = 0;
        var msg = "";
        TxtRange.expand("textedit");
        TxtRange.collapse();
        TxtRange.select();
        while (TxtRange.findText(searchval, 1000000000, matchtype())){
            TxtRange.select();
            TxtRange.text = document.formSearch.stringReplace.value;
            wordcount++;
        }
        if (wordcount == 0) msg = "<?php echo $dedi_lang['wys_pop_find_notfound'] ?>"
        else msg = wordcount + " <?php echo $dedi_lang['wys_pop_find_repl'] ?>";
        alert(msg);
    }
}
</script>

</HEAD>
<BODY id=bdy onload="Init()" style="background: threedface; color: windowtext;" scroll=no>
<FORM NAME="formSearch" method="post" action="">
<TABLE CELLSPACING="0" cellpadding="5" border="0">
<TR><TD VALIGN="top" align="left" nowrap>
<label for="stringSearch"><?php echo $dedi_lang['wys_pop_Search'] ?>:</label><br>
<INPUT TYPE=TEXT SIZE=40 NAME=stringSearch id="stringSearch" style="width : 280px; height: 22px;"><br>
<label for="stringReplace"><?php echo $dedi_lang['wys_pop_Replace'] ?>:</label><br>
<INPUT TYPE=TEXT SIZE=40 NAME=stringReplace id="stringReplace" style="width : 280px; height: 22px;"><br>
<INPUT TYPE=Checkbox SIZE=40 NAME=MatchWholeWord ID="MatchWholeWord"><label for="MatchWholeWord"><?php echo $dedi_lang['wys_pop_WholeWord'] ?></label><br>
<INPUT TYPE=Checkbox SIZE=40 NAME=MatchCase ID="MatchCase"><label for="MatchCase"><?php echo $dedi_lang['wys_pop_MatchCase'] ?></label>
</td>
<td rowspan="2" valign="top">
<button name="Find" style="width:75px; height:22px;  margin-top:15px" onClick="FindTxt();"><?php echo $dedi_lang['wys_pop_Next'] ?></button><br>
<button name="Replace" style="width:75px; height:22px;  margin-top:7px" onClick="ReplaceTxt();"><?php echo $dedi_lang['wys_pop_Replace'] ?></button><br>
<button name="Replaceall" style="width:75px; height:22px;  margin-top:7px" onClick="ReplaceAllTxt();"><?php echo $dedi_lang['wys_pop_ReplaceAll'] ?></button><br>
<button name="Cancel" style="width:75px; height:22px;  margin-top:7px" onClick="window.close();"><?php echo $dedi_lang['wys_pop_Close'] ?></button><br>
</td>
</tr>
</table>
</FORM>
</BODY>
</HTML>