var load;

function addListeners(e)
            {
              
                        var typeLoad = $('typeLoad');
						typeLoad.observe('change',submitcheck);
                typeLoad.observe('focus',changeBackColor);
                typeLoad.observe('blur',changeBackColor);
				
                           var pitch = $('pitch');
                    pitch.observe('focus',changeBackColor);
                pitch.observe('blur',changeBackColor);
                pitch.observe('blur',submitcheck);
                pitch.observe('keyup',submitcheck);
				           
						   var pa = $('pa');
					pa.observe('focus',changeBackColor);
					pa.observe('blur',changeBackColor);
					pa.observe('change',submitcheck);
                        
                           var teeth = $('teeth');
                    teeth.observe('focus',changeBackColor);
                teeth.observe('blur',changeBackColor);
                teeth.observe('blur',submitcheck);
                teeth.observe('change',submitcheck);
                teeth.observe('keyup',submitcheck);
                        
                           var fw = $('fw');
                    fw.observe('focus',changeBackColor);
                fw.observe('blur',changeBackColor);
                fw.observe('blur',submitcheck);
                fw.observe('change',submitcheck);
                fw.observe('keyup',submitcheck);
                        
                           var rpm = $('rpm');
                    rpm.observe('foucs',changeBackColor);
                rpm.observe('focus',submitcheck);
                rpm.observe('blur',changeBackColor);
                rpm.observe('blur',submitcheck);
                rpm.observe('change',submitcheck);
                rpm.observe('keyup',submitcheck);
                        
                           var ha = $('ha');
                    ha.observe('focus',changeBackColor);
                ha.observe('blur',changeBackColor);
                ha.observe('blur', submitcheck);
                ha.observe('change',changeHelix);
                ha.observe('keyup',changeHelix);
                ha.observe('keyup',submitcheck);
                        
                           var inchesLabel = $('inchesLabel');
                inchesLabel.observe('click',submitcheck);
                        
                           var inches = $('inches');
                inches.observe('click',submitcheck);
                        
                           var mmLabel = $('mmLabel');
                mmLabel.observe('click',submitcheck);
                        
                           var mm = $('mm');
                mm.observe('click',submitcheck);
                        
                           var stress = $('stress');
                    stress.observe('change',changestress);
                stress.observe('change',submitcheck);
                stress.observe('focus',changeBackColor);
                stress.observe('blur',changeBackColor);
                        
                           var stressOther = $('stressOther');
                    stressOther.observe('change',changestress);
                stressOther.observe('keypress',submitcheck);
                stressOther.observe('focus',changeBackColor);
                stressOther.observe('blur',changeBackColor);
                        
						   var gtype = $('gtype');
					gtype.observe('focus',changeBackColor);
					gtype.observe('blur',changeBackColor);
					gtype.observe('change',submitcheck);
					
                           var spur = $('spur');
                    spur.observe('click', radioClick);
                spur.observe('focus',radioClick);
                spur.observe('click', submitcheck);
                
                        
                        <?php if($browserName == "msie"){?>
                           var computeBtn = $('computeBtn');
                    computBtn.observe('click',submitcheck);
                        <?php }?>
}


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

function changeShock(e)
{
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    
    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') 
    {
        $(elId + 'Load' + idNum).checked = true;
        $('gearsf').value = $(elId + 'Load' + idNum).value.substr(0, 4);
    }
    else 
    {
        $('gearsf').value = $(elId).value.substr(0, 4);
    }
}

function submitcheck(e)
{
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    var hawarning;
    var dontsend;
    var ptwarning;
    var hangle;
    dontsend = false;
    warning = "no";
    geartypelen = document.hpform.gtype.length;
    hangle = $('ha').value;
    //if(hangle == ''){hangle = 45;}
    stress = $('stressOther').value
    stressother = $('stressOther').value;
    dp = $('pitch').value;
    fw = $('fw').value;
    rpm = $('rpm').value;
    teeth = $('teeth').value;
	pitchName = $('pName').value;
	pa = $('pa').value;
    ////////get the mm or inch radio button
    var mmInchesObj = new findRadioBtn(document.hpform.mmInches);
    var mmInches = mmInchesObj.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
    
    
    ////////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)
    
    if ($('ndp').checked) 
    {
        ptype = $('ndp').value;
    }
    else 
    {
        if ($('tdp').checked) 
        {
            ptype = $('tdp').value;
            if (hangle == '') 
            {
                ptwarning = true;
            }
        }
        
        
    }
    /*
     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 = $('dp').value;
    tteeth = $('teeth').value;
    facew = $('fw').value;
    rpmm = $('rpm').value;
    helixx = $('ha').value;
    stressother = $('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
        $('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)
{
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    if (elId.substr(elId.length - 5, 5) == 'Label') 
    {
        elId = elId.substr(0, elId.length - 5);
    }
    pitchId = $(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')
        }
        $('pitchName1').checked = true
    }
    else 
    {
        switch (pitchId)
        {
            case "DP":
                $('pitchId').value = "Diametral Pitch"
                break
            case "MOD":
                $('pitchId').value = "Module (metric pitch)"
                break
            case "CP":
                $('pitchId').value = "Circular Pitch"
                break
            default:
                $('pitchId').value = "Diametral Pitch"
        }
    }
}

function changestress(e)
{
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    stressval = $('stress').value;
    stressname = $(elId).name;
    
    
    if (stressname == "stress") 
    {
        $('stressOther').value = stressval
    }
    else 
    {
        $('stress').value = ""
    }
}

function showHideElement(ShowElementName, HideElementName)
{
    if (ShowElementName != 'none') 
    {
        ShowElementObj = $(ShowElementName).style.display = ''
    }
    if (HideElementName != 'none') 
    {
        HideElementObj = $(HideElementName).style.display = 'none'
    }
}

function changeHelix(e)
{
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    helixAngle = $('ha').value;
    if (helixAngle.length > 0) 
    {
        $('helical').checked = true
        showHideElement('helicalimage', 'spurimage');
    }
    else 
    {
        $('spur').checked = true
        $('ndp').checked = true
        showHideElement('spurimage', 'helicalimage');
    }
}

////////gather results from the server and display on the screen
var xmlResults = 
{
    hpResults: function(req)
    {
        if (req.readyState == 4) 
        {
            if (req.status == 200) 
            {
            
                var results = req.responseText;
                allresults = results.split("*");
                for (counter = 0; counter < allresults.length - 1; counter++) 
                {
                    torque = allresults[0];
                    stress = allresults[1];
                    velocity = allresults[2];
                    hp = allresults[3];
                    torqeinlbs = allresults[4];
                    pd = allresults[5];
                    od = allresults[6];
                    sf = allresults[7];
                }
                $('mainload').value = torque;
                $('mainstress').value = stress;
                $('velocity').value = velocity;
                $('horsepower').value = hp;
                $('torque').value = torqeinlbs;
                $('gearpd').value = pd;
                $('gearod').value = od;
            }
            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)
{
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    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';
    }
    window.status = "id="+checkedItemId;
    if (checkedItemId == 'spur' || checkedItemId == 'ndp') 
    {
        $('ha').value = '';
        $('ndp').checked = true;
        showHideElement('spurimage', 'helicalimage');
        $('spur').checked = true;
    }
    if (checkedItemId == 'helical' || checkedItemId == 'tdp') 
    {
        showHideElement('helicalimage', 'spurimage');
        $('helical').checked = true;
        if (idNum == '3' || idNum == '2') 
        {
            $('helical').checked = true;
        }
    }
}

