/**
 * ecw_forms.js
 *
 * This file contains javascript functions used by mpower-suite
 * modules.
 * functions in this file are concerned with manipulating form controls
 *
 * $Header: /JavaScript/ecw_forms.js 5     5/01/06 10:33 Ds $
 * $Revision: 5 $
 *
 * $Log: /JavaScript/ecw_forms.js $
 * 
 * 5     5/01/06 10:33 Ds
 * 2026146
 *
 * 4     17/11/05 11:28 Gc
 *
 * Copyright 1996-2004 Monitor Management Control Systems Ltd.
 *
 *  Amendment History
 *
 *  Who  When         Why
 *  GC   13-09-05     2024975 : Removed openReports, hideReports and openReport.(part of everypage.js for mpower 4)
 *  AA   27-10-05     2026457 : f_save added to, can now pass in string, object boolean.
 *  GC   17-11-05             : Removed redundant mpower3 functions,
 *  DS   05-01-06     2026146 : escape long text values in f_save
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


//------------------------------------------------------------------------
// Functions to get the value of a checkbox - assumes '1' or '0' as the 2 states
// setCbx is tolerant of strings or numbers being passed in

function getCbx(p_field) {
  if (p_field.checked) {
    return 1;
  }
  else {
    return 0;
  }
}

function setCbx(p_field, p_value) {
  var sValue = ""+p_value;
  p_field.checked = (sValue == "1");
}

function getObjRef(p_ref, p_frame) {
  if (!p_frame) p_frame = window ;
  return p_frame.document.all ? p_frame.document.all[p_ref] : p_frame.document.getElementById(p_ref);
}

//
// If you are modifying this function remember to
// update ECWEB_UTILS.F_ENCODE_STRING as well
//
function f_encode_string(p_string){
  var chars = new Array("&",     "\"",    "'");       // List of characters to be replaced
  var codes = new Array("&#38;", "&#34;", "&#39;");   // List of URL-encoded characters to use instead
  var newValue = p_string;
  var re;

  for (var i = 0; i < chars.length; i++){
    if (newValue.indexOf(chars[i]) != -1){
      re = new RegExp("\\" + chars[i], "g");
      newValue = newValue.replace(re, codes[i]);
    }
  }
  return newValue ;
}

/**
 * Function     f_save
 * Description  Builds an input tag that can be used to save values to an ecweb save procedure
 *
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | field                     | param1                    | param2                    | param3                    |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | Object - screen field     |                           |                           |                           |
 * | used to derive the data   |                           |                           |                           |
 * | and the name of the save  |                           |                           |                           |
 * | field                     |                           |                           |                           |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | As above                  | Boolean - include Old     |                           |                           |
 * |                           | String  - the value       |                           |                           |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | As above                  | String - the value        | Boolean - include Old     |                           |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | As above                  | String - the name of the  | String - the value        |                           |
 * |                           |          save field       |                           |                           |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | As above                  | String - as above         | String - as above         | Boolean - include Old     |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | String - the name of the  | String - the value        |                           |                           |
 * |          save field       |                           |                           |                           |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | String - as above         | Object - screen field     |                           |                           |
 * |                           | containing the value      |                           |                           |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 * | String - as above         | Object - screen field     | Boolean - include Old     |                           |
 * |                           | containing the value      |                           |                           |
 * +---------------------------+---------------------------+---------------------------+---------------------------+
 *
 *  "Include Old" means that a field p_oldFIELDNAME will be created containing the value of the p_FIELDNAME when
 *  the screen was loaded (defaultValue for text fields, defaultChecked for checkboxes, and defaultSelected
 *  for comboboxes (selects))
 */
