function form_submit_old(cAction, cPage)	// grab values and submit form
 {
  document.FORM.txtAction.value=cAction;
  document.FORM.txtNextPage.value=cPage; 
  document.FORM.submit(); 
 }

function form_submit(cAction, cPage)	// grab values and submit form
 {
// alert(top.main.document.FORM.txtClassProcess.value) 

  // find all 'select' list boxes (slst) on form (if main frame is open - could be in child window)
  if (top.main)
  {
   for (i=0; i<top.main.document.FORM.length; i++)
   {
    var tempobj = top.main.document.FORM.elements[i];
    if (tempobj.name.substring(0,4) == "slst")
     {
      // select all items in list box for post
      var count = 0;
      while(count < tempobj.length)
      {
       tempobj.options[count].selected = true;
       count++; 
      }
     }
   } 
  }
  
  document.FORM.txtAction.value=cAction;
  document.FORM.txtNextPage.value=cPage; 
  document.FORM.submit(); 
 }

function OkCancel(cAction, cText) 
 {
  var Answer = confirm(cText);
  if (Answer)
   {
    form_submit(cAction, '');
   } 
 }

function OkCancel2(cAction, cForm, cText) // include form parameter
 {
  var Answer = confirm(cText);
  if (Answer)
   {
    form_submit(cAction, cForm, '');
   } 
 }

function move(fbox,tbox,sortitems) {
//sortitems = 1;  
for(var i=0; i<fbox.options.length; i++) {
if(fbox.options[i].selected && fbox.options[i].value != "") {
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
   }
}
BumpUp(fbox);
if (sortitems) SortD(tbox);

// deselect items in drop-downs - will stay highlighted after move otherwise
for(var i=0; i < fbox.options.length; i++)
 {
  fbox.options[i].selected = false;
 } 
for(var i=0; i < tbox.options.length; i++)
 {
  tbox.options[i].selected = false;
 } 
}
function BumpUp(box)  {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value == "")  {
for(var j=i; j<box.options.length-1; j++)  {
box.options[j].value = box.options[j+1].value;
box.options[j].text = box.options[j+1].text;
}
var ln = i;
break;
   }
}
if(ln < box.options.length)  {
box.options.length -= 1;
BumpUp(box);
   }
}

function Listbox_ItemUp(box)
{
 var cText = "";
 var nSelected = 0;
 
 for (var i = 0; i < box.length; i++)
  { 
   if (box.options[i].selected)
    {nSelected++;} 
  } 

 if (nSelected != 1)
  {alert('An item must be selected (one item at a time).');}
 else
  {
   nNewIndex = box.selectedIndex - 1;

   if (nNewIndex >= 0) 
    {
     cData = box.options[nNewIndex].text;
     box.options[nNewIndex].text = box.options[box.selectedIndex].text;
     box.options[box.selectedIndex].text = cData;

     cData = box.options[nNewIndex].value;
     box.options[nNewIndex].value = box.options[box.selectedIndex].value;
     box.options[box.selectedIndex].value = cData;

     box.selectedIndex = nNewIndex;
    }
  }
}

function Listbox_ItemDown(box)
{
 var cText = "";
 var nSelected = 0;
 
 for (var i = 0; i < box.length; i++)
  { 
   if (box.options[i].selected)
    {nSelected++;} 
  } 

 if (nSelected != 1)
  {alert('An item must be selected (one item at a time).');}
 else
  {
   nNewIndex = box.selectedIndex + 1;

   if (nNewIndex < box.length) 
    {
     cData = box.options[nNewIndex].text;
     box.options[nNewIndex].text = box.options[box.selectedIndex].text;
     box.options[box.selectedIndex].text = cData;

     cData = box.options[nNewIndex].value;
     box.options[nNewIndex].value = box.options[box.selectedIndex].value;
     box.options[box.selectedIndex].value = cData;

     box.selectedIndex = nNewIndex;
    }
  }
}

function SortD(box)  {
var temp_opts = new Array();
var temp = new Object();
for(var i=0; i<box.options.length; i++)  {
temp_opts[i] = box.options[i];
}
for(var x=0; x<temp_opts.length-1; x++)  {
for(var y=(x+1); y<temp_opts.length; y++)  {
if(temp_opts[x].text > temp_opts[y].text)  {
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
      }
   }
}
for(var i=0; i<box.options.length; i++)  {
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
   }
}

