if (!(typeof(addEvent)=='function')) {
  function addEvent(name,obj,f) {
    if (window.attachEvent) {
      obj.attachEvent("on"+name,f);
    } else if (window.addEventListener) {
      obj.addEventListener(name,f,false);
    }
  }
}

if (!(typeof(removeEvent)=='function')) {
  function removeEvent(name,obj,f) {
    if (window.attachEvent) {
      obj.detachEvent("on"+name,f);
    } else if (window.addEventListener) {
      obj.removeEventListener(name,f,false);
    }
  }
}

addEvent('load',window,function() {
  // private variables
  var el=document.getElementById('stsame');
  var formlist=['company','firstName','lastName','street1','street2','city','postalCode'];

  // private members

  // Attach this event to input[text] items which are to be mirrored from billTo to shipTo
  // when "same as BillTo" box is checked.
  var change=function(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;

    var m=document.getElementById('shipTo_'+targ.id.replace(/billTo_/,''));
    m.value=targ.value;
    m.tabIndex=-1;
    m.readOnly=true;
    m.style.backgroundColor="#cccccc";
  };


  // Attach this event to select items which are to be mirrored from billTo to shipTo
  // when "same as BillTo" box is checked.

  // Select boxes don't have a readonly property. So we make a select box with a single
  // element and replace the existing one, accomplishing more or less the same thing.
  var sel=function(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;

    var m=document.getElementById('shipTo_'+targ.id.replace(/billTo_/,''));
    var S=document.createElement('select');
    var opt=new Option(targ.options[targ.selectedIndex].text,targ.options[targ.selectedIndex].value);
    S.options[0]=opt;
    S.id=m.id;
    S.className=m.className;
    S.name=m.name;
    S.style.backgroundColor="#cccccc";
    S.tabIndex=-1;
    m.parentNode.replaceChild(S,m);
  };

  // Event to attach to the "same as BillTo" box. Attaches the above events to appropriate form
  // elements named in formlist, and also to a select box hard-coded inside here.
  addEvent('click',el,function(e) {

    // If the box is checked, attach the events, and call the each by hand.
    if (el.checked) {
      for (var i=0;i < formlist.length;i++) {
        var el2=document.getElementById('billTo_'+formlist[i]);
        addEvent('keyup',el2,change);
        change({'target':el2});
      }

      el2=document.getElementById('billTo_state');
      addEvent('change',el2,sel);
      sel({'target':el2});

    // If the box is NOT checked, detach the events, color the form elements properly,
    // and rebuild the select box.
    } else {
      for (var i=0;i < formlist.length;i++) {
        var el2=document.getElementById('billTo_'+formlist[i]);
        removeEvent('keyup',el2,change);

        var el3=document.getElementById('shipTo_'+formlist[i]);
        el3.readOnly=false;
        el3.style.backgroundColor="white";
        el3.tabIndex=el2.tabIndex+30;
      }

      el2=document.getElementById('billTo_state');
      removeEvent('change',el2,sel);

      el3=document.getElementById('shipTo_state');
      var el3_temp=el2.cloneNode(true);
      el3_temp.id=el3.id;
      el3_temp.name=el3.name;
      el3_temp.className=el3.className;
      el3.parentNode.replaceChild(el3_temp,el3);

      // Somehow, this doesn't get set with cloneNode.
      el3_temp.selectedIndex=el2.selectedIndex;
      el3_temp.tabIndex=el2.tabIndex+30;
    }
  });
});