/*   tools.js  version 1.1

  hide(ID)  Hide a section identified by ID  
  show(ID)  Show a section identified by ID

  The following 5 functions work on sets of three div/span sections with the IDs NAME_show, NAME_hide and NAME
  NAME identifies the main content (section to be hidden or shown)
  NAME_show identifies the link/button used to show the main content
  NAME_hide identifies the link/button used to hide the main content

  ExpandNC('NAME')  Expand without setting a cookie 
  Expand('NAME')   Expand and set a cookie to remember the state
  ContractNC('NAME')   Contract without setting a cookie
  Contract('NAME')    Contract and set a cookie to remember the state
  CheckMenu('NAME')  Expand or contract based on the state of the cookie.

***********************************************************************  

  auto_fill(file_name_field,file_field)
  Use as an onchange event or a form submit even to fill the name of the file selected in the form field 'file_field' into the text or hidden field 'file_name_field'

  scroll_to(id)  scroll the window to the section identified by id

***********************************************************************
  The following functions are used to validate form information.
  Inputs:
    group: a string used to identify the group of fields to be  validated.  In most cases, all fields form the same form will be added to the same group but in some cases multiple groups can be used to identify different sections of the same form.
    field: the form field object to be validated.
    message: the warning message to be displayed when the required condition is not met.
 
  CSS classes:
    requied-input: usually a yellow background to identify an invalid field.
    normal-input:  does not need to be defined, identifies a valid field.
   
  Functions: 
    add_required_field(group,field, message, required_length, string_match)
         Text field must be at least 'required_length' long and contain 'string_match' 
          string_match can be ('') to turn off the string matching requirement.  Use ('@') to validate an Email address
    add_required_select_field(group,field, message)
         A Select field must have any option selected other than the FIRST option
    add_file_upload_field(group,file_name_field,file_field)
         A FILE input (file_field) is added with the corresponding text or hidden field (file_name).
   
    check_required_fields(group)
         Validates the files in group, returns TRUE if all fields validated.
    check_file_upload_fields(group)
         For every file input added with add_file_upload_field(…), fill the file name field.
         This function returns TRUE if any file input within 'group' contains a file to be uploaded.

***************************************************
   The following functions can be used as the onchange event for any text input.
   input_box is the input object (usually 'this')
  
   zip_filter(input_box)    force text value into 55555 or 55555-5555 format
   numeric_filter(input_box) remove any non digit characters
   ucase_filter(input_box) make every character upper case
   lcase_filter(input_box) make every character lower case
   capitalized_filter(input_box) capitalize the first character of each word

*************************************************
    date_conv(st) 
      Converts the sting 'st' into a valid date in the format (mm/dd/yyyy)
      If 'st' is not a valid date a warning message is displayed and a blank string is returned.
      onChange="this.value=date_conv(this.value);"

*/

function fillName(fl_name, file_name_field){
  var pos=0;
  var name="";
  pos=fl_name.lastIndexOf('/');
  if(pos == -1){
    pos=fl_name.lastIndexOf('\\');
  }
  if(pos == -1){
    pos=fl_name.lastIndexOf(':');
  }  
  if((pos != -1)){
    name=fl_name.substring(pos+1, fl_name.length);
    file_name_field.value=name;
  }  
  file_name_field.value=file_name_field.value.replace(/[^a-zA-Z0-9_\.]/g,"_");
}
function auto_fill(file_name_field,file_field){
  if(file_name_field.value.length>3) fillName(file_name_field.value,file_name_field);
  else fillName(file_field.value,file_name_field);
}

function ExpandNC(ID){ hide(ID+"_show"); show(ID+"_hide");show(ID);}
function Expand(ID){
  hide(ID+"_show"); show(ID+"_hide");show(ID);
  SetCookie(ID,'on');
}
function ContractNC(ID){ hide(ID+"_hide"); show(ID+"_show");hide(ID); }
function Contract(ID){
  hide(ID+"_hide"); show(ID+"_show");hide(ID);
  DeleteCookie(ID);
}
function CheckMenu(ID){
  if(GetCookie(ID)=='on')ExpandNC(ID);
  else ContractNC(ID);
}

function hide(ID1){
  obj1=document.getElementById(ID1);
  if(obj1!=null){    obj1.style.visibility='hidden';obj1.style.position='absolute'; }

}
function show(ID1){
  obj2=document.getElementById(ID1);
  if(obj2!=null){ obj2.style.visibility=''; obj2.style.position=''; }
}

