
function AJAX( ) {
	this.ready = 0;
	this.loading = 0;
	this.stage = 1;
	this.xslkind = 0;
	this.usexsl = false;
	this.fronta = new Array( );
	this.infoDiv = null;
	this.infoTextDiv = null;
	this.successMessage = "Uloženo";
	this.sendingMessage = "Ukládání";
	this.ladtIdRem = 0;
	
	if( window.XMLHttpRequest ) {
		this.object = new XMLHttpRequest();
	} else if( window.ActiveXObject ) {
		try { this.object = new ActiveXObject("Msxml2.XMLHTTP"); } catch( e ) { return ; }
		try { this.object = new ActiveXObject("Microsoft.XMLHTTP"); } catch( e ) { return ; }
	} else {
		return ;
	}

	this.ready = 1;
}

AJAX.prototype.onStartLoad = function( ) { };
AJAX.prototype.onLoad = function( ) { };
AJAX.prototype.onReady = function( ) { };
AJAX.prototype.onError = function( err ) { };

AJAX.prototype.loadXSL = function( xsl ) {
	this.stage = 0;
	this.usexsl = true;
	this.get( xsl );
};

AJAX.prototype.unloadXSL = function( ) {
	this.usexsl = false;
};

AJAX.prototype.onState = function( ) {
	if( this.object.readyState == 4 ) {
		this.loading = 0;
		if( this.object.status == 200 ) if( this.stage == 0 ) {
			this.xsl = this.getXML( );
			this.stage = 1;
			
			if( window.ActiveXObject ) {
				this.xslkind = 1;
			} else if( document.implementation.createDocument ) {
				this.xslkind = 2;
				this.processor = new XSLTProcessor();
				this.processor.importStylesheet( this.xsl );
			}

			return this.onReady( );
		} else {
			if( this.infoDiv != null ) {
				this.infoDiv.className = 'infodiv id-success';
				this.infoDiv.id = "for-remove-" + this.ladtIdRem;
				this.infoDivText.innerHTML = this.successMessage;
				setTimeout( "if( getObj( 'for-remove-" + this.ladtIdRem + "' )) getObj( 'for-remove-" + this.ladtIdRem + "' ).parentNode.removeChild( getObj( 'for-remove-" + this.ladtIdRem + "' ))", 1500 );
				this.ladtIdRem ++;
				this.infoDiv = null;
			}
			if( this.fronta.length > 0 ) {
				var rec = this.fronta[0];
				this.fronta.splice( 0, 1 );
				this.IsendInput(rec.addr, rec.name, rec.value, rec.onload);
			}
			if( typeof( this.onLoad ) == 'function' ) {
				return this.onLoad( );
			}
		}
		if( this.stage == 0 ) this.usexsl = false;
		return this.onError( this.object.status );
	}
};

AJAX.prototype.isReady = function( ) {
	if( this.ready == 0 ) return false;
	if( this.loading == 1 ) return false;
	return true;
};

AJAX.prototype.addCookies = function( url ) {
	if( document.cookie.length == 0 ) return url;
	
	if( url.indexOf( "?" ) == -1 ) {
		url = url + "?";
	} else {
		url = url + "&";
	}
	return url + "ajax&cookies=" + escape( document.cookie );
};

AJAX.prototype.get = function( url ) {
	if( !this.isReady( )) {
		return false;
	}

	var me = this;
	url = this.addCookies( url );
	this.object.open( 'GET', url, true );
	this.object.onreadystatechange = function( ) { me.onState( ); };
	this.object.send( null );
	this.loading = 1;
	this.onStartLoad( );
	return true;
};

AJAX.prototype.post = function( url, data ) {
	if( !this.isReady( )) return false;
	
	var me = this;
	url = this.addCookies( url );
	this.object.open( 'POST', url, true );
	this.object.onreadystatechange = function( ) { me.onState( ); };
 	this.object.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	this.object.send( data );
	this.loading = 1;
	this.onStartLoad( );
	return;
};

AJAX.prototype.parseXML = function( xml ) {
	if(( this.usexsl ) && ( this.stage == 1 )) {
		if( this.xslkind == 1 ) return xml.transformNode( this.xsl );
		if( this.xslkind == 2 ) {
			var res = this.processor.transformToDocument( xml );
			if( typeof( res ) == 'undefined' ) return null;
			var serializer = new XMLSerializer(); 
			return serializer.serializeToString( res.documentElement );
		}
		return null;
	} else {
		return xml;
	}
};

