var g_iFlashCount = 0;
var g_oFlashElem = null;

var g_aErrorMessage = new Array("A value must be provided for each required answer before the survey can be submitted.",
                                "You must provide a text value for options with text entry before the survey can be submitted.");

function Form_OnSubmit(aEvent)
{
  var oEvent = new EventObj(aEvent);
  InputForm_Submit(aEvent,oEvent);
  if (oEvent.GetReturnValue())
    {
      var oCell,oElem,oTextEntry,sBuf;
      var bFound = false;
      var aInputs = new Array();
      var aCells = document.getElementsByTagName("TD");
      for (var iLup = 0; iLup < aCells.length; iLup++)
        {
          oCell = aCells[iLup];
          
          if (oCell.id == "AnswerCell" && oCell.getAttribute("AnswerRequired") == "1")
            {
              oElem = GetFirstChild(oCell);
              while (oElem)
                {
                  if (oElem.tagName == "SELECT" && oElem.value == "0")
                    {
                      DoValidationAlert(0,oCell.parentNode.parentNode.parentNode)
                      oElem.focus();
                      return false;
                    }
                  else if (oElem.tagName == "INPUT" && oElem.type.toUpperCase() == "RADIO")
                    {
                      bFound = false;
                      aInputs = document.getElementsByName(oElem.name);
                      for (var i = 0; i < aInputs.length; i++)
                        if (aInputs[i].checked)
                          {
                            bFound = true;
                            break;
                          }
                      if (!bFound)
                        {
                          DoValidationAlert(0,oCell.parentNode.parentNode.parentNode);
                          return false;
                        }
                      else
                        {
                          oTextEntry = document.forms[0].elements["TextEntryAnswer" + aInputs[i].value];
                          if (oTextEntry)
                            {
                              // must have a value for selected options with text entry
                              sBuf = oTextEntry.value;
                              while (sBuf.indexOf(" ") > -1)
                                sBuf = sBuf.replace(" ","");
                              if (sBuf == "")
                                {
                                  DoValidationAlert(1,oCell.parentNode.parentNode.parentNode);
                                  return false;
                                }
                            }                          
                        }
                    }
                  else if (oElem.tagName == "INPUT" && oElem.type.toUpperCase() == "CHECKBOX")
                    {
                      bFound = false;
                      while (oElem)
                        {
                          if (oElem.tagName == "INPUT" && oElem.type.toUpperCase() == "CHECKBOX" && oElem.checked)
                            {
                              bFound = true;
                              
                              // make sure any text entry for this selected answer is filled out
                              oTextEntry = document.forms[0].elements["TextEntryAnswer" + oElem.value];
                              if (oTextEntry)
                                {
                                  // must have a value for selected options with text entry
                                  sBuf = oTextEntry.value;
                                  while (sBuf.indexOf(" ") > -1)
                                    sBuf = sBuf.replace(" ","");
                                  if (sBuf == "")
                                    {
                                      DoValidationAlert(1,oCell.parentNode.parentNode.parentNode);
                                      return false;
                                    }
                                }                          
                              
                            }

                          oElem = GetNextSibling(oElem);
                        }
                      if (!bFound)
                        {
                          DoValidationAlert(0,oCell.parentNode.parentNode.parentNode);
                          return false;
                        }                      
                    }
                  else if (oElem.tagName == "INPUT" && oElem.type.toUpperCase() == "HIDDEN")
                    {
                      // make sure any text entry for this selected answer is filled out
                      oTextEntry = document.forms[0].elements["TextEntryAnswer" + oElem.value];
                      if (oTextEntry)
                        {
                          // must have a value for selected options with text entry
                          sBuf = oTextEntry.value;
                          while (sBuf.indexOf(" ") > -1)
                            sBuf = sBuf.replace(" ","");
                          if (sBuf == "")
                            {
                              DoValidationAlert(1,oCell.parentNode.parentNode.parentNode);
                              return false;
                            }
                        }                          
                    }
                    
                  if (oElem)
                    oElem = GetNextSibling(oElem);
                }
            }
        }
    }
  else
    return false;
  
  document.forms[0].setAttribute("PassedValidation","1");

  // set the value on the parent page that indicates if required UDFs are missing
  if (window.parent && window.parent.SetIsMissingRequiredSurvey)
    window.parent.SetIsMissingRequiredSurvey(document.forms[0].elements["ShowSurveyKey"].value);

  return true;
}

