﻿function after(e)
{
/*Grabbing the key pressed*/
    var unicode;
    try
    {   unicode = e.keyCode; }
    catch(err)
    {   try{unicode = event.keyCode;}
        catch(error)
        { unicode=e.which;}
    }

/*  Grabbing the element that triggered the event*/
   var who;
   if(e)
   {    who=e.target;
        if(!who)
        { who=event.srcElement;}
   }
   else
   {
        who=event.srcElement;
   } 
   
/* Does nothing if Tab, Shift, Up, or Down are pressed */
    if(unicode == 9 || unicode == 16 || unicode == 40 || unicode == 38 || unicode == 192) {return true;} 
    
/*Grabbing all the input elements on the page */
    var AllElements=document.getElementsByTagName('input');
    var L=AllElements.length;	
    var group=new Array(L);
    var j=0;
/* looping through all of the input elements and grabbing only those with "Serial" in their ID
   and also have a max field length that's greater than 1 to use for processing*/
    for(var i = 0; i < L; i++){
        var temp = AllElements[i];     
        if(temp.id.indexOf("Serial")>0 && temp.disabled==false)
        {   group[j]=temp; 
            j=j+1; 
        }
    }
    //setting L to the length of the group array, which is the one we'll be working with.
    L=group.length;
  
/*sets the global variable to keep track of the cursor position*/
    globalCursorPos = getCursorPos(who);
   
    if(globalCursorPos == 0 && who.value.length>0)
    {  //we need to check to see if we actually reached position 0.
       //position 0 is a valid place to have the cursor and we don't
       //want to move it before we have reached this valid place.
       if(document.getElementById('atBeginning').value=="False")
       { document.getElementById('atBeginning').value="True";}
       else{   globalCursorPos=-1;}
     }
     else if(who.value.length<=0)
     {  if(unicode==8)
        {   //if backspace was pressed and there was nothing in the text box
            //we need to do the following, because backspace deletes
            //the character before it reaches this script.
            if(document.getElementById('atBeginning').value=="False")
            { document.getElementById('atBeginning').value="True";
            }
            else{globalCursorPos=-1; }
        }
        else
        {   //if backspace wasn't pressed, we want to move backwards.
            globalCursorPos=-1;  
        }
     }
     else
     {  document.getElementById('atBeginning').value="False";}
     
/*This is the case when the left arrow key or backspace is pressed and we are at the
  beginning of the text field*/
     if((unicode == 37 || unicode == 8) && globalCursorPos == -1)
     { 
        for(var i = 0; i < L; i++)
        {
            temp = group[i];
            try
            {   if(temp == who && group[i-1]) 
                {  
                    i--;
                    group[i].focus();
                    
                    //if backspace was pressed and we're changing the focus, we need to remove 
                    //the last character of the string
                    if(unicode == 8)
                    {   group[i].value= group[i].value.substr(0,group[i].value.length-1);}
                    
                    //placing the cursor at the end of the text field
                    var resetRange = group[i].createTextRange(); 
                    resetRange.move("character", group[i].value.length); 
                    resetRange.select();
                    globalCursorPos=group[i].value.length;
                    
                    return ;
                 }  
            }
            catch(error)
            {   return;}
        }
        return;
     }
/* if the right arrow key was pressed and we're at the end of the text field*/
     if(unicode == 39 && globalCursorPos == who.maxLength)
     {
        for(var i = 0; i < L; i++)
        {
            temp = group[i];
            try
            {   if(temp == who && group[i+1]) 
                {   document.getElementById('atBeginning').value="True";
                    return group[++i].focus();  
                }
            }
            catch(error)
            {   return;}
        }
        return;
     }
    	
 /*if any other character was pressed and the textfield is at its max length
    we need to move to the next text field.*/
   if(globalCursorPos == who.maxLength)
   {    /* Looping through all the elements and checking to see if they are the
        one who called triggered this function, and if so sets the focus to
        the next element if one exists */
        for(var i = 0; i < L; i++){
            temp = group[i];
            try
            {
                if(temp == who && group[i+1])
                {   ++i
                   // group[i].select();
                     document.getElementById('atBeginning').value="True";
                    return group[i].focus();
                }                
            }
            catch(error)
            {  return;}
        }
    }
}//end of function


var globalCursorPos; // global variable to keep track of where the cursor was

function getCursorPos(textElement)
{   
    if(document.selection)//Internet Explorer  
    {   //Since we need to insert a character into the text box where the cursor is,
        //then search for this charcter later, we'll use the varaible "text" and have set it to
        //a value that is not normally found in French, English, or Spanish (don't think you can find it on any keyboard)
        var text="₧";
        var range = document.selection.createRange();
        // Save the current value. We will need this value later to find out, where the text has been changed 
        var orig = textElement.value.replace(/rn/g, "n"); 
        //Increasing the maxLength of the text element by one in order to insert the text
        textElement.maxLength++; 
        // Insert the text variable into the textbox
        range.text = text; 
         
        //Now get the new content (w/text inserted) and save it into a temporary variable
         var actual = tmp = textElement.value.replace(/rn/g, "n"); 
         
         //Find the first occurance, where the original differs from the actual content, and that will be the cursor position
         var cursorPos = 0;
         for(var diff = 0; diff < orig.length; diff++) 
        {  
            if(orig.charAt(diff) != actual.charAt(diff))
            { break; }
         } 
        cursorPos=diff;
        
        //Setting the text in the text field back to what it was originally,resetting the text field's maxLength and
        //moving the cursor back to where it was originally        
        textElement.value=orig;
        textElement.maxLength--;
        var resetRange = textElement.createTextRange(); 
        resetRange.move("character", diff); 
        resetRange.select(); 
        
        return cursorPos;
    }
    else if (textElement.selectionStart || textElement.selectionStart == '0')
    {
        //var startPos = textElement.selectionStart;
        //var endPos = textElement.selectionEnd;
        //alert(textElement.value.substr(0,startPos)+ "|" +textElement.value.substr(endPos,textElement.value.length));
        return textElement.selectionStart;
    }  
}