AJAX.prototype.getXML = function( ) {
	if( this.ready == 0 ) return false;
	if( this.loading == 1 ) return false;
	if( this.object.readyState != 4 ) return false;
	if( this.object.status != 200 ) return false;

	return this.parseXML( this.object.responseXML );
};

AJAX.prototype.sendInput = function( addr, name, value, object, onload ) {
	this.infoDiv = document.createElement( 'div' );
	this.infoDiv.className = 'infodiv';
	this.infoDiv.innerHTML = '<div class="id-bottom"><div class="id-bottomin">&nbsp;</div></div>';
	var self = this;
	this.infoDiv.onclick = function( ) { this.parentNode.removeChild( this ); self.infoDiv = null; };
	this.infoDiv.style.position = 'absolute';

	this.infoDivText = document.createElement( 'div' );
	this.infoDivText.className = 'id-text';
	this.infoDivText.innerHTML = this.sendingMessage;
	
	object.parentNode.insertBefore( this.infoDiv, object );
	this.infoDiv.insertBefore( this.infoDivText, this.infoDiv.firstChild );

	var obj_coords = objCoords( object );
	var wSize = windowSize( );
	var l = ( obj_coords.x + obj_coords.w - 24 );
	if(( l + this.infoDiv.offsetWidth ) > wSize.w ) l = wSize.w - this.infoDiv.offsetWidth;

	this.infoDiv.style.left = l + 'px';
	this.infoDiv.style.top = ( obj_coords.y - this.infoDiv.offsetHeight ) + 'px';

	if( !this.isReady( )) {
		this.fronta.splice( this.fronta.length, 0, {"addr": addr, "name": name, "value": value, "onload": onload });
		
		return;
	}
	this.IsendInput(addr, name, value, object, onload);
};

AJAX.prototype.IsendInput = function( addr, name, value, onload ) {
	if( onload ) this.onLoad = onload;
	this.post( addr, name + "=" + value );
};

// AJAX Select functions
var ajaxSelsO = new Array( );
var ajaxSelsF = new Array( );
var ajaxSelsD = new Array( );
var ajaxSelsOH = new Array( );

function ajaxSelectBI( id_sel ) {
	var tab_obj = getObj( id_sel + "_tabsel" );
	if( tab_obj == null ) return ;
	
	var input_obj = getObj( id_sel + "_selected_ids" );
	if( input_obj == null ) return ;

	var did_obj = getObj( id_sel + "_did" );
	if( did_obj == null ) return ;

	var new_val = '';
	var lid = id_sel.length + 6 ;
	for( i = 0; i < tab_obj.rows.length; i ++ ) {
		id = tab_obj.rows[i].id.substring( lid );
		new_val = new_val + id + ', ';
	}
	
	input_obj.value = new_val + did_obj.value;
}

function ajaxSelectDrop( id_sel, id_item ) {
	var row_obj = getObj( id_sel + "_item_" + id_item );
	if( row_obj == null ) return ;
	
	row_obj.parentNode.removeChild( row_obj );

	ajaxSelectBI( id_sel );
	ajaxSelectUpdate( id_sel );
}

function ajaxSelectAddI( tab_obj, id_sel, id_item, title ) {
	var new_row = tab_obj.insertRow( tab_obj.rows.length );
	new_row.id = id_sel + "_item_" + id_item;
	ajaxSelsD[id_sel].makeDraggable( new_row );
	
	var title_cell = new_row.insertCell( 0 );
	title_cell.innerHTML = title;
//	title_cell.onclick = function() { ajaxSelectSel( this ); };

	var del_cell = new_row.insertCell( 1 );
	del_cell.onclick = function() { ajaxSelectDrop( id_sel, id_item ); };
	del_cell.innerHTML = '<img src="/admin/img/smazat.gif" alt="" title="Smazat" width="15" height="15" />';
}

function ajaxSelectAdd( id_sel, id_item, title ) {
	var tab_obj = getObj( id_sel + "_tabsel" );
	if( tab_obj == null ) return ;
	ajaxSelectAddI( tab_obj, id_sel, id_item, title );
	ajaxSelectBI( id_sel );
	ajaxSelectUpdate( id_sel );
}

function ajaxSelectCheck( id_sel ) {
	if( ajaxSelsO[id_sel].loading == 1 ) return ;
	
	var filt_obj = getObj( id_sel + "_filt" );
	if( filt_obj == null ) return ;
	
	if( filt_obj.value == ajaxSelsF[id_sel] ) {
		setTimeout( "ajaxSelectCheck( '" + id_sel + "' );", 500 );
		return ;
	}

	ajaxSelsF[id_sel] = filt_obj.value;
	ajaxSelectUpdate( id_sel );
}

