function formatNumberWithThousandSeparator(nStr) {
	nStr += '';
	var x = nStr.split('.');
	var x1 = x[0];
	var x2;
	if (x.length > 1) {
		x2 = '.' + x[1];
		if (x[1].length == 1) {
			x2 += '0';
		}
	} else {
		x2 = '';
	}

	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}

function numericValue(nStr) {
	nStr += '';
	nStr = nStr.replace(/ /g,'');
	nStr = nStr.replace(/,/g,'.');

	var result;
	var result1 = false;
	var result2 = false;

	var x = nStr.split('.');
	var x1 = x.length > 0 ? x[0] : '';
	var x2 = x.length > 1 ? x[1] : '';

	var rgx = /^(\d+)$/;
	if (rgx.exec(x1)) {
		result1 = x1;
	}
	if (rgx.exec(x2)) {
		result2 = x2;
	}

	if (result1 != false && result2 != false) {
		result = Math.round( (result1+'.'+result2) * 100 ) / 100;
	} else if (x.length > 1 && result2 === false) {
		result = (result1 === false) ? 0 : result1;
		result += '.';
	} else if (result1 === false) {
		result = '';
	} else {
		result = result1;
	}

	return result;
}

function runLoanCalc(input) {
	var elSum = document.getElementById('loan_sum');
	var elPeriod = document.getElementById('loan_period');
	var elPayment = document.getElementById('loan_payment');
	
	var payment = 0;
	var x = 0;
	var i = 0;
	
	if (input) {
		input.value = numericValue(input.value);
	}
	
	if (input==elSum && (input.value > 1000000 || input.value < 0)) {
		elSum.value = 1000000;
	}

	if (elPeriod.value > 0) {
		var temp = loanpercent / 100 / 12
		payment = temp * (elSum.value / (1 - (1 / Math.pow(temp + 1, elPeriod.value))));
	}
	elPayment.value = formatNumberWithThousandSeparator(Math.round(payment*100)/100);
}


function openConditions() {
	//document.getElementById('conditions').style.display='block';
	var link = window.location.href;
	if (-1 == link.indexOf("index.php"))
	{
		link = link + "/index.php";
	}
	// alert(link);
	window.location.href = link + '/c/1';
	return false;
}
function closeConditions() {
	//document.getElementById('conditions').style.display='none';
	s = window.location.href;
	window.location.href = s.substring(0, s.length-4);
	
	return false;
}
