var xseed_validateMsg = "";
var xseed_validateControl = "";
var xseed_validateCount = 0;
var xseed_validateMoving = "";
var xseed_keepObject = ""; 
var xseed_keepValue = "";
var xseed_alertMsg = "";

var xseed_exceptionRaised = false;

var option_invokeMode = "";

// Xseed Methods _________________________________________________________

function xseed_clearElements()
{
   xseed_exceptionRaised = false;

   form.setAttribute("LastKeyCode", "");
   form.setAttribute("LastShiftKey", "");
   form.setAttribute("LastAltKey", "");
   form.setAttribute("LastCtrlKey", "");
}

function xseed_submitHandler()
{
    try
    {
       form.setAttribute("LastAction", "Submit");
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_submitHandler");
    }    
}

function xseed_clickHandler()
{
    try
    {
       if (xseed_exceptionRaised == true)
       {
	  return true;
       }

       return false;
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_clickHandler");
    }    
}

function xseed_onkeydown()
{
    try
    {
       form.setAttribute("LastKeyCode", event.keyCode);
       form.setAttribute("LastShiftKey", event.shiftKey);
       form.setAttribute("LastAltKey", event.altKey);
       form.setAttribute("LastCtrlKey", event.ctrlKey);

       if (event.keyCode == 34) 
       {
          form_submit();
       }

       if (event.shiftKey == true && event.keyCode == 123 ) // Shift-F12
       {
          var wException = getElement("form_exceptionMessage");

          if (wException != null)
          {
             alert( wException.value );
          }
       }		 
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_onkeydown");
    }    
}

function xseed_keydownHandler(pEvent)
{
    try
    {
       form.setAttribute("LastKeyCode", pEvent.keyCode);
       form.setAttribute("LastShiftKey", pEvent.shiftKey);
       form.setAttribute("LastAltKey", pEvent.altKey);
       form.setAttribute("LastCtrlKey", pEvent.ctrlKey);

       if (pEvent.keyCode == 34) 
       {
          form_submit();
       }

       if (pEvent.shiftKey == true && pEvent.keyCode == 123 ) // Shift-F12
       {
          var wException = getElement("form_exceptionMessage");

          if (wException != null)
          {
             alert( wException.value );
          }
       }		 
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_onkeydown");
    }    
}

function xseed_movingFocus()
{
    try
    {
        xseed_exceptionRaised = false; 

        var wElement;

        if (document.hasFocus() == false )
        {
           return true;
        }

        if (get("form_mode") == "PrepareQuery")
        {
           return true;
        }

        if (event.toElement == null)
        {   
           return true;
        }

        if (event.toElement.getAttribute("ValidateMode") != null)
        {
           var wValidateMode = event.toElement.getAttribute("ValidateMode");

           if (wValidateMode == "Ignore")
           {
              return true;
           }  
           else if (wValidateMode == "Customize")
           {
              return true;
           }  
           else if (wValidateMode == "CheckMaster")
           {
              if (event.toElement.getAttribute("MasterField") != null)
              {
                 if (event.toElement.getAttribute("MasterField") == event.srcElement.id)
                 {
                    return true;
                 }
                 else
                 {
                    return false;
                 }              
              } 
           }  
        } 

        return false;
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_movingFocus");
    }    
}

function xseed_onclickForCheckBox(pObject, pObjectName, pDatablock, pCheckedValue, pUncheckedValue)
{
   var wField = document.getElementById(pObjectName);
   var wInput = document.getElementById(pObjectName + "_checkbox");

   if ( wInput.checked == true )
   {
      wField.value = pCheckedValue;
   }
   else
   {
      wField.value = pUncheckedValue;
   }

   if (pDatablock != "")
   {
      xseed_setStatus(); 
   }
}

function xseed_onfocusin(pControl, pPanel)
{
    try 
    {
       xseed_pendingAlert();

       xseed_validateMoving = "";

       xseed_clearElements();

       if (xseed_validateControl != "" && xseed_validateControl != pControl.id)
       {
          if ( xseed_pendingValidation(pControl) )
          {
             return;  
          }  
       }

       var wActiveControl = document.getElementById("form_activeControl");

       if (wActiveControl == null)
       {
          return; 
       }

       var wActiveValue   = document.getElementById("form_activeValue");

       wActiveControl.value = pControl.id;

       if (xseed_keepObject == pControl.id)
       {
          wActiveValue.value = xseed_keepValue;
          xseed_keepObject = "";
          xseed_keepValue = "";
       }
       else
       {
          wActiveValue.value = pControl.value;
       }

       if (pPanel != null)
       {
          xseed_selectLine(pControl, pPanel);
       }
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_onfocusin");
    }    
}

function xseed_selectLine(pControl, pPanel)
{
    try 
    {
        if (pPanel != null)
        {
           var wActivePanel   = document.getElementById("form_activePanel");
           wActivePanel.value = pPanel;

           // Change the default value of the selected Panel
           if ( xseed_getStatus() == "INSERT")
           {
              form_getPanel(pPanel);

              for (i = 0; i < form_fieldName.length; i++)
              {
                  wField = document.getElementById(form_fieldName[i]);

                  if (wField.value == "" )
                  {
                     var wInitialValue = wField.getAttribute("InitialValue");

                     if (wInitialValue != null && wInitialValue != "" )
                     {
                        setProperty(wField, "InitialValue", wInitialValue);
                     } 
                  }
              }
           }

           // Change the backgroundColor of the selected Panel
           var wOccurrence = document.getElementById(pPanel + "_occurrence");

           if (wOccurrence != null)
           {
              var wParentPanel = document.getElementById(pPanel + "_parentPanel");
              var wSelected = document.getElementById(wParentPanel.value + "_selected");
              var wCurrent = document.getElementById(wParentPanel.value + "_current");

              // Clears the backgroundColor of the previous selected Panel

              form_getPanel(wParentPanel.value + "_panel_" + wSelected.value);

              for (i = 0; i < form_fieldName.length; i++)
              {
                  wField = document.getElementById(form_fieldName[i]);

                  if (getProperty(wField, "borderColor") == "")
                  {
                     setProperty(wField, "borderColor", "C0C0C0"); 
                  }

                  if (getProperty(wField, "borderStyle") == "")
                  {
                     setProperty(wField, "borderStyle", "solid");  
                  }

                  if (getProperty(wField, "borderWidth") == "")
                  {
                     setProperty(wField, "borderWidth", "1px");    
                  }

                  setProperty(wField, "backgroundColor", form_backColor[i]); 
              }

              form_getPanel(pPanel);

              // Change the backgroundColor of the selected Panel
              for (i = 0; i < form_fieldName.length; i++)
              {
                  wField = document.getElementById(form_fieldName[i]);

                  if (getProperty(wField, "borderColor") == "")
                  {
                     setProperty(wField, "borderColor", "C0C0C0"); 
                  }

                  if (getProperty(wField, "borderStyle") == "")
                  {
                     setProperty(wField, "borderStyle", "solid");  
                  }

                  if (getProperty(wField, "borderWidth") == "")
                  {
                    setProperty(wField, "borderWidth", "1px");    
                  }

                  setProperty(wField, "backgroundColor", "FFFF80");
              }

              wSelected.value = wOccurrence.value;
              wCurrent.value = wOccurrence.value;
           }
        }
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_selectLine");
    }    
}

function xseed_unselectLine()
{
    
}

function xseed_setStatus()
{
    var wActivePanel  = document.getElementById("form_activePanel");

    if (wActivePanel.value == "")
    {
       return;
    }

    var wFormStatus   = document.getElementById("form_status");
    var wFormMode     = document.getElementById("form_mode");
    var wPanelStatus  = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wToolbarMode  = getAttribute("toolbarMode");

    if (wToolbarMode.value != "Basic")
    {
       if (wFormMode.value != "PrepareQuery")
       {
          if (wPanelStatus.value == "NEW" || wRecordStatus.value == "NEW" || wRecordStatus.value == "INSERT")
          {
             wRecordStatus.value = "INSERT";
          }
          else
          {
             wRecordStatus.value = "CHANGED";
          }

          wFormStatus.value = "CHANGED";
          wPanelStatus.value = "CHANGED";
       }
    }
}

function xseed_getStatus()
{
    var wActivePanel  = document.getElementById("form_activePanel");

    if (wActivePanel.value == "")
    {
       return "";
    }

    var wFormStatus   = document.getElementById("form_status");
    var wFormMode     = document.getElementById("form_mode");
    var wPanelStatus  = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wToolbarMode  = getAttribute("toolbarMode");

    if (wToolbarMode.value != "Basic")
    {
       if (wFormMode.value != "PrepareQuery")
       {
          if (wPanelStatus.value == "NEW" || wRecordStatus.value == "NEW" || wRecordStatus.value == "INSERT")
          {
             return "INSERT";
          }
          else
          {
             return "CHANGED";
          }
       }
    }

    return "";
}

function xseed_clearValidation()
{
   xseed_validateMsg = "";
   xseed_validateControl = "";
   xseed_validateCount = 0; 
   xseed_validateMoving = "";

   xseed_keepObject = "";
   xseed_keepValue = "";
}

function xseed_ignoreValidation()
{
   xseed_pendingAlert();
    
   xseed_validateMsg = "";
   xseed_validateControl = "";
   xseed_validateCount = 0; 
   xseed_validateMoving = "";

   xseed_keepObject = "";
   xseed_keepValue = "";
}

function xseed_pendingValidation(pObject)
{
    xseed_pendingAlert();

    if (xseed_validateMsg == "" && xseed_validateControl == "")
    {
       return false;
    }
    else
    {
       xseed_validateCount++;

       if (xseed_validateCount < 4)
       {
          var wValidateControl = xseed_validateControl;

          if (xseed_validateMsg != "")
          {
             alert(xseed_validateMsg);
          }

          var wControl = document.getElementById(wValidateControl);

          xseed_validateMsg = "";
          xseed_validateControl = "";

          if (xseed_validateControl != pObject.id)
          {
             xseed_validateCount = 0; 
          } 

          if (wControl != null)
          {  
             xseed_validateMoving = "True";

             setFocus(wControl);
          }
       }

       return true;
    } 
}

function xseed_pendingAlert()
{
    if (xseed_alertMsg != "")
    {
       alert(xseed_alertMsg);
       xseed_alertMsg = "";
    }
}


function xseed_validateRequired(pObject)
{
    try
    {
       if (rTrim(pObject.value) == "")
       {
          raise(MSG_0001);
       }
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_validateRequired");
    }    
}

function xseed_validateHighest(pObject, pHighest)
{
    try
    {
       if (pObject.value > pHighest)
       {
          raise(MSG_0010  + " (" + pHighest + ")");
       }
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_validateHighest");
    }    
}

function xseed_validateLowest(pObject, pLowest)
{
    try
    {
       if (pObject.value < pLowest)
       {
          raise(MSG_0011 + " (" + pLowest + ")");
       }
    }
    catch (Err)
    {
       fatalMsg(Err, "xseed_validateHighest");
    }    
}

function xseed_openPopup(pObjectName)
{
   var wObject        = document.getElementById(pObjectName);
   var wHideColumns   = document.getElementById(wObject.id + "_hideColumns");
   var wListColumns   = document.getElementById(wObject.id + "_listColumns");
   var wListTitles    = document.getElementById(wObject.id + "_listTitles");
   var wSubmitColumns = document.getElementById(wObject.id + "_submitColumns");
   var wReturnFields  = document.getElementById(wObject.id + "_returnFields");
   var wPopupCaption  = document.getElementById(wObject.id + "_popupCaption");
   var wPopupHeight   = document.getElementById(wObject.id + "_popupHeight");
   var wPopupWidth    = document.getElementById(wObject.id + "_popupWidth");
   var wMaxRows       = document.getElementById(wObject.id + "_maxRows");
   var wPopupStyles   = document.getElementById(wObject.id + "_popupStyles");
   var wDataSrc       = document.getElementById(wObject.id + "_dataSrc");

   var wUrl = "XseedLOV";
   var wTitle = "_blank";

   wUrl = wUrl + "?ControlName=" + wObject.id;
   wUrl = wUrl + "&HideColumns=" + wHideColumns.value;
   wUrl = wUrl + "&ListColumns=" + wListColumns.value;
   wUrl = wUrl + "&ListTitles=" + wListTitles.value;
   wUrl = wUrl + "&SubmitColumns=" + wSubmitColumns.value;
   wUrl = wUrl + "&ReturnFields=" + wReturnFields.value;
   wUrl = wUrl + "&PopupCaption=" + wPopupCaption.value;
   wUrl = wUrl + "&PopupHeight=" + wPopupHeight.value;
   wUrl = wUrl + "&PopupWidth=" + wPopupWidth.value;
   wUrl = wUrl + "&MaxRows=" + wMaxRows.value;

   wDataSrc.value = wDataSrc.value.replace(/%/gi,'%25');
   wDataSrc.value = wDataSrc.value.replace(/\+/gi,'%2B');

   wUrl = wUrl + "&DataSrc=" + wDataSrc.value;

   window.open(wUrl, wTitle, wPopupStyles.value);
}