function old_add_item(cList)
 {
  if (document.FORM.oPerson_Prefix.value == "Add")
   {	   
    var NewItem = prompt("Enter New Item", "");
	 if (NewItem != null)
	  {
       var no = new Option();
       no.value = NewItem;
       no.text = NewItem;
       document.FORM.oPerson_Prefix.options[document.FORM.oPerson_Prefix.options.length] = no;
//     SortD(document.FORM.oPerson_Prefix);
       document.FORM.oPerson_Prefix.value = NewItem;
      } 
   } 
 }

function add_refitem(cAction, cRefItem)
 {
  var obj = eval('document.FORM.' + cRefItem);

  // check first 3 bytes - expression is longer sometimes (Add a New Organization)
  if (obj.value.substring(0,3) == "Add")
   {	   
	form_submit(cAction, '');
   } 
 }

function form_help(cPage, cHelpUrl)  // method to activate help screens
 {
  // cPage - page user is currently viewing - has a related help page (same name)
  // cHelpUrl - location of help files (femp.nrel.gov/femptracksbeta/femphelp/, etc)

  if (cPage.indexOf('.htm') == -1) // if '.htm' not contained in string, add it -contained in calls to bookmarks
   {
    cPage = cPage + '.htm';
   }

  var cLink = 'http://' + cHelpUrl + cPage;
       
  window.open(cLink,"Help",config='width=800,height=550,scrollbars,resizable')
 }

function uppercase(formvar)  // convert data in a text box to upper case 
{
 formvar.value = formvar.value.toUpperCase();
}

function check_key(event, cAction, cPage)  // check last key pressed - submit form if <enter> key
{
 var keyCode = event.keyCode;
 if (keyCode == 13)  // if user hit enter, submit form
  {
   // BCA 08/09/06 added switch statement to handle special logic for some
   // forms, some forms need extra logic when the form is submitted so they
   // have a specific 'form_submit' function, need to call that function here
   // need to update check_key_and_criteria function too
   switch (cPage) 
    {
     case "ProjectSelect" :
      projectselect_form_submit(cAction, cPage);
      break;
     default :
      form_submit(cAction, cPage);
    }
  }
}

function check_refdata(formvar)
{
 var lerror = false;
 
 // BCA 04/26/07 - realized that " < > can create dislay issues in drop-downs
 // and listboxes, because those characters are part of the 'option' html 
 // (e.g. <OPTION Value="Test">Test), so don't allow those characters into
 // textbox data entry fields that feed drop-downs or listboxes, didn't just
 // use the check_criteria function for this because that functions excludes
 // several characters, some that users may want to use: ( ) ' etc.
 if (formvar.value.indexOf('>') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf('<') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf('"') >= 0)
  {lerror = true;}

 if (lerror == true)
  {
   // put a \ in front of " (to display a " in a string enclosed with "s)
   alert("The following characters can't be used in this field: \" < >");
   formvar.focus();
  } 
}

function check_key_and_criteria(formvar, event, cAction, cPage)  // check last key pressed - submit form if <enter> key
{
 var keyCode = event.keyCode;
 var lerror = false;

 if (keyCode == 13)  // if user hit enter, submit form
  {
   // BCA 11/29/05 - added " as an invalid character, text gets cutoff
   // after the " (noticed this on 'group email' form)
   //
   // BCA 04/24/07 - also not allowing ' ( ) & ; to prevent sql injection issues (see FCComments.doc)
   // BCA 04/25/07 - now not allowing < > to prevent cross-site scripting (<script>)
   if (formvar.value.indexOf('[') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf(']') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf('"') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf("'") >= 0)
    {lerror = true;}
   if (formvar.value.indexOf('(') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf(')') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf('&') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf(';') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf('>') >= 0)
    {lerror = true;}
   if (formvar.value.indexOf('<') >= 0)
    {lerror = true;}

   if (lerror == true)
    {
     // put a \ in front of " (to display a " in a string enclosed with "s)
     alert("The following characters can't be used in this field: [ ] \" ' ( ) & ; < >");
     formvar.focus();
    }
   else
    {
     // BCA 08/09/06 added switch statement to handle special logic for some
     // forms, some forms need extra logic when the form is submitted so they
     // have a specific 'form_submit' function, need to call that function here
     // need to update check_key function too
     switch (cPage) 
      {
       case "ProjectSelect" :
        projectselect_form_submit(cAction, cPage);
        break;
       default :
        form_submit(cAction, cPage);
      }
    } 
  }
}

