/* Setting up raw data.  This will be parsed into actual menu commands below */

// holds the current selections in an array by name
var currentSelections = new Array();

function p_series(menu_val, code, group, lehigh_code) {
   this.menu_val = menu_val;
   this.code = code;
   this.group = group;
   this.lehigh_code = lehigh_code;
}

series_AA = new p_series('Pneumatic Standard Steel', 'AA', '0', 'J'); 
series_MH = new p_series('Hydraulic Med-Duty', 'MH', '1', 'H');
series_HD = new p_series('Hydraulic Heavy-Duty Steel', 'HD', '2', 'X');

// Lehigh-specific series
series_L = new p_series('Pneumatic Stainless', 'L', '0', 'L');
series_E = new p_series('Hydraulic Med-Duty Stainless', 'E', '1', 'E');
series_W = new p_series('Hydraulic Heavy-Duty Stainless', 'W', '2', 'W');

var series = [series_AA, series_MH, series_HD, series_L, series_E, series_W];

var bores_0 = [1, 1.5, 2, 2.5, 3.25, 4, 5, 6, 8, 10, 12, 14, 16, 18, 20]; // size 1 is Lehigh only, as are 12 - 20
var bores_1 = [1, 1.5, 2, 2.5, 3.25, 4, 5, 6, 8, 10]; // size 8 is higginson-only, size 1 is Lehigh only
var bores_2 = [1.5, 2, 2.5, 3.25, 4, 5, 6, 7, 8, 10, 12]; // sizes 10, 12 are Lehigh only
var bores_3 = [8, 10, 12, 14, 16, 18, 20] // all are Lehigh only


var rods_0_S = [0.5, 0.63, 0.63, 0.63, 1, 1, 1, 1.375, 1.375, 1.75, 2, 2.5, 3, 3.5, 3.5]
var rods_0_1 = [0.63, 1, 1, 1, 1.375, 1.375, 1.375, 1.75, 1.75, 2, 2.5, 3, 3.5, 4, 4]
var rods_0_2 = [null, null, 1.375, 1.375, 1.75, 1.75, 1.75, 2, 2, 2.5, 3, 3.5, 4, 5, 5]
var rods_0_3 = [null, null, null, 1.75, 2, 2, 2, 2.5, 2.5, 3, 3.5, 4, 5, 5.5, 5.5]

// the Higginson-only '8' bore head diameters are the same as the '6' diameters - they are Higginson-only
var rods_1_S = [0.5, 0.63, 0.63, 0.63, 1, 1, 1, 1.38, 1.38, 1.38]
var rods_1_1 = [0.63, 1, 1, 1, 1.38, 1.38, 1.38, 1.75, 1.75, 1.75]
var rods_1_2 = [null, null, 1.38, 1.38, 1.75, 1.75, 1.75, 2, 2, 2]
var rods_1_3 = [null, null, null, 1.75, 2, 2, 2, 2.5, 2.5, 2.5]

var rods_2_S = [0.63, 1, 1, 1.38, 1.75, 2, 2.5, 3, 3.5, 5, 5.5]
var rods_2_1 = [1, 1.375, 1.38, 1.75, 2, 2.5, 3, 3.5, 4, 7, 8.5]
var rods_2_2 = [null, null, 1.75, 2, 2.5, 3, 3.5, 4, 5, null, null]
var rods_2_3 = [null, null, null, null, null, 3.5, 4, 5, 5.5, null, null]

var rods_3_S = [1, 1, 1.38, 1.75, 2, 2.5, 3]
var rods_3_1 = [1.38, 1.38, 1.75, 2, 2.5, 3, 3.5]


var mounts_a = [
		['NFPA: MX0', 'MX0.gif', '01 S'], 
		['NFPA: MDX0 DRE', 'MX1-DRE.gif', '01 D'],
        ['NFPA: MX1', 'MX1.gif', '06 S'], 
        ['NFPA: MDX1 DRE', 'MX1-DRE.gif', '06 D'],
		['NFPA: MX2', 'MX2.gif', '18 S'], 
		['NFPA: MDX2 DRE', 'MX2-DRE.gif', '18 D'],
		['NFPA: MX3', 'MX3.gif', '19 S']
		]

