

skills_matrix = function( div_control, name, width )
	{
	this.objectname = name;
	this.main_div = div_control;
	this.main_table = gen.create_element('table');
	this.main_tbody = gen.create_element('tbody');
	this.main_tr_header = gen.create_element('tr');
	this.main_td_header = gen.create_element('td');
	this.header_table = gen.create_element('table');
	this.header_tbody = gen.create_element('tbody');

	this.main_tr_matrix = gen.create_element('tr');
	this.main_td_matrix = gen.create_element('td');
	this.matrix_table = gen.create_element('table');
	this.matrix_tbody = gen.create_element('tbody');

	this.main_div.appendChild( this.main_table );
	this.main_table.appendChild( this.main_tbody );

	this.main_tbody.appendChild( this.main_tr_header );
	this.main_tr_header.appendChild( this.main_td_header );
	this.main_td_header.appendChild( this.header_table );
	this.header_table.appendChild( this.header_tbody );

	this.main_tbody.appendChild( this.main_tr_matrix );
	this.main_tr_matrix.appendChild( this.main_td_matrix );
	this.main_td_matrix.appendChild( this.matrix_table );
	this.matrix_table.appendChild( this.matrix_tbody );

	status_message = document.getElementById(this.objectname + '_status_message');
	if ( status_message ) this.main_div.removeChild(status_message);

	if ( !width ) alert('please spesify width and height');

	this.main_div.style.backgroundColor = 'white';
	this.main_div.style.border = '1px solid silver';
	this.main_div.style.width = width;

	this.main_table.style.width = '100%';

	this.main_td_header.style.width = '100%';
	this.main_td_header.style.border = '1px solid silver';

	this.main_td_matrix.style.width = '100%';
	this.main_td_matrix.style.border = '1px solid silver';
	this.main_td_matrix.style.backgroundColor = '#E1E0DE';

	this.header_table.style.width = '100%';

	this.matrix_table.style.width = '100%';

	this.last_row_with_text = 0;
	this.default_num_rows = 4;
	this.default_num_cols = 0;
	this.header_definition = Array();


	//----------------------------------------------------------------------------------------------------------------
	//Methods()
	//----------------------------------------------------------------------------------------------------------------

	this.load_data = function ( matrix_data )
		{
		if ( matrix_data.length > 0 )
			{
			for ( var row in matrix_data ) this.add_row( matrix_data[row] );
			}
		else
			{
			for ( var count = 0 ; count < this.default_num_rows ; count++ ) this.add_row();
			}
		}

	this.add_header = function( value, width )
		{
		var heading = Array();
			
		heading['width'] = width;
		heading['value'] = value;

		this.default_num_cols += 1;
		this.header_definition[this.default_num_cols] = heading;
		}

	this.show_headers = function( )
		{
		var header = Array();
		var header_row = gen.create_element('tr','', this.objectname + '_heading', this.objectname + '_heading');
		var width;

		for ( count = 1 ; count <= this.default_num_cols ; count++ )
			{
	   		var txt = document.createTextNode( this.header_definition[ count ]['value'] );
	   		header[count] = gen.create_element('td');
			header[count].appendChild( txt );
			header[count].style.fontWeight = 'bold';
			header[count].style.fontFamily = 'arial,verdana,tohoma';
			header[count].style.fontSize = '12px';
			header[count].style.textAlign = 'center';
			header[count].style.color = 'black';
			header[count].style.borderRight = '1px solid silver';
			header[count].style.borderLeft = '1px solid silver';
			header[count].style.width = this.header_definition[ count ]['width'];
			header_row.appendChild(header[count]);
			}

		this.header_tbody.appendChild(header_row); 
		}


	this.event_handler = function( event )
		{
		if ( typeof event == 'object' )  
			{
			event = event || window.event;
			var source_control = event.target || event.srcElement;
			}
		else 
			if ( event.charAt(0) == this.objectname.charAt(0) )
				{
				source_control = document.getElementById(event);
				event = null;
				}

		var num_rows = this.get_num_rows();
		var max_row_with_text = this.get_number_max_row_with_text();

		//alert(max_row_with_text);

		if ( source_control.options ) 
			var this_text_length = source_control.options.selectedIndex;
		else
			var this_text_length = source_control.value.length;

		if ( max_row_with_text < this.last_row_with_text ) 
			{
			this.remove_empty_rows( num_rows, max_row_with_text );
  			}
			  
		if ( max_row_with_text > this.last_row_with_text ) 
			{
			if ( max_row_with_text > num_rows - 1 ) this.add_row();
  			}

		this.last_row_with_text = max_row_with_text;

		if ( event )
  			if ( event.type == 'paste' || event.type == 'cut' ) 
				window.setTimeout( "this.event_handler('" + source_control.id + "')", 10 );	
		}

//------------------------------------------------------------------------------------------------------------------------------------------------------------
// modify the event_handler to be used by external events.
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	eval('this.event_handler = ' + ( this.event_handler + '' ).replace(/this\./gi,this.objectname + '.'));
//------------------------------------------------------------------------------------------------------------------------------------------------------------


	this.add_row = function( row_data )
		{
		var rows = this.get_num_rows() + 1;
	  	var matrix_row = gen.create_element('tr','',this.objectname + '_r_' + rows, this.objectname + '_r_' + rows);
		var input_name;
		var count;

		for ( count = 1 ; count <= this.default_num_cols ; count++ )
			{
			input_name = this.objectname + '_skills_matrix[' + rows + '][' + count + ']';

			switch( count )
					{
					case 2:	
						var inp = skills_matrix.create_dropdown( input_name, input_name );
						skills_matrix.add_dropdown_option( inp,'0','(Make Selection)');
						skills_matrix.add_dropdown_option( inp,'1','Beginner');
						skills_matrix.add_dropdown_option( inp,'2','Intermediate');
						skills_matrix.add_dropdown_option( inp,'3','Expert');
						gen.attachEvent( inp, 'change', this.event_handler, true );
						break;
					case 3:
						var inp = skills_matrix.create_dropdown( input_name, input_name );
						skills_matrix.add_dropdown_option( inp,'0','(Make Selection)');
						skills_matrix.add_dropdown_option( inp,'1','Currently');
						skills_matrix.add_dropdown_option( inp,'2','1 year ago');
						skills_matrix.add_dropdown_option( inp,'3','2 years ago');
						skills_matrix.add_dropdown_option( inp,'4','more than 3 years ago');
						gen.attachEvent( inp, 'change', this.event_handler, true );
						break;
					default:
						var inp = skills_matrix.create_textbox( input_name, input_name );
						inp.style.border = '1px solid silver';
						gen.attachEvent( inp, 'cut', this.event_handler, true );
						gen.attachEvent( inp, 'input', this.event_handler, true );
						gen.attachEvent( inp, 'paste', this.event_handler, true );
						gen.attachEvent( inp, 'focus', this.event_handler, true );
						gen.attachEvent( inp, 'keyup', this.event_handler, true );
						gen.attachEvent( inp, 'keydown', this.event_handler, true );
					}

			var matrix_row_td = gen.create_element('td');
			var field_width = this.header_definition[count]['width'];
			matrix_row_td.style.width = field_width;
			inp.style.width = field_width.charAt(field_width.length - 1) == '%'?'100%':field_width;

			if ( row_data && row_data.length ) 
				{
				switch( count )
						{
						case 2:	
							inp.options[row_data[count]].selected = true;
							break;
						case 3:	
							inp.options[row_data[count]].selected = true;
							break;
						default:	
							inp.value = row_data[count];
					}
				}

			matrix_row_td.appendChild(inp);
			matrix_row.appendChild(matrix_row_td);
			}
		this.matrix_tbody.appendChild(matrix_row);
		}

	this.remove_row = function()
		{
		var rows = this.get_num_rows();
		var last_row = document.getElementById( this.objectname + '_r_' + rows );

		if ( rows > 1 ) this.matrix_tbody.removeChild( last_row ); 
		}

	this.remove_empty_rows = function( rows, max_row_with_text )
		{
		for ( count = rows ; count > this.default_num_rows ; count-- ) 
			if ( count > max_row_with_text + 1 ) this.matrix_tbody.removeChild( document.getElementById( this.objectname + '_r_' + count ) ); 
		}


	this.get_num_rows = function()
		{
		var number;

		try {
			number = this.matrix_table.rows.length;
			}
		catch( e )
			{	
			number = 0;
			}
		return number;
		}

	this.get_number_max_row_with_text = function()
		{
		var num_rows = this.get_num_rows();

		for ( count = num_rows; count >= 1 ; count-- )
			if ( this.row_has_text( count ) ) return count;
		return 0;		
		}

	this.row_has_text = function( row_number )
		{
		var field = '';
		var count = 1;

		for ( count = 1 ; count <= this.default_num_rows ; count++ )
			{ 
			field = document.getElementById( this.objectname + '_skills_matrix[' + row_number + '][' + count + ']');
			if ( field )
				{
				if ( field.options ) 
					{	
					if ( field.options.selectedIndex > 0 ) return true;
					}
				else
					{
					if ( field.value.length > 0 ) return true;
					}
				}
			}

		return false;
		}

	this.get_textarea_row_col = function( textarea )
		{
		var output = Array();
		if ( textarea ) 
			{
  			var id_split = textarea.id.split('[');
			output['row'] = gen.trim(id_split[1],']');
			output['col'] = gen.trim(id_split[2],']');
			return output;
			}
		}

	//----------------------------------------------------------------------------------------------------------------
	} 


	//----------------------------------------------------------------------------------------------------------------
	//Static members
	//----------------------------------------------------------------------------------------------------------------
	skills_matrix.log = function( text )
		{
		document.getElementById('scroll_values').innerHTML = text;
		}

	skills_matrix.create_textarea = function( name, id, rows, cols  )
		{
	    element = document.createElement('textarea');
		if ( id > '' ) element.setAttribute('id', id);
		if ( name > '') element.setAttribute('name', name);
		if ( cols > '') element.setAttribute('cols', cols);
		if ( rows > '') element.setAttribute('rows', rows);

		element.style.overflow = 'hidden';
		return element;
		}

	skills_matrix.create_textbox = function( name, id  )
		{
	    element = document.createElement('input');
		element.setAttribute('type', 'text');
		if ( id > '' ) element.setAttribute('id', id);
		if ( name > '') element.setAttribute('name', name);

		return element;
		}

	skills_matrix.create_dropdown = function( name, id  )
		{
	    element = document.createElement('select');
		if ( id > '' ) element.setAttribute('id', id);
		if ( name > '') element.setAttribute('name', name);

		return element;
		}
	
	skills_matrix.add_dropdown_option = function( dropdown, name, value )
		{
		if ( name > '' && value > '' ) dropdown.options[dropdown.options.length] = new Option(value,name );
		return dropdown.options[dropdown.options.length];
		}

	//----------------------------------------------------------------------------------------------------------------


