
  var aRegisteredHtmlPageForms = new Array();

  function Form_IsExists(formName)
  {
    if (window.document.forms[formName] != null)
    {
      return true;
    }
    return false;
  }

  function Form_Get(formName)
  {
    if (window.document.forms[formName] != null)
    {
      return window.document.forms[formName];
    }
    alert("Error at: Form_Get(..). Requested form with name '" + formName + "' is not found!");
    return null;
  }
  
  function Input_IsExists(formName,inputName)
  {
    if (Form_IsExists(formName) == true)
    {
      if (window.document.forms[formName][inputName] != null)
      {
        return true;
      }
    }
    return false;
  }

  function Input_Get(formName,inputName)
  {
    var form = Form_Get(formName);
    if (form != null)
    {
      if (window.document.forms[formName][inputName] != null)
      {
        return window.document.forms[formName][inputName];
      }
      else
      {
        alert("Error at: Input_Get(..). Requested input with name '" + inputName + "' is not found!");
      }
    }
    return null;
  }

  function Input_GetById(id)
  {
    var input = document.getElementById(id);
    if (input != null)
    {
      return input;
    }
    else
    {
      alert("Error at: Input_GetById(..). Requested input with id '" + id + "' is not found!");
    }
    return null;
  }

  function Element_HideById(id)
  {
    //safe function to hide an element with a specified id
    // DOM3 = IE5, NS6
    if (document.getElementById) 
    { 
      document.getElementById(id).style.display = 'none';
    }
    else
    {
      // Netscape 4
      if (document.layers)
      { 
        document.id.display = 'none';
      }
      // IE 4
      else
      { 
        document.all.id.style.display = 'none';
      }
    }
  }

  function Element_ShowById(id)
  {
    //safe function to show an element with a specified id
    // DOM3 = IE5, NS6
    if (document.getElementById)
    { 
      document.getElementById(id).style.display = '';
    }
    else
    {
      // Netscape 4
      if (document.layers)
      { 
        document.id.display = '';
      }
      // IE 4
      else
      { 
        document.all.id.style.display = '';
      }
    }  
  }

  function Input_GetValue(formName,inputName)
  {
    var input = Input_Get(formName,inputName);
    if (input != null)
    {
      if (input.type == "text" ||
          input.type == "textarea" ||
          input.type == "password" ||
          input.type == "file" ||
          input.type == "hidden"
         )
      {
        return input.value;
      }
      else if (input.type == "radio")
      {
        for (var i=0; i<input.length; i++)
        {
          if (input[i].checked == true)
          {
            return input.value;
          }
        }
        return false;
      }
      else if (input.type == "checkbox")
      {
        return input.value;
      }        
      else if (input.type == "select-one")
      {
        if (input.selectedIndex != -1)
        {
          return input.options[input.selectedIndex].value;
        }
      }
      else
      {
        alert("Error at: Input_GetValue(..). Unsupported input type: '" + input.type + "' at form '" + formName + "' and input named '" + inputName + "'!");
      }
      return "";
    }
    return "";
  }

  function Input_GetValueById(id)
  {
    var input = document.getElementById(id);
    if (input != null)
    {
      if (input.type == "text" ||
          input.type == "textarea" ||
          input.type == "password" ||
          input.type == "file" ||
          input.type == "hidden"          
         )
      {
        return input.value;
      }
      else if (input.type == "radio")
      {
        return input.checked;
        /*
        for (var i=0; i<input.length; i++)
        {
          if (input[i].checked == true)
          {
            return input.value;
          }
        }
        return false;
        */
      }
      else if (input.type == "checkbox")
      {
        return input.value;
      }        
      else if (input.type == "select-one")
      {
        if (input.selectedIndex != -1)
        {
          return input.options[input.selectedIndex].value;
        }
      }
      else
      {
        alert("Error at: Input_GetValue(..). Unsupported input type: '" + input.type + "' at form '" + formName + " and input named '" + inputName + "'!");
      }
      return "";
    }
    return "";
  }
  
  function Input_SetValue(formName,inputName,value)
  {
    var input = Input_Get(formName,inputName);
    if (input != null)
    {
      if (input.type == "text" ||
          input.type == "textarea" ||
          input.type == "password" ||
          input.type == "file" ||
          input.type == "hidden"
         )
      {
        input.value = value;
      }
      else if (input.type == "radio")
      {
        for (var i=0; i<input.length; i++)
        {
          if (input[i].value == value)
          {
            input[i].checked = true;
          }
        }
        return "";
      }
      else if (input.type == "checkbox")
      {
        input.checked = value;
      }        
      else if (input.type == "select-one")
      {
        for (var i=0; i<input.options.length; i++)
        {
          if (input.options[i].value == value)
          {
            input.selectedIndex = i;
            break;
          }
        }
      }
      else
      {
        alert("Error at: Input_SetValue(..). Unsupported input type: '" + input.type + "'!");
      }
      return "";
    }
    return "";
  }

  function Input_SetValues()
  {
    var formName = Input_SetValues.arguments[0];
    var index = Input_SetValues.arguments[Input_SetValues.arguments.length - 2];
    var value = Input_SetValues.arguments[Input_SetValues.arguments.length - 1];
    for (var i=1; i<(Input_SetValues.arguments.length - 2); i++)
    {
      var input = Input_Get(formName,Input_SetValues.arguments[i]);
      if (input != null)
      {
        if (input[index].type == "radio")
        {
          input[index].checked = value;
        }
        else
        {
          alert("Error at: Input_SetValues(..). Unsupported input type: '" + input.type + "'!");
        }
      }
    }
    return false;
  }
  
  function Input_SetValueById(id,value)
  {
    var input = document.getElementById(id);
    if (input != null)
    {
      if (input.type == "text" ||
          input.type == "textarea" ||
          input.type == "password" ||
          input.type == "file" ||
          input.type == "hidden"
         )
      {
        input.value = value;
      }
      else if (input.type == "radio")
      {
        for (var i=0; i<input.length; i++)
        {
          if (input[i].value == value)
          {
            input[i].checked = true;
          }
        }
        return "";
      }
      else if (input.type == "checkbox")
      {
        input.checked = value;
      }        
      else if (input.type == "select-one")
      {
        for (var i=0; i<input.options.length; i++)
        {
          if (input.options[i].value == value)
          {
            input.selectedIndex = i;
            break;
          }
        }
      }
      else
      {
        alert("Error at: Input_SetValueById(..). Unsupported input type: '" + input.type + "'!");
      }
      return "";
    }
    return "";
  }
  
  function Input_SetDisabled(formName,inputName,value)
  {
    var input = Input_Get(formName,inputName);
    if (input != null)
    {
      input.disabled = value;
      return true;
    }
    return false;
  }
  
  function Input_Clear(formName,inputName)
  {
    var input = Input_Get(formName,inputName);
    if (input != null)
    {
      if (input.type == "text" ||
          input.type == "textarea" ||
          input.type == "password" ||
          input.type == "file" ||
          input.type == "hidden"
         )
      {
        input.value = "";
      }
      else if (input.type == "checkbox")
      {
        input.checked = false;
      }        
      else if (input.type == "select-one")
      {
        while (input.length > 0)
        {
          input.remove(input.length - 1);
        } 
      }
      else
      {
        alert("Error at: Input_Clear(..). Unsupported input type: '" + input.type + "'!");
      }
      return "";
    }
    return "";
  }

  function Input_ClearById(id)
  {
    var input = document.getElementById(id);  
    if (input != null)
    {
      if (input.type == "text" ||
          input.type == "textarea" ||
          input.type == "password" ||
          input.type == "file" ||
          input.type == "hidden"
         )
      {
        input.value = "";
      }
      else if (input.type == "checkbox")
      {
        input.checked = false;
      }        
      else if (input.type == "select-one")
      {
        while (input.length > 0)
        {
          input.remove(input.length - 1);
        } 
      }
      else
      {
        alert("Error at: Input_Clear(..). Unsupported input type: '" + input.type + "'!");
      }
      return "";
    }
    return "";
  }

  function Input_AddOption(formName,inputName,value,text)
  {
    var input = Input_Get(formName,inputName);
    if (input != null)
    {
      if (input.type == "select-one")
      {
        input.options[input.options.length] = new Option(text, value, false);
      }
      else
      {
        alert("Error at: Input_AddOption(..). Unsupported input type: '" + input.type + "'!");
      }
      return "";
    }
    return "";
  }

  function Input_SwapOptions(formName,inputName,indexOne,indexTwo)
  {
    var select = Input_Get(formName,inputName);
    var options = select.options;
    var indexOneSelected = options[indexOne].selected;
    var indexOneClass = options[indexOne].className;
    var indexTwoSelected = options[indexTwo].selected;
    var indexTwoClass = options[indexTwo].className;
    var optionOne = new Option(options[indexOne].text, 
                               options[indexOne].value,
                               options[indexOne].defaultSelected,
                               options[indexOne].selected);
    var optionTwo = new Option(options[indexTwo].text,
                               options[indexTwo].value,
                               options[indexTwo].defaultSelected,
                               options[indexTwo].selected);
    options[indexOne] = optionTwo;
    options[indexTwo] = optionOne;
    options[indexOne].selected = indexTwoSelected;
    options[indexOne].className = indexTwoClass;
    options[indexOne].selected = indexOneSelected;
    options[indexOne].className = indexOneClass;
  }

  function Input_MoveOptionUp(formName,inputName)
  {
    var select = Input_Get(formName,inputName);
    var selectedIndex = select.selectedIndex;
    if (selectedIndex < 1)
    {
      return false;
    }
    Input_SwapOptions(formName,inputName,selectedIndex,(selectedIndex - 1));
    select.options[(selectedIndex - 1)].selected = true;
  }

  function Input_MoveOptionDown(formName,inputName)
  {
    var select = Input_Get(formName,inputName);
    var selectedIndex = select.selectedIndex;
    if (selectedIndex < 0 ||
        selectedIndex > (select.options.length - 2))
    {
      return false;
    }
    alert(selectedIndex + "," + (selectedIndex + 1));
    Input_SwapOptions(formName,inputName,selectedIndex,(selectedIndex + 1));
    select.options[(selectedIndex + 1)].selected = true;
  }

  function Input_DeleteOption(formName,inputName)
  {
    var select = Input_Get(formName,inputName);
    var selectedIndex = select.selectedIndex;
    if (selectedIndex < 0)
    {
      return false;
    }
    select.options[selectedIndex] = null;
  }

  function HtmlForm_ShowUploadingAnimations(formName)
  {
    var requiredFields = new Array();
    for (fieldName in window.aRegisteredHtmlPageForms[formName])
    {
      var formObject = window.document.forms[formName][fieldName];
      if (formObject != null)
      {
        if (formObject.type == "file" &&
            formObject.value != "")
        {
          var uploaDiv = document.getElementById(formObject.name + "_upload_div");
          if (uploaDiv != null)
          {
            uploaDiv.style.display = "block";
          }
        }
      }
    }
  }

  function HtmlForm_Submit(formName,submitCommand)
  {
    if (window.document.forms[formName]["command"] != null)
    {
      window.document.forms[formName]["command"].value = submitCommand;
    }
    HtmlForm_ShowUploadingAnimations(formName);
    window.document.forms[formName].submit();
  }
  
  function HtmlForm_ValidateEmailAddress(formName,eMail)
  {
    if (eMail == null ||
        eMail.length < 3 ||
        eMail.indexOf("@")  == -1 ||
        eMail.indexOf(".")  == -1 ||
        eMail.indexOf("*")  != -1 ||
        eMail.indexOf("'")  != -1 ||
        eMail.indexOf(",")  != -1 ||
        eMail.indexOf("\\") != -1 ||
        eMail.indexOf("/")  != -1)
    {
      alert("Email address is not valid! \n\n" +
            "Folowing rules are used for e-mail check:\n\n" +
            "1. Must have more than 2 char\n" +
            "2. Must have @ in it\n" +
            "3. Must have minimum one dot in it\n" +
            "4. Must *not* have following characters in it: *',/\\\n");
      return false;
    }
    return true;
  }
  
  function HtmlForm_ValidateUrl(formName,url)
  {
    /*
    function validarURL(valor)
    {
      if (/^w+([.-]?w+)*.w+([.-]?w+)*(.w{2,3})+$/.test(valor))
      {
       return (true)
      } 
      else 
      {
        return (false);
      }
    }
    */
    if (url == null ||
        url.length < 3 ||
        url.indexOf(".") == -1)
    {
      alert("Url address is not valid! \n\n" +
            "Folowing rules are used for e-mail check:\n\n" +
            "1. Must have more than 2 char\n" +
            "2. Must have minimum one dot in it\n");
      return false;
    }
    return true;
  }
  
  function HtmlForm_ValidateRequiredFields(formName,validateSpecialFields)
  {
    var requiredFields = new Array();
    for (fieldName in window.aRegisteredHtmlPageForms[formName])
    {
      if (window.aRegisteredHtmlPageForms[formName][fieldName]["is_required_field"])
      {
        var formObject = window.document.forms[formName][fieldName];
        if (window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"] == "radio")
        {
          formObject = window.document.forms[formName][fieldName][0];
        }
        if (window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"] == "checkbox-group")
        {
          formObject = window.document.forms[formName][fieldName + "[]"][0];
        }      
        if (formObject != null)
        {
          var fieldIsOk = true;
          var fieldValue = null;
          if (formObject.type == "text")
          {
            fieldValue = formObject.value;
          }
          else if (formObject.type == "hidden")
          {
            fieldValue = formObject.value;
          }
          else if (formObject.type == "textarea")
          {
            fieldValue = formObject.value;
          }
          else if (formObject.type == "password")
          {
            fieldValue = formObject.value;
          }
          else if (formObject.type == "radio")
          {
            fieldValue = "";
            for (var i=0; i<window.document.forms[formName][fieldName].length; i++)
            {
              if (window.document.forms[formName][fieldName][i].checked == true)
              {
                formObject = window.document.forms[formName][fieldName][i];
                fieldValue = formObject.value;
              }
            }
          }
          else if (formObject.type == "checkbox" &&
                   window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"] == "checkbox-group")
          {
            fieldValue = "";
            for (var i=0; i<window.document.forms[formName][fieldName + "[]"].length; i++)
            {
              if (window.document.forms[formName][fieldName + "[]"][i].checked == true)
              {
                formObject = window.document.forms[formName][fieldName + "[]"][i];
                fieldValue = formObject.value;
              }
            }
          }        
          else if (formObject.type == "checkbox")
          {
            fieldValue = "";
            if (window.document.forms[formName][fieldName].checked == true)
            {
              formObject = window.document.forms[formName][fieldName];
              fieldValue = formObject.value;
            }
          }        
          else if (formObject.type == "select-one" &&
                   formObject.selectedIndex != -1)
          {
            fieldValue = HtmlSelect_GetSelectedValue(formName,fieldName);
          }
          else if (formObject.type == "file")
          {
            fieldValue = formObject.value;
          }
          else
          {
            alert("Unsupported input type: '" + formObject.type + "' for field with name '" + fieldName + "'!"); 
          }
          if (validateSpecialFields == true)
          {
            if (window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"] == "email")
            {
              if (HtmlForm_ValidateEmailAddress(formName,fieldValue) == false)
              {
                requiredFields = new Array();
                requiredFields.push(formObject);
                requiredFields.push(window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"]);
                requiredFields.push(window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_error_text"]);
                return requiredFields;
              }
            }
            else if (window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"] == "url")
            {
              if (HtmlForm_ValidateUrl(formName,fieldValue) == false)
              {
                requiredFields = new Array();
                requiredFields.push(formObject);
                requiredFields.push(window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"]);
                requiredFields.push(window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_error_text"]);
                return requiredFields;
              }
            }
          }
          else
          {
            if (fieldValue == window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_not_allowed_value"])
            {
              requiredFields.push(formObject);
              requiredFields.push(window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"]);
              requiredFields.push(window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_error_text"]);
            }
          }
        }
      }
    }
    return requiredFields;
  }
  
  function HtmlForm_ValidateAllFields(formName)
  {
    var requiredFields = HtmlForm_ValidateRequiredFields(formName,false);
    if (requiredFields.length > 0)
    {
      var alertMessage = "Following fields are required:\r\n";
      for (var i=0; i<requiredFields.length; i+=3)
      {
        alertMessage += "  " + requiredFields[i + 2] + "\r\n";
      }
      alert(alertMessage);
      if (requiredFields[0].type != "hidden")
      {
        requiredFields[0].focus();
      }
      return false;
    }
    requiredFields = HtmlForm_ValidateRequiredFields(formName,true);
    if (requiredFields.length > 0)
    {
      if (requiredFields[0].type != "hidden")
      {
        requiredFields[0].focus();
      }
      return false;
    }
    if (window.document.forms[formName]["password"] != null &&
        window.document.forms[formName]["password_confirm"] != null)
    {
      if (window.document.forms[formName]["password"].value != window.document.forms[formName]["password_confirm"].value)
      {
        alert("'Password' and 'Confirm Password' must be same values.");
        window.document.forms[formName]["password"].focus();
        return false;
      }
    }
    return true;
  }
  
  function HtmlHidden_IsExists(formName,hiddenName)
  {
    if (window.document.forms[formName] != null)
    {
      if (window.document.forms[formName][hiddenName] != null)
      {
        return true;
      }
      else
      {
        alert("HtmlHidden_IsExists(...) - Hidden field does not exists: '" + formName + "." + hiddenName + "'");
      }
    }
    else
    {
      alert("HtmlHidden_IsExists(...) - Form does not exists: '" + formName + "'");
    }
    return false;
  }
  
  function HtmlHidden_GetValue(formName,hiddenName)
  {
    if (HtmlHidden_IsExists(formName,hiddenName))
    {
      return window.document.forms[formName][hiddenName].value;
    }
    return "";
  }
  
  function HtmlHidden_SetValue(formName,hiddenName,value)
  {
    if (HtmlHidden_IsExists(formName,hiddenName))
    {
      window.document.forms[formName][hiddenName].value = value;
    }
    return "";
  }
  
  function HtmlSelect_IsExists(formName,selectName)
  {
    if (window.document.forms[formName] != null)
    {
      if (window.document.forms[formName][selectName] != null)
      {
        return true;
      }
      else
      {
        alert("HtmlSelect_IsExists(...) - Select does not exists: '" + formName + "." + selectName + "'");
      }
    }
    else
    {
      alert("HtmlSelect_IsExists(...) - Form does not exists: '" + formName + "'");
    }
    return false;
  }

  function HtmlSelect_Clear(formName,selectName)
  {
    if (HtmlSelect_IsExists(formName,selectName) == true)
    {
      var select = window.document.forms[formName][selectName];
      while (select.options.length > 0)
      {
        select.remove(0);
      }
      return true;
    }
    return false;
  }
  
  function HtmlSelect_AddOption(formName,selectName,value,text)
  {
    if (HtmlSelect_IsExists(formName,selectName) == true)
    {
      var select = window.document.forms[formName][selectName];
      select.options[select.options.length] = new Option(text,value);
      return true;
    }
    return false;
  }
  
  function HtmlSelect_GetSelectedIndex(formName,selectName)
  {
    if (HtmlSelect_IsExists(formName,selectName) == true)
    {
      return window.document.forms[formName][selectName].selectedIndex;
    }
    return -1;
  }
  
  function HtmlSelect_GetSelectedValue(formName,selectName)
  {
    if (HtmlSelect_IsExists(formName,selectName) == true)
    {
      return window.document.forms[formName][selectName].options[window.document.forms[formName][selectName].selectedIndex].value;
    }
    return null;
  }
  
  function HtmlForm()
  {
    window.aRegisteredHtmlPageForms = new Array();
  }
  
  HtmlForm.prototype.registerForm = function(formName)
  {
    if (!window.aRegisteredHtmlPageForms[formName])
    {
      window.aRegisteredHtmlPageForms[formName] = new Array();
    }
  }
  
  HtmlForm.prototype.registerFormField = function(formName,fieldName)
  {
    this.registerForm(formName);
    if (!window.aRegisteredHtmlPageForms[formName][fieldName])
    {
      window.aRegisteredHtmlPageForms[formName][fieldName] = new Array();
    }
  }
  
  HtmlForm.prototype.registerRequiredFieldValidator = function(formName,fieldName,fieldType,notAllowedValue,errorText)
  {
    while (fieldName.indexOf("[]") != -1)
    {
      fieldName = fieldName.replace("[]","");
    }
    this.registerFormField(formName,fieldName);
    window.aRegisteredHtmlPageForms[formName][fieldName]["is_required_field"] = true;
    window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_field_type"] = fieldType;
    window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_not_allowed_value"] = notAllowedValue;  
    window.aRegisteredHtmlPageForms[formName][fieldName]["rfv_error_text"] = errorText; 
  }
  
  HtmlForm.prototype.getSelectedIndex = function(formName,fieldName)
  {
    this.registerFormField(formName,fieldName);
    var formObject = window.document.forms[formName][fieldName];
    if (formObject != null &&
        formObject.type == "select-one")
    {
      return formObject.selectedIndex;
    }
    else
    {
      alert("Error at: HtmlForm.getSelectedIndex(..). Requested object is not select!");
    }
    return -1;
  }
  
  HtmlForm.prototype.getSelectedValue = function(formName,fieldName)
  {
    var selectedIndex = this.getSelectedIndex(formName,fieldName);
    if (selectedIndex != -1)
    {
      var formObject = window.document.forms[formName][fieldName];
      return formObject.options[selectedIndex].value;
    }
    return null;
  }
  
  HtmlForm.prototype.validateEmailAddress = function(eMail)
  {
    if (eMail == null ||
        eMail.length < 3 ||
        eMail.indexOf("@")  == -1 ||
        eMail.indexOf(".")  == -1 ||
        eMail.indexOf("*")  != -1 ||
        eMail.indexOf("'")  != -1 ||
        eMail.indexOf(",")  != -1 ||
        eMail.indexOf("\\") != -1 ||
        eMail.indexOf("/")  != -1)
    {
      alert("Email address is not valid! \n\n" +
            "Folowing rules are used for e-mail check:\n\n" +
            "1. Must have more than 2 char\n" +
            "2. Must have @ in it\n" +
            "3. Must have minimum one dot in it\n" +
            "4. Must *not* have following characters in it: *',/\\\n");
      return false;
    }
    return true;
  }
  
  HtmlForm.prototype.validateUrl = function(url)
  {
    /*
    function validarURL(valor)
    {
      if (/^w+([.-]?w+)*.w+([.-]?w+)*(.w{2,3})+$/.test(valor))
      {
       return (true)
      } 
      else 
      {
        return (false);
      }
    }
    */
    if (url == null ||
        url.length < 3 ||
        url.indexOf(".") == -1)
    {
      alert("Url address is not valid! \n\n" +
            "Folowing rules are used for e-mail check:\n\n" +
            "1. Must have more than 2 char\n" +
            "2. Must have minimum one dot in it\n");
      return false;
    }
    return true;
  }
  
  HtmlForm.prototype.onValidateRequiredFields = function(formName,validateSpecialFields)
  {
    return HtmlForm_ValidateRequiredFields(formName,validateSpecialFields);
  }
  
  HtmlForm.prototype.onValidateAllFields = function(formName)
  {
    return HtmlForm_ValidateAllFields(formName);
  }
  
  HtmlForm.prototype.onButtonSaveAndClose = function(formName)
  {
    if (this.onValidateAllFields(formName) == false)
    {
      return false;
    }
    return HtmlForm_Submit(formName,"save_and_close");
  }
  
  HtmlForm.prototype.onButtonGoBack = function(url)
  {
    document.location.href = url;
  }
  
  var clsHtmlForm = new HtmlForm();