function form_validatePopup(pControl)
{
    var wBuffer = get(pControl.id + "_returnFields");
    var wFields = new Array();
    var wIndex = 0;
    var wEachElement;

    // ReturnField Array
    if (wBuffer.indexOf(",") == -1)
    {
       wFields[0] = wBuffer;
    }
    else
    {
       while (wBuffer.indexOf(",") != -1)
       {
          wFields[wIndex] = wBuffer.substr(0, wBuffer.indexOf(","));
          wBuffer = wBuffer.substr(wBuffer.indexOf(",") + 1);       
          wIndex++;
       }

       wFields[wIndex] = wBuffer;
    }

    // Check for pendingValidation 

    if ( get("form_activeControl") != "" && getElement( get("form_activeControl") ) != null )
    {
       var wActiveControl = getElement( get("form_activeControl") );
       var wMasterField  = getElement(pControl.id + "_masterField");

       if (wMasterField != null)
       {
          if (wMasterField.value == wActiveControl.id)
          { 
             xseed_ignoreValidation();
          } 
       }
       else
       {   
          for (i = 0; i < wFields.length; i++)
          {
              if (wFields[i] == wActiveControl.id)
              { 
                 xseed_ignoreValidation();
              } 
          }  
       } 

       if (xseed_validateControl != "")
       {
          if ( xseed_pendingValidation(pControl) )
          {
             return "Error";  
          } 
       }
    }

    // Validating AllowDelete, AllowInsert, AllowUpdate e AllowQuery
    var wActivePanel  = getElement("form_activePanel");

    if (wActivePanel != null && wActivePanel.value != "")
    {
       var wPanelStatus  = getElement(wActivePanel.value + "_panelStatus");
       var wRecordStatus = getElement(wActivePanel.value + "_recordStatus");
       var wMasterField  = getElement(pControl.id + "_masterField");
       var wMasterElement;


       if (wRecordStatus.value == "DELETE")
       {
          alert(MSG_0210); // Record selected to be deleted
          return "Error";
       }

       // Allow Insert and Allow Update validation
       var wFormMode = getElement("form_mode");

       if (wFormMode.value == "PrepareQuery")
       {
          form_getPanel(wActivePanel.value);

          for (i = 0; i < form_fieldName.length; i++) 
          {
              if (wMasterField != null)
              {
                  if (wMasterField.value == form_fieldName[i])
                  {  
                     if ( form_allowQuery[i] == 'False' ) 
                     {
                        alert(MSG_0203); // Field is protected against inquiry

                        wMasterElement = getElement(wMasterField.value);

                        setFocus(wMasterElement);
                        return "Error";
                     }
                  } 
              }
              else
              {   
                 for (j = 0; j < wFields.length; j++)
                 {
                     wEachElement = getElement(wFields[j]);

                     if (wEachElement != null && wEachElement.id == form_fieldName[i])
                     {  
                        if ( form_allowQuery[i] == 'False' ) 
                        {
                           alert(MSG_0203); // Field is protected against inquiry

                           setFocus(wEachElement);
                           return "Error";
                        }
                     } 
                 }  
              }
          }
       } 
       else
       { 
          if (wPanelStatus.value == "NEW" || wRecordStatus.value == "NEW" || wRecordStatus.value == "INSERT")
          {
             form_getPanel(wActivePanel.value);

             for (i = 0; i < form_fieldName.length; i++) 
             {
                 if (wMasterField != null)
                 {
                     if (wMasterField.value == form_fieldName[i])
                     {  
                        if ( form_allowInsert[i] == 'False' ) 
                        {
                           alert(MSG_0201); // Field is protected against insert

                           wMasterElement = getElement(wMasterField.value);
 
                           setFocus(wMasterElement);
                           return "Error";
                        }
                     } 
                 }
                 else
                 {   
                     for (j = 0; j < wFields.length; j++)
                     {
                         wEachElement = getElement(wFields[j]);

                         if (wEachElement != null && wEachElement.id == form_fieldName[i])
                         {  
                            if ( form_allowInsert[i] == 'False' ) 
                            {
                               alert(MSG_0201); // Field is protected against insert
 
                               setFocus(wEachElement);
                               return "Error";
                            }
                         } 
                     }  
                 }
             } 
          }
          else
          {
             form_getPanel(wActivePanel.value);

             for (i = 0; i < form_fieldName.length; i++) 
             {
                 if (wMasterField != null)
                 {
                     if (wMasterField.value == form_fieldName[i])
                     {  
                        if ( form_allowUpdate[i] == 'False' ) 
                        {
                           alert(MSG_0202); // Field is protected against update

                           wMasterElement = getElement(wMasterField.value);
 
                           setFocus(wMasterElement);

                           return "Error";
                        }
                     } 
                 }
                 else
                 {   
                     for (j = 0; j < wFields.length; j++)
                     {
                         wEachElement = getElement(wFields[j]);

                         if (wEachElement != null && wEachElement.id == form_fieldName[i])
                         {  
                            if ( form_allowUpdate[i] == 'False' ) 
                            {
                               alert(MSG_0202); // Field is protected against update
 
                               setFocus(wEachElement);

                               return "Error";
                            }
                         } 
                     }  
                 }
             } 
          }
       }
    }
    
    return "";
}

function xseed_dropdownClick(pName) 
{
    var wTextbox = getElement(pName + "_textbox");
    var wListbox = getElement(pName + "_listbox");
    var wDropdown = getElement(pName + "_dropdown");

    var wActiveControl = getElement( get("form_activeControl") );

    // Check for pendingValidation 
    if (wActiveControl.id == wTextbox.id || wActiveControl.id == wListbox.id)
    {
       xseed_ignoreValidation();
    }
    else
    {  
       if (xseed_validateControl != "")
       {
          if ( xseed_pendingValidation(wDropdown) )
          {
             return;  
          } 
       }
    } 

    var wActivePanel  = getElement("form_activePanel");

    if (wActivePanel != null && wActivePanel.value != "")
    {
       var wPanelStatus  = getElement(wActivePanel.value + "_panelStatus");
       var wRecordStatus = getElement(wActivePanel.value + "_recordStatus");

       if (wRecordStatus.value == "DELETE")
       {
          alert(MSG_0210); // Record selected to be deleted
          return;
       }

       // Allow Insert and Allow Update validation
       var wFormMode     = getElement("form_mode");

       if (wFormMode.value == "PrepareQuery")
       {
          form_getPanel(wActivePanel.value);

          for (i = 0; i < form_fieldName.length; i++) 
          {
              if (pName == form_fieldName[i])
              {
                 if ( form_allowQuery[i] == 'False' ) 
                 {
                    alert(MSG_0203); // Field is protected against inquiry
                    return;
                 }
                 break; 
              }
          }
       } 
       else
       { 
          if (wPanelStatus.value == "NEW" || wRecordStatus.value == "NEW" || wRecordStatus.value == "INSERT")
          {
             form_getPanel(wActivePanel.value);

             for (i = 0; i < form_fieldName.length; i++) 
             {
                 if (pName == form_fieldName[i])
                 {
                    if ( form_allowInsert[i] == 'False' ) 
                    {
                       alert(MSG_0201); // Field is protected against insert
                       return;
                    }
                    break; 
                 }
             }
          }
          else
          {
             form_getPanel(wActivePanel.value);

             for (i = 0; i < form_fieldName.length; i++) 
             {
                 if (pName == form_fieldName[i])
                 {
                    if ( form_allowUpdate[i] == 'False' ) 
                    {
                       alert(MSG_0202); // Field is protected against update
                       return;
                    }
                    break; 
                 }
             }
          }
       }
    }

    if (wListbox.style.visibility.toUpperCase() == 'HIDDEN') 
    {
        wListbox.style.top = parseInt(wTextbox.style.top) + parseInt(wTextbox.style.height);
        wListbox.style.left = wTextbox.style.left;
        wListbox.style.width = parseInt(wTextbox.style.width) + parseInt(wDropdown.style.width);

        wListbox.style.visibility = 'inherit';

        wListbox.options[0].selected = true;

        for (i = 0; i < wListbox.options.length; i++) 
        {
            if (wTextbox.value == wListbox.options[i].text)
            {
               wListbox.options[i].selected = true;
            }
        }

        setFocus(wListbox);
    } 
    else 
    {
        wListbox.style.visibility = 'hidden';
    }
}

function xseed_listboxBlur(pName) 
{
    wListbox = document.getElementById(pName + "_listbox");
    wListbox.style.visibility = 'hidden';
}

function xseed_listboxClick(pName) 
{
    var wTextbox = document.getElementById(pName + "_textbox");
    var wListbox = document.getElementById(pName + "_listbox");
    var wCombobox = document.getElementById(pName);

    for (i = 0; i < wListbox.options.length; i++) 
    {
        if (wListbox.options[i].selected == true) 
        {
            wTextbox.value = wListbox.options[i].text;

            if (wCombobox.value != wListbox.options[i].value)
            {
               wCombobox.value = wListbox.options[i].value;

               form_changeStatus(wCombobox);            
            }
        }
    }

    wListbox.style.visibility = 'hidden';

    setFocus(wTextbox);
}

function xseed_setValueForCombobox(pField) 
{
    var wTextbox = document.getElementById(pField.id + "_textbox");
    var wListbox = document.getElementById(pField.id + "_listbox");
    var wCombobox = pField;

    wTextbox.value = wCombobox.value;

    for (i = 0; i < wListbox.options.length; i++) 
    {
        if (wCombobox.value == wListbox.options[i].value)
        {
            wTextbox.value = wListbox.options[i].text;
        }
    }
}

function xseed_textboxKeyup(pName, pValue) 
{
    var wListbox = document.getElementById(pName + "_listbox");
    var wCombobox = document.getElementById(pName);

    wCombobox.value = pValue;

    for (i = 0; i < wListbox.options.length; i++) 
    {
        if (wListbox.options[i].text.toUpperCase() == pValue.toUpperCase()) 
        {
            wListbox.selectedIndex = i;
            wCombobox.value = wListbox.options[i].value;
			break;
        } 
    }
}

// Form Methods _________________________________________________________

function form_onfocusin(pControl, pPanel)
{
    var wActiveControl = document.getElementById("form_activeControl");
    var wActiveValue   = document.getElementById("form_activeValue");

    wActiveControl.value = pControl.id;
    wActiveValue.value = pControl.value;

    if (pPanel != null)
    {
       var wActivePanel   = document.getElementById("form_activePanel");
       var wFormStatus    = document.getElementById("form_status");
       var wPanelStatus   = document.getElementById(wActivePanel.value + "_panelStatus");

       wActivePanel.value = pPanel;

       if (wFormStatus.value != "CHANGED")
       {
          wFormStatus.value = wPanelStatus.value;
       }
    }
}

function form_onload()
{
    try
    {
        var wActiveControl = document.getElementById("form_activeControl");

        if (wActiveControl.value != "")
        {
           if (wActiveControl.value.toUpperCase() == "(PANEL)")
           {
              var wActivePanel = document.getElementById("form_activePanel");

              if (wActivePanel.value != "")
              { 
                 form_setPanelFocus(wActivePanel.value);   
              }
    
              wActiveControl = document.getElementById("form_activeControl");
           }

           var wControl = document.getElementById(wActiveControl.value);

           if (wControl != null)
           {
              setFocus(wControl);
           }
        }
    }
    catch (Err)
    {
       fatalMsg(Err, "form_onload");
    }    
}

function form_setActivePanel(pActivePanel)
{
    var wActivePanel = document.getElementById("form_activePanel");
    
    if (wActivePanel != null) 
    {
       wActivePanel.value = pActivePanel;
    }
}