function check_criteria(formvar)
{
 var lerror = false;
 
 // BCA 11/29/05 - added " as an invalid character, text gets cutoff
 // after the " (noticed this on 'group email' form)
 //
 // BCA 04/24/07 - also not allowing ' ( ) & ; to prevent sql injection issues (see FCComments.doc) 
 // BCA 04/25/07 - now not allowing < > to prevent cross-site scripting (<script>)
 if (formvar.value.indexOf('[') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf(']') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf('"') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf("'") >= 0)
  {lerror = true;}
 if (formvar.value.indexOf('(') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf(')') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf('&') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf(';') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf('>') >= 0)
  {lerror = true;}
 if (formvar.value.indexOf('<') >= 0)
  {lerror = true;}

 if (lerror == true)
  {
   // put a \ in front of " (to display a " in a string enclosed with "s)
   alert("The following characters can't be used in this field: [ ] \" ' ( ) & ; < >");
   formvar.focus();
  } 
}

function check_criteria_onkey(FormObject, event)  // check last key pressed
{
 var FormValue = FormObject.value;
 var nKeyCode = event.keyCode;
 var lShiftKey = event.shiftKey;
 var lRemove = false;

 // don't allow '[' and ']' as criteria on selection forms - creates an error during .Execute()
 //
 // BCA 11/29/05 - added " as an invalid character to the other 'check_criteria'
 // functions (see above), didn't add here though because this function uses
 // the 'keycode' property which returns the same value for uppercase and 
 // lowercase keystrokes (e.g. 222 is returned for ' and "), since ' is a valid
 // keystroke, can't add logic here for " (would have to add nKeyCode = 222 
 // below which would not allow " and '), see more notes about this in the
 // check_criteria_onkeypress function below)
 //
 // BCA 04/24/07 - now not allowing " or ' (so include keycode 222), also not
 // allowing & ( ) ; to prevent sql injection issues (see FCComments.doc), note:
 // by not allowing &, also not allowing 7 (same key code), same issue for ( etc.
 //
 // BCA 04/25/07 - now not allowing < > to prevent cross-site scripting (<script>)
 //
 // BCA 05/17/07 - rosie uses . in filter fields (e.g. searching for email address),
 // . wasn't allowed because it's the lowercase of >, now using the 'shiftKey' property
 // so the entire key isn't excluded just because one of its characters isn't allowed,
 // now > isn't allowed but . is (now excluding exact same characters as check_criteria)
 //
 // [{=219, ]}=221, '"=222, 9(=57, 0)=48, 7&=55, ;:=186, ,<=188, .>=190

 if ( (nKeyCode == 219 || nKeyCode == 221) && lShiftKey == false )
  {lRemove = true;}
 if (nKeyCode == 222)
  {lRemove = true;}
 if ( (nKeyCode == 57 || nKeyCode == 48) && lShiftKey == true )
  {lRemove = true;}
 if (nKeyCode == 55 && lShiftKey == true)
  {lRemove = true;}
 if (nKeyCode == 186 && lShiftKey == false)
  {lRemove = true;}
 if ( (nKeyCode == 188 || nKeyCode == 190) && lShiftKey == true)
  {lRemove = true;}

 if (lRemove)
  {FormObject.value = FormValue.substr(0, (FormValue.length-1));}
}

