/*
 * MS PostBack wrappers
 */

/*
 * Description: Lookup for input control by ID on current page
 * Returns:     true if form found; false otherwise.
 */
function ___get_inp( nm ) {
var ctrl, objs, i;

	objs = document.getElementsByTagName( 'input' );
	for( i = objs.length; --i >= 0 && ( ctrl = objs[ i ] ); )
		if( 'object' == typeof( ctrl ) && nm == ctrl.name )
			return ctrl;

  return null;
} // ___get_inp

/*
 * Description: Wrap around Microsoft ASP.NET postback problems:
 *              This method allows to get button Act and Arguments values
 *              on Init stage of Page processing.
 *
 * Arguments:
 * act_nm       Name of hidden control for store Act value
 * arg_nm       Name of hidden control for store Argument value
 * et           Event Target (for standart PostBack)
 * ea           Event Argument (for standart PostBack)
 * act          Act value
 * arg          Argument value
 * tgt          URI of form for submit to, null for simple submit form
 * is_reset     Flag for reset ViewState value if Act not empty
 */
function __doPostBack_lbtn( act_nm, arg_nm, et, ea, act, arg, tgt, is_reset ) {
var is_submit = false;
var theform   = null;
var lbtn_act  = null, tmp = null;
var lbtn_arg  = null;

  // Always reset 'act' for get rid of 'Back' button problem
  tmp = ___get_inp( 'd4m_act' );
  if( null != tmp )
    tmp.value = '';

  if( null != act_nm && '' != act_nm ) {
    tmp = ___get_inp( act_nm );
    if( null != tmp )
      tmp.value = '';
  }

  // check form exists
  if( null == theform ) {
    if( window.navigator.appName.toLowerCase().indexOf("netscape") > -1 )
      theform = document.forms[ 'main' ];
    else
      theform = document.main;

    if( ( !theform || theform == null ) && document.forms != null && document.forms.length > 0 )
      theform = document.forms[ 0 ];

    if( theform == null ) {
      // let ASP.NET handle this :)
      if( 'function' == typeof( __doPostBack ) )
        __doPostBack( et, ea );
      return;
    }
  } // form exists?

  if( null != act_nm && '' != act_nm && null != act && '' != act ) {
    lbtn_act = ___get_inp( act_nm );
    if(	null != lbtn_act ) {
	    lbtn_act.value = act;
	    if( 1 == is_reset && 'undefined' != typeof( theform.__VIEWSTATE ) && null != theform.__VIEWSTATE ) {
        theform.__VIEWSTATE.value = '';
        is_submit = true;
      } // if need reset ViewState
    } // if control for store Act exists
	} // if need send Act

  if( null != arg_nm && '' != arg_nm && null != arg && '' != arg ) {
    lbtn_arg = ___get_inp( arg_nm );
	  if( null != lbtn_arg )
	    lbtn_arg.value = arg;
	} // if need send Argument

  // Do 'onsubmit' stuff -- ASP.NET don't do that
  if( 'function' == typeof( theform.onsubmit ) && null != theform.onsubmit )
    theform.onsubmit();

  // Allow post to other URL
  if( tgt && null != tgt && '' != tgt ) {
    var bk_tgt = theform.action;
    theform.action = tgt;
    theform.submit();
    theform.action = bk_tgt;
    return;
	}

  // Submit form byself if ViewState was resetted
	if( true == is_submit ) {
    theform.submit();
    return;
  }

  // No special arguments, lets ASP.NET stuff works
  if( 'function' == typeof( __doPostBack ) ) {
    __doPostBack( et, ea );
  }
} // __doPostBack_lbtn

// Backward-compatibility wrapper
function __doPostBackD4M( et, ea, act, arg, tgt, is_reset ) {
  __doPostBack_lbtn( 'd4m_act', 'd4m_arg', et, ea, act, arg, tgt, true );
} // __doPostBackD4M

// Simplifier, for use in scripts
// Perform submit with standard values
function __4pb_glob( act, arg, tgt ) {
  __doPostBack_lbtn( 'd4m_act', 'd4m_arg', '', '', act, arg, tgt, true );
} // __4pb_glob