function scroll_to(id){
  pos=document.getElementById(id);
  var total_x=0;
  var total_y=0;
  while (pos!=null){
   total_x+=pos.offsetLeft;
   total_y+=pos.offsetTop;
   pos=pos.offsetParent;
  }
  alert(id+":"+total_x+":"+total_y);
  window.scroll(total_x,total_y); 
}

var expire_today = new Date();
var expire_year = new Date(expire_today.getTime()+365*24*60*60*1000);

function getCookieVal(offset){
  var endstr=document.cookie.indexOf(";", offset);
  if(endstr==-1){endstr=document.cookie.length;}
  return unescape(document.cookie.substring(offset,endstr));
}

function GetCookie(name){
  var arg=name+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  var j;
  while(i<clen){
    j=i+alen;
    if(document.cookie.substring(i,j)==arg){
      return getCookieVal(j);
    }
    i=document.cookie.indexOf(" ",i)+1;
    if(i==0)break;
  }
  return null;
}

function DeleteCookie(name,path,domain){
  if(GetCookie(name)){
    document.cookie=name+"="+
      ((path)?"; path="+path : "")+
      ((domain)?"; domain="+domain:"")+
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";      
  }
}

function SetCookie(name,value,expires,path,domain,secure){
  document.cookie=name+"="+escape(value)+"; expires="+
    ((expires)?expires.toGMTString():expire_year.toGMTString())+
    ((path)?"; path="+path : "")+
    ((domain)?"; domain="+domain:"")+
    ((secure)?"; secure":"");   
}


var required_fields =new Array();
var file_upload_fields=new Array();
function required_field_object(group,field,message,required_length,string_match,isSelectField){
  this.group=group;
  this.field=field;
  this.alert_message=message;
  this.required_length=required_length;
  this.string_match=string_match;
  this.isSelectField=isSelectField;
  return this;
}
function file_upload_field_object(group,name_field, file_field){
  this.group=group;
  this.file_field=file_field;
  this.name_field=name_field;
  return this;
}
function add_required_field(group,field, message, required_length, string_match){
  required_fields[required_fields.length] = new required_field_object(group,field, message, required_length, string_match,0);
}
function add_required_select_field(group,field, message){
  required_fields[required_fields.length] = new required_field_object(group,field, message,0,'',1);
}
function add_file_upload_field(group,file_name_field,file_field){
  file_upload_fields[file_upload_fields.length]=new file_upload_field_object(group,file_name_field,file_field);
}

function check_required_fields(group){
  first_field=-1;
  for(var i=0; i<required_fields.length; i++){
    if(required_fields[i].group==group){
      required_fields[i].field.className="normal-input";
      if(required_fields[i].isSelectField){
        if(required_fields[i].field.selectedIndex==0){
          required_fields[i].field.className="required-input";
          if(first_field<0)first_field=i;
        }
      }else{
        if( required_fields[i].field.value.length<required_fields[i].required_length
         || (required_fields[i].string_match!=""&&
              required_fields[i].field.value.indexOf(required_fields[i].string_match)<0) ){
           required_fields[i].field.className="required-input";
           if(first_field<0)first_field=i;
        }
      }
    }
  }
  if(first_field>=0){
    alert(required_fields[first_field].alert_message);
    required_fields[first_field].field.focus();
    return false;
  }else   return true;
}

function check_file_upload_fields(group){
  var upload=false;
  for(var i=0; i<file_upload_fields.length; i++){
    if(file_upload_fields[i].group==group&&file_upload_fields[i].file_field.value!=''){       
       auto_fill(file_upload_fields[i].name_field, file_upload_fields[i].file_field);    
       if(file_upload_fields[i].name_field.value.length>3) upload=true;
    }
  }
  return upload;
}

  /*
    returns whether a character is numeric or not
  */
function is_numeric(character){
  var digits="0123456789";
  if (digits.indexOf(character)!=-1) return true;
  else return false;
}
  /*
    remove everything except digits
  */
function strip_letters(mixed_text){
  var char_only="";
  for (var i=0;i<mixed_text.length;i++){
    if (is_numeric(mixed_text.charAt(i)))  char_only+=mixed_text.charAt(i);
  }
  return char_only;
}
  
  /*
    this is necessary because focus() is broken in NS 7.x from any of the event handlers.  
    it simply puts together a line of the focus and select calls for the given form element, 
    and then calls a setTimeout of 1/10th of a second.
  */
