// *************************
// begin tax calc routines *
// *************************

	function updateCartTotal(cart_form, cartTotal, cartShipping) {

	  var stateIndex = cart_form.state.options.selectedIndex;

	  var state = cart_form.state.options[stateIndex].value;

	  if (state != "") {
        var stateRate = calculateTax(state, cartTotal);

        var totalTax = (parseFloat(cartTotal) * (parseFloat(stateRate) / 100));

        cart_form.cart_tax.value = formatCurrency(totalTax);

        cart_form.cart_adjusted.value = formatCurrency(cartTotal + cartShipping + totalTax);
	  }
	  else {
	    var totalTax = formatCurrency(0);
        cart_form.cart_tax.value = totalTax;
        cart_form.cart_adjusted.value = formatCurrency(cartTotal + cartShipping);
      }
      
      

	}

    function state(stateName, stateTax){
      //fill the instance with the values passed in
      this.name=stateName;
      this.tax=stateTax;
      //then return the instance of state
      return this;
    }

    function taxTable(){
      //the instance of taxTable is filled with
      //an array of state objects
      this[0] =new state("", 0.00);
      this[1] =new state("AL", 4.00);
      this[2] =new state("AK", 0.00);
      this[3] =new state("AZ", 5.60);
      this[4] =new state("AR", 5.125);
      this[5] =new state("CA", 7.25);
      this[6] =new state("CO", 2.90);
      this[7] =new state("CT", 6.00);
      this[8] =new state("DE", 0.00);
      this[9] =new state("DC", 5.75);
      this[10]=new state("FL", 6.00);
      this[11]=new state("GA", 4.00);
      this[12]=new state("HA", 4.00);
      this[13]=new state("ID", 6.00);
      this[14]=new state("IL", 6.25);
      this[15]=new state("IN", 6.00);
      this[16]=new state("IA", 5.00);
      this[17]=new state("KS", 5.30);
      this[18]=new state("KY", 6.00);
      this[19]=new state("LA", 4.00);
      this[20]=new state("ME", 5.00);
      this[21]=new state("MD", 5.00);
      this[22]=new state("MA", 5.00);
      this[23]=new state("MI", 6.00);
      this[24]=new state("MN", 6.50);
      this[25]=new state("MS", 7.00);
      this[26]=new state("MO", 4.225);
      this[27]=new state("MT", 0.00);
      this[28]=new state("NE", 5.50);
      this[29]=new state("NV", 6.50);
      this[30]=new state("NH", 0.00);
      this[31]=new state("NJ", 6.00);
      this[32]=new state("NM", 5.00);
      this[33]=new state("NY", 4.25);
      this[34]=new state("NC", 4.50);
      this[35]=new state("ND", 5.00);
      this[36]=new state("OH", 6.00);
      this[37]=new state("OK", 4.50);
      this[38]=new state("OR", 0.00);
      this[39]=new state("PA", 6.00);
      this[40]=new state("RI", 7.00);
      this[41]=new state("SC", 5.00);
      this[42]=new state("SD", 4.00);
      this[43]=new state("TN", 7.00);
      this[44]=new state("TX", 6.25);
      this[45]=new state("UT", 4.75);
      this[46]=new state("VT", 6.00);
      this[47]=new state("VA", 4.50);
      this[48]=new state("WA", 6.50);
      this[49]=new state("WV", 6.00);
      this[50]=new state("WI", 5.00);
      this[51]=new state("WY", 4.00);


      //return the newly created taxTable to
      //the calling function
      return this;
    }


    function calculateTax(stateValue, cartValue){
      // reset values?
      var index=0;
      var result=0;
      var temp=0;
	  var totalWithTax = 0;
      var taxGuide = taxTable();



      //while there's an instance of state
      //in the taxGuide array
      while (taxGuide[index]) {
        //if the state passed in is in the
        //taxGuide array
        if (stateValue.toUpperCase() == taxGuide[index].name) {
          // put the correct tax rate in "result"
          result = taxGuide[index].tax;
        }
        index++;
      }



      //calcTotal();
      var temp1 = cartValue;
      // calculate total with tax
      var temp2 = (parseFloat(temp1) * (1 + parseFloat(result)));
      // chop off extra decimal places
      var totalWithTax = (Math.round(temp2 * 100)) / 100;

      return(result);
      // change value in form
      //document.forms.total.value = totalWithTax;

      // return value

    }

