function calculateHypo() {
	
	var xPriceId = 'price';
	var xOwnInvestId = 'ownInvest';
	var xYearSelectId = 'yearSelect';
	var xResultId = 'result';
	
	var xMessage1Id = '';
	var xMessage2Id = '';
	
	var xInterestRate = 0.0569;
	var xOwnInvestRatio = 0.8;
	var xPrice = 0;
	var xOwnInvest = 0;
	var xYear = 0;
	var xResult = 0;
		
	if (document.getElementById(xPriceId) && document.getElementById(xOwnInvestId) && 
			document.getElementById(xYearSelectId) && document.getElementById(xResultId)) {
		
		try {			
			// PARSE PRICE
			try {
				xPrice = intval(parseInt(trimText(document.getElementById(xPriceId).value)));
			} catch (priceError) {
				xPrice = 0;
			}
			
			// PARSE OWN INVEST
			try {
				xOwnInvest = intval(parseInt(trimText(document.getElementById(xOwnInvestId).value)));
			} catch (priceError) {
				xOwnInvest = 0;
			}
			
			// PARSE YEAR
			try {
				xYear = intval(parseInt(document.getElementById(xYearSelectId).options[document.getElementById(xYearSelectId).selectedIndex].value));
			} catch (priceError) {
				xYear = 0;
			}
			
			if (xPrice > 0 && xYear > 0) {				
				// CALCULATE
				xResult = (((xPrice-xOwnInvest)*xInterestRate/12)/(1-Math.pow((1/(1+xInterestRate/12)),(12*xYear))));
				
				// APPEND RESULT
				document.getElementById(xResultId).value=Math.round(xResult);
				return true;		
			}			
			document.getElementById(xResultId).value = '';				
			
		} catch (e) {
			
		}
	}
}

function kbNewWin(src,name,w,h,prop,lp,tp) {
	if ((!w && !h) && (lp && tp)) {
		w = screen.width - (lp * 2);
		h = screen.height - (tp * 2) - tp;
		tp = tp / 2;
	} else if ((w && h) && (!lp && !tp)) {
		lp = (screen.width - w) / 2;
		tp = (screen.height - h) / 2;
	}
	prop = (!prop)? "" : prop+",";
	prop += (!w && !h)? "" : "width="+w+",height="+h+",left="+lp+",top="+tp;
	Win = window.open(src,name,prop);
	Win.focus();
}

function kbCalc () {
    var xPriceId = 'price';
	var xOwnInvestId = 'ownInvest';
	var xYearSelectId = 'yearSelect';

    var kbCena = 0;
	var kbSporeni = 0;
	var kbSplatnost = 0;

        // PARSE PRICE
		try {
			kbCena = intval(parseInt(trimText(document.getElementById(xPriceId).value)));
		} catch (priceError) {
			kbCena = 0;
		}

		// PARSE OWN INVEST
		try {
			kbSporeni = intval(parseInt(trimText(document.getElementById(xOwnInvestId).value)));
		} catch (investError) {
			kbSporeni = 0;
		}

		// PARSE YEAR
		try {
			kbSplatnost = intval(parseInt(document.getElementById(xYearSelectId).options[document.getElementById(xYearSelectId).selectedIndex].value));
		} catch (yearError) {
			kbSplatnost = 0;
		}

	kbNewWin('http:\x2f\x2fwww.kb.cz\x2fcs\x2fapp\x2fmort_calc.shtml?rates=3&cena='+kbCena+'&sporeni='+kbSporeni+'&splatnost='+kbSplatnost,'kbMortCalc',467,405,'status=yes');
}


function intval(mixed_var,base) {
    var tmp;
 
    var type = typeof(mixed_var);
 
    if(type == 'boolean'){
        if (mixed_var == true) {
            return 1;
        } else {
            return 0;
        }
    } else if(type == 'string'){
        tmp = parseInt(mixed_var * 1);
        if(isNaN(tmp) || !isFinite(tmp)){
            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if(type == 'number' && isFinite(mixed_var) ){
        return Math.floor(mixed_var);
    } else{
        return 0;
    }
}

function trimText(mixed_var) {
	mixed_var = mixed_var.replace (/\s+/g, '');
	return mixed_var;
}