function form_keyPress(pControl, pEvent, pDatatype, pAllowInsert, pAllowUpdate, pCaseRestriction)
{
    var wActivePanel  = document.getElementById("form_activePanel");
    var wField;

    var wFormMode     = document.getElementById("form_mode");

    if (wFormMode.value != "PrepareQuery")
    {
       if (wActivePanel.value != "")
       {
          // Allow Insert and Allow Update validation
          var wPanelStatus  = document.getElementById(wActivePanel.value + "_panelStatus");
          var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");

          if (wPanelStatus.value == "NEW" || wRecordStatus.value == "NEW" || wRecordStatus.value == "INSERT")
          {
             if (pAllowInsert == 'FALSE')
             {
                alert(MSG_0201); // Field is protected against insert
                pEvent.keyCode = 0;
                xseed_ignoreValidation();
                return;
             }
          }
          else
          {
             if (pAllowUpdate == 'FALSE')
             {
                alert(MSG_0202); // Field is protected against update
                pEvent.keyCode = 0;
                xseed_ignoreValidation();
                return;
             }
          }
       }

       // Numeric field validation
       if (pDatatype == 'NUMBER')
       {
          var wDecimalSeparator = document.getElementById("form_decimalSeparator"); // The default decimal separator is dot
          var wDecimalSeparatorKeyCode = 46; // The default decimal separator is dot

          if (wDecimalSeparator.value == ',')
          {
             wDecimalSeparatorKeyCode = 44;
          }

          if (pEvent.keyCode != 48 &&
              pEvent.keyCode != 49 &&
              pEvent.keyCode != 50 &&
              pEvent.keyCode != 51 &&
              pEvent.keyCode != 52 &&
              pEvent.keyCode != 53 &&
              pEvent.keyCode != 54 &&
              pEvent.keyCode != 55 &&
              pEvent.keyCode != 56 &&
              pEvent.keyCode != 57 &&
              pEvent.keyCode != wDecimalSeparatorKeyCode )
          {
              alert(MSG_0200); // Invalid KeyCode on numeric fields
              pEvent.keyCode = 0;
              xseed_ignoreValidation();
              return;
          }
       }

       // Case Restriction validation
       if (pCaseRestriction == 'UPPERCASE')
       {
          if (pEvent.keyCode >= 97 && pEvent.keyCode <= 122)
          {
             pEvent.keyCode = pEvent.keyCode - 32;
          }

          if (pEvent.keyCode == 231) { pEvent.keyCode = 199; }

          if (pEvent.keyCode == 225) { pEvent.keyCode = 193; }
          if (pEvent.keyCode == 233) { pEvent.keyCode = 201; }
          if (pEvent.keyCode == 237) { pEvent.keyCode = 205; }
          if (pEvent.keyCode == 243) { pEvent.keyCode = 211; }
          if (pEvent.keyCode == 250) { pEvent.keyCode = 218; }

          if (pEvent.keyCode == 226) { pEvent.keyCode = 194; }
          if (pEvent.keyCode == 234) { pEvent.keyCode = 202; }
          if (pEvent.keyCode == 238) { pEvent.keyCode = 206; }
          if (pEvent.keyCode == 244) { pEvent.keyCode = 212; }
          if (pEvent.keyCode == 251) { pEvent.keyCode = 219; }

          if (pEvent.keyCode == 227) { pEvent.keyCode = 195; }
          if (pEvent.keyCode == 245) { pEvent.keyCode = 213; }
       }
       else if (pCaseRestriction == 'LOWERCASE')
       {
          if (pEvent.keyCode >= 65 && pEvent.keyCode <= 90)
          {
             pEvent.keyCode = pEvent.keyCode + 32;
          }

          if (pEvent.keyCode == 199) { pEvent.keyCode = 231; }

          if (pEvent.keyCode == 193) { pEvent.keyCode = 225; }
          if (pEvent.keyCode == 201) { pEvent.keyCode = 233; }
          if (pEvent.keyCode == 205) { pEvent.keyCode = 237; }
          if (pEvent.keyCode == 211) { pEvent.keyCode = 243; }
          if (pEvent.keyCode == 218) { pEvent.keyCode = 250; }

          if (pEvent.keyCode == 194 ) { pEvent.keyCode = 226; }
          if (pEvent.keyCode == 202 ) { pEvent.keyCode = 234; }
          if (pEvent.keyCode == 206 ) { pEvent.keyCode = 238; }
          if (pEvent.keyCode == 212 ) { pEvent.keyCode = 244; }
          if (pEvent.keyCode == 219 ) { pEvent.keyCode = 251; }

          if (pEvent.keyCode == 195 ) { pEvent.keyCode = 227; }
          if (pEvent.keyCode == 213 ) { pEvent.keyCode = 245; }
       }

    }
}

function form_changeStatus(pControl)
{
    var wActivePanel  = document.getElementById("form_activePanel");

    if (wActivePanel == null || wActivePanel.value == "")
    {
       return;       
    }

    var wFormStatus   = document.getElementById("form_status");
    var wFormMode     = document.getElementById("form_mode");
    var wPanelStatus  = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wToolbarMode  = getAttribute("toolbarMode");

    if (wToolbarMode.value != "Basic")
    {
       if (wFormMode.value != "PrepareQuery")
       {
          if (wPanelStatus.value == "NEW" || wRecordStatus.value == "NEW" || wRecordStatus.value == "INSERT")
          {
             wRecordStatus.value = "INSERT";
             // setProperty(pControl, "borderColor", "green");
             // setProperty(pControl, "borderStyle", "solid");
             // setProperty(pControl, "borderWidth", "1px");
          }
          else
          {
             wRecordStatus.value = "CHANGED";
             // setProperty(pControl, "borderColor", "yellow");
             // setProperty(pControl, "borderStyle", "solid");
             // setProperty(pControl, "borderWidth", "1px");
          }
          wFormStatus.value = "CHANGED";
          wPanelStatus.value = "CHANGED";
       }
    }
}


function form_changeStatusForLOV(pControl)
{
    if (event.propertyName != "value")
    {
       return;
    }

    var wActivePanel  = document.getElementById("form_activePanel");
    var wFormStatus   = document.getElementById("form_status");
    var wFormMode     = document.getElementById("form_mode");
    var wPanelStatus  = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wToolbarMode  = getAttribute("toolbarMode");

    if (wToolbarMode.value != "Basic")
    {
       if (wFormMode.value != "PrepareQuery")
       {
          var wBuffer = get(pControl.id + "_returnFields");
          var wFields = new Array();
          var wIndex = 0;
          var wEachElement;

          if (wBuffer.indexOf(",") == -1)
          {
             wFields[0] = wBuffer;
          }
          else
          {
             while (wBuffer.indexOf(",") != -1)
             {
                wFields[wIndex] = wBuffer.substr(0, wBuffer.indexOf(","));
                wBuffer = wBuffer.substr(wBuffer.indexOf(",") + 1);       
                wIndex++;
             }

             wFields[wIndex] = wBuffer;
          }

          if (wPanelStatus.value == "NEW" || wRecordStatus.value == "NEW" || wRecordStatus.value == "INSERT")
          {
             wRecordStatus.value = "INSERT";

             for (i = 0; i < wFields.length; i++)
             {
                 wEachElement = getElement(wFields[i]);

                 if (wEachElement != null)
                 {  
                    // setProperty(wEachElement, "borderColor", "green");
                    // setProperty(wEachElement, "borderStyle", "solid");
                    // setProperty(wEachElement, "borderWidth", "1px");
                 } 
             }  
          }
          else
          {
             wRecordStatus.value = "CHANGED";

             for (i = 0; i < wFields.length; i++)
             {
                 wEachElement = getElement(wFields[i]);

                 if (wEachElement != null)
                 {  
                    // setProperty(wEachElement, "borderColor", "yellow");
                    // setProperty(wEachElement, "borderStyle", "solid");
                    // setProperty(wEachElement, "borderWidth", "1px");
                 }
             } 
          }

          wFormStatus.value = "CHANGED";
          wPanelStatus.value = "CHANGED";

       }
    }
}

function form_clearPanel(pPanel)
{
    var wPanelStatus = document.getElementById(pPanel + "_panelStatus");
    var wRecordStatus = document.getElementById(pPanel + "_recordStatus");
    var wRowid        = document.getElementById(pPanel + "_rowid");
    var wField;

    form_getPanel(pPanel);

    // Clear panel fields
    for (i = 0; i < form_fieldName.length; i++)
    {
        wField = document.getElementById(form_fieldName[i]);

        setProperty(wField, "value", "");  

        if (wField.style.borderStyle.toUpperCase() != 'NONE')
        {
           setProperty(wField, "borderColor", "C0C0C0"); 
           setProperty(wField, "borderStyle", "solid");  
           setProperty(wField, "borderWidth", "1px");    
        }

        if ( form_allowInsert[i] == 'True' ) 
        {
           setProperty(wField, "readOnly", "false"); 
        }
        else
        {
           setProperty(wField, "readOnly", "true"); 
        }
    }

    wPanelStatus.value = "NEW";
    wRecordStatus.value = "NEW";
    wRowid.value = "";
}

function form_executeQuery() 
{
    var wActivePanel = document.getElementById("form_activePanel");
    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wFormStatus = document.getElementById("form_status");
    var wPanelStatus = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wDynamicWhere = getAttribute("dynamicWhere");
    var wToolbarMode  = getAttribute("toolbarMode");
    var wGroupingSeparator = document.getElementById("form_groupingSeparator");
    var wDecimalSeparator = document.getElementById("form_decimalSeparator");
    var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");

    var wAllowQuery;
    var wField;
    var wFieldValue;
    var wSql = "";
    var wOperator = "";

    // Validation 
    if ( wParentPanel.value == '(None)')
    {
       wAllowQuery = document.getElementById(wActivePanel.value + "_allowQuery");
    }
    else
    {
       wAllowQuery = document.getElementById(wParentPanel.value + "_allowQuery");
    }

    if ( wAllowQuery.value == 'False')
    {
       alert(MSG_0109); // Query not allowed
       return;
    }

    if ( wToolbarMode.value != "Basic" && wFormStatus.value == "CHANGED") 
    {
       if ( confirm(MSG_0105) ) // Do you want to save the changes you have made? 
       {
          form_saveChanges();
          return;
       }
    }

    if ( wParentPanel.value != '(None)')
    {
       form_setActivePanel(wParentPanel.value + "_panel_0");
       set(wParentPanel.value + "_selected", "0");
       set(wParentPanel.value + "_current", "0");
    }

    form_getActivePanel();

    if ( wToolbarMode.value.toUpperCase() == "BASIC" || wFormMode.value == "PrepareQuery") {
        for (i = 0; i < form_fieldName.length; i++)
        {
            if ( form_allowQuery[i] == 'True' && form_columnName[i] != "undefined")
            {
               wField = document.getElementById(form_fieldName[i]);
               wFieldValue = replaceAll(wField.value, ' ', '');

               if (wFieldValue != '') {
                  // Replacing GroupingSeparator and DecimalSeparator
                  if (form_dataType[i] == "Number")
                  {
                     wFieldValue = replaceAll(wField.value, wGroupingSeparator.value, '');
                     wFieldValue = replaceAll(wFieldValue, wDecimalSeparator.value, '.');
                  }
                  else
                  {
                     wFieldValue = wField.value;
                  }
                  if (wField.value.indexOf("%") != -1) {
                      wSql = wSql + wOperator + form_columnName[i] +  " like '" + wFieldValue + "' ";
                  } else if (wField.value.indexOf("#") != -1) {
                      wSql = wSql + wOperator + form_columnName[i] +  " " + wFieldValue.substr(wField.value.indexOf("#") + 1) +  " ";
                  } else if (wField.value.charAt(0) == '>') {
                      wSql = wSql + wOperator + form_columnName[i] +  " " + wFieldValue +  " ";
                  } else if (wField.value.charAt(0) == '<') {
                      wSql = wSql + wOperator + form_columnName[i] +  " " + wFieldValue +  " ";
                  } else if (wField.value.charAt(0) == '=') {
                      wSql = wSql + wOperator + form_columnName[i] +  " " + wFieldValue +  " ";
                  } else if (wField.value.charAt(0) == '!') {
                      wSql = wSql + wOperator + form_columnName[i] +  " " + wFieldValue +  " ";
                  } 
                  else 
                  {
                      if (form_dataType[i] == "Number") 
                      {
                          wSql = wSql + wOperator + form_columnName[i] +  " = " + wFieldValue +  " ";
                      } 
                      else if (form_dataType[i] == "Date") 
                      {
                          if (form_mask[i] == "undefined")
                          {
                             wSql = wSql + wOperator + form_columnName[i] +  " = TO_DATE('" + wFieldValue +  "', 'yyyy-mm-dd') ";
                          }
                          else
                          {
                             wSql = wSql + wOperator + form_columnName[i] +  " = TO_DATE('" + wFieldValue +  "', '" + form_mask[i] + "') ";
                          }
                      } 
                      else 
                      {
                          wSql = wSql + wOperator + form_columnName[i] +  " = '" + wFieldValue +  "' ";
                      }
                  }

                  wOperator = " and ";
               }
            }
        }
        wDynamicWhere.value = wSql;
    }

    wFormMode.value = "ExecuteQuery";
    wFormEvent.value = "ExecuteQuery";
    wFormStatus.value = "QUERY";
    wPanelStatus.value = "QUERY";
    wRecordStatus.value = "QUERY";

    form_submit();
}

function form_executeInsert()
{
    var wActivePanel  = document.getElementById("form_activePanel");
    var wFormEvent = document.getElementById("form_event");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");

    form_getActivePanel();
    wRecordStatus.value = "INSERT";
    wFormEvent.value = "ExecuteInsert";

    form_submit();
}

function form_executeUpdate()
{
    var wActivePanel  = document.getElementById("form_activePanel");
    var wFormEvent = document.getElementById("form_event");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");

    form_getActivePanel();
    wRecordStatus.value = "UPDATE";
    wFormEvent.value = "ExecuteUpdate";

    form_submit();
}

function form_executeDelete()
{
    var wActivePanel  = document.getElementById("form_activePanel");
    var wFormEvent = document.getElementById("form_event");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");

    if ( confirm("Do you want to delete the current record?") ) 
    {
       form_getActivePanel();
       wRecordStatus.value = "DELETE";
       wFormEvent.value = "ExecuteDelete";

       form_submit();
    }
}

