/**
 *   Copyright 2005 Componence DDS. All rights reserved.
 */

/**
 * JSBeaUtils is the javascript class which contains the most used functions
 * A JSBeaUtils object encapsulates static function for common work :
 *    getObjectName
 *    getForm
 *    getFormAndCheck
 *    getInput
 *    getInputAndCheck
 *    getInputForm
 *    getInputFormName
 *    addOnSubmitFunction
 * 
 * @version 1.1.78
 * @constructor
 * @since   1.0
 */


function JSBeaUtils() {

  // this method will return object id not name.
  // in bea portal 8.1 name and id was the same but in bea portal 9.2 they are different
  // so to lookup real object name it is needed to get object by id and then read it's name property, also it is possible to get object name from netui mapping
  
  this.getObjectId = function (objectId) {
	return objectId;
  }

  this.getObjectName = function (objectId) {
  	return NetUi.getMapping(objectId);
  }

  this.getForm = function (formTagId) {
    var formId = this.getObjectId(formTagId);
    return Global.getObject(formId);
  }

//  this.getForm = function (formTagId) {
//    var result = null;
//    var id = getNetuiTagName(formNameInternal); 
//    for(var i=0; i< document.forms.length; i++) {
//      var frm = document.forms[i];
//      if(id == frm.id) {
//        result = frm;
//        break;
//      }
//    }
//    return result;
//  }

  this.validateForm = function (form, tagId) {
    tagId = (tagId) ? " (tagId: " + tagId + ") " : "";
    var result = false;
    if (!Global.isDefined(form)) {
      alert('form "' + tagId + '" is not present');
    } else if (("" + form.tagName).toLowerCase() != "form") {
      alert('object "' + tagId + '" is not a form');
    } else {
      result = true;
    }
    return result;
  }

  this.getFormAndCheck = function (formTagId) {
    var form = this.getForm(formTagId);
    return (this.validateForm(form, formTagId)) ? form : null;
  }

  this.getInput = function (/*can be inputId*/formTagId, /*is not required*/inputTagId) {

    var result = null;

    if (Global.isDefined(inputTagId)) {
      // we have two parameters and should get input by formname and inputname.
      var form = this.getForm(formTagId);
      if (Global.isDefined(form)) {
        var inputName = this.getObjectName(inputTagId);
        //result = Global.getObject(inputId); //does not work when there are inputs with same name in differt forms
        result = form[inputName]; //fix to get the input element only from given form
        if (!Global.isDefined(result)) {
          // it is because we can put non bea input into this function
          result = FormManager.getInput(inputTagId, form.name);
        }
      } else {
        // if form is not present then trying to get input by id
        result = this.getInput(inputTagId);
      }
    } else {
      var inputId = BeaUtils.getObjectId(formTagId);
      result = Global.getObject(inputId);
    }

    return result;
  }

  this.validateInput = function (input, tagId) {
    var result = false;
    tagId = (tagId) ? " (tagId: " + tagId+ ") " : "";
    if (!Global.isDefined(input)) {
      alert('input "' + tagId + '" is not present');
    } else {
      var tagName = ("" + input.tagName).toLowerCase();
      if (tagName != "input" && tagName != 'select' && tagName != 'textarea') {
        alert('object "' + tagId + '" is not an input\nor you have several inputs with the same name');
      } else {
        result = true;
      }
    }
    return result;
  }

  this.getInputAndCheck = function(/*can be inputId*/formTagId, /*is not required*/inputTagId) {
    var input = null;
    var f = Global.isDefined(inputTagId);
    var form = null;
    if (f) {
      form = this.getFormAndCheck(formTagId);
    }
    if (!f || (f && form != null)) {
      input = this.getInput(formTagId, inputTagId);
      input = (this.validateInput(input, (f) ? inputTagId : formTagId)) ? input : null;
    }
    return input;
  }

  this.getInputForm = function(inputTagId, check) {
    var input = (check) ? this.getInputAndCheck(inputTagId) : this.getInput(inputTagId);
    var result = null;
    if (Global.isDefined(input)) {
      result = input.form;
      if (check) {
        result = (this.validateForm(result)) ? result : null; 
      }
    }
    return result;
  }

  this.getInputFormAndCheck = function(inputTagId) {
    return this.getInputForm(inputTagId, true);
  }
  
  this.setCheckBoxValue = function setCheckBoxValue(inputTagId, value) {
    var input = this.getInputAndCheck(inputTagId);
    if (input != null) {
      input.checked = (value == "true");
    }
  }

  this.addOnSubmitFunction = function(formTagId, func) {
    var form = this.getFormAndCheck(formTagId);
    if (Global.isDefined(form)) {
      FormManager.addOnSubmitFunction(form.name, func);
    }
  }

  this.getImageSource = function (src, attributes) {
    return "<img src=\"" + ComponentConfig.imagePath + src + "\" " + attributes + " >";
  }

  this.showImage = function (src, attributes) {
    document.write(this.getImageSource(src, attributes));
  }
  
  this.showSortOrderImage = function(sortOrder, attributes) {
  	if(sortOrder) {
  		this.showImage("sd-dynmenu-arrow-sorting-asc.gif", attributes);
  	} else {
  		this.showImage("sd-dynmenu-arrow-sorting-desc.gif", attributes);
  	}
  }

  this.getAttachmentImageSource = function (nodeId, field, attributes) {
    return "<img src=\"" + ComponentConfig.attachmentsPath + "?nodeId=" + nodeId + "&field=" + field + "\" "  + attributes + " >";
  }

  this.showAttachmentImage = function (nodeId, field, show, attributes) {
    if(show) {
      document.write(this.getAttachmentImageSource(nodeId, field, attributes));
    }
  }

  this.getImageInputSource = function (src, attributes) {
    return "<input type=\"image\" src=\"" + ComponentConfig.imagePath + src + "\" " + attributes + " >";
  }

  this.showImageInput = function (src, attributes) {
    document.write(this.getImageInputSource(src, attributes));
  }

}

BeaUtils = new JSBeaUtils();