/* Tests to figure out the browser */
   var ie4=document.all
var ns6=document.getElementById&&!document.all

function buildPartNum() {
   form = document.partbuilderform;
   result = form.partno1.value + form.partno2.value + form.partno3.value + form.partno4.value + form.partno5.value + form.partno6.value + (form.partno7.value.replace(' ', ''));
   //   alert("partnum: " + result);
   VirtualCAD('testing@VirtualCAD.com',result);
}

menuImages = ['cylinder', 'bore', 'rod', 'stroke', 'rodend', 'cushions', 'mounting']

/* Set menu values and swap images etc. */
   function setupNextSelection(source_number) {
      for (i=0; i<menuImages.length; i++) {
	 if (i == (source_number - 1)) {
	    imgAct(menuImages[i]);
	 } else {
	    imgInact(menuImages[i]);
	 }
      }

      menuItemActive = (source_number - 1);
      document.partbuilderform.download_match.disabled = true;

      for (i=source_number; i<=7; i++) {
	 eval("document.partbuilderform.partno" + i + ".value = '';");
	 eval("document.partbuilderform.fieldno" + i + ".value = '';");
      }
   }

/* *** SET VALUES FROM MENU SELETIONS *** */
   function setSeries(newval) {
      // passed value is series name, like 'JHD', 'VAC', etc.
      document.partbuilderform.fieldno1.value = newval;
      buildBoreMenu(newval);
      setupNextSelection(2);
   }

function setBore(newval) {
   document.partbuilderform.fieldno2.value = newval + "\"";
   buildRodMenu(newval);
   buildRodEndMenu();
   buildCushionMenu(newval);
   buildMountingTopMenu();
   buildMountingSubMenus();
   setupNextSelection(3);
}

function setRodDiameter(number) {
   numRodDiameter = number;
   document.partbuilderform.fieldno3.value = numRodDiameter + "\"";
   //enableButtonIfFormComplete();
   setupNextSelection(4);
   document.partbuilderform.fieldno4.readOnly = false;
   document.part_graphic.src = 'newimages/strokeNote.gif'
}

/* If you've finished selecting but accidentally mouseover and then mousout of the stroke field, everything resets.
 * To keep this from happening, we store the value on mouseover with this function and see if it's changed on mouseout in setStrokeValue.
 */
function storeStrokeValue() {   
   cleanStroke();
   prevStrokeValue = document.partbuilderform.fieldno4.value;
}

function cleanStroke() {
   newval = document.partbuilderform.fieldno4.value;
   newval = newval.replace("\"", "");
   allcharsRegExp = /[^\.\d]/g
   newval = newval.replace(allcharsRegExp, "");
   if (newval >= 100) 
   {
      alert ("Note that 99.99 inches is the max value.\nFor custom requirements, contact Lehigh at (800) 257-9515.");
      newval = '';
   }
   if (newval != '') {
      fieldval = newval + "\"";
   } else {
      fieldval = newval;
   }
   document.partbuilderform.fieldno4.value = fieldval;
   return newval;
}

function setStrokeValue() {   
   newval = cleanStroke();
   if ((newval > 0) && (newval < 100)) {
      strokepartno = 100*newval;
      strokepartno2 = strokepartno.toFixed(0);
      if ((strokepartno2 <= 999) && (strokepartno2 >= 100)) {
	 strokepartno2 = "0" + strokepartno2;
      }
      else if ((strokepartno2 <= 99) && (strokepartno2 >= 1)){
         strokepartno2 = "00" + strokepartno2;
      }
      
      //strokepartno = 100*newval;
      document.partbuilderform.partno4.value = strokepartno2;
      //enableButtonIfFormComplete();
      if ((newval + "\"") != prevStrokeValue) {
	 setupNextSelection(5);
	 document.part_graphic.src = 'newimages/blank.gif'
      }
   }
   else if (newval >= 100) {
      document.partbuilderform.fieldno4.value = '';
      alert ("Note that 99.99 inches is the max value.\nFor custom requirements, contact Lehigh at (800) 257-9515.");
   } else if (newval == '') {
      document.partbuilderform.fieldno4.value = prevStrokeValue;
   } else {
      document.partbuilderform.fieldno4.value = '';
      alert ("You have entered an invalid Stroke Length.  Please do not use letters or more than one decimal point.  Example, 14.50.");      
   }
   //   } 
}

function setRodEnd(newval) {
   document.partbuilderform.fieldno5.value = newval;
   //enableButtonIfFormComplete();
   setupNextSelection(6);
}

function setCushion(newval) {
   document.partbuilderform.fieldno6.value = newval;
   setupNextSelection(7);
}


function setMounting(newval) {
   document.partbuilderform.fieldno7.value = newval;
   setupNextSelection(8);
   document.partbuilderform.download_match.disabled = false;
}


