var load;

function shockTitle(e)
{
   if(window.event && window.event.srcElement)
   {
      el = window.event.srcElement;
   }
   if(e && e.target)
   {
      el = e.target;
   }
   if(!el)
   {
      el = 'no el';
   }

   loadmsgDiv = document.getElementById('loadmsg');
   if(browserType == 'ie')
   {
      loadmsgIframe = document.getElementById('iframeForIE');
   }
   
   elId = el.id;
   ldMsg = '';
   
   if(elId == 'shock1')
   {
      loadMsg = 'Service factor 0.80';
   }
   else if(elId == 'shock2' || elId == 'shock4')
   {
      loadMsg = 'Service factor 1.00';
   }
   else if(elId == 'shock3' || elId == 'shock5' || elId == 'shock7')
   {
      loadMsg = 'Service factor 1.25';
   }
   else if(elId == 'shock6' || elId == 'shock8' || elId == 'shock10')
   {
      loadMsg = 'Service factor 1.50';
   }
   else if(elId == 'shock9' || elId == 'shock11')
   {
      loadMsg = 'Service factor 1.80';
   }
   else if(elId ==  'shock12')
   {
      loadMsg = 'Service factor 2.00';
   }
   loadmsgDiv.innerHTML = loadMsg;
   loadmsgDiv.style.top = yMousePos +'px';
   loadmsgDiv.style.left = xMousePos + 20 + 'px';
   if(browserType == 'ie')
   {
      loadmsgIframe.style.top = yMousePos + 'px';
      loadmsgIframe.style.left = xMousePos + 20 + 'px';
      loadmsgIframe.style.display = '';
   }
   loadmsgDiv.style.display = '';
}

function clearLoadMsg(e)
{
   document.getElementById('loadmsg').style.display='none';
   if(browserType == 'ie')
   {
      document.getElementById('iframeForIE').style.display='none';
   }
}

function changeShock(e)
{

   if(window.event && window.event.srcElement)
   {
      el = window.event.srcElement;
   }
   if(e && e.target)
   {
      el = e.target;
   }
   if(!el)
   {
      el = 'no el';
   }

   elId = el.id;

   if(!isNaN(elId.substr(elId.length-2,2)))
   {
      idNum = elId.substr(elId.length-2,2);
   }
   else
   {
      idNum = elId.substr(elId.length-1,1);
   }
   
  if(elId.indexOf('Load') == '-1')
  {
     document.getElementById(elId+'Load'+idNum).checked=true;
     document.getElementById('gearsf').value = document.getElementById(elId+'Load'+idNum).value.substr(0,4);
  }
  else
  {
     document.getElementById('gearsf').value = document.getElementById(elId).value.substr(0,4);
  }



}

