

// MCU Product Database - Parametric Search
// Version 0.9 July 2006

// More Global variables
// matrix to remember selected attributes and value; reservere additional 200 elements for new selection fields (pincount) and additonal romsize entries
var attribute_sel = new Array(nbr_attribute_matrix+200);
// values to keep result (count, list)
var result_count=0;
var result_list = new Array;
// stores position of BITS attribute in table during form setup
var bits_pos_in_table=0;
// special romtype, used for setup of selection fields and during selection process
var romtype =           new Array("FLASH",       	    "OTP",      		"COMPACTFLASH",       "DUALOPFLASH",        "SUBFLASH",           "MASK"            );
var romtype_attribute = new Array("ZPDB_ROMSIZE",	    "ZPDB_OTP",			"ZPDB_COMPACTFLASH",  "ZPDB_DUALOPFLASH",   "ZPDB_SUBFLASH",      "ZPDB_ROM"        );
var romtype_security  = new Array("ZPDB_FLASHSECURITY", "ZPDB_ROMSECURITY", "ZPDB_FLASHSECURITY", "ZPDB_FLASHSECURITY", "ZPDB_FLASHSECURITY", "ZPDB_ROMSECURITY");
var romtype_position  = new Array(0,             	    0,         			0,                    0,                    0,                    0                 );
// points to the product directory
var Document_Location="../mcu_product/detail/";
var Web_Root="..";


function ResetCheck () {
  var chk = window.confirm("Do you really want to clear the form?");
  if (chk) { for (var i=0; i<=nbr_attribute_matrix-1; i++) {attribute_sel[i]=0;}}
  return (chk);
}

