function home() {
if (navigator.userAgent.indexOf("Mozilla/3.0") != -1) version = "n3";
else version = "n2";
if (version == "n3") {
window.close();
}
else {
window.close();
}
}

function percentValid( per ) {
var tempstr = "";
var cnt = 0;

for( strctr=0; strctr <per.length; strctr++) {
	strchar = per.charAt(strctr);
	if( (strchar == '.') || (strchar == ',') ) { cnt++; }
	
	if( cnt>1 ){ tempstr=0;}
		else{
			if( strchar == ',' ) strchar=".";
			
			if( (strchar == '.') || (strchar == '0') || (strchar == '1') || (strchar == '2') 
			|| (strchar == '3') || (strchar == '4') || (strchar == '5') || (strchar == '6') 
			|| (strchar == '7') || (strchar == '8') || (strchar == '9')){tempstr = tempstr + strchar;}
		}
	}
tempstr = new Number(tempstr);
if(tempstr>25){alert("Dozvoljena vrijednost je od 1-25.");return(0);}
else return(tempstr);
}

function makeNumValid( p_value ) {
var strchar;
var strctr;
var strlen;
var instr = p_value;
var newstr;
var tempstr;
if( instr.indexOf('.') != instr.lastIndexOf('.') ) 
return(-1); 
newstr = removeCommas( instr );
strlen = newstr.length;
exit = 0;
tempstr = '';
for( strctr=0; strctr <strlen; strctr++) {
strchar = newstr.charAt(strctr);
if( (strchar == '.') || (strchar == '0') || (strchar == '1') || (strchar == '2') 
|| (strchar == '3') || (strchar == '4') || (strchar == '5') || (strchar == '6') 
|| (strchar == '7') || (strchar == '8') || (strchar == '9')) 
tempstr = tempstr + strchar;
}
newstr = tempstr;
if( exit == 1 ) 
return(0);
else 
return(newstr); 
}
function roundFloat( s_value ) {
var newval;
s_value = parseFloat(s_value);
newval = (Math.floor(s_value*100))/100;
newval = newval + "";
if ( newval.indexOf('.') == -1 ) 
newval = newval + ".00"; 
return(newval);
}
function removeCommas( t_value ) {
var ctr;
var newstr = t_value;
var commapos;
var strlen = t_value.length - 1;
commapos = newstr.indexOf(',');
while ( commapos != -1 ) {
newstr = newstr.substring(0,commapos) +  
newstr.substring(commapos + 1, newstr.length );
commapos = newstr.indexOf(',');
}
return(newstr);
}
function addCommas( u_value ) {
var ctr;
var decCtr=0;
var decIndex;
var decIncr = 3;
var newstr = u_value;
var strlen = u_value.length;
decIndex = u_value.indexOf('.');
if ( decIndex == -1 ) 
decIndex = strlen; 
for ( ctr = decIndex-1; ctr > 0; ctr-- ) {
decCtr++;
if ( decCtr == 3 ) { 
newstr = newstr.substring(0,ctr) +
"," +
newstr.substring(ctr, strlen );
strlen++;
decCtr = 0; 
}
}
return(newstr);
}
function isDigit( v_value ) {
if ( (v_value >= 0) || (v_value <= 9) ) 
return(true);
else 
return(false);
}
function resetTextField( textField ) {
textField.value = roundFloat("0");
textField.focus();
textField.select();
}
//var v_MonthlyPaymentHold='';  hold montly payment for onchange event
function calcMonthlyPayment() {
var v_HomePrice,
v_InterestRate,
v_DownPayment,
v_InterestRate,
v_Razdoblje,
v_MonthlyPayment,
v_Principle,
v_formName = document.frmCreditCalc;
// check values used to compute montly payment

if ( confirmRazdobljeValues() ) {
v_HomePrice = roundFloat( makeNumValid( v_formName.HomePrice.value ) );
v_MonthlyPayment = roundFloat( makeNumValid( v_formName.MonthlyPayment.value ) );

if (v_HomePrice != 0)  {
v_DownPayment = 0;
v_InterestRate = makeNumValid( v_formName.InterestRate.value ) / 100;
v_Razdoblje = makeNumValid(v_formName.Razdoblje.options[v_formName.Razdoblje.selectedIndex].value);
v_Principle = v_HomePrice - v_DownPayment;
if ( v_Razdoblje == "" || v_Razdoblje == 0 ) 
v_MonthlyPayment = v_Principle;
else {
v_MonthlyRate = v_InterestRate/12;
if ( v_MonthlyRate != 0 ) 
v_MonthlyPayment = Math.floor((v_Principle*v_MonthlyRate)/(1-Math.pow((1+v_MonthlyRate),
(-1*(v_Razdoblje*12))))*100)/100;
else  // no monthly rate
v_MonthlyPayment = Math.floor((v_Principle / (v_Razdoblje*12)) * 100) / 100;
if ( isNaN(v_MonthlyPayment) || v_MonthlyPayment <0 ) 
v_MonthlyPayment = 0;
}
v_formName.MonthlyPayment.value = addCommas(roundFloat(v_MonthlyPayment));
} else {

v_InterestRate = makeNumValid( v_formName.InterestRate.value ) / 100;
v_Razdoblje = makeNumValid(v_formName.Razdoblje.options[v_formName.Razdoblje.selectedIndex].value);
v_Principle = v_MonthlyPayment;
if ( v_Razdoblje == "" || v_Razdoblje == 0 ) 
v_HomePrice = v_Principle;
else {
v_MonthlyRate = v_InterestRate/12;
if ( v_MonthlyRate != 0 ) 
v_HomePrice = Math.floor((v_Principle/v_MonthlyRate)*(1-Math.pow((1+v_MonthlyRate),
(-1*(v_Razdoblje*12))))*100)/100;
else  // no monthly rate
v_HomePrice = Math.floor((v_Principle * (v_Razdoblje*12)) * 100) / 100;
if ( isNaN(v_HomePrice) || v_HomePrice <0 ) 
v_HomePrice = 0;
}
v_formName.HomePrice.value = addCommas(roundFloat(v_HomePrice));
}

}

}

