/**
 *  cms toplevel function library for the AJAX components
 *
 *  @author   Tobias Hettinger
 *  @version  $Id: cms_top.js,v 1.3 2007/12/07 10:46:43 tobias Exp $
 */
 
 
function cmsChangePasswordForm()
{
  watAjaxSendRequest(cmsAjaxApplication, 'task=sxu_cpf' + cmsSessionParam, WAT_AJAX_REQUEST_POST, 'cmsResponse');
}

function cmsResponse(requestId)
{
  //  get the used xmlHttpRequest for the request
  var xmlHttpRequest = WATAjaxRequestData[requestId][0];
  
  //  check if the request has been finished
  if (xmlHttpRequest.readyState == 4)
  {
    //  check if the request succeeded
    if (xmlHttpRequest.status == 200)
    {
      var ajaxResponse = xmlHttpRequest.responseXML.documentElement;
      if (ajaxResponse)
      {
        //  parse the ajax response
        var node = ajaxResponse.firstChild;
        while (node)
        {
          switch (node.nodeName)
          {
            //  the request requires a special java script
            case 'script' :
              var scripts = document.getElementsByTagName('script');
              var scriptUrl = node.firstChild.data;

              //  check if the script has not yet been loaded
              var found = false;
              for (var i in scripts)
                if (scripts[i].src == scriptUrl) found = true;
                
              if (!found)
              {
                //  the script was not loaded yet
                var scriptElement = document.createElement('script');
                scriptElement.type = 'text/javascript';
                scriptElement.src = scriptUrl;
                document.getElementsByTagName('head')[0].appendChild(scriptElement);
              }
              break;
          
            //  the content requires a special css file
            case 'style' :
              var styles = document.getElementsByTagName('link');
              var styleUrl = node.firstChild.data;
              
              //  check if the css file has not yet been loaded
              var found = false;
              for (var i in styles)
                if (styles[i].href == styleUrl) found = true;

              if (!found)
              {
                //  the css file was not loaded yet
                var styleElement = document.createElement('link');
                styleElement.rel = 'stylesheet';
                styleElement.type = 'text/css';
                styleElement.href = styleUrl;
                styleElement.media = 'screen,print';
                document.getElementsByTagName('head')[0].appendChild(styleElement); 
              }
              break;
          
            //  a form was sent
            case 'form' :
              //  initialize the data that is required for splitted transfer of the HTML code
              var htmlCode = '';
              var htmlData = null;
              var formKey = node.getAttribute('key');
              var caption = node.getAttribute('caption');
              var buttons = null;
              
              //  check the attributes
              if (!formKey) formKey = 'default';
              
              //  get the form data
              var formData = node.firstChild;
              while(formData)
              {
                switch(formData.nodeName)
                {
                  //  the html code to show was sent
                  case 'html' :
                    //  get the data parameter (show a form to add a new item if there is none)
                    htmlData = formData.getAttribute('data');
                    //  get the code
                    htmlCode = htmlCode + formData.firstChild.data;
                    break;
                    
                  //  a button has been sent
                  case 'button' :
                    //  get the attributes
                    var buttonCaption = formData.getAttribute('caption');
                    var buttonOnClick = formData.getAttribute('onclick');
                    
                    //  register the button
                    if (buttonCaption && buttonOnClick)
                    {
                      if (!buttons) buttons = new Array();
                      var button = new Array();
                      button['onclick'] = buttonOnClick;
                      button['caption'] = buttonCaption;
                      buttons.push(button);
                    }
                    break;
                }
                
                //  get the next part of the form data
                formData = formData.nextSibling;
              }
              
              //  show the form if there is some HTML code
              if (htmlCode) cmsDesktop.CreateForm(formKey, 'ajax_form', caption, htmlCode, buttons);
              break;
              
            //  close a form
            case 'closeform' :
              //  initialize the variables
              var formKey = node.getAttribute('key');
              if (!formKey) formKey = 'default';
              //  close the form
              cmsDesktop.CloseForm(formKey);
              break;
          
            //  an error was sent
            case 'error' :
              //  show the error message
              var message = node.getElementsByTagName('message');
              if (message && message.item(0)) alert(message.item(0).firstChild.data);
              break;
              
            //  a message has been sent
            case 'msg' :
              //  show the message
              var message = node.getElementsByTagName('message');
              if (message && message.item(0)) alert(message.item(0).firstChild.data);
              break;
          }
          
          //  get the next node
          node = node.nextSibling;
        }
      }
      else
      {
        //  there is no valid XML
        alert(xmlHttpRequest.responseText);
      }
    }
    else
    {
      //  there was an error
      alert('the request failed with error \n\n' + xmlHttpRequest.statusText + ' (' + xmlHttpRequest.status + ')');
    }
          
    //  release the request object (this has to be done manually)
    WATAjaxRequestData[requestId][1] = false;
  }
}

/**
 *
 */
function cmsCloseForm(key)
{
  cmsDesktop.CloseForm(key);
}



function cmsDesktopControl__AjaxResponse(requestId)
{
  cmsResponse(requestId);
}
