var ie4                 = false;
var ie5                 = false;
var ns4                 = false;
var ns6                 = false;
var mac                 = false;
var running             = false;
var timerId             = null;
var running2            = false; //added for 2nd level menus on TSA
var timerId2            = null;  //added for 2nd level menus on TSA
var delay               = 500;
var currentMenu         = null;
var previousMenu        = null;
var previousSubMenu     = null;  //added for 2nd level menus on TSA
var loaded              = false;
var testing             = false;
var allopened           = false;
var homepage            = false;
var dir_images          = 'images/';

function Popup(title,filename,w,h,scroll,resizable){if (title == ''){ title = 'popup'; }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=no,scrollbars="+scroll+",resizable="+resizable+",copyhistory=no'");window.open(filename,title,winOptions);return;}
function SetFocus(fieldname,formname){if (formname == ''){ formname = 'form'; }if (eval("document."+formname+".elements[fieldname].value == ''")){ eval("document."+formname+".elements[fieldname].focus();"); }}

// modified for 2nd level menus on TSA
function StartTimer(event, secondlevel){
  running = true; timerId = setTimeout(event,delay);
  if (secondlevel) {running2 = true; timerId2 = setTimeout(secondlevel,delay); }
}
// modified for 2nd level menus on TSA
function StopTimer(both){
  running = false; clearTimeout(timerId);
  if (both) {running2 = false; clearTimeout(timerId2); }
}

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 in_array(needle,haystack) {
  for (i=0; i<haystack.length; i++) {
    if (haystack[i] == needule) { return true; }
  }
  return false;
}
///////////////////////////
///////////////////////////

function Init() {
  SetBrowser();
  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 LoadMenus() {
  for (var i=1; i<=5; 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);
    
    //different top position for different ids
    menuTop  = t + 25;
    
    //different left position for different ids
    menuLeft = l;
    
    //different position for sub menus
    if (i >= 12) {
      menuTop  = t - 2;
      menuLeft = l + w + 8;
    }
    
    //set
    SetElementPosition(menuId,menuTop,menuLeft);
    
    //shadow
    menuWidth  = getElementWidth(menuId);
    menuHeight = getElementHeight(menuId);
    /*
    // JOE'S EDIT JOES EDIT
    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);
    }
    */
  }
}

// modified for 2nd level menus on TSA
function Show(id, pid) {
  if (running) { StopTimer(); }
  parentMenu  = pid;
  currentMenu = id;
  if (currentMenu != previousMenu && previousMenu != parentMenu && previousMenu != null) { Hide(previousMenu); }
  if (currentMenu != previousSubMenu && previousSubMenu != null) { Hide(previousSubMenu); }
  eval('document.getElementById("menu'+id+'Div").style.visibility = "visible"');
  eval('document.getElementById("menubg'+id+'Div").style.visibility = "visible"');
  if (!parentMenu) { eval('document.images["nav'+id+'"].style.visibility = "hidden"'); }
  previousMenu = (parentMenu) ? parentMenu : currentMenu;
  previousSubMenu = (parentMenu) ? currentMenu : null;
}

function Hide(id) {
  if (id == null) { return; }
  eval('document.getElementById("menu'+id+'Div").style.visibility = "hidden"');
  eval('document.getElementById("menubg'+id+'Div").style.visibility = "hidden"');
  // hide menus but not subnav arrows
  if (id < 12 && eval('document.images["nav'+id+'"]')) { eval('document.images["nav'+id+'"].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;
var international   = 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 first input is a "+" assume its an international number and do not format
      if (input.value.length == 1 && input.value == '+') { international = true; return true;}
      else if (input.value.length == 1 && input.value == '(') { previousLength = 1; return true;}
      //dont do anything on backspace
      else if (input.value.length >= previousLength && international == false) {
        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; }
    } else if (type == 'money') {
      // set decimal places
      var places = 2;
      
      // clean number
      var regex  = /[^.0-9]/g;
      num = new String(input);
      num = num.replace(regex, '');
      
      if (num.length == 0) {
        return '';
      }
      
      // preserve zeros
      decimalPos = num.indexOf('.');
      if (decimalPos == -1 && places > 0) {
        difference = places;
        num += '.';
      } else if (decimalPos == -1) {
        difference = 0;
      } else {
        actualDecimals = (num.length - 1) - decimalPos;
        difference = places - actualDecimals;
      }
      for (i=0; i<difference; i++) {
        num += '0';
      }
      
      // format
      if (isNaN(num) || num == '') {
        num = 0;
      } else {
        var regex = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
        arrNumber = num.split('.');
        arrNumber[0] += '.';
        do {
          arrNumber[0] = arrNumber[0].replace(regex, '$1,$2');
        }
        while (regex.test(arrNumber[0]));
        if (arrNumber.length > 1) {
          return '$' + arrNumber.join('');
        } else {
          return '$' + arrNumber[0].split('.')[0];
        }
      }
    }
  }
}

// from client
function GP_popupConfirmMsg(msg) { //v1.0
  document.MM_returnValue = confirm(msg);
}