/**
 * namespace for js, ex.: eval javascripts after an ajax calling
 */
var xc_namespace = {};

if ( typeof console == "undefined" )
{
  var console = 
  {
    log: function()
    {}
  };
}

/**
 * YUI helper methods for manipulating Dom elements
 * @type   object
 */
var Dom = YAHOO.util.Dom,
    /**
     * YUI utilities for managing DOM Events and tools for building event systems
     * @type   object
     */
    Event = YAHOO.util.Event,
    /**
     * YUI simplified interface to the XMLHttpRequest object
     * @type   object
     */
    Connect = YAHOO.util.Connect,
    /**
     * YUI JSON parser
     * @type   object
     */
    JSON = YAHOO.lang.JSON,
    ff3 = ( YAHOO.env.ua.gecko >= 1.9 ),
    /**
     * true if browser is Internet Explorer
     * @type   Boolean
     */
    ie = ( YAHOO.env.ua.ie > 0 ) ? true : false,
    /**
     * true if browser is Internet Explorer 6.0
     * @type   Boolean
     */
    ie6 = ( YAHOO.env.ua.ie == 6 ) ? true : false,
    /**
     * true if browser is Internet Explorer 7.0
     * @type   Boolean
     */
    ie7 = ( YAHOO.env.ua.ie == 7 ) ? true : false,
    linux = ( navigator.userAgent.toLowerCase().indexOf( "linux" ) != -1 ) ? true : false;  
    
    
YAHOO.log = function( text )
{
  if ( typeof console != "undefined" ) 
  {
    //console.log(text);
  }
}

/**
 * @namespace xc
 */
