/* Created by Martin Holmes (MDH) 2011-10-21.
    This file contains various utility functions used
    in the interface of the London Maps project.
*/

/* Store the original "clean" version of the search string before changing it. */
var origSearchString = '';
/* Might be useful to have the changed version too. */
var modSearchString = '';

function handleLongS(sender){
  var searchBox = document.getElementById('searchTerm');
  if (searchBox == null){return;}
  var s = searchBox.value.normalizeSpace();
  if (sender.checked){
  //We need to stash the original, and put the changed one back.
    origSearchString = s;
    modSearchString = sToQMark(s, false);
    searchBox.value = modSearchString;
  }
  else{
    var temp = sToQMark(s, true);
    searchBox.value = temp;
  }
}

/* This function swaps out medial "s" with a question mark. */
function sToQMark(s, reverse){
  if (reverse){
    if (s.length < 2) return s;
 //Just can't do this with regexps -- problems with word boundaries
 //being triggered by question marks, and double question marks being
 //a problem.
    var ret = s.charAt(0);

    for (var i=1; i<s.length-1; i++){
      if ((s.charAt(i) == '?') && (s.charAt(i-1) != ' ') && (s.charAt(i+1) != ' ')){
        ret += 's';
      }
      else{
        ret += s.charAt(i);
      }
    }
    ret += s.charAt(s.length-1);
    return ret;
  }
  else{
    return s.replace(/\Bs\B/g, '?');
  }
}

function checkSearchString(){
//We normalize the search string.
  var searchBox = document.getElementById('searchTerm');
  if (searchBox == null){return false;}
  var s = searchBox.value.normalizeSpace();
//We now check to see if the long-s checkbox is set. and 
//just in case it was set before the search text was typed, we
//re-run the algorithm to replace s with ?.
  var chkHandleLongS = document.getElementById('chkHandleLongS');
  if (chkHandleLongS != null){
    if (chkHandleLongS.checked){
      s = sToQMark(s, false);
    }
  }
  searchBox.value = s;
  
//We're going to allow leading wildcards for now, but if we need to 
//prevent them because such searches take too long, then we can remove
//the next line of code and allow the check to complete.
  return true;
  
  if ((s.match(/ [?*]/))||(s.match(/^[?*]/))){
    var wildcardWarning = document.getElementById('noWildcardsAtStart');
    if (wildcardWarning == null){
      alert('You cannot use wildcards (? and *) at the beginning of words.');
    }
    else{
      alert(wildcardWarning.innerHTML);
    }
    return false;
  }
  else{
    return true;
  }
}

/* Code for showing references, biblios etc. in a popup div. */
var infoPopup = null;
var infoContent = null;

function clearContent(targetEl){
  if (targetEl != null){
    targetEl.innerHTML = '';
 /*   for (var i=targetEl.childNodes.length-1; i>=0; i--){
       targetEl.removeChild(targetEl.childNodes[i]);
    }*/
  } 
}

function showReference(text, sourceId){
//Make sure we have an available popup element.
  infoPopup = document.getElementById('infoPopup');
  infoContent = document.getElementById('infoContent');
  if ((infoPopup == null)||(infoContent == null)){return;}
  
//Make sure we have a source element
  var sourceEl = document.getElementById(sourceId);
  if (sourceEl == null){return;}
 
//Clear the popup content div
  clearContent(infoContent);
  
//Copy the content from the source to the popup
    if (text.length > 0){
        infoContent.innerHTML = text + sourceEl.innerHTML;
    }
    else{
        infoContent.innerHTML = sourceEl.innerHTML;
    }
  
//Show the popup
  infoPopup.style.display = 'block';
}

function getVariantSpellings(waitingMessage){
  var searchTermEl = document.getElementById('searchTerm');
  if (searchTermEl == null){return;}
  var searchTerm = searchTermEl.value.normalizeSpace();
  if (searchTerm.length < 2){
    alert('Please type one or more place names into the search box.');
  }
  var targEl = document.getElementById('variantSpellings');
  if (targEl == null){return;}

  
  targEl.innerHTML = waitingMessage;
  targUrl = 'get_variant_spellings.xq?inputTerms=' + searchTerm;
  jx.load(targUrl, showVariants);
  
}

function showVariants(result){
  var targEl = document.getElementById('variantSpellings');
  if (targEl == null){return;}
  targEl.innerHTML = result;
}

/* Handy regex functions from Alex Vassiliev. */
String.prototype.trim = function() {
// Strip leading and trailing white-space
  return this.replace(/^\s*|\s*$/g, "");
}

String.prototype.normalizeSpace = function() {
// Replace repeated spaces, newlines and tabs with a single space
  return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}