var mounts_b = [
		['NFPA: MS1', 'MS1.gif', '05 S'], 
		['NFPA: MS1 DRE', 'MS1-DRE.gif', '05 D'],
		['NFPA: MS2', 'MS2.gif', '12 S'], 
		['NFPA: MS2 DRE', 'MS2-DRE.gif', '12 D'],
		['NFPA: MS3', 'MS3.gif', '13 S'],  
		['NFPA: MS4', 'MS4.gif', '11 S'], 
		['NFPA: MS4 DRE', 'MS4-DRE.gif', '11 D'],
		['NFPA: MS7', 'MS7.gif', '25 S'], 
		['NFPA: MS7 DRE', 'MS7-DRE.gif', '25 D']
		]

var mounts_c = [['NFPA: MP1', 'MP1.gif', '09 S'], ['NFPA: MP2', 'MP2.gif', '10 S'],
		['NFPA: MP3', 'MP3.gif', '13 S'], ['NFPA: MP5', 'MP5.gif', '28 S']]

var mounts_d = [
	['NFPA: MF1', 'MF1.gif', '07 S'], 
	['NFPA: MF1 DRE', 'MF1-DRE.gif', '07 D'],
	['NFPA: MF2', 'MF2.gif', '08 S'], 
	['NFPA: MF5', 'MF5.gif', '22 S'], 
	['NFPA: MF5 DRE', 'MF5-DRE.gif', '22 D'],
	['NFPA: MF6', 'MF6.gif', '26 S'], 
	['NFPA: MF6 DRE', 'MF6-DRE.gif', '26 D'],
	['NFPA: ME3 Square Cap', 'ME3-Cap.gif', '36 00'], 
	['NFPA: ME3 DRE Square Cap', 'ME3-Cap-DRE.gif', '36 D'],
	['NFPA: ME3 Square Flange', 'ME3-Flange.gif', '03 S'], 
	['NFPA: ME3 DRE Square Flange', 'ME3-Flange-DRE.gif', '03 D'],
	['NFPA: ME4 Square Cap', 'ME4-Cap.gif', '37 00'],
	['NFPA: ME4 Square Flange', 'ME4-Flange.gif', '48 S'],
	['NFPA: ME5', 'ME5.gif', '15 S'], 
	['NFPA: ME5 DRE', 'ME5-DRE.gif', '15 D'],
	['NFPA: ME6', 'ME6.gif', '16 S'], 
	['NFPA: ME6 DRE', 'ME6-DRE.gif', '16 D']]


var mounts_e = [
		['NFPA: MNR1', 'MNR1.gif', '10 00'], 
		['NFPA: MNR1 DRE', 'MNR1-DRE.gif', '10 D'],
		['NFPA: MT1', 'MT1.gif', '14 S'], 
		['NFPA: MT1 DRE', 'MT1-DRE.gif', '14 D'],
		['NFPA: MT2', 'MT2.gif', '23 S'],
		['NFPA: MT4', 'MT4.gif', '24 S'], 
		['NFPA: MT4 DRE', 'MT4-DRE.gif', '24 D']
		]







var l_mounts_a = [['01 (NFPA: MX0)', '01 00'], ['DRE 01 (NFPA: MDX0)', '01 D'], 
		['02 (NFPA: MX1)', '02 00'], ['DRE 02 (NFPA: MDX1)', '02 D'],
		['03 (NFPA: MX2)', '03 00'], ['DRE 03 (NFPA: MDX2)', '03 D'],
		['04 (NFPA: MX3)', '04 00']]

