// Names of selectbox
var Names = ConnectorDB[0].split( Delimiter ); 
// Hints of selectbox
var Hints = ConnectorDB[1].split( Delimiter ); 

// array of array - properties of Connectors; 1st index == connector index, 2nd index == connector property
var Connectors = new Array;
for ( var i = 2; i < ConnectorDB.length; ++i ) {
	Connectors.push( ConnectorDB[i].split( Delimiter ) );
}

// All properties of connectors - NO dublicate lists !!!
var Properties = new Array;

// Create properties list based on 'Visible' value. If init==true - 1st run
function UpdateProperties(Init) {
	
	if (Init) {
		// For each connector setup property 'Visible' to 1
		for ( var ConnectorIdx = 0; ConnectorIdx < Connectors.length; ++ConnectorIdx ) {
			Connectors[ ConnectorIdx ][ 0 ] = 1;
		}
	}

	// create new properties list
	Properties = new Array;

	// For each property
    for ( var PropertyIndex = 0; PropertyIndex < Names.length; ++PropertyIndex ) { 
    	var Arr = new Array; // create empty Array()
		// For each connector
    	for ( var ConnectorIdx = 0; ConnectorIdx < Connectors.length; ++ConnectorIdx ) { 
    		if ( Connectors[ ConnectorIdx ][ 0 ] != 0 ) {
        		var Property = Connectors[ ConnectorIdx ][ PropertyIndex ];
        		var Found = 0; // flag - found Property in the Arr ?

        		// check, Property included in the Arr list ?
        		for ( var i = 0; i < Arr.length; ++i ) {
        			if ( Property == Arr[i] ) {
        				Found = 1;
        				break;
        			}
        		}
        		// if no included - insert to list
        		if ( Found == 0 ) {
        			Arr.push( Property );
        		}
			}
		}
		// Add Arr to properties list
    	Properties.push( Arr ); 
    }
}

// Filter Connectors database : set 'Visible' to 0 if selected Property != value
function FilterConnectors(PropertyIndex,Value) {
	// For each connectors
	for ( var ConnectorIdx = 0; ConnectorIdx < Connectors.length; ++ConnectorIdx ) {
		if ( Connectors[ ConnectorIdx ][ PropertyIndex ] != Value ) {
			Connectors[ ConnectorIdx ][ 0 ] = 0;
		}
	}
}


function WriteTable() {
    document.write('<h2>SMX-Scopio Probe Tips:</h2>');
	document.write('<table id="cameraList"><tbody>');
	for ( var i = 0; i < ConnectorNames.length; ++i ) {
		document.write( '<tr id="Connector' + i + '"><td>' );
		document.write( '<a href="'+ConnectorLinks[i]+'">' + ConnectorNames[i] + '</a>' ); // name of connector
    	document.write( '</tr></tr>' );
    }
	document.write('</tbody></table>');
} 


function UpdateTable() {
	// Detect what connector is visible
	var ConnectorCount = ConnectorNames.length;
	var ConnectorVisible = new Array( ConnectorCount );

	for ( var i = 0; i < ConnectorCount; ++i ) {
		ConnectorVisible[i] = 0;
	}

	// For each connectors
	for ( var ConnectorIdx = 0; ConnectorIdx < Connectors.length; ++ConnectorIdx ) {
		if ( Connectors[ ConnectorIdx ][ 0 ] != 0 ) {
			ConnectorVisible[  Connectors[ ConnectorIdx ][ 1 ] ] = 1;
		}
	}

	// For each type of connectors
	for ( var i = 0; i < ConnectorCount; ++i ) {
		var elm = document.getElementById( 'Connector' + i );
		
		if (elm) {
			if ( ConnectorVisible[i] == 0 ) {
				elm.style.display= 'none';
			} else {
				elm.style.display= '';
			}
		}
	}
}



function ClearSelect(selectbox) {
	for( var i = selectbox.options.length-1; i>=0; i-- ) {
		selectbox.remove(i);
	}
}

function AddOption( selectbox, text, value ) {
	var optn = document.createElement( "OPTION" );
		
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}


function UpdateOneSelect(PropertyIndex) {
	var PropertyValues = Properties[ PropertyIndex ];
	var select_name = 'SELECT' + PropertyIndex;
	var selectbox = eval("document.joss."+select_name);

	ClearSelect( selectbox );

	for ( var i = 0; i < PropertyValues.length; ++i ) {
		AddOption( selectbox, PropertyValues[ i ], PropertyValues[ i ] )
	}
}

function UpdateAllSelect() {
	for ( var i = 0; i < Criterias.length; ++i ) {
		UpdateOneSelect( Criterias[i] );
	}
}

function WriteOneSelect(PropertyIndex) {
	var PropertyValues = Properties[ PropertyIndex ];
	var width = 0;
	// detect width of select
	for ( var i = 0; i < PropertyValues.length; ++i ) {
		var Value = PropertyValues[ i ];
		if ( Value.length > width ) {
			width = Value.length;
		}
	}
	document.writeln( '<div class="grid_selector"><div class="filterSelect"><label>' + Names[PropertyIndex] + ':</label>' + '&nbsp;');
	document.writeln( '<select name="SELECT' + PropertyIndex + '" size=' + PropertyValues.length + ' width='+width+' onChange="SelectItem(this,'+PropertyIndex+')" ></select>' );
	document.writeln( '<span class="filterHint">' + Hints[ PropertyIndex] + '</span></div></div>');
}

function WriteAllSelect() {
	for ( var i = 0; i < Criterias.length; ++i ) {
		WriteOneSelect(	Criterias[i] );
	}
}


function SelectItem(selectbox,PropertyIndex) {
	FilterConnectors( PropertyIndex, selectbox.options[selectbox.selectedIndex].value );
	UpdateProperties(false);
	UpdateAllSelect();
	UpdateTable();
}

function ClearFilter() {
	UpdateProperties(true);
	UpdateAllSelect();
	UpdateTable();
}