function form_saveChanges() 
{
    if (xseed_pendingValidation(this)) 
    {
       return;
    }

    var wFormStatus = document.getElementById("form_status");
    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");

    if (wFormStatus.value == "CHANGED") 
    {
        // wFormStatus.value = "NEW";
        wFormMode.value = "ExecuteCommit";
        wFormEvent.value = "SaveChanges";

        form_submit();
    } 
    else 
    {
        alert(MSG_0110); // No changes to be saved
    }
}

function form_discardChanges(pObject) 
{
    var wFormStatus = document.getElementById("form_status");
    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wField;
    var wFirstField = -1;

    if (wFormStatus.value == "CHANGED") 
    {
       if ( confirm(MSG_0100) == false) 
       {
          if (pObject != null)
          {
             if (xseed_pendingValidation(pObject)) 
             {
                return;
             }
          } 

          return;
       }
    } 

    xseed_ignoreValidation();

    form_clear();

    form_getActivePanel();

    // Disable panel fields
    for (i = 0; i < form_fieldName.length; i++) 
    {
        wField = document.getElementById(form_fieldName[i]);
        
        setProperty(wField, "value", "");

        wField.setAttribute("OriginalValue", "");  

        if ( form_allowInsert[i] == 'True' ) 
        {
           setProperty(wField, "readOnly", "false"); 

           if (wField.type.toUpperCase() != "HIDDEN" && wField.style.visibility.toUpperCase() != "HIDDEN")
           {
              if (wFirstField == -1)
              {
                 wFirstField = i;
              }
           }
        }
        else
        {
           setProperty(wField, "readOnly", "true");
        }
    }

    // Set focus on first insert field
    if (wFirstField != -1)
    {
       wField = document.getElementById(form_fieldName[wFirstField]);

       setFocus(wField);
    }

    wFormStatus.value = "NEW";

}

function form_refresh()
{
    var wActivePanel = document.getElementById("form_activePanel");
    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wFormStatus = document.getElementById("form_status");
    var wPanelStatus = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wDynamicOrderBy = getAttribute("dynamicOrderBy");

    var wField;
    var wFirstField = -1;

    form_getActivePanel();

    // Clear panel fields
    for (i = 0; i < form_fieldName.length; i++)
    {
        wField = document.getElementById(form_fieldName[i]);
        
        setProperty(wField, "value", "");  

        if (wField.type.toUpperCase() != "HIDDEN" && wField.style.visibility.toUpperCase() != "HIDDEN")
        {
           if (wFirstField == -1)
           {
              wFirstField = i;
           }
        }
    }

    // Set focus on first query field
    if (wFirstField != -1)
    {
       wField = document.getElementById(form_fieldName[wFirstField]);
       setFocus(wField);
    }

    // Clear dynamicOrderBy clause
    wDynamicOrderBy.value = ''

    wFormStatus.value = "NEW";
    wPanelStatus.value = "NEW";
    wRecordStatus.value = "NEW";
    wFormMode.value = "Refresh";
    wFormEvent.value = "Refresh";
}

function form_prepareQuery(pObject) 
{
    var wActivePanel = document.getElementById("form_activePanel");
    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wFormStatus = document.getElementById("form_status");
    var wPanelStatus = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wDynamicOrderBy = getAttribute("dynamicOrderBy");
    var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");
    var wAllowQuery;

    var wField;
    var wFirstField = -1;
    var i = 0;
    var j = 0;

    if ( wParentPanel.value == '(None)')
    {
       wAllowQuery = document.getElementById(wActivePanel.value + "_allowQuery");

       if ( wAllowQuery.value == 'False')
       {
          alert(MSG_0104);  // Query is not allowed
          return;
       }

       if (wPanelStatus.value == "CHANGED") {
          if ( confirm(MSG_0105) ) // Do you want to save the changes you have made?
          {
             if (pObject != null)
             {
                if (xseed_pendingValidation(pObject)) 
                {
                   return;
                }
             } 

             form_saveChanges();
             return;
          }
       }

       xseed_ignoreValidation();

       form_clear(wActivePanel.value);
    }
    else
    {
       wAllowQuery = document.getElementById(wParentPanel.value + "_allowQuery");

       if ( wAllowQuery.value == 'False')
       {
          alert(MSG_0104); // Query is not allowed
          return;
       }

        var wOccurs = document.getElementById(wParentPanel.value + "_occurs");
        var wMax = parseInt(wOccurs.value);
        var wCurrentStatus = "";

        for (i = 0; i < wMax; i++)
        {
            wCurrentStatus = document.getElementById(wParentPanel.value + '_panel_' + i + '_panelStatus');

            if (wCurrentStatus.value == "CHANGED")
            {
               break;
            }
        }

        if (wCurrentStatus.value == "CHANGED")
        {
           if ( confirm(MSG_0105) ) // Do you want to save the changes you have made?
           {
              if (pObject != null)
              {
                 if (xseed_pendingValidation(pObject)) 
                 {
                    return;
                 }
              } 

              form_saveChanges();
              return;
          }
       }

        xseed_ignoreValidation();
        form_clear(wParentPanel.value);

        // lock all the panels of a collection panel, except the first one

        for (i = 1; i < wMax; i++)
        {
            form_getPanel(wParentPanel.value + "_panel_" + i);

            for (j = 0; j < form_fieldName.length; j++) 
            {
                wField = getElement(form_fieldName[j]);

                setProperty(wField, "readOnly", "true"); 
            }
        }

        form_setActivePanel(wParentPanel.value + "_panel_0");

        set(wParentPanel.value + "_selected", "0");
        set(wParentPanel.value + "_current", "0");
    }

    form_getActivePanel();

    // Disable panel fields
    for (i = 0; i < form_fieldName.length; i++) 
    {
        wField = document.getElementById(form_fieldName[i]);

        setProperty(wField, "value", "");  

        if ( form_allowQuery[i] == 'True' ) 
        {
           setProperty(wField, "readOnly", "false"); 

           if (wField.type.toUpperCase() != "HIDDEN" && wField.style.visibility.toUpperCase() != "HIDDEN")
           {
              if (wFirstField == -1)
              {
                 wFirstField = i;
              }
           }
        }
        else
        {
           setProperty(wField, "readOnly", "true"); 
        }
    }

    // Clear dynamicOrderBy clause
    wDynamicOrderBy.value = "";

    wFormStatus.value = "NEW";
    wPanelStatus.value = "NEW";
    wRecordStatus.value = "NEW";
    wFormMode.value = "PrepareQuery";
    wFormEvent.value = "PrepareQuery";

    // Set focus on first query field
    if (wFirstField != -1)
    {
       wField = document.getElementById(form_fieldName[wFirstField]);

       setFocus(wField);
    }
}

function form_prepareInsert() 
{
    if (xseed_pendingValidation(this)) 
    {
       return;
    }

    var wActivePanel = document.getElementById("form_activePanel");
    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wFormStatus = document.getElementById("form_status");
    var wPanelStatus = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");
    var wAllowInsert;
    var wField;
    var wFirstField = -1;

    if ( wParentPanel.value == '(None)')
    {
       // Validation 
       wAllowInsert = document.getElementById(wActivePanel.value + "_allowInsert");
       if ( wAllowInsert.value == 'False')
       {
          alert(MSG_0106); // Insert is not allowed
          return;
       }

       if (wPanelStatus.value == "CHANGED")
       {
          if ( confirm(MSG_0105) ) // Do you want to save the changes you have made?
          {
             form_saveChanges();
             return;
          }
       }

       form_clear(wActivePanel.value);
    }
    else
    {
       wAllowInsert = document.getElementById(wParentPanel.value + "_allowInsert");

       if ( wAllowInsert.value == 'False')
       {
          alert(MSG_0106); // Insert is not allowed
          return;
       }

       var wOccurs = document.getElementById(wParentPanel.value + "_occurs");
       var wMax = parseInt(wOccurs.value) - 1;
       var wLastPanelStatus = document.getElementById(wParentPanel.value + '_panel_' + wMax + '_panelStatus');

       if (wLastPanelStatus.value == "CHANGED")
       {
          if ( confirm(MSG_0105) ) // Do you want to save the changes you have made?
          {
             form_saveChanges();
             return;
          }
       }

       form_shift();
    }

    form_getActivePanel();

    // Disable panel fields
    for (i = 0; i < form_fieldName.length; i++) 
    {
        wField = document.getElementById(form_fieldName[i]);

        setProperty(wField, "value", "");  

        if ( form_allowInsert[i] == 'True' ) 
        {
           setProperty(wField, "readOnly", "false"); 

           if (wField.type.toUpperCase() != "HIDDEN" && wField.style.visibility.toUpperCase() != "HIDDEN")
           {
              if (wFirstField == -1)
              {
                 wFirstField = i;
              }
           }
        }
        else
        {
           setProperty(wField, "readOnly", "true"); 
        }
    }

    // Set focus on first insert field
    if (wFirstField != -1)
    {
       wField = document.getElementById(form_fieldName[wFirstField]);
       setFocus(wField);
    }

    if (wFormStatus.value != "CHANGED")
    {
        wFormStatus.value = "NEW";
    }
    wPanelStatus.value = "NEW";
    wRecordStatus.value = "NEW";
    wFormMode.value = "PrepareInsert";
    wFormEvent.value = "PrepareInsert";
}

function form_prepareDelete(pObject, pMethod) 
{
    var wActivePanel  = document.getElementById("form_activePanel");
    var wFormMode     = document.getElementById("form_mode");
    var wFormEvent    = document.getElementById("form_event");
    var wFormStatus   = document.getElementById("form_status");
    var wPanelStatus  = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wRowid        = document.getElementById(wActivePanel.value + "_rowid");
    var wParentPanel  = document.getElementById(wActivePanel.value + "_parentPanel");
    var wAllowDelete;

    var wField;

    // Validation 

    if ( wParentPanel.value == '(None)')
    {
       wAllowDelete = document.getElementById(wActivePanel.value + "_allowDelete");
    }
    else
    {
       wAllowDelete = document.getElementById(wParentPanel.value + "_allowDelete");
    }

    if ( wAllowDelete.value == 'False')
    {
       alert(MSG_0103); // Record can't be deleted
       return;
    }

    if (wPanelStatus.value == "NEW") 
    {
       form_shrink();
       return;
    }
    else if (wPanelStatus.value == "CHANGED") 
    {
       if (wRecordStatus.value == "DELETE")
       {
          alert(MSG_0108); // Record already selected to be deleted
          return;
       }
       else
       {
          if ( wRowid.value == "")
          {
             form_shrink();
             return;
          }
          else
          {
             if ( confirm(MSG_0105) ) // Do you want to save the changes you have made?
             {
                if (pObject != null)
                {
                   if (xseed_pendingValidation(pObject)) 
                   {
                      return;
                   }
                } 

                form_saveChanges();
                return;
             }
          }
       }
    }

    xseed_ignoreValidation();

    if ( confirm(MSG_0102) ) // Remove the current record
    {
       if (pMethod != null)
       {
          set("form_exception", "False");
          var wStatusMsg;

          if (option_invokeMode.toUpperCase() == "SUBMIT")
          {
	     invoke(pMethod);
             return;
          }
          else
          { 
	     ajaxInvoke(pMethod);

             wStatusMsg = ajaxGet("form_statusMsg");
          }

          option_invokeMode = "";

          if (wStatusMsg != null && wStatusMsg != "")
          {
             window.status = wStatusMsg; 
          }

          if (get("form_exception") == "True")
          {
	     form_setFocus( get("form_activePanel") );
	     return;
          }
       }

       form_getActivePanel();

       // Disable panel fields
       for (i = 0; i < form_fieldName.length; i++)
       {
           wField = document.getElementById(form_fieldName[i]);

           if (form_allowDelete[i] == 'True')
           {
              setProperty(wField, "readOnly", "true");  
              setProperty(wField, "borderColor", "red");
              setProperty(wField, "borderStyle", "solid");
              setProperty(wField, "borderWidth", "1px");
           }
       }

       wFormStatus.value = "CHANGED";
       wPanelStatus.value = "CHANGED";
       wRecordStatus.value = "DELETE";
       wFormMode.value = "PrepareDelete";
       wFormEvent.value = "PrepareDelete";
    }
}

function form_firstRecord() 
{
    form_navigate("FIRST");
}

function form_previousRecord() 
{
    form_navigate("PREVIOUS");
}

function form_nextRecord() 
{
    form_navigate("NEXT");
}

function form_lastRecord() 
{
    form_navigate("LAST");
}

