var storeValue=new Array;//stores a field value in case it needs to be restored
var currentname;//stores the name of the field to be restored
var currentBox;//refer to the current input box being checked
var indexNum;//index value (in the forms.elements array) for the currentBox being looked at
var workSheetValue;//variable to save the worksheet generated output
var workSheetName;//variable to save the name of the textbox where the output goes
var hasSpace;//variable used in detecting spaces in number entries
var endValidate=0;//variable used by inner functions to tell Validate to return
//**ERROR MESSAGES TO BE DISPLAYED BY FUNCTION ERRORHANDLER**//
var error0="** Input error found **\n\n";//Default header error message
var error1="Please enter a whole number.";
var error2="The calculator bases its calculations on a 30 day month.\nPlease enter an hour per month value up to 720 hours.";
var error3="Please make sure you input your rate in cents.";
var error4="Please enter a rate greater than zero.";
var error5="Your entry contains a space.\nPlease make sure your input is numbers only.";
var error6="The value must be a number.\nPlease enter another value.";
var error7="You must enter a positive value.";//default footer error message
var errorEnd="\n\nYour entry will be reset to its previous value."




function openWindow(url,title){//used for the hot water worksheet
var worksheet=window.open(url,title, 'width=530,height=560,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=no,left=10,top=10,screenX=10,screenY=10');
}//end doworksheet

function openWindow2(url,title){//used for other window openings
var worksheet=window.open(url,title, 'width=760,height=500,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=no,left=10,top=10,screenX=10,screenY=10'); 
}//end doworksheet

function openWindow3(url,title){//used for other window openings
var worksheet=window.open(url,title, 'width=760,height=500,directories=no,location=yes,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=no,left=10,top=10,screenX=10,screenY=10');
}//end doworksheet

function saveValue(){
	for(var i=0;i<document.energycalc.length;i++)
	storeValue[i]=document.energycalc.elements[i].value;}
function setFocus(boxname){boxname.focus();boxname.select();}

function errorHandler(whichError){
	setTimeout("setFocus(currentBox);",0);
	eval('setTimeout("alert(error0+'+whichError+'+errorEnd);",1500);');
	setTimeout("currentBox.value=storeValue[indexNum];",1600);
	setTimeout("currentBox.select();",2000);
}//end errorHandler

function checkQuantityValue(quantityNumber){
var hasDecimal=0;
for(var i=0;i<=quantityNumber.length-1;i++){
	if(quantityNumber.charAt(i)==".")hasDecimal=1;}
	if(hasDecimal==1){errorHandler('error1');endValidate=1;}
}//end checkQuantityValue

function checkHoursValue(hoursNumber){
	if(parseInt(hoursNumber)>720){errorHandler('error2');endValidate=1;}
}//end checkHoursValue

function checkCustomerRate(inputRate){
	if(inputRate.charAt(0)=='.'){errorHandler('error3');endValidate=1;}//if input start w/ a decimal
	if(inputRate==0){errorHandler('error4');endValidate=1;}//if they enter a rate of zero
}//end checkCustomerRate

function foundSpace(){
if(hasSpace==0){errorHandler('error5');hasSpace=1;}
}//end foundSpace

function worksheetChecker(box){
if(box.checked==true){
        openWindow('worksheet.html','worksheet');
	//alert('You haven\'t done the worksheet yet!\nIf you have an electric hot water heater, please take the time to fill out the worksheet.\nWhen you finish, this box will be checked off for you.\nIf you don\'t have an electric hot water heater, this check box doesn\'t apply to you.');
	//box.checked=false;
}
else if(box.checked==false){
	alert('You already finished the worksheet!\nPlease don\'t uncheck the reminder.');
	box.checked=true;}
}//end worksheetChecker

function workSheetSave(boxname){workSheetName=boxname;workSheetValue=boxname.value;}
function protectWorksheetOutput(){
alert("Please click the 'Go!' button if you want to add information\nfor your electric hot water heater or if you want to change\nthis value. If you want to erase this value, please open the\nworksheet and press 'cancel'.");
workSheetName.value=workSheetValue;
//setTimeout('setFocus(workSheetName);',0);
}//end protectWorksheetOutput

function validateForm(){
window.status="Processing your information...";
var formLength=document.energycalc.length;
endValidate=0;

for(var q=0;q<formLength;q++){//check all the input boxes
	currentBox=document.energycalc.elements[q];
	indexNum=q;//update the global variable so other functions can reference the current index
	if((currentBox.type=='text')&&(currentBox.name.charAt(1)!="a")){
	//if it's a textbox and it is not the appliance name for the misc catagory
		hasSpace=0;//each time you check a field, start with no spaces found
		if(isNaN(currentBox.value)){//check if the input is a number
			errorHandler('error6');
			return;}//if the entry isn't a number, don't bother testing further
		if(parseInt(currentBox.value)<0||currentBox.value=="-0"){//check for negative #
			errorHandler('error7');
			return;}//if the entry is negative, don't bother testing further
		for(var i=0;i<=currentBox.value.length-1;i++){
			if(currentBox.value.charAt(i)==" "){foundSpace();return;}//check for spaces
			if(currentBox.name!='customerRate'){//if the box is a qty, watt, or hour:
				for(var j=0;j<=currentBox.name.length-1;j++){//test the type and values
					if(currentBox.name.charAt(j)=="q"){
						checkQuantityValue(currentBox.value);
						if(endValidate==1)return;}
					else if(currentBox.name.charAt(j)=="h"){
						checkHoursValue(currentBox.value);
						if(endValidate==1)return;}
				}//end for
			}//end if the box is a qty, watt, or hour
			else if(currentBox.name=='customerRate'){
				checkCustomerRate(currentBox.value);
				if(endValidate==1)return;}
		}//end for
	}//end if currentBox.type=='text'
}//end for
//if the function gets to this point, then all the input boxes are OK so send in the input
document.forms.energycalc.submit();
}//end validateForm