function ajaxInit( id_sel, addr, xsl ) {
	ajaxSelsO[id_sel] = new AJAX( );
	ajaxSelsF[id_sel] = '';
	
	ajaxSelsO[id_sel].sel_id = id_sel;
	ajaxSelsO[id_sel].sel_addr = addr;
	ajaxSelsOH[id_sel] = getObj( id_sel + '_divsel' ).innerHTML;
	
	ajaxSelsO[id_sel].onLoad = function( ) {
		var target = getObj( this.sel_id + '_all' );
		if( target == null ) return ;
		
		target.innerHTML = this.getXML( );
		setTimeout( "ajaxSelectCheck( '" + this.sel_id + "' );", 500 );
	};

	ajaxSelsO[id_sel].onError = function( err ) {
		alert( "Chyba AJAX !" );
	};
	
	if( xsl != '' ) {
		ajaxSelsO[id_sel].onReady = function( ) {
			var input_obj = getObj( this.sel_id + "_selected_ids" );
			if( input_obj == null ) return ;

			this.get( this.sel_addr + '&sel=' + input_obj.value );
		};
		
		ajaxSelsO[id_sel].loadXSL( xsl );
	} else {
		var input_obj = getObj( id_sel + "_selected_ids" );
		if( input_obj == null ) return ;

		ajaxSelsO[id_sel].get( addr + '&sel=' + input_obj.value );
	}

	var tab_obj = getObj( id_sel + "_tabsel" );
	if( tab_obj == null ) return ;
	
	ajaxSelsD[id_sel] = new TableDnD();
	ajaxSelsD[id_sel].onDrag = function(tbl, droppedRow) {
        droppedRow.style.cursor = "move";
		droppedRow.className = 'ASDrag';
	};

	ajaxSelsD[id_sel].onClick = function(tbl, droppedRow) {
		droppedRow.className = droppedRow.className == 'ASSel' ? '' : 'ASSel';
	};

	ajaxSelsD[id_sel].onDrop = function (tbl, droppedRow) {
        droppedRow.style.cursor = "";
		droppedRow.className = '';
		ajaxSelectBI( id_sel );
	};

	ajaxSelsD[id_sel].init( tab_obj );
}

function ajaxSelectUpdate( id_sel ) {
	var input_obj = getObj( id_sel + "_selected_ids" );
	if( input_obj == null ) return ;

	var filt_obj = getObj( id_sel + "_filt" );
	if( filt_obj == null ) return ;

	var addr = ajaxSelsO[id_sel].sel_addr + '&sel=' + input_obj.value;
	ajaxSelsO[id_sel].post( addr, 'filt=' + filt_obj.value );
}

function ajaxSelectSel( obj ) {
	obj = obj.parentNode;
	var cls = obj.className;
	obj.className = cls == 'ASSel' ? '' : 'ASSel';
}

function ajaxSelectMulti( id_sel, typ ) {
	var tab_obj = getObj( id_sel + "_tab" + ( typ == 'a' ? 'all' : 'sel' ));
	if( tab_obj == null ) return ;

	var tabt_obj = getObj( id_sel + "_tab" + ( typ == 'a' ? 'sel' : 'all' ));
	if( tabt_obj == null ) return ;

	var lid = id_sel.length + 6 ;
	for( var i = 0; i < tab_obj.rows.length; i ++ ) {
		if( typ != 'dall' ) if( tab_obj.rows[i].className != 'ASSel' ) continue;
		
		if( typ == 'a' ) {
			var id = tab_obj.rows[i].id.substring( lid );
			var cobj = getObj( id_sel + '_ititle_' + id );
			if( cobj == null ) continue;
			
			var title = cobj.innerHTML; 
			ajaxSelectAddI( tabt_obj, id_sel, id, title );
		} else {
			tab_obj.rows[i].parentNode.removeChild( tab_obj.rows[i] );
			i --;
		}
	}

	ajaxSelectBI( id_sel );
	ajaxSelectUpdate( id_sel );
}

function resetAjaxSelect( ) {
	for( i in ajaxSelsOH ) {
		getObj( i + '_divsel' ).innerHTML = ajaxSelsOH[i];
		setTimeout( 'ajaxSelectUpdate( "' + i + '" );', 30 );
	}
}