function focus_and_select(box){
    var full_field_path="document."+box.form.name+"."+box.name+".";
    setTimeout(full_field_path+"focus();"+full_field_path+"select();",100);
}
  
  /*
    force text value into (555) 555-5555 format
  */
function phone_filter(input_box, first,second, third, ext_char ){
  var prefix="";   var area_code="";   var first_three="";   var last_four="";  var extension="";    
  var phone_text=input_box.value.toLowerCase();
  var ext_pos=phone_text.indexOf("x");  // grab extension 
  if(ext_pos>=0){
    extension=strip_letters(phone_text.substr(ext_pos+1,phone_text.length));    
    phone_text=phone_text.substr(0,ext_pos);
  }
  phone_text=strip_letters(phone_text);
  if (phone_text.length==7||phone_text.length>=10){
    input_box.value="";
    last_four=phone_text.substr(phone_text.length-4,4);  //next, grab the last 4 digits       
    first_three=phone_text.substr(phone_text.length-7,3); //then, grab the first 3 digits

    //try for an area code
    if (phone_text.length>=10){
      area_code=phone_text.substr(phone_text.length-10,3);

      //last, look for anything at the beginning (country code, etc)
      if(phone_text.length>10){
        prefix=phone_text.substr(0,phone_text.length-10);
        input_box.value+=prefix+" ";
      }
      input_box.value+=first+area_code+second;
    }
    input_box.value+=first_three+third+last_four;
    if (extension!="") input_box.value+=ext_char+extension;

  }else{
    alert("Please enter a 7 or 10 digit phone number.");
    focus_and_select(input_box);
  }
} 
  
  /*
    force text value into 55555 or 55555-5555 format
  */
function zip_filter(input_box){       
  var zip_text=strip_letters(input_box.value);
  if (!(zip_text.length==5||zip_text.length==9)&&zip_text.length>0){      
    alert("Please enter a 5 or 9 digit zip code.");                  
    focus_and_select(input_box);
  }else{  
    if(zip_text.length==9) input_box.value=zip_text.substr(0,5)+"-"+zip_text.substr(5,4)   
    else input_box.value=zip_text
  }
}  

  /*
    Numeric
  */
function numeric_filter(input_box){     
  input_box.value=strip_letters(input_box.value);
}
  
         
  /*  
    make every character upper case
  */
function ucase_filter(input_box){
  input_box.value=input_box.value.toUpperCase();    
}
  
  /*
    make every character lower case
  */
function lcase_filter(input_box){
  input_box.value=input_box.value.toLowerCase();
}
  
  /*
    capitalize the first character of each word
  */
function capitalized_filter(input_box){
  var input_text="";
  var last_char_is_space=1;
  for (var i=0;i<input_box.value.length;i++){
    if(last_char_is_space) input_text+=input_box.value.charAt(i).toUpperCase();
    else input_text+=input_box.value.charAt(i).toLowerCase(); 
    if(input_box.value.charAt(i)==' ')last_char_is_space=1;
    else last_char_is_space=0;
  }
  input_box.value=input_text;
}

function date_conv(st){
  var invalid=false;
  var date_array = st.split("/");
  var month_st=date_array[0];
  var day_st=date_array[1];
  var year_st = date_array[2];
  if(year_st==null||month_st==null||day_st==null){
    alert("Date must be in the form mm/dd/yyyy");
	return "";
  }
  if(month_st.length>2){invalid=true;}
  if(day_st.length>2){invalid=true;}
  if(year_st.length>4){
    invalid=true;
  }
  if(year_st.length==2){
    if(year_st<50){
	  year_st=year_st*1+2000;
	}else{
	  year_st=year_st*1+1900;
	}
  }
  if((year_st>2200)||(year_st<1900)){
    invalid=true;
  }    
  var month_st=date_array[0];
  if((month_st>12)||(month_st<1)){
    invalid=true;
  }
  var day_st=date_array[1];
  if((day_st>31)||(day_st<1)){
    invalid=true;
  }
  if(invalid){
    alert("Date must be in the form mm/dd/yyyy");
	return "";
  }else{
    year_st=month_st+"/"+day_st+"/"+year_st;
    return year_st;
  }
}