// DESCRIPTION:  Cross-browser Text-based Button Handler
// Modified by Alexander Yap.
// Requires utils.js

function cssButtonSwap() {
  var but = document.getElementById( this.name );
  if (but) {
    but.className='button'+this.current;

    if (this.current=='Up') {
        but.style.backgroundColor=this.ColrUp;
    } else if (this.current=='Over') {
        but.style.backgroundColor=this.ColrOver;
    } else if (this.current=='Down') {
        but.style.backgroundColor=this.ColrDown;
    } else if (this.current=='Disabled') {
        but.style.backgroundColor=this.ColrDisabled;
    }
    but.style.color = this.textColor;
  }
}
function cssButtonUp() {
  if ( this.disabled==false ) {
    if (this.toggle==true && this.active==true) {
      this.current = 'Down'
    } else {
      this.current = 'Up'
    }
    this.swap()            
  }  
  window.status=""
  return true
}         
function cssButtonDo() {
  eval( this.action )
  return true
}

function cssButtonSetColor(color) {
  this.textColor = color;
  this.swap();
}

function cssButtonSetColors(up, over, down, disabled) {
  if (up != '') {
    this.ColrUp = up;
  }
  if (over != '') {
    this.ColrOver = over;
  }
  if (down != '') {
    this.ColrDown = down;
  }
  if (disabled != '') {
    this.ColrDisabled = disabled;
  }
  this.swap();  
}
 
function cssButtonClick(evt) {
  if ( this.disabled==false ) {
    if ( this.toggle==true ) {
      if ( this.active==true ) {
        this.current = 'Up'
        this.active=false
      } else {
        this.current = 'Down'
        this.active=true
      }
    } else {
      this.current = 'Over'
    }
    this.swap()
    this.doAction()
  }
  window.status=""
  
  
  return true;
}
function cssButtonOver() {
  if ( this.disabled==false ) {
    if (this.toggle==true && this.active==true) {
      this.current = 'Down'
    } else {
      this.current = 'Over'
    }
    this.swap();
  }
}
function cssButtonDown() {
  if ( this.disabled==false ) {
    if (this.toggle==true && this.active==true) {
      this.current = 'Over'
    } else {
      this.current = 'Down'
    }
    this.swap()
  }
}
function cssButtonOff() {
  this.disabled = true
  this.current = 'Disabled'
  this.swap()
}
function cssButtonOn() {
  this.disabled = false
  if (this.toggle==true && this.active==true) {
    this.current = 'Down'
  } else {
    this.current = 'Up'
  }
  this.swap()
}

function cssGenerateHTML() {
  var classAtt;
  if ( this.toggle==true && this.active==true ) {
    classAtt = "buttonDown";
  } else {
    classAtt = "buttonUp";
  }
  var styleAtt = 'background-color:'+(classAtt == "buttonDown" ? this.ColrDown : this.ColrUp) +
                 ';font-size:' + this.textSize;
  if (this.widthStr!="") {
    styleAtt = styleAtt + ';width:' + this.widthStr;
  } else if (document.all) {
    styleAtt = styleAtt + ';width:100%';  // IE needs 100% to ensure that it is big enough to contain the text
  }
  if (this.heightStr!="") {
    styleAtt = styleAtt + ';height:' + this.heightStr;
  } else if (document.all) {
    styleAtt = styleAtt + ';height:100%';  // IE needs 100% to ensure that it is big enough to contain the text
  }
  var statustext = this.statustext.replace(/'/g, "\\'");
  document.write('<div id="' + this.name + '" class="' + classAtt + '" title="' + this.statustext + '"' +
    'style="' + styleAtt + '"' +
    ' onmouseover="' + this.name + '.over(); window.status=\''+statustext+'\'; return true;"' +
    ' onmousedown="' + this.name + '.down();"' +
    ' onmouseout="'  + this.name + '.up();"' +
    ' onmouseup="'   + this.name + '.clicked(); return true;"' +
    '>' + this.label + '</div>');

  return true
}

function cssButton_setEnabled( en ) {
    if (en ) {
        this.enable();
    } else {
        this.disable();
    }
}

// Constructor for cssButton
// height and width can be left out by passing in "".
// If left out, width and height will automatically be set to be big enough to contain the text.
function cssButton ( nm, ltext, act, width, height, disb, stxt, toggle, active, normColr, overColr, disColr, textSize ) {
  this.label = ltext
  this.current = "Up"
  this.ColrUp = normColr
  this.ColrOver = overColr
  this.ColrDown = overColr
  this.ColrDisabled = disColr
  if (textSize==null) {
    this.textSize = "9px"
  } else {
    this.textSize = textSize
  }
  this.toggle = toggle
  this.active = active
  this.action = act
  this.textColor = "#ffffff";

  // width and height might be specified in units of em, px or %.
  // If no units, assume px.
  this.widthStr = String(width);
  if ( (this.widthStr!="") && (this.widthStr.indexOf("px")==-1) && (this.widthStr.indexOf("em")==-1) && (this.widthStr.indexOf("%")==-1) ) {
    this.widthStr = this.widthStr+"px";
  }

  this.heightStr = String(height);
  if ( (this.heightStr!="") && (this.heightStr.indexOf("px")==-1) && (this.heightStr.indexOf("em")==-1) && (this.heightStr.indexOf("%")==-1) ) {
    this.heightStr = this.heightStr+"px";
  }

  this.name = nm
  this.statustext = stxt
  this.generateHTML = cssGenerateHTML
  this.swap = cssButtonSwap
  this.up = cssButtonUp
  this.over = cssButtonOver
  this.down = cssButtonDown
  this.clicked = cssButtonClick
  this.doAction = cssButtonDo
  this.disable = cssButtonOff
  this.enable = cssButtonOn
  this.setEnabled = cssButton_setEnabled
  this.setColor = cssButtonSetColor
  this.setColors = cssButtonSetColors;

  this.disabled = disb
  if ( disb==true ) {
    this.current = 'Disabled'
  } else {
    if (active==true) {
      this.current = 'Down'
    } else {
      this.current = 'Up'
    }
  }
}
function canSelection() {
  return true
}
document.onselectstart = canSelection