//function check_criteria_onkeypress(FormObject, event)  // check last key pressed
//{
 // BCA 11/29/05
 // was going to use the check_criteria_onkey function (above) on the 
 // 'group email' form (to not allow entry of "s, text gets cutoff after the ")
 // when tested the function, noticed the " and the ' were not allowed (the 
 // keycode is 222 for both), the cause isn't the check_criteria function, it's 
 // how that function is called from the form, it's called with 'onkeyup', need
 // to use 'onkeypress' to distinguish between uppercase and lowercase for a keystroke
 //
 // found this on a javascript message board...
 // the keyCode property under MSIE returns the character code (the Unicode) 
 // on a onkeypress event while it will return the keyboard character on a 
 // onkeyup regardless if the shift key was also down at that time. 
 //
 // so created this function (with onkeypress in the name) to be clear what
 // it's used for, should update forms to use 'onkeypress' and this function
 // if need to distinguish between uppercase and lowercase in the keystroke
 //
 // note about onkeyup vs. onkeypress - different values are returned for
 // keystrokes (e.g. onkeyup " = 222, onkeypress " = 34)
 //
 // DECIDED NOT TO USE THIS FUNCTION, THE KEYSTROKE VALIDATION DIDN'T WORK
 // WITH ONKEYPRESS LIKE IT DOES WITH ONKEYUP (WITH ONKEYUP, IF AN INVALID
 // KEY IS PRESSED, THE CHARACTER IS REMOVED FROM THE FIELD, THAT DIDN'T 
 // WORK WITH ONKEYPRESS), SO NOW JUST USING ONBLUR ON THE 'GROUP EMAIL'
 // FORM (WITH ONBLUR CAN CHOOSE THE EXACT CHARACTERS NOT TO ALLOW)
 //
// var FormValue = FormObject.value;
// var nKeyCode = event.keyCode;
// don't allow '"' in text fields - text gets cutoff after the "
// if (nKeyCode == 34) 
//  {FormObject.value = FormValue.substr(0, (FormValue.length-1));}
//}

function completed_reports() // launch the completed reports window
{
// window.open('CompletedReports.wp?cSessId=' + document.FORM.txtSessId.value + '&cClassProcess=' + document.FORM.txtClassProcess.value,'CompletedReports','width=800,height=370,scrollbars,resizable,menubar=no,toolbar=no');
 window.open('CompletedReports.wp?cSessId=' + document.FORM.txtSessId.value + '&cClassProcess=Main','CompletedReports','width=800,height=370,scrollbars,resizable,menubar=no,toolbar=no');
}

function completed_reports_new() // launch the new completed reports window
{
 window.open('CompletedReportsNew.wp?cSessId=' + document.FORM.txtSessId.value + '&cClassProcess=Main','CompletedReports','width=800,height=370,scrollbars,resizable,menubar=no,toolbar=no');
}

function messageWindow(cMessage, winWidth, winHeight, cAction, cPage) 
{
 // function to open a message window when a page is being refreshed
 // window closes using onBlur (when next page gets focus, this window closes -
 // need to put messageClear() in the <body onload> of next page)

 winTop = (screen.availWidth-winWidth)/2;		// used to center window
 winLeft = (screen.availHeight-winHeight)/2;	// used to center window

 msgWindow = window.open('', 'msgWindow', 'resizable=yes,location=no,menubar=no,toolbar=no,top='+winTop+',left='+winLeft+',width='+winWidth+',height='+winHeight);

 var doc = msgWindow.document;
 doc.open();
 doc.write("<link rel='stylesheet' type='text/css' href='femp.css'>")
 doc.write("<body onBlur='self.close()' bgcolor='#FFFFCC'><p><center>" + cMessage + "</center><br><center>Please Wait...<center></body>");
 doc.close();
 doc.title = 'FEMP Central';

 // setTimeout("msgWindow.close()", 1000); // close the window in 1 second

 document.FORM.txtMessageWindow.value = 'Yes'
 form_submit(cAction, cPage);
}

function messageClear()  // clear msgWindow - see explanation in MessageWindow()
{
 self.focus()
}

function contactsgroup_form_submit(cAction, cPage)  // adding or removing contacts from group
{
 if (cPage == 'AddContactsToGroup')
  {var groupdd = eval('document.FORM.cboGroup1');}
 else
  {var groupdd = eval('document.FORM.cgroup_group_seq');}
 
 if (groupdd.value == '')
  {alert('A Group must be selected.');}
 else
  {
   var i = 0;
   var lSelected = false;
   while (i < document.FORM.lstContacts.length)
    {
     if (document.FORM.lstContacts.options[i].selected)
      {lSelected = true;}
     i++; 
    }

// if (document.FORM.lstContacts.length > 0)
   if (lSelected)
    {form_submit(cAction, cPage);}
   else
    {alert('At least one Contact must be selected.');}
  } 
} 