var xc =
{
  /**
  * Ajax call 
  * Method for initiating an asynchronous request via the XHR object.
  * @param String url
  * @param Object callback name of the callback function
  * example:<br />
  * var callback =<br /> 
  * {<br />
  *   success: function(o)<br />
  *   {<br />
  *     //o.responseText;<br />
  *   },<br />
  *   failure: function()<br />
  *   {<br />
  *   <br />
  *   }<br />
  * }<br />
  * 
  * @param String [method] "post" || "get"<br /> 
  * default is get
  * @param Object [postData] data to post<br /> 
  * example:<br />
  * "variable=value&variable2=value2"<br />  
  */
  ajax: function( url, callback, method, postData )
  {
    method = method || "get";
    postData = postData || null;
    Connect.resetFormState();
    var transaction = Connect.asyncRequest( method, url, callback, postData );
    return transaction;
  },
  /**
   * 
   * Ajax call with form sending<br /> 
   * Method for initiating an asynchronous request via the XHR object.
   * @param {String | HTMLelement} id of the form or the form
   * @param Object callback name of the callback function
   * example:<br /> 
   * var callback = <br /> 
   * {<br /> 
   *   success: function(o)<br /> 
   *   {<br /> 
   *     //o.responseText;<br /> 
   *   },<br /> 
   *   failure: function()<br /> 
   *   {<br /> 
   *   <br /> 
   *   }<br /> 
   * }<br /> 
   * @param {Boolean} true if there is file upload
   * 
   */
  ajaxForm: function( form_element, callback, postData, upload )
  {
    var form_obj = Dom.get( form_element );
    var url = form_obj.action;
    var method = form_obj.method;
    
    upload = upload || false;
    postData = postData || null;
    
    Connect.setForm( form_obj, upload );
    
    var transaction = Connect.asyncRequest( method, url, callback, postData );
    
    return transaction;
  },
  /**
   * ajax call updates tag after getting data
   * @param {String} url ajax url
   * @param {String} id Elements id to update
   * @param {String} method default is get
   */
  ajaxUpdate: function( url, id, method, postData )
  {
    var xc_ajax = new this.ajaxhandler.send_handler( url, id, 
    function()
    {
      Dom.get( id ).innerHTML = this.responseText;
    },  null, method, postData );
    xc_ajax.send();
    
    return true;
  },
   /**
   * shows alert dialog <br / >
   * an alias for this.showAlertDialog
   * @param {String} text alert text to show
   */
  alert: function( text )
  {
    alert_lightbox.showAlertDialog( text );
  },
  /**
   * 
   * @param {Object} boxnames
   * @param {Object} current_num
   * @param {Object} active_classname
   */
  changeTab: function( tab_id, max_num, current_num, active_classname )
  {
    active_classname = active_classname || "active";
    var current_tab = Dom.get( tab_id + "_tab_" + current_num );
    if ( Dom.hasClass( current_tab, active_classname ) )
    {
      return true;
    }
    
    var tabs = [];
    
    for( i_tabs = 1; i_tabs <= max_num; i_tabs++ )
    {
      var act_tab_obj = {};
      if ( Dom.get( tab_id + "_tab_" + i_tabs ) )
      {
        act_tab_obj.tab = Dom.get( tab_id + "_tab_" + i_tabs );
        act_tab_obj.content = Dom.get( tab_id + "_content_" + i_tabs );
        if ( Dom.get( tab_id + "_extracontent_" + i_tabs ) )
        {
          act_tab_obj.extracontent = Dom.get( tab_id + "_extracontent_" + i_tabs )
        }
        tabs.push( act_tab_obj );
      }
      
    }
    
    for( i_tab_obj = 0; i_tab_obj < tabs.length; i_tab_obj++ )
    {
      var act_tab_obj = tabs[i_tab_obj];
      if ( act_tab_obj.tab == current_tab )
      {
        Dom.addClass( act_tab_obj.tab, active_classname );
        if ( act_tab_obj.content )
        {
          this.show( act_tab_obj.content );
        }
        if ( act_tab_obj.extracontent )
        {
          this.show( act_tab_obj.extracontent );
        }
      }
      else
      {
        Dom.removeClass( act_tab_obj.tab, active_classname );
        if ( act_tab_obj.content )
        {
          this.hide( act_tab_obj.content );
        }
        if ( act_tab_obj.extracontent )
        {
          this.hide( act_tab_obj.extracontent );
        }
      }
    }
    
    return true;
  },
  /**
   * shows confirm dialog<br / >
   * an alias for this.showAlertDialog
   * @param {Object} text confirm question to show
   * @param {Object} handleYes callback function for Yes button
   */
  confirm: function( text, handleYes )
  {
    confirm_lightbox.showConfirmDialog( text, handleYes );
  },
  /**
  * get dom element's real height<br /> 
  * TODO: finish it
  * @param {string || HTMLelement} el HTML element or id of the HTML element
  * @returns {Number} height of the element<br /> 
  * or false if failed to get height ( perhaps not displayed )
  */
  getRealHeight: function( el )
  {
    el = Dom.get( el );
    if ( !el )
    {
      return false;
    }
    
    var hidden = ( Dom.hasClass( el, "invisible" ) ) ? true : false; 

    if ( hidden )
    {
      Dom.removeClass( el, "invisible" );
    }
    
    var region = Dom.getRegion( el );
    var top = parseInt( region.top );
    var bottom = parseInt( region.bottom );

    if ( hidden )
    {
      Dom.addClass( el, "invisible" );
    }
    
    return bottom-top;
  },
  /**
   * makes element hidden
   * @param {HTMLelement || String} id element or id of the element to hide 
   */
  hide: function( id )
  {
    Dom.addClass( id, "invisible" );
  },
  /**
  * hides custom dialog
  * @id id of the markup code
  */
  hideLight: function( id )
  {
    custom_lightbox.hideCustomDialog( id );
  },
  /**
  * hides ajax dialog
  */
  hideLightAjax: function()
  {
    ajax_lightbox.onReady = function()
    {}
    ajax_lightbox.hideAjaxDialog();
  },
  /**
  * an alias for hideWaitingDialog
  */
  hideWaiting: function()
  {
    waiting_lightbox.hideWaitingDialog();
  },
  /**
   * whether an input field is empty
   * @param {HTMLElement || String} element or id of the element to check
   * @param {Boolean} trimmed trim the value or not
   */
  isEmptyField: function( el, trimmed )
  {
    el = Dom.get( el );
    var value = el.value;
    
    if ( trimmed )
    {
      value = YAHOO.lang.trim( el.value );
    }
    
    return ( value == "" ) ? true : false;
  },
  /**
   * whether an element is visible
   * @param {String | HTMLelement} el element or id of the element to check
   * @return true if visible
   */
  isVisible: function( el )
  {
    el = Dom.get( el );
    if ( Dom.hasClass( el, "invisible" ) )
    {
      return false;
    }
    else
    {
      return true;
    }
  },
  /**
  * an alias for showCustomDialog
  * @param {Object} id id of the markup code
  * @param {Object} config config properties for setting custom dialog
  */
  openLight: function( id, config )
  {
    custom_lightbox.showCustomDialog( id, config );
  },
  /**
  * an alias for showAjaxDialog
  * @param {Object} id id of the markup code
  * @param {Object} config config properties for setting ajax dialog
  */
  openLightAjax: function( url, config )
  {
    ajax_lightbox.showAjaxDialog( url, config );
  },
  /**
   * an alias for showWaitingDialog
   */
  openWaiting: function()
  {
    waiting_lightbox.showWaitingDialog();
  },
  /**
   * makes element visible
   * @param {HTMLelement || String} id element or id of the element to show 
   */
  show: function( id )
  {
    Dom.removeClass( id, "invisible" )
  },
  /**
   * show or hide current element 
   * @param {String || HTMLelement} show_el element or id of the element to show 
   * @param {HTMLelement} check_el optional if checked   
   */
  toggle: function( show_el, check_el )
  {
    show_el = Dom.get( show_el );
    
    var visible;
    
    if ( check_el )
    {
      if ( check_el.nodeName.toLowerCase() == "input" && check_el.type == "checkbox")
      {
        visible = ( check_el.checked ) ? false: true;
      }   
    }
    else
    {
      visible = ( this.isVisible( show_el ) ) ? true : false; 
    }
    
    
    if ( visible )
    {
      this.hide( show_el );
    }
    else
    {
      this.show( show_el );
    }
    return true;
  }
}

