// JavaScript Document
// class AjxForm
// code by Azer Manafov (azerman[at]hotmail[dot]com)

var THEME             = 'theme';
var THEME_ALBUM       = 'album';
var THEME_SUBSCRIBE   = 'subscribe';


function clsReqStr          ( visihlp,theme )
{
	var m_visi    = (visihlp == 'undefined')?new clsViewHelper():visihlp;
	var m_request = 
	{   'theme' : ''
	};
	var m_encode  = false;
	var m_encknd  = true;//false;
	
	this.encknd   = function( hex )  { m_encknd =  ( hex == undefined )?false:hex; }
	this.newrq    = function( theme ){ m_request = new Object(); m_request.theme = theme; };
	this.append   = append;
	this.appctrl  = appctrl;
	this.setrq    = function ( opt ) 
	{   for ( var i in opt ) 
	          m_request[i] = opt[i]; 
	}
	this.getrq    = function ( )     
	{   var rqstr = '';
	    var k     = 0;
	    for ( var i in m_request )
			  rqstr += (k++==0)?(i+'='+m_request[i]):('&' + i+'='+m_request[i]);
	    return rqstr; 
	}
	this.encode   = function ( enc ) 
	{   if ( (enc == undefined) || (enc != true)) enc = false;
        m_encode = enc;
	}

	function appctrl    ( opt )// [option name] : [control to take from]
    {   for ( var i in opt)
	    {   var obj = m_visi.getElement( opt[i] );
			if ( !obj ) return false;
			if ( obj.type == 'checkbox')
			     obj.value = (obj.checked)?1:0;
			append( {i : obj.value});
	    }
		return m_request;
	}
	function append     ( opt )
	{   for ( var i in opt )
		{   if ( !opt[i] ) continue;
			if ( m_encode == true )
			{    if ( m_encknd == false )
			          opt[i] = base64_encode( opt[i] );
			     else opt[i] = m_visi.str2hex ( opt[i] );
			}
			m_request[i] = opt[i];
		}
		return m_request;
	}
};

function clsAjxHelper( )
{
	var m_ajx    = new clsAjxRequest  ( );
	var m_ajxBat = new batchAjxRequest( null,"POST" );
	var m_prop   = 
	{
		img_progress   : "images/sys/progress.gif",
		bkg_progress   : "images/sys/x.gif",//bg_transp128_50.gif",
		crq_object     : null,
        url            : visi.getbaseurl()+"engine.php",//visi.getbaseurl("spear")
		show_wait      : true,
		parseCalback   : null,
		chalengeRqCblk : null //
	};
	var m_session =
	{   'action'     : 'submitted',
		'username'   : '',
		'userlevel'  : '',
	    'userid'     : '',
		'chalenge'   : ''
	};
	var m_this = this;
	
	this.addrequest       = addrequest;
	this.applySessionVars = applySessionVars;

	this.getSessionVars   = getSessionVars;
	this.getRequestObject = getRequestObject;
    this.method           = method;
    this.request          = request;
    this.resetall         = resetall;
	this.setProperties    = setProperties;
	this.syncrq           = syncrq;


	function addrequest         ( parameters )
	{   var rq = { url : m_prop.url, callback: utilResults, querystr: m_prop.crq_object.getrq(), arguments:null, retAsXml:0 };
	    if ( parameters != undefined )
		{   for ( var i in parameters )
		    {
				if ( typeof rq[i] != 'undefined' ) rq[i] = parameters[i];
			}
		}
		m_ajxBat.addRequest( rq );
	}
	
	function applySessionVars   ( ) // public
	{   if ( m_prop.crq_object )
		{   for ( var i in m_session )
			{   if ( m_session[i].length )
		             m_prop.crq_object.append ( i, m_session[i]  );
			}
		}
	}
    
	function getSessionVars     ( ) {  return m_session;} // public
	function getRequestObject   ( theme ) { return (m_prop.crq_object = new clsReqStr( visi, theme)); } // public
    function method             ( sMethod ) { m_ajxBat.setMethod ( sMethod ); } // public
    function request            ( bShowWait ) // public
    {   
		bShowWait    = ( bShowWait  == undefined )?m_prop.show_wait:bShowWait;

        utilShowWait( bShowWait );
    	
		m_ajxBat.completionRoutine( utilEndRequest );
    	m_ajxBat.runRequest( );
    }
    function resetall           ( ) { m_ajxBat.resetAll();} // public
    function setProperties      ( propObj ) // public
	{   for ( var i in propObj )
	          if ( typeof m_prop[i] != 'undefined' ) m_prop[i] = propObj[i];
	}
	function syncrq             ( ){ return m_ajx.postSyncAjaxRq( m_prop.url, m_prop.crq_object.getrq()); } // public

	function utilEndRequest     ( ) {   visi.setVisible('ajxMaskDiv',false); }
	function utilResults        ( ajx, respond )
	{   if ( respond ) respond = visi.trim( respond );
		if ( !respond.length ) return;


		for ( var i in m_session )
			m_session[i] = '';
		m_session.action = 	'submitted';

        var prop       = utilParseReXml( respond );
		for ( var i in prop )
		{   if ( typeof m_session[i] != 'undefined' ) m_session[i] = prop[i];
		}
		if ( m_prop.chalengeRqCblk != null )
			 m_prop.chalengeRqCblkext( prop );
	}
	
};
var ajxform = new clsAjxHelper( );
ajxform.method("POST");

