
var aRegisteredHtmlPageForms = new Array();

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 folowin chars 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 filedIsOk = true;
        var fieldValue = null;
        if (formObject.type == "text")
        {
          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 + "'!");
        }
        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);
    requiredFields[0].focus();
    return false;
  }
  requiredFields = HtmlForm_ValidateRequiredFields(formName,true);
  if (requiredFields.length > 0)
  {
    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)
{
  alert("getSelectedValue" + "=" + 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 folowin chars 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();

