﻿function click_button(_objEvent, _objButton) {
  if (_objButton) {
    var intKeyNumber;
    
    if (window.event) { // IE
      intKeyNumber = _objEvent.keyCode;
      }
    else if (_objEvent.which) { // some other browsers
      intKeyNumber = _objEvent.which;
      }
    
    if (intKeyNumber == 13) { // Enter Key
      _objButton.click();
      return false;
      }
    else {
      return true;
      }
    }
  }

function sanitize_text(_objField) {
  if (_objField) {
    _objField.value = _objField.value.replace(/</g, "[");
    _objField.value = _objField.value.replace(/>/g, "]");
    }
  }

function show_layer(_strLayerName, _intYPos, _intXPos) {
  document.getElementById(_strLayerName).style.top = _intYPos + "px";
  document.getElementById(_strLayerName).style.left = _intXPos + "px";
  document.getElementById(_strLayerName).style.visibility = "visible";
  }
  
function hide_layer(_strLayerName) {
  if (document.getElementById(_strLayerName)) {
    document.getElementById(_strLayerName).style.visibility = "hidden";
    }
  }

function set_location(_strLocation, _intHeight, _intWidth, _intLevel) {
  var intPointerOffset = 15; 
  var intOffsetY = 0;
  var intOffsetX = 0;
  var intPageTop = document.documentElement.scrollTop;
  var intPageLeft = document.documentElement.scrollLeft;
  var intPageBottom = 0; //document.documentElement.scrollTop + window.innerHeight;
  var intPageRight = 0; //document.documentElement.scrollLeft + window.innerWidth;

  if (!_intLevel) {
   _intLevel = 0;
  }

  if (document.all) { // IE (is stupid)
    intPageBottom = document.documentElement.scrollTop + document.documentElement.offsetHeight;
    intPageRight = document.documentElement.scrollLeft + document.documentElement.offsetWidth;
    }
  else {
    intPageBottom = document.documentElement.scrollTop + window.innerHeight;
    intPageRight = document.documentElement.scrollLeft + window.innerWidth;
    }

  switch (_strLocation) {
    case "below":
      intOffsetY = intPointerOffset;
      intOffsetX = parseInt(-(_intWidth / 2));
      break;
    case "left":
      intOffsetY = parseInt(-(_intHeight / 2));
      intOffsetX = -(_intWidth + intPointerOffset);
      break;
    case "right":
      intOffsetY = parseInt(-(_intHeight / 2));
      intOffsetX = intPointerOffset;
      break;
    case "above":
    default:
      intOffsetY = -(_intHeight + intPointerOffset);
      intOffsetX = parseInt(-(_intWidth / 2));
      break;
    }

  if (document.all) { // IE (is stupid)
    intLocationY = intPageY + intOffsetY + intPageTop;
    intLocationX = intPageX + intOffsetX + intPageLeft;
    }
  else {
    intLocationY = intPageY + intOffsetY;
    intLocationX = intPageX + intOffsetX;
    }
    
  if (_intLevel < 1) {
   switch (_strLocation) {
     case "below":
       if ((intLocationY + _intHeight + intMargin) > intPageBottom) {
         set_location("above", _intHeight, _intWidth, _intLevel + 1);
         }
       break;
     case "left":
       if ((intLocationX - intMargin) < (intPageLeft)) {
         set_location("right", _intHeight, _intWidth, _intLevel + 1);
         }
       break;
     case "right":
       if ((intLocationX + _intWidth + intMargin) > intPageRight) {
         set_location("left", _intHeight, _intWidth, _intLevel + 1);
         }
       break;
     case "above":
     default:
       if ((intLocationY - intMargin) < intPageTop) {
         set_location("below", _intHeight, _intWidth, _intLevel + 1);
         }
       break;
     }
    }
  
  switch (_strLocation) {
    case "below", "above":
      if (intLocationX < intPageLeft) {
        intLocationX = intPageLeft + intMargin;
        }
      if ((intLocationX + _intWidth) > intPageRight) {
        intLocationX = intPageRight - _intWidth - intMargin;
        }
      break;
    case "left", "right":
      if (intLocationY < intPageTop) {
        intLocationY = intPageTop + intMargin;
        }
      if ((intLocationY + _intHeight) > intPageBottom) {
        intLocationY = intPageBottom - _intHeight - intMargin;
        }
      break;
      }
  }