function form_navigate(pAction) 
{
    var wActivePanel = document.getElementById("form_activePanel");
    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wFormStatus = document.getElementById("form_status");
    var wPanelStatus = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");

    // Validation 
    if ( form_validate() == null ) 
    {
       return;
    }

    if (pAction == "FIRST") 
    {
        wFormMode.value = "firstRecord";
        wFormEvent.value = "First";
        wRecordStatus.value = "FIRST";
    } 
    else if (pAction == "NEXT") 
    {
        wFormMode.value = "nextRecord";
        wFormEvent.value = "Next";
        wRecordStatus.value = "NEXT";
    } 
    else if (pAction == "PREVIOUS") 
    {
        wFormMode.value = "previousRecord";
        wFormEvent.value = "Previous";
        wRecordStatus.value = "PREVIOUS";
    } 
    else if (pAction == "LAST") 
    {
        wFormMode.value = "lastRecord";
        wFormEvent.value = "Last";
        wRecordStatus.value = "LAST";
    } 
    else 
    {
        wFormMode.value = "";
        wFormEvent.value = "";
        wRecordStatus.value = "";
    }

    wFormStatus.value = "QUERY";
    wPanelStatus.value  = "QUERY";

    // If DBCollection, clear the recordStatus of all the panels
    var wOccurrence = document.getElementById(wActivePanel.value + "_occurrence");

    if (wOccurrence != null)
    {
       var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");
       var wOccurs = document.getElementById(wParentPanel.value + "_occurs");

       // Restore backgroundColor of the entire DBCollection
       for (j = 0; j < parseInt(wOccurs.value); j++)
       {
           wRecordStatus = getElement(wParentPanel.value + "_panel_" + j + "_recordStatus");
           wRecordStatus.value = pAction;
       }
    }

    // Submit the web page
    form_submit();
}

function form_orderAscending()
{
    var wActivePanel = document.getElementById("form_activePanel");

    if (get("form_status") == "CHANGED") 
    {
       if ( confirm(MSG_0105) ) // Save Changes 
       {
          form_saveChanges();
          return;
       }
    }

    if (wActivePanel.value == "")
    {
        alert(MSG_0107); // No field selected
        return;
    }

    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wFormStatus = document.getElementById("form_status");
    var wPanelStatus = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wActiveControlId = document.getElementById("form_activeControl");

    if (wActiveControlId.value == "")
    {
        alert(MSG_0107); // No field selected
        return;
    }

    var wActiveControl = document.getElementById(wActiveControlId.value);

    if (wActiveControl == null)
    {
        alert(MSG_0107); // No field selected
        return;
    }

    var wDynamicOrderBy = getAttribute("dynamicOrderBy");
    var wField;
    var wPosition = 0;

    form_getActivePanel();

    for (i = 0; i < form_fieldName.length; i++)
    {
        if ( form_allowQuery[i] == 'True' )
        {
           wField = document.getElementById(form_fieldName[i]);

           if (wField.id == wActiveControl.id)
           {
              if (form_columnName[i] == "undefined")
              {
                 alert(MSG_0111); // Selected field is no database field
                 return;
              }

              wDynamicOrderBy.value = form_columnName[i] + ' asc';
           }
           else
           {
              wPosition = wActiveControl.id.indexOf("_textbox");

              if (wPosition != -1)
              {
                 if ( wField.id == wActiveControl.id.substring(0, wPosition) )
                 {
                    if (form_columnName[i] == "undefined")
                    {
                       alert(MSG_0111); // Selected field is no database field
                       return;
                    }

                    wDynamicOrderBy.value = form_columnName[i] + ' asc';
                 }
              }
           } 
        }
    }

    wFormStatus.value = "QUERY";
    wPanelStatus.value = "QUERY";
    wRecordStatus.value = "QUERY";
    wFormMode.value = "ExecuteQuery";
    wFormEvent.value = "OrderAscending";

    // If DBCollection, clear the recordStatus of all the panels
    var wOccurrence = document.getElementById(wActivePanel.value + "_occurrence");

    if (wOccurrence != null)
    {
       var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");
       var wOccurs = document.getElementById(wParentPanel.value + "_occurs");

       // Restore backgroundColor of the entire DBCollection
       for (j = 0; j < parseInt(wOccurs.value); j++)
       {
           wRecordStatus = getElement(wParentPanel.value + "_panel_" + j + "_recordStatus");
           wRecordStatus.value = "QUERY";
       }
    }

    form_submit();
}

function form_orderDescending()
{
    var wActivePanel   = document.getElementById("form_activePanel");

    if (get("form_status") == "CHANGED") 
    {
       if ( confirm(MSG_0105) ) // Save Changes 
       {
          form_saveChanges();
          return;
       }
    }

    if (wActivePanel.value == "")
    {
        alert(MSG_0107); // No field selected
        return;
    }

    var wFormMode = document.getElementById("form_mode");
    var wFormEvent = document.getElementById("form_event");
    var wFormStatus = document.getElementById("form_status");
    var wPanelStatus = document.getElementById(wActivePanel.value + "_panelStatus");
    var wRecordStatus = document.getElementById(wActivePanel.value + "_recordStatus");
    var wActiveControlId = document.getElementById("form_activeControl");

    if (wActiveControlId.value == "")
    {
        alert(MSG_0107); // No field selected
        return;
    }

    var wActiveControl = document.getElementById(wActiveControlId.value);

    if (wActiveControl == null)
    {
        alert(MSG_0107); // No field selected
        return;
    }

    var wDynamicOrderBy = getAttribute("dynamicOrderBy");
    var wField;
    var wPosition = 0;

    form_getActivePanel();

    for (i = 0; i < form_fieldName.length; i++)
    {
        if ( form_allowQuery[i] == 'True' )
        {
           wField = document.getElementById(form_fieldName[i]);

           if (wField.id == wActiveControl.id)
           {
              if (form_columnName[i] == "undefined")
              {
                 alert(MSG_0111); // Selected field is no database field
                 return;
              }

              wDynamicOrderBy.value = form_columnName[i] + ' desc';
           }
           else
           {
              wPosition = wActiveControl.id.indexOf("_textbox");

              if (wPosition != -1)
              {
                 if ( wField.id == wActiveControl.id.substring(0, wPosition) )
                 {
                    if (form_columnName[i] == "undefined")
                    {
                       alert(MSG_0111); // Selected field is no database field
                       return;
                    }

                    wDynamicOrderBy.value = form_columnName[i] + ' desc';
                 }
              }
           } 
        }
    }

    wFormStatus.value = "QUERY";
    wPanelStatus.value = "QUERY";
    wRecordStatus.value = "QUERY";
    wFormMode.value = "ExecuteQuery";
    wFormEvent.value = "OrderDescending";

    // If DBCollection, clear the recordStatus of all the panels
    var wOccurrence = document.getElementById(wActivePanel.value + "_occurrence");

    if (wOccurrence != null)
    {
       var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");
       var wOccurs = document.getElementById(wParentPanel.value + "_occurs");

       // Restore backgroundColor of the entire DBCollection
       for (j = 0; j < parseInt(wOccurs.value); j++)
       {
           wRecordStatus = getElement(wParentPanel.value + "_panel_" + j + "_recordStatus");
           wRecordStatus.value = "QUERY";
       }
    }

    form_submit();
}

function form_shift()
{
   xseed_ignoreValidation();

   var wActivePanel = document.getElementById("form_activePanel");
   var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");
   var wOccurs = document.getElementById(wParentPanel.value + "_occurs");

   var wMax = parseInt(wOccurs.value) - 1;
   var wInputPanel;
   var wOutputPanel;

   var wInputArray;
   var wOutputArray;

   var wInputField;
   var wOutputField;

   var wInputStatus;
   var wOutputStatus;

   var wInputIndex;
   var wOutputIndex;

   var wIndex = 0;
   var i = 0;

   for (wIndex = wMax; wIndex > -1; wIndex--)
   {
       wInputIndex = wIndex - 1;
       wOutputIndex = wIndex;

       wInputPanel = wParentPanel.value + '_panel_' + wInputIndex;
       wOutputPanel = wParentPanel.value + '_panel_' + wOutputIndex;

       form_getPanel(wInputPanel);
       wInputArray = form_fieldName;

       form_getPanel(wOutputPanel);
       wOutputArray = form_fieldName;

       // If the activePanel is already the outputPanel then clears only the outputArray
       if (wOutputPanel == wActivePanel.value)
       {
          wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_panelStatus');
          wOutputStatus.value = "NEW";

          wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_recordStatus');
          wOutputStatus.value = "";

          wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_rowid');
          wOutputStatus.value = "";

          for (i = 0; i < wOutputArray.length; i++)
          {
              clear(wOutputArray[i]); 
          }

          break;
       }

       // Shift the contents of the input panel into the output panel and clears the input panel
       wInputStatus = document.getElementById(wParentPanel.value + '_panel_' + wInputIndex + '_panelStatus');
       wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_panelStatus');
       wOutputStatus.value = wInputStatus.value;
       wInputStatus.value = "NEW";

       wInputStatus = document.getElementById(wParentPanel.value + '_panel_' + wInputIndex + '_recordStatus');
       wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_recordStatus');
       wOutputStatus.value = wInputStatus.value;
       wInputStatus.value = "";

       wInputStatus = document.getElementById(wParentPanel.value + '_panel_' + wInputIndex + '_rowid');
       wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_rowid');
       wOutputStatus.value = wInputStatus.value;
       wInputStatus.value = "";

       for (i = 0; i < wInputArray.length; i++)
       {
           move(wInputArray[i], wOutputArray[i]);

           clear(wInputArray[i]);
       }

       // If the activePanel is inputPanel then breaks the loop
       if (wInputPanel == wActivePanel.value)
       {
           break;
       }
    }
}

function form_shrink()
{
   xseed_ignoreValidation();

   var wActivePanel = document.getElementById("form_activePanel");

   if (wActivePanel == null || wActivePanel.value == "")
   {
      return;
   }

   var wParentPanel = document.getElementById(wActivePanel.value + "_parentPanel");

   if (wParentPanel == null || wParentPanel.value == "" || wParentPanel.value == "(None)")
   {
      return;
   }

   var wOccurs = document.getElementById(wParentPanel.value + "_occurs");

   var wMax = parseInt(wOccurs.value) - 1;
   var wInputPanel;
   var wOutputPanel;

   var wInputArray;
   var wOutputArray;

   var wInputField;
   var wOutputField;

   var wInputStatus;
   var wOutputStatus;

   var wInputIndex;
   var wOutputIndex;

   var wMin = 0;
   var wIndex = 0;
   var i = 0;

   for (wIndex = 0; wIndex <= wMax; wIndex++)
   {
       wInputPanel = wParentPanel.value + '_panel_' + wIndex;

       if (wInputPanel == wActivePanel.value)
       {
          wMin = wIndex;
          break;
       }
   }    

   for (wIndex = wMin; wIndex <= wMax; wIndex++)
   {
       wOutputIndex = wIndex;
       wOutputPanel = wParentPanel.value + '_panel_' + wOutputIndex;

       form_getPanel(wOutputPanel);
       wOutputArray = form_fieldName;

       // If the activePanel is already the outputPanel then clears only the outputArray
       if (wOutputIndex == wMax)
       {
          wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_panelStatus');
          wOutputStatus.value = "NEW";

          wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_recordStatus');
          wOutputStatus.value = "";

          wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_rowid');
          wOutputStatus.value = "";

          for (i = 0; i < wOutputArray.length; i++)
          {
              clear(wOutputArray[i]);
          }

          form_setMode(wOutputPanel);

          break;
       }


       wInputIndex = wIndex + 1;
       wInputPanel = wParentPanel.value + '_panel_' + wInputIndex;

       form_getPanel(wInputPanel);
       wInputArray = form_fieldName;

       // Shift the contents of the input panel into the output panel and clears the input panel
       wInputStatus = document.getElementById(wParentPanel.value + '_panel_' + wInputIndex + '_panelStatus');
       wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_panelStatus');
       wOutputStatus.value = wInputStatus.value;
       wInputStatus.value = "NEW";

       wInputStatus = document.getElementById(wParentPanel.value + '_panel_' + wInputIndex + '_recordStatus');
       wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_recordStatus');
       wOutputStatus.value = wInputStatus.value;
       wInputStatus.value = "";

       wInputStatus = document.getElementById(wParentPanel.value + '_panel_' + wInputIndex + '_rowid');
       wOutputStatus = document.getElementById(wParentPanel.value + '_panel_' + wOutputIndex + '_rowid');
       wOutputStatus.value = wInputStatus.value;
       wInputStatus.value = "";

       for (i = 0; i < wInputArray.length; i++)
       {
           move(wInputArray[i], wOutputArray[i]);

           clear(wInputArray[i]);
       }
    }

    // Checks if the current panel has rowid

    var wRowid = get(wActivePanel.value + "_rowid");
    var wNewPanel = wActivePanel.value;

    if (wRowid == "")
    {
       var wOccurrenceAlfa = get(wActivePanel.value + "_occurrence");
       var wOccurrence = parseInt(wOccurrenceAlfa) - 1;

       for (i = wOccurrence; i >= 0; i--)
       {
           wNewPanel = wParentPanel.value + '_panel_' + i;
           wRowid = get(wNewPanel + "_rowid");

           if (wRowid != "")
           {
              break;
           }           
       } 

   }    

   form_setPanelFocus(wNewPanel);

   var wActiveControl = getElement(get("form_activeControl"));  

   setFocus(wActiveControl);
}

