/**
 * File with widgets function in javascript
 *
 * @copyright  Copyright(c) 2005, IC Zones
 * @author		Michael Jolin
 * @since		2005,11,04
 * @package		JS
**/

/**
 * Change page of datagrid
 *
 * @author		Michael Jolin
 *	@since		2005,11,04
**/
function datagrid_ChangePage( _nbPage ) {
	var nbPage	= document.getElementById( '_nbPage' );
	if( _nbPage > -1 && nbPage.value != _nbPage ) {
		nbPage.value	= _nbPage;
		document.getElementById( '_submit' ).click();
	}
}

/**
 * Search in datagrid
 *
 * @author		Guillaume Lacroix
 *	@since		2005,12,14
**/
function datagrid_Search( ) {
	var nbPage	= document.getElementById( '_nbPage' );
	var select		= document.getElementById( '_select' );
	select.value = '';
	search = document.getElementById( 'search' );
	nbPage.value	= 0;
	_search = document.getElementById( '_search' );
	_search.value = search.value;
	_cb_search = document.getElementById( '_search_position' );
	_cb_search.value = document.getElementById( 'cb_search' ).value;  
	document.getElementById( '_submit' ).click();
}


/**
 * Change order on datagrid
 *
 * @author		Michael Jolin
 *	@since		2005,11,04
**/
function datagrid_ChangeOrder( _order ) {
	var order	= document.getElementById( '_order' );

	if( order.value == _order )
		order.value = _order + '|R';
	else
		order.value	= _order;
	document.getElementById( '_submit' ).click();
}

/**
 * Select all row on datagrid
 *
 * @author		Michael Jolin
 *	@since		2005,11,04
 *
 *	@update		Guillaume Lacroix
**/
function datagrid_select_all () {
	var allcheck 	= document.getElementById( 'chk_all' ).checked;
	var select		= document.getElementById( '_select' );	
	var ids 			= document.getElementById( '_all_id' ).value;	
	if( allcheck ){
		select.value =  ids;
		updateSelection( ids );
	}else{
		ids = ids.split( ',' );
		select.value =  '';		
		for( var i=0; i<ids.length; i++ ) {
			if( document.getElementById( 'chk_' + ids[i] ) ){
				document.getElementById( 'chk_' + ids[i] ).checked = false;
				document.getElementById( 'row_' + ids[i] ).className = 'dg_unCheck';
			}
		}
	}
	
}

/**
 * Select row on datagrid
 *
 * @author		Michael Jolin
 *	@since		2005,11,04
 *
 *	@update		Guillaume Lacroix
**/
function datagrid_SelectRow( _id ) {
	var check	= document.getElementById( 'chk_' + _id );
	var row		= document.getElementById( 'row_' + _id );
	var select	= document.getElementById( '_select' );
	var last_select = document.getElementById( '_last_select' );
	var sel		= select.value;
	if( sel.indexOf( _id ) > -1 ) {
		check.checked = false;
		var pos		= sel.indexOf( _id );
		var start	= sel.substring( 0, ( pos - 1 ) );
		var end		= sel.substring( ( pos + 3 ), sel.length );

		if( start == '' || end == '' )
			select.value	= start + end;
		else
			select.value	= start + ',' + end;
		row.className = 'dg_unCheck';
	} else {
		check.checked = true;
		if( document.getElementById( '_wantcheck' ).value == 'false' && document.getElementById( '_last_select' ).value != '' ){
			select.value = '';
			document.getElementById( 'row_'  +document.getElementById( '_last_select' ).value  ).className = 'dg_unCheck';
		}
		last_select.value = _id;
		if( select.value == '' )
			select.value = _id;
		else
			select.value = select.value + ',' + _id;

		row.className = 'dg_Check';
	}
}
/**
 * Select item in Combobox
 *
 * @author		Guillaume Lacroix
 *	@since		2005,12,14
**/
function ComboSelect( _inValue ) {
	var _combo	= document.getElementById( 'cb_search' );
	_combo.value = _inValue;
}


/**
 * Simulte Search click if enter
 *
 * @author		Guillaume Lacroix
 * @since		2005,12,15
**/
function datagrid_keypress( _inField,e ){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13)	{
		datagrid_Search( 0 );
		return false;
	}else return true;
}


/**
 * Simluate click on modify
 *
 * @author		Michael Jolin
 *	@since		2005,11,29
**/
function datagrid_DblClick( _id ) {
	var select		= document.getElementById( '_select' );
	select.value	= '';

	datagrid_SelectRow( _id );
	changeMode( 'modify' );
}

/**
 * Update selection of datagrid
 *
 * @author		Michael Jolin
 * @since		2005,12,01
 *
 * @update		Guillaume Lacroix
**/
function updateSelection( _ids ) {
	var ids = _ids.split( ',' );
	
	for( var i=0; i<ids.length; i++ ) {
		if( document.getElementById( 'chk_' + ids[i] ) ){
			document.getElementById( 'chk_' + ids[i] ).checked = true;
			document.getElementById( 'row_' + ids[i] ).className = 'dg_Check';
		}
	}
}

/**
 * Add tag for bold
 *
 * @author		Michael Jolin
 * @since		2005,12,15
**/
function richText_Bold( _id ) {
	var text		= document.getElementById( _id );
	var div		= document.getElementById( 'richText_Bold-' + _id );
	var select	= document.getElementById( 'richText_Bold_' + _id );

	if( select.value == '0' ) {
		text.value		+= '<b>';
		div.className	= 'boldSelect';
		select.value	= 1;
	} else {
		text.value		+= '</b>';
		div.className	= 'bold';
		select.value	= 0;
	}
	text.focus();
}

/**
 * Add tag for italic
 *
 * @author		Michael Jolin
 * @since		2005,12,15
**/
function richText_Italic( _id ) {
	var text		= document.getElementById( _id );
	var div		= document.getElementById( 'richText_Italic-' + _id );
	var select	= document.getElementById( 'richText_Italic_' + _id );

	if( select.value == '0' ) {
		text.value		+= '<i>';
		div.className	= 'italicSelect';
		select.value	= 1;
	} else {
		text.value		+= '</i>';
		div.className	= 'italic';
		select.value	= 0;
	}
	text.focus();
}





/**
 * Add tag for underline
 *
 * @author		Michael Jolin
 * @since		2005,12,15
**/
function richText_Underline( _id ) {
	var text		= document.getElementById( _id );
	var div		= document.getElementById( 'richText_Underline-' + _id );
	var select	= document.getElementById( 'richText_Underline_' + _id );

	if( select.value == '0' ) {
		text.value		+= '<u>';
		div.className	= 'underlineSelect';
		select.value	= 1;
	} else {
		text.value		+= '</u>';
		div.className	= 'underline';
		select.value	= 0;
	}
	text.focus();
}

