// Copyright 1998 Macromedia, Inc. All rights reserved.
//Constructs a text entry element
function KO_text(theParent, theName, theInitialValue) {
  // properties
  this.initialValue = theInitialValue?theInitialValue:'';
  this.value = null;
	this.disabled = true;
	
  this._parent = theParent;
  this._name = theName;
	this._obj = '';
	
  this.c = new Array();

  // member functions
  this.init = KO_textInit;
  this.reset = KO_textReset;
  this.enable = KO_textEnable;
  this.disable = KO_textDisable;
  this.setDisabled = KO_textSetDisabled;
  this.update = KO_textUpdate;
	this.redraw = KO_textRedraw;
  this.setValue = KO_textSetValue;
  this.focus = KO_textFocus;
}

//Initializes the element
function KO_textInit() {
  with (this) _obj = KO_findObject(_parent._self + _name + "Inp");
}

//Resets the element
function KO_textReset() {
	with (this) {
	  _parent.disabled ? disable() : enable();
    setValue(initialValue);
	}
}

//Enables the element
function KO_textEnable() {
  var i;
	if (this._obj) with (this) {
    disabled = false;
    for (i in c) if (i != 'length') c[i].disabled = false;
    setValue(_obj.value);
	}
}

//Disables the element
function KO_textDisable() {
  this.disabled = true;
	this.redraw();
}

//Calls the approppriate disable or enable function
function KO_textSetDisabled(theDisabled) {
  if (theDisabled) this.disable();
  else this.enable();
}

//Called by onClick event to update this elements value
//We also store the object reference of the control, so that
// we can reset it later.
function KO_textUpdate() {
	if (!this.disabled) with (this) {
	  value = _obj.value;
	  for (var i in c) if (i != 'length') c[i].validValue();
	  _parent.update();   // call the parent's update
	}
}

// Sets the text value
function KO_textRedraw() {
	if (this._obj) with (this) {
		if (_obj.disabled != null) _obj.disabled = disabled;
	  if (_obj.value != null) _obj.value = value;
	}
}

function KO_textSetValue(theValue) {
  with (this) {
    value = theValue;
    redraw();
    for (var i in c) if (i != 'length') c[i].validValue();
    _parent.update(true);  // update int, no judge
  }
}

function KO_textFocus() {
  if (this.disabled) with (this._parent)
    if (browserIsNS && browserVersion >= 4.0 && osIsWindows) this._obj.blur();
}


//////////////////////////////////////////
//Create a string choice object
function KO_textComp(theParent, theElement, theName,
                     theExpectedValue, theIsCorrect, theScore,
                     theMatchCase, theMatchAll)
{
  // properties
  this.expectedValue = theExpectedValue;
  this.isCorrect = theIsCorrect;
  this.score = theScore;
  this.selected = false;
  this.disabled = false;
	
  this.matchCase = theMatchCase;
  this.matchAll = theMatchAll;
	
  this._elem = eval(theParent._self+".e['"+theElement+"']");
  this._isChoice = true;

  // methods
  this.validValue = KO_textCompValidValue;
  this.setSelected = KO_textCompSetSelected;
  this.setDisabled = KO_textCompSetDisabled;
  this.deencrypt = KO_textDeencrypt;

}

function KO_textCompValidValue() {
  var theValue, expValue;
	with (this) {
    selected = false;
    if (!disabled) {
      theValue = _elem.value;
      expValue = deencrypt(expectedValue);

      if (!matchCase) {
        theValue = theValue.toUpperCase();
        expValue = expValue.toUpperCase();
      }

      if (theValue != '') {
        if (matchAll)  selected = (theValue == expValue);
        else  selected = (theValue.indexOf(expValue) != -1);
  } } }
	return this.selected;
}

function KO_textCompSetSelected(theSelected) {
  with (this) {
    if (theSelected) {
      _elem.setValue(deencrypt(expectedValue));
    } else {
      selected = false;
      _elem._parent.update(true); // update int, no judge
  } }
}

function KO_textCompSetDisabled(theDisabled) {
  with (this) {
    disabled = theDisabled;
    validValue();
    _elem._parent.update(true); // update interaction, no judge
  }
}

function KO_textDeencrypt(theStr) {
  var decipher='',i,key,clength,part='-###-',keyOffsetObscure,keyOffset='',hyphen;
  
  if (theStr.indexOf(part)!=-1) {
    strStart = theStr.indexOf(part)+part.length;
	hyphen=theStr.indexOf('-');
    key = theStr.substring(0,hyphen);
	keyOffsetObscure=theStr.substring(hyphen+1, theStr.indexOf('-',hyphen+1));
    for (i=keyOffsetObscure.length-1;i>=0;i--)
	  keyOffset+=keyOffsetObscure.charAt(i);
    clength = key-keyOffset;
    retVal = theStr.substring(strStart, theStr.length);
	for (i=0;i<retVal.length;i+=(clength+1))
	  decipher=decipher+retVal.charAt(i);

	retVal = decipher;
  }
  else retVal = theStr;
    
  return retVal;
}