function form_setMode(pPanel, pMode) 
{
    var wRecordStatus = getElement(pPanel + "_recordStatus");

    if (pMode == null)
    {
       var wRowid = document.getElementById(pPanel + "_rowid");

       if (wRowid.value == "")
       {
          pMode = "INSERT";
       }
       else
       {
          pMode = "UPDATE";
       }
    }

    form_getPanel(pPanel);

    if (pMode == "UPDATE")
    {
       if (wRecordStatus.value == "DELETE")
       {
          for (i = 0; i < form_fieldName.length; i++)
          {
              wField = document.getElementById(form_fieldName[i]);

              if (form_allowDelete[i] == 'True')
              {
                 setProperty(wField, "readOnly", "true"); 

                 setProperty(wField, "borderColor", "red");
                 setProperty(wField, "borderStyle", "solid");
                 setProperty(wField, "borderWidth", "1px");
              }
          }
       }
       else if (wRecordStatus.value == "CHANGED")
       {
          for (i = 0; i < form_fieldName.length; i++) 
          {
              wField = document.getElementById(form_fieldName[i]);

              if ( form_allowUpdate[i] == 'True' ) 
              {
                 setProperty(wField, "readOnly", "false"); 
              }
              else
              {
                 setProperty(wField, "readOnly", "true"); 
              }

              // setProperty(wField, "borderColor", "yellow");
              // setProperty(wField, "borderStyle", "solid");
              // setProperty(wField, "borderWidth", "1px");
          }
       }
       else
       {
          for (i = 0; i < form_fieldName.length; i++) 
          {
              wField = document.getElementById(form_fieldName[i]);

              if ( form_allowUpdate[i] == 'True' ) 
              {
                 setProperty(wField, "readOnly", "false"); 
              }
              else
              {
                 setProperty(wField, "readOnly", "true"); 
              }
          }
       }
    }
    else if (pMode == "INSERT")
    {
       if (wRecordStatus.value == "INSERT")
       {
          for (i = 0; i < form_fieldName.length; i++) 
          {
              wField = document.getElementById(form_fieldName[i]);

              if ( form_allowInsert[i] == 'True' ) 
              {
                 setProperty(wField, "readOnly", "false"); 
              }
              else
              {
                 setProperty(wField, "readOnly", "true"); 
              }

              // setProperty(wField, "borderColor", "green");
              // setProperty(wField, "borderStyle", "solid");
              // setProperty(wField, "borderWidth", "1px");
          }
       }
       else
       {
          for (i = 0; i < form_fieldName.length; i++) 
          {
              wField = document.getElementById(form_fieldName[i]);

              if ( form_allowInsert[i] == 'True' ) 
              {
                 setProperty(wField, "readOnly", "false"); 
              }
              else
              {
                 setProperty(wField, "readOnly", "true"); 
              }
          }
       }
    }
}

function form_setPanelFocus(pPanel) 
{
    var wRowid = document.getElementById(pPanel + "_rowid");
    var wFirstField = -1;

    if (wRowid.value == "")
    {
       pMode = "INSERT";
    }
    else
    {
       pMode = "UPDATE";
    }

    form_getPanel(pPanel);

    if (pMode == "UPDATE")
    {
       for (i = 0; i < form_fieldName.length; i++) 
       {
           wField = document.getElementById(form_fieldName[i]);
           
           if ( form_allowUpdate[i] == 'True' ) 
           {
              if (wField.type.toUpperCase() != "HIDDEN" && wField.style.visibility.toUpperCase() != "HIDDEN")
              {
                 if (wFirstField == -1)
                 {
                    wFirstField = i;
                    break;
                 }
              }
           }
       }
    }
    else if (pMode == "INSERT")
    {
       for (i = 0; i < form_fieldName.length; i++) 
       {
           wField = document.getElementById(form_fieldName[i]);

           if ( form_allowInsert[i] == 'True' ) 
           {
              if (wField.type.toUpperCase() != "HIDDEN" && wField.style.visibility.toUpperCase() != "HIDDEN")
              {
                 if (wFirstField == -1)
                 {
                    wFirstField = i;
                    break;
                 }
              }
           }
       }
    }

    // Set focus on first insert field
    if (wFirstField != -1)
    {
       var wActiveControl = document.getElementById("form_activeControl");  
       wActiveControl.value = form_fieldName[wFirstField];
    }
}

function form_setFocus(pPanel)
{
   form_setPanelFocus(pPanel);

   var wActiveControl = getElement(get("form_activeControl"));  

   setFocus(wActiveControl);
}

function form_populate(pXml)
{
    var wDocument;
    var wRoot;
    var wField;
    var wValue;

    if (window.ActiveXObject)
    {
         wDocument = new ActiveXObject("Microsoft.XMLDOM");

         if (!wDocument)
         {
            wDocument = new ActiveXObject("Msxml.DOMDocument");
         }

         wDocument.async = "false";
         wDocument.loadXML(pXml);
    }
    else
    {
         var wParser = new DOMParser();
         wDocument = wParser.parseFromString(pXml, "text/xml");
    }

    wRoot = wDocument.documentElement;

    if (wRoot == null)
    {
       alert(MSG_0300); // Xml contains invalid caracters
       return;
    }

    if (wRoot.childNodes == null)
    {
       alert(MSG_0300); // Xml contains invalid caracters
       return;
    }

    for (wCount = 0; wCount < wRoot.childNodes.length; wCount++)
    {
        if (wRoot.childNodes[wCount].nodeName != "form_alert")
        {
           wField = document.getElementById(wRoot.childNodes[wCount].nodeName);

           if (wField != null)
           {
              if (wRoot.childNodes[wCount].childNodes.length == 0)
              {
                  if (wField.value != "")
                  {
                     wField.value = "";
                  }
              }
              else
              {
                 for (wSubCount = 0; wSubCount < wRoot.childNodes[wCount].childNodes.length; wSubCount++)
                 {
                     wValue = wRoot.childNodes[wCount].childNodes[0].nodeValue;
                     wValue = wValue.replace(/&lt;/g, "<");
                     wValue = wValue.replace(/&amp;/g, "&");
               
                     if (wValue != wField.value)
                     {
                        wField.value = wValue;
                     }
                 }  
              }
           }
        }
    }
}

function form_parameters()
{
    var wForm = null;
    var wFormCount = 0;
    var wElement = null;
    var wElementCount = 0;
    var wParameters = "";

    if (document.forms.length > 0)
    {

       for (wFormCount = 0; wFormCount < document.forms.length; wFormCount++)
       {
          wForm = document.forms(wFormCount);

          if(wForm.elements.length > 0)
          {
             wElementCount = 0;

             for (wElementCount = 0; wElementCount < wForm.elements.length; wElementCount++)
             {
                wElement = wForm.elements(wElementCount);

                if (wElement.value != "")
                {
                   wParameters += "&" + wElement.id + "=" + wElement.value;
                }
             }
          }
       } 
    }

    return wParameters;
}

function form_initialize()
{

    var wForm = null;
    var wFormCount = 0;
    var wElement = null;
    var wElementCount = 0;

    if (document.forms.length > 0)
    {
       for (wFormCount = 0; wFormCount < document.forms.length; wFormCount++)
       {
          wForm = document.forms(wFormCount);
          if(wForm.elements.length > 0)
          {
             wElementCount = 0;
             for (wElementCount = 0; wElementCount < wForm.elements.length; wElementCount++)
             {
                 wElement = wForm.elements(wElementCount);
                 wElement.value = "";
             }
          }
       } 
    }
}

function form_unselectLine(pCollectionPanel)
{
     var wSelectedLine = getElement(pCollectionPanel + "_selected");

     if (wSelectedLine != null) 
     { 
          // Clears the backgroundColor of the previous selected Panel
          form_getPanel(pCollectionPanel + "_panel_" + wSelectedLine.value);

          for (i = 0; i < form_fieldName.length; i++)
          {
              wField = document.getElementById(form_fieldName[i]);

              if (getProperty(wField, "borderColor") == "")
              {
                 setProperty(wField, "borderColor", "C0C0C0"); 
              }

              if (getProperty(wField, "borderStyle") == "")
              {
                 setProperty(wField, "borderStyle", "solid");  
              }

              if (getProperty(wField, "borderWidth") == "")
              {
                 setProperty(wField, "borderWidth", "1px");    
              }

              setProperty(wField, "backgroundColor", form_backColor[i]); 
          }
    }
}


// Ajax Methods _________________________________________________

var ajax_requestHttp;
var ajax_responseText;
var ajax_optionPopulate = "True";
var ajax_optionDisplayAlerts = "True";
var ajax_optionGetBrowserValues = "True";

var ajax_submitFields;
var ajax_returnFields;

function ajaxGetAlerts()
{
   return ajax_getAlerts();
}

function ajax_getAlerts()
{
    var wDocument;
    var wRoot;
    var wTagName = "form_alert";
    var wTagValue = "";
    var wMessage = "";
    var wNewLine = "";
    var wXml = ajax_responseText;
    var wStatusMsg = document.getElementById("form_statusMsg");

    if (wStatusMsg != '')
    {
       window.status = wStatusMsg.value;
    }

    if (window.ActiveXObject)
    {
         wDocument = new ActiveXObject("Microsoft.XMLDOM");

         if (!wDocument)
         {
            wDocument = new ActiveXObject("Msxml.DOMDocument");
         }

         wDocument.async = "false";
         wDocument.loadXML(wXml);
    }
    else
    {
         var wParser = new DOMParser();
         wDocument = wParser.parseFromString(wXml, "text/xml");
    }

    wRoot = wDocument.documentElement;

    for (wCount = 0; wCount < wRoot.childNodes.length; wCount++)
    {
        if (wTagName == wRoot.childNodes[wCount].nodeName)
        {
           for (wSubCount = 0; wSubCount < wRoot.childNodes[wCount].childNodes.length; wSubCount++)
           {
               wTagValue = wRoot.childNodes[wCount].childNodes[0].nodeValue;
               wMessage = wMessage + wNewLine + wTagValue;
               wNewLine = "\n";
           }
        }
    }

    return wMessage;
}

function ajaxValidate(pObject, pMethod)
{
   ajax_validate(pObject, pMethod);
}

function ajax_validate(pObject, pMethod)
{
   try
   {
      if (pObject.value != pObject.getAttribute("OriginalValue"))
      {
         ajax_optionDisplayAlerts = "False";

         set("form_exception", "False");
         set("form_statusMsg", "");

         ajaxInvoke(pMethod);

         window.status = "";

         var wStatusMsg = ajaxGet("form_statusMsg");

         if (wStatusMsg != null && wStatusMsg != "")
         {
            window.status = wStatusMsg; 
         }

         var wException = ajaxGet("form_exception");
 
         if (wException == "True")
         {
            var wMsg = ajaxGetAlerts();
            raise(wMsg);
         }
         else
         {
            alertMsg(ajaxGetAlerts());
            // xseed_alertMsg = ajaxGetAlerts(); //Miranda
            // ajax_displayAlerts();
         }
      }
    }
    catch (Err)
    {
       fatalMsg(Err, "ajax_validate");
    }    
}

function ajaxInvokeUrl(pUrl, pMethod, pParameters, pPopulate)
{
   ajax_invokeUrl(pUrl, pMethod, pParameters, pPopulate);
}

function ajax_invokeUrl(pUrl, pMethod, pParameters, pPopulate)
{
    var wUrl = pUrl;
    var wParameters;
    var wResponse;
    var wWindowStatus = window.status;

    if (pParameters == null)
    {
       pParameters = "All";
    }

    if (pPopulate == null)
    {
       pPopulate = "Populate";
    }

    wParameters = "XseedSubmitMode=AjaxSubmit";

    if (ajax_optionGetBrowserValues.toUpperCase() == "FALSE")
    {
       wParameters = wParameters + "&XseedGetBrowserValues=False";

       ajax_optionGetBrowserValues = "True";
    }

    if (pMethod != null && pMethod != "")
    {
       wParameters = wParameters + "&XseedMethod="+ pMethod;
    }

    if (pParameters.toUpperCase() == 'ALL')
    {
       window.status = 'ajax_parameters ...';

       if (ajax_submitFields == null)
       {
          wParameters = wParameters + form_parameters();
       }
       else
       {
          wParameters = wParameters + ajax_submitParameters();
       }
    }
    else
    {
       wParameters = wParameters + "&" + pParameters;
    }

    window.status = 'ajax_post ...';

    wResponse = ajax_post(wUrl, wParameters);


    if (pPopulate.toUpperCase() == 'POPULATE')
    {
       if ( ajax_optionPopulate == "True" )
       {  
          window.status = 'ajax_populate ...';
          ajax_populate();
       }

       if ( ajax_optionDisplayAlerts == "True" )
       { 
          window.status = 'ajax_displayAlerts ...';
          ajax_displayAlerts();
       }
    }

    window.status = wWindowStatus;

    // Restore ajax properties
    ajax_optionPopulate = "True";
    ajax_optionDisplayAlerts = "True";
    ajax_submitFields = null;
}

