﻿/*
This file should be a general repository of javascripts that are useful
*/

/*
    This function is designed to take the focus from the input item
    (text box)
    and put it on the destination item if the source item is the correct length
    (also passed in).  This should be called from onKeyPress for a textbox
    so that it will work in IE and in FF
*/
function transferFocus(ItemToCheck, MaxLength, Destination, evt)
{    
   //Safari sucks even worse than IE (I know, you didn't think
   //it possible) and has a problem with this entire script
   //so only run it if the browser isn't Safari
    var agt=navigator.userAgent.toLowerCase();
    
    if (agt.indexOf("safari") == -1)
    {
       
        var TextLength = MaxLength - 1;

        //IE Sucks and handles things differently so
        //we have to check to see if they are using it
        if (navigator.appName == "Microsoft Internet Explorer") 
        {
            //Detect IE5.5+
            version=0
            if (navigator.appVersion.indexOf("MSIE")!=-1)
            {
                temp=navigator.appVersion.split("MSIE")
                version=parseFloat(temp[1])
            }
            
            if (version>=5.5) //NON IE browser will return 0
            {
             TextLength = MaxLength;

            }
        } 
        
        
        if ((document.getElementById(ItemToCheck).value.length ) == (TextLength))
        {
            //Set focus to the new item

            document.getElementById(Destination).focus();
            document.getElementById(Destination).select();
        }
    }
    
}

/*
    This function is designed to take the focus from the input item
    (text box)
*/
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
    input.form[(getIndex(input)+1) % input.form.length].select();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

    //Get the index of the control
  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

/*
    This function is used to limit the number of characters that can be enterd by a user in a 
    Multiline text box. The max length property of the Multiline text box does not work here
    because the browser renders it as a Text Area which does not have max length attribute.
    
    This function will also show the number of characters that the user can still enter and cuts
    off excess characters automatically (both when entered by teh user and when cut and paste)
    and works in all latest browsers (IE, FF, Safari etc)
    
    This can be customized based on whether you want to cut the excess characters or not and to
    set the max limit to the one you desire.
    
    It takes 3 parameters
        1. The control you want to validate (ex: txtComment from Usage section below)
        2. The control you want to display the remaining character count (ex: remLen from Usage section below)
            which can be a normal text box or an input type of text
        3. The max allowed length
        
    Usage:
    1. Add text box to the form and set its TextMode property to "Multiline"
        and then add the three attributes to it - onkeydown, onkeyup, onfocus
    <asp:TextBox ID="txtComment" runat="server" MaxLength="300" Height="80px" TextMode="MultiLine" Width="256px" 
                    onkeydown="textCounter(this,remLen,795);"
                    onkeyup="textCounter(this,remLen,795);" 
                    onfocus="textCounter(this,remLen,795);">
                    <br />
    <input readonly="readonly" type="text" name="remLen" id="remLen" size="2" maxlength="3" value="795" style="border: 0px; text-align:right; color:red;" />
     <span style="color:Red;">characters left out of maximum 795.</span>

    <!-- Original: Ronnie T. Moore -->
    <!-- Web Site: The JavaScript Source -->

    <!-- Dynamic 'fix' by: Nannette Thacker -->
    <!-- Web Site: http://www.shiningstar.net -->
     
*/

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}