// GIY 7/26 per N.Shimabukuro adjusted the cost for Oahu/Maui.
// DTN 7/25/05 per N.Shimabukuro adjusted the cost for Oahu/Maui.

var defaultEmptyOK = false;
var decimalPointDelimiter = "."
var resetflag = false;

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


//  ==========================================
//  1. Compute the Area given the Length and Width of a Room.
//  ==========================================
function ComputeArea(theForm)
  {
  var theField, theLength, theWidth, theArea
  var validLength, validWidth

  if (!isEmpty(document.Calculate.textLength.value) && isInteger(document.Calculate.textLength.value)) {
    validLength = true;
  } else {
    validLength = false; 
  }

  if (!isEmpty(document.Calculate.textLength.value) && isInteger (document.Calculate.textWidth.value)) {
    validWidth = true;
  } else {
    validWidth = false;
  }

  theField =theForm.name;

  switch (theField)
    {
    case 'textLength':

      if (validLength) {
        theLength =  document.Calculate.textLength.value;

      } else {
	alert ("Please enter a positive numerical value.");
	//document.Calculate.textWidth.value = "9";
        document.Calculate.textArea.value = "";
        document.Calculate.textLength.focus();
        return false; 
      }

      if (validWidth) {
	theWidth = document.Calculate.textWidth.value;
	theArea = theLength * theWidth;
	document.Calculate.textArea.value = theArea;
	if (theArea > 500)
	{
		alert("The area of the room is larger than this guide can handle. Check with your dealer for the appropriate Air Conditioner size.")
	}
	document.Calculate.ACType[1].focus();
      } else {
	//alert ("Please enter a valid number.");
	document.Calculate.textWidth.focus();
	return false; 
      }
      break;
    case 'textWidth':
		if (validLength) {
			theLength =  document.Calculate.textLength.value;
 		} else {
			//alert ("a-Please enter a positive numerical value.");
			document.Calculate.textWidth.value = "";
		        document.Calculate.textArea.value = "";
		        document.Calculate.textLength.focus();
		        return false;
		}
		if (validWidth) {
			theWidth = document.Calculate.textWidth.value;
			theArea = theLength * theWidth;
			document.Calculate.textArea.value = theArea;
			if (theArea > 500)
			{
			alert("The area of the room is larger than this guide can handle. Check with your dealer for the appropriate Air Conditioner size.")
			}
			document.Calculate.ACType[1].focus();
		} else {
			alert ("Please enter a postiive numerical value.");
			document.Calculate.textWidth.focus();
			return false;
		}
		break;
    default:
		break;
    }
  ResetBTUs();
  ResetCosts();
  return (true);
  }
//  ==========================================
//  2. Compute the A/C BTUs based on Area and A/C Type
//  ==========================================
function ResetBTUs()
  {
  document.Calculate.textLowBTU.value = "";
  document.Calculate.textHighBTU.value = "";
  return (true);
  }
function ComputeBTU(theForm)
  {
  var theField, theACBTU, theArea, theLow, theHigh, theBTU
  theField =theForm.value;
  //alert("the Field is " + theField);

  theArea = document.Calculate.textArea.value;
  if (theArea == 0)
    {
    alert("Please calculate the Area of the Room (1.)");
    document.Calculate.textLength.focus();
    return (false);
    }

  switch (theField)
    {
    case 'Low':
      theBTU = 35;
      break;
    case 'Medium':
      theBTU = 45;
      break;
    case 'High':
      theBTU = 50;
      break;
    default:
      break;
    }

  theACBTU = theArea * theBTU;
  //alert("the Calculated BTU is " + theACBTU);

  theLow = theACBTU - 500;
 // theLow = theLow - theLow%1000;
  document.Calculate.textLowBTU.value =  theLow;

  theHigh = theACBTU + 500;
 // theHigh = theHigh - theHigh%1000;
  document.Calculate.textHighBTU.value =  theHigh;
  ResetCosts();
  document.Calculate.textDaily.focus();
  return (true);
  }
//  ==========================================
//  3. Compute the Monthly Usage based on Daily Average.
//  ==========================================
function ComputeHours(theForm)
  {

  if (isFloat (document.Calculate.textDaily.value) && (document.Calculate.textDaily.value > 0) && (document.Calculate.textDaily.value <= 24)) {
  var theField, theDaily
  theField =theForm.name;
  //alert("the Field is " + theField);

  theDaily =  document.Calculate.textDaily.value;
  theMonthly = theDaily * 30;
  document.Calculate.textMonthly.value = theMonthly;
  ResetCosts()
  document.Calculate.textIsland[0].focus();
  return (true);
  } else {
    alert ("Please enter a value between 1 and 24.");
    document.Calculate.textDaily.focus();
    return false; 
  }
  }
