
/*/
	File:  global.js
	© 2001 Locke Enterprises - All rights reserved.
	Purpose:  Export some of Nathan's stock javascript routines.
	Created:  5/8/2001 - Nathan
	Modification History:
		· 5/29/2001 - Nathan - added getSelectedButtonValue() and checkLength()
		· 1/3/2004 - Nathan - added getSelectedButtonValue() and checkLength()
	Exports: getSelectedButtonIndex(), getSelectedButtonValue(), getSelectedOptionIndex(), getSelectedOptionValue(), checkLength()
/*/


//-------------------------------------------------------------------------
// getSelectedButtonIndex:  Returns the index of the selected option button.
// NOTE:  Can only be used on a group of more than one objects.
//-------------------------------------------------------------------------
function getSelectedButtonIndex( buttonGroup ) {

	if( typeof(buttonGroup.length)!="undefined" ) {
		for( var i=0; i<buttonGroup.length; i++) {
			if( buttonGroup[i].checked ) {
				return i;
			}
		}
	} else {
		return -2;
	}
	return -1;

} // getSelectedButtonIndex()


//-------------------------------------------------------------------------
// getSelectedElementValue:  Returns the value of the selected option button.
//-------------------------------------------------------------------------
function getSelectedElementValue( theElement ) {

	if( typeof(theElement.length)!="undefined" ) {
		var theIndex = getSelectedButtonIndex(theElement);
		if( theIndex == -1 ) return null;
		return theElement[theIndex].value;
	} else {
		if( theElement.checked==true ) {
			return theElement.value;
		} else {
			return null;
		}
	}

} // getSelectedElementValue()


//-------------------------------------------------------------------------
// getSelectedOptionIndex:  Returns the index the first selected option item in a <select> combobox.
//-------------------------------------------------------------------------
function getSelectedOptionIndex( optionList ) {

	if( typeof(optionList.length)=="undefined" ) return -2;
	for( var i=0; i<optionList.length; i++) {
		if( optionList[i].selected ) {
			return i;
		}
	}
	return -1;

} // getSelectedOptionIndex()


//-------------------------------------------------------------------------
// getSelectedOptionValue:  Returns the value of the first selected option item in a <select> combobox.
//-------------------------------------------------------------------------
function getSelectedOptionValue( theElement ) {

	if( typeof(theElement.length)=="undefined" ) return -2;
	var theIndex = getSelectedOptionIndex(theElement);
	if( theIndex<0 ) return null;
	return theElement[theIndex].value;

} // getSelectedOptionValue()


//-------------------------------------------------------------------------
// checkLength:  Regulates a maximum length for the specified form element.
//-------------------------------------------------------------------------
function checkLength( theElement, Length, Name ) {

	var theValue = String(theElement.value);
	if( theValue.length > Length ) {
		theElement.value = theValue.substr(0,Length);
		alert( Name + " cannot exceed " + Length + " characters.   " );
	}

} // checkLength()


function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

function ReadForm (obj1) { // process un-named selects
var i,amt,des,obj,pos,val;
  amt = obj1.baseamt.value*1.0;       // base amount
  des = obj1.basedes.value;           // base description
  for (i=0; i<obj1.length; i++) {     // run entire form
    obj = obj1.elements[i];           // a form element
    if (obj.type == "select-one" &&   // just get selects
        obj.name == "") {             // must be un-named
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // selected value
      pos  = val.indexOf ("@");       // price set?
      if (pos >= 0) amt = val.substring (pos + 1)*1.0;
      pos  = val.indexOf ("+");       // price increment?
      if (pos >= 0) amt = amt + val.substring (pos + 1)*1.0;
      pos  = val.indexOf ("%");       // percent change?
      if (pos >= 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
      if (des.length == 0) des = val;
      else des = des + ", " + val;    // accumulate value
    }
  }
  obj1.item_name.value = des;
  obj1.amount.value = Dollar (amt);
  if (obj1.tot) obj1.tot.value = "$" + Dollar (amt);
}