function selectcontacts()  // select contacts in add/remove contacts from group forms
{
 move(document.FORM.lstContacts, document.FORM.slstSelContacts,1);	
 document.FORM.txtAvailContacts.value = document.FORM.lstContacts.length; 
 document.FORM.txtSelContacts.value = document.FORM.slstSelContacts.length; 
}

function removecontacts() // remove contacts in add/remove contacts from group forms
{
 move(document.FORM.slstSelContacts, document.FORM.lstContacts,1);	
 document.FORM.txtAvailContacts.value = document.FORM.lstContacts.length; 
 document.FORM.txtSelContacts.value = document.FORM.slstSelContacts.length; 
}

function deselect_listbox(nbox)   // deselect all items in list box
{
 for(var i=0; i < nbox.options.length; i++)
  {
   nbox.options[i].selected = false;
  } 
}

function DateEntry(DateObject, event, mmyyyy)	// date entry logic
{
 //  mmyyyy passes as true if date entry is just month and year
 if (!mmyyyy) mmyyyy = false	// if undefined, set to false

 var DateValue = DateObject.value;
 var keyCode = event.keyCode;
 var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";

 // characters not allowed in date field
 if (alphaCheck.indexOf(DateValue) >= 0)
 {
  DateObject.value = DateValue.substr(0, (DateValue.length-1));
 }

 // if last key pressed was a number (keyboard or keypad), check length and add '/'
 if ((keyCode >= 48 && keyCode <= 57) || (keyCode >=96 && keyCode <= 105))  
  {
   if (DateValue.length == 2)
    {
     DateObject.value = DateValue + '/';
    }
   if (!mmyyyy && DateValue.length == 5)
    {
     DateObject.value = DateValue + '/';
    }
  }
}

function NumericEntry(NumObject, event)	// numeric entry validation (keyboard or keypad) - also allow backspace (8), tab (9), enter (13), '+' (107), '-' (109,189)
{
 var NumValue = NumObject.value;
 var nKeyCode = window.event.keyCode;

 if ( (nKeyCode > 47 && nKeyCode < 58) || (nKeyCode > 95 && nKeyCode < 106) || nKeyCode == 8 || nKeyCode == 9  || nKeyCode == 13 || nKeyCode == 107 || nKeyCode == 109 || nKeyCode == 189); 
 else
  {
   NumObject.value = NumValue.substr(0, (NumValue.length-1));
  }
}

function CheckDate(DateToCheck)	// check for valid date (mm/dd/yyyy)
{
 var month = DateToCheck.substring(0,2) - 1;	// months start at 0 in new Date()
 var day = DateToCheck.substring(3,5);
 var year = DateToCheck.substring(6,10);

 myDate = new Date(year, month, day);

 // getDate returns the day of myDate - if myDate is invalid, getDate != day
 if ( (myDate.getDate() != day) || (year.length != 4) || (month > 11) || (month < 0) )  {return false;}
 else {return true;}
}

function CheckDateFuture(DateToCheck)	// check for future date (mm/dd/yyyy)
{
 var month = DateToCheck.substring(0,2) - 1;	// months start at 0 in new Date()
 var day = DateToCheck.substring(3,5);
 var year = DateToCheck.substring(6,10);

 var myDate = new Date(year, month, day);
 var today = new Date();
 
 if (myDate > today)
  {return true;}
 else
  {return false;}
}

function menu_form_submit(cAction, cPage)
{
// need to change values in 'main' frame if a menu option has been selected
// otherwise action/txtClassProcess will be wrong (current form values, contactselect, etc.)
 top.main.document.FORM.action = 'Main.wp';	// change action to 'Main.wp', look for this in webprocess when calling process class
// top.main.document.FORM.txtClassProcess.value = 'Main';  // don't change txtClassProcess, back button won't (txtClassProcess set to 'Main')

 form_submit(cAction, cPage);
// top.location.href=top.main.location.href
}

function sendmail(cTo)
{
 if (cTo == '')
  {
   alert('E-mail Address is blank.');
  }
 else
  {
   window.open('mailto:' + cTo);
  } 
}