function submitcheck(e)
{
   if(window.event && window.event.srcElement)
   {
      el = window.event.srcElement;
   }
   if(e && e.target)
   {
      el = e.target;
   }
   if(!el)
   {
      el = 'no el';
   }

   elId = el.id;  
   var hawarning
   var dontsend
   var ptwarning
   var hangle
   dontsend = false
   warning = "no"
   geartypelen = document.hpform.gtype.length	
   hangle = document.hpform.ha.value
   //if(hangle == ''){hangle = 45;}
   stress = document.hpform.stressother.value
   stressother = document.hpform.stressother.value
   dp = document.hpform.dp.value
   fw = document.hpform.fw.value
   rpm = document.hpform.rpm.value
   teeth = document.hpform.teeth.value
   ////////get the mm or inch radio button
   var mmInchesObj = new findRadioBtn(document.hpform.mmInches);
   var mmInches = mmInchesObj.buttonValue;
   ////////get the pitchname value (dp, mod, or cp)
   var pitchnameObj = new findRadioBtn(document.hpform.pitchname);
   var pitchname = pitchnameObj.buttonValue;
   ////////get load load value from the radio button
   var loadObj = new findRadioBtn(document.hpform.load);
   var load = loadObj.buttonValue;
   ////////get pressure angle value from radio button
   var paObj = new findRadioBtn(document.hpform.pa);
   var pa = paObj.buttonValue;

   ////////get gear type value from radio button
   for (counter = 0; counter < geartypelen; counter++)
   {
      if (document.hpform.gtype[counter].checked)
      {
         gtype = document.hpform.gtype[counter].value;
         if (document.hpform.gtype[counter].value == "helical" && hangle =='')
         {
            hawarning = true
         }      
      }
   }
   ////////get pitch type from (transvers or normal dp)
   pitchtypelen = document.hpform.ptype.length;
   for (counter = 0; counter < pitchtypelen; counter++)
   {
      if (document.hpform.ptype[counter].checked)
      {
         ptype = document.hpform.ptype[counter].value;
         if (document.hpform.ptype[counter].value == "TDP" && hangle =='')
         {
            ptwarning = true;
         }      
      }
   }

   ////////var dpitch, tteeth, facew, rpmm, helixx, dontsend
   dpitch = document.hpform.dp.value;
   tteeth = document.hpform.teeth.value;
   facew = document.hpform.fw.value;
   rpmm = document.hpform.rpm.value;
   helixx = document.hpform.ha.value;
   stressother = document.hpform.stressother.value;
   warninginfo = "Please check the following:  \n" ;

   if (isNaN(dpitch) || isNaN(tteeth) || isNaN(facew) || isNaN(rpmm) || isNaN(helixx)) 
   {
      if(dpitch.indexOf('.') != -1 && tteeth.indexOf('.') != -1 && facew.indexOf('.') != -1 && rpmm.indexOf('.') != -1 && helixx.indexOf('.') != -1)
      { 
         warning = "yes";
         warninginfo = warninginfo +  " \n All Data Must Be Numeric";
         dontsend = true;
      }
   }
   if (dpitch == "" || dpitch == "0" )
   {
      if(e.type != 'change' && e.type != 'keyup')
      {
         warning = "yes";
         warninginfo = warninginfo + '\nEnter the ' + document.hpform.pitchId.value;  
         dontsend = true;
      }
   }
   if (tteeth == "" || tteeth == "0" )
   {
      if(e.type != 'change' && e.type != 'keyup')
      {
         warning = "yes";
         warninginfo = warninginfo + "\nEnter the # of Teeth";  
         dontsend = true;
      }
   }
   if (facew == "" || facew == "0" )
   {
      if(e.type != 'change' && e.type != 'keyup')
      {
         warning = "yes";
         warninginfo = warninginfo + "\nEnter the Face Width"  
         dontsend = true
      }
   }
   if (rpmm == "" || rpmm == "0" )
   {
      if(e.type != 'change' && e.type != 'keyup')
      {
         warning = "yes";
         warninginfo = warninginfo + "\nEnter the RPM's"  
         dontsend = true;
      }
   }
   if (hawarning)
   {
      //warning = "yes"
      //warninginfo = warninginfo + "\nEnter the Helix Angle or set gear type to Spur" 
      //dontsend = true
      document.hpform.ha.value = 45;
      hangle = 45;
   }
   if (ptwarning)
   {
      //warning = "yes"
      warninginfo = warninginfo + "\nEnter the Helix Angle or set pitch type to Normal Pitch" 
      dontsend = true
   }
   if (helixx > 89 || helixx == "0")
   {
      warning = "yes"
      warninginfo = warninginfo + "\nA helical gear with a helix angle of 90� or 0� is a spur gear"
      dontsend = true
   }
   if (warning == "yes")
   {
      alert(warninginfo);
   }

   if (!dontsend)
   {
      showHideElement('none','spaceTable');
      showHideElement('results','none');

     getPage('Tech_Tools/hpcalc.php?gtype='+gtype+'&ptype='+ptype+'&stress='+stress+'&teeth='+teeth+'&load='+load+'&ha='+hangle+'&dp='+dp+'&fw='+fw+'&rpm='+rpm+'&pa='+pa+'&pitchname='+pitchname+'&mmInches='+mmInches,'hpResults');
     //getPage('/Tech_Tools/hpcalc.asp','gtype',gtype+'&ptype='+ptype+'&stress='+stress+'&teeth='+teeth+'&load='+load+'&ha='+hangle+'&dp='+dp+'&fw='+fw+'&rpm='+rpm+'&pa='+pa+'&pitchname='+pitchname+'&mmInches='+mmInches);
     //xmlreqGET('/Tech_Tools/hpcalc.asp?gtype='+gtype+'&ptype='+ptype+'&stress='+stress+'&teeth='+teeth+'&load='+load+'&ha='+hangle+'&dp='+dp+'&fw='+fw+'&rpm='+rpm+'&pa='+pa+'&pitchname='+pitchname+'&mmInches='+mmInches);
        
      return true
   }
   else 
   {
      return false
   }
}

function enterKeyResult(keypressed)
{
   if(keypressed == 13)
   {
      submitcheck();
   }
}	
	