function ajax_submitParameters()
{
   var wBuffer = ajax_submitFields;
   var wFields = new Array();
   var wIndex = 0;
   var wElement;
   var wParameters = "";

   if (wBuffer.indexOf(",") == -1)
   {
      wFields[0] = wBuffer;
   }
   else
   {
      while (wBuffer.indexOf(",") != -1)
      {
         wFields[wIndex] = wBuffer.substr(0, wBuffer.indexOf(","));
         // wFields[wIndex] = wFields[wIndex].trim();  //Miranda - revisar
         wBuffer = wBuffer.substr(wBuffer.indexOf(",") + 1);       
         wIndex++;
      }

      wFields[wIndex] = wBuffer;
      // wFields[wIndex] = wFields[wIndex].trim(); //Miranda - revisar
    }

    for (i = 0; i < wFields.length; i++)
    {
        wElement = getElement(wFields[i]);

        if (wElement != null)
        {  
           wParameters += "&" + wElement.id + "=" + wElement.value;
        } 
    }  

    return wParameters;
}

function ajaxGet(pField)
{
   return ajax_get(pField);
}

function ajax_get(pField)
{
    var wDocument;
    var wRoot;
    var wTagName;
    var wTagValue = "";
    var wXml = ajax_responseText;

    if (typeof pField == 'object')
    {
       wTagName = pField.value;
    }
    else
    {
       wTagName = pField;
    }

    if (window.ActiveXObject)
    {
         wDocument = new ActiveXObject("Microsoft.XMLDOM");

         if (!wDocument)
         {
            wDocument = new ActiveXObject("Msxml.DOMDocument");
         }

         wDocument.async = "false";
         wDocument.loadXML(wXml);
    }
    else
    {
         var wParser = new DOMParser();
         wDocument = wParser.parseFromString(wXml, "text/xml");
    }

    wRoot = wDocument.documentElement;

    for (wCount = 0; wCount < wRoot.childNodes.length; wCount++)
    {
        if (wTagName == wRoot.childNodes[wCount].nodeName)
        {
           for (wSubCount = 0; wSubCount < wRoot.childNodes[wCount].childNodes.length; wSubCount++)
           {
               wTagValue = wRoot.childNodes[wCount].childNodes[0].nodeValue;
               wTagValue = wTagValue.replace(/&lt;/g, "<");
               wTagValue = wTagValue.replace(/&amp;/g, "&");

               break;
           }
        }
    }

    return wTagValue;
}
function ajaxDisplayAlerts()
{
   ajax_displayAlerts();
}

function ajax_displayAlerts()
{
    var wDocument;
    var wRoot;
    var wTagName = "form_alert";
    var wTagValue = "";
    var wMessage = "";
    var wNewLine = "";
    var wXml = ajax_responseText;
    var wStatusMsg = document.getElementById("form_statusMsg");

    if (wStatusMsg != '')
    {
       window.status = wStatusMsg.value;
    }

    if (window.ActiveXObject)
    {
         wDocument = new ActiveXObject("Microsoft.XMLDOM");

         if (!wDocument)
         {
            wDocument = new ActiveXObject("Msxml.DOMDocument");
         }

         wDocument.async = "false";
         wDocument.loadXML(wXml);
    }
    else
    {
         var wParser = new DOMParser();
         wDocument = wParser.parseFromString(wXml, "text/xml");
    }

    wRoot = wDocument.documentElement;

    for (wCount = 0; wCount < wRoot.childNodes.length; wCount++)
    {
        if (wTagName == wRoot.childNodes[wCount].nodeName)
        {
           for (wSubCount = 0; wSubCount < wRoot.childNodes[wCount].childNodes.length; wSubCount++)
           {
               wTagValue = wRoot.childNodes[wCount].childNodes[0].nodeValue;
               wTagValue = wTagValue.replace(/&lt;/g, "<");
               wTagValue = wTagValue.replace(/&amp;/g, "&");

               wMessage = wMessage + wNewLine + wTagValue;
               wNewLine = "\n";
           }
        }
    }

    if (wMessage != "")
    {
       alert(wMessage);
    }
}

function ajaxPopulate()
{
   ajax_populate();
}

function ajax_populate()
{
    form_populate(ajax_responseText);
}

function ajax_request(pUrl, pParameters)
{
    var wUrl = pUrl;
    var wParameters = "XseedSubmitMode=AjaxRequest" + pParameters;
    var wResponse;

    wParameters = wParameters.replace(/%/gi,'%25');

    // window.status = 'ajax_post...';
    wResponse = ajax_post(wUrl, wParameters);

    return wResponse;
}

function ajax_submit()
{
    var wUrl = get("form_currentUrl");
    window.status = 'form_parameters...';
    var wParameters = "XseedSubmitMode=AjaxSubmit" + form_parameters();
    var wResponse;

    wParameters = wParameters.replace(/%/gi,'%25');

    window.status = 'ajax_post...';
    wResponse = ajax_post(wUrl, wParameters);

    window.status = 'form_populate...';
    ajax_populate();
    ajax_displayAlerts();
}

function ajaxInvoke(pMethod, pParameters, pPopulate)
{
   ajax_invoke(pMethod, pParameters, pPopulate);
}

function ajax_invoke(pMethod, pParameters, pPopulate)
{
    var wUrl = get("form_currentUrl");
    ajax_invokeUrl(wUrl, pMethod, pParameters, pPopulate);
}