var l_mounts_b = [
		['05 (NFPA: MS1)', '05 00'], 
		['DRE 05 (NFPA: MS1)', '05 D'],
		['11 (NFPA: MS2)', '11 00'], 
		['DRE 11 (NFPA: MS2)', '11 D'],
		['xxx', 'xxx'], 
		['09 (NFPA: MS4)', '09 00'], 
		['DRE 09 (NFPA: MS4)', '09 D'],
		['15 (NFPA: MS7)', '15 00'], 
		['DRE 15 (NFPA: MS7)', '15 D']
		]

var l_mounts_c = [['8F (NFPA: MP1)', '8F 00'],['8D (NFPA: MP2)', '8D 00'],
		['8M (NFPA: MP3)', '8M 00'], ['8S (NFPA: MP5)', '8S 00']]

var l_mounts_d = [
	['06 (NFPA: MF1)', '06 00'],    
	['DRE 06 (NFPA: MF1)', '06 D'], 
	['07 (NFPA: MF2)', '07 00'],    
	['16 (NFPA: MF5)', '16 00'],    
	['DRE 16 (NFPA: MF5)', '16 D'], 
	['17 (NFPA: MF6)', '17 00'],    
	['xxx','xxx'],                  
	['36 (NFPA: ME3)', '36 00'],    
	['DRE 36 (NFPA: ME3)', '36 D'], 
	['6S (NFPA: ME3)', '6S 00'],    
	['DRE 6S (NFPA: ME3)', '6S D'], 
	['37 (NFPA: ME4)', '37 00'],    
	['7S (NFPA: ME4)', '7S 00'],    
	['26 (NFPA: ME5)', '26 00'],    
	['DRE 26 (NFPA: ME5)', '26 D'], 
	['27 (NFPA: ME6)', '27 00'],    
	['xxx','xxx']]                  

var l_mounts_e = [
		['10 (NFPA: MNR1)', '10 00'], 
		['DRE 10 (NFPA: MNR1)', '10 D'],
		['13 (NFPA: MT1)', '13 00'], 
		['DRE 13 (NFPA: MT1)', '13 D'],
		['12 (NFPA: MT2)', '12 00'],
		['14 (NFPA: MT4)', '14 00'], 
		['DRE 14 (NFPA: MT4)', '14 D']
		]






















function showMount(sub_letter, series_group, bore_key, code_value) {
   result = true;
   if (sub_letter != 'a' && series_group == '3'){
      result = false;
   } else {
      switch(sub_letter) {      
      case 'a':
	 if ((series_group == '2') && (bore_key > 8)) {
	     result = false;
	  }
	 break;
      case 'b': // foot and flush mounts
	 if (code_value == '13 S' || code_value == '13 D') {
 	    if (series_group == '0' || series_group == '1') {
	       result = false;
	    }
	 }
	 break;
      case 'c': // clevis and pivot
	 if (code_value == '17 S') {
	    if (series_group == '2') {
	       result = false;
	    }
	 }

	 if (code_value == '10 S') {
	    if (series_group > '2' && bore_key > 6) {
	       result = false;
	    }
	 }
	 break;
      case 'd':  //flange mounts



	 if (code_value == '48 S') {
			 	
	    if (series_group == '0' && bore_key != 7 && bore_key != 8) result = false;
	    
	 }


	 if (code_value == '03 S' || code_value == '03 D') {
		
	 	
	    if ((series_group == '0' && bore_key != 7 && bore_key != 8) || (series_group == '1' && bore_key < 7) || series_group == '2') {
	       result = false;
	    }

	 }
	 if ((code_value == '15 S' || code_value == '15 D' || code_value == '16 S' || code_value == '16 D') && series_group != '2') {
	    result = false;
	 }
	 break;
      }
   }
      
	if( 
		(code_value=="36 00" && (series_group != 2))  
	  ||			
		(code_value=="36 D" && (series_group != 2))  
	  ||					
		(code_value=="37 00" && (series_group != 2))  
	  ||			
		(code_value=="36 00" && bore_key<7 && (series_group == 2))  
	  ||	
		(code_value=="36 D" && bore_key<7 && (series_group == 2))  
	  ||	
		(code_value=="03 S" && bore_key<7 && (series_group == 0))  
	  ||	
		(code_value=="03 D" && bore_key<7 && (series_group == 0))  
	  ||	
		(code_value=="10 S" && (series_group == 2))  
	  ||	
		(code_value=="17 S" && (series_group == 2))  
	  ||	
		(code_value=="28 S" && (series_group == 2))  
	  ||	
		(code_value=="48 S" && (series_group == 2))  
	) result = false;
   
   
   
   
   
   
   
   
   
   
   
   return result;   
}

