//************************************************
// calc_pret_max.js
//
// Form validation
//
//************************************************


//************************************************
// checkFloatField()
//************************************************
function checkFloatField(FLD, DEFAULT) {
	
	// String value
	SVAL  = FLD.value;
	SVAL  = SVAL.replace(',','.');
	SVAL  = SVAL.replace(' ','');
	
	// Float value
	FVAL  = parseFloat(SVAL);
	if (isNaN(FVAL)) {
		FLD.value = DEFAULT.toFixed(2).replace('.',',');
	} else {
		//FLD.value = FVAL.toFixed(2).replace('.',',');
	}
	
	return FVAL;
	
} // checkFloatField


//************************************************
// checkFloatFieldNoNegative()
//************************************************
function checkFloatFieldNoNegative(FLD, DEFAULT) {
	
	// String value
	SVAL  = FLD.value;
	SVAL  = SVAL.replace(',','.');
	SVAL  = SVAL.replace(' ','');
	
	// Float value
	FVAL  = parseFloat(SVAL);
	if (isNaN(FVAL)) {
		FLD.value = DEFAULT.toFixed(2).replace('.',',');
	} else {
		//FLD.value = FVAL.toFixed(2).replace('.',',');
	}
	
	if (FVAL < 0) {
		FLD.value = DEFAULT.toFixed(2).replace('.',',');
		alert("Pas de valeur négative ...");
	}
	
	return FVAL;
	
} // checkFloatFieldNoNegative


//************************************************
// checkFields()
//************************************************
function checkFields() {
	
	BAL_TOT  = 0;
	BAL_TOT += checkFloatFieldNoNegative(document.calc.BAL_MO1, 0.00);
	BAL_TOT += checkFloatFieldNoNegative(document.calc.BAL_MO2, 0.00);
	BAL_TOT += checkFloatFieldNoNegative(document.calc.BAL_CAR, 0.00);
	BAL_TOT += checkFloatFieldNoNegative(document.calc.BAL_PER, 0.00);
	BAL_TOT += checkFloatFieldNoNegative(document.calc.BAL_CCD, 0.00);
	BAL_TOT += checkFloatFieldNoNegative(document.calc.BAL_LOC, 0.00);
	BAL_TOT += checkFloatFieldNoNegative(document.calc.BAL_OTH, 0.00);
	
	PAY_TOT  = 0;
	PAY_TOT += checkFloatFieldNoNegative(document.calc.PAY_MO2, 0.00);
	PAY_TOT += checkFloatFieldNoNegative(document.calc.PAY_MO2, 0.00);
	PAY_TOT += checkFloatFieldNoNegative(document.calc.PAY_CAR, 0.00);
	PAY_TOT += checkFloatFieldNoNegative(document.calc.PAY_PER, 0.00);
	PAY_TOT += checkFloatFieldNoNegative(document.calc.PAY_CCD, 0.00);
	PAY_TOT += checkFloatFieldNoNegative(document.calc.PAY_LOC, 0.00);
	PAY_TOT += checkFloatFieldNoNegative(document.calc.PAY_OTH, 0.00);
	
	document.calc.BAL_TOT.value = BAL_TOT.toFixed(2).replace('.',',');
	document.calc.PAY_TOT.value = PAY_TOT.toFixed(2).replace('.',',');
	
	checkFloatFieldNoNegative(document.calc.INCOME, 0.00);
	
	checkFloatFieldNoNegative(document.calc.ESTATE_TAXES,      100);
	checkFloatFieldNoNegative(document.calc.ESTATE_HEAT,      1800);
	checkFloatFieldNoNegative(document.calc.ESTATE_CHARGES,   0.00);
	checkFloatFieldNoNegative(document.calc.ESTATE_INCOME,    0.00);
	checkFloatFieldNoNegative(document.calc.ESTATE_VALUE,   110000);
	
	checkFloatFieldNoNegative(document.calc.CASH,     0);
	
	checkFloatFieldNoNegative(document.calc.ABD,     32);
	checkFloatFieldNoNegative(document.calc.ATD,     40);
	checkFloatFieldNoNegative(document.calc.PV,      90);
	
} // checkFields


//************************************************
// checkRate()
//************************************************
function checkRate() {
	
	RATE = checkFloatField(document.calc.NIR, 5.00);
	
	if (RATE < 0.01) {
		document.calc.NIR.value = "0,01";
		alert("Taux minimal de 0,01%");
	}
	
	if (RATE > 60.0) {
		document.calc.NIR.value = "60,00";
		alert("Taux maximal de 60%");
	}
	
} // checkRate


//************************************************
// checkMortgage()
//************************************************
function checkMortgage() {
	
	if (!document.calc.INC_MO1.checked && !document.calc.INC_MO2.checked) {
		
		document.calc.INC_MO1.checked = true;
		alert("Vous devez choisir au moins une hypothèque");
		
	}
	
} // checkMortgage


//************************************************
// popInfo()
//************************************************
function popInfo(ANCHOR) {
	
	URL = "../modules/glossaire.php?anchor=" + ANCHOR;
	WND = window.open(URL, 'calc_info', 'height=400,width=450,left=200,top=150,resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no');
	
} // popInfo