function ajax_post(pUrl, pParameters)
{
   var wParameters;

   if (window.XMLHttpRequest)
   {
      ajax_requestHttp = new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      ajax_requestHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (ajax_requestHttp == null)
   {
      alert ("Browser does not support HTTP Request");
      return;
   }

   ajax_responseText = "";

   wParameters = url_encode(pParameters);

   // Using DoPost ...
   ajax_requestHttp.onreadystatechange = ajax_response;
   ajax_requestHttp.open("POST", pUrl, false);
   ajax_requestHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajax_requestHttp.send(wParameters);

   // Using DoGet ...
   //pUrl = pUrl + "?" + pParameters;
   //ajax_requestHttp.onreadystatechange = ajax_response;
   //ajax_requestHttp.open("GET", pUrl, false);
   //ajax_requestHttp.send(null);

   return ajax_responseText;
}
function ajax_response()
{
   if (ajax_requestHttp.readyState == 4 || ajax_requestHttp.readyState == "complete")
   {
      ajax_responseText = ajax_requestHttp.responseText;
   }
}

function url_encode(str) 
{  
    var hex_chars = "0123456789ABCDEF";  
    var noEncode = /^([a-zA-Z0-9\_\-\.\=\;\&\,])$/;  
    var n, strCode, hex1, hex2, strEncode = "";  

    for(n = 0; n < str.length; n++) 
    {  
        if (noEncode.test(str.charAt(n))) 
        {  
            strEncode += str.charAt(n);  
        } 
        else 
        {  
            strCode = str.charCodeAt(n);  
            hex1 = hex_chars.charAt(Math.floor(strCode / 16));  
            hex2 = hex_chars.charAt(strCode % 16);  
            strEncode += "%" + (hex1 + hex2);  
        }  
    }  

    return strEncode;  
}

// Generic Functions

// Utilities methods _____________________________________________________
function invoke(pMethod, pParameters)
{
    var wUrl = document.getElementById("form_currentUrl");
    invokeUrl(wUrl.value, pMethod, pParameters);
}

function invokeUrl(pUrl, pMethod, pParameters)
{
    var wUrl = pUrl;
    var wParameters;
    var wResponse;

    if (pParameters == null)
    {
       pParameters = "All";
    }

    wParameters = "XseedSubmitMode=AjaxSubmit";

    if (pMethod != null && pMethod != "")
    {
       var wMethod = document.getElementById("form_invokedMethod");
       wMethod.value = pMethod;
    }

    if (pParameters.toUpperCase() != 'ALL')
    {
       wParameters = "&" + pParameters;
    }

    xseed_submitHandler();
    form.submit();
}

function enable(pImageName)
{
   var wActiveObject   = document.getElementById(pImageName);
   var wDisabledObject = document.getElementById(pImageName + "_disabled");

   if (wDisabledObject != null)
   {
      wDisabledObject.style.visibility = "Hidden";  
   }

   if (wActiveObject != null)
   {
      wActiveObject.style.visibility = "Inherit";  

      var wValidateSpan = document.getElementById("_" + pImageName + "_validateSpan");

      if (wValidateSpan != null)
      {
         wValidateSpan.style.visibility = "Inherit";  
      }  
   }
}

function disable(pImageName)
{
   var wActiveObject   = document.getElementById(pImageName);
   var wDisabledObject = document.getElementById(pImageName + "_disabled");

   if (wActiveObject != null)
   {
      wActiveObject.style.visibility = "Hidden";  

      var wValidateSpan = document.getElementById("_" + pImageName + "_validateSpan");

      if (wValidateSpan != null)
      {
         wValidateSpan.style.visibility = "Hidden"; 
      }  
   } 

   if (wDisabledObject != null)
   {
      wDisabledObject.style.visibility = "Inherit";  
   }
}

function show(pObjectName)
{
   var wActiveObject   = document.getElementById(pObjectName);

   if (wActiveObject != null)
   {
      wActiveObject.style.visibility = "Inherit";  
   }
}

function hide(pObject)
{
   if (pObject == null || pObject.value == "")
   {
      return;
   }

   var wActiveObject   = document.getElementById(pObject.value);

   if (wActiveObject != null)
   {
      wActiveObject.style.visibility = "Hidden";  
   }
}

function menu_showBody(pObjectName)
{
   var wActiveObject   = document.getElementById(pObjectName + "Body");

   if (wActiveObject != null)
   {
      wActiveObject.style.visibility = "Inherit";  
   }
}

function menu_hideBody(pObjectName)
{
   var wActiveObject   = document.getElementById(pObjectName + "Body");

   if (wActiveObject != null)
   {
      wActiveObject.style.visibility = "Hidden";  
   }
}

function getAttribute(pAttribute)
{
   var wActivePanel  = getElement("form_activePanel");
   var wParentPanel  = getElement(wActivePanel.value + "_parentPanel");
   var wResult;

   if ( wParentPanel.value == '(None)')
   {
      wResult = getElement(wActivePanel.value + "_" + pAttribute);
   }
   else
   {
      wResult = getElement(wParentPanel.value + "_" + pAttribute);
   }

   return wResult;
}


function getElement(pObjectName)
{
   var wObject = document.getElementById(pObjectName);

   if (wObject != null)
   {
      return wObject;  
   }
   else
   {
      return null;
   }
}

function get(pObjectName)
{
   var wObject = document.getElementById(pObjectName);

   if (wObject != null)
   {
      return wObject.value;  
   }
   else
   {
      return null;
   }
}


function set(pObject, pValue)
{
   var wField; 

   if (typeof pObject == "string")
   {
      wField = getElement(pObject); 
   }
   else
   {
      wField = pObject;
   }

   if (wField != null)
   {
      wField.value = pValue;  
   }
}

function trace(pMsg)
{
   var wTraceMsg = document.getElementById("form_traceMsg");

   if (wTraceMsg.value == "")
   {
      wTraceMsg.value = pMsg;
   }
   else
   { 
      wTraceMsg.value = wTraceMsg.value + ", " + pMsg;
   }
}

function formatDate(pInputDate, pInputMask, pOutputMask)
{
   var wDay = 0;
   var wDayAlpha = "";

   var wMonth = 0;
   var wMonthAlpha = "";

   var wYear = 0;
   var wOutputDate = "";
   
   if ( checkDate(pInputDate, pInputMask) != "" )
   {
      return pInputDate;
   }

   // Parse input ____________________________________________________
   if (pInputMask == "ddmmyyyy")
   {
      wDay = parseInt(pInputDate.substring(0, 2), 10);
      wMonth = parseInt(pInputDate.substring(2, 4), 10);
      wYear = parseInt(pInputDate.substr(4), 10);
   }
   else if (pInputMask == "dd/mm/yyyy")
   {
      if (pInputDate.indexOf("/") != -1)
      {
         var wMonthAndYear = ""; 
         wDay = parseInt(pInputDate.substring(0, pInputDate.indexOf("/")), 10);

         wMonthAndYear = pInputDate.substr(pInputDate.indexOf("/") + 1); 
		 
         if (wMonthAndYear.indexOf("/") != -1)
         { 
            wMonth = parseInt(wMonthAndYear.substring(0, wMonthAndYear.indexOf("/")), 10);
			
            wYear = parseInt(wMonthAndYear.substr(wMonthAndYear.indexOf("/") + 1), 10);
         }
      }
   }

   // Fixes
   if (wDay < 10)
   {
      wDayAlfha = "0" + wDay;
   }
   else
   {
      wDayAlfha = "" + wDay;
   }

   if (wMonth < 10)
   {
      wMonthAlfha = "0" + wMonth;
   }
   else
   {
      wMonthAlfha = "" + wMonth;
   }

   // Output Parse ____________________________________________________

   if (pOutputMask == "ddmmyyyy")
   {
      wOutputDate = wDayAlfha + wMonthAlfha + wYear;
   }
   else if (pOutputMask == "dd/mm/yyyy")
   {
      wOutputDate = wDayAlfha + "/" + wMonthAlfha + "/" + wYear;
   }

   return wOutputDate;
}

function checkDate(pInputDate, pInputMask)
{
   var wDay = 0;
   var wDayAlpha = "";

   var wMonth = 0;
   var wMonthAlpha = "";

   var wYear = 0;
   var wOutputDate = "";
   
   // Parse input ____________________________________________________
   if (pInputMask == "ddmmyyyy")
   {
      wDay = parseInt(pInputDate.substring(0, 2), 10);
      wMonth = parseInt(pInputDate.substring(2, 4), 10);
      wYear = parseInt(pInputDate.substr(4), 10);
   }
   else if (pInputMask == "dd/mm/yyyy")
   {
      if (pInputDate.indexOf("/") != -1)
      {
         var wMonthAndYear = ""; 
         wDay = parseInt(pInputDate.substring(0, pInputDate.indexOf("/")), 10);

         wMonthAndYear = pInputDate.substr(pInputDate.indexOf("/") + 1); 
		 
         if (wMonthAndYear.indexOf("/") != -1)
         { 
            wMonth = parseInt(wMonthAndYear.substring(0, wMonthAndYear.indexOf("/")), 10);
			
            wYear = parseInt(wMonthAndYear.substr(wMonthAndYear.indexOf("/") + 1), 10);
         }
      }
   }

   // Fixes
   if (wDay < 10)
   {
      wDayAlfha = "0" + wDay;
   }
   else
   {
      wDayAlfha = "" + wDay;
   }

   if (wMonth < 10)
   {
      wMonthAlfha = "0" + wMonth;
   }
   else
   {
      wMonthAlfha = "" + wMonth;
   }

   // Date Validation   
   wOutputAux  = wDayAlfha + "/" + wMonthAlfha + "/" + wYear;
   var expReg  = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
  
   if ((wOutputAux.match(expReg)) && (wOutputAux != ""))
   {
      var dia = parseInt(wOutputAux.substring(0,2), 10);
      var mes = parseInt(wOutputAux.substring(3,5), 10);
      var ano = parseInt(wOutputAux.substring(6,10), 10);

      if ( (mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30 )
      {
         return MSG_0021 + " (" + wOutputAux + ")"; // Month contains only 30 days
      } 
      else
      {
         if (ano%4 != 0 && mes == 2 && dia > 28) 
         {
            return MSG_0023 + " (" + wOutputAux + ")"; // Month contains only 28 days
         } 
         else
         {
            if ( ano%4 == 0 && mes == 2 && dia > 29 )
            {
               return MSG_0022 + " (" + wOutputAux + ")"; // Month contains only 29 days
            } 
         }
      }
   } 
   else 
   {
      return MSG_0020 + " (" + wOutputAux + ")";
   }

   return "";

}

function rTrim(pText)
{
   var wText = "";
   var wConsider = false;

   if (pText == "")
   {
      return "";
   }

   for (i = pText.length - 1; i >= 0; i--)
   {
       if (wConsider == false && pText.charAt(i) != " ")
       {
          wConsider = true;
       }

       if (wConsider == true)
       {
          wText = wText + pText.charAt(i);   
       } 
   }  

   return wText;
}

function replaceAll (pText, pInputChar, pOutputChar)
{
   if (pInputChar == pOutputChar)
   {
      return pText;
   }

   while (pText.indexOf(pInputChar) != -1)
   {
      pText = pText.replace(pInputChar, pOutputChar);
   }

   return pText;
}

function nvl (pText, pValue)
{
   if (pText == '')
   {
      return pValue;
   }
   else
   {
      return pText;
   }
}

function move(pInputName, pOutputName)
{
   var wInputField = getElement(pInputName); 

   if (wInputField == null) 
   { 
      return; 
   } 

   var wOutputField = getElement(pOutputName); 
 
   if (wOutputField == null) 
   { 
      return; 
   } 

   wOutputField.value = wInputField.value;

   if ( wInputField.getAttribute("OriginalValue") != null )
   {
      wOutputField.setAttribute("OriginalValue", wInputField.getAttribute("OriginalValue"));
   }

   if (wInputField.type.toUpperCase() == "HIDDEN")
   {
      move(pInputName + "_textbox", pOutputName + "_textbox");

      if (isCheckbox(wOutputField))
      {
         updateCheckbox(wOutputField);
      }
   }
   else
   {
      wOutputField.readOnly = wInputField.readOnly;
      wOutputField.style.borderColor = wInputField.style.borderColor;
      wOutputField.style.borderStyle = wInputField.style.borderStyle;
      wOutputField.style.borderWidth = wInputField.style.borderWidth;
   }
}

function clear(pInputName)
{
   var wInputField = getElement(pInputName); 

   if (wInputField == null) 
   { 
      return; 
   } 

   wInputField.value = "";

   if ( wInputField.getAttribute("OriginalValue") != null )
   {
       wInputField.setAttribute("OriginalValue", "");
   }

   if (wInputField.type.toUpperCase() == "HIDDEN")
   {
      clear(pInputName + "_textbox");

      if (isCheckbox(wInputField))
      {
         updateCheckbox(wInputField);
      }
   }
   else
   {
      // wInputField.readOnly = "false"; //Miranda
      wInputField.style.borderColor = "C0C0C0"; 
      wInputField.style.borderStyle = "solid";  
      wInputField.style.borderWidth = "1px";    
   }
}

function setProperty(pObject, pProperty, pValue)
{
   var wField; 

   if (typeof pObject == "string")
   {
      wField = getElement(pObject); 
   }
   else
   {
      wField = pObject;
   }

   if (wField == null) 
   { 
      return; 
   } 

   if (pProperty.toUpperCase() == "INITIALVALUE")
   {
      wField.value = pValue;

      // Tratamento especial para combobox
      if (wField.type.toUpperCase() == "HIDDEN" && getElement(wField.id + "_textbox") != null)
      {
         xseed_setValueForCombobox(wField);          
      } 

      // Tratamento especial para checkbox
      if (isCheckbox(wField))
      {
         updateCheckbox(wField);
      }

      return;
   }

   if (wField.type.toUpperCase() == "HIDDEN")
   {
      if (pProperty.toUpperCase() == "VALUE")
      {
         wField.value = pValue;
      }

      setProperty(wField.id + "_textbox", pProperty, pValue);

      if (isCheckbox(wField))
      {
         updateCheckbox(wField);
      }
   }
   else
   {
      if (pProperty.toUpperCase() == "VALUE")
      {
         wField.value = pValue;
      }
      else if (pProperty.toUpperCase() == "BACKGROUNDCOLOR")
      {
         wField.style.backgroundColor = pValue;
      }
      else if (pProperty.toUpperCase() == "READONLY")
      {
         if (pValue.toUpperCase() == "TRUE")
         {
            wField.readOnly = true;
         }
         else
         {
            wField.readOnly = false;
         }
      }
      else if (pProperty.toUpperCase() == "BORDERCOLOR")
      {
         wField.style.borderColor = pValue;
      }
      else if (pProperty.toUpperCase() == "BORDERSTYLE")
      {
         wField.style.borderStyle = pValue;
      }
      else if (pProperty.toUpperCase() == "BORDERWIDTH")
      {
         wField.style.borderWidth = pValue;
      }
   }
}

function getProperty(pObject, pProperty)
{
   var wField; 

   if (typeof pObject == "string")
   {
      wField = getElement(pObject); 
   }
   else
   {
      wField = pObject;
   }

   if (wField == null) 
   { 
      return; 
   } 

   if (wField.type.toUpperCase() == "HIDDEN")
   {
      if (pProperty.toUpperCase() == "VALUE")
      {
         return wField.value;
      }

      if (isCheckbox(wField))
      {
         return getProperty(wField.id + "_checkbox", pProperty);
      }

      if (isCombobox(wField))
      {
         return getProperty(wField.id + "_textbox", pProperty);
      }
   }
   else
   {
      if (pProperty.toUpperCase() == "VALUE")
      {
         return wField.value;
      }
      else if (pProperty.toUpperCase() == "BACKGROUNDCOLOR")
      {
         return wField.style.backgroundColor;
      }
      else if (pProperty.toUpperCase() == "READONLY")
      {
         if (wField.readOnly == true)
         {
            return "True";
         }
         else
         {
            return "False";
         }
      }
      else if (pProperty.toUpperCase() == "BORDERCOLOR")
      {
         return wField.style.borderColor;
      }
      else if (pProperty.toUpperCase() == "BORDERSTYLE")
      {
         return wField.style.borderStyle;
      }
      else if (pProperty.toUpperCase() == "BORDERWIDTH")
      {
         return wField.style.borderWidth;
      }
   }

   return "";
}

function appendSuffix(pArray, pSuffix)
{
   for (i = 0; i < pArray.length; i++)
   {
       pArray[i] = pArray[i] + pSuffix;
   }

   return pArray;
}

function copyArray(pInputArray)
{
   var wArray = new Array();

   for (i = 0; i < pInputArray.length; i++)
   {
       wArray[i] = pInputArray[i];
   }

   return wArray;
}

function nothing()
{
}

function isCombobox(pObject)
{
   var wField; 

   if (typeof pObject == "string")
   {
      wField = getElement(pObject); 
   }
   else
   {
      wField = pObject;
   }

   if (wField == null) 
   { 
      return; 
   } 

   var wCombobox = getElement(wField.id + "_textbox");
   
   if (wCombobox == null)
   {
       return false;
   }    

   return true;
}

function isCheckbox(pObject)
{
   var wField; 

   if (typeof pObject == "string")
   {
      wField = getElement(pObject); 
   }
   else
   {
      wField = pObject;
   }

   if (wField == null) 
   { 
      return; 
   } 

   var wCheckbox = getElement(wField.id + "_checkbox");
   
   if (wCheckbox == null)
   {
       return false;
   }    

   return true;
}

function updateCheckbox(pObject)
{
   var wField; 

   if (typeof pObject == "string")
   {
      wField = getElement(pObject); 
   }
   else
   {
      wField = pObject;
   }

   if (wField == null) 
   { 
      return; 
   } 

   var wCheckedValue = get(wField.id + "_checkedValue");
   var wUncheckedValue = get(wField.id + "_uncheckedValue");
   var wCheckbox = getElement(wField.id + "_checkbox");

   if (wField.value == wCheckedValue)
   {
      wCheckbox.checked = true; 
   }
   else
   {
      wCheckbox.checked = false; 
   }  
}

function exceptionRaised()
{
    if (xseed_exceptionRaised == true)
    {
       return true;
    }

    return false;
}

function raise(pMsg)
{
    try
    {
        xseed_exceptionRaised = true;

        if (pMsg != "")
        {
           alert(pMsg); 
        }

        try
        {
           event.srcElement.select();
        }
        catch (Err)
        {
           // Ignore Exception 
        }    

        event.cancelBubble = true;
   	event.returnValue = false;
    
    }
    catch (Err)
    {
       fatalMsg(Err, "raise");
    }    
}

function alertMsg(pMsg)
{
    try
    {
        if (pMsg != "")
        {
           alert(pMsg); 
        }

        if (event.toElement != null && event.toElement.getAttribute("ValidateMode") == "AlertOnly")
        {
           var wResultado;

           if (event.toElement.getAttribute("MemberOf") == null) 
           { 
              wResultado = event.toElement.fireEvent("onclick");
           } 
           else
           {
              var wField = getElement( event.toElement.getAttribute("MemberOf") );
              wResultado = wField.fireEvent("onclick");
           }
        }
    }
    catch (Err)
    {
       fatalMsg(Err, "alertMsg");
    }    
}

function fatalMsg(pException, pFunction)
{
    try
    {
       alert("Fatal error: " + pException.description + " executing " + pFunction + ".");

       setFocus(event.srcElement);
    }
    catch (Err)
    {
       alert("Fatal error: " + Err.description + " executing fatalMsg.");
    }    
}

function tabPressed()
{
    try
    {
       if (form.getAttribute("LastKeyCode") == "9" && form.getAttribute("LastShiftKey") == false)
       {
          return true; 
       }

       return false; 

    }
    catch (Err)
    {
       fatalMsg(Err, "tabPressed");
    }    
}

function shiftTabPressed()
{
    try
    {
       if (form.getAttribute("LastKeyCode") == "9" && form.getAttribute("LastShiftKey") == true)
       {
          return true; 
       }

       return false; 

    }
    catch (Err)
    {
       fatalMsg(Err, "shiftTabPressed");
    }    
}

function setFocus(pControl)
{
    try
    {
        pControl.focus();
        try
        {
           pControl.select();
        }
        catch (Err)
        {
           // exception will be ignored
        }    
    }
    catch (Err)
    {
        // exception will be ignored
    }    
}

function getKeyCode(pEvent)
{
    try
    {
         var wKeyCode = pEvent.keyCode?pEvent.keyCode:pEvent.charCode; 

         return wKeyCode;
    }
    catch (Err)
    {
       fatalMsg(Err, "getKeyCode");
    }    
  
}
