document.write('<div style="display:none;position:absolute;"><iframe id="frame-to-server" name="frame-to-server" width="0" height="0" frameBorder="0" src=""> </iframe><form id="client_source" method="POST" enctype="multipart/form-data" action="/command.php" target="frame-to-server" > </form></div>');
function Frame( ){
  var i_oCalback    = { }
  var oProperty  = {
          frame   : 'frame-to-server'
        , id      : 'client_source'
        , func    : 'CallBack'
        , server  : '/command.php'
        , method  : 'POST'
      }
  var i_oMessage    = { }
//  var i_oForm = oDocument.Create( 'form', { method :  oProperty.method, action : oProperty.server, target : oProperty.frame, id : oProperty.id  } )
//  alert( i_oForm )
  var i_oForm = GetObjById( oProperty.id )
  this.SendWoForm = function( sCommand, aSource , oFunction, oArg, bIsNoClear ){
    Clear( )
    FillForm( aSource )

   //alert( i_oForm.innerHTML )
//alert( oFunction)
    oFrame.Send( sCommand, null, oFunction, oArg )
  }
  var FillForm = function ( aSource ){
    //  alert(oFrame)
      for ( sName in aSource ){
      if ( typeof( aSource[ sName ] ) != 'object' ){
        oFrame.Append( sName, aSource[ sName ] )
      }else{
          for ( sSubName in aSource[ sName ] ){
//            try{
             // alert(aSource[ sName ][ sSubName ] )
              oFrame.Append( sName + '[' + sSubName + ']', aSource[ sName ][ sSubName ] )
//            }catch(oError){
//              alert( oError )
//            }
          }
      }
    }
  }

  var Clear = function( ){
    /* Óäàëåíèå Ýëåìåíòîâ, ÷òîá íå ìíîæèëèñü */
    try {
      while ( i_oForm.firstChild ) {
        i_oForm.removeChild( i_oForm.firstChild )
      }

//      for( var iElement = 0; iElement < i_oForm.elements.length; iElement++){
//        i_oForm.removeChild( i_oForm.elements[ iElement ] )
//      }
     // i_oMessage = { }
     // i_oCalback = { }
//      alert(i_oForm.innerHTML)
    //  i_oForm.innerHTML = ''
    }catch( oError ){
      alert( oError )
    }
  }
  this.SendInForm = function ( sCommand, oObj ){
    if (oObj.nodeName != 'FORM'){
      oForm  = oDocument.GetParentNodeOfElement( 'form', oObj )
    }else{
      oForm = oObj
    }
    oFrame.Append( 'command', sCommand )
    oFrame.Send( 'test', oForm )
  }
  this.Send = function ( sCommand, oForm, oFunction, oArg  ){

    //alert(1)

    if ( IsSet( oFunction ) ) {

      i_oCalback = { func : oFunction, arg : oArg }
    //alert(i_oCalback.func)
    }
    var sMapKey = ''
    var sMapVal = ''
    iState = 0

    if ( IsSet( oForm ) ){
    //alert(oForm)
      for( var iElement = 0; iElement < oForm.elements.length; iElement++){
        if ( IsSet( oForm.elements[iElement].name ) && oForm.elements[iElement].name != '' ){
          if ( oForm.elements[iElement].type == 'checkbox' ){
            if (oForm.elements[iElement].checked){
              oForm.elements[iElement].value = 'T'
            }else{
              oForm.elements[iElement].value = 'F'
            }
          }
          if (oForm.elements[iElement].type =='file'){
            AddFile( oForm.elements[ iElement ] )
          }
          sMapKey += iElement + ','
          sMapVal += oForm.elements[iElement].name + ','
          oFrame.Append( oForm.elements[iElement].name, oForm.elements[iElement].value )
        }
      }
      if ( IsSet(oForm.id) ){
        oFrame.Append( '_form[src]', oForm.id )
      }else{
        oFrame.Append( '_form[src]', '' )
      }
    }else{
      oFrame.Append( '_form[src]', '' )
    }

    oFrame.Append( '_form[map-key]', sMapKey )
    oFrame.Append( '_form[map-val]', sMapVal )
    oFrame.Append( '_form[command]', sCommand )
    //}
    BlockClick( true )
    //alert( i_oForm.action )
//    alert( i_oForm.outerHTML )
    i_oForm.submit( )

    Clear( )
    //alert(i_oCalback.func)
  }
  var BlockClick = function ( bBlock ){
    return false
    for( var iElement = 0; iElement < i_oForm.elements.length; iElement++){
      if (
          i_oForm.elements[iElement].type == 'button' ||
          i_oForm.elements[iElement].type == 'submit' ||
          i_oForm.elements[iElement].type == 'reset'
         ){
           i_oForm.elements[iElement].disabled= (bBlock?"disabled":"")
      }
    }
  }
  this.CallBack = function( oMessage ){

    i_oMessage = oMessage
      BlockClick( false )
//      alert( i_oCalback.func  )
    try {
      if ( IsSet( i_oCalback ) && IsSet( i_oCalback.func ) ) {
          if ( IsSet( i_oCalback.arg ) ) {
          //alert(i_oCalback.func)
            i_oCalback.func( i_oCalback.arg )
          }else{
            i_oCalback.func(  )
          }
          i_oCalback = { }
      }
    }catch( oError ){
      alert( 'Îøèáêà ïðè âûçîâå Ôóíêöèè âîçâðàòà' + oError )
    }
  }

  this.GetMessage = function (){
    return i_oMessage
  }
  this.Append = function ( sCode, sValue ){
     oInput =  oDocument.Create( 'input', { type : 'hidden', name : sCode , value: sValue } , i_oForm )
    // alert( sCode + ':' + oInput.name )
  }
  var AddFile = function( oElement ){
    var oFileNew = oDocument.Create( 'input', { type : 'file' ,name : oElement.name  } )
    oElement.parentNode.insertBefore( oFileNew, oElement )
   // alert(oElement.name)
    oDocument.Append( oElement, i_oForm )
  }
  this.SendFile = function( oObj, sId, aSource ){
    try {
      aSource['_file'] ='temp-name'
      var oFile = GetObjById( sId )
      oFile.id = ''
      //sName =oFile.name
      oFile.name = aSource['_file']

      var oFileNew = oDocument.Create( 'input', { type : 'file' ,id : sId  } )
      oFile.parentNode.insertBefore( oFileNew, oFile )
      Clear( )
      oDocument.Append( oFile, i_oForm )
      FillForm( aSource )
      oFrame.Append( 'command', 'SendFile' )
      oFrame.Send( 'SendFile', null )
    }catch( oError ){
      alert( 'Îøèáêà ïåðåäà÷è ôàéëà' )
    }
  }

  this.SetElementByName = function( sName, oArgs ){
    var aElement = document.getElementsByName( sName )
    for( iElement = 0; iElement < aElement.length; iElement++ ){
      SetElement( aElement[ iElement ], oArgs )
    }

  }
  this.SetElementById = function (sId, oArgs ){
    var oElement = GetObjById( sId )
    if ( IsSet( oElement ) ){
       SetElement( oElement, oArgs )
    }
  }
  var SetElement = function( oElement, oArgs ){
    try {
      for ( sName in oArgs ){
        oElement[ sName ] =oArgs[ sName ]
      }
    }catch( oError ){
      alert( 'Îøèáêà óñòàíîâêè ñâîéñòâ ýëåìåíòà' )
    }
  }
}
// alert(1)
oFrame = new Frame

