// Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.

//*************** GLOBALS VARS *****************

var helpDoc = MM.HELP_behSetTextOfTextField;

//******************* BEHAVIOR FUNCTION **********************

//Passed a textfield name and a string, replaces textfield text.

function MM_setTextOfTextfield(objName,x,newText) { //v3.0
  var obj = MM_findObj(objName); if (obj) obj.value = newText;
}

MM.VERSION_MM_setTextOfTextfield = 3.0; //define latest version number for behavior inspector

//******************* API **********************


//Can be used with any tag and any event

function canAcceptBehavior(){
  var retVal = false;
  var nameArray = getAllObjectRefs("NS 4.0","INPUT/TEXT","TEXTAREA","INPUT/PASSWORD");
  if (nameArray.length > 0) retVal = "onMouseOver,(onMouseOver),onClick,(onClick)";
  return retVal;
}



//Returns a Javascript function to be inserted in HTML head with script tags.

function behaviorFunction(){
  return "MM_findObj,MM_setTextOfTextfield";
}



//Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
//Calls escQuotes to find embedded quotes and precede them with \

function applyBehavior() {
  var i,index,objNS,newlinePos,msgStr="",retVal="";
  with (document.theForm) {
    index = menu.selectedIndex;
    objNS = escQuotes(document.MM_NS_REFS[index]); //get textfield name from list
    msgStr = escExprStr(message.value,false);
  }
  if (objNS.indexOf(REF_UNNAMED) == 0) retVal = MSG_UnnamedTextfield;
  else if (msgStr == null) retVal = MSG_BadBraces;
  else {
    objNS = getNameFromRef(objNS);
    updateBehaviorFns("MM_findObj","MM_setTextOfTextfield");
    retVal = "MM_setTextOfTextfield('"+objNS+"','','"+msgStr+"')";
  }
  return retVal
}



//Passed the function call above, takes prior arguments and reloads the UI.
//Removes any escape characters "\"

function inspectBehavior(fnStr){
  var argArray,found,numTxtflds,i;
  var objNS,objIE,startStr,endStr,msgStr;

  argArray = extractExprStr(fnStr);//get new list of textfields, text triplets
  if (argArray.length == 3) { //3 args
    //set textfield obj
    objNS=argArray[0];
    objIE=argArray[1];
    found = false;
    numTxtflds = document.MM_NS_REFS.length;
    for (i=0; i<numTxtflds; i++){  //check if textfield is in menu
      if (objNS==document.MM_NS_REFS[i] || objNS==getNameFromRef(document.MM_NS_REFS[i])) { //if textfield there
        document.theForm.menu.selectedIndex = i;
        found = true;
        break;
      }
    }
    if (!found) alert(errMsg(MSG_TextfieldNotFound,objNS));

    //set text, converting all string expressions to {expression} etc.
    document.theForm.message.value = unescExprStr(argArray[2],false);
  }
}



//Returns a dummy function call to inform Dreamweaver the type of certain behavior
//call arguments. This information is used by DW to fixup behavior args when the
//document is moved or changed.
//
//It is passed an actual function call string generated by applyBehavior(), which
//may have a variable list of arguments, and this should return a matching mask.
//
//The return values are:
//  objName:  argument is simple object name, such as "textfield1"
//  other...: argument is ignored

function identifyBehaviorArguments(fnCallStr) {
  var argArray, retVal="", fullObjRef;;

  argArray = extractArgs(fnCallStr);
  fullObjRef = (argArray[1].indexOf(".")!=-1);
  if (argArray.length == 4) {
    retVal = (fullObjRef)?"NS4.0ref,IE4.0ref,other" : "objName,other,other";
  }
  return retVal;
}

//***************** LOCAL FUNCTIONS  ******************


//Load up the textfields, set the insertion point

function initializeUI(){
  var i, niceNameSrcArray, nameArray;

  document.MM_NS_REFS = getAllObjectRefs("NS 4.0","INPUT/TEXT","TEXTAREA","INPUT/PASSWORD");
  document.MM_IE_REFS = getAllObjectRefs("IE 4.0","INPUT/TEXT","TEXTAREA","INPUT/PASSWORD");
	//Search for unreferenceable objects. <DIV id="foo"> is IE only, <LAYER> is NS only.
	//if REF_CANNOT found, return empty string, and use IE refs for nice namelist.
	niceNameSrcArray = document.MM_NS_REFS;
	for (i=0; i<document.MM_NS_REFS.length; i++) {
		if (document.MM_IE_REFS[i].indexOf(REF_CANNOT) == 0) {
			document.MM_IE_REFS[i] = ""; //blank it out
		}
		if (document.MM_NS_REFS[i].indexOf(REF_CANNOT) == 0) {
			document.MM_NS_REFS[i] = ""; //blank it out
			niceNameSrcArray = document.MM_IE_REFS; //use the IE list
		}
	}
	nameArray = niceNames(niceNameSrcArray,MM.TYPE_Text);  //get textfield names
	with (document.theForm.menu) {
		for (i=0; i<nameArray.length; i++) options[i]=new Option(nameArray[i]); //load menu
		selectedIndex = 0;
	}
  document.theForm.message.focus(); //set focus on textbox
  document.theForm.message.select(); //set insertion point into textbox
}
