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

Developed by NetJam. 
www.netjam.nl

**/


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

	// Set events on buttons
	$('#dso_goto_step_2').click(dso_step_1);
	$('#dso_goto_step_3').click(dso_step_2);
	$('#dso_reset').click(resetDso);
	
	
	// Set event on pulldown
	$('#dso_interest').change( function() {
		if($(this).attr('value') == 'CUST') {
			$('#dso_interest_custom').show(); 
		} else {
			$('#dso_interest_custom').attr({value: ''});
			$('#dso_interest_custom').hide();
		}
	});
	
	
	// Set events on input elements
	$('#dso_revenue').change( function() {
		$(this).attr({value: checkNumber($(this).attr('value'))});
		$(this).attr({value: number_format( $(this).attr('value').replace(/\./g,''),0,',','.')}); 
		});

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


/**
 * Reset
 */
 
function resetDso() {
	$('#dso_step_2').hide();
	$('#dso_step_3').hide();
	$('#dso_goto_step_2').show();
	$('#dso_goto_step_3').show();
	$('#dso_decline').attr({value:''});
	$('#dso_interest_decline').attr({value:''});
	$('#dso_improvement').attr({value:''});
	$('#dso_current').attr({value:''});
	$('#dso_wish').attr({value:''});
	$('#dso_interest_custom').attr({value: ''});
	$('#dso_interest').attr({value: ''});
	
	
}

/**
 * First step
 */
 
 function dso_step_1() {
 	// Get input
 	var iRevenue = $('#dso_revenue').attr('value');
 	var iPending = $('#dso_pending').attr('value');
 	var iInterest = $('#dso_interest').attr('value');
 	var iInterestCustom = $('#dso_interest_custom').attr('value');
 
 	// Check input
 	if(iRevenue == '' || iPending == '' || iInterest == '') {
 		alert('Niet alle gegevens zijn (correct) ingevuld, probeer het nogmaals');
 		$('#dso_step_2').hide();
 		$('#dso_step_3').hide();
 		return false;
 	}
 	
 	if(iInterest == 'CUST' && iInterestCustom == '') {
 		alert('Niet alle gegevens zijn (correct) ingevuld, probeer het nogmaals');
 		$('#dso_step_2').hide();
 		$('#dso_step_3').hide();
 		return false; 	
 	}
 	
 	// Calculate
 	iRevenue = iRevenue.replace(/\./g,'');
 	iPending = iPending.replace(/\./g,'');
 	var iCurrentDso = number_format ( (iPending / (iRevenue/365)),2,',','.');
 	
 	// Set in DOM
 	$('#dso_current').attr({value: iCurrentDso});
 	
 	
 	// Hide step 1 button 
 	$('#dso_goto_step_2').hide();
 	
 	// Show step 2
 	$('#dso_step_2').slideDown();
 
 }
 
 
 /**
  * Second step
  */
 
 function dso_step_2() {
	// Get input
 	var iRevenue 	= parseFloat( $('#dso_revenue').attr('value').replace(/\./g,'').replace(/\,/g,'.') );
 	var iPending 	= parseFloat( $('#dso_pending').attr('value').replace(/\./g,'').replace(/\,/g,'.') );
 	var iWish 		= parseFloat( $('#dso_wish').attr('value').replace(/\./g,'').replace(/\,/g,'.') );
 	
 	var iCustomInterest = $('#dso_interest_custom').attr('value');
 	if(iCustomInterest == '') {
 		var iInterest	= parseFloat( $('#dso_interest').attr('value').replace(/\./g,'').replace(/\,/g,'.') );
 	} else {
 		var iInterest	= parseFloat( iCustomInterest.replace(/\./g,'').replace(/\,/g,'.') );
 	}
 	
 	
 	var iCurrentDso = (iPending / (iRevenue/365));
 	
 	// Check input
 	if($('#dso_wish').attr('value') == '') {
 		alert('Niet alle gegevens zijn (correct) ingevuld, probeer het nogmaals');
 		$('#dso_step_3').hide();
 		return false;
 	} 	
 	 
 	 
 	// Calculate 	
 	var iDecline = (iPending - (  (iPending/iCurrentDso)*iWish) );
 	var iInterestDecline = iDecline*(iInterest/100);
 	var iImprovement = iDecline+iInterestDecline;
 	
 	// Format
 	
 	// Set in DOM
 	$('#dso_decline').attr({value: number_format(iDecline,2,',','.')});
 	$('#dso_interest_decline').attr({value: number_format(iInterestDecline,2,',','.')});
 	$('#dso_improvement').attr({value: number_format(iImprovement,2,',','.')});

 	// Hide step 2 button 
 	$('#dso_goto_step_3').hide();
 	 	
 	 // Show step 3
 	$('#dso_step_3').slideDown();
 
 	// Apply colors
 	if(iDecline < 0) {
 		$('#dso_decline').css({color: 'red'});
 	} else {
 		$('#dso_decline').css({color: ''});
 	}
 	
 	if(iInterestDecline < 0) {
 		$('#dso_interest_decline').css({color: 'red'});
 	} else {
 		$('#dso_interest_decline').css({color: ''});
 	}
 	
 	if(iImprovement < 0) {
		$('#dso_improvement').css({color: 'red'});	 	
 	} else {
 		$('#dso_improvement').css({color: ''});
 	}
 
 }
 
 
 
 /**
  * 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,'');
	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;
}
 