function f_save(field, param1, param2, param3){
  var rVal = "";
  var screenField ;
  var saveField;
  var includeOld = false;
  var oldSaveField ;
  var value = "";
  var defaultValue = "" ;

  if (typeof field == "object") {
    screenField = field ;

    if (screenField.type == "checkbox") {
      value = "0";
      if (screenField.checked){
        value = "1";
      }
    } else {
      value = screenField.value ;
    }

    saveField = screenField.name ;

    if (param1){
      if (typeof param1 == "boolean"){
        includeOld = param1;
      } else if (typeof param1 == "string"){
        value = param1 ;
      }
    }

    if (param2){
      if (typeof param1 == "string" && typeof param2 == "boolean") {
        value = param1 ;
        includeOld = param2;
      } else if (typeof param1 == "string" && typeof param2 == "string") {
        saveField = param1 ;
        value = param2 ;
      }
    }

    if (param3){
      if (typeof param1 == "string" && typeof param2 == "string" && typeof param3 == "boolean"){
        saveField = param1 ;
        value = param2 ;
        includeOld = param3 ;
      }
    }
  } else {
    saveField = field ;

    if (typeof param1 == "string"){
      value = param1 ;
    } else if (typeof param1 == "object"){
      screenField = param1;
      if (param1.type == "checkbox"){
        if (param1.checked){
          value = "1";
        }else{
          value = "0";
        }
      }else{
        value = param1.value ;
      }

      if (param2){
        if (typeof param2 == "boolean"){
          includeOld = param2;
        }
      }
    }
  }

  if (typeof screenField == "object") {
    oldSaveField = saveField.substr(0, 2) + "old" + saveField.substr(2);

    if (screenField.type == "checkbox"){
      rVal = "<INPUT type=text NAME=" + saveField + " VALUE=\"" + value + "\">";
      if (includeOld) {
        rVal += "<INPUT type=text NAME=" + oldSaveField + " VALUE=\"" + f_default_value(screenField) + "\">";
      }

    }else if (screenField.type == "textarea"){
      rVal = "<TEXTAREA NAME=" + saveField + ">" + escape(value) + "</TEXTAREA>";
      if (includeOld) {
        rVal += "<TEXTAREA NAME=" + oldSaveField + ">" + escape(f_default_value(screenField)) + "</TEXTAREA>";
      }

    }else{
      rVal = "<INPUT type=text NAME=" + saveField + " VALUE=\"" + f_encode_string(value) + "\">";
      if (includeOld) {
        rVal += "<INPUT type=text NAME=" + oldSaveField + " VALUE=\"" + f_encode_string(f_default_value(screenField)) + "\">";
      }
    }
  } else {
    rVal = "<INPUT type=text NAME=" + saveField + " VALUE=\""+f_encode_string(value)+"\">";
  }

  rVal += "\r\n" ;

  return rVal ;
}

/**
 * Function f_default_value
 * @param screenField, a reference to a field on a web form can be text, checkbox and select-one
 * @return String, a string representing the default value (the initial setting) of the field.
 *
 * This function can be used to return the default value for a particular field
 * it is currently only called from f_save (above).
 */
function f_default_value(screenField){
  var defaultValue = "" ;
  if (screenField.type == "select-one") {
    for(var i=0; i<screenField.length; i++) {
      if (screenField.options[i].defaultSelected) {
        defaultValue = screenField.options[i].value ;
        break ;
      }
    }
  } else if (screenField.type == "checkbox") {
    defaultValue = "0";
    if (screenField.defaultChecked){
      defaultValue = "1";
    }
  } else {
    defaultValue = screenField.defaultValue ;
  }

  return defaultValue;
}

/**
 * Function f_radio_value
 * @param screenField, a reference to a field on a web form can be checkbox or radio
 * @return String, a string representing the value of the current checked field
 *
 * This function can be used to return the value of checkbox or radio, where several
 * input fields are named the same (thus creating an array) but only one of these is
 * actually checked.
 */
function f_radio_value(screenField){
  var value = "" ;
  for(var i=0; i<screenField.length; i++) {
    if (screenField[i].checked) {
      value = screenField[i].value ;
      break ;
    }
  }

  return value;
}

/** Return the 'real' object to use. If it is an array, then return the correct entry */
function realObj( p_obj, p_row ){
  if ( typeof p_obj != "undefined" ){
    if ( typeof p_obj.length != "undefined" ){
      return p_obj[p_row];
    }
    return p_obj;
  }
}