// this function updates the estimated price and total count(s) when a checkbox is checked or unchecked
// NON-ACCORDION version:
function update_pricing_count2(checkbox, price, id, quantity)
{
  var objTotalSelected1 = document.getElementById(strTotalSelectedName1);
  var objEstimatedPrice1 = document.getElementById(strEstimatedPriceName1);
  var objTotalSelected2 = document.getElementById(strTotalSelectedName2);
  var objEstimatedPrice2 = document.getElementById(strEstimatedPriceName2);

  if (!objTotalSelected1 || !objEstimatedPrice1 || 
      !objTotalSelected2 || !objEstimatedPrice2 || !checkbox || !price || !quantity) {
    return;
    }
  
  var fltTotalPrice = parseFloat(objEstimatedPrice1.innerHTML);
  var fltCurrentPrice = parseFloat(price.innerHTML.replace("$", ""));
  var fltQuantity = parseFloat(quantity);
  var intQuantity = parseInt(Math.ceil(fltQuantity));
  
  if (isNaN(fltCurrentPrice)) {
    fltCurrentPrice = 0;
    }
  
  if (isNaN(intQuantity)) {
    intQuantity = 1;
    }
    
  fltCurrentPrice = fltCurrentPrice * intQuantity;
  
  if (checkbox.checked) {
    objEstimatedPrice1.innerHTML = (fltTotalPrice + fltCurrentPrice);
    objTotalSelected1.innerHTML = parseInt(objTotalSelected1.innerHTML) + 1;
    }
  else {
    objEstimatedPrice1.innerHTML = (fltTotalPrice - fltCurrentPrice);
    objTotalSelected1.innerHTML = parseInt(objTotalSelected1.innerHTML) - 1;
    }

  if (parseInt(objTotalSelected1.innerHTML) < 0) {
    objTotalSelected1.innerHTML = 0;
    }
  if (parseInt(objEstimatedPrice1.innerHTML) < 0) {
    objEstimatedPrice1.innerHTML = 0;
    }

  objEstimatedPrice2.innerHTML = objEstimatedPrice1.innerHTML;
  objTotalSelected2.innerHTML = objTotalSelected1.innerHTML;
}

function load_cart() {
  // this loop goes through the elements of the form, looking for the textboxes
  // if a box has "Quantity" in the name, the focus/blur events are called
  // the blur even updates the totals
  for (x = 0; x < document.forms[0].length; x++) {
    var objTemp = document.forms[0].elements[x];
    if (objTemp) {
      if (objTemp.type == "text" && objTemp.id.indexOf("Quantity") >= 0) {
        objTemp.focus();
        objTemp.blur();
        window.scrollTo(0, 0); // reset the screen position
        }
      }
    }
  }
   
function update_cart_total(quantity, price, subTotal) {
  var objEstimatedPrice = document.getElementById(strEstimatedPriceName);
  
  if (!quantity || !price || !subTotal || !objEstimatedPrice) {
    return;
    }

  quantity.value = quantity.value.replace(/[^0-9.\-]/g, "");  
  var fltQuantity = parseFloat(quantity.value);
  var intQuantity = parseInt(Math.ceil(fltQuantity));

  var fltPrice = parseFloat(price.innerHTML.replace(/[^0-9.\-]/g, ""));
  var fltPreviousSubTotal = parseFloat(subTotal.innerHTML.replace("$", ""));
  var fltNewSubTotal = 0;
  var fltPreviousTotal = parseFloat(objEstimatedPrice.innerHTML);
  
  if (isNaN(fltPrice)) {
    fltPrice = 0;
    }
    
  if (isNaN(intQuantity)) {
    quantity.value = "1";
    intQuantity = 1;
    }
  
  fltNewSubTotal = (fltPrice * intQuantity).toFixed(2);
  
  subTotal.innerHTML = "$" + (fltPrice * intQuantity).toFixed(2);
  objEstimatedPrice.innerHTML = fltPreviousTotal + (fltNewSubTotal - fltPreviousSubTotal);
  }
  
function update_cart_total2(quantity, price, subTotal) {
  var objEstimatedPrice = document.getElementById(strEstimatedPriceName);
  
  if (!quantity || !price || !subTotal || !objEstimatedPrice) {
    return;
    }
  
  quantity.value = quantity.value.replace(/[^0-9.\-]/g, "");  
  var fltQuantity = parseFloat(quantity.value);
  quantity.value = Math.ceil(fltQuantity)
  var intQuantity = parseInt(quantity.value);

  price.value = price.value.replace(/[^0-9.\-]/g, "")
  var fltPrice = parseFloat(price.value);

  var fltPreviousSubTotal = parseFloat(subTotal.innerHTML.replace("$", ""));
  var fltNewSubTotal = 0;
  var fltPreviousTotal = parseFloat(objEstimatedPrice.innerHTML);
  
  if (isNaN(fltPrice)) {
    fltPrice = 0;
    }
    
  if (isNaN(intQuantity)) {
    quantity.value = "1";
    intQuantity = 1;
    }
  
  fltNewSubTotal = (fltPrice * intQuantity).toFixed(2);
  
  subTotal.innerHTML = "$" + (fltPrice * intQuantity).toFixed(2);
  objEstimatedPrice.innerHTML = fltPreviousTotal + (fltNewSubTotal - fltPreviousSubTotal);
  }