/* Set up the menu divs */
   /* Normal single-level menus */
   if (ie4||ns6) {
      document.write('<div id="dropmenudiv" class="menustyle" style="visibility:hidden;width: 160px" ')
      document.write(' onmouseover="clearhidemenu(\'dropmenudiv\')" onmouseout="delayhidemenu(\'dropmenudiv\')"></div>')
   }

/* Menus that have cascading children */
   if (ie4||ns6) {
      document.write('<div id="dropmenudiv-top" class="menustyle" style="visibility:hidden;width: 160px" ')
      document.write(' onmouseover="clearhidemenu(\'dropmenudiv-top\'); clearhidemenu2(\'dropmenudiv-sub\')" onmouseout="delayhidemenu(\'dropmenudiv-top\'); delayhidemenu2(\'dropmenudiv-sub\')"></div>')
   }

/* Child menus of dropmenudiv-top menus */
   if (ie4||ns6) {
      document.write('<div id="dropmenudiv-sub" class="menustyle" style="visibility:hidden;width: 160px" ')
      document.write(' onmouseover="clearhidemenu(\'dropmenudiv-top\'); clearhidemenu2(\'dropmenudiv-sub\')" onmouseout="delayhidemenu(\'dropmenudiv-top\'); delayhidemenu2(\'dropmenudiv-sub\')"></div>')
   }

/* Makes it easier to build menus */
   function build_menu_link(functionName, builderfieldname, builderval, label, graphic) {
      result = "<A HREF=";
      result = result + '"javascript:';
      result = result + "document.partbuilderform." + builderfieldname + ".value = '" + builderval + "'; ";
      result = result + functionName + "; "; 
      result = result + 'hidemenu(' + "'dropmenudiv'" + '); "' 
      if (graphic != undefined) {
	 result = result + ' onmouseover="swapimage(\'' + graphic + '\')" '
      }
      result = result + '>' + label + "</a>";
      return result;
   }

/* Makes it easier to build menus that have sub-menus */
   function build_top_menu_link(submenuname, label, graphic) {
      result = "<A HREF='#' "
      result = result + 'onMouseover="clearhidemenu(); clearhidemenu2(); return dropdownmenu(this, event, ' + submenuname + ', \'165px\', \'dropmenudiv-sub\', \'sub\')">' + label + "<img src=newimages/arrow.gif width=9 height=7 border=0 align=absmiddle></a>";
      return result;
   }

/* Makes it easier to build menus that extend from other menus */
   function build_sub_menu_link(functionName, builderfieldname, builderval, label, graphic) {
      result = "<A HREF=";
      result = result + '"javascript:' + functionName + ";";
      result = result + "document.partbuilderform." + builderfieldname + ".value = '" + builderval + "';";
      result = result + 'hidemenu(' + "'dropmenudiv-sub'" + '); hidemenu(' + "'dropmenudiv-top'" + '); "';
      if (graphic != undefined) {
	 result = result + ' onmouseover="swapimage(\'' + graphic + '\')" ';
      }
      result = result + '>' + label + "</a>";
      return result;
   }

function swapimage(imgName) {
   document.part_graphic.src = imgName;
}

function imgAct(imgName) {
   document[imgName].src = "newimages/" + imgName + "-over.gif";
}

function imgInact(imgName) {
   document[imgName].src = "newimages/" + imgName + ".gif";
}



/***********************************************
* AnyLink Vertical Menu- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************
* Heavily modified by Andrew Burke for cascading menus and 'clarity'
* http://www.andrewburke.ca
***********************************************/
   var disappeardelay=150  //menu disappear speed onMouseout (in miliseconds)
   var horizontaloffset=3 //horizontal offset of menu from default location. (0-5 is a good value)

/* Climbs back up the collection of offset parents for the passed 'what' to get the absolute page position - either 'left' or 'top'*/
   function getposOffset(what, offsettype){
      var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
      var parentEl=what.offsetParent;
      while (parentEl!=null){
	 if (offsettype=="left") {
	    totaloffset = totaloffset + parentEl.offsetLeft;
	 } else {
	    totaloffset = totaloffset + parentEl.offsetTop;
	 }
	 parentEl=parentEl.offsetParent;
      }
      return totaloffset;
   }

/* sets up the target_menu div style to show correctly or to hide, depending on the passed event */
   function showhide(obj, e, visible, hidden, menuwidth, target_menu){
      if (ie4||ns6)
	 target_menu.style.left=target_menu.style.top=-500
      target_menu.widthobj=target_menu.style
      target_menu.widthobj.width=menuwidth
      if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
	 obj.visibility=visible
      else if (e.type=="click")
      obj.visibility=hidden
   }

