Another Jquery sample

satya - Mon Oct 01 2012 09:59:26 GMT-0400 (Eastern Daylight Time)

Here it is


<script>

var savedletters = "";
var curword = "";

function resetSavedLetters()
{
  savedletters = "";
}

function fsaveLetters(s)
{
  var i = s.indexOf('(');
  var j = s.indexOf(')');
  var letterNumbers = s.substring(i+1,j);
  
  var s1 = "";
  for (i=0;i<letterNumbers.length;i++)
  {
     var charIndex = letterNumbers.charAt(i);
     var nCharIndex = parseInt(charIndex);
     var c = s.charAt(nCharIndex-1);
     s1= s1.concat(c);
  }
  return s1;
}

function writeSavedLetters()
{
   var s = savedletters.toUpperCase();
   s = spacethem(s);
   s = "<p>" + s + "</p>";
   $("#savediv").html(s);
}

function cbExtractAndSaveLetters()
{
  var s = getWordField();
  var newsavedletters = fsaveLetters(s);
  //alert("nsl:" + newsavedletters);
  savedletters = savedletters.concat(newsavedletters);
  //alert("sl:" + savedletters);
  writeSavedLetters();
  resetword();
}

function checkfunction()
{
   var s = getWordField();
   if (invalidstring(s))
   {
      //empty string. continue
      return true;
   }
   //valid string
   var i = s.indexOf('(');
   if (i >= 0)
   {
       //this is a function
       cbExtractAndSaveLetters();
       //took care of it don't continue
       return false;
   }
   //valid string and no function
   //continue
   return true;
}

function invalidstring(s)
{
   return !validstring(s);
}
function validstring(s)
{
   if (s==null)
   {
     return false;
   }
   var s1 = $.trim(s);
   if (s1 == "")
   {
      return false;
   }
   return true;
}
function myaction()
{
  if (checkfunction()== false)
  {
     //function took care of it
     return;
  }
  if (curword == "")
  {
     fnewword();
     resetword();
     return;
  }
  //alert("hi");

  var curtext = $("#rundiv").html();

  var newtext = getWordField();
  newtext = $.trim(newtext);
  if (newtext == "")
  {
     alert('empty word');
     return;
  }
  newtext = newtext.toUpperCase();

  var rest = "";
  var len = newtext.length;
  var rcurword = curword;
  for (i=0;i<len;i++)
  {
     var c = newtext.charAt(i);
     rcurword = removeChar(rcurword,c);
  }

  rcurword = spacethem(rcurword);
  rcurword = rcurword.toUpperCase();
  //alert(rcurword);
  
  var spacer = "        ";
  newtext = newtext + spacer + "==>" + spacer + rcurword;
  //newtext = newtext.toUpperCase();
  newtext = "<p>" + newtext + "</p>";
  //alert(newtext);
  var finaltext = newtext + curtext;

  $("#rundiv").html(finaltext);
  resetword();
  
}

function resetword()
{
  setWordField("");
}
function testRemoveChar()
{
   var s = "012345678";
   var c = '3';
   alert(removeChar(s,c));
}

function removeChar(s, c)
{
   var i = s.indexOf(c);
   if (i == -1)
   {
     return s;
   }
   //it exists
   var front = s.substring(0,i);
   var back = s.substring(i+1);
   return front + back;
}
function spacethem(s)
{
  var ns = ""; 
  for (i=0; i<s.length; i++)
  {
     ns = ns.concat(s.charAt(i));
     ns = ns.concat(" ");
  }
  return ns;
}

function fnewword()
{
  var newword = getWordField();
  newword = $.trim(newword);
  if (newword == "")
  {
      alert('you need to type a new word');
      setWordFieldFocus();
      return;
  }
  newword=newword.toUpperCase();
  newword = spacethem(newword);
  curword = newword;
  $("#wordid").html("<p>" + newword + "</p>");
  resetword();
  resetprompt();
  setRealWordFieldFocus();
}

function setWordField(s)
{
 $("#word_field_id").val(s);
}
function getWordField()
{
 return $("#word_field_id").val();
} 

function setWordFieldFocus()
{
//   setTimeout(setRealWordFieldFocus(),2000);
 $("#word_field_id").focus();
 setWordField("");
}

function setRealWordFieldFocus()
{
 $("#word_field_id").focus();
 setWordField("");
}

function resetprompt()
{
  var msg1 = $("#msg1").html();
  $("#rundiv").html(msg1);
}

function lookupword(s)
{
  var url = "http://dictionary.reference.com/browse/" + s;
  window.open(url,"_blank");
}

function flookupword()
{
   var word = getWordField();
   word = $.trim(word);
   if (word == "")
   {
      alert("enter a full word and click this button");
      return;
   }
   lookupword(word);
}

var numOfItems=10;
var curItem = 1;
var divIdArray = new Array();
var targetdivid ="curpuzzleDivId";
var puzzleDivClass = "puzzle";

function test2()
{
  divIdArray = gatherDivIdsWithAClassname("puzzle");
  numOfItems = divIdArray.length;
  curItem = numOfItems;
  //alert(divIdArray);
  displayCurrent();
}

function gatherDivIdsWithAClassname(classname)
{
   var lDivIdArray = new Array();
    var divElementArray = $("." + classname);
    //alert(divElementArray.length);
    for(i=0;i<divElementArray.length;i++)
    {
        var curdiv = divElementArray[i];
        var curdivid = $(curdiv).attr("id");
        lDivIdArray[i]=curdivid;
    } 
    return lDivIdArray;
}

function displayCurrent()
{
    var divid = divIdArray[curItem-1];
    //alert(divid);
    var curdivcontent = $("#" + divid).html();
    $("#curpuzzleDivId").html(curdivcontent);
}

function gotoPrev()
{
   if (curItem > 1)
   {
      curItem = curItem - 1;
      return;
   }
   alert('List is at the begining');
}

function gotoNext()
{
   if (curItem < numOfItems)
   {
      curItem = curItem + 1;
      //alert(curItem);
      return;
   }
   alert('List is at the end');
}

function cbGotoNext()
{
   gotoNext();
   displayCurrent();
}

function cbGotoPrev()
{
   gotoPrev();
   displayCurrent();
}


//************************************
//*Boot strapping operation
//************************************
$(document).ready(testonload());

function testonload()
{
  setTimeout("test2()",500);
  setWordFieldFocus();
}

</script>