function changePitch(e)
{
   if(window.event && window.event.srcElement)
   {
      el = window.event.srcElement;
   }
   if(e && e.target)
   {
      el = e.target;
   }
   if(!el)
   {
      el = 'no el';
   }

   elId = el.id;
   if(elId.substr(elId.length-5,5) == 'Label')
   {
      elId = elId.substr(0,elId.length -5);
   }
   pitchId = document.getElementById(elId).value;
   if(pitchId != 'DP' && !window.XMLHttpRequest && !window.ActiveXObject )
   {
      if(navigator.appName == 'Opera')
      {
         alert('This option will be available with Opera version 7.6');
      }
      else
      {
         alert('You may need to upgrade your browser to the latest version to use part of the calculator')
      }
      document.hpform.pitchname[0].checked = true
   }
   else
   {
      switch(pitchId)
      {
         case "DP":
            document.hpform.pitchId.value = "Diametral Pitch"
            break
         case "MOD":
            document.hpform.pitchId.value = "Module (metric pitch)"
            break
         case "CP":	
            document.hpform.pitchId.value = "Circular Pitch"
            break
         default:
            document.hpform.pitchId.value = "Diametral Pitch"
      }
   }
}

function changestress(e)
{
 if(window.event && window.event.srcElement)
   {
      el = window.event.srcElement;
   }
   if(e && e.target)
   {
      el = e.target;
   }
   if(!el)
   {
      el = 'no el';
   }

   elId = el.id;
   stressval = document.getElementById('stress').value;
   stressname = document.getElementById(elId).name;


   if (stressname =="stress")
   {
      document.hpform.stressother.value = stressval
   }
   else
   {
      document.hpform.stress.value = ""
   }
}
	
function showHideElement(ShowElementName,HideElementName)
{
   if(ShowElementName != 'none')
   {
      ShowElementObj = document.getElementById(ShowElementName).style.display = ''
   }
   if(HideElementName != 'none')
   {
      HideElementObj = document.getElementById(HideElementName).style.display = 'none'
   }
}
	
function changeHelix(e)
{
  if(window.event && window.event.srcElement)
   {
      el = window.event.srcElement;
   }
   if(e && e.target)
   {
      el = e.target;
   }
   if(!el)
   {
      el = 'no el';
   }

   elId = el.id;
   helixAngle = document.getElementById('ha').value;
   if(helixAngle.length > 0)
   {
      document.hpform.gtype[1].checked = true
      showHideElement('helicalimage','spurimage');
   }
   else
   {
      document.hpform.gtype[0].checked = true
      document.hpform.ptype[0].checked = true
      showHideElement('spurimage','helicalimage');
   }
}

////////gather results from the server and display on the screen
var xmlResults = {
	hpResults: function(req){
		window.status += req.readyState+' ';
		if (req.readyState == 4) {
			if (req.status == 200) {
			
				var results = req.responseText;
				allresults = results.split("*");
				for (counter = 0; counter < allresults.length - 1; counter++) {
					torqe = allresults[0];
					stress = allresults[1];
					velocity = allresults[2];
					hp = allresults[3];
					torqeinlbs = allresults[4];
					pd = allresults[5];
					od = allresults[6];
					sf = allresults[7];
				}
				document.hpform.mainload.value = torqe;
				document.hpform.mainstress.value = stress;
				document.hpform.velocity.value = velocity;
				document.hpform.horsepower.value = hp;
				document.hpform.torque.value = torqeinlbs;
				document.hpform.gearpd.value = pd;
				document.hpform.gearod.value = od;
				//document.hpform.gearsf.value = sf;           
			}
			else {
				//alert("There was a problem retrieving the data: " + req.statusText +"\n An alert was just emailed to the staff at Rush Gears.") ;
				/////////put a call to an email xml email page to send an alert that the page is not working properly
			}
		}
	}
}

function radioClick(e)
{
   if(window.event && window.event.srcElement)
   {
      el = window.event.srcElement;
   }
   if(e && e.target)
   {
      el = e.target;
   }
   if(!el)
   {
      el = 'no el';
   }

   elId = el.id;
   if(isNaN(elId.charAt(elId.length-1)))
   {
      checkedItemId = elId;
      idNum = '0';
   }
   else
   {
      idNum = elId.charAt(elId.length-1);
      checkedItemId = elId.substr(0,elId.length-1);
      checkedItemName = 'trans';
   }

   if(checkedItemId == 'spur')
   {
      document.hpform.ha.value='';
      document.hpform.ptype[0].checked=true;
      showHideElement('spurimage','helicalimage');
      document.hpform.gtype[0].checked=true;
   }
   if(checkedItemId == 'helical')
   {
      showHideElement('helicalimage','spurimage');
      document.hpform.gtype[1].checked=true;
      if(idNum == '3' || idNum =='2')
      {
         document.getElementById('helical2').checked=true;
      }
   }
}


   	