function iecompattest(){
   return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

/* Rearranges menu to keep within the browser window */
   function clearbrowseredge(obj, whichedge, target_menu){
      var edgeoffset=0
      if (whichedge=="rightedge"){
	 var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
	 target_menu.contentmeasure=target_menu.offsetWidth
	 if (windowedge-target_menu.x-obj.offsetWidth < target_menu.contentmeasure)
	    edgeoffset=target_menu.contentmeasure+obj.offsetWidth
      }
      else{
	 var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
	 var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
	 target_menu.contentmeasure=target_menu.offsetHeight
	 if (windowedge-target_menu.y < target_menu.contentmeasure){ //move menu up?
	    edgeoffset=target_menu.contentmeasure-obj.offsetHeight
	    if ((target_menu.y-topedge)<target_menu.contentmeasure) //up no good either? (position at top of viewable window then)
	       edgeoffset=target_menu.y
	 }
      }
      return edgeoffset
   }

/* builds a menu from the passed menuitems_list array and puts it into the target_menu div */
   function populatemenu(menuitems_list, target_menu){
      if (ie4||ns6) {
	 target_menu.innerHTML=menuitems_list.join("")
      }
   }

/* Wraps up the ugly code and browser-specific conditions for finding the menu with the passed name */
   function getMenuByName(targetMenuName) {
      targetMenu = null;

      // Get the menu DIV by the passed name - if browser doesn't support getElementById, get generic version
      if (document.getElementById) {
	 targetMenu = document.getElementById(targetMenuName) 
      } else {
	 /* this part doesn't seem to do anything */
	    targetMenu = dropmenudiv
      }
      return targetMenu;
   }

/* Builds and displays the menu with the passed contents, width, and name */
function dropdownmenu(obj, e, menucontents, menuwidth, target_menu_name, sub_flag){
   clearhidemenu(target_menu_name);
   target_menu = getMenuByName(target_menu_name);
   hide_other_menus(target_menu_name);
   
   if (window.event) {
      event.cancelBubble=true;
   } else if (e.stopPropagation) {
      e.stopPropagation();
   }
   populatemenu(menucontents, target_menu);
   
   if (ie4||ns6){
      showhide(target_menu.style, e, "visible", "hidden", menuwidth, target_menu);

      target_menu.x=getposOffset(obj, "left");
      target_menu.y=getposOffset(obj, "top");

      // adjust slightly down for sub-menus
      if (sub_flag == 'sub') {
	 target_menu.x -= 3;
	 target_menu.y += 7;
      } else {
	 target_menu.x -= 1;
	 target_menu.y -= 5;
      }
      
      target_menu.style.left=target_menu.x+obj.offsetWidth+horizontaloffset+"px";
      target_menu.style.top=target_menu.y+"px";
   }
   
   return clickreturnvalue();
}

/* returns false if browser compatible - otherwise return true */
function clickreturnvalue(){
   if (ie4||ns6) return false
   else return true
}

/* Called from dropdownmenu() - makes sure to hide other menus based on the passed name */
function hide_other_menus(menu_name) {
   if (menu_name == 'dropmenudiv') {
      top_menu = getMenuByName('dropmenudiv-top');
      hidemenu(top_menu);
      sub_menu = getMenuByName('dropmenudiv-sub');
      hidemenu(sub_menu);
   }
   if (menu_name == 'dropmenudiv-top') {
      regular_menu = getMenuByName('dropmenudiv');
      hidemenu(regular_menu);
   }
}

/* Starts a timer that calls hidemenu on the passed menu after the specified # of miliseconds.
 * This timer is cancelled by clearhidemenu() */
function delayhidemenu(menu_name){
   if (ie4||ns6) {
      delayhide=setTimeout("hidemenu('" + menu_name + "')", disappeardelay)
   }
}

/* Starts a timer like delayhidemenu but stores it in another variable so we can track more than one menu timeout - handy for cascading menus */
function delayhidemenu2(menu_name){
   if (ie4||ns6) {
      delayhide2=setTimeout("hidemenu('" + menu_name + "')", disappeardelay)
   }
}

/* Clears the timer set in delayhidemenu - which cancels the hide operation */
function clearhidemenu(){
   if (typeof delayhide!="undefined") {
      clearTimeout(delayhide)
   }
}

/* Clears the timer set in delayhidemenu2 - which cancels the other hide operation */
function clearhidemenu2(){
   if (typeof delayhide2!="undefined") {
      clearTimeout(delayhide2)
   }
}

/* Hides the specified menu element - you can pass the actual element or the name of the element */
function hidemenu(target_menu){
   if (typeof target_menu != "undefined"){
      if (typeof target_menu == "string") {
	 target_menu_obj = getMenuByName(target_menu);    
      } else {
	 target_menu_obj = target_menu;
      }
      if (ie4||ns6) {
	 target_menu_obj.style.visibility="hidden"
      }
   }
}
   

/* FireBug logging */
function printfire() {
   if (document.createEvent) {
      printfire.args = arguments;
      var ev = document.createEvent("Events");
      ev.initEvent("printfire", false, true);
      dispatchEvent(ev);
   }
}