// *************************
// end tax calc routines *
// *************************


function showStatus(sMsg) {
    window.status = sMsg ;
    return true ;
} // end showStatus()

function confirmDelete(dMsg) {
    if(confirm(dMsg) == true) {
	    return(true);
	} else {
	    return(false);
	}
} // end confirmDelete

function formatCurrency(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
} // end formatCurrency()

function formatComma(amount)
{
	var delimiter = ",";
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
} // end formatComma()

function showOrderForm(formId, hideId, showId, buttonId)
{
    var showFormId = formId;
	var showForm = document.getElementById(showFormId);
	showForm.style.visibility = "visible";
	showForm.style.display = "block";

	var hideLinkId = hideId;
	var hideLink = document.getElementById(hideLinkId);
	hideLink.style.visibility = "hidden";

	var showLinkId = showId;
	var showLink = document.getElementById(showLinkId);
	showLink.style.visibility = "visible";

	var focusButtonId = buttonId;
	var focusButton = document.getElementById(focusButtonId);
	focusButton.style.visibility = "visible";


}

function hideOrderForm(formId, hideId, showId, buttonId)
{
    var showFormId = formId;
	var showForm = document.getElementById(showFormId);
	showForm.style.visibility = "hidden";
	showForm.style.display = "none";

	var hideLinkId = hideId;
	var hideLink = document.getElementById(hideLinkId);
	hideLink.style.visibility = "hidden";

	var showLinkId = showId;
	var showLink = document.getElementById(showLinkId);
	showLink.style.visibility = "visible";

	var focusButtonId = buttonId;
	var focusButton = document.getElementById(focusButtonId);
	focusButton.style.visibility = "hidden";

	var showDollarId = dollarId;
	var showDollar = document.getElementById(showDollarId);
	showDollar.style.visibility = "visible";

}

function showPrice(basePrice, orderForm, formId)
{
	if (orderForm.order_option) {
		if (isNaN(orderForm.order_option.length)) {
			var result = parseFloat(basePrice);
			result = formatComma(formatCurrency(result));
		}
		else {
			for ( var i = 0; i < orderForm.order_option.length; i++) {
				if(orderForm.order_option[i].checked) {
					var modifier = orderForm.order_option[i].value;
					var result = parseFloat(basePrice) + parseFloat(modifier);
					result = formatComma(formatCurrency(result));
				}
			}
		}
	}
	else {
		var result = parseFloat(basePrice);
		result = formatComma(formatCurrency(result));
	}
	/*
	var result = parseFloat(basePrice);
	result = formatComma(formatCurrency(result));
	*/
	orderForm.amount.value = parseFloat(result);
	quantity = orderForm.order_quantity.value;
	if(isNaN(quantity)) { quantity = 1; }
	result = formatComma(formatCurrency(result * quantity));

	orderForm.order_price.value = result;
	orderForm.order_price.style.visibility = "visible";
	orderForm.order_button.style.visibility = "visible";
	var dollarId = "dollar_" + formId;
	var dollar = document.getElementById(dollarId);
	dollar.style.visibility = "visible";

	var focusId = "focusbutton_" + formId;
	var focusB = document.getElementById(focusId);
	focusB.style.visibility = "visible";

} // end showPrice()

function popWinImg(winlocation, imgWidth, imgHeight, padWidth, padHeight)
{
    // recitfy height/width
	imgWidth = imgWidth + padWidth;
	imgHeight = imgHeight + padHeight;
	// window attribs
	var winName = "vpImgViewer";
	var winAttributes = "width=" + imgWidth + ",height=" + imgHeight + ",resizable=yes,scrollbars=yes,location=no";
	// calculate window x,y
	var winX = (screen.availWidth - imgWidth) / 2;
	var winY = (screen.availHeight - imgHeight) / 2;
	// pop window
	var imgWin;
	imgWin = window.open(winlocation, winName, winAttributes);
	// center window
	imgWin.moveTo(winX,winY);
	imgWin.focus();
} // end popWin()