function DoValidationAlert(iMessageIndex,oFlashElem)
{
  window.alert(g_aErrorMessage[iMessageIndex]);
  g_iFlashCount = 0;
  g_oFlashElem = oFlashElem;
  g_oFlashElem.scrollIntoView(true);
  window.setTimeout(FlashElem,100);
}

function FlashElem()
{
  g_oFlashElem.style.backgroundColor = (g_iFlashCount % 2) ? "#ffcccc" : "#ffffff";
  g_iFlashCount++;
  if (g_iFlashCount < 7)
    window.setTimeout(FlashElem,500);
}

function CheckMaxLength(oElem,iMaxLength)
{
  if (oElem.value.length > iMaxLength)
    oElem.value = oElem.value.substr(0,iMaxLength);
}

function SelectParentOption(oElem)
{
  var oOption = GetPrevSibling(oElem);
  while (oOption && oOption.tagName != "INPUT")
    oOption = GetPrevSibling(oOption);
    
  if (oOption && (oOption.type.toUpperCase() == "RADIO" || oOption.type.toUpperCase() == "CHECKBOX"))
    oOption.checked = true;
}

function InitValues()
{
  // initialize the selects
  var sInitVal = "";
  var aSelects = document.getElementsByTagName("SELECT");
  for (var iLup = 0; iLup < aSelects.length; iLup++)
    {
      sInitVal = aSelects[iLup].getAttribute("InitVal");
      if (sInitVal && sInitVal != "" && sInitVal != "NULL")
        aSelects[iLup].value = sInitVal;
    }
    
  // initialize the radios and checkboxes
  var oCell;
  var aCells = document.getElementsByTagName("TD");
  for (var iLup = 0; iLup < aCells.length; iLup++)
    {
      oCell = aCells[iLup];

      if (oCell.id == "AnswerCell")
        {
          oElem = GetFirstChild(oCell);
          if (oElem.tagName == "INPUT" && oElem.type.toUpperCase() == "RADIO")
            {
              sInitVal = oCell.getAttribute("InitVal");
              if (sInitVal && sInitVal != "" && sInitVal != "NULL")
                {
                  aInputs = document.getElementsByName(oElem.name);
                  for (var i = 0; i < aInputs.length; i++)
                    if (aInputs[i].value == sInitVal)
                      aInputs[i].checked = true;
                }
            }
          else if (oElem.tagName == "INPUT" && oElem.type.toUpperCase() == "CHECKBOX")
            {
              sInitVal = oCell.getAttribute("InitVal");
              if (sInitVal && sInitVal != "" && sInitVal != "NULL")
                {
                  while (oElem)
                    {
                      if (sInitVal.indexOf("|" + oElem.value + "|") > -1)
                        oElem.checked = true;
                      oElem = GetNextSibling(oElem);
                    }
                }
            }
        }
    }
    
  // handle uninitialized text entry
  var aInputs = document.getElementsByTagName("INPUT");
  for (var iLup = 0; iLup < aInputs.length; iLup++)
    {
      if (aInputs[iLup].type.toUpperCase() == "TEXT" && aInputs[iLup].value == "NULL")
        aInputs[iLup].value = "";
    }
  var aInputs = document.getElementsByTagName("TEXTAREA");
  for (var iLup = 0; iLup < aInputs.length; iLup++)
    {
      if (aInputs[iLup].value == "NULL")
        aInputs[iLup].value = "";
    }
}

