#!/usr/bin/perl 

use File::stat;
use Time::localtime;
use Switch;
use Spreadsheet::Read;

#############################
# Config
#

$version		= 'v2.04';

# $scriptName 	= "chipParse-v2.01.pl";
$scriptName 	= $ENV{'SCRIPT_NAME'};
$serverName 	= $ENV{'SERVER_NAME'};

$spreadsheetFileName = "chipCollection-v1.00.ods";

# $fullFileName 	= "chipList-v2.14.html";
# $manufFileName	= 'chipManuf-v2.html';
# $familyFileName	= 'chipFamily-v2.html';
# $familyListFileName = 'chipFamilyList-v2.html';

$familyErasOrderedFileName = 'familyErasOrdered.txt';

$imageLoc	= 'main-images';
$photoLoc 	= 'main-images/chip-v2';
$thumbLoc 	= 'main-images/chip-v2/sm';
$logoLoc 	= 'main-images/ic-manuf-logos';
$datasheet_loc = 'main-files/datasheets';

$skipLines 	= 1;


##### Manuf file columns #####



#### Family file columns ####



######################################################
############## Coding ################################
######################################################

sub check_script_location {
	my @scriptParts = split(/\//,$scriptName);
	$relativeLoc = '.';
	if($scriptName =~ /\//) {
		@scriptParts = split(/\//,$scriptName);
		# print "$#scriptParts $scriptParts[$#scriptParts]<br />\n";
		$scriptName = $scriptParts[$#scriptParts];
		if($#scriptParts > 1) {
			$relativeLoc = '..';
		}
	}
}

################## Import ARGS ##############
sub read_input {
    local ($buffer, @pairs, $pair, $name, $value, %FORM);
    # Read in text
    $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
    if ($ENV{'REQUEST_METHOD'} eq "POST")
    {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    } else
    {
        $buffer = $ENV{'QUERY_STRING'};
    }
    # Split information into name/value pairs
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs)
    {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%(..)/pack("C", hex($1))/eg;
        $FORM{$name} = $value;
    }
    %FORM;
}
###############Inport ARGS (end) ##################











##################################################
############### Reading Files ####################

#open file
sub openFile { 
	my $fileName = $_[0];
	open(FILEVARIABLE, "< $fileName");
	@fileLines= <FILEVARIABLE>;
	close(FILEVARIABLE);
	return @fileLines;
}

# sub loadSpreadsheets {
	# $chipListSpreadsheet = ReadData ("chipCollection-v1.00.ods");
# }

sub bbl_sort {
	# my (@a, @b) = @_;
    my @array =  @_;
	# print_2d(@array);
	my $row_sort = 0;
	# my $array2 =  @{$_[1]};
    my $not_complete = 1;
    my $index;
    my $len = ($#{$array[$row_sort]} - 2);
    # print "|sub=" . scalar @array . " -- " . scalar @array2 . "|";
    while ($not_complete) {
        $not_complete = 0;
        foreach $index (0 .. $len) {
            if ($array[$row_sort][$index] > $array[$row_sort][$index + 1]) {
                my $temp = $array[0][$index + 1];
				my $temp2 = $array[1][$index + 1];
                $array[0][$index + 1] = $array[0][$index];
				$array[1][$index + 1] = $array[1][$index];
                $array[0][$index] = $temp;
				$array[1][$index] = $temp2;
                $not_complete = 1;
				# print @array2[$index]; 
            }
        }
    }
	return (@array);
}


sub parse_ods {
	my $file = $_[0];
	my @sheets = ();
	# my @rows = ();
	# my @cells = ();
	my @data = ();
	my $sheet_num=0;
	my $row_num=0;
	my $col_num=0;
	my $col_repeated=0;
	
	my $zip = Archive::Zip->new($file);
	my $content=$zip->contents('content.xml');
	
	$content =~ s/<\?xml .*\?>\n//;
	$content =~ s/^.*<office:spreadsheet>//;
	$content =~ s/<\/office:spreadsheet>.*$//;
	$content =~ s/<table:named-expressions>.*<\/table:named-expressions>//;
	$content =~ s/<table:database-ranges>.*<\/table:database-ranges>//; #keep greedy?
	$content =~ s/<\/table:table-cell>//; #maybe not neeeded
	$content =~ s/<\/table:table-row>//; #maybe not neeeded
	# $content =~ s///; 
	
	@sheets = split(/<table:table .*?>/,$content);
	# print "Sheets= $#sheets \n";	
	shift(@sheets); #skips the first _blank_ element

	# print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";	
	foreach $sheet (@sheets) {
		$row_num=-1;
		foreach $row (split(/<table:table-row .*?>/,$sheet)) {
			# print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";	
			if ($row_num != -1) {
				#skips first element
				$col_num=-1;
				foreach $cell ( split(/<table:table-cell/,$row )) {
					if ( $col_num == -1 ) {
						#skips first element
						$col_num++;
					} else {
						$cell_content = $cell;
						if ( $cell_content =~ /<text:p>/ ) {
							$cell_content =~ s/.*<text:p>//;
							$cell_content =~ s/<\/text:p>.*//;
						} else { # no content in cell
							$cell_content = '';
						}
						if ($cell =~ /number-columns-repeated/) {
							# repeat cells contents n-times
							$col_repeated = $cell;
							$col_repeated =~ s/table:number-columns-repeated="//;
							$col_repeated =~ s/".*//;
							# print "repeats: $col_repeated ";
							for (my $x=1;$x<=$col_repeated;$x++) {
								$data[$sheet_num][$row_num][$col_num]=$cell_content;
								# print "#$data[$sheet_num][$row_num][$col_num] ($sheet_num,$row_num,$col_num) ";
								$col_num++;
							}
						} else {
							# no repeated cols
							$data[$sheet_num][$row_num][$col_num]=$cell_content;
							# print "#$data[$sheet_num][$row_num][$col_num] ($sheet_num,$row_num,$col_num) ";
							$col_num++;
						}
					}
				}
				# print "\n";
			}
			$row_num++
		}
		$sheet_num++;
	}
	return @data;
}


############# Reading Files (end) ##################
####################################################











########################################################
################# Processing Data ######################


sub getColLabels {
	my $col=1;
	while ( &chipData2(0,$col) ) {
		# print &chipData2(1,$col) . ", ";
		switch (&chipData2(0,$col)) {
			case "My Label" {$colJeffLable=$col}
			case "#" 		{$colTotal	=$col}
			case "Trade" 	{$colTrade	=$col}
			case "Manufacturer" {$colManuf	=$col}
			case "Part #" 	{$colPart	=$col}
			case "Lable ID"	{$colLabel	=$col}
			case "Type" 	{$colType	=$col}
			case "Family" 	{$colFamily	=$col}
			case "Core" 	{$colCore	=$col}
			case "Architecture"	{$colArch	=$col}
			case "Bus Width" 	{$colDataBus=$col}
			case "Address Width" {$colAddBus=$col}
			case "Speed" 	{$colSpeed	=$col}
			case "blank" 	{$colBlank	=$col}
			case "FPU" 		{$colFPU	=$col}
			case "L1 cache" {$colL1		=$col}
			case "L2 cache" {$colL2		=$col}
			case "L3 cache"	{$colL3		=$col}
			case "Package" 	{$colPack	=$col}
			case "Transistors" 	{$colTrans	=$col}
			case "Process Size" {$colProc	=$col}
			case "Technology" 	{$colTech	=$col}
			case "Die Size" {$colDie	=$col}
			case "Vcc" 		{$colVcc	=$col}
			case "Vcc (I/O)" 		{$col_io_volt	=$col}
			case "Power Max" 		{$col_power	=$col}
			case "Date Introduced" 	{$colDate=$col}
			case "Condition" 	{$colCond	=$col}
			case "Variants" 	{$colVar	=$col}
			case "Description" 	{$colDesc	=$col}
			case "Photo 1" 	{$colPhoto1	=$col}
			case "Photo 2" 	{$colPhoto2	=$col}
			case "Photo 3" 	{$colPhoto3	=$col}
			case "Photo 4" 	{$colPhoto4	=$col}
			case "Photo 5" 	{$colPhoto5	=$col}
			case "Datasheet" {$col_datasheet	=$col}
			case "Lable ID" {$col_lable_id	=$col}
			case "Alt ID"	{$col_alt_id	=$col}
			case "ROM Int"	{$col_rom_int	=$col}
			case "ROM type"	{$col_rom_type	=$col}
			case "RAM Int"	{$col_ram_int	=$col}
			case "RAM Max"	{$col_ram_max	=$col}
			case "Socket"	{$col_socket	=$col}
			case "Features"	{$col_features	=$col}
			case "# Cores"	{$col_num_cores	=$col}
			case "Designer of Core"	{$col_designer	=$col}
			case "Bus speed"	{$col_bus_speed	=$col}
			else {}
		}
		$col++;
	}
	$col=1;
	while ( &manufData2(0,$col) ) {
		# print &chipData2(1,$col) . ", ";
		switch (&manufData2(0,$col)) {
			case "Short name"	{$colManufName	=$col}
			case "Abrev"	{$colManufAbrev	=$col}
			case "Full name"	{$colManufFullName	=$col}
			case "Date"	{$colManufDate	=$col}
			case "Homepage"	{$colManufHomepage	=$col}
			case "Wiki"	{$colManufWiki	=$col}
			case "Traditional"	{$colManufTrad	=$col}
			case "Current"	{$colManufCurrent	=$col}
			case "Status"	{$colManufStatus	=$col}
			case "Description"	{$colManufDesc	=$col}
			case "Wide logo"	{$colManufWideLogo	=$col}
			case "Logo1"	{$colManufLogo1	=$col}
			case "Logo2"	{$colManufLogo2	=$col}
			case "Logo3"	{$colManufLogo3	=$col}
			case "Logo4"	{$colManufLogo4	=$col}
			case "Logo5"	{$colManufLogo5	=$col}
			else {}
		}
		$col++;
	}
	$col=1;
	while ( &familyData2(0,$col) ) {
		# print &chipData2(1,$col) . ", ";
		switch (&familyData2(0,$col)) {
			case "Var"		{$colFamilyName	=$col}
			case "Full name"	{$colFamilyFullName	=$col}
			case "Manuf"	{$colFamilyManuf	=$col}
			case "Date"		{$colFamilyDate	=$col}
			case "Chip"		{$colFamilyChip	=$col} #maybe dont use
			case "Arch"		{$colFamilyArch	=$col}
			case "Freq range"	{$colFamilyFreq	=$col}
			case "Technology"	{$colFamilyTech	=$col}
			case "# Intructions"	{$colFamilyIntrucNum	=$col}
			case "Int Set Name"	{$colFamilyInstrucName	=$col}
			case "Intruction link"	{$colFamilyInstrucLink	=$col}
			case "Description"	{$colFamilyDesc	=$col}
			case "Core arch"	{$colFamilyCoreArch	=$col}
			case "Wiki"			{$colFamilyWiki	=$col}
			case "Homepage"		{$colFamilyHomepage	=$col}
			case "Logo"			{$colFamilyLogo	=$col}
			else {}
		}
		$col++;
	}

}

sub getColChipData {
	my $row=1;
	my @manufList = ();
	my @familyList = ();
	@manufListGlobal = ();
	@familyListGlobal = ();

	while (&chipData2($row,$colPart) ne '') {
		#Makes a list of manuf and fams
		$manufList[$row-1]=&chipData2($row,$colManuf);
		$familyList[$row-1]=&chipData2($row,$colFamily);
		
		# This makes a list and count of manuf
		if ($manufListCountGlobal{&chipData2($row,$colManuf)} eq '') {
			$manufListCountGlobal{&chipData2($row,$colManuf)} =1;
		} else {
			$manufListCountGlobal{&chipData2($row,$colManuf)}++;
		}

		# Count families for this manufacturer
		if ( &chipData2($row,$colManuf) eq $data{'manufacturer'} ) {
			if (&chipData2($row,$colFamily) eq '') {
				$fam='Other';
			} else {$fam=&chipData2($row,$colFamily);}
			if ( $manufFamilyCountGlobal{$fam} eq '' ) {
				$manufFamilyCountGlobal{$fam} = 1;
			} else {
				$manufFamilyCountGlobal{$fam}++;
			}
		}
		$row++;
	}
	$totalChipCountGlobal=$row;

	undef %saw;
	@saw{@manufList} = ();
	@manufListGlobal = sort keys %saw;  
	# foreach $man (@manuf) {print "$man, ";}print "\n";
	undef %saw;
	@saw{@familyList} = ();
	@familyListGlobal = sort keys %saw;  
	# foreach $key (sort keys %manufListCountGlobal) {print "$key -> $manufListCountGlobal{$key} | ";}
	$totalManufCountGlobal=$#manufListGlobal+1;
	$totalFamilyCountGlobal=$#familyListGlobal+1;

	# Count families for this manufacturer (Part 2)
	if ( $data{'page'} eq 'manufacturer' ) {
		@manufFamilies = keys %manufFamilyCountGlobal;
		&sortManufFamilies(%manufFamilyCountGlobal);
		# foreach $key (keys %manufFamilyCountGlobal) {print "$key -> $manufFamilyCountGlobal{$key} | ";}
	}
}

sub sortManufFamilies {
	#this creating two ordered arrays, organized by era to print prettylike
	# @manufFamilyListGlobal
	# @manufFamilyListCountGlobal
	my %manufFamilyCount = %manufFamilyCountGlobal; # $_[0]???
	my $x=0;
	my $hasOther=0;
	my $isInList=0;
	
	foreach $famEra (@chipErasGlobal) { #sorts the families included in chipEras list  
		if ($manufFamilyCount{$famEra} ne '' ) {
			$manufFamilyListGlobal[$x]=$famEra;
			$manufFamilyListCountGlobal[$x]=$manufFamilyCount{$famEra};
			# print "$x -- $manufFamilyListGlobal[$x] ($manufFamilyListCountGlobal[$x]), ";
			$x++;	
		}
	}
	
	foreach $manufFamily (keys %manufFamilyCount) {
		$isInList=0;
		if ($manufFamily eq 'Other') {
			$hasOther=1;
			$isInList=1; #a bypass
		} else {
			foreach $famEra (@chipErasGlobal) {
				if ( $manufFamily eq $famEra){
					$isInList=1;
				}
			}
		}
		if ($isInList == 0){ #is its not in the list, add it
			$manufFamilyListGlobal[$x]=$manufFamily;
			$manufFamilyListCountGlobal[$x]=$manufFamilyCount{$manufFamily};
			# print "$x -- $manufFamilyListGlobal[$x] ($manufFamilyListCountGlobal[$x]), ";
			$x++;
		}
	}

	if ($hasOther == 1) { #Adds Other to list
		$manufFamilyListGlobal[$x]='Other';
		$manufFamilyListCountGlobal[$x]=$manufFamilyCount{'Other'};
		$x++
	}

	if ($x >= 2) {
		unshift(@manufFamilyListGlobal, "All");
		unshift(@manufFamilyListCountGlobal, "null");
	}

	# print "======================";
	# for ($x=0;$x<=$#manufFamilyListGlobal;$x++) { print "$manufFamilyListGlobal[$x] ($manufFamilyListCountGlobal[$x]) |\n"; }

}

############### Process Data (end) ######################
#########################################################
#
#

sub chipData2 {
	# Format: $cellContents = &chipData2($row,$col)
	my $col = $_[1];
	my $row = $_[0];
	return $spreadsheetData[0][$row][$col];
	# return $chipListSpreadsheet->[1]{cr2cell($col,$row)};
}	
sub manufData2 {
	# Format: $cellContents = &chipData2($row,$col)
	my $col = $_[1];
	my $row = $_[0];
	return $spreadsheetData[1][$row][$col];
	# return $chipListSpreadsheet->[2]{cr2cell($col,$row)};
}	
sub familyData2 {
	# Format: $cellContents = &chipData2($row,$col)
	my $col = $_[1];
	my $row = $_[0];
	return $spreadsheetData[2][$row][$col];
	# return $chipListSpreadsheet->[3]{cr2cell($col,$row)};
}	




######### Process list (end) ############################

sub grapGlobals {
	# $totalChipCountGlobal=1;


	$pageTypeGlobal	= $data{'page'};
		# manufacturer
		# fullList
		# tradeList
	$selectedManufglobal	= $data{'manufacturer'};
	$selectedFamilyGlobal	= $data{'family'};


}

# sub chipSelectedDetails { # Currently unimplemented
	# my $x = 0;
	# my $y = 0;

	# print "chipDetails<br />\n";
	# for ($x=0; $x<=$#chipData; $x++) {
		# print "-";
		# if ( &chipData2($x,$colPart) eq $data{'chipName'} ) {
			# foreach $y (0..40) {
				# $chipSelected[$y] = &chipData2($x,$y);
			# }
			# $chipSelectedFamily = $chipSelected[$colFamily];

			# print "...$chipSelectedFamily...<br />\n";
		# }
	# }

# }


sub wikify { # makes wikipedia links
	my $text=$_[0];
	my $url='http://en.wikipedia.org/wiki/';

	@linkables = qw(CMOS NMOS PMOS CHMOS HMOS PSoC EPROM BIOS I2C);

	foreach $link (@linkables) {
		$text =~ s/ $link / <a href=\"$url$link\">$link<\/a> /;
	}	
	# PLD	prigrammable logic device
	# flash Flash memory
	# PAL Programmable Array Logic
	# ASIC Application-specific integrated circuit
	# DRAM Dynamic random access memory
	# SRAM Static random access memory
	# SPI Serial Peripheral Interface
	# SCI Serial Communication Interface
	# GPIO General Purpose Input/Output
	# CAN Controller-area network
	# ADC Analog-to-digital converter
	# DAC digital-to-analog converter
}

###################################################
####### Coding (End) ##############################
###################################################


















############################################################
###################### HTML ################################
############################################################

############## Tables ################

############## Tables, chips ########
sub displayTableData2 {
	my $row = $_[0], $flipColor=$_[1], $thumb;
	#&chipData2($row,...)
	my $manufTableSub	=  &chipData2($row,$colManuf);
	my $type	= &chipData2($row,$colType);
	my $part	= &chipData2($row,$colPart);
	my $photo	= "PHOTO<br />To be added\n";
	my $family	= '';
	my $speed	= '?';
	my $arch	= '?';
	my $data	= '?';
	my $address = '?';
	my $package = '?';
	my $trans	= '?';
	my $tech	= '?';
	my $proc	= '?';
	my $die		= '?';
	my $volt	= '?';
	my $date	= '?';
	my $L1		= '?';
	my $L2		= '?';
	my $L3		= '?';
	my $max_ram	= '?';
	my $socket	= '?';
	my $ram_int	= '?';
	my $rom_int = '?';
	my $fpu		= '?';
	my $core	= '?';
	my $datasheet = '?';
	my $features = '?';
	my $num_cores = '?';
	my $core_designer = '?';
	my $text = '';

	
	if (&chipData2($row,$colPhoto1) ne ""){
		$photoFile = &chipData2($row,$colPhoto1);
		$thumb = $photoFile . '_sm.jpg';
		$photo = "<a href=\"$relativeLoc/$photoLoc/$photoFile\"><img src=\"$relativeLoc/$thumbLoc/$thumb\" width=\"250\"></a>\n";
		if (&chipData2($row,$colPhoto2) ne ""){
			$photoFile = &chipData2($row,$colPhoto2);
			$thumb = $photoFile . '_sm.jpg';
			$photo = $photo . "\t\t\t<br />\n\t\t\t<a href=\"$relativeLoc/$photoLoc/$photoFile\"><img src=\"$relativeLoc/$thumbLoc/$thumb\" width=\"250\"></a>\n";
		} if (&chipData2($row,$colPhoto3) ne ""){
			$photoFile = &chipData2($row,$colPhoto3);
			$thumb = $photoFile . '_sm.jpg';
			$photo = $photo . "\t\t\t<br />\n\t\t\t<a href=\"$relativeLoc/$photoLoc/$photoFile\"><img src=\"$relativeLoc/$thumbLoc/$thumb\" width=\"250\"></a>\n";
		} if (&chipData2($row,$colPhoto4) ne ""){
			$photoFile = &chipData2($row,$colPhoto4);
			$thumb = $photoFile . '_sm.jpg';
			$photo = $photo . "\t\t\t<br />\n\t\t\t<a href=\"$relativeLoc/$photoLoc/$photoFile\"><img src=\"$relativeLoc/$thumbLoc/$thumb\" width=\"250\"></a>\n";
		} if (&chipData2($row,$colPhoto5) ne ""){
			$photoFile = &chipData2($row,$colPhoto5);
			$thumb = $photoFile . '_sm.jpg';
			$photo = $photo . "\t\t\t<br />\n\t\t\t<a href=\"$relativeLoc/$photoLoc/$photoFile\"><img src=\"$relativeLoc/$thumbLoc/$thumb\" width=\"250\"></a>\n";
		}
	}
	if ( &chipData2($row,$colFamily) ne "" && &chipData2($row,$colFamily) ne "Other") {
		$family = "<div class=\"tableDisplayFamily\">" . &chipData2($row,$colFamily) ." Family</div>";
	}
	if ( &chipData2($row,$colSpeed) ne '' ) {
		$speed = &chipData2($row,$colSpeed);
	}
	if ( &chipData2($row,$colArch) ne '' ) {
		if ( &chipData2($row,$colArch) eq '16/32' ) {
			$arch = "<a href=\"http://en.wikipedia.org/wiki/16-bit\">16/32-bit</a>";
		} else {
			$arch = "<a href=\"http://en.wikipedia.org/wiki/&chipData2($row,$colArch)" . "-bit\">" . &chipData2($row,$colArch) . "-bit</a>";
		}
	}
	if ( &chipData2($row,$colDataBus) ne '' ) {
		$data = &chipData2($row,$colDataBus);
	}
	if ( &chipData2($row,$colAddBus) ne '' ) {
		$address = &chipData2($row,$colAddBus);
	}
	if ( &chipData2($row,$colPack) ne '' ) {
		$package = '<a href="http://howto.wikia.com/wiki/Howto_identify_chip_packages">' . &chipData2($row,$colPack) . '</A>';
	}
	if ( &chipData2($row,$colTech) ne '' ) {
		$tech = &chipData2($row,$colTech);
		if ( $tech =~ ' ' ) {
			@techFirst = split(/ /, $tech);
			$tech = $techFirst[0];
		}
		$tech="<a href=\"http://en.wikipedia.org/wiki/$tech\">$tech</a> $techFirst[1] $techFirst[2] $techFirst[3]\n";
	}
	if ( &chipData2($row,$colTrans) ne '' ) {
		$trans = &chipData2($row,$colTrans);
	}
	if ( &chipData2($row,$colProc) ne '' ) {
		$proc = &chipData2($row,$colProc);
	}
	if ( &chipData2($row,$colDie) ne '' ) {
		$die = &chipData2($row,$colDie);
	}
	if ( &chipData2($row,$colVcc) ne '' ) {
		$volt = &chipData2($row,$colVcc);
	}
	if ( &chipData2($row,$colDate) ne '' ) {
		$date = &chipData2($row,$colDate);
	}
	if ( &chipData2($row,$colL1) ne '' ) {
		$L1 = &chipData2($row,$colL1);
	}
	if ( &chipData2($row,$colL2) ne '' ) {
		$L2 = &chipData2($row,$colL2);
	}
	if ( &chipData2($row,$colL3) ne '' ) {
		$L3 = &chipData2($row,$colL3);
	}
	if ( &chipData2($row,$colCore) ne '' ) {
		$core = &chipData2($row,$colCore);
	}
	if ( &chipData2($row,$colFPU) ne '' ) {
		$fpu = &chipData2($row,$colFPU);
	}
	if ( &chipData2($row,$col_ram_int) ne '' ) {
		$ram_int = &chipData2($row,$col_ram_int);
	}
	if ( &chipData2($row,$col_ram_max) ne '' ) {
		$ram_max = &chipData2($row,$col_ram_max);
	}
	if ( &chipData2($row,$col_rom_int) ne '' ) {
		$rom_int = &chipData2($row,$col_rom_int);
	}
	if ( &chipData2($row,$col_socket) ne '' ) {
		$socket = &chipData2($row,$col_socket);
	}
	if ( &chipData2($row,$col_datasheet) ne '' ) {
		$datasheet = "<a href=\"$relativeLoc/$datasheet_loc/" . &chipData2($row,$col_datasheet) . "\">" . &chipData2($row,$col_datasheet) . "</a>";
	}
	if ( &chipData2($row,$col_features) ne '' ) {
		$features = &chipData2($row,$col_features);
	}
	if ( &chipData2($row,$col_num_cores) ne '' ) {
		$num_cores = &chipData2($row,$col_num_cores);
	}
	if ( &chipData2($row,$col_designer) ne '' ) {
		$core_designer = &chipData2($row,$col_designer);
	}
	if ( &chipData2($row,$colDate) ne '' ) {
		$date = &chipData2($row,$colDate);
	}
	if ( &chipData2($row,$colDate) ne '' ) {
		$date = &chipData2($row,$colDate);
	}

	$text .= "\t<!-- chip table -->\n";
	$text .= "\t<tr bgcolor=\"#454545\">\n";

	# if ( $flipColor == 0 ) {
		# $text .= "\t<tr bgcolor=\"#454545\">\n";
	# }else{
		# $text .= "\t<tr bgcolor=\"#545454\">\n";
	# }
	$text .= "\t\t<td width=\"256\">\n\t\t\t$photo\n\t\t</td>\n";

	$text .= "\t\t<td width=\"500\" valign=\"top\">\n";
	$text .=  "\t\t\t<div class=\"tableDisplayManufType\">$manufTableSub";
   	if ($type ne '') {
		$text .= " - $type";
	}
	$text .= "</div>\n";
	$text .= "\t\t\t$family<br />\n";
	
	$text .= "\t\t\t<div class=\"tableDisplayPartNum\">$part</div><br />\n";
	$text .= <<Endhtml;
		<table width="400">
			<tr>
				<td colspan="4">
					<span class="tableChipSubHeading">General Specifications:</span>
				</td>
			</tr><tr>
				<td nowrap width="100" style="padding-left: 10px;">
					<span class="tableChipParam">Speed:</span>
				</td><td nowrap width="100">
					<span class="tableChipValue">$speed</span>
				</td><td nowrap width="100">
					<span class="tableChipParam">Architecture:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$arch</span>
				</td>
			</tr>
Endhtml
	if ( $type =~ /(CPU|MCU|DSP)/ ) {
	$text .= <<Endhtml;
			<tr>
				<td colspan="4">
					<span class="tableChipSubHeading">Architecture Specifications:</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Processor core:</span>
				</td><td nowrap>
					<span class="tableChipValue">$core</span>
				</td><td nowrap>
					<span class="tableChipParam"># of cores:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$num_cores</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Designer:</span>
				</td><td nowrap>
					<span class="tableChipValue">$core_designer</span>
				</td><td nowrap>
					<span class="tableChipParam">FPU:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$fpu</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Ext data bus:</span>
				</td><td nowrap>
					<span class="tableChipValue">$data</span>
				</td><td nowrap>
					<span class="tableChipParam">Ext address bus:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$address</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Features:</span>
				</td><td colspan="3" valign="top">
					<span class="tableChipValue">$features</span>
				</td>
			</tr>
Endhtml
	}
	if ($type eq 'CPU') {
	$text .= <<Endhtml;
			<tr>
				<td colspan="4">
					<span class="tableChipSubHeading">Memory Specification:</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Max RAM:</span>
				</td><td nowrap>
					<span class="tableChipValue">$ram_max</span>
				</td><td nowrap>
					<span class="tableChipParam">L1 cache:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$L1</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam"></span>
				</td><td nowrap >
					<span class="tableChipValue"></span>
				</td><td nowrap>
					<span class="tableChipParam">L2 cache:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$L2</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam"></span>
				</td><td nowrap>					
					<span class="tableChipValue"></span>
				</td><td nowrap>
					<span class="tableChipParam">L3 cache:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$L3</span>
				</td>
			</tr>
Endhtml
	} elsif ( $type eq 'MCU' ) {
	$text .= <<Endhtml;
			<tr>
				<td colspan="4">
					<span class="tableChipSubHeading">Memory Specification:</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Max RAM:</span>
				</td><td nowrap>
					<span class="tableChipValue">$ram_max</span>
				</td><td nowrap>
					<span class="tableChipParam">L1 cache:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$L1</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Internal ROM:</span>
				</td><td nowrap >
					<span class="tableChipValue">$rom_int</span>
				</td><td nowrap>
					<span class="tableChipParam">L2 cache:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$L2</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Internal RAM:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$ram_int</span>
				</td><td nowrap>
					<span class="tableChipParam">L3 cache:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$L3</span>
				</td>
			</tr>
Endhtml
	}
	$text .= <<Endhtml;
			<tr>
				<td colspan="4">
					<span class="tableChipSubHeading">Technology:</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Technology:</span>
				</td><td nowrap>
					<span class="tableChipValue">$tech</span>
				</td><td nowrap>
					<span class="tableChipParam">Process:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$proc</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Transistors:</span>
				</td><td nowrap>
					<span class="tableChipValue">$trans</span>
				</td><td nowrap>
					<span class="tableChipParam">Die size:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$die</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Vcc:</span>
				</td><td nowrap >
					<span class="tableChipValue">$volt</span>
				</td><td nowrap>
					<span class="tableChipParam"></span>
				</td><td nowrap>					
					<span class="tableChipValue"></span>
				</td>
			</tr><tr>
				<td colspan="4">
					<span class="tableChipSubHeading">Other:</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Package:</span>
				</td><td nowrap>
					<span class="tableChipValue">$package</span>
				</td><td nowrap>
					<span class="tableChipParam">Socket:</span>
				</td><td nowrap>					
					<span class="tableChipValue">$socket</span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Introduced:</span>
				</td><td nowrap>
					<span class="tableChipValue">$date</span>
				</td><td nowrap>
					<span class="tableChipParam"></span>
				</td><td nowrap>					
					<span class="tableChipValue"></span>
				</td>
			</tr><tr>
				<td nowrap style="padding-left: 10px;">
					<span class="tableChipParam">Datasheet:</span>
				</td><td colspan="3" valign="top">
					<span class="tableChipValue">$datasheet</span>
				</td>				

		</table>
		</td>
	</tr>	
Endhtml
	$text .= "\t<!-- chip table (end) -->\n";
	
	# getting frequesncy to sort
	my $freq = $speed;
	if ( $freq =~ /kHz/ ) {
		$freq =~ s/kHz//;
		$freq *= 1000;
	} elsif ( $freq =~ /MHz/ ) {
		$freq =~ s/MHz//;
		$freq *= 1000000;
	} elsif ( $freq =~ /GHz/ ) {
		$freq =~ s/GHz//;
		$freq *= 1000000000;
	} 
	# print "<tr><td>Vampires .....  $speed = $freq</td></tr>";

	return ($text, $freq, $type);
}







###########################################
############ Table ##################
###########################################






sub displayTableManuf2 {
	my $manufSub = $_[0];

	my $name	= '';
	my $logo1 	= '';
	my $logo2 	= '';
	my $logo3 	= '';
	my $logo4 	= '';
	my $logo5 	= '';
	my $founded = '';
	my $homepage= '';
	my $wiki 	= '';
	my $trad 	= '';
	my $current = '';
	my $status 	= '';
	my $desc 	= '';

	print "\t<!-- manuf details -->\n";
	# print "$manufSub";

	my $x=2;
	while ( &manufData2($x,$colManufName) ne '' ) {
		# print &manufData2($x,$colManufName) . ", ";
		if ( $manufSub eq &manufData2($x,$colManufName) ) {
			$name	= &manufData2($x,$colManufFullName);
			$name	=~ s/_/ /g;
			$logo1 	= &manufData2($x,$colManufLogo1);
			$logo2 	= &manufData2($x,$colManufLogo2);
			$logo3 	= &manufData2($x,$colManufLogo3);
			$logo4 	= &manufData2($x,$colManufLogo4);
			$logo5 	= &manufData2($x,$colManufLogo5);
			$founded = &manufData2($x,$colManufDate);
			$homepage= &manufData2($x,$colManufHomepage);
			$wiki 	= &manufData2($x,$colManufWiki);
			$wiki	=~ s/ /_/g;
			$trad 	= &manufData2($x,$colManufTrad);
			$current = &manufData2($x,$colManufCurrent);
			$status 	= &manufData2($x,$colManufStatus);
			$desc 	= &manufData2($x,$colManufDesc);
			$abrev = &manufData2($x,$colManufAbrev);
			#print "............. &manufData2($x,$colManufName) ................<br />\n";
		}
		$x++;
	}

	if ( $name eq '' ) {
		$name = $data{'manufacturer'};
	}
	if ( $logo1 ne '' ) {
		$logo1 = "<a href=\"http://howto.wikia.com/wiki/Howto_identify_integrated_circuit_%28chip%29_manufacturers_by_their_logos\"><img src=\"$relativeLoc/$logoLoc/$logo1\" alt=\"$name\"></a>";
	}
	if ( $logo2 ne '' ) {
		$logo2 = "<a href=\"http://howto.wikia.com/wiki/Howto_identify_integrated_circuit_%28chip%29_manufacturers_by_their_logos\"><img src=\"$relativeLoc/$logoLoc/$logo2\" alt=\"$name\"></a><br />";
	}
	if ( $logo3 ne '' ) {
		$logo3 = "<a href=\"http://howto.wikia.com/wiki/Howto_identify_integrated_circuit_%28chip%29_manufacturers_by_their_logos\"><img src=\"$relativeLoc/$logoLoc/$logo3\" alt=\"$name\"></a><br />";
	}
	if ( $logo4 ne '' ) {
		$logo4 = "<a href=\"http://howto.wikia.com/wiki/Howto_identify_integrated_circuit_%28chip%29_manufacturers_by_their_logos\"><img src=\"$relativeLoc/$logoLoc/$logo4\" alt=\"$name\"></a><br />";
	}
	if ( $logo5 ne '' ) {
		$logo5 = "<a href=\"http://howto.wikia.com/wiki/Howto_identify_integrated_circuit_%28chip%29_manufacturers_by_their_logos\"><img src=\"$relativeLoc/$logoLoc/$logo5\" alt=\"$name\"></a>";
	}
	if ( $founded eq '' ) {
		$founded = "unknown";
	}
	if ( $homepage ne '' ) {
		if ( $homepage eq 'Dead' || $homepage eq 'dead' ) {
			$homepage = 'dead';
		} else {
			$homepage = "<a href=\"$homepage\">$homepage</a>";
		}
	} elsif ( $homepage =~ 'http' ) { $homepage = $homepage; } else { $homepage = "Unknown"; }
	if ( $wiki ne '' ) {
		$wiki = "&nbsp\; (<a href=\"http://en.wikipedia.org/wiki/$wiki\">Wikipedia</a>)";
	}
	if ( $trad eq '' ) {
		$trad = " - ";
	}
	if ( $current eq '' ) {
		$current = " - ";
	}
	if ( $status eq '' ) {
		$status = ' ? ';
	}
	if ( $desc ne '' ) {
		$desc = "<span class=\"manufTableParam\">Description:</span> <span class=\"manufTableValue\">$desc</span><br />\n";
	}
	if ( $abrev ne '' ) {
		$abrev = '(' . $abrev . ')';
	}

	print <<Endhtml;
	<table summary="manuf intro alignment"><tr><td>
	<table align="left" width="100%" summary="manuf description">
		<tr>
			<td width="80">
				$logo1
			</td>
			<td>
				<span class="tableTitle">$name $abrev</span>
			</td>
		</tr>
			<td>
				$logo2
				$logo3
				$logo4
				$logo5
			</td>
			<td>
				<span class=\"tableParam\">Founded:</span> <span class=\"tableValue\">$founded</span><br />\n
				<span class=\"tableParam\">Homepage:</span> <span class=\"tableValue\">$homepage</span>$wiki<br />\n
				<span class=\"tableParam\">Traditional products:</span> <span class=\"tableValue\">$trad</span><br />\n
				<span class=\"tableParam\">Current products:</span> <span class=\"tableValue\">$current</span><br />\n
				<span class=\"tableParam\">Status:</span> <span class=\"tableValue\">$status</span><br />\n
				$desc
			</td>
		</tr>
	</table>
Endhtml
	print "\t</td></tr><tr><td>\n";
	print "\t\t<br />\n\t\t<table border=\"0\" summary=\"family and,or chip list\"><tr><td>\n"; #table-X1
	print "\t\tList of IC families:<br />\n";
	print "\t\t<ul>\n";
	# &manufFamilyList;
	
			for ($x=0;$x<=$#manufFamilyListGlobal;$x++) {
				print "\t\t<li><a href=\"$scriptName?page=manufacturer&manufacturer=$data{'manufacturer'}&family=$manufFamilyListGlobal[$x]\"><span class=\"manufFamilyListSelected\">$manufFamilyListGlobal[$x]</a>";
				if ($manufFamilyListGlobal[$x] eq "All") {
					print "</span></li>\n";
				} else {
					print "&nbsp;($manufFamilyListCountGlobal[$x])</span></li>\n"; 
				}
			}
	

	print "\t\t</ul>\n";

	#if ( $manufCount[$selectedManufIDglobal]	<= 15 ) {
		#print "\t\t</td><td>\n";
		#print "\t\tList of chips:<br />\n";
		#print "\t\t<ul>\n";
		# chiplist
		 #print "\t\t</ul>\n";

	#}
	print "\t\t</td></tr></table>\n";#table-X1(end)
	print "\t</td></tr></table>\n";
	print "\t<!-- manuf details (end) -->\n";
}


sub displayTableFamily {
	my $x=2;
	my $name = $data{'family'};
	my $fullName = '';
	my $manufSub = '?';
	my $dateSub = '?';
	my $chip= '?';
	my $arch = '?';
	my $coreArch = '?';
	my $freq = '?';
	my $tech = '?';
	my $instrucName = '';
	my $instrucCount = '?';
	my $instrucLink = '?';
	my $homepage = '?';
	my $wiki = '?';
	my $date = '?';
	my $add = '?';
	my $desc = '...';
	my $logo = '';
	my $familyLogo = '';
	my $famSub = $data{'family'};
	my $link = '';
	my $linkName = '';
	my $datasheet = '?';
	#print "match => &familyData2($x,$colFamilyName] ";

	if ( $data{'page'} eq 'chip' ) {
		my $flipColor=0;
		$x=0;
		while(&chipData2($x,$colPart) ne ""){
			if ( &chipData2($x,$colPart) eq $data{'chipName'} ) {
				&displayTableData($x,$flipColor);
			}
			$x++;
		}
	}

	$x = 2;
	while ( &familyData2($x,$colFamilyName) ne '' ) {
		if ( &familyData2($x,$colFamilyName) eq $famSub ) {
			#print "match => &familyData2($x,$colFamilyName) ";
			$name 	= &familyData2($x,$colFamilyName);
			if ( &familyData2($x,$colFamilyFullName) ne '' ) {
				$fullName = "of the " . &familyData2($x,$colFamilyFullName) . " Family<br />";
			} else {
				$fullName = "of the " . $data{'family'} . " Family<br />";
			}
			if ( &familyData2($x,$colFamilyManuf) ne '' ) {
				$manufSub = &familyData2($x,$colFamilyManuf);
			}
			if ( &familyData2($x,$colFamilyDate) ne '' ) {
				$dateSub 	= &familyData2($x,$colFamilyDate);
			}
			# if ( &familyData2($x,$colFamilyChip) ne '' ) {
				# $chip	= &familyData2($x,$colFamilyChip);
			# }
			if ( &familyData2($x,$colFamilyArch) ne '' ) {
				$arch 	= &familyData2($x,$colFamilyArch) . '-bit';
			}
			if ( &familyData2($x,$colFamilyInstrucName) ne '' ) {
				$instrucName  = &familyData2($x,$colFamilyInstrucName);
			}
			if (&familyData2($x,$colFamilyIntrucNum)  ne '' ) {
				$instrucNum = &familyData2($x,$colFamilyIntrucNum);		
			}
			if ( &familyData2($x,$colFamilyInstrucLink) ne '' ) {
				$instrucLink = &familyData2($x,$colFamilyInstrucLink);
			}
			if ( &familyData2($x,$colFamilyCoreArch) ne '' ) {
				$coreArch = &familyData2($x,$colFamilyCoreArch);	
			}
			if ( &familyData2($x,$colFamilyFreq) ne '' ) {
				$freq = &familyData2($x,$colFamilyFreq);		
			}
			if (&familyData2($x,$colFamilyTech)  ne '' ) {
				$tech =&familyData2($x,$colFamilyTech) ;		
			}
			if ( &familyData2($x,$colFamilyHomepage) ne '' ) {
				($link,$linkName) = split(/ /,&familyData2($x,$colFamilyHomepage));
				if ($linkName eq '') { 
					$linkName = substr($link,0,20) . '...'; 
				}
				$homepage = "<a href=\"" . &familyData2($x,$colFamilyHomepage) . "\">$linkName</a>";	;		
			}
			if ( &familyData2($x,$colFamilyWiki) ne '' ) {
				$wiki = &familyData2($x,$colFamilyWiki);
				$wiki =~ s/ /_/;
				$wiki = "(<a href=\"http://en.wikipedia.org/wiki/$wiki\">Wikipedia</a>)";		
			}
			if ( &familyData2($x,$colFamilyLogo) ne '' ) {
				$familyLogo = &familyData2($x,$colFamilyLogo);		
			}

			#if ( $logo ne '' ) {
				#$logo = "<a href=\"http://howto.wikia.com/wiki/Howto_identify_integrated_circuit_%28chip%29_manufacturers_by_their_logos\"><img src=\"$relativeLoc/$logoLoc/$logo\" alt=\"$name\" width=\"70\"></a>";
			#}

			##### EXTRA LOGOs

			if (&familyData2($x,$colFamilyDesc) ne '') {
				$desc 	= &familyData2($x,$colFamilyDesc);
			}
		}
		$x++;
	}
	
	$x=2;
	while ( &manufData2($x,$colManufName) ne '' ) {
		#print &manufData2($x,$colManufFullName);
		if ( $data{'manufacturer'} eq &manufData2($x,$colManufName) ) {
			$nameManuf	= &manufData2($x,$colManufFullName);
			$logo 	= &manufData2($x,$colManufLogo1);
			#$founded = &manufData2($x,$colManufDate);
			#$homepage= &manufData2($x,$colManufHomepage);
			#$wiki 	= &manufData2($x,$colManufWiki);
			#$trad 	= &manufData2($x,$colManufTrad);
			#$current = &manufData2($x,$colManufCurrent);
			#$status 	= &manufData2($x,$colManufStatus);
			#$desc 	= &manufData2($x,$colManufDesc);
			#print "............. &manufData2($x,$colManufName) ................<br />\n";
		}
		$x++;
	}
	if ( $logo ne '' ) {
		$logo = "<a href=\"http://howto.wikia.com/wiki/Howto_identify_integrated_circuit_%28chip%29_manufacturers_by_their_logos\"><img src=\"$relativeLoc/$logoLoc/$logo\" alt=\"$name\"></a>";
	}
	if ( $nameManuf eq '' ) {
		$nameManuf = $data{'manufacturer'};
	}

	print <<Endhtml;

	<table align="left" summary="chip family desc">
		<tr>
			<td width="80">
				$logo
			</td>
			<td>
				<span class="tableTitle">$nameManuf - $name</span>
			</td>
		</tr>
		<tr>
			<td></td>
			<td>
			<!-- ---family summary--- -->
<!--			  <table summary="border for family table" frame="box"><tr><td> -->
			<div class="familyDescTable">
				<table cellpadding="5" summary="family table organization" width="100%">
					<tr>
						<td colspan="2">
							<span class='familyDescTableName'>$name Family<br /></span>
<span class='familyDescTableParam'>$fullName</span><br />
							<span class='familyDescTableParam'>Homepage:</span> <span class='familyDescTableValue'>$homepage $wiki</span><br />
							<span class='familyDescTableParam'>Family description:</span> <span class='familyDescTableValue'>$desc</span>
						</td>
					</tr>
					<tr>
						<td valign="top" width="50%">
						 <table style="border-collapse: collapse;">
							<tr>
								<td class='familyDescTableParam'>Designer:</td> 
								<td class='familyDescTableValue'>$manufSub</td>
							</tr>	
							<tr>
								<td class='familyDescTableParam'>Initial release:</td>
								<td class='familyDescTableValue'>$dateSub</td>
							</tr>
							<tr>
								<td class='familyDescTableParam'>Datasheet:</td> 
								<td class='familyDescTableValue'>$datasheet</td>
							</tr>
						</table>
						</td>
						<td valign="top" width="50%">
						<table style="border-collapse: collapse;">
							<tr>
								<td class='familyDescTableParam'>Architecture:</td>
								<td class='familyDescTableValue'>$arch</td>
							</tr><tr>
								<td class='familyDescTableParam'>Frequencies:</td>
								<td class='familyDescTableValue'>$freq</td>
							</tr><tr>
								<td class='familyDescTableParam'>Technology:</td>
								<td class='familyDescTableValue'>$tech</td>
							</tr>
						 </table>
						</td>
					</tr>
				</table>
				</div>
			<!--  </td></tr></table> -->
			<!-- ---Family summary (end) -->
			</td>
		</tr>
	</table>
Endhtml

}


sub displayTableSupportChips {
	my @supportChips = @_;

	print "\t<br /><br />\n";
	print "\t<table border=\"1\" summary=\"list of support chips for family\">\n";
	print "\t\t<tr><td><b>Support chips</b></td></tr>\n";
	foreach $chip (@supportChips) {
		print "\t\t<tr><td>$chip</td></tr>\n";
	}
	print "\t</table>\n";
}
####################################################
############# Table (end) ##########################
####################################################


sub sidebar2 {
	my $x=0;
	print <<Endhtml;
	
	<!-- sidebar -->
	<div>
		<form action="$scriptName">
		<input type="hidden" name="page" value="family" \>
		<select name="family" style="width:125px;">
		<option value="null" selected>--family--</option>
Endhtml
	foreach $fam (@familyListGlobal) {
		print "\t\t\t<option value=\"$fam\">$fam</option>\n";
	}
	print <<Endhtml;
		</select>
		<input type="submit" value="Go" style="width:30px; align:left;" />
		</form>
		<hr>
Endhtml
	foreach $manuf (sort keys %manufListCountGlobal) {
		if ($manuf eq $data{'manufacturer'}) {
			print "\t\t<a href=\"$scriptName?page=manufacturer&manufacturer=$manuf\"><span class=\"sidebarManufNameSelected\">$manuf</span></a>&nbsp;<span class=\"sidebarManufCountSelected\">($manufListCountGlobal{$manuf})</span><br />\n";
			print "\t<ul>\n";
			# &manufFamilyList; #no longer used
			

			for ($x=0;$x<=$#manufFamilyListGlobal;$x++) {
				print "\t\t<li><a href=\"$scriptName?page=manufacturer&manufacturer=$manuf&family=$manufFamilyListGlobal[$x]\"><span class=\"manufFamilyListSelected\">$manufFamilyListGlobal[$x]</a>";
				if ($manufFamilyListGlobal[$x] eq "All") {
					print "</span></li>\n";
				} else {
					print "&nbsp;($manufFamilyListCountGlobal[$x])</span></li>\n"; 
				}
			}
	




			print "\t</ul>\t\n<hr />\n";
		}else{
			$unselectedCompanies= $unselectedCompanies .  "\t<a href=\"$scriptName?page=manufacturer&manufacturer=$manuf\"><span class=\"sidebarManufName\">$manuf</span></a>&nbsp;<span class=\"sidebarManufCount\">($manufListCountGlobal{$manuf})</span><br />\n";
		}
	}


	print $unselectedCompanies;
	print "\t</div>\n";
	print "\t<!-- sidebar (end) -->\n\n\n";
}



##### introductions #####

	
sub intro {
	my $type=$data{'page'};
	my $manuf=$data{'manufacturer'};
	my $family=$data{'family'};
	my $logo='', $desc='';
	#print "$type, $manuf, $family";

	print "\t<!-- intro -->\n";
	if ( $type eq '' ) { #initial page
		print <<Endhtml;
<div class="introTextTitle">This is my CPU collection.</div><br />
<div class="introText">I currently have <b>$totalChipCountGlobal</b> chips in my collection, spanning from $totalManufCountGlobal different companies, and $totalFamilyCountGlobal CPU families.  They are mostly processors (CPU) and <a href="http://en.wikipedia.org/wiki/Microcontroller">microcontrolers</a> (MCU).  I have some rare ones in my collections and some you may not see anywhere else.  I have a special affinity for the <a href="$scriptName?page=family&family=AM2900">AMD AM2900 family</a>.  A collection of chips to form <a href="http://en.wikipedia.org/wiki/Bit_slicing">Bitslice</a> processors.</div>
	<div style="align:center;">
	<table>
		<tr>
			<td>
				<img src="$relativeLoc/$logoLoc/ic_manuf_logo--AMD-Advanced_Micro_Dev.gif" alt="Advanced Micro Devices" height="63">
			</td>
			<td>
				<a href="?page=manufacturer&manufacturer=AMD">AMD</a>
			</td>
		</tr>
		<tr>
			<td>
				<img src="$relativeLoc/$logoLoc/ic_manuf_logo--Intel_Corp-2.gif" alt="Intel" height="63">
			</td>
			<td>
				<a href="?page=manufacturer&manufacturer=Intel">Intel</a>
			</td>
		</tr>
		<tr>
			<td>
				<img src="$relativeLoc/$logoLoc/ic_manuf_logo--Motorola_Semi_Products_inc.gif" alt="Motorola" height="63">
			</td>
			<td>
				<a href="?page=manufacturer&manufacturer=Motorola">Motorola</a>
			</td>
		</tr>
		<tr>
			<td>
				<img src="$relativeLoc/$logoLoc/ic_manuf_logo--Zilog_Semi-1.gif" alt="Zilog" height="63">
			</td>
			<td>
				<a href="?page=manufacturer&manufacturer=Zilog">Zilog</a>
			</td>
		</tr>
	</table>
	</div>
				
Endhtml
	} elsif ( $type eq 'manufacturer' ) { #Manufacturer pages
		print "\t<h2>$manuf</h2>\n";
		if ( $data{'family'} eq '' ) {
			&displayTableManuf2($manuf);
		} elsif ( $data{'family'} eq "All" ) {
			print "All families<br />\n";
		} elsif ( $data{'family'} eq "Other" ) {
			print "Other families<br />\n";
		} else {
			&displayTableFamily;
		}
		print "\t<br />\n";
	} elsif ( $type eq 'trade' ) {
		print "Trade desc\n";
	}
	print "\t<!-- intro (end) -->\n";
}






##########################################################
############### List methods #############################
##########################################################

sub manuList {
	my $x=$skipLines; #
	my $flipColor=0;
	my $supportChipCount=0;
	my @supportChips=();
	my @freq=();
	my @chip_text=();
	my @chip_text2=();
	my @chip_text3=();
	my @A=(), @B=();
	my $listed_cnt=0;

	print "\n\t<!-- manuf list -->\n\t<table cellpadding=\"5\" border=\"0\" bgcolor=\"#333333\" summary=\"manufacturer list\">\n"; 
	while(&chipData2($x,$colManuf) ne "") {
		# print ". " . &chipData2($x,$colManuf);
		if(&chipData2($x,$colManuf) =~ /^$data{'manufacturer'}$/i) {
			# print ', Family:: ' . &chipData2($x,$colFamily);
			if ( (&chipData2($x,$colFamily) eq $data{'family'} || $data{'family'}  eq "All" || (($data{'family'}  eq "Other") && (&chipData2($x,$colFamily) eq ''))) && ($data{'family'} ne "") ) {
				# print ".";
				if ( &chipData2($x,$colType) =~ /^Support$/i ) {
					$supportChips[$supportChipCount] = &chipData2($x,$colPart);
					# need to fix to the send full ship data
					$supportChipCount++;
				} else {
					# &displayTableData($x,$flipColor);
					# ($chip_text[1][$listed_cnt], $chip_text[0][$listed_cnt]) = &displayTableData2($x,$flipColor);
					($A[$listed_cnt], $B[$listed_cnt], $type[$listed_cnt]) = &displayTableData2($x,$flipColor);
					$listed_cnt++;
					# print $text;
					if ( $flipColor == 0 ) {$flipColor = 1;} else {$flipColor = 0;}
				}
			}
		}
		$x++;
	}
	# @chip_text3 = @chip_text;
	# @chip_text2 = bbl_sort(@chip_text3);
	# foreach $text (@freq) { print "$text, "; }

	# my @array =  @chip_text;
	# print_2d(@array);
	my $row_sort = 0;
	# my $array2 =  @{$_[1]};
    my $not_complete = 1;
    my $index;
    my $len = ($#B - 1);
	# print "lenght=$len<br />";
    # print "|sub=" . scalar @array . " -- " . scalar @array2 . "|";
	# print $A[0];
	my $temp = '';
	my $temp2 = '';
	while ($not_complete) {
		$not_complete = 0;
		foreach $index (0 .. $len) {
			if ($B[$index] > $B[$index + 1]) {
				$temp = $B[$index + 1];
				$temp2 = $A[$index + 1];
				$temp3 = $type[$index + 1];
				$B[$index + 1] = $B[$index];
				$A[$index + 1] = $A[$index];
				$type[$index + 1] = $type[$index]; 
				$B[$index] = $temp;
				$A[$index] = $temp2;
				$type[$index] = $temp3;
				$not_complete = 1;
			}
		}
	}
	# print "<tr><td>asdsadasd</td></tr>";
	# print $A[0];
	# printing the chip tables
	$x=0;
	# while ( $chip_text[1][$x] ne '' ) {
		# print "poop $x :: $chip_text[0][$x] <br />" ;
		# print $chip_text2[1][$x] ;
		# print "poop $x :: $array[0][$x] <br />" ;
		# print $array[1][$x] ;
		# $x++;
	# }
	my $cpus = '';
	my $mcus = '';
	my $fpus = '';
	my $others = '';
	while ( $B[$x] ne '' ) {
		# print "poop $x :: $B[$x] <br />" ;
		if ( $type[$x] =~ /^cpu$/i ) {
			$cpus .= $A[$x];
		} elsif ( $type[$x] =~ /^mcu$/i ) {
			$mcus .= $A[$x];
		} elsif ( $type[$x] =~ /^fpu$/i ) {
			$fpus .= $A[$x];
		} else {
			$others .= $A[$x];
		}
		# print "$A[$x]";
		# print "<tr><td>doggy</td><tr>\n\n\n\n" ;
		$x++;
	}
	if ( $cpus ne '' ) {
		print "<tr><td colspan=\"2\" bgcolor=\"#545454\"><div class=\"chipTypeHeading\">&nbsp;&nbsp;Central Processing Units (CPU)</div></td><tr>\n";
		print $cpus;
	}
	if ( $mcus ne '' ) {
		print "<tr><td colspan=\"2\" bgcolor=\"#545454\"><div class=\"chipTypeHeading\">&nbsp;&nbsp;Micro-Controller Units (MCU)</div></td><tr>\n";
		print $mcus;
	}
	if ( $fpus ne '' ) {
		print "<tr><td colspan=\"2\" bgcolor=\"#545454\"><div class=\"chipTypeHeading\">&nbsp;&nbsp;Floating Point Units (FPU)</div></td><tr>\n";
		print $fpus;
	}
	if ( $others ne '' ) {
		print "<tr><td colspan=\"2\" bgcolor=\"#545454\"><div class=\"chipTypeHeading\">&nbsp;&nbsp;Other components</div></td><tr>\n";
		print $others;
	}

	print "\t</table>\n";
	if ( $supportChipCount > 0 ) {
		&displayTableSupportChips(@supportChips);
	}
	print "\t<!-- manuf list (end) -->\n";
}




sub tradeList { 
	my $x=$skipLines;


	print "<h1>Trade List</h1>\n\n";
	&contactMe;
	print "\n\n<table cellspacing=\"5\" border=\"0\" summary=\"trade list table\">\n";
	print "<table border=\"1\"><tr>\n<td width=\"20\"><b>Quantity</b></td>\n";
	print "<td><b>Manufacturer</b></td>\n";
	print "<td><b>Family</b></td>\n";
	print "<td><b>Part #</b></td>\n";
	print "<td><b>Photo</b></td></tr>\n";

	while(&chipData2($x,$colPart) ne "") {
		print "<tr>\n";
		my $trade=&chipData2($x,$colTrade);
		my $manuf=&chipData2($x,$colManuf);
		my $family=&chipData2($x,$colFamily);
		my $part=&chipData2($x,$colPart);
		my $photo=&chipData2($x,$colPhoto1);
		if(&chipData2($x,$colTrade) ne "X") {
			print "<td>$trade</td>\n";
			print "<td>$manuf</td>\n";
			print "<td>$family</td>\n";
			print "<td>$part</td>\n";
			print "<td><a href=\"../$photoLoc/$photo\">Photo</a></td>\n";
		}
		print "</tr>\n";
		$x++;
	}
	print "</table>\n";
}


sub familyList { # problem with 1802 and 1806, maybe skipping first entry
	my $x=$skipLines; # 
	print "<h1>Family List</h1>\n\n";
	&displayTableFamily;
	print "\n<table cellspacing=\"5\" border=\"0\" bgcolor=\"#333333\" summary=\"family list\">\n";
	while(&chipData2($x,$colPart) ne "") {
		if(&chipData2($x,$colFamily) =~ /$data{'family'}/i) {
			&displayTableData($x);
		}
		$x++;
	}
	print "</table>\n";
}


sub fullList {
	my $x=$skipLines; # fix me
	print "<h1>Full chip list</h1>\n\n<table cellspacing=\"5\" border=\"0\" summary=\"full list\">\n";
	print "<table border=\"1\" summary=\"full chip list, organized\"><tr>\n";
	print "<td><b>Manufacturer</b></td>\n";
	print "<td><b>Family</b></td>\n";
	print "<td><b>Part #</b></td>\n";
	print "<td><b>Photo</b></td></tr>\n";

	while(&chipData2($x,$colPart) ne ""){
		print "<tr>\n";
		print "<td>" . &chipData2($x,$colManuf) . "</td>\n";
		print "<td>" . &chipData2($x,$colFamily) . "</td>\n";
		print "<td>" . &chipData2($x,$colPart) . "</td>\n";
		print "<td><a href=\"$photoLoc/" . &chipData2($x,$colPhoto1) . "\">Photo</a></td>\n";
		print "</tr>\n";
		$x++;
		}
	print "</table>\n";
}

sub contactMe {
	print <<Endhtml
		<table border="1">
			<tr><td align="center">
				Contact Me<br />
				<a href="http://howto.wikia.com/wiki/Spam">SPAM</a> is such a miserable thing. Hopefully this will avoid some. At least until the spammers figure out howto circumvent this method<br />
				<img src="$relativeLoc/$imageLoc/sneezmale.jpg" width="100"><br />
			</td></tr>
		</table>
Endhtml
}



sub singleChipPrint {
	# $data{'page'} eq chip
	# ?page=chip&manufacturer=Intel&chipName=A80486SX-33
	my $flipColor=0;
	my $x=2;
	print "yippy<br />\n";
	if( $data{'chipName'} eq '' && $data{'manufacturer'} eq '' ) {
		print "No chip selected, invalid state<br />\n";
	} else {
		while(&chipData2($x,$colPart) ne ""){
			# print ".";
			if ( &chipData2($x,$colPart) eq $data{'chipName'} && &chipData2($x,$colManuf) eq $data{'manufacturer'} ) {
				# print "-";
				&displayTableData($x,$flipColor);
			}
			$x++;
		}
	}
}
#########################################################
################# List methods (end) ####################
#########################################################









#########################################################
################# Page Organization #####################
#########################################################

sub mainPage {
	&pageHead;
	&pagebody;
	&pageTail;
}
############# Page organization (end) #######################

############## head #############################
sub pageHead {
	# http://www.w3schools.com/css/css_font.asp
	my $title = "CPU Grave Yard";
   	if ($data{'page'} eq '') {
		$title = $title . " - Chip Collection";
	} elsif ($data{'page'} eq 'manufacturer') {
		$title = $title . " - $data{'manufacturer'} processors and microcontrollers";
		$meta = "$data{'manufacturer'}, ";
	}
	if ($data{'family'} ne '') {
		$title = $title . " - $data{'family'} Chip Family";
		$meta = $meta . "$data{'family'}, ";
	}
print <<Endhtml;
<html lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="description" content="My CPU collection">
<meta name="keywords" content="CPU, processor, MCU, microcontroller, collection, Intel, AMD, Motorola, $meta">
<title>$title</title>

<STYLE TYPE="text/css">
<!--
	a 		{ color: #dddddd; }
	a:hover { color: #ffffff; }

	div { font-size:12px; }
	span { font-size:12px; }

	.chipTypeHeading { font-size:25px; font-weight:bold; }

	.tableChipParam 		{ font-weight:bold; }
	.tableDisplayManufType 	{ font-size:30px; }
	.tableDisplayFamily 	{ font-size:25px; }
	.tableDisplayPartNum 	{ font-size:25px; }

	.sidebarManufName 	{ font-size:12px; }
	.sidebarManufCount  { font-size:12px; }
	.sidebarManufCountSelected  { font-size:12px; }
	.sidebarManufNameSelected 	{ font-size:14px; font-weight:bold; }
	.manufFamilyListSelected 	{ font-size:12px; font-weight:bold; }
	.topChipCount { font-size:10px; }

	.tableParam { font-size:12px; font-weight:bold; }
	.tableValue { font-size:12px; }
	.tableChipParam { font-size:12px; font-weight:bold; }
	.tableChipValue { font-size:12px; }
	.tableChipSubHeading { font-size:12px; font-weight:bold; text-decoration: underline; }

	.tableTitle { font-size:30px; }
	
	.familyTable { border: 1px solid white; border-collapse: collapse; }
	.familyDescTable { width: 400px; border: 1px solid #656565; font-size:12px; }
	.familyDescTableName { font-size:16px; font-weight:bold; }
	.familyDescTableParam { font-size:12px; font-weight:bold; }
	.familyDescTableValue { font-size:12px; }

	.title { font-size:36px; font-weight:bold; text-align:center; }
	.introTextTitle { font-size:28px; font-weight:bold; }
	.introText { font-size:18px; }
	.tail { text-align:center; font-size:12px; }
-->
</STYLE>

</head>
<body bgcolor="#444444" link="#dddddd" alink="#FFOOFF" vlink="#aaaaaa" text="#cccccc">

<div style="float:right;" class="topChipCount">
$totalChipCountGlobal Chips<br />
updated<br />
$updateDate
</div><br />

<div class="title">
CPU Grave Yard
</div><br />

<hr>
	<center>
	<a href="$scriptName">Home</a> -- 
	<a href="$scriptName?page=fullList">Full Chip List</a> -- 
	<a href="$scriptName?page=tradeList">Trade List</a> --
	<a href="$scriptName?page=sneezmale">Contact Me</a>
	</center>
<hr>
Endhtml
}
########## head (end) #####################

######## body ##########
sub pagebody {
	my $type = $_[0];
	my $display = $_[1];
	
	print <<Endhtml;
<!-- pagebody -->
<table id="body-id" summary="table body, aligning sidebar and maincontent areas">
 <tr>
  <td valign="top" width="190">
Endhtml
				&sidebar2;
print "  </td>\n  <td valign=\"top\" width=\"650\" bgcolor=\"#333333\" cellpadding=\"0\">\n";
				&maincontent;
				print <<Endhtml;
  </td>
 </tr>
</table>
<!-- pagebody (end) -->
Endhtml
}
###### body (end) #######



sub maincontent {
	print "<!-- main content -->\n";
	# company profile

	print "\t <table summary=\"main content\">\n\t  <tr>\n\t  <td>\n";
	&intro;
	print "\t  </td>\n\t </tr>\n";
	
	print "\t <tr>\n\t  <td>\n";
	switch ($data{'page'}) {
		case "fullList"		{ &fullList; 	}
		case "tradeList"	{ &tradeList; }
		case "family"		{ &familyList; }
		case "manufacturer"	{ &manuList; }
		case "chip"			{ &singleChipPrint; }
		case "sneezmale"	{ &contactMe; }
		case ""			{}	#{ &defaultContent; }
		else				{ print "Script Error: page type unknown"; }
	}
	# chip lists
	print "\t  </td>\n\t  </tr>\n </table>\n";

	print "<!-- main content (end) -->\n";	
}


######### tail ############################
sub pageTail {
print <<Endhtml;
<br />
<hr>
<div class="tail">
	This page was created by Jeff Israel<br />
	Images are (CopyLeft) under the <a href="http://en.wikipedia.org/wiki/GNU_Free_Documentation_License">GFDL</a> licence, unless noted. The code to creat this page is licenced under <a href="http://en.wikipedia.org/wiki/GPL_license">GPL</a><br />
	<a href="$scriptName?page=sneezmale">Contact Me</a></div>

</body>
</html>
Endhtml
}
######## tail (end) ######################

#########################################
########## HTML (end) ###################
#########################################


























##################################################
########## Main code #############################
##################################################

#===Global Vars===#
# List of Manuf		=	@manuf						(string)
# List of families	=	@family						(string)
# chip info			=	&chipData2(chipNum,colNum)	(string)
#
# input manuf		=	$data{'manufacturer'}			(string)
#  manuf ID #		=	$selectedManufIDglobal						(int)
# input family		=	$data{'family'}					(string)
# 	family ID #		=	
# input page type	=	$data{'page'}					(string
#
# manuf families 	=	@manufFamUnique					(string)
# manuf chip cnt	=	$manufCount[$selectedManufIDglobal]			(int)
# manuf fam chip cnt=	$familyCount[$selectedManufIDglobal][family]	(int)


#===Page organization===
#
# Main page
#	&pageHead
#	&pagebody
#	&pageTail
#
#	pageBody ->
#		&sidebar
#		&maincontent
#
#	sidebar ->
#		&manufFamilyList
#
# 	maincontent ->
#  		&intro
#  		options->
#			&fullList
#			&tradeList
#			&familyList
#			&manuList
#
#	intro ->
#		&displayTableManuf2
#		&displayTableFamily
#
#	manufList ->
#		foreach(chip)
#			 &displayTableData
# 
#

#Initialize
%data = ();
$updateDate = '';
@updateDateA = ();

# loading
%data = &read_input; #get input


# time stamp
$updateDate = ctime(stat($spreadsheetFileName)->mtime);
@updateDateA = split(/ /,$updateDate);
$updateDate = $updateDateA[1] . " " . $updateDateA[2] . ", " . $updateDateA[4]; 

print "Content-type:text/html\n\n"; # starting right


&check_script_location;
# foreach (keys %ENV){
	# print "$_ -> $ENV{$_}<br>";
# }


@spreadsheetData = &parse_ods($spreadsheetFileName);

# print $spreadsheetData[0][0][0];

# print "}}}}}} loading spread" . times() . "\n";
# &loadSpreadsheets;
# print "}}}}}} get collables col chipdata" . times() . "\n";
# print "}}}}}} open chipData" . times() . "\n";
# @filelines = ();
# @filelines = &openFile($fullFileName); # read file
# @chipData = &parseList(@filelines); # extract chip data
# print "}}}}}} open manuf" . times() . "\n";
# @filelines = ();
# @filelines = &openFile($manufFileName); # read file
# @manufData = &parseList(@filelines); # extract chip data
# print "}}}}}} open fam" . times() . "\n";
# @filelines = ();
# @filelines = &openFile($familyFileName); # read file
# @familyData = &parseList(@filelines); # extract chip data
# print "}}}}}} open fam list" . times() . "\n";
# @filelines = ();
# @filelines = &openFile($familyListFileName); # read file
# @familyList = &parseList(@filelines); # extract chip data

# print "}}}}}} open eras" . times() . "\n";
@chipErasGlobal = &openFile($familyErasOrderedFileName);
chomp(@chipErasGlobal);
# foreach $chipEra (@chipErasGlobal) {print "$chipEra, ";}

&getColLabels;
&getColChipData;


# print "}}}}}} grabGlobal" . times() . "\n";
&grapGlobals;

# print "}}}}}} get group" . times() . "\n";
# print "}}}}}} manuf info" . times() . "\n";
# print "}}}}}} manuf info (done)" . times() . "\n";
# if ( $data{'page'} eq "chip" ) { &chipSelectedDetails; }

# print "}}}}}} main shit " . times() . "\n";
&mainPage; # display page

# print "}}}}}} end " . times() . "\n";
######## Main code (end) ###############
print "\n";