function confirmRazdobljeValues() {
var v_form = document.frmCreditCalc,
v_HomePrice = v_form.HomePrice.value,
v_MonthlyPayment = v_form.MonthlyPayment.value,
v_DownPayment = 0, // v_form.DownPayment.value,
v_InterestRate = v_form.InterestRate.value,
v_Razdoblje = v_form.Razdoblje.options[v_form.Razdoblje.selectedIndex].value,
v_tHomePrice,
v_tMonthlyPayment,
v_tDownPayment,
t_tInterestRate,
v_tRazdoblje,
v_errorflag = 0,
v_errorstr,
v_returnval;
// get values from fields  
v_tHomePrice = parseFloat(makeNumValid(v_HomePrice));
v_tMonthlyPayment = parseFloat(makeNumValid(v_MonthlyPayment));
if ( v_tHomePrice == '' || isNaN(v_tHomePrice) ) {
v_form.HomePrice.value = 0;
v_tHomePrice = 0;
}
if ( v_tMonthlyPayment == '' || isNaN(v_tMonthlyPayment) ) {
v_form.MonthlyPayment.value = 0;
v_tMonthlyPayment = 0;
}
v_tDownPayment = 0 // parseFloat(makeNumValid(v_DownPayment));
v_tInterestRate = parseFloat(makeNumValid(v_InterestRate));
v_tRazdoblje = Math.round(makeNumValid(v_Razdoblje)); 
if ( v_tDownPayment <0 ) { // N3.x fix; -1 bleeds thru when 0
v_tDownPayment = 0;
}
if ( v_tInterestRate == '' || isNaN(v_tInterestRate) ) { 
v_tInterestRate = 0;
}
// Traženi kredit <0 or  > 100,000,000
if ( v_tMonthlyPayment >= 100000000 || v_tMonthlyPayment < 0 ) {
v_errorstr = "Nažalost,  " + v_MonthlyPayment + " DM je kriva vrijednost.\nMolimo Vas da unesete vrijednost izme?u 0,00 DM and 1,000,000.00 DM"; 
v_form.MonthlyPayment.value = '';
v_errorflag = 1;
// Tražena rata <0 or  > 100,000,000
} else if ( v_tHomePrice >= 100000000 || v_tHomePrice < 0 ) {
v_errorstr = "Nažalost,  " + v_HomePrice + " DM je kriva vrijednost.\nMolimo Vas da unesete vrijednost izme?u 0,00 DM and 1,000,000.00 DM"; 
v_form.HomePrice.value = '';
v_errorflag = 1;
// Kamata <1 or > 25
} else if ( v_tInterestRate <1 || v_tInterestRate > 25 ) {
v_errorstr = "Nažalost.  Kamata od " + v_InterestRate + " je pogrešna.\nMolimo Vas da unesete vrijednost izme?u 1% i 25%.";
v_form.InterestRate.value = '';
v_errorflag = 1;
// Razdoblje <5 ; textfield limited to 2 chars so not > 99 by default
}
else if ( v_tRazdoblje <1 ) {
v_errorstr = "Sorry.  Your Razdoblje of " + v_Razdoblje + " is invalid.\nPlease enter a minimum value of 5 godina.";
v_form.Razdoblje.value = '';
v_errorflag = 1;
// zadani kredit i rata
} else if (v_tHomePrice != 0 && v_tMonthlyPayment!=0) {
	v_errorstr = "Ako zadajete iznos kredita ostavite praznim visinu rate, obrnuto ako zadajete visinu rate ostavite prazan iznos kredita";
	v_errorflag = 1;
}
// display error and clear result
if ( v_errorflag ) {
alert ( v_errorstr );
//clearMonthlyPayment();
v_returnval = 0;
} else {
v_returnval = 1;
}
return ( v_returnval );
}