var menu1=new Array();
var menu2=new Array();
var menu3=new Array();
var menu4=new Array();
var menu5=new Array();
var menu6=new Array();
var menu7=new Array();

var menu6a=new Array();
var menu6b=new Array();
var menu6c=new Array();
var menu6d=new Array();
var menu6e=new Array();

/* Menu 1 */
for (i=0; i<series.length; i++) {
   eval("menu1[" + i + "]=build_menu_link(\"setSeries('" + series[i].code + "', '" + series[i].menu_val + "')\", 'partno1', '" + series[i].code  + "', '" + series[i].menu_val + "')");
}

function buildBoreMenu(seriesName) {
   // get the series by name - passed value is the suffix of the series name
   selected_series = eval("series_" + seriesName);
   //store the series object in the currentSelection
   currentSelections.series = selected_series
   menu2 = new Array();
   next_bore = eval("bores_" + selected_series.group);
   // calculate the code value
   for (k=0; k<next_bore.length; k++) {
      next_value = calculateBoreValue(next_bore[k]);
      // build menu items
      eval("menu2[" + k + "]=build_menu_link(\"setBore('" + next_bore[k] + "')\", 'partno2', '" + next_code + "', '" + next_bore[k] + "&quot;')");
   }
}

function calculateBoreValue(boreNum) {
   if (boreNum == 1) {
      next_code = '01'; 
   } else if (boreNum < 10) {
      next_code = Math.floor(boreNum * 10);
   } else if (boreNum == 20) {
      next_code = '02';
   } else {
      next_code = boreNum;
   }
   return next_code;
}


function buildRodMenu(borevalue) {
   menu3 = new Array();
   selectedSeries = currentSelections.series
   // find the index of the selected bore and put it in the currentSelection so we can more easily access the arrays for the menus that depend on bore
   current_bore_menu = eval("bores_" + selected_series.group);
   matchingKey = null;   
   for (i=0; i<current_bore_menu.length && matchingKey == null; i++) {
      if (borevalue == (current_bore_menu[i])) {
	 matchingKey = i;
	 currentSelections.bore_key = matchingKey;
      }
   }

   selected_series = eval("series_" + currentSelections.series.code);
   rods = ["S", "1", "2", "3"]
   for (i=0; i<rods.length; i++) {
      rods_options = null
      
      
      try {
	 rods_options = eval("rods_" + selectedSeries.group + "_" + rods[i]);
      } catch(e) {
	 ;
      }
      

      if (rods_options != null) {
	 rod_match = rods_options[matchingKey]
	 if (rod_match != null) {
	    eval("menu3[" + i + "]=build_menu_link(\"setRodDiameter('" + rod_match + "')\", 'partno3', '" + rods[i] + "', '" + rod_match + " inch');");
	 }
      }
   }
}

function buildRodEndMenu() {
   // these are always the same so just create them explicitly
   menu4=new Array();
   menu4[0]=build_menu_link("setRodEnd('A - Standard Male')", "partno5", "A", "Type A - Standard Male", "newimages/typeA.gif");
   menu4[1]=build_menu_link("setRodEnd('B - Standard Female')", "partno5", "B", "Type B - Standard Female", "newimages/typeB.gif");
   menu4[2]=build_menu_link("setRodEnd('D - Intermediate Male')", "partno5", "D", "Type D - Intermediate Male", "newimages/typeD.gif");
   menu4[3]=build_menu_link("setRodEnd('F - Full Male')", "partno5", "F", "Type F - Full Male", "newimages/typeF.gif");
   menu4[4]=build_menu_link("setRodEnd('X - Special')", "partno5", "X", "Type X - Special", "newimages/typeX.gif");
}