function padright(cString, nLen, cPad)  // pad string with spaces
{
 if (!cPad) cPad = ' '	// set default

 var nDiff = nLen - cString.length;
 for(var i=0; i < nDiff; i++)
  {
   cString = cString + cPad ; 
  }
 return cString;
}

function padleft(cString, nLen, cPad)  // pad string with spaces
{
 if (!cPad) cPad = ' '	// set default

 var nDiff = nLen - cString.length;
 for(var i=0; i < nDiff; i++)
  {
   cString = cPad + cString ; 
  }
 return cString;
}

function trim(string)  // trim spaces from string (left and right)
{
 return string.replace(/(^\s+)|(\s+$)/g, '');
}

function stripchr(string)	// remove chr(160)'s (used for spacing)
{
 var temp1 = escape(string);               // convert string to ascii codes
 var temp2 = temp1.replace(/%A0/g, " ");   // replace all occurences of %AO chr(160) with spaces
 return trim(unescape(temp2));
}

function stripnumeric(value)	// strip out '$' and ','s 
{
 // strip '$' - needs to be stripped as ascii code (%24)
 var temp1 = escape(value);           
 var temp2 = temp1.replace(/%24/g,'');   
 value = unescape(temp2);

 // strip ','
 value = value.replace(/,/g,'')

 return value
}

function mbtu_calc(FormulaList)
{
 // use java script array (FormulaList) contained on sespcecm form for commodity conversion
 // convert all commodities on form then get total

 // commodity 1 conversion

 var nSeq = Number(document.FORM.cboCommodity1.value.substring(0,10));
 var nMbtu1 = 0;
 for(var i=1; i < FormulaList.length; i++) 
  {
   if (FormulaList[i].commod_seq == nSeq)  // search array for commodity by seq #
    {
     var nUnits = Number(stripnumeric(document.FORM.txtComm1Sav.value));
     cFormula = FormulaList[i].formula;
     if (cFormula == '')
      {nMbtu1 = nUnits;}  // default
     else
      {nMbtu1 = eval(cFormula);}  // use formula for conversion
    } 
  }

 // commodity 2 conversion
 var nSeq = Number(document.FORM.cboCommodity2.value.substring(0,10));
 var nMbtu2 = 0;
 for(var i=1; i < FormulaList.length; i++) 
  {
   if (FormulaList[i].commod_seq == nSeq)  // search array for commodity by seq #
    {
     var nUnits = Number(stripnumeric(document.FORM.txtComm2Sav.value));
     cFormula = FormulaList[i].formula;
     if (cFormula == '')
      {nMbtu2 = nUnits;}  // default
     else
      {nMbtu2 = eval(cFormula);}  // use formula for conversion
    } 
  }

 // commodity 3 conversion
 var nSeq = Number(document.FORM.cboCommodity3.value.substring(0,10));
 var nMbtu3 = 0;
 for(var i=1; i < FormulaList.length; i++) 
  {
   if (FormulaList[i].commod_seq == nSeq)  // search array for commodity by seq #
    {
     var nUnits = Number(stripnumeric(document.FORM.txtComm3Sav.value));
     cFormula = FormulaList[i].formula;
     if (cFormula == '')
      {nMbtu3 = nUnits;}  // default
     else
      {nMbtu3 = eval(cFormula);}  // use formula for conversion
    } 
  }

 // commodity 4 conversion
 var nSeq = Number(document.FORM.cboCommodity4.value.substring(0,10));
 var nMbtu4 = 0;
 for(var i=1; i < FormulaList.length; i++) 
  {
   if (FormulaList[i].commod_seq == nSeq)  // search array for commodity by seq #
    {
     var nUnits = Number(stripnumeric(document.FORM.txtComm4Sav.value));
     cFormula = FormulaList[i].formula;
     if (cFormula == '')
      {nMbtu4 = nUnits;}  // default
     else
      {nMbtu4 = eval(cFormula);}  // use formula for conversion
    } 
  }

 if (nMbtu1+nMbtu2+nMbtu3+nMbtu4 > 99999999)
  {alert('Energy Unit Savings Total is too large for the field.');}
 else 
  // BCA 08/01/05 - only update field if 'commodity' savings != 0 (otherwise may overwrite data that has been entered)
  if (nMbtu1+nMbtu2+nMbtu3+nMbtu4 != 0)
   {document.FORM.txtEnergyTot.value = nMbtu1+nMbtu2+nMbtu3+nMbtu4;}

 var nSave1 = document.FORM.txtComm1DolSav.value;
 if (!nSave1) nSave1 = 0	// if undefined, set to 0
 var nSave2 = document.FORM.txtComm2DolSav.value;
 if (!nSave2) nSave2 = 0	// if undefined, set to 0
 var nSave3 = document.FORM.txtComm3DolSav.value;
 if (!nSave3) nSave3 = 0	// if undefined, set to 0
 var nSave4 = document.FORM.txtComm4DolSav.value;
 if (!nSave4) nSave4 = 0	// if undefined, set to 0
 
 // strip out '$' and ','s - '$' needs to be stripped as ascii code (%24)
 nSave1 = Number(stripnumeric(nSave1));
 nSave2 = Number(stripnumeric(nSave2));
 nSave3 = Number(stripnumeric(nSave3));
 nSave4 = Number(stripnumeric(nSave4));

 if (nSave1+nSave2+nSave3+nSave4 > 99999999)
  {alert('Energy Dollar Savings Total is too large for the field.');}
 else
  // BCA 08/01/05 - only update field if 'commodity' savings != 0 (otherwise may overwrite data that has been entered)
  if (nSave1+nSave2+nSave3+nSave4 != 0)
   {document.FORM.txtEnergyTotDol.value = nSave1+nSave2+nSave3+nSave4;}

// savings_calc(); // updated total savings field
}
    
