/**
            _    _                 
 _ __   ___| |_ (_) __ _ _ __ ___  
| '_ \ / _ \ __|| |/ _` | '_ ` _ \ 
| | | |  __/ |_ | | (_| | | | | | |
|_| |_|\___|\__|/ |\__,_|_| |_| |_|
              |__/                 

Developed by NetJam. 
www.netjam.nl

**/


/**
 * On load
 */
$(window).ready(function() {

	// Set events on buttons
	$('#tvd_goto_step_2').click(tvd_step_1);
	$('#tvd_reset').click(resetTvd);
	
	// Set events on input elements
	$('#tvd_amount').change( function() {
		$(this).attr({value: checkNumber($(this).attr('value'))});
		$(this).attr({value: number_format( $(this).attr('value').replace(/\./g,'').replace(/\,/g,'.'),0,',','.')}); 
		});

	$('#tvd_margin').change( function() {
		$(this).attr({value: checkNumber($(this).attr('value'))});
		$(this).attr({value: number_format( $(this).attr('value').replace(/\./g,'').replace(/\,/g,'.'),0,',','.')}); 
		});
		 
		
}) 


/**
 * Reset
 */
 
function resetTvd() {
	$('#tvd_step_2').hide();
	$('#tvd_goto_step_2').show();
	$('#tvd_amount').attr({value:''});
	$('#tvd_margin').attr({value:''});
	
	
}

/**
 * First step
 */
 
 function tvd_step_1() {
 	// Get input
 	var iAmount = $('#tvd_amount').attr('value').replace(/\./g,'').replace(/\,/g,'.');
 	var iMargin = $('#tvd_margin').attr('value').replace(/\./g,'').replace(/\,/g,'.');
 	
 	// Check input
 	if(iAmount == '' || iMargin == '') {
 		alert('Niet alle gegevens zijn (correct) ingevuld, probeer het nogmaals');
 		return false;
 	}

	// Calculate
	var iRevenue = (iAmount / iMargin)*100; 	 	
 	
 	 	
 	// Set in DOM
 	$('#tvd_revenue').html(number_format(iRevenue,0,',','.') );
 	$('#tvd_margin_copy').html(number_format(iMargin,0,',','.') );
 	$('#tvd_amount_copy').html(number_format(iAmount,2,',','.') );
 	
 	
 	// Hide step 1 button 
 	$('#tvd_goto_step_2').hide();
 	
 	// Show step 2
 	$('#tvd_step_2').slideDown();
 
 }
 
  
 
 /**
  * Force numeric input
  */

function checkNumber(sVal) {
	sVal = sVal.toLowerCase();
	iVal = sVal.replace(/a/g,'').replace(/b/g,'').replace(/c/g,'').replace(/d/g,'').replace(/e/g,'').replace(/f/g,'').replace(/g/g,'').replace(/h/g,'').replace(/i/g,'').replace(/j/g,'').replace(/k/g,'').replace(/l/g,'').replace(/m/g,'').replace(/n/g,'').replace(/o/g,'').replace(/p/g,'').replace(/q/g,'').replace(/r/g,'').replace(/s/g,'').replace(/t/g,'').replace(/u/g,'').replace(/v/g,'').replace(/w/g,'').replace(/x/g,'').replace(/y/g,'').replace(/z/g,'').replace(/%/g,'');
	return iVal;
}

 
 /**
  * Number format function 
  */
  
  function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}
 
