	var plugin_call_obj = {};
	function plugin_add_param( obj )
	{
		for( i in obj )
			plugin_call_obj[ i ] = obj[ i ];
	}
	function make_call_str( obj )
	{
		var str = "";
		for( i in obj )
		{
			if ( isObject( obj[ i ] ) )
			{
				for( j in obj[i] )
					str+="&var[]="+i+"_OBJ_"+j+"&val[]="+(obj[i][j]);
			}
			else if ( isArray( obj[ i ] ) )
			{
				for( j=0;j<obj[i].length;j++ )
					str+="&var[]="+i+"_ARR&val[]="+(obj[i][j]);
			}
			else
				str+= "&var[]="+i+"&val[]="+(obj[i]);
		}
		return str;
	}
	function to_ajax( str )
	{
		var a = str.substr( 1 );
		return a;
	}
	function call_plugin( obj )
	{
		window.location.href = "<%$plugin_script%>&"+make_call_str(obj);
	}
	
	var ajax_update = {};
	var ajax_update_notice = {};
	var ajax_callback = {};
	var ajax_oncomplete = {};
	var ajax_script = false;
	var ajax_onerror = {};
	var ajax_ps = {}; // param save
	var noticewindow_options = {};
	
	function	ajax_setscript( scr )
	{
		ajax_script = scr;
	}
	
	function	ajax_connect_ext( obj )
	{ 
		fnDone = function( res, params )
		{
			if ( !isset( res[ 'error' ] ) )
			{				
				if ( isset( ajax_update[ 'obj'+params['id'] ]) && ajax_update[ 'obj'+params['id']] != false )
				{
					ajax_update[ 'obj'+params['id']].innerHTML = res[ 'content' ];
					sso_set_chooser( ajax_update[ 'obj'+params['id']] );
				}
				if ( isset( ajax_update_notice[ 'obj'+params['id']] ) && ajax_update_notice[ 'obj'+params['id']] != false )
				{
					var o = { content: res[ 'content' ], nobutton : true };
					$extend( o, noticewindow_options[ 'obj'+params['id']] );
					if ( noticewindow_options[ 'obj'+params['id']][ 'big' ]  || noticewindow_options[ 'obj'+params['id']][ 'contentwindow' ] )
					{
						if ( noticewindow_options[ 'obj'+params['id']][ 'small' ] ) noticeWindow_contentsmall.open( o );
						else noticeWindow_content.open( o );
					}
					else noticeWindow.open( o );
				}

				if ( isset( ajax_callback[ 'obj'+params['id']] ) && ajax_callback[ 'obj'+params['id']] != false )
					ajax_callback[ 'obj'+params['id']]( res, obj, params['obj'] );
				if ( res[ 'result' ] == "ok" && isset( ajax_oncomplete[ 'obj'+params['id']]) && ajax_oncomplete[ 'obj'+params['id']]!=false)
					ajax_oncomplete[ 'obj'+params['id']]( res, obj, params['obj'] );
			}
			else
			{
				//console.log( 'ajax_conn_ext onerror ajax_onerror[',params['id'],']:',ajax_onerror, ajax_onerror[ 'obj'+params['id']] );
				if ( isset( ajax_onerror[ 'obj'+params['id']] ) && ajax_onerror[ 'obj'+params['id']] != false )
				{
					ajax_onerror[ 'obj'+params['id']]( res, obj, params['obj'] );
				}
			}
		}

		//obj[ 'currhash' ] = window.location.hash.substr(1);
		
		var id = Math.floor(Math.random()*10000);
		var o = {};
		$extend( o, obj );
		if ( isset( obj.update ) ) { ajax_update['obj'+id] = obj.update; delete obj.update; } else ajax_update['obj'+id] = false;
		if ( isset( obj.callback ) ) { ajax_callback['obj'+id] = obj.callback;  delete obj.callback; } else ajax_callback['obj'+id] = false;
		if ( isset( obj.oncomplete ) ) { ajax_oncomplete['obj'+id] = obj.oncomplete;  delete obj.oncomplete; } else ajax_oncomplete['obj'+id] = false;
		if ( isset( obj.update_nw ) ) { ajax_update_notice['obj'+id] = obj.update_nw; delete obj.update_nw; } else ajax_update_notice['obj'+id] = false;
		if ( isset( obj.onerror ) ) { ajax_onerror['obj'+id] = obj.onerror;  delete obj.onerror; } else ajax_onerror['obj'+id] = false;
		if ( isset( obj.noticewindow_options ) ) { noticewindow_options['obj'+id] = obj.noticewindow_options;  delete obj.noticewindow_options; } else noticewindow_options['obj'+id] = {};
		if ( isset( obj.nwo ) ) { noticewindow_options['obj'+id] = obj.nwo;  delete obj.nwo; } else
		{
			if ( !isset(noticewindow_options['obj'+id]) ) noticewindow_options['obj'+id] = {};
		}
		if ( isset( obj.ps ) ) { delete obj.ps; }

		var myConn = new XHConn();
		if ( obj.script == "" ) obj.script = ajax_script;
		if ( obj.script == "" ) { console.log('no script specified');return;}
		
		console.log("window.location.href=",window.location.href);
		console.log("obj ",obj, ' o:' );

		var op = {id:id, obj:o};
		if ( isset( o['nooverlay'] ) )
				op['nooverlay']=true;
		if ( !isset( obj[ 'no_make_str'] ) )
			myConn.connect2(obj.script, "POST", to_ajax( make_call_str ( obj ) ), fnDone, op, fnDone );
		else
//			myConn.connect2(obj.script, "POST", to_ajax( make_call_str ( { 'currhash': obj['currhash'] } ) ), fnDone, {id:id, obj:o}, fnDone );
			myConn.connect2(obj.script, "POST", '', fnDone, op, fnDone );
	}


	var ajax_upload_callback = false;
	function	ajax_upload( formID, obj, callback )
	{
		if ( !isset( obj.script ) || obj.script == "" ) obj.script = '<%$vars.script%>';
		if ( obj.script == "" ) obj.script = ajax_script;

		if ( !isset(obj.script) || obj.script=='' )
		{
			console.log('noscript');return;
		}
		
		$(formID).action = obj.script;
		delete obj.script;
		var str = make_call_str( obj );
		if ( isset( obj.notplugin ) )
		{
			str = '?'+str.substr( 1 );
			delete obj.notplugin;
		}
		$(formID).action+=str;
		
		ajax_upload_callback = callback;
		
		validatorOBJ.formSubmit( $(formID), { onComplete: function( res ){
			if ( typeof( ajax_upload_callback ) == 'function' ) ajax_upload_callback( res );
		} } );		
	}


	function plugin_get_fields( obj, classname, spec_chk )
	{
		var elemOBJ = {};
		
		if ( !isset( classname ) ) classname="pli";
		if ( !isset( spec_chk ) ) spec_chk=false;
		
		var arr = false;
				
		if (typeof(obj)=='string')
		{
			var obj2 = document.getElementById( obj );
			arr = getElementsByClassNameIE( obj2, '*', classname);
		}
		else
			arr = obj.getElements( '.'+classname );
		for (  var i=0;i<arr.length;i++ )
		{
			ar=arr[i];
			if ( ar.type == "text" || ar.type == "textarea"  || ar.type == "password") value = nl2br( ar.value );
			if ( ar.type == 'select-multiple' )
			{
				var n = ar.name+"[]";
				var d = new Array();
				for (var j = 0; j < ar.options.length; j++)
					if (ar.options[ j ].selected) d.push( ar.options[j].value );
				//if ( d.length > 0 )
					elemOBJ[ n ] = d;
			}
			if ( ar.type == "select-one" )
			{
				try{
					value = select_getOption( ar );
				}
				catch(z)
				{
					continue;
					return 'noselect';
				}
			}
			if ( ar.type == "checkbox" || ar.type == 'radio' )
			{
				if ( spec_chk )
				{
					value = ( isset( ar.value ) && ar.value!=""  ) ? ar.value : ar.checked;
					if ( !ar.checked ) value = "N";
				}
				else
				{
					if ( ar.value && !ar.checked ) continue;
					value = ( isset( ar.value ) && ar.value!=""  ) ? ar.value : ar.checked;
				}
			}
			if ( ar.type == "hidden" ) value = ar.value;
			if ( ar.type == "hidden" || ar.type=="text" || ar.type=="textarea" || ar.type=="password"  || ar.type=="checkbox" || ar.type=="select-one" || ar.type=='radio')
			{
				value = encodeURIComponent(value);
				if ( ar.name.substr( ar.name.length-2, 2 ) == "[]" )
				{
					if ( !isset( elemOBJ[ ar.name ] ) || !isArray( elemOBJ[ ar.name ] ) ) elemOBJ[ ar.name ] = new Array();
					elemOBJ[ ar.name ].push( value );
				}
				else elemOBJ[ ar.name ] = value;
			}
		}
		return elemOBJ;
	}
	
	function plugin_clear_fields( obj, classname  )
	{	
		if ( !isset( classname ) ) classname="pli";
		
		var arr = false;
		if (typeof(obj)=='string')
		{
			var obj2 = document.getElementById( obj );
			arr = getElementsByClassNameIE( obj2, '*', classname);
		}
		else
			arr = obj.getElements( '.'+classname );

		for (  var i=0;i<arr.length;i++ )
		{
			ar=arr[i];
			if ( ar.type == 'select-multiple' || ar.type == "select-one" )
			{
				try{
					ar.selectedIndex = 0; }
				catch(z) { 
					continue; }
			}
			if ( ar.type == "checkbox" ) ar.checked = false;
			if ( ar.type == "hidden" || ar.type=="text" || ar.type=="textarea" )
				ar.value='';
		}
	}

	function plugin_set_field( ar, value  )
	{	
		if ( ar.type == 'select-multiple' || ar.type == "select-one" )
		{
			set_selectKey( ar, value );
		}
		if ( ar.type == "radio" )
			{
			ar.checked = ( ( ( value == true) || ( ar.value == value ) ) ? true : false );
			}
		if ( ar.type == "checkbox" ) ar.checked = ( ( ( value == true) || ( value == "Y" ) ) ? true : false );
		if ( ar.type == "hidden" || ar.type=="text" || ar.type=="textarea" )
			ar.value=value;
	}
	function plugin_set_fields( ars, values  )
	{	
		for( var i=0;i<ars.length;i++)
		{
			var ar = ars[i];
			var value = values[i];
			console.log(ar,' ', ar.value,' ', value);
			if ( ar.type == 'select-multiple' || ar.type == "select-one" )
			{
				set_selectKey( ar, value );
			}
			if ( ar.type == "radio" ) ar.checked = ( ( ( value == true) || ( ar.value == value ) ) ? true : false );
			if ( ar.type == "checkbox" ) ar.checked = ( ( ( value == true) || ( value == "Y" ) ) ? true : false );
			if ( ar.type == "hidden" || ar.type=="text" || ar.type=="textarea" )
				ar.value=value;
		}
	}
	
	
	function	form_saver( php, div )
	{
		var obj = plugin_get_fields( div, 'pli', true );
		obj.script = php;
		ajax_connect_ext( { script: obj[ 'script' ], obj:obj, oncomplete: function(res)
			{
				var el = div.getElements( '.okicon' )[0];
				el.fade('hide').removeClass('hide');
				el.fade('in').get('tween').chain( function(){ el.fade('out' ) } );
			} } );
	}
