title: Js & Jquery:Understanding a search code with JSON request
body: <p>i have a js search in my page that i don't get perfectly how does work because i don't know 100% js and jquery. As far as i think the code takes the input and search match with a link to a database that returns a JSON value depending on what name you put on the link (?name="the-input-name-here"), then, the code parse the json and determinates if the name of the input it's a valid surname and if it is the check if it has a running page, if it has redirects you to that page. If the input is a valid surname but doesn't have a running page it redirects you to "landing-page-yes.html". If the input isn't a valid surname it redirects you to "landing-page-no.html". </p>
  
  <p>I need help to understand how the code does this in order to make a simplify version. How that call to another url database is parsed by the js ? How can i think something similar with a backend and ajax ? I need to understand 100% what this code does and i'm kinda lost.</p>
  
  <p>THANKS !</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;input id="srchid" width="100" onkeypress="submitonenter(document.getElementById('srchid').value, event, this)" /&gt;
  
  &lt;input onclick="nameCheck(document.getElementById('srchid').value);" value="CLICK HERE" type="button" style="background-color:#990033; color:#fff;border-style:outset;"&gt;
  
  &lt;div id="nameresults"&gt;&lt;/div&gt;
  
  
  &lt;script &gt;
  &lt;!--
   
  Array.prototype.contains = function(obj) {
      var i = this.length;
      while (i--) {   
          if (this[i] === obj) {
              return true;   
          } 
      }   return false;
  }
   
  function cursor_wait() {
    document.body.style.cursor = 'wait';
  }
   
  // Returns the cursor to the default pointer
  function cursor_clear() {
    document.body.style.cursor = 'default';
  }
   
  function nameCheck(sName) {   
      sName = trim(sName);
      if(sName == ""){
          alert("Please enter a name!");
          return false;
      }
      cursor_wait();   
      routeToNameLookup(sName);
      cursor_clear();
  }
   
  function $(id){return document.getElementById(id);}
   
  // Get JSONP
  function getJSON(url){
    var s = document.createElement('script');
    s.setAttribute('src',url);
    document.getElementsByTagName('head')[0].appendChild(s);
  }
   
  function testcb(data){
   //alert(data[0]);
  }
   
  function loaded(data) {
      var name = document.getElementById('srchid').value;
      var xmlhttp2;
     
      //Using innerHTML just once to avoid multi reflow
      $("nameresults").innerHTML = data[0];
      if($("nameresults").innerHTML == 1){
          if(data[1] == 1){
              //display name page with content
              var sNewName = name.replace (/'/g, ""); //remove any '
              sNewName = removeSpaces(sNewName);
              sNewName = convertNonAscii(sNewName);
              //redirect to name crest
              var sUrl = "http://www.heraldicjewelry.com/" + sNewName.toLowerCase() + "-crest-page.html";
              //getJSON("http://www.gohapp.com/updatenamesearch.php?id=" + data[2] + "&amp;pageurl=" + sUrl + "&amp;callback=testcb");
              //postwith(sUrl,{'pname':name});
              window.location=sUrl;
          } else {   
              //post to yes page
              //postwith("http://www.heraldicjewelry.com/landing-page-yes.html",{'pname':name});
                                                  window.location="http://www.heraldicjewelry.com/landing-page-yes.html";
          }
      } else {
          //post to no page
          //postwith("http://www.heraldicjewelry.com/landing-page-no.html",{'pname':name});
                                  window.location="http://www.heraldicjewelry.com/landing-page-no.html";
      }
      $("nameresults").innerHTML = "";
  }
   
  function routeToNameLookup(sSrchName) {
    var name = document.getElementById('srchid').value;
    if(sSrchName==""){
     alert("Please enter your family name.");
    } else {
        var rn=Math.floor(Math.random()*1000000000000001)
        getJSON("http://www.gohapp.com/namesearch_new.php?name="+name+"&amp;rec=1&amp;callback=loaded&amp;rn="+rn);
    }
  }
   
  function trim (sStr) {
      var str = sStr.replace(/^\s+/, '');
      for (var i = str.length - 1; i &gt;= 0; i--) {
          if (/\S/.test(str.charAt(i))) {
              str = str.substring(0, i + 1);
              break;
          }
      }
      return str;
  }
   
  function postwith (to,p) {
    var myForm = document.createElement("form");
    myForm.method="post" ;
    myForm.action = to ;
    for (var k in p) {
      var myInput = document.createElement("input") ;
      myInput.setAttribute("name", k) ;
      myInput.setAttribute("value", p[k]);
      myForm.appendChild(myInput) ;
    }
    document.body.appendChild(myForm) ;
    myForm.submit() ;
    document.body.removeChild(myForm) ;
  }
   
  function removeSpaces(string) {
   return string.split(' ').join('');
  }
   
  var PLAIN_ASCII =
      "AaEeIiOoUu" // grave
    + "AaEeIiOoUuYy" // acute
    + "AaEeIiOoUuYy" // circumflex
    + "AaOoNn" // tilde
    + "AaEeIiOoUuYy" // umlaut
    + "Aa" // ring
    + "Cc" // cedilla
    + "OoUu" // double acute
  ;
   
  var UNICODE =
      "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9"
    + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD"
    + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177"
    + "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1"
    + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF"
    + "\u00C5\u00E5"
    + "\u00C7\u00E7"
    + "\u0150\u0151\u0170\u0171"
  ;
   
  // remove accentued from a string and replace with ascii equivalent
  function convertNonAscii(s) {
    if (s == null)
      return null;
    var sb = '';
    var n = s.length;
    for (var i = 0; i &lt; n; i++) {
      var c = s.charAt(i);
      var pos = UNICODE.indexOf(c);
      if (pos &gt; -1) {
        sb += PLAIN_ASCII.charAt(pos);
      } else {
        sb += c;
      }
    }
    return sb;
  }
   
  function submitonenter(name, evt,thisObj) {
          evt = (evt) ? evt : ((window.event) ? window.event : "")
          if (evt) {
              // process event here
              if ( evt.keyCode==13 || evt.which==13 ) {
                thisObj.blur();
                nameCheck(name);
                //alert("looking for " + name); 
              }
          }
      }
   
  //--&gt;
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>