function buildCushionMenu(borevalue) {
   menu5 = new Array();
   selectedSeries = currentSelections.series
   menu5[0]=build_menu_link("setCushion('N - None')", "partno6", "N", "N - No Cushions");

   if (selectedSeries.group != '3') {
      menu5[1]=build_menu_link("setCushion('B - Both Ends')", "partno6", "B", "B - Cushions at Both Ends");
      menu5[2]=build_menu_link("setCushion('C - Cap End Only')", "partno6", "C", "C - Cushions at Cap End Only");
      menu5[3]=build_menu_link("setCushion('H - Head End Only')", "partno6", "H", "H - Cushions at Head End Only");      
   }
}

function buildPortPosition() {
   // these are always the same so just create them explicitly
   menu7=new Array();
   menu7[0]=build_menu_link("setPortPosition('1 Top')", "partno8", "1 Top","1 Top");
   menu7[1]=build_menu_link("setPortPosition('2 Right')", "partno8", "2 Right","2 Right");
   menu7[2]=build_menu_link("setPortPosition('3 Bottom')", "partno8", "3 Bottom","3 Bottom");
   menu7[3]=build_menu_link("setPortPosition('4 Left')", "partno8", "4 Left","4 Left");
}

function buildMountingTopMenu() {
   menu6 = new Array();
   selectedSeries = currentSelections.series;
   selectedBore = currentSelections.bore_key;
   counter = 0;
   if ((selectedSeries.group == '2' && selectedBore > 8) == false) {
     menu6[counter++]=build_top_menu_link('menu6a', "Tie Head Mounts");
   }
   if (selectedSeries.group != '3') {
      menu6[counter++]=build_top_menu_link('menu6b', "Foot and Flush Mounts");
      menu6[counter++]=build_top_menu_link('menu6c', "Clevis and Pivot Mounts");
      menu6[counter++]=build_top_menu_link('menu6d', "Flange Mounts");
      if (selectedSeries.group == '2') { // if (selectedSeries.code == 'X') {
	 menu6[counter++]=build_top_menu_link('menu6e', "Trunnion Mounts");
      } else {
	 menu6[counter++]=build_top_menu_link('menu6e', "Trunnion and Head Mounts");
      }
   }
}

/* Builds the submenus - if the passed non-lehigh argument is true, then call reset_prefix - otherwise, depends on the selection */
function buildMountingSubMenus(non_lehigh) {
   selectedSeries = currentSelections.series;
   selectedBore = currentSelections.bore_key;

//   printfire("Selected Bore: " + selectedBore);
   menus = ['a', 'b', 'c', 'd', 'e'];
   // clear previous menu values 

   for (i=0; i<menus.length; i++) {
      eval("if (menu6" + menus[i] + " != undefined) { menu6" + menus[i] + " = new Array(); }");
   }
   for (i=0; i<menus.length; i++) {
      menuvals = eval("mounts_" + menus[i]);

      for (j=0; j<menuvals.length; j++) {

	 
	 label = menuvals[j][0]
	 code = menuvals[j][2]
	 graphic = menuvals[j][1]

		 if (showMount(menus[i], selectedSeries.group, selectedBore, code) == true) {
			graphic_name = "newimages/" + graphic;
			eval ("menu6" + menus[i] + "[" + j + "]=build_sub_menu_link(\"setMounting('" + label + "')\", 'partno7', \"" + code + "\", \"" + label + "\", \"" + graphic_name + "\");");
		 }
	 }
   }
}