xc.ajaxhandler = 
{
  onReady: null,
  payload: null,
  update: null,
  send_handler: function( url, hide_el, onReady, payload, method, postData )
  {
    this.url = url;
    this.hide_el = hide_el;
    this.onReady = onReady || null;
    this.payload = payload || {};
    this.method = method || null;
    this.postData = postData || null;
    this.ind = ( this.hide_el ) ? new indicator.handler( hide_el ) : null;
    this.send = function()
    {
      if ( this.ind )
      {
        this.ind.show(); 
      }
      else
      {
        xc.openWaiting();
      }
      xc.ajax( this.url, this.sendCallback, this.method, this.postData );
    }
    
    this.sendCallback =
    {
      success: function( o )
      {
        xc.hideLight();
        if ( this.ind )
        {
          this.ind.hide();
        }
        
        try
        {
          eval( "var response_error = "+ o.getResponseHeader['Errormsg'] );
        }
        catch(e)
        {
          
        }
        
        if ( typeof response_error != "undefined" )
        {
          xc.alert( response_error );
          return false; 
        }
        
        try
        {
          eval( "var response_success = " + o.getResponseHeader['Successmsg'] );
        }
        catch(e)
        {
          
        }
        
        if ( typeof response_success != "undefined" )
        {
          xc.alert( response_success );
          return false; 
        }
  
        try 
        {
          var response = JSON.parse( o.responseText );
          if ( response.type == 0 )
          {
            xc.alert( response.message );
            return false;
          }      
          else if ( response.type == 1 )
          {
            if ( this.onReady )
            {
              this.payload.o = response;
              this.payload.ajaxobj = this;
              this.onReady.call( o, this.payload );
              return true;
            }
            else if ( response.message )
            {
              xc.alert( response.message );
              return false;
            }
          }
        }
        catch ( e ) 
        {
          
        }
        
        if ( this.onReady )
        {
          this.payload.ajaxobj = this;
          this.onReady.call( o, this.payload );
          return true;
        }    
      },
      failure: function()
      {
        xc.hideLight();
        if ( this.ind )
        {
          this.ind.hide();
        }
      },
      scope: this
    }
  },
  send: function( url, hide_el, onReady, payload, method, postData )
  {
    var ind = null;
    this.update = null;
    if ( hide_el )
    {
      ind = new indicator.handler( hide_el ); 
      //indicator.showIndicator( hide_el );
    }
    else
    {
      xc.openWaiting();
    }
    
    this.onReady = onReady || null;
    this.payload = payload || null;
    method = method || null;
    postData = postData || null,
    
    xc.ajax( url, this.sendCallback, method, postData );
  },
  sendForm: function( form_el, hide_el, onReady, payload, update, postData, upload )
  {
    if ( hide_el )
    {
      indicator.showIndicator( hide_el );
    }
    else
    {
      xc.openWaiting();
    }
    
    this.onReady = onReady || null;
    this.payload = payload || null;
    this.update = update || null;
    postData = postData || null;
    upload = upload || false;

    xc.ajaxForm( form_el, this.sendCallback, postData, upload );
  },
  sendCallback:
  {
    success: function( o )
    {
      xc.hideLight();
      indicator.hideIndicator();
      try
      {
        eval( "var response_error = "+ o.getResponseHeader['Errormsg'] );
      }
      catch(e)
      {
        
      }
      
      if ( typeof response_error != "undefined" )
      {
        xc.alert( response_error );
        return false; 
      }
      
      try
      {
        eval( "var response_success = "+ o.getResponseHeader['Successmsg'] );
      }
      catch(e)
      {
        
      }
      
      if ( typeof response_success != "undefined" )
      {
        xc.alert( urldecode(response_success) );
        return false; 
      }

      try 
      {
        if ( xc.ajaxhandler.update )
        {
          //should update the given element with the response
          Dom.get(xc.ajaxhandler.update).innerHTML = o.responseText;
          
          //eval JS in response
          var js_script = Dom.get(xc.ajaxhandler.update).getElementsByTagName( "script" );
          for ( i in js_script )
          {
            if ( typeof(js_script[i]) != "undefined" )
            {
              if ( js_script[i].innerHTML != "" )
              {
                eval( js_script[i].innerHTML );
              }
            }
          }
          xc.hideWaiting();
        }
        else
        {
          //it should be in JSON format (type, message)
          if ( o.responseText.match(/^\{/) )
          {
            eval( "var response = " + o.responseText );
          }
          else
          {
            var response = JSON.parse(o.responseText);
          }
          if ( response.type == 0 )
          {
            xc.alert( response.message );
            return false;
          }      
          else if ( response.type == 1 )
          {
            if ( xc.ajaxhandler.onReady )
            {
              if ( xc.ajaxhandler.payload )
              {
                xc.ajaxhandler.payload.o = response;
              }
              xc.ajaxhandler.onReady.call( o, xc.ajaxhandler.payload );
              xc.hideWaiting();
              return true;
            }
            else if ( response.message != "" )
            {
              xc.hideWaiting();
              xc.alert( response.message );
              return false;
            }
          }
        }
      }
      catch ( e ) 
      {
        //Dom.get("ajax_dialog").innerHTML = o.responseText;
        xc.hideWaiting();
        xc.alert( o.responseText );
        return false;
      }
      
      if ( xc.ajaxhandler.onReady )
      {
        xc.ajaxhandler.onReady.call( o, xc.ajaxhandler.payload );
        return true;
      }    
    },
    upload: function( o )
    {
      this.success( o );
      return true;
      xc.hideLight();
      indicator.hideIndicator();
      try
      {
        eval( "var response_error = "+ o.getResponseHeader['Errormsg'] );
      }
      catch(e)
      {

      }
      
      if ( typeof response_error != "undefined" )
      {
        xc.alert( response_error );
        return false; 
      }
      
      try
      {
        eval( "var response_success = "+ o.getResponseHeader['Successmsg'] );
      }
      catch(e)
      {
        
      }
      
      if ( typeof response_success != "undefined" )
      {
        xc.alert( response_success );
        return false; 
      }

      try 
      {
          var response = JSON.parse( o.responseText );
          if ( response.type == 0 )
          {
            xc.alert( response.message );
            return false;
          }      
          else if ( response.type == 1 )
          {
            if ( xc.ajaxhandler.onReady )
            {
              xc.ajaxhandler.payload.o = response;
              xc.ajaxhandler.onReady.call( o, xc.ajaxhandler.payload );
              return true;
            }        
          }
      }
      catch ( e ) 
      {
        
      }
      
      if ( xc.ajaxhandler.onReady )
      {
        xc.ajaxhandler.onReady.call( o, xc.ajaxhandler.payload );
        return true;
      }    
    },
    failure: function()
    {
      xc.hideLight();
      indicator.hideIndicator();
      xc.alert( "A művelet sikertelen volt." );
    }
  }
}

/**
 * animation effects
 */
xc.effect = 
{
  afterOpen: function()
  {

  },
  close: function( el )
  {
    el = Dom.get( el );
    var height = xc.getRealHeight( el );
    
    var animate = new YAHOO.util.Anim( el, 
                  {
                    height: { to: 1 } 
                  },
                  0.5,
                  YAHOO.util.Easing.backIn
    );
    
    animate.onComplete.subscribe( function()
    {
      Dom.addClass( el, "invisible" );
      this.edit_box.style.height = "";
    }, this, true );
    
    animate.animate();
  },
  fadeManager: function( config )
  {
    this.fadeout_element = config.fadeout_element || null;
    this.fadein_element = config.fadein_element || null;
    
    this.afterFadeOut = function()
    {
      xc.hide( this.fadeout_element );
      if ( !this.fadein_element )
      {
        return true;
      }
      Dom.setStyle( this.fadein_element, "opacity", 0 );
      xc.show( this.fadein_element );
      this.fadeIn();
    }
    
    this.afterFadeIn = function()
    {
      if ( ie6 )
      {
        pngHack();
      }
      return true;
    }
    
    this.fadeOut = function()
    {
      var animate = new YAHOO.util.Anim( this.fadeout_element, 
                  {
                    opacity: { to: 0 },
                    zoom: { from: 0,
                            to: 0 }
                  },
                  0.5 );
      animate.onComplete.subscribe( this.afterFadeOut, this, true );
      
      animate.animate();
    }
    
    this.fadeIn = function()
    {
      var animate = new YAHOO.util.Anim( this.fadein_element, 
                  {
                    opacity: { to: 1 },
                    zoom: { from: 0,
                            to: 0 }
                  },
                  0.5 );
      animate.onComplete.subscribe( this.afterFadeIn, this, true );
      
      animate.animate();
    }
  },
  open: function( el )
  {
    el = Dom.get( el );
    var height = xc.getRealHeight( el );
    el.style.height = "0px";
    Dom.removeClass( el, "invisible" );
    
    var animate = new YAHOO.util.Anim( el, 
                  {
                    height: { to: height } 
                  },
                  0.5,
                  YAHOO.util.Easing.backOut
    );
    
    animate.onComplete.subscribe( function()
    {
      el.style.height = "auto";
      xc.scrollTo( el );
      xc.effect.afterOpen();
    }, this, true )
    
    animate.animate();
  },
  /**
   * remove HTML element
   * @param {HTMLelement} el element to remove
   */
  remove: function( el )
  {
    el = Dom.get( el );
    el.style.overflow = "hidden";
    var animate = new YAHOO.util.Anim( el,
                  {
                    height: { to: 0 },
                    opacity: { to: 0 }
                  },
                  0.3,
                  YAHOO.util.Easing.backOut
    );
    
    animate.onComplete.subscribe( function()
    {
      el.parentNode.removeChild( el );
    }, this, true );
    
    animate.animate();
    return true;
  }
}

/**
 * indicators
 * @namespace 
 */
var indicator =
{
   /**
   * object for block indicator
   * @type HTMLelement
   */
  indicatorY: null,
  /**
   * iframe tag behind the indicator tag
   * @type HTMLelement  
   */
  indicatorYiframe: null,
  /**
   * hides indicator
   */
  hideIndicator: function()
  {
    Dom.setStyle( this.indicatorY, "top", "0px" );
    Dom.setStyle( this.indicatorY, "left", "0px" );
    Dom.setStyle( this.indicatorY, "width", "0px" );
    Dom.setStyle( this.indicatorY, "height", "0px" );
    Dom.setStyle( this.indicatorYiframe, "top", "0px" );
    Dom.setStyle( this.indicatorYiframe, "left", "0px" );
    Dom.setStyle( this.indicatorYiframe, "width", "0px" );
    Dom.setStyle( this.indicatorYiframe, "height", "0px" );  
      
  },
  /**
   * sets up indicator
   */
  setIndicator: function()
  {
    this.indicatorY = document.createElement( "DIV" );
    this.indicatorYiframe = document.createElement( "IFRAME" );
    var body = document.getElementsByTagName( "body" )[0];
    Dom.setStyle( this.indicatorY, "position", "absolute" );
    Dom.setStyle( this.indicatorY, "top", "0px" );
    Dom.setStyle( this.indicatorY, "left", "0px" );
    Dom.setStyle( this.indicatorY, "width", "0px" );
    Dom.setStyle( this.indicatorY, "height", "0px" );
    Dom.setStyle( this.indicatorY, "opacity", "0.6" );
    Dom.setStyle( this.indicatorY, "z-index", "100000" );
    Dom.setStyle( this.indicatorYiframe, "position", "absolute" );
    Dom.setStyle( this.indicatorYiframe, "border", "0px" );  
    Dom.setStyle( this.indicatorYiframe, "top", "0px" );
    Dom.setStyle( this.indicatorYiframe, "left", "0px" );
    Dom.setStyle( this.indicatorYiframe, "width", "0px" );
    Dom.setStyle( this.indicatorYiframe, "height", "0px" );
    if ( ie )
    {
      Dom.setStyle( this.indicatorYiframe, "visibility", "hidden" );
    }
    body.appendChild( this.indicatorY );
    body.appendChild( this.indicatorYiframe );
  },
  handler: function( el )
  {
    this.el = Dom.get( el );
    this.indicator = null;
    this.iframe = null;
    this.show = function()
    {
      if ( this.indicator )
      {
        this.hide();
      }
      
      this.indicator = document.createElement( "div" );
      this.iframe = document.createElement( "iframe" );
      Dom.setStyle( this.indicator, "position", "absolute" );
      Dom.setStyle( this.indicator, "top", "0px" );
      Dom.setStyle( this.indicator, "left", "0px" );
      Dom.setStyle( this.indicator, "width", "0px" );
      Dom.setStyle( this.indicator, "height", "0px" );
      Dom.setStyle( this.indicator, "opacity", "0.6" );
      Dom.setStyle( this.indicator, "z-index", "100000" );
      Dom.setStyle( this.iframe, "position", "absolute" );
      Dom.setStyle( this.iframe, "border", "0px" );  
      Dom.setStyle( this.iframe, "top", "0px" );
      Dom.setStyle( this.iframe, "left", "0px" );
      Dom.setStyle( this.iframe, "width", "0px" );
      Dom.setStyle( this.iframe, "height", "0px" );
      if ( ie )
      {
        Dom.setStyle( this.iframe, "visibility", "hidden" );
      }
      document.body.appendChild( this.indicator );
      document.body.appendChild( this.iframe );
      
      this.indicator.className="indicatorY";
      
      var region = YAHOO.util.Region.getRegion( this.el );
      var newHeight = region.bottom-region.top;
      var newWidth = region.right-region.left;
      var newXY = Dom.getXY( this.el )    
  
      Dom.setXY( this.indicator, newXY );
      Dom.setStyle( this.indicator, "width", newWidth+"px" );
      Dom.setStyle( this.indicator, "height", newHeight+"px" );
      Dom.setXY( this.iframe, newXY );
      Dom.setStyle( this.iframe, "width", newWidth+"px" );
      Dom.setStyle( this.iframe, "height", newHeight+"px" );
      return true;
    },
    this.hide = function()
    {
      this.indicator.parentNode.removeChild( this.indicator );
      this.iframe.parentNode.removeChild( this.iframe );
      this.indicator = null;
      this.iframe = null;
      return true;
    }
  },
  /**
   * shows indicator
   * 
   * @param {Object} id id of hte block the indicator display above
   */
  showIndicator: function( id )
  {
    if ( !this.indicatorY )
    {
      this.setIndicator();
    }
    this.indicatorY.className="";
    this.indicatorY.className="indicatorY";
    
    var el = Dom.get( id ); 
    var body = document.getElementsByTagName( "body" )[0];
    var region = YAHOO.util.Region.getRegion( el );
    var newHeight = region.bottom-region.top;
    var newWidth = region.right-region.left;
    var newXY = Dom.getXY( el );

    Dom.setXY( this.indicatorY, newXY );
    Dom.setStyle( this.indicatorY, "width", newWidth+"px" );
    Dom.setStyle( this.indicatorY, "height", newHeight+"px" );
    Dom.setXY( this.indicatorYiframe, newXY );
    Dom.setStyle( this.indicatorYiframe, "width", newWidth+"px" );
    Dom.setStyle( this.indicatorYiframe, "height", newHeight+"px" );
    
  }
}
 
/**
 * singleton for ajax lightbox
 * @namespace 
 */
var ajax_lightbox =
{
  /**
   * ajax dialog
   */
  ajax_dialog: null,
  ajax_wrap: null,
  dialog_config: null,
    /**
   * hides ajax dialog
   */
  hideAjaxDialog: function()
  {
    if ( this.ajax_dialog )
    {
      this.ajax_dialog.destroy();
      this.ajax_dialog  = null;
    }
  },
  /**
   * opens ajax dialog
   * @return {Boolean} true if succeed, false else 
   */
  onReady: function()
  {
    
  },
  showAjaxDialog: function( url, config )
  {
    this.dialog_config = config;
    if ( !this.ajax_wrap )
    {
      this.ajax_wrap = Dom.get( "ajax_wrap" );
    }
    
    this.hideAjaxDialog();

    xc.openWaiting();
    xc.ajax( url, this.showCallback );
    return true;
  },
  showCallback:
  {
    success:function( o )
    {
      xc.hideWaiting();
      
      try
      {
        eval( "var response_error = "+ o.getResponseHeader['Errormsg'] );
      }
      catch(e)
      {
        
      }
      
      if ( typeof response_error != "undefined" )
      {
        xc.alert( response_error );
        return false; 
      }
      
      try
      {
        eval( "var response_success = " + o.getResponseHeader['Successmsg'] );
      }
      catch(e)
      {
        
      }
      
      if ( typeof response_success != "undefined" )
      {
        xc.alert( response_success );
        return false; 
      }
      
      try 
      {
        var response = JSON.parse( o.responseText );
        if ( response.type == 0 )
        {
          xc.alert( response.message );
          return false;
        }
      }
      catch ( e ) 
      {
        
      }
      ajax_lightbox.ajax_wrap.innerHTML = o.responseText;
      
      //eval JS in response
      var js_script = ajax_lightbox.ajax_wrap.getElementsByTagName( "script" );
      for ( i in js_script )
      {
        if ( typeof(js_script[i]) != "undefined" )
        {
          if ( js_script[i].innerHTML != "" )
          {
            eval( js_script[i].innerHTML );
          }
        }
      }
      
      var dialog = ajax_lightbox.ajax_wrap.getElementsByTagName( "div" )[0];
      if ( !dialog )
      {
        return false;
      }
      
      if ( dialog.length == 0 )
      {
        return false;
      }
      
      dialog.id = "ajax_dialog";
      
      var width;
      if ( dialog.style.width )
      {
        width = dialog.style.width;
      }
      else
      {
        width = "auto";
      }
      
      var default_config =
      {
          width: width, 
          fixedcenter: false, 
          constraintoviewport: true, 
          close: false, 
          draggable: true, 
          zindex: 4,
          modal: true,
          visible: false,
          iframe: true
      }
      
      for ( i in ajax_lightbox.dialog_config )
      {
        eval( "default_config."+i+" = ajax_lightbox.dialog_config."+i );
      }
  
      var config = default_config;
      
      Dom.removeClass( dialog, "invisible" );
      
      ajax_lightbox.ajax_dialog = new YAHOO.widget.Panel("ajax_dialog", config );

      ajax_lightbox.ajax_dialog.showMaskEvent.subscribe( function()
      {
        var dialog_iframe = ajax_lightbox.ajax_dialog.iframe;
        Dom.addClass( dialog_iframe, "xc_lightboxiframe" );
        if ( linux )
        {
          dialog_iframe.style.visibility = "visible";
        }
      } );

      ajax_lightbox.ajax_dialog.render();
      xc.show( ajax_lightbox.ajax_dialog.element );
      ajax_lightbox.ajax_dialog.show();
      
      ajax_lightbox.ajax_dialog.center();
//      ajax_lightbox.dialog_config = {};
//      var mask_height = parseInt( Dom.getStyle( Dom.get( "ajax_dialog_mask" ), "height" ) );
//      var document_height = Dom.getDocumentHeight();
//      if ( document_height > mask_height )
//      {
//        Dom.setStyle( Dom.get( "ajax_dialog_mask" ), "height", ( document_height + 30 ) + "px" )
//      }
//      
//      ajax_lightbox.onReady = function(){};
      ajax_lightbox.onReady();
      
      return true;
    },
    failure: function()
    {
      xc.hideWaiting();
    }
  }
}

/**
 * singleton for alert lightbox
 * @namespace 
 */
var alert_lightbox =
{
  /**
  * alert dialog
  */
  alert_dialog: null,
  /**
  * hides alert dialog
  */
  hideAlertDialog: function()
  {
    this.alert_dialog.hide();
    if ( this.alert_dialog )
    {
      xc.hide( this.alert_dialog.element );
    }
  },
  /**
  * sets up alert_dialog
  */
  setAlertDialog: function()
  {
    if ( this.alert_dialog )
    {
      return false;
    }
    
    this.alert_dialog = new YAHOO.widget.Panel("alert_dialog",  
      { fixedcenter:true, 
        close:false, 
        draggable:false, 
        zindex:4,
        modal:true,
        visible:false,
        iframe: true
      } 
    );
    
    Dom.removeClass( Dom.get( "alert_dialog" ), "invisible" );
    
    alert_lightbox.alert_dialog.showMaskEvent.subscribe( function()
    {
      var dialog_iframe = alert_lightbox.alert_dialog.iframe;
      Dom.addClass( dialog_iframe, "xc_lightboxiframe" );
      if ( linux )
      {
        dialog_iframe.style.visibility = "visible";
      }
    } );
    
    this.alert_dialog.render();
    
    return true;
  },
  /**
  * opens alert dialog
  * @param {String} text alert text to show
  * 
  * @return {Boolean} true if succeed, false else 
  */
  showAlertDialog: function( text )
  {
    if ( Dom.get( "alert_dialog" ) )
    {
      if ( !this.alert_dialog )
      {
        this.setAlertDialog();
      }
      
      var alert_content = Dom.get( "alert_content" );
      alert_content.innerHTML = text; 
      
      this.alert_dialog.hide();
      xc.show( this.alert_dialog.element );
      this.alert_dialog.show();
      return true;
    }
    
    return false;
  }
}

/**
 * singleton for confirm lightbox
 * @namespace 
 */
var confirm_lightbox =
{
  /**
  * confirm dialog
  */
  confirm_dialog: null,
  callbackYes: function()
  {
    
  },
  callbackYesDefault: function()
  {
    this.hideConfirmDialog();
  },
  callbackNo: function()
  {
    this.hideConfirmDialog();
  },
  handleConfirm: function()
  {
    this.callbackYes();
    confirm_lightbox.hideConfirmDialog();
  },
  /**
  * hides confirm dialog
  */
  hideConfirmDialog: function()
  {
    this.confirm_dialog.hide();
    if ( this.confirm_dialog )
    {
      xc.hide( this.confirm_dialog.element );
    }
  },
  /**
  * sets up confirm_dialog
  */
  setConfirmDialog: function()
  {
    if ( this.confirm_dialog )
    {
      return false;
    }
    
    this.confirm_dialog = new YAHOO.widget.Panel("confirm_dialog",  
      { width:"278px",
        fixedcenter:true, 
        close:false, 
        draggable:false, 
        zindex:4,
        modal:true,
        visible:false,
        iframe: true
      } 
    );
    
    confirm_lightbox.confirm_dialog.showMaskEvent.subscribe( function()
    {
      var dialog_iframe = confirm_lightbox.confirm_dialog.iframe;
      Dom.addClass( dialog_iframe, "xc_lightboxiframe" );
      if ( linux )
      {
        dialog_iframe.style.visibility = "visible";
      }
    } );
    
    Dom.removeClass( Dom.get( "confirm_dialog" ), "invisible" );
    this.confirm_dialog.render();
    
    this.button_yes = Dom.get("confirm_yes_btn");
    return true;
  },
  /**
  * opens confirm dialog
  * @param {String} text alert text to show
  * 
  * @return {Boolean} true if succeed, false else 
  */
  showConfirmDialog: function( text, handleYes )
  {
    if ( Dom.get( "confirm_dialog" ) )
    {
      if ( !this.confirm_dialog )
      {
        this.setConfirmDialog();
      }
      
      var confirm_content = Dom.get( "confirm_content" );
      confirm_content.innerHTML = text; 
      
      if ( handleYes )
      {
        this.callbackYes = handleYes;
      }
      else
      {
        this.callbackYes = this.callbackYesDefault;
      }
      
      this.confirm_dialog.hide();
      xc.show( this.confirm_dialog.element );
      this.confirm_dialog.show();
      return true;
    }
    
    return false;
  }
}

/**
 * singleton for custom lightbox
 * @namespace 
 */
var custom_lightbox = 
{
  act_dialog: null,
  /**
   * array contains instantiated custom dialogs
   */
  custom_dialogs : [],
  iframe: null, 
  /**
   * constructor for setting custom dialog
   * @param {Object} id id of the markup code
   * @param {Object} config config properties for setting custom dialog
   */
  customDialog : function( id, config )
  {
    var width;
    if ( Dom.get( id ).style.width )
    {
      width = Dom.get( id ).style.width;
    }
    else
    {
      width = "auto";
    }
    
    var default_config =
    {
        width: width,
        fixedcenter: false, 
        constraintoviewport: true, 
        close: false, 
        draggable: true, 
        zindex: 4,
        modal: true,
        visible: false,
        iframe: true
    }
    
    for ( i in config )
    {
      eval( "default_config." + i + " = config[i]");
    }

    var config = default_config;
    var custom_dialog = new YAHOO.widget.Panel( id, config );
    
    custom_lightbox.custom_dialogs.push( custom_dialog );
    Dom.removeClass( Dom.get( id ), "invisible" );

    custom_dialog.showMaskEvent.subscribe( function()
    {
      var dialog_iframe = custom_dialog.iframe;
      Dom.addClass( dialog_iframe, "xc_lightboxiframe" );
      if ( linux )
      {
        dialog_iframe.style.visibility = "visible";
      }
    } );
    
    custom_dialog.center();

    custom_dialog.render();
    return custom_dialog;
  },
  /**
  * hides custom dialog
  */
  hideCustomDialog: function( id )
  {
    for ( i_custom = 0; i_custom < this.custom_dialogs.length; i_custom++ )
    {
      if ( this.custom_dialogs[i_custom].id == id )
      {
        this.custom_dialogs[i_custom].hide();
      }
    }
    
    if ( this.act_dialog )
    {
      xc.hide( this.act_dialog.element );
    }
  },
  /**
  * opens custom dialog
  * 
  * @param {Object} id id of the markup code
  * @param {Object} config config properties for setting custom dialog
  * @return {Boolean} true if succeed, false else 
  * 
  */
  showCustomDialog: function( id, config )
  {
    if ( Dom.get( id ) )
    {
      var act_dialog = null;
      
      for ( i_custom = 0; i_custom < this.custom_dialogs.length; i_custom++ )
      {
        if ( this.custom_dialogs[i_custom].id == id )
        {
          act_dialog = this.custom_dialogs[i_custom];
        }
      }
      
      if ( this.custom_dialogs.length == 0 || !act_dialog )
      {
        if ( Dom.get( id ).parentNode != document.body )
        {
          document.body.appendChild( Dom.get( id ) );
        }
        act_dialog = new this.customDialog( id, config );
      }
      
      this.act_dialog = act_dialog;
      act_dialog.hide();
      xc.show( this.act_dialog.element );
      act_dialog.show();
      return true;
    }
    
    return false;
  }
}
/**
 * singleton for waiting lightbox
 * @namespace 
 */
var waiting_lightbox =
{
  /**
   * waiting dialog
   */
  waiting_dialog: null,
    /**
   * hides waiting dialog
   */
  hideWaitingDialog: function()
  {
    if ( !this.waiting_dialog )
    {
      return false;
    }
    this.waiting_dialog.hide();
    if ( this.waiting_dialog )
    {
      xc.hide( this.waiting_dialog.element );
    }
  },
  /**
   * sets up waiting_dialog
   */
  setWaitingDialog: function()
  {
    if ( this.waiting_dialog )
    {
      return false;
    }
    
    this.waiting_dialog = new YAHOO.widget.Panel("waiting_dialog",  
      { width:"240px", 
        fixedcenter:true, 
        close:false, 
        draggable:true, 
        zindex:4,
        modal:true,
        visible:false,
        iframe: true
      } 
    );
    
    waiting_lightbox.waiting_dialog.showMaskEvent.subscribe( function()
    {
      var dialog_iframe = waiting_lightbox.waiting_dialog.iframe;
      Dom.addClass( dialog_iframe, "xc_lightboxiframe" );
      if ( linux )
      {
        dialog_iframe.style.visibility = "visible";
      }
    } );
    
    Dom.removeClass( Dom.get( "waiting_dialog" ), "invisible" );
    this.waiting_dialog.render();
    
    return true;
  },
  /**
   * opens waiting dialog
   * @return {Boolean} true if succeed, false else 
   */
  showWaitingDialog: function()
  {
    if ( Dom.get( "waiting_dialog" ) )
    {
      if ( !this.waiting_dialog )
      {
        this.setWaitingDialog();
      }
      
      this.waiting_dialog.hide();
      xc.show( this.waiting_dialog.element );
      this.waiting_dialog.show();
      return true;
    }
    
    return false;
  }
}

function getFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         /*
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         */
         break;
   }
   
   return "";
}
