function get(id) {
	return document.getElementById(id);
}

function toggleOption(id, name, amount) {
	if (get(id).checked) {
		addOption(id, name, amount);
	} else removeOption(id, name, amount);
}

function addOption(id, name, amount) {
	setPriceColor(id,'#000');
	updateTotal(amount);
	updatePaypalAmount();
	get('desc').value += name; // add to paypal desc
}

function removeOption(id, name, amount) {
	if( null != id) {
		setPriceColor(id,'#999');			
	}
	
	updateTotal(-amount);
	updatePaypalAmount();
	get('desc').value = replaceSubstring(get('desc').value, name, ''); // remove from paypal desc
}

function setPriceColor(id, color) {
	if(get(id + 'Price') != null) {
		get(id + 'Price').style.color = color;		
	}
}

function updateTotal(amount) {
	var total = parseFloat(get('total').innerHTML);
	get('total').innerHTML = total + parseFloat(amount);
}

function updatePaypalAmount() {
	get('amount').value = get('total').innerHTML;
}

function onShippingChange() {
	var newShipping = parseFloat(get('shipping').value);
	if(isNaN(newShipping)) {
		newShipping = 0;
		get('shipping').value = newShipping;
	}
	removeOption(null, "+ Shipping($" + get('old_shipping').value + ")", get('old_shipping').value);
	addOption(null, "+ Shipping($" + newShipping + ")", newShipping);
	get('old_shipping').value = newShipping;
}

function resetOptions() {
	get('baseBumper').checked = true;
	get('receiverHitch').checked = false; // reset checkboxes
	get('powderCoating').checked = false;
	get('swingAway').checked = false;
	get('shackles').checked = false;	
	get('baseBumper').style.color = '#000';
	get('receiverHitchPrice').style.color = '#999'; // reset all colors;
	get('powderCoatingPrice').style.color = '#999';
	get('swingAwayPrice').style.color = '#999';
	get('shacklesPrice').style.color = '#999';
	get('total').innerHTML = '695';	// reset total;
	get('amount').value = '695'; // reset paypal price, desc;
	get('desc').value = '+ Base Bumper';
	get('shipping').value = '0';
	get('old_shipping').value = '0';
}

function replaceSubstring(inputString, fromString, toString) {
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) {
 	while (temp.indexOf(fromString) != -1) {
		var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else {
	  var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      }
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   }

   return temp;
}