//  Slider Control for Web Forms
//     This is a collection of three JavaScript functions which generate
//    the HTML and JavaScript for graphical slider controls.
//
//     For explanation of how this works, go to http://www.webacademy.com/
//    and click on the link referencing "Slider Controls for Web Forms". 
// 
//  gen_slider()
//  This function creates a slider control for entering numbers in a form
//  The arguments are
//       field  - the name of the text field being generated
//         min  - the minimum value for the field
//         max  - the maximum value for the field
//        init  - the initial (default) value
//      hidden  - if present, this argument is a Boolean (true/false)
//                   value to indicate whether the text field next to
//                   the slider control should be omitted (hidden)
//   form_name  - if present, the name of the form in which the slider is
//                   located.  If not supplied (or if an empty string is
//                   supplied, the name is assumed to be "forms[0]".        
//   image_dir  - if present, a directory or partial URL indicating the
//                   location of the five images used in the slider
//                   (s_left.gif, s_track.gif, s_slider.gif, s_right.gif
//                   and s_slidedown.gif)
// 
//   This function returns true if it successfully created a slider.  It
//     returns false if the browser did not support JavaScript image
//     replacement.  In this case a select field is generated, instead.
// 
//  Example:
//              genslider("Rating", 0, 10, 5, true, "f1", "/Images/")
//          would create a text field named "Rating" which would allow
//          the user to select a value between 0 and 10 with 5 as the
//          initial value.  The text field would be hidden.  The form's
//          name is "f1" and the four images are taken from the "/Images/"
//          directory.
//
//  To use these functions, you *must* include the copyright notice below,
//    but you can omit the instructions above.
//
//  Copyright (c) 1998, The Web Academy, Inc.    Author: Dave Elliott
//          All rights reserved.  Permission is granted to copy and
//          modify this code provided that this copyright notice be
//          included in its entirety.  This code may not be sold.
//  
var sliderfree=new Array(40);  
function gen_slider(field, min, max, init){
   var hidden=false; var form_name='forms[0]'; var image_dir='';
   if (gen_slider.arguments.length>6)
      image_dir=gen_slider.arguments[6];
   if (gen_slider.arguments.length>5) 
      if (gen_slider.arguments[5].length>0)
         form_name=gen_slider.arguments[5]; 
   if (gen_slider.arguments.length>4)
      hidden=gen_slider.arguments[4];
   if (!document.images) {
      with (document){
        writeln('<select name="'+field+'">');
        for (var i=min; i<=max; i++){
           write('<option ');
           if (i==init) write('selected');
           writeln('>'+i);
           }
        writeln('</select>');
        }
      return false;
      }
   sliderfree[field+form_name]=false;
   with (document){
     writeln('<nobr><img src="'+image_dir+'s_left.gif"><a\n');
     for (var i=min; i<=max; i++) {
        if (document.all)
           write('nohref onclick="clickslide(');
        else 
           write('href="javascript:;" onclick="clickslide(');
        write("'"+field+"',");
        write("'"+form_name+"'"+','+i+','+min+','+max+',');
        write("'"+image_dir+"'"+');return false" onmouseover="slide(');
        write("'"+field+"','"+form_name+"'"+','+i+','+min+','+max+',');
        write("'"+image_dir+"'"+')"><img src="'+image_dir);
        if (i==init) write('s_slidedown.gif" alt="Click to unlock"')
        else write('s_track.gif"');
        write(' border=0 name="i_'+field+i+'"></a><a\n');
        }
     writeln('></a><img src="'+image_dir+'s_right.gif"></nobr>'); 
     write('<input type=');
     if (hidden) write('hidden')
     else write('text');
     writeln(' name='+field+' size=3 value=" '+init+'">');
     }
   var s_Img=new Image();
   s_Img.src=image_dir+'s_slider.gif';
   return true;
   } 
     
function slide(field,form_name,val,min,max,image_dir){
   if (sliderfree[field+form_name]){
      var x=eval('document.'+form_name+'.'+field+'.value');
      x=parseInt(x);
      document.images["i_"+field+x].src=image_dir+'s_track.gif';
      document.images["i_"+field+x].alt='';
      document.images["i_"+field+val].src=image_dir+'s_slider.gif';
      document.images["i_"+field+val].alt='Click to lock';
      eval('document.'+form_name+'.'+field+'.value=" "+'+val);
      }
   }

function clickslide(field,form_name,val,min,max,image_dir){
   if (document.images["i_"+field+val].src.indexOf('s_slide')>=0){
      if (sliderfree[field+form_name]){
        document.images["i_"+field+val].src=image_dir+'s_slidedown.gif';
        document.images["i_"+field+val].alt='Click to unlock';
        }
      else{
        document.images["i_"+field+val].src=image_dir+'s_slider.gif';
        document.images["i_"+field+val].alt='Click to lock';
        }
      sliderfree[field+form_name]= !sliderfree[field+form_name]
      }
   }
