// Global vars
var currencySymbol;
var thousandsSep;
var decimalSep;

function resetSelectedAdPostingDropdown(originalProduct, changePrice) {

	var dropdown = document.getElementById('selectableAdPostingProduct');

	for (i = 0 ; i < dropdown.length ; i++) {

		if (dropdown[i].value == originalProduct) {
			dropdown.selectedIndex = i;
			var label = dropdown.options[dropdown.selectedIndex].label;
			break;
		}//End if

	}//End for

	//If change price flag is true then uhh change the price
	if (changePrice) {
		calculateSubtotal();
	}//End if

}//End resetSelectedAdPostingDropdown()

function calculateSubtotal() {

  var upsellBoxes = document.getElementById('subtotalWrapper').getElementsByTagName('input');

  var total = 0;

  if (document.getElementById('selectableAdPostingProduct') || document.getElementById('adPostingProductPriceHidden')) {

  	//Get the currently selected product label
  	var el = document.getElementById('selectableAdPostingProduct');
  	if (el) {
        var currentProductLabel = document.getElementById('selectableAdPostingProduct').options[document.getElementById('selectableAdPostingProduct').selectedIndex].attributes.getNamedItem('label').value;
        currentProductPrice = getPriceFromLabel(currentProductLabel);
    }

    //Get the selected ad posting product value
    else {
        currentProductPrice = document.getElementById('adPostingProductPriceHidden').getAttribute('value');
    }

    total += parseFloat(currentProductPrice);

    document.getElementById('selectedProductPrice').innerHTML = formatNumberCurrency(currentProductPrice);

  }//End if

  if (upsellBoxes.length > 0) {

	  for (i = 0; i < upsellBoxes.length; i++) {

	    if (upsellBoxes[i].checked == true){
	      total += parseFloat(upsellBoxes[i].attributes.getNamedItem('label').value);
	    }//End if

	  }//End for

  }//End if

  document.getElementById('subtotalNumeric').value = total;
  document.getElementById('subtotal').innerHTML = formatNumberCurrency(total);

}//End calculateSubtotal()

function adjustOnLoad( element, amt ) {

  var ot = document.getElementById(element);

  if (ot.checked) {
    adjustSubtotal( ot, amt );
  }//End if

}//End adjustOnLoad()

function adjustSubtotal(element, amt) {

    cur_val = Number(document.getElementById('subtotalNumeric').value);

    if ( element.checked ) {
        cur_val = cur_val + Number( amt );
    } else {
        cur_val = cur_val - Number( amt );
    }

    // Some values may have cents, round if this is the case
    cur_val *= 100;
    cur_val = Math.round(cur_val);
    cur_val /= 100;

    document.getElementById('subtotalNumeric').value = cur_val;
    document.getElementById('subtotal').innerHTML = formatNumberCurrency(cur_val);

}//End adjustSubtotal()

function getPriceFromLabel(label) {

	var potentialPrice;

	//Strip out the values between parens
  potentialPrice = new String(label.match(/\(.*\)/));

  //If nothing found return 0
  if (potentialPrice == "null") {
  	return 0;
  }//End if

  //Remove parens
  potentialPrice = potentialPrice.replace(/\(/, "");
  potentialPrice = potentialPrice.replace(/\)/, "");

  //If we have a currency symbol we have a valid price
  if (potentialPrice.match(/\$/)) {

  	//Remove currency symbol
  	potentialPrice = potentialPrice.replace(/\$/, "");

  }//End if

  //No currency symbol so it's probably a prepaid product
  else {
  	potentialPrice = 0;
  }//End else

  //Just in case
  potentialPrice = Number(potentialPrice);

  return potentialPrice;

}//End getPriceFromLabel()

function initCurrencyFormat(cSymbol, tSep, dSep)
{
    currencySymbol  = cSymbol;
    thousandsSep    = tSep;
    decimalSep      = dSep;
}

function formatNumberCurrency(number)
{
    // number does not have any "comma" separators
    //  So add them, but instead of commas use thousandsSep var
    number = number.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, thousandsSep);

    // Replace decimal with decimalSep var
    number = number.toString().replace(/\./g, decimalSep);

    // Special handling for currencies that occur at end of number vs before
    if (currencySymbol == 'kr') {
        number += ' ' + currencySymbol;
    } else {
        number = currencySymbol + number;
    }

    return number;
}


