/*
** open finance calculator in window & show calcs for given price
** user can change term
*/

//alert("+financeCalc - load");

/*
document.write('<div id="dialog" title="Dialog Title">Test dialog</div>');
*/

// get window size
var viewportwidth;
var viewportheight;  
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined') {
  viewportwidth = window.innerWidth;
  viewportheight = window.innerHeight;
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
 && typeof document.documentElement.clientWidth !=
 'undefined' && document.documentElement.clientWidth != 0) {
   viewportwidth = document.documentElement.clientWidth;
   viewportheight = document.documentElement.clientHeight;
} else {
	// older versions of IE
   viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
   viewportheight = document.getElementsByTagName('body')[0].clientHeight;
}

// onclick on `Calc` button in browsepages - opens window
function financeCalc(element, eventHandle, price) {	
	//alert("+financeCalc " + price);

	// pop event bubble
	if (!eventHandle) var eventHandle = window.event;
	if (eventHandle) eventHandle.cancelBubble = true;
	if (eventHandle.stopPropagation) eventHandle.stopPropagation();

	/*
	// bypassed until finalised
	//var url = "index2.php?option=com_content&view=article&id=29&Itemid=37&total=" + round_decimals( price, 0 );

	popupCenter(url, "Finance calculator", 805, viewportheight + 20);
	*/
	
	var url = "index2.php?option=com_content&view=article&id=28&Itemid=12";
	popupCenter(url, "Financing", 805, viewportheight + 20);
	
	/*
	//alert("$=" + $);	
	$(document).ready(function() {
		$("#dialog").dialog();
	  });
  */
  
	//alert("-financeCalc");
}

// returns values for given price
function financeCalcGetValues(element, eventHandle, price, targetId) {
	//alert(0);

	// given amount	
	var amountField = document.getElementById("amountField");
	//alert(amountField);
	if (amountField == null || amountField == undefined) {
		document.write('<input id="amountField" ' +
				'style="display: none;" />');
		amountField = document.getElementById("amountField");
		//alert(amountField);
	}
	amountField.value = price;
	
	// given term
	//alert(termField);
	var termField = document.getElementById("termField");
	if (termField == null || termField == undefined) {
		document.write('<select id="termField" onchange="payment();"' +
				'style="display: none;">' +
				'<option value="12">12</option>' +
				'<option value="18">18</option>' +
				'<option value="24" selected="yes">24</option>' +
				'<option value="30">30</option>' +
				'<option value="36">36</option>' +
			'</select>');
		termField = document.getElementById("termField");
		//alert(termField);
	}
	termField.value = 24;

	// refresh
	payment();

	//alert(1);
	
	var targetPay = document.getElementById("finCalcPay" + targetId);
	var targetDep = document.getElementById("finCalcDep" + targetId);
	var targetTotal = document.getElementById("finCalcTotal" + targetId);
	var targetTerm = document.getElementById("finCalcTerm" + targetId);
	var targetInterestRate = document.getElementById("finCalcInterestRate" + targetId);
	
	// finCalcPay & fincalcDep are set global by finCalcMain.js
	// debug:
	//finCalcPay=1;
	//finCalcDep=2;
	try {
		targetPay.innerHTML = nbsp("R" + finCalcPay + "");
		targetDep.innerHTML = nbsp("R" + finCalcDep + "");
		targetTotal.innerHTML = nbsp("R" + finCalcTotal + "");
		targetTerm.innerHTML = nbsp("" + finCalcTerm + "");
		targetInterestRate.innerHTML = nbsp("" + finCalcInterestRate + "%");
	} catch (ex) {
		// targetDep etc may not always be defined..
	}
}

function nbsp(s) {
	return replaceAll(s, " ", "&nbsp;");
}

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

function round_decimals(original_number, decimals) {

    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)

    //return result3.toFixed(decimals); // fails! NaN
    return result3;

}

function popupCenter(pageURL, title, w, h) {
	var left = (viewportwidth/2) - (w/2); // screen.width
	var top = (viewportheight/2) - (h/2); // screen.height
	var targetWin;
	try {
		targetWin = window.open(pageURL, title, 
			'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, copyhistory=0, width='+w+
			', height='+h+', top='+top+', left='+left);
	} catch(ex) {
		try {
			// see: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
			targetWin = window.open(pageURL);//, 
				//'width='+w+', height='+h+'');
			//targetWin.moveTo(top, left);
		} catch(ex) {
			alert("Problem opening window\nPlease allow popups\nSee the top of page for options bar");
		}
	}
} 

//alert("+financeCalc - jquery dialog");
/*$(document).ready(function() {
	$("#dialog").dialog();
  });
*/
//alert("-financeCalc - jquery dialog");
  
//alert("-financeCalc - load");