// ************************
// layer utility routines *
// ************************

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject

// ********************************
// position location functions    *
// ********************************

function getElementLeft(eObj)
{
	var nLeftPos = eObj.offsetLeft;                // initialize var to store calculations
    var eParElement = eObj.offsetParent;           // identify first offset parent element
    while (eParElement != null)
    {                                              // move up through element hierarchy
        nLeftPos += eParElement.offsetLeft;        // appending left offset of each parent
        eParElement = eParElement.offsetParent;    // until no more offset parents exist
    }
    return nLeftPos;                               // return the number calculated
}

function getElementRight(eObj)
{
    var nRightPos = eObj.offsetWidth;              // init var to store calculations
	nRightPos = nRightPos + getElementLeft(eObj);  // add width to leftOffset
	return nRightPos;                              // return value
}

function getElementTop(eObj)
{
	var nTopPos = eObj.offsetTop;                  // initialize var to store calculations
    var eParElement = eObj.offsetParent;           // identify first offset parent element
    while (eParElement != null)
    {                                              // move up through element hierarchy
        nTopPos += eParElement.offsetTop;          // appending top offset of each parent
        eParElement = eParElement.offsetParent;    // until no more offset parents exist
    }
    return nTopPos;                                // return the number calculated
}

function getElementBottom(eObj)
{
    var nBottomPos = eObj.offsetHeight;            // init var to store calculations
	nBottomPos = nBottomPos + getElementTop(eObj); // add height to top offset
	return nBottomPos;                             // return value
}

// ********************************
// application-specific functions *
// ********************************
function showMenu(menuNumber, elementObj, eventObj, menuPosition, menuParent, menuGrandParent, menuGreatGrandParent)
//function showMenu(menuNumber, elementObj, eventObj, menuPosition)
{
	hideAllMenus(menuNumber, menuParent, menuGrandParent, menuGreatGrandParent);
    //hideAllMenus();

	// Calculate Element Id
	var id = elementObj.id;
	// Get reference to Element Obj
	var elementId = document.getElementById(id);
	// Calculate Element Position
	if (menuParent) {
	    var posLeft   = getElementLeft(elementId.parentNode)   + 'px';
	    var posRight  = getElementRight(elementId.parentNode)  + 'px';
	    var posTop    = getElementTop(elementId.parentNode)    + 'px';
	    var posBottom = getElementBottom(elementId.parentNode) + 'px';
	} else {
	    var posLeft   = getElementLeft(elementId)   + 'px';
	    var posRight  = getElementRight(elementId)  + 'px';
	    var posTop    = getElementTop(elementId)    + 'px';
	    var posBottom = getElementBottom(elementId) + 'px';
	}
	//alert("Left X: " + posLeft + " | " + "Right X: " + posRight + " | " + "Top Y: " + posTop + " | " + "Bottom : " + posBottom);

    var menuId = subMenuPrefix + menuNumber;

	var menuStyle = getStyleObject(menuId);

	// Position Menu Layer
	if (menuPosition == 'vertical') {
	    moveObject(menuId, posRight, posTop);
	}
	if (menuPosition == 'vertleft') {
		var width = (getElementLeft(elementId) - (getElementRight(elementId) - getElementLeft(elementId))) + 'px';
	    moveObject(menuId, width, posTop);
	}
	else if (menuPosition == 'horizontal') {
	    moveObject(menuId, posLeft, posBottom);
	}

	if(changeObjectVisibility(menuId, 'visible')) {
		/*
		var menuTitle = getStyleObject(id);
		menuTitle.backgroundColor = '{color_light}';
		*/
		eventObj.cancelBubble = true;
		return true;
    } else {
		return false;
    }
}

//function hideAllMenus(menuParent, menuGrandParent, menuGreatGrandParent) {
function hideAllMenus(menuNumber, menuParent, menuGrandParent, menuGreatGrandParent) {
    for(counter = 1; counter <= numMenus; counter++) {
		if ( (menuNumber != counter) && (menuParent != counter) && (menuGrandParent != counter) && (menuGreatGrandParent != counter) ) {
		    changeObjectVisibility(subMenuPrefix + counter, 'hidden');
		}
	}
}