var Front = new Object();

Front.onResponse = function() 
{
	if(Ajax.CheckReadyState(Ajax.request))
	{

		priceStr = document.getElementById('orgTotal').value;
		priceStr = priceStr.replace(/,/g,'');
		priceStr = parseFloat(priceStr);
		document.getElementById('orgTotal').value = priceStr;
		document.getElementById('interestrate').value = 0;
		document.getElementById('instamount').value = 0;
		
		
		document.getElementById('instable').innerHTML = "";
		document.getElementById('instable').innerHTML = "<div style='background-color:#fff; width: 210px;' id='header'>"
													  	+ "<div style='background-color:#fff; width: 210px;'>"
													  	+ "<div style='float: left; height:16px; width:18px; text-align:center; background-color:#f0f0f0; border: 2px solid rgb(255, 255, 255);'><span class='Required'>*</span></div>"
													  	+ "<div style='float: left; height:16px; width:90px; text-align:center; background-color:#f0f0f0; border: 2px solid rgb(255, 255, 255);'><strong>Taksit</strong></div>"
													  	+ "<div style='float: left; height:16px; width:90px; text-align:center; background-color:#f0f0f0; border: 2px solid rgb(255, 255, 255);'><strong>Vade</strong></div>"
														+ "</div>"
														+ "<div style='background-color:#fff; width: 210px;' id='insts'>"
														+ "</div>"
														+ "</div>";
    
		var xmlobject = Ajax.request.responseXML;
		
		var insts = xmlobject.getElementsByTagName('insts')[0];
		
		if (insts.childNodes.length == 0) {

			document.getElementById('insts').innerHTML = '<span>Tanimli Taksit Yok.</span>&nbsp';
				
		}
		
		for (var i = 0; i < insts.childNodes.length; i++) {
			
			var instalments = insts.childNodes.item(i);
	
			var instid = instalments.childNodes.item(0);
			var bankid = instalments.childNodes.item(1);
			var instalment = instalments.childNodes.item(2);
			var interestrate = instalments.childNodes.item(3);
				
			var _instid = instid.childNodes.item(0).nodeValue;
			var _bankid = bankid.childNodes.item(0).nodeValue;
			var _instalment = instalment.childNodes.item(0).nodeValue;
			var _interestrate = interestrate.childNodes.item(0).nodeValue
			var checked;
			
			if(_instalment == 0){
				
				checked = "checked='checked'"
				
				Front.calculate(priceStr,_interestrate,_instalment);
				
				insttxt = "Peşin";
				
			} else {
				
				insttxt = _instalment;
				
				checked = "";
			}
			
			if (_interestrate == 0.00) {
				
				interesttxt = "<strong>Yok</strong>";
				
			} else {
				
				interesttxt = _interestrate;
				
			}
				
			var insthtml = "<div style='background-color:#fff; width: 210px;'>"
						 + "<div style='float: left; height:16px; width:18px; text-align:center; background-color:#f0f0f0; border: 2px solid rgb(255, 255, 255);'><input type='radio' name='Insts' value='" + _instalment +"' "+ checked +" style='height:14px; width:14px;' onclick='javascript:Front.calculate("+priceStr+","+_interestrate+","+_instalment+");'></div>"
						 + "<div style='float: left; height:16px; width:90px; text-align:center; background-color:#f0f0f0; border: 2px solid rgb(255, 255, 255);'><span>"+ insttxt +"</span></div>"
						 + "<div style='float: left; height:16px; width:90px; text-align:center; background-color:#f0f0f0; border: 2px solid rgb(255, 255, 255);'><span>"+ interesttxt +"</span></div>"
						 + "</div>";
																			
			document.getElementById('insts').innerHTML += insthtml;
									
		}
	}	
}

Front.calculate = function(priceStr,_interestrate,_instalment)
{
	// add interestrate
	var addedInterest = priceStr*(_interestrate/100);
	var newTotal = (addedInterest+priceStr)

	// round new total
	newTotal = Math.round(newTotal*100)/100;
	
	// main calculations
	
	// math with 0 is undefined so put the condition first.
	
	if (_instalment == 0 && _interestrate == 0.00){
		
		// instalment is 0 , so show only price
		
		document.getElementById('TotalAmount').innerHTML = newTotal + " TL";
		
	}
	
	if (_instalment == 0 && _interestrate != 0.00) {
		
		document.getElementById('TotalAmount').innerHTML = newTotal + " TL ( " + _interestrate + " % vade )";
		
	}
	
	if (_instalment != 0 && _interestrate == 0.00) {

		instalment = newTotal/_instalment;
		instalment = Math.round(instalment*100)/100;

		document.getElementById('TotalAmount').innerHTML = newTotal + " TL ( " + instalment + " TL x " + _instalment + " ay, vade yok )";
		document.getElementById('instamount').value = instalment + " TL"
		
	}
	
	if (_instalment != 0 && _interestrate != 0.00) {
		
		instalment = newTotal/_instalment;
		instalment = Math.round(instalment*100)/100;
		
		// show it on the total amount
		
		document.getElementById('TotalAmount').innerHTML = newTotal + " TL ( " + instalment + " TL x " + _instalment + " ay, " + _interestrate + " % vade )";
		document.getElementById('instamount').value = instalment + " TL"
		
	}
	document.getElementById('taksit').value = _instalment;
	document.getElementById('newTotal').value = newTotal;
	document.getElementById('interestrate').value = Math.round(addedInterest*100)/100;
	
}