function user_feedback() // launch the user feedback window
{
 if (top.contents.winUFb != null)
 {
  top.contents.winUFb.focus();
 }
 else
 {
  top.contents.winUFb = window.open('UserFeedback.wp?cSessId=' + document.FORM.txtSessId.value + '&cClassProcess=Main&cForm=' +  top.main.document.FORM.txtCurrPage.value,'UserFeedback','width=800,height=525,scrollbars,resizable,menubar=no,toolbar=no');
 }
}

function logoffcheck(cAction, cPage) 
{
 if (top.contents.winUFb != null)
 {
  var answer = confirm('The User Feedback window is open, Log Off anyway?');
  if(answer == true)
  {
   form_submit(cAction, cPage);
   top.contents.winUFb.close();
  } 
  else
  {
   top.contents.winUFb.focus();
  }
 }
 else
 {
  form_submit(cAction, cPage);
 }
}
function calculator(formvar)
{
 window.open('calculator.htm?name=' + formvar.name,'Calculator','width=230,height=220,scrollbars,resizable,menubar=no,toolbar=no');
 formvar.focus();
}

function MaxEntry(EntryObject, MaxLen)
{
 if ( EntryObject.value.length > MaxLen ) 
  {  
   alert('Entry for this field cannot exceed ' + MaxLen + ' characters.');
   EntryObject.focus();
  }
}

function numericFormat(fld, lDol, lDec, maxLen)
{
 var txtNumber = fld.value;

 // strip out '$' and ','s and then reformat below - '$' needs to be stripped as ascii code (%24)
 var temp1 = escape(txtNumber);           
 var temp2 = temp1.replace(/%24/g, '');   
 txtNumber = unescape(temp2);
 txtNumber = txtNumber.replace(/,/g,'')

 // check for valid number
 if (isNaN(txtNumber))
 {
  alert("You have entered an invalid number.");
  fld.select();
  fld.focus();
  return;
 }

 // check for decimal in number - lDec needs to be true to include decimals
 if ( txtNumber.indexOf('.') >= 0 )
 {
  if (lDec)
  {
   nDec = txtNumber.substring(txtNumber.indexOf('.')) // e.g. '.45'

   // e.g. change .2 to .20 (expecting two decimal places in logic below)
   if (nDec.length == 2) {txtNumber = txtNumber + '0'}

   if (nDec.length > 3)
   {
    alert("You have entered too many decimal places.");
    fld.select();
    fld.focus();
    return;
   }
  }
  else
  {
   alert("Decimal places not allowed for this number.");
   fld.select();
   fld.focus();
   return;
  }
 }
 else
 {
  // e.g. change 10 to 10.00
  if (lDec && txtNumber.length > 0) {txtNumber = txtNumber + '.00'}
 } 
 
 // check length - just numbers not including decimals - '$,' stripped out above
 nLen = txtNumber.length
 if (txtNumber.indexOf('.') >= 0)
 {
  nLen = nLen - 3
 }
 if (nLen > maxLen)
 {
  alert("The number have entered is too large.");
  fld.select();
  fld.focus();
  return;
 }

 var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
 var arrNumber = txtNumber.split('.');
 arrNumber[0] += '.';
 do
 {
  arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
 }
 while (rxSplit.test(arrNumber[0]));

 if (arrNumber.length > 1)
 {
  val = arrNumber.join('');
 }
 else
 {
  val = arrNumber[0].split('.')[0];
 }

 // add '$' if lDol is true
 if (val != '' && lDol)
  {
   fld.value = '$' + val
  }
 else
  {
   fld.value = val
  }  
}