function getURLParam(strParamName){
// returns the paramter from the URL
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( 
        aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
} 


function splitAttributeLine(attribute_line) {
	// split an attribute record with '|' into the fields
	// adds <= and/or >= for numeric fields into option text and value
	// adds a =0 selection for CAN
	var attribute_line;
	var texts = attribute_line.split("|",6);
	this.attribute = texts[0];
	this.uom = texts[1];
	this.posintable = texts[2];
	this.alphanum = texts[3];
	// all numeric fields are treated as min (add >= for eval-function for specific fields)
	if (this.alphanum=="N") {
		if (this.attribute=="ZPDB_ADCCONVTIME" || this.attribute=="ZPDB_PINCOUNT_TO")
			{this.optiontxt = "<="+texts[4];this.optionval = "<="+texts[5];}
		else if (this.attribute=="ZPDB_VCCMIN") 
      {this.optiontxt = "  "+texts[4];this.optionval = "<="+texts[5];}
		else if (this.attribute=="ZPDB_VCCMAX") 
      {this.optiontxt = "  "+texts[4];this.optionval = ">="+texts[5];}
		else if (this.attribute=="ZPDB_CAN" && texts[5]=="0") 
      {this.optiontxt = "="+texts[4];this.optionval = "=="+texts[5];}
    else if ( this.attribute=="ZPDB_ROMSIZE" && texts[5]=="0.00") 
      {this.optiontxt = "="+texts[4];this.optionval = "=="+texts[5];}
    else 
      {this.optiontxt = ">="+texts[4];this.optionval = ">="+texts[5];}
    }
	else 
		{this.optiontxt = texts[4];this.optionval = texts[5];} 
}

function Numsort (a, b) {
// sort function for numeric sort, returns true or false if a!=b ; assumes numeric values for type conversion
  a1=parseFloat(a[0]); b1=parseFloat(b[0]);
  return a1 - b1;
}

function setOptions() {
// sets all select options for dropdown boxes dynamically based on values in attribute_matrix
// first split attribute_line and set optiontext and optionvalue by ElementId of selection box
// sets initially optiontext=All and value ="" 
// keeps position of bits in table; stores distinct romsizes; keeps all pincounts for additional field not provided in attribute table
// sets pos of attribute in _sel table to zero to indicate 'not selected'
var attribute_line;
var attribute_line_h;
var attribute_prev="";
var selbox;
// special fields for ROMTYPE & -SIZE, selection fields, attributes and value list
var romsizes_option = new Array();
var position;
var already_there = Boolean;
var romsizes_not_numeric = false;
var initial_attribute_matrix_len = nbr_attribute_matrix;

// First get paramter from URL
var Document_Location=getURLParam("DocLoc");

// get romsize positions
for (var i=0; i<=nbr_attribute_matrix-1; i++) {
	attribute_line = new splitAttributeLine(attribute_matrix[i]);
	// add all romsizes for all romtypes
	for (var j=0; j<6; j++) {
		if (attribute_line.attribute==romtype_attribute[j]) {
			romtype_position[j] = attribute_line.posintable;
		}
	}
}	
// repeat all romsizes for all romtypes
for (var i=0; i<=nbr_attribute_matrix-1 && i<initial_attribute_matrix_len-1; i++) {
	attribute_line = new splitAttributeLine(attribute_matrix[i]);
	var fit = 0;
	for (var j=0; j<6; j++) {
		if (attribute_line.attribute==romtype_attribute[j]) {
			fit = 1
		}
	}
	for (var j=0;fit==1 && j<6; j++) {	
		var texts = attribute_matrix[i].split("|",6);		
		attribute_matrix[nbr_attribute_matrix]=romtype_attribute[j] + "|" + texts[1] + "|" + romtype_position[j] + "|" + texts[3] + "|" + texts[4] + "|" + texts[5];
		nbr_attribute_matrix++;	
	}
}

// ***************************************************************************************************************************
// for attributes VCCMIN and VCCMAX the original product values need to be replaced
// with calculated values betwwen the min and max values of both attributes (stepwidth = 0.10 V)
// trailing zeroes are suppressed as a side effect
// 1. determine min and max value for VCCMIN and VCCMAX
var vccmin_value = 0.00;
var vccmin_row   = "";
var vccmax_value = 0.00;
var vccmax_row   = "";
for (var i=0; i<=nbr_attribute_matrix-1; i++) {
  attribute_line = new splitAttributeLine(attribute_matrix[i]);
  if ( attribute_line.attribute=="ZPDB_VCCMIN" || attribute_line.attribute=="ZPDB_VCCMAX" )
  {
  	var texts = attribute_matrix[i].split("|",6);
    // get min value  		
    if ( (texts[5] > 0 && texts[5] < vccmin_value) || vccmin_value == 0) 
    {
      vccmin_value = texts[5];
      vccmin_row   = attribute_matrix[i];
    }  
    // get max value  		
    if (texts[5] > vccmax_value) 
    {
      vccmax_value = texts[5];
      vccmax_row   = attribute_matrix[i];
    }
    // avoid the old entries from being displayed
    attribute_matrix[i]="ZPDB_IGNORE";
  }  
}
//alert("Min value = " + vccmin_value + "; max value = " + vccmax_value);
//alert("MinRow = " + vccmin_row);
//alert("MaxRow = " + vccmax_row);

// 2. generate the new entries between min and max values
for (var i = parseFloat(vccmin_value); i < parseFloat(vccmax_value); i = i + parseFloat("0.1")) {
  // for VCCMIN selection 
//	attribute_matrix[nbr_attribute_matrix]=attribute_line_h.replace(vccmin_value,(parseInt((i + 0.005) * 100)/100).toFixed(2));
//	attribute_matrix[nbr_attribute_matrix]=attribute_matrix[nbr_attribute_matrix].replace(vccmin_value,(parseInt((i + 0.005) * 100)/100).toFixed(2));
	attribute_line_h=vccmin_row;
	attribute_matrix[nbr_attribute_matrix]=attribute_line_h.replace(vccmin_value,i.toFixed(2));
	attribute_matrix[nbr_attribute_matrix]=attribute_matrix[nbr_attribute_matrix].replace(vccmin_value,i.toFixed(2));
	nbr_attribute_matrix++;	
}
for (var i = parseFloat(vccmin_value); i < parseFloat(vccmax_value); i = i + parseFloat("0.1")) {
  // for VCCMAX selection 
//	attribute_matrix[nbr_attribute_matrix]=attribute_line_h.replace(vccmax_value,(parseInt((i + 0.005) * 100)/100).toFixed(2));
//	attribute_matrix[nbr_attribute_matrix]=attribute_matrix[nbr_attribute_matrix].replace(vccmax_value,(parseInt((i + 0.005) * 100)/100).toFixed(2));
	attribute_line_h=vccmax_row;
	attribute_matrix[nbr_attribute_matrix]=attribute_line_h.replace(vccmax_value,i.toFixed(2));
	attribute_matrix[nbr_attribute_matrix]=attribute_matrix[nbr_attribute_matrix].replace(vccmax_value,i.toFixed(2));
	nbr_attribute_matrix++;	
}
// ***************************************************************************************************************************


for (var i=0; i<=nbr_attribute_matrix-1; i++) {
	attribute_line = new splitAttributeLine(attribute_matrix[i]);
	
	if (attribute_line.attribute=="ZPDB_BITS") { bits_pos_in_table = attribute_line.posintable ;};
	if (attribute_line.attribute=="ZPDB_PINCOUNT") {
		// add attribute line for Pincount-To (extend matrix and add same text with -TO attribute name)
		attribute_line_h=attribute_matrix[i];
		attribute_matrix[nbr_attribute_matrix]=attribute_line_h.replace("ZPDB_PINCOUNT","ZPDB_PINCOUNT_TO");
		nbr_attribute_matrix++;	
	}
	var add_all_entry_for_vcc = true;
	selbox=document.getElementById(attribute_line.attribute);
	if (selbox != null && selbox != undefined && attribute_line.attribute!="ZPDB_ROMSIZE") {
		// if a field name exists with the given attribute name
		if (selbox.type == "select-one") {
		// only for select boxes
			if (attribute_line.attribute != attribute_prev) {
			// initalize selectbox options for every new attribute
			  if ((attribute_line.attribute != "ZPDB_VCCMIN" && attribute_line.attribute!="ZPDB_VCCMAX") || add_all_entry_for_vcc) {
          selbox.options.length = 0;
  			  selbox.options[selbox.options.length] = new Option("All","*");
  			  attribute_prev = attribute_line.attribute;
  			}
      }
			// set selectbox optiontext and value
			if (attribute_line.optiontxt != ">=0" ) {
		      selbox.options[selbox.options.length] = new Option(attribute_line.optiontxt,attribute_line.optionval);
		  }
		} }
	else {
		// check if a ROMTYPE field and store value for romsize selection list
		for (var j=0; j<6; j++) { 
			if (attribute_line.attribute==romtype_attribute[j]) {
			  // check for duplicates
			  already_there=false;
			  for (var l=0; l < romsizes_option.length; l++) { 
			  	if (attribute_line.optiontxt==romsizes_option[l][1]) 
			  	{ already_there=true; break; }
			  }
			  if (!already_there) {
			  // store new combination
			  	 position=romsizes_option.length;
			 	 romsizes_option[position] = new Array (3); 
			 	 // numeric romsize needed, get number from 3rd pos, replace comma and check if numeric for sort lateron
			 	 var x = attribute_line.optionval.substr(2) ;
			 	 var a = x.replace(",",""); 
			 	 if (!romsizes_not_numeric) {romsizes_not_numeric=isNaN(a)};
 			 	 romsizes_option[position][0]= a;
			 	 romsizes_option[position][1]=attribute_line.optiontxt ;
			 	 romsizes_option[position][2]=attribute_line.optionval ; } }
			   }
	    }
    attribute_sel[i]=0;
}
// now set romtype fields
selbox=document.getElementById("ROMTYPE"); selbox.options.lenth=0;
selbox.options[selbox.options.length] = new Option("All","*");
for (var k=0; k<6; k++) { selbox.options[selbox.options.length] = new Option(romtype[k],romtype_attribute[k]); };
// now set romsize fields, sort depends if all fields are numeric
selbox=document.getElementById("ZPDB_ROMSIZE"); selbox.options.lenth=0;
selbox.options[selbox.options.length] = new Option("All","*");
if (romsizes_not_numeric) { romsizes_option.sort(); }
else romsizes_option.sort(Numsort);
for (var i=0; i < romsizes_option.length; i++) { 
    if (romsizes_option[i][0]!="0.00" || romsizes_option[i][2]=="==0.00") {
      selbox.options[selbox.options.length] = new Option(romsizes_option[i][1],romsizes_option[i][2]);
    }
}
}

function markAttributeSelection(name, value) {
	var selbox;
	var new_security = "";
	// input paramter is field name and selected value
	// marks row in attribute table based on field selection (value 1 is attribute is selected)
	
 	// special case for romtype, switch to different attribute and get value from romsize field
  	if (name=="ROMTYPE" ) {	   
		// re-init	  	
	    for (var j=0; j<6; j++) {	
            for (var i=0; i<=nbr_attribute_matrix-1; i++) {
	            var attribute_line = new splitAttributeLine(attribute_matrix[i]);
	            if (attribute_line.attribute==romtype_attribute[j]) {
	                attribute_sel[i]=0;
           	    }
 	        }
 	    }
	  	
 	   for (var i=0; i<6; i++) { 
 	   // loop for all romtypes/attributes and mark selection
 	    if (romtype_attribute[i]==value) {
 	    	name=romtype_attribute[i];
 	   		selbox=document.getElementById("ZPDB_ROMSIZE");
 	   		value=selbox.value ;
			new_security = romtype_security[i];;
 	   		break; } 
 	   }
 	   // reset security selection
  	   if (new_security=="") {
 		  document.getElementById("SECURITY").checked = false;
 		  document.getElementById("ZPDB_FLASHSECURITY").checked = false;
 		  document.getElementById("ZPDB_ROMSECURITY").checked = false;
 		  markAttributeSelection("ZPDB_FLASHSECURITY", false);
 		  markAttributeSelection("ZPDB_ROMSECURITY", false);
  	   } 
 	}

 	if (name=="ZPDB_ROMSIZE" ) {	
 	// get romtype selection and change attribute name
 	   	selbox=document.getElementById("ROMTYPE");
		for (var i=0; i<6; i++) { 
 	      if (romtype_attribute[i]==selbox.value) { name=romtype_attribute[i]; 
 	      break; }
 	   	}
 	}
 	
	for (var i=0; i<=nbr_attribute_matrix-1; i++) {
		var attribute_line = new splitAttributeLine(attribute_matrix[i]);
		if (attribute_line.attribute==name)  {
		// in all other cases the fieldname is attribute name 
			selbox=document.getElementById(name);
			if (selbox != null && selbox != undefined && selbox.type == "checkbox") {
				// if checkbox selected (value is true) set matrix is -1 to indicate a checkbox!
				if (value) { attribute_sel[i]=-1; }
				else {attribute_sel[i]=0;} }
			else {
			// just compare values for selection boxes 
				if (attribute_line.optionval==value ) {attribute_sel[i]=1; }
				else {attribute_sel[i]=0;} 
			}
		}
	}
	
 	if (new_security!="") {
		document.getElementById("ZPDB_FLASHSECURITY").checked = false;
		document.getElementById("ZPDB_ROMSECURITY").checked = false;
        markAttributeSelection("ZPDB_FLASHSECURITY", false);
 		markAttributeSelection("ZPDB_ROMSECURITY", false);
 		document.getElementById(new_security).checked = document.getElementById("SECURITY").checked;
		markAttributeSelection(new_security, document.getElementById("SECURITY").checked);
  	}
  	
  	if (name=="SECURITY") {
	  	new_security=""
 	   	selbox=document.getElementById("ROMTYPE");
		for (var i=0; i<6; i++) { 
 	      if (romtype_attribute[i]==selbox.value) { 
	 	      new_security=romtype_security[i]; 
    	 	  document.getElementById(new_security).checked = document.getElementById("SECURITY").checked;
			  markAttributeSelection(new_security, document.getElementById("SECURITY").checked);
 	      	  break; 
 	      }
 	   	}
 	   	if (new_security=="") {
	 	   	alert("Please select a 'ROM Type' first.");
	 	   	document.getElementById("SECURITY").checked = false;
 	   	}
  	}
}

function CountProducts(name,value) {
	// called for any change on checkboxes or selection boxes, name is field name, value is selectionvalue or true|fals for checkboxes
	// display the number of qualified products on form
	// since we have been called by an action of a field, mark selection first
  if ((name == "ZPDB_VCCMIN")
  && ((value == "*") 
      ||(document.getElementById("ZPDB_VCCMAX").value == "*")
      ||(document.getElementById("ZPDB_VCCMIN").options[document.getElementById("ZPDB_VCCMIN").selectedIndex].text > document.getElementById("ZPDB_VCCMAX").options[document.getElementById("ZPDB_VCCMAX").selectedIndex].text)
     )
  ) {
    document.getElementById("ZPDB_VCCMAX").options.selectedIndex = document.getElementById("ZPDB_VCCMIN").options.selectedIndex;
    CountProductsField(name,value);
    return CountProductsField("ZPDB_VCCMAX",document.getElementById("ZPDB_VCCMAX").value)
  } else if ((name == "ZPDB_VCCMAX")
  && ((value == "*") 
      || (document.getElementById("ZPDB_VCCMIN").value == "*")
      || (document.getElementById("ZPDB_VCCMAX").options[document.getElementById("ZPDB_VCCMAX").selectedIndex].text < document.getElementById("ZPDB_VCCMIN").options[document.getElementById("ZPDB_VCCMIN").selectedIndex].text)
     )  
  ) {
    document.getElementById("ZPDB_VCCMIN").options.selectedIndex = document.getElementById("ZPDB_VCCMAX").options.selectedIndex;
    CountProductsField(name,value);
    return CountProductsField("ZPDB_VCCMIN",document.getElementById("ZPDB_VCCMIN").value);
  } else {
    return CountProductsField(name,value);
  }
}

function CountProductsField(name,value) {
	// called for any change on checkboxes or selection boxes, name is field name, value is selectionvalue or true|fals for checkboxes
	// display the number of qualified products on form
	// since we have been called by an action of a field, mark selection first
	if (name != "BITS8" && name != "BITS16" && name != "BITS32") { markAttributeSelection(name,value); }
	FindProducts();
 	return result_count+" products qualified";
}

function ShowProducts() {
// outputs all qualified products on separate window
// input is result_count and _list
	var product_line;
	var texts1 = new Array();
	var output_html = "";
	var output_row  = "";
	var lt="<";
	var gt=">";
//	alert(document.getElementById("ZPDB_VCCMIN").value.replace("<","").replace(">","").replace("=","").replace(" ",""));
//	if (document.getElementById("ZPDB_VCCMIN").value.replace("<","").replace(">","").replace("=","").replace(" ","") > document.getElementById("ZPDB_VCCMAX").value.replace("<","").replace(">","").replace("=","").replace(" ","")) { 
//	alert(document.getElementById("ZPDB_VCCMIN").options[document.getElementById("ZPDB_VCCMIN").selectedIndex].text);
	if (document.getElementById("ZPDB_VCCMIN").options[document.getElementById("ZPDB_VCCMIN").selectedIndex].text > document.getElementById("ZPDB_VCCMAX").options[document.getElementById("ZPDB_VCCMAX").selectedIndex].text) { 
	  alert("'Power Supply from' must not be greater than 'Power Supply to'.")
  } else if (result_count==0) { 
    alert("Sorry, no qualified products found. Pls change your selection criteria.");
  }
	else {
		
		outputwin=window.open("","_blank");

		output_row  = '<?xml version="1.0" encoding="utf-8"?>\n';
       	output_row += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
        output_row += '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">\n';
		output_row += '<head>\n';
	    output_row += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n';
	    output_row += '<meta http-equiv="Content-Script-Type" content="text/javascript"/>\n';
	    output_row += '<title>MCU Product Database - Parametric Search Result</title>\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += '<style type="text/css">\n'
		output_row += '@import url("' + Web_Root + '/css/fme.css");\n'
		output_row += '@import url("' + Web_Root + '/css/fujitsu_layout_s.css");\n'
		output_row += '@import url("' + Web_Root + '/css/page_layout_s.css");\n'
		output_row += '<\/style>\n'
		outputwin.document.write(output_row); output_row="";

		output_row += '<script type="text/javascript">\n';
		outputwin.document.write(output_row); output_row="";

		output_row += 'var dom = (document.getElementsByTagName) ? true : false;\n';
		output_row += 'var ie5 = (document.getElementsByTagName && document.all) ? true : false;\n';
		output_row += 'var arrowUp, arrowDown;\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'if (ie5 || dom)\n';
		output_row += 'initSortTable();\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function initSortTable() {\n';
		output_row += 'arrowUp = document.createElement("SPAN");\n';
		output_row += 'var tn = document.createTextNode("    ^");\n';
		output_row += 'arrowUp.appendChild(tn);\n';
		output_row += 'arrowUp.className = "arrow";\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += '	arrowDown = document.createElement("SPAN");\n';
		output_row += '	var tn = document.createTextNode("    v");\n';
		output_row += '	arrowDown.appendChild(tn);\n';
		output_row += '	arrowDown.className = "arrow";\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function sortTable(tableNode, nCol, bDesc) {\n';
		output_row += '	var tBody = tableNode.tBodies[0];\n';
		output_row += '	var trs = tBody.rows;\n';
		output_row += '	var trl = trs.length;\n';
		output_row += '	var a = new Array();\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'for (var i = 0; i < trl; i++) {\n';
		output_row += 'a[i] = trs[i];\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'var bIsString = false;\n';
		output_row += 'var fTypeCast = null;\n';
		output_row += 'for (var i = 0; i < trl && bIsString == false; i++) {\n';
		output_row += 'bIsString = isNaN(checkNumber(getInnerText(a[i].cells[nCol])));\n';
		output_row += '}\n';
		output_row += 'if (bIsString == false) { \n';
		output_row += 'fTypeCast = toNumber;\n';
		output_row += '} else {\n';
		output_row += 'fTypeCast = CaseInsensitiveString;\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'a.sort(compareByColumn(nCol,bDesc,fTypeCast));\n';
		output_row += 'for (var i = 0; i < trl; i++) {\n';
		output_row += 'tBody.appendChild(a[i]);\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'if (typeof tableNode.onsort == "string")\n';
		output_row += 'tableNode.onsort = new Function("", tableNode.onsort);\n';
		output_row += 'if (typeof tableNode.onsort == "function")\n';
		output_row += 'tableNode.onsort();\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function checkNumber(s) {\n';
		output_row += 'var x = Number(s.replace(/[0-9\.,+-]/g, ""));\n';
		output_row += 'return x;\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function toNumber(s) {\n';
		output_row += 'var x = Number(s.replace(/[^0-9\.+-]/g, ""));\n';
		output_row += 'return x;\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function CaseInsensitiveString(s) {\n';
		output_row += 'return String(s).toUpperCase();\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function compareByColumn(nCol, bDescending, fTypeCast) {\n';
		output_row += 'return function (n1, n2) {\n';
		output_row += 'if (fTypeCast(getInnerText(n1.cells[nCol])) < fTypeCast(getInnerText(n2.cells[nCol])))\n';
		output_row += 'return bDescending ? -1 : +1;\n';
		output_row += 'if (fTypeCast(getInnerText(n1.cells[nCol])) > fTypeCast(getInnerText(n2.cells[nCol])))\n';
		output_row += 'return bDescending ? +1 : -1;\n';
		output_row += 'return 0;\n';
		output_row += '};\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function sortColumn(e) {\n';
		output_row += 'var tmp = e.target ? e.target : e.srcElement;\n';
		output_row += 'var tHeadParent = getParent(tmp, "THEAD");\n';
		output_row += 'var el = getParent(tmp, "TD");\n';
		output_row += 'if (el == null) el = getParent(tmp, "TH");\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'if (tHeadParent == null)\n';
		output_row += 'return;\n';
		output_row += 'if (el != null) {\n';
		output_row += 'var p = el.parentNode;\n';
		output_row += 'var i;\n';
		output_row += 'el._descending = !Boolean(el._descending);\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'if (tHeadParent.arrow != null) {\n';
		output_row += 'if (tHeadParent.arrow.parentNode != el) {\n';
		output_row += 'tHeadParent.arrow.parentNode._descending = null;	//reset sort order		\n';
		output_row += '}\n';
		output_row += 'tHeadParent.arrow.parentNode.removeChild(tHeadParent.arrow);\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'if (el._descending)\n';
		output_row += 'tHeadParent.arrow = arrowUp.cloneNode(true);\n';
		output_row += 'else\n';
		output_row += 'tHeadParent.arrow = arrowDown.cloneNode(true);\n';
		output_row += 'el.appendChild(tHeadParent.arrow);\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += '// get the index of the td\n';
		output_row += 'var cells = p.cells;\n';
		output_row += 'var l = cells.length;\n';
		output_row += 'for (i = 0; i < l; i++) {\n';
		output_row += 'if (cells[i] == el) break;\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'var table = getParent(el, "TABLE");\n';
		output_row += 'sortTable(table,i,el._descending);\n';
		output_row += '}\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function getInnerText(el) {\n';
		output_row += 'if (ie5) return el.innerText;\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'var str = "";\n';
		output_row += 'var cs = el.childNodes;\n';
		output_row += 'var l = cs.length;\n';
		output_row += 'for (var i = 0; i < l; i++) {\n';
		output_row += 'switch (cs[i].nodeType) {\n';
		output_row += 'case 1:\n';
		output_row += 'str += getInnerText(cs[i]);\n';
		output_row += 'break;\n';
		output_row += 'case 3:\n';
		output_row += 'str += cs[i].nodeValue;\n';
		output_row += 'break;\n';
		output_row += '}\n';
		output_row += '}\n';
		output_row += 'return str;\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";
		
		output_row += 'function getParent(el, pTagName) {\n';
		output_row += 'if (el == null) return null;\n';
		output_row += 'else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())\n';
		output_row += 'return el;\n';
		output_row += 'else\n';
		output_row += 'return getParent(el.parentNode, pTagName);\n';
		output_row += '}\n';
		outputwin.document.write(output_row); output_row="";		
		
		output_row += '<\/script>\n';
		outputwin.document.write(output_row); output_row="";		
		
		output_row += '<style type="text/css">\n';
		output_row += 'input.message { border: none; }\n';
    	output_row += 'thead .arrow		{color: black; padding: 0; font-size: 12px;';
		output_row += 'margin-bottom: 2; margin-top: -3; padding: 0; padding-top: 0; padding-bottom: 2;}'		
		output_row += '</style>\n';
		output_row += '</head>\n';
		output_row += '<body>\n';
		outputwin.document.write(output_row); output_row="";

		output_row += '<div style="text-align:left; font-size: 83%;" class="cv_c">\n';
		output_row += '<table onclick="sortColumn(event)" id="tresult" cellspacing="0" cellpadding="3" border="1">\n';
		output_html += output_row; output_row = "";
		
		// write column header line first
		output_row  += '<thead align="left">\n';
		output_html += output_row; output_row = "";

		output_row += '<tr class="header textsmall" align="left" valign="top">\n';
		texts1 = product_matrix_header.split("|",nbr_char_columns+1);
    		for (var j=0; j<=nbr_char_columns; j++) {
				output_row += '<th style="cursor: pointer">' + texts1[j] + '</th>\n';
			}
			 output_row +=  '</tr>\n';	
		output_row  += '</thead>\n';
		output_row  += '<tbody align="left" valign="top">\n';
		output_html += output_row; output_row = "";

		for (var i=0; i < result_count; i++) {
			// now loop for all products
			// get details for product ( | is separator, nbr of cols is in nbr_char_columns) and check its value
			product_line=product_matrix[result_list[i]];
			texts1 = product_line.split("|",nbr_char_columns+1);
			if (i % 2) {
				output_row = '<tr class="odd textsmall">\n';	
			} else {				
				output_row = '<tr class="even textsmall">\n';	
			}
			// for all attributes
    		for (var j=0; j<=nbr_char_columns; j++) {
    		// output field
    			if (texts1[j]=="" || texts1[j]==undefined) {texts1[j]=".";}
				if (j==0) { output_row += '<td><a href="' + Document_Location+texts1[j] + '.htm" target="_blank">' + texts1[j] + '</a></td>\n'; }
				else { output_row += '<td>' + texts1[j] + '</td>\n';}
			}
			output_row  += '</tr>\n';
			output_html += output_row; output_row = "";
		}

		output_row  += '</table>\n';
		output_row  += '</div>\n';
		output_row  += '</body>\n';
		output_row  += '</html>';

		output_html += output_row; output_row = "";
		outputwin.document.write(output_html);
		outputwin.document.close();
	}
}

function FindProducts() {
// main routine to count and extract all products based on selection criteria (attribute_sel and product_matrix)
// output is result count and _list
	var product_line;
	var product_selected = Boolean;
	var bits8_selected = Boolean;
	var bits16_selected = Boolean;
	var bits32_selected = Boolean;
	var chkbox_bits8;
	var chkbox_bits16;
	var chkbox_bits32;
	var attribute_line;
	var attribute;
	result_count=0;
	// first retrieve selection criteria for bits (8,16,32), will be used for checking in loop
	if (bits_pos_in_table==0) { 
		alert("Error: Selection for Bits 8,16,32 not possible."); 
		bits8_selected=false; 
		bits16_selected=false;
		bits32_selected=false;
	} else {
		chkbox_bits8=document.getElementById("BITS8"); bits8_selected=chkbox_bits8.checked;
		chkbox_bits16=document.getElementById("BITS16"); bits16_selected=chkbox_bits16.checked;
		chkbox_bits32=document.getElementById("BITS32"); bits32_selected=chkbox_bits32.checked;
	}
	for (var i=0; i<=nbr_products-1; i++) {
		// get details for product ( | is separator, 250 is a max number) and check its value
	    product_line=product_matrix[i];
	    var texts1 = product_line.split("|",250);
	    // assume product is qualified (check is logical and)
	    product_selected=true; 
	    // first check bits 8/16/32 selection
		if ( (bits8_selected && texts1[bits_pos_in_table]=="8") || 
             (bits16_selected && texts1[bits_pos_in_table]=="16") ||
             (bits32_selected && texts1[bits_pos_in_table]=="32") )
        {
		  for (var j=0;j<=nbr_attribute_matrix-1 && product_selected;j++) {
      		if (attribute_sel[j]!=0 && product_selected) {
      		    // now check for all selected attributes, get details and compare with product attribute
				attribute_line = new splitAttributeLine(attribute_matrix[j]);
				// check if selected attributes matches product attribute, =1 indicates selection box, -1 is for checkbox
				attribute=texts1[attribute_line.posintable];
				if (attribute_sel[j]==-1) { 
      				// if checkbox is checked, do not allow 0 or No
     	 			if (attribute=="0" || attribute=="NO"  ) { product_selected=false; } }
	    		else {
// to be decided	if (attribute=="") { product_selected=false; }
//     		 		else {
     	 				if (attribute_line.alphanum=="N") { 
     	 				// for numeric fields evaluate optionalvalue and value for product
     	 					if (attribute=="") { 
	     	 					product_selected=false; 
	     	 				} else { 
		     	 				// tperne - eval error if value contains comma e.g. 1,024.00
		     	 				//if (!eval(attribute+attribute_line.optionval)) { 
		     	 				if (!eval(attribute.replace(/,/,"")+attribute_line.optionval.replace(/,/,""))) { 
			     	 				product_selected=false; 
			     	 			} 
		     	 			} 
		     	 		} else {
     	 				// for character fields compare product characteristic with optionvalue 
	 	     				if (attribute!=attribute_line.optionval ) { product_selected=false; } }
//	 	     		  }
	 	     	}
	 	     }
	 	   }
	 	} else { 
		 	product_selected=false; 
		};  	
	  	// add to result list if qualified
	  	if (product_selected) { 
		  	result_count++ ; result_list[result_count-1]=i; } 
	  }
}

