function isValid(value)
{
    // value must not be blank
    if(value == "")
    {
        return false;
    }
    // value must be a number
    if(isNaN(value))
    {
        return false;
    }
    // value must > zero
    if(value <= 0)
    {
        return false;
    }
    return true;
}

function isValid(value, allowZero, allowNegative)
{
    // value must not be blank
    if(value == "")
    {
        return false;
    }
    // value must be a number
    if(isNaN(value))
    {
        return false;
    }
    // check for zero
    if(!allowZero && value == 0)
    {
        return false;
    }
    // check for negative
    if(!allowNegative && value < 0)
    {
        return false;
    }
    return true;
}

function resetCalculator(calculator)
{
    var fieldNames;
    // build up a list of field names for the selected calculator
    switch(calculator)
    {
        case "shortTermBend":
            fieldNames = new Array("TBdiameter", "TBbendRadius");
		    break;
	    case "longTermBend":
	        fieldNames = new Array("TBdiameterLong", "TBbendRadiusLong");
		    break;
	    case "numericalAperture":
	        fieldNames = new Array("TBnumAp", "TBcoreIndex", "TBcladIndex");
		    break;
	    case "temperatureConversion":
	        fieldNames = new Array("TBdegreesF", "TBdegreesC");
		    break;
	    case "metricConversion":
	        fieldNames = new Array("TBcentimeters", "TBmeters", "TBinches", "TBfeet");
		    break;
	    case "bendRadius":
	        fieldNames = new Array("TBappliedStress", "TBfiberRadius", "TBbndRadius", "TBcoatingThickness");
		    break;
	    case "laserPower":
	        fieldNames = new Array("TBenergy", "TBfiberDiameter", "TBpulseDuration");
		    break;
		default:
		    fieldNames = new Array();
		    break;
    }
    // now loop through the fields in the array and reset each one's value to blank
    for(var i=0;i<fieldNames.length;i++)
    {
        document.getElementById(fieldNames[i]).value = "";
    }
}

function calculatorVisible(isVisible, calculator)
{
    // before showing a calculator, be certain it's fields are empty
    if(isVisible)
    {
	    resetCalculator(calculator);
    }
    // now either show or hide the requested calculator.  "block" display shows it and "none" hides it.
    document.getElementById(calculator).style.display = isVisible==true?"block":"none";
}

function resetPowerMode()
{
    document.getElementById("topHat").checked = true;
}

function powerMode(mode)
{
    // "block" display makes the <tr> visible and "none" hides it
    document.getElementById("modeField").style.display = mode=="singleMode"?"block":"none";
    document.getElementById("coreArea").style.display = mode=="topHat"?"block":"none";
    resetCalculator("laserPower");
}

function hideAllCalculators()
{
    var calculators = new Array("shortTermBend", "longTermBend", "numericalAperture", "temperatureConversion", 
        "metricConversion", "bendRadius", "laserPower");
    // loop through names of all the calculators in the array and hide each one
    for(var i=0;i<calculators.length;i++)
    {
        calculatorVisible(false, calculators[i]);
    }
}

function buildMessage(requiredCount, calculatedCount, allowZero, allowNegative)
{
    var rqdValue = requiredCount > 1?" numeric values":" numeric value";
    var calcValue = calculatedCount > 1?" values":" value";
    var message = "You must provide " + requiredCount;
    if(!allowNegative)
    {
        message += " positive";
    }
    if(!allowZero)
    {
        message += " non-zero";
    }
    message += rqdValue;
    message += ". The calculator will find the missing" + calcValue + " for you.";
    return message;
}

function showMessage(message)
{
    // get a reference to the message field
    var messageField = document.getElementById("message");
    // write the message.  This is appending so that two or more messages could be displayed at a time.
    messageField.innerHTML += "<br/>" + message;
}

function clearMessage()
{
    // get a reference to the message field
    var message = document.getElementById("message");
    // clear the message;
    message.innerHTML = "";
}

function showCalculator()
{
    // find out which calculator is to be shown by getting the value of the dropdown
    var calculator = document.getElementById("calculationType").value;
    // hide all the calculators
    hideAllCalculators();	
    // ignore the --please select-- option in the dropdown and show any other selection
    if(calculator != "none")
    {		
        calculatorVisible(true,calculator);
    }
}

function setPrecision(value, decimals)
{
    // since Math.round returns an integer, we must do some hand-waving to return more decimals
    var multiplierString = new String("1");
    // append zeros until we get the right multiplier for the desired precision
    for(var i=0;i<decimals;i++)
    {
        multiplierString += "0";
    }
    // convert the multiplier string to a numeric value
    var multiplier = new Number(multiplierString);
    // multiply the base by the multipier and then round.  Divide afterward so only the desired decimals are returned.
    return Math.round(value * multiplier) / multiplier;
}