function numericFormatTest(fld, lDec, lDol, thousSep, decSep, maxLen, e)
{
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
len = fld.value.length;

for(i = 0; i < len; i++)
 {
  if ( (fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep) && (fld.value.charAt(i) != '$') ) 
   {
    break;
   } 
 }

aux = '';
for(; i < len; i++)
 {
  if (strCheck.indexOf(fld.value.charAt(i))!=-1)
   {
    aux += fld.value.charAt(i);
   } 
 }

aux += key;
len = aux.length;

if (len == maxLen + 1) return false;  // Not a valid key
if (len == 0) fld.value = '';

if (lDec)
 {
  if (len == 1) fld.value = '0'+ decSep + '0' + aux;
  if (len == 2) fld.value = '0'+ decSep + aux;
  alen = 2
 }
else
 {
  if (len == 1) fld.value = aux;
  if (len == 2) fld.value = aux;
  alen = 0
 }
  
if (len > alen)
 {
  aux2 = '';

  if (lDec)
   {
    blen = len - 3
   }
  else
   {
    blen = len - 1
   }

  for (j = 0, i = blen; i >= 0; i--)
   {
    if (j == 3)
     {
      aux2 += thousSep;
      j = 0;
     }
    aux2 += aux.charAt(i);
    j++;
   }

  fld.value = '';
  len2 = aux2.length;
  for (i = len2 - 1; i >= 0; i--)
  fld.value += aux2.charAt(i);

  if (lDec) 
   {
    fld.value += decSep + aux.substr(len - 2, len);
   } 
 }

 if (lDol) 
  {
   fld.value = '$' + fld.value
  } 

return false;
}

function GenerateReportMessage(cAction, cPage)
 {
  alert("A new window will be opened to display completed reports. This window will be refreshed automatically and will display reports when they're finished and ready for viewing. This window will not open automatically if Pop-up Blocker software is being used. In that case, it can be opened manually be clicking the Completed Reports option in the menu.");
 }

function ViewModProj()
 {
  window.open('RepPrevProjectView.wp?project=RepPrev&cSessId=' + document.FORM.txtSessId.value + '&page=' + document.FORM.txtCurrPage.value + '&seq=' + document.FORM.txtModProjSeq.value + '&code=1' + '&cClassProcess=' + document.FORM.txtClassProcess.value,'ProjectRepPrev','width=800,height=700,scrollbars,resizable,location=no,menubar=no,toolbar=no')
 }

function ViewFacility()
{
 var i = 0;
 var selectedfac = 0

 while (i < document.FORM.slstFac.length)
  {
   if (document.FORM.slstFac.options[i].selected)
    {var selectedfac = document.FORM.slstFac.options[i].value.substring(0,6);}
   i++; 
  }

 if (selectedfac == 0)
  {alert('A Facility must be selected.');}
 else
  {window.open('FacilityView.wp?cClassProcess=' + document.FORM.txtClassProcess.value + '&cSeq=' + selectedfac + '&cSessId=' + document.FORM.txtSessId.value,'Facility','width=800,height=480,scrollbars,resizable,menubar=no,toolbar=no');}
}

function ismaxlength(obj)
{
 //******************************************************************
 // Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
 // This notice must stay intact for legal use.
 // Visit http://www.dynamicdrive.com/ for full source code
 //******************************************************************
 var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
 if (obj.getAttribute && obj.value.length>mlength)
 obj.value=obj.value.substring(0,mlength)
}