//  ==========================================
//  4. Compute the Monthly Costs based on Location and EER
//  ==========================================
function ResetCosts()
  {
  document.Calculate.textIsland[0].checked = false;
  document.Calculate.textIsland[1].checked = false;
  document.Calculate.textOther.value = "";  
  document.Calculate.textALow.value = "";
  document.Calculate.textAHigh.value = "";
  document.Calculate.textBLow.value = "";
  document.Calculate.textBHigh.value = "";
  document.Calculate.textCLow.value = "";
  document.Calculate.textCHigh.value = "";
  return (true);
  }
function ComputeCosts(theForm)
  {
  var theField, theArea, theLow, theHigh, theMonthly, theCost
  var theAEER, theBEER, theCEER, theLocation, theCents

  theArea = document.Calculate.textArea.value;
  if (theArea == 0)
    {
    alert("Please calculate the Area of the Room (1.)");
    document.Calculate.textLength.focus();
    return (false);
    }

  theLow = document.Calculate.textLowBTU.value;
  theHigh = document.Calculate.textHighBTU.value;
  if (theHigh == 0)
    {
    alert("Please select an Air Conditioning Need (2.)");
    document.Calculate.ACType[1].focus();
    return (false);
    }

  theMonthly = document.Calculate.textMonthly.value;
  if (theMonthly == 0)
    {
    alert("Please calculate Monthly Usage Hours (3.)");
    document.Calculate.textDaily.focus();
    return (false);
    }
    
  if (!isInteger (document.Calculate.textAEER.value)) {
    alert ("Please enter a positive number.");
    document.Calculate.textAEER.focus();
    return false;
  } 

  if (!isEmpty (document.Calculate.textOther.value)) {
	  if (!isInteger (document.Calculate.textOther.value)) {
	    alert ("Please enter a positive number.");
  	    document.Calculate.textOther.value = "";
	    document.Calculate.textOther.focus();
	    return false;
	  } 
  }

  theField = theForm.name;
  //alert("the Field is " + theField);
  if (theField == 'textAEER')
  	{ 
  	if (document.Calculate.textIsland[0].checked)
  		{
  		theField = 'Oahu';
  		}
  	if (document.Calculate.textIsland[1].checked)
  		{
  		theField = 'Maui';
		}
	if (document.Calculate.textOther.value > 0)
		{
		theField = 'Other'; 
		}
	if (theField == 'textAEER')
   		{
    	alert("Please select a Service Area (4.)");
    	document.Calculate.textIsland[0].focus();
    	return (false);
    	}
	}
  else
  	{
  	theField = theForm.value
  	}

   switch (theField)
    {
    case 'Oahu':
      theCost =  0.299;
      document.Calculate.textOther.value = "";
      break;
      
    case 'Maui':
      theCost =  0.405;
      document.Calculate.textOther.value = "";
      break;
      
    default:
      theCost = document.Calculate.textOther.value/100;
      if (theCost < .01)
      		{
      		theCents = theCost * 10000 + .05;
      		theCents = theCents - (theCents % .1);
      		document.Calculate.textOther.value = theCents;
      		}
      document.Calculate.textIsland[0].checked = false;
      document.Calculate.textIsland[1].checked = false;
      break;
    }

  theAEER = document.Calculate.textAEER.value;
  document.Calculate.textALow.value = ComputeFinal(theLow, theAEER, theMonthly, theCost);
  document.Calculate.textAHigh.value = ComputeFinal(theHigh, theAEER, theMonthly, theCost);

  theBEER = document.Calculate.textBEER.value;
  document.Calculate.textBLow.value = ComputeFinal(theLow, theBEER, theMonthly, theCost);
  document.Calculate.textBHigh.value = ComputeFinal(theHigh, theBEER, theMonthly, theCost);

  theCEER = document.Calculate.textCEER.value;
  document.Calculate.textCLow.value = ComputeFinal(theLow, theCEER, theMonthly, theCost);
  document.Calculate.textCHigh.value = ComputeFinal(theHigh, theCEER, theMonthly, theCost);
  //document.Calculate.textBLow.focus();
  return (true);
  }
//  ==========================================
//  4. Compute the Final Operating Cost.
//    Note: Cost is in dollars per kwh.
//    So divide by 1000 to get dollars per wh.
//	   Caution: Format result as Dollars and Cents
//  ==========================================
function ComputeFinal(theBTUs, theEER, theMonthly, theCost)
  {
  var theFinal, theWatt, theDollar, theA, theB, theC, theCents
  theWatt = theBTUs/theEER;
  theDollar = theCost/1000;
  theFinal = (theWatt*theMonthly*theDollar) + .001;
  theA = new String(theFinal);
  
  //	Check if there's a decimal point. If none, put in .00.
  //	Otherwise pad with 00 and then trim off excess digits
  var regExp = /\./;
  theC = theA.search(regExp);
  if (theC == -1)
  	{
  	theB = '$' + theA + ".00";
  	}
  else
  	{
  	theB = '$' + theA + "00";
  	theC = theB.search(regExp) + 3;
  	theB = theB.slice(0,theC);
  	}
  return (theB);
  }

