var ie5 = false;
var ns6 = false;
var moz = false;
var mac = false;
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 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 safemail(name, domain, display, css, subject) {
  css       = (css)     ? ' class="' + css + '"' : '';
  subject   = (subject) ? '?subject=' + subject : '';
  displayed = (display) ? display : name + '@' + domain;
  mailto    = name + '@' + domain + subject;
  document.write('<a href="mailto:' + mailto + '"' + css + '>' + displayed + '</a>');
}
function Popup(title,filename,w,h,scroll,resizable,menubar) {
  title     = (title == '')         ? 'popup' : title;
  scroll    = (scroll == 'scroll')  ? 'yes'   : 'no';
  resizable = (resizable == 'lock') ? 'no'    : 'yes';
  menubar   = (menubar == 'menu')   ? 'yes'   : 'no';
  
  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 ToggleVisibility(item, indicator, stylesheet_hidden, stylesheet_visible) {
  var obj = document.getElementById(item);
  if (obj.className == stylesheet_hidden) {
    obj.className = stylesheet_visible;
    if (indicator) {
      obj_indicator = document.getElementById(indicator);
      obj_indicator.src = "images/icon_minus.gif";
    }
  } else {
    obj.className = stylesheet_hidden;
    if (indicator) {
      obj_indicator = document.getElementById(indicator);
      obj_indicator.src = "images/icon_plus.gif";
    }
  }
}
function DisableButton(obj) {
  obj.src          = 'images/btn_processing.gif';
  obj.disabled     = true;
  obj.style.border = 'none';
}
function DisableButtonTxt(obj) {
  obj.value        = 'Processing...';
  obj.disabled     = true;
  obj.style.border = 'none';
}
function SetFocus(fieldname,formname) {
  if (formname == '') { 
    formname = 'form';
  }
  if (eval("document."+formname+".elements[fieldname].value == ''")) {
    eval("document."+formname+".elements[fieldname].focus();");
  }
}
function SubmitForm(obj, form_action) {
  var frm = document.forms[obj.form.name];
  var act = frm.elements['form_action'];
  act.value = form_action;
  frm.submit();
}
function WriteToElement(id, str) {
  var e = document.getElementById(id);
  if (e) {
    if (e.innerHTML) {
      e.innerHTML = str;
    } else {
      e.document.write(str);
      e.document.close();
    }
  }
}
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 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; }
    }
  }
}