var ie4          = false;
var ie5          = false;
var ns4          = false;
var ns6          = false;
var mac          = false;
var running      = false;
var timerId      = null;
var delay        = 500;
var currentMenu  = null;
var previousMenu = null;
var pageId       = null;
var loaded       = false;
var testing      = false;
var allopened    = false;
var homepage     = false;
var dir_images   = 'images/';
var hide_form    = false; //hide form from dhtml

if(document.layers){origWidth=innerWidth;origHeight=innerHeight;onresize=function(){if(innerWidth!=origWidth||innerHeight!=origHeight)location.reload()}}else{onresize=function(){window.location.reload();}}
function SetFocus(fieldname,formname){if (formname == ''){ formname = 'form'; }if (eval("document."+formname+".elements[fieldname].value == ''")){ eval("document."+formname+".elements[fieldname].focus();"); }}
function Popup(title,filename,w,h,scroll,resizable,menubar){if (title == ''){ title = 'popup'; }if (menubar == 'menubar'){ menubar = 'yes'; }else{ menubar = 'no'; }if (scroll == 'scroll'){ scroll = 'yes'; }else{ scroll = 'no'; }if (resizable == 'lock'){ resizable = 'no'; }else{ resizable = 'yes'; }winOptions = eval("'width="+w+",height="+h+",toolbar=no,location=no,directories=no,status=no,menubar="+menubar+",scrollbars="+scroll+",resizable="+resizable+",copyhistory=no'");window.open(filename,title,winOptions);return;}
function safemail(name,domain,display,email_class) {displayed = (typeof(display) == "undefined" || display == "") ? name+"@"+domain : display;displayed_class = (typeof(email_class) == "undefined" || email_class == "") ? '' : ' class="'+email_class+'"';document.write('<a href=mailto:'+name+'@'+domain+displayed_class+'>'+displayed+'</a>');}
function StartTimer(event){running = true; timerId = setTimeout(event,delay);}
function StopTimer(){running = false; clearTimeout(timerId);}
function SetElementPosition(id,top,left){
  if (testing) { alert("SetElementPosition\nid = "+id+"\ntop = "+top+"\nleft = "+left); }
  if (ns4) {
    eval('document.'+id).top = top;
    eval('document.'+id).left = left;
  } else if (ns6) {
    document.getElementById(id).style.top = top;
    document.getElementById(id).style.left = left;
  } else {
    eval(id).style.top  = top;
    eval(id).style.left = left;
  }
}
function SetVisibility(id,flag) {
  if (ns4) {
    var str = (flag) ? 'show' : 'hide';
    eval("document."+id).visibility = str;
  } else if (ns6) {
    var str = (flag) ? 'visible' : 'hidden';
    document.getElementById(id).style.visibility = str;
  } else {
    var str = (flag) ? 'visible' : 'hidden';
    eval("document.all."+id).style.visibility = str;
  }
}
function getImageTop(imgID) {
  if (ns4) return eval(imgID).y;
  else return getRealTop(imgID);
}
function getImageLeft(imgID) {
  if (ns4) return eval(imgID).x
  else return getRealLeft(imgID);
}
function getRealTop(imgElem) {
  yPos = eval(imgElem).offsetTop;
  tempEl = eval(imgElem).offsetParent;
  while (tempEl != null) {
    yPos += tempEl.offsetTop;
    tempEl = tempEl.offsetParent;
  }
  return yPos;
}
function getRealLeft(imgObj) {
	xPos = eval(imgObj).offsetLeft;
	tempObj = eval(imgObj).offsetParent;
  while (tempObj != null){
    xPos   += tempObj.offsetLeft;
    tempObj = tempObj.offsetParent;
  }
	return xPos;
}
function getImageWidth(id) {
  if (ie4){
    return eval('document.all.'+id).style.pixelWidth;
  } else if (ns4) {
    return eval('document.'+id).width;
  } else {
    return document.getElementById(id).offsetWidth;
  }
}
function getElementWidth(id) {
  if      (ie4){ return eval('document.all.'+id).offsetWidth; }
  else if (ns4){ return eval('document.'+id).clip.width; }
  else         { return document.getElementById(id).offsetWidth; }
}
function getElementHeight(id) {
  if      (ie4){ return eval('document.all.'+id).offsetHeight; }
  else if (ns4){ return eval('document.'+id).clip.height; }
  else         { return document.getElementById(id).offsetHeight; }
}
function layerWrite(id,text) {
  if (ns4) {
    var lyr = document.layers[id].document;
    lyr.open();
    lyr.write(text);
    lyr.close();
  } else if (ns6) {
    rng = document.createRange();
    el = document.getElementById(id);
    rng.setStartBefore(el);
    htmlFrag = rng.createContextualFragment(text);
    while (el.hasChildNodes()) el.removeChild(el.lastChild);
    el.appendChild(htmlFrag);
  } else if (ie4 || ie5) {
    document.all[id].innerHTML = text;
  }
}
//////////////////////////////////////////
//////////////////////////////////////////
function Init(id) {
  SetBrowser();
  LoadImages();
  pageId = id;
  if (ie5 || ns6) { LoadMenus(); }
  loaded = true;
}
function SetBrowser() {
  agent = navigator.userAgent.toLowerCase();
  mac   = (agent.indexOf("mac")!=-1);
  ie4   = (document.all && !document.getElementById) ? true : false;
  ie5   = (document.all && document.getElementById)  ? true : false;
  ns4   = (document.layers)                          ? true : false;
  ns6   = (document.getElementById && !document.all) ? true : false;
  if (testing){ alert("agent: "+agent+"\nmac: "+mac+"\nie4: "+ie4+"\nie5: "+ie5+"\nns4: "+ns4+"\nns6: "+ns6+"\n"); }
}
function LoadImages() {
  var num_images = 13;
  NavOff = new Array(num_images);
  for (var i=0; i<num_images; i++){ NavOff[i] = new Image(); }
  NavOff[0].src = "images/nav_tbainfo_a.gif";
  NavOff[1].src = "images/nav_education_a.gif";
  NavOff[2].src = "images/nav_advocacylegal_a.gif";
  NavOff[3].src = "images/nav_products_a.gif";
  NavOff[4].src = "images/nav_insurance_a.gif";
  NavOff[5].src = "images/nav_convention_a.gif";
  NavOff[6].src = "images/nav_fraud_a.gif";
  NavOff[7].src = "images/nav_contact_a.gif";
  NavOff[8].src = "images/auxnav_vendors_a.gif";
  NavOff[9].src = "images/auxnav_foundation_a.gif";
  NavOff[10].src = "images/auxnav_events_a.gif";
  NavOff[11].src = "images/auxnav_jobbank_a.gif";
  NavOff[12].src = "images/auxnav_sitemap_a.gif";

  NavOn = new Array(num_images);
  for (var i=0; i<num_images; i++){ NavOn[i] = new Image(); }
  NavOn[0].src = "images/nav_tbainfo_b.gif";
  NavOn[1].src = "images/nav_education_b.gif";
  NavOn[2].src = "images/nav_advocacylegal_b.gif";
  NavOn[3].src = "images/nav_products_b.gif";
  NavOn[4].src = "images/nav_insurance_b.gif";
  NavOn[5].src = "images/nav_convention_b.gif";
  NavOn[6].src = "images/nav_fraud_b.gif";
  NavOn[7].src = "images/nav_contact_b.gif";
  NavOn[8].src = "images/auxnav_vendors_b.gif";
  NavOn[9].src = "images/auxnav_foundation_b.gif";
  NavOn[10].src = "images/auxnav_events_b.gif";
  NavOn[11].src = "images/auxnav_jobbank_b.gif";
  NavOn[12].src = "images/auxnav_sitemap_b.gif";
  
	ImagePlus      = new Image();
  ImagePlus.src  = dir_images+"icon_plus.gif";
  ImageMinus     = new Image();
  ImageMinus.src = dir_images+"icon_minus.gif";
}
function LoadMenus() {
  //menus
  for (var i=0; i<13; i++) {
    imgId    = 'nav'+i;
    menuId   = 'menu'+i+'Div';
    shadowId = 'menubg'+i+'Div';
    
    //get anchor image
    if (ns6){ anchorObj = eval('document.getElementById("'+imgId+'")'); }
    else    { anchorObj = eval('document.images["'+imgId+'"]'); }
    
    //set position
    t = getImageTop(anchorObj);
    l = getImageLeft(anchorObj);
    w = getImageWidth(imgId);
    
    //less top padding for 8 and 9
    menuTop  = (i < 8) ? t + 22 : t + 15;
    
    if (i == 7) { menuLeft = l - 105; }
    else { menuLeft = l + 2; }
    SetElementPosition(menuId,menuTop,menuLeft);
    
    //shadow
    menuWidth  = getElementWidth(menuId);
    menuHeight = getElementHeight(menuId);
    if (menuWidth > 1 && menuHeight > 1) {
      var tempstring,writestring;
      tempstring = '<img src="'+dir_images+'shadow.gif" width="'+menuWidth+'" height="'+menuHeight+'" alt="" border="0">';
      writestring =  (!(mac && (ie4 || ie5))) ? '<table cellpadding="0" cellspacing="0" border="0"><tr><td class="transparent">'+tempstring+'</td></tr></table>' : tempstring;
      layerWrite(shadowId,writestring);
      shadowTop  = menuTop + 4;
      shadowLeft = menuLeft + 4; 
      SetElementPosition(shadowId,shadowTop,shadowLeft);
    }
  }
}
function Show(id) {
  if (running) { StopTimer(); }
  currentMenu = id;
  if (currentMenu != previousMenu && previousMenu != null) { Hide(previousMenu); }
  eval('document.getElementById("nav'+id+'").src = NavOn['+id+'].src');
  //if (id < 10 && ((ie5 && !mac) || ns6)) {
  if (id < 10 && (ie5 || ns6)) {
    if (!homepage) { eval('document.getElementById("menu'+id+'Div").style.visibility = "visible"'); }
    if (!homepage) { eval('document.getElementById("menubg'+id+'Div").style.visibility = "visible"'); }
    
    //special case for education calendar page
    if (hide_form == 1 && ((id > 0 && id < 7) || id == 8 || id == 9)) { document.getElementById("hideFormDiv").style.visibility = "hidden"; }
    }
  previousMenu = currentMenu;
}
function Hide(id) {
  if (id == null) { return; }
  if (id != pageId){ eval('document.getElementById("nav'+id+'").src = NavOff['+id+'].src'); }
  //if (id < 10 && ((ie5 && !mac) || ns6)) {
  if (id < 10 && (ie5 || ns6)) {
    if (!homepage) { eval('document.getElementById("menu'+id+'Div").style.visibility = "hidden"'); }
    if (!homepage) { eval('document.getElementById("menubg'+id+'Div").style.visibility = "hidden"'); }

    //special case for education calendar page
    if (hide_form == 1 && ((id > 0 && id < 7) || id == 8 || id == 9)) { document.getElementById("hideFormDiv").style.visibility = "visible"; }
  }
}
//phone format code
var areacodeLength  = 3;
var firstThree      = 3;
var previousLength  = null;
var thisInput       = null;
var originalLength  = null;
var thisAreacode    = null;
var thisFirstThree  = null;
var thisLastFour    = null;
var phone_test      = false;
function autoFormat(input,type) {
  //lock out NS4
  if (!document.layers) {
    if (type == 'phone') {
      //var temp_selection = input.selectionStart;
      //alert("selectionStart=" + input.selectionStart);
      var addFirstParen   = false;
      var addSecondParen  = false;
      var addDash         = false;
      thisInput       = input.value.replace(/[. ()-\/]/gi,'');
      //if just '(', don't do anything
      if (input.value.length == 1 && input.value == '(') { previousLength = 1; return true;}
      //dont do anything on backspace
      else if (input.value.length >= previousLength) {
        thisAreacode    = thisInput.substr(0,3);
        thisFirstThree  = thisInput.substr(3,3);
        thisLastFour    = thisInput.substr(6,4);
        
        //add '('
        if (thisInput.length > 0) { addFirstParen = true; }
        //add ') '
        if (thisAreacode.length == areacodeLength) { addSecondParen = true; }
        //add '-'
        if (thisFirstThree.length == firstThree) { addDash = true; }
        
        //add everything, assign to field
        if (addFirstParen) { thisAreacode = '(' + thisAreacode; }
        if (addSecondParen) { thisAreacode += ') '; }
        if (addDash) { thisFirstThree += '-'; }
        if (phone_test) { alert('writing'); }
        input.value = thisAreacode + thisFirstThree + thisLastFour;
      }
      previousLength = input.value.length
      //if (temp_selection && temp_selection != 'undefined') { input.selectionStart = temp_selection; input.selectionEnd = temp_selection; }
    }
  }
}
function ShowAnswer(id,targetonly) {
  var answer = document.getElementById("a"+id);
  if (answer.style.display == "" || answer.style.display == "none") {
    answer.style.display = "block";
    document.getElementById("plusminus"+id).src = ImageMinus.src;
    //document.getElementById("countview").src = "faq_countview.php?faq_id="+id;
  } else if (!targetonly) {
    answer.style.display = "none";
    document.getElementById("plusminus"+id).src = ImagePlus.src;
  }
}
function OpenAll(id) {
  //open all answers
  divColl = document.getElementsByTagName("DIV");
  for (i=0; i<divColl.length; i++) {
    if (divColl[i].className == "answer") {
      divColl[i].style.display = (allopened) ? "none" : "block";
    }
  }
  
  //change "plus" to "minus"
  regex = new RegExp("plusminus(\\d){1,2}");
  imgColl = document.getElementsByTagName("IMG");
  for (i=0; i<imgColl.length; i++) {
    if (regex.test(imgColl[i].id)) {
      imgColl[i].src = (allopened) ? ImagePlus.src : ImageMinus.src;
    }
  }
  
  //set flag
  allopened = !allopened;
}