/**
 * Chronopret project JS $Id$
 * (C) Chronopret.com , 2007-2010. All Rights Reserved.
 */

var f2 = [
		'text10b_2','FormField011',
		'FormField013','FormField015',
		'FormField017','group019',
		'FormField021','FormField023',
		'FormField025','FormField027',
		'group030','group033','FormField035','group037',
 		'FormField039','group040'
		,'group042','FormField051'
		,'FormField053','FormField0532'	//FormField053_2'
		,'FormField055','FormField057'
		,'group057_3','group060','FormField0612'
		,'FormField063','FormField065','FormField067','group070','FormField094'
];



var arr_table095 = [ 'FormField096','FormField097','FormField098','FormField099' ];
var arr_table112 = [ 'FormField113','FormField114','FormField115','FormField116','FormField117','FormField118','FormField119','FormField120' ];
var arr_table123 = [ 'FormField123','FormField124' ];



function addline(group_id)
{
	// add 1 line to group table
	var i = Number( $F('FormGroup'+group_id) );
	if( isNaN(i)) i = 1;
	if( i<4 ) {
		i++;
		el = 'table'+group_id + '' + i;
		arr = eval( 'arr_table'+group_id );
		arr.each( function(n) { Element.show( n+''+i); }  ); // show inner elements - IE fix
		Element.show( el ); // <tbody id="table0953">
		$('FormGroup'+group_id).value = i;
	}

}  // addline

function rmline(group_id) {
	// remove 1 line from group table
	var i = Number( $F('FormGroup'+group_id) );
	if( isNaN(i)) i = 1;
	if( i>1 ) {
		arr = eval( 'arr_table'+group_id );
		arr.each( function(n) { Form.Element.clear( n+''+i ); Element.hide( n+''+i);	}  ); // remove inner elements + IE fix
		Element.hide( 'table'+group_id + '' + i ); // <tbody id="table0953">
		i--;
		$('FormGroup'+group_id).value = i;	// counter in hidden field
	}
}

function adjust_table(group_id) {
	// initialization: show as many lines as set in the hidden field 'FormGroup<group_id>'
	// example: upon init, <input id="FormGroup095"..> contains 3
	// so 3 lines will be added to table095
	var i = Number( $F('FormGroup'+group_id) );
	if( isNaN(i)) i = 1;

	$('FormGroup'+group_id).value = 1;
	
	for( var j=1; j<i; j++ ) {
		addline( group_id );
	}
}


function calcul01(i,l,a) {
/*
	Finding the monthly payment (m) given Loan Amount (a), interest rate(i), and length in years (l).
	i: annual Interest rate
	l: Lenght in years
	a: initial Amount of the loan
	returns: Monthly amount
*/
	var j=i/1200.00;
	var n=l*12;
	var m = a * ( j / ( 1 - Math.pow(1+j,-n)) );

	m = Math.floor( 10000.00 * m ) / 10000.00 ;
	return m;
}	


function calcul03(i,m,a) {
/*
	Finding the number of months given Loan Amount (a), interest rate(i), and monthly payment(m).
	!!!TODO!!!
*/
	
	i=i/1200.00;
	var l = - Math.log( 1-(i*a/m) ) / Math.log(1+i);

	l = Math.floor( 100.00 * l ) / 100.00 ;
	
	var r = Math.floor(l);
	if( r!=l ) {
		r1 = Math.floor( m*(l-r));
		r = r+' mois + 1 mois X '+r1+' Euro';
	} else {
		r = r+' mois';		
	}	
	return r;
}

function calcul04(nm,ny,i,m) {
/*
	Finding the Loan Amount (a) given number of months (n), yearly interest rate(i), and monthly payment(m).
*/
	var n = nm+(12*ny);
	i = i/1200.00;
	var a = (m/i)*(1.00-Math.pow(1.00+i,-n));
	return Math.ceil(a);
}


function calcul02(a,m,l) {
/*
	Finding the Interest Rate (i) given Loan Amount (a), monthly payment (m), and Length in Years (l).
	
	Algorithm by: Hugh Chou http://www.hughchou.org/calc/formula.html
	Javascript: by Oleg Sverdlov <info [] ols.co.il>
	License: Kto spizdit, tot pidoras i vafel.
 */
	var min_rate=0;
	var max_rate=100;
	var j,mid_rate,guessed_pmt;
	var n = l*12;
	while (min_rate < max_rate - 0.0001) {
		mid_rate = (min_rate + max_rate) / 2; 
		j = mid_rate / 1200; 
		// calculate payment based on this interest, term of F and loan_amt
		guessed_pmt = a * ( j / (1 - Math.pow(1 + j,-n )));
		if (guessed_pmt > m) { 
			max_rate = mid_rate; // current rate is new maximum 
		} else {
			min_rate = mid_rate; // current rate is new minimum
		}
	}
	return mid_rate;
}


function calcul05() {
}


function set0(elem) {
	if($(elem)) $(elem).value = 0;
}

function grey() {
	var n    = Number( $F('FormField001') );
	var n002 = Number( $F('FormField002') );
	var formId = $('formId')? $F('formId'): 0;
	var page1 = $('page1')? $F('page1'): 0;
	var page2 = $('page2')? $F('page2'): 0;
	var page3 = $('page3')? $F('page3'): 0;
	var page4 = $('page4')? $F('page4'): 0;

	// rule 1. Show FormField0011 only if FormField001.value = 1
	// rule 2. Show FormField0012 only if FormField001.value = 2
	switch(n) {
		case 1: 
			if( $('FormField0011') ) Element.show('FormField0011');
			if( $('FormField0012') ) Element.hide('FormField0012');
			break;
		case 2:
			if( $('FormField0012') ) Element.show('FormField0012');
			if( $('FormField0011') ) Element.hide('FormField0011');
			break;
		default:
			if( $('FormField0011') ) Element.hide('FormField0011');
			if( $('FormField0012') ) Element.hide('FormField0012');
			if( $('group080') ) Element.show('group080');
			//alert('rule1-2');
	}

	// rule 3. Show FormField0021 if FormField002.value =1 or 2
	if( n002 == 1 || n002 == 2 ) {
		if( $('FormField0021') ) Element.show('FormField0021');
	} else {
		if( $('FormField0021') ) Element.hide('FormField0021');
	}

	
	// rule 6. Added 05/12/2006 0:23
	if( n002 == 3 || n002 == 5 ) {
		if( $('group075') ) Element.show( 'group075' );
	} else {
		if( $('group075') ) { Element.hide( 'group075' );	set0( 'FormField075' ); 	}
			
	}

	// rule 4, changed: hide fields 077, 078 / group077 , if n=1,2,4,5
	if( n==1 || n==2 || n==4 || n==5 ) {
		if( $('group077') ) { Element.hide( 'group077' ); set0('FormField077'); set0('FormField078'); }
	} else {
		if( $('group077') ) Element.show( 'group077' );
	}

	// final processing
	var is_single;

	if( $('FormField010a') ) { 
		is_single = ( 1==$F('FormField010a') );
	} else {
		is_single = $('field010a_1').checked;
	}

	// * remove fields if 'single person' 

	if( is_single ) {
		f2.each( function(obj) { if( $(obj)) { Element.hide(obj) }; } );
	} else {
		f2.each( function(obj) { if( $(obj)) { Element.show(obj)}; } );
	}

	// 
	if( page1 ) {
		if( n==3 ) {
			Element.show( 'group0061'); // rule 7. 21.11.2006 
			Element.hide('group005');
		} else {
			Element.hide('group0061');
			$('FormField0061').value = 0;
			$('FormField0062').value = 0;
			Element.show('group005');
		}

		if( $('field005_1_1').checked ) {
			Element.hide( 'FormField005' );
		}
		if( $('field005_1_2').checked ) {
			Element.show( 'FormField005' );
		}
	}


/*
 * Added 16.01.2007
 */
 	if( page2 ) {
		var f038=$F('FormField038');
		var f039=$F('FormField039');
		
	 	if( f038!=2 && f038!=4 ) {
 			Element.hide( 'group040' );
 			Element.hide( 'group042' );
		}
		if( f038==2 ) {
 			Element.show( 'group040' );
 			Element.show( 'group041' );
 			Element.show( 'group042' );
		}
		if( f038==4 ) {
 			Element.show( 'group040' );
 			Element.hide( 'group041' );
		}
		
		// 048 =1 or 4 ==> disable group049
		var f048 = $F('FormField048');
		switch(f048) {
			case '1':
				Element.hide( 'group049' );
				Element.hide( 'group0491' );
				break;
			case '2':
				Element.show( 'group049' );
				Element.show( 'group0491' );
				break;
			case '3':
				Element.hide( 'group049' );
				break;
			case '4':
				Element.hide( 'group049' );
				break;
		}
			
		/*			
		if( f048 == 1 || f048 == 4 ) {
			Element.hide( 'group049' );
		} else {
			Element.show( 'group049' );
		}
		if( f048 == 1 || f048 == 3 ) {
			Element.hide( 'group0491' );
		} else {
			Element.show( 'group0491' );
		}
		*/
	}


	// rule 7. Added 04/02/07
	// rule 5. hide fields 072...076 / group072 AND 080...089 , if n=3
	
	if( page3 ) {
		if( n==3 ) {
			Element.hide( 'group072' ); set0('FormField072'); set0('FormField073'); set0('FormField0731');set0('FormField075');set0('FormField076');
			Element.hide( 'group074' ); set0('FormField074');
		} else if ( n==4 ) {
			Element.hide( 'group072' ); set0('FormField072');
			Element.show( 'group074' );
		
		} else {
			Element.show( 'group072'  );
			Element.show( 'group074' );
		}
		
		if( n==3 ) {
			Element.hide( 'group080' ); set0('FormField080');
		} else {
			Element.show( 'group080'  );
		}
	}




 	if( page3 ) {
		if( $('field081_1').checked ) { Element.show('euro082'); }
		if( $('field081_2').checked ) { Element.hide('euro082'); }
		
		if( $('field083_1').checked ) { Element.show('euro084'); }
		if( $('field083_2').checked ) { Element.hide('euro084'); }
		
		if( $('field085_1').checked ) { Element.show('euro086'); }
		if( $('field085_2').checked ) { Element.hide('euro086'); }

		if( $('field087_1').checked ) { Element.hide('group089'); }
		if( $('field087_2').checked ) { Element.show('group089'); }

		// events for rule095 - #table095
		if( $('field095_1').checked ) { Element.show('table095'); }
		if( $('field095_2').checked ) { Element.hide('table095'); }

		if( $('field100_1').checked ) { Element.show('group100'); }
		if( $('field100_2').checked ) { Element.hide('group100'); }
		
		if( $('field105_1').checked ) { Element.show('group105'); }
		if( $('field105_2').checked ) { Element.hide('group105'); }

		if( $('field112_1').checked ) { Element.show('table112'); }
		if( $('field112_2').checked ) { Element.hide('table112'); }
		if( $('field121_1').checked ) { Element.show('group121'); }
		if( $('field121_2').checked ) { Element.hide('group121'); }

		calc079();
	} 


	// 19.02.2007
 	if( page4 ) {
 		if( n == 3 || n== 4 ) {
	 		Element.hide( 'group128' );
 		} else {
	 		Element.show( 'group128' );
 		}
 	}
} // grey


function calc079() {
/*
 * calculate summary of fields 72..78
 */
	var num = 0.00;
	var arr_acquisition = [
		'FormField072','FormField073','FormField0731','FormField074','FormField075','FormField076','FormField077','FormField078' 
	];		
	var num1;
	arr_acquisition.each( 
		function(obj) { 
			if( $(obj).style.visibility =='hidden') { alert ('hidden'+obj); }
			num1 = Number( $F(obj) ); 
			num += (isNaN(num1) ? 0 : num1); 
		}
	)
	$('FormField079').value = num;
}



function calc072() {
	// assigns FormField073
	var f002  = $F('FormField002');
	var f0021 = $F('FormField0021');
	var sum   = $F('FormField072');

	sum = fdn(f002,f0021,sum);
		
	var old = $F('FormField073');
	if( isNaN(old) || old=='') {
		$('FormField073').value = sum;
		calc079();
	} else if( old != sum ) {

		if( confirm( "L'ancienne valeur est de : "+old+".\nLa nouvelle valeur calculée à titre indicatif est de : "+sum+"\nVoulez vous utiliser cette nouvelle valeur ?" ) ) {
			$('FormField073').value = sum;
			calc079();
		}
	}
} //calc072



function fdn( f002,f0021,summ) {
	// Envelope for frais_de_notaire
	// uses values of FormField002,FormField0021,FormField072
	var nature = 0;

	if( f002!=1 && f002!=2 && f002!=3 && f002 !=4 && f002!=5 ) {
		alert( 'Remplissez manuellement tout seul' );
		return;
	}
	
	if( f0021 !=1 && f0021 != 2 && f0021 != 3 ) {
		alert( 'Remplissez manuellement tout seul' );
		return;
	}

	if( f002 == 5 ) nature = 7; 
	if( f002 == 3 ) nature = 5;
	if( f002 == 4 ) nature = 6;
	if( f002 == 1 ) {
		if( f0021 == 1 ) nature = 4;
		if( f0021 == 2 ) nature = 3;
		if( f0021 == 3 ) nature = 9;
	}
	if( f002 == 2 ) {
		if( f0021 == 1 ) nature = 2;
		if( f0021 == 2 ) nature = 1;
		if( f0021 == 3 ) nature = 8;
	}		 
		
	summ = frais_de_notaire( summ, nature );
	
	return summ;
}  // fdn(f002,f0021,summ)




/* -----------------19/12/2006 14:27-----------------
natbien = neuf
	1 - Maison particulie`re - neuf 	 | F002 = 2, F0021 = 2
	8 - VEFA - Maison particulie`re    | F002 = 2, F0021 = 3
	3 - Appartement - neuf             | F002 = 1, F0021 = 2 
	9 - VEFA - Appartement             | F002 = 1, F0021 = 3
 natbien = ancien                    |
	2 - Maison particulie`re - ancien  | F002 = 2, F0021 = 1
	4 - Appartement - ancien           | F002 = 1, F0021 = 1
 natbien = batir                     |
	5 - Terrain + Construction         | F002 = 3
	6 - Terrain seul                   | F002 = 4
	7 - Construction Seule             | F002 = 5
 --------------------------------------------------*/


function frais_de_notaire(montant_achatnet,nature)
{
	//-- Ve'rifications
	if (( isNaN(parseInt(montant_achatnet,10))) || (montant_achatnet==0)){
		return(0);
	}
	if (nature == 7) return(0);

	//-- Initialisation
	var montant,natbien,honoraires,taxes,fraisnotaire,taux,ajout,nbTranche;
	honoraires = 0;
	montant    = parseInt(montant_achatnet,10);
	if (nature == 1 || nature == 3 || nature == 8 || nature == 9) natbien = "neuf";
	if (nature == 2 || nature == 4 ) natbien = "ancien";
	if (nature == 5 || nature == 6 ) natbien = "batir";


	/* tab_ref famille : immo_nature
		1 - Maison particulie`re - neuf 
		2 - Maison particulie`re - ancien 
		3 - Appartement - neuf 
		4 - Appartement - ancien 
		5 - Terrain + Construction 
		6 - Terrain seul 
		7 - Construction Seule 
		8 - VEFA - Maison particulie`re 
		9 - VEFA - Appartement 
	*/
	
	//-- Tableau des tranches de taux pour honoraires
	nbTranche = 4;
	trancheD  = new Array(    0,  3051,   6101,   16771 );
	trancheF  = new Array( 3050,  6100,  16770, 99999999);
	taux_0    = new Array(    5,   3.3,   1.65,    0.825);
	ajout_0   = new Array(    0, 51.85,  152.5,   290.85);

	//-- Taux des taxes
	tauxAnc  = 5.09;
	tauxNeu  = 0.715;
	tauxBat  = 5.09;
	tauxTVA  = 19.6;
	
	//-- Calcul des honoraires
	montant = Math.round(montant);
	for (i=0; i < nbTranche ;i++) {
		if(trancheD[i]<montant && montant<trancheF[i]) {
			taux  = taux_0[i];
			ajout = ajout_0[i];
			break;
		}
	}
	honoraires = (montant*taux/100+ajout)*(1+tauxTVA/100);

	//-- Calcul des taxes
	//if (natbien=="neuf") 	  taxes = montant/(1+tauxTVA/100) * tauxNeu/100;
	if (natbien=="neuf") 		taxes = montant * tauxNeu/100;
	if (natbien=="ancien") 	taxes = montant * tauxAnc/100;
	if (natbien=="batir") 	taxes = montant * tauxBat/100;

	//-- Calcul des Frais divers
	fraisdivers = 1007 + montant/1000;

	//-- Frais de notaire arrondi a` la centaine
	fraisnotaire = honoraires + fraisdivers + taxes ;
	fraisnotaire = Math.round(fraisnotaire/100)*100;

	return(fraisnotaire);
}





function formatCurrency(num) {
	var CurrencySumbol = '';
	var ThousandsDelimiter = ',';
	var DecimalsDelimiter = '.';

	num = num.toString().replace(/\s\$|\,/g,'');

	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3)) + ThousandsDelimiter + num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + CurrencySumbol + num + DecimalsDelimiter + cents);
}







function formHandler(form_obj,form_serial) {
	grey();
	return true;
}	


function formValidatedCallback(result, form) {
	if( result ) {
		alert('OK');
	} else {
		alert( "Information nécessaire: check list of fields marked as red" );
	}
}



function freeze(elem) {
	if( $(elem).readonly == true ) {
		$(elem).readonly = false;
	} else {
		$(elem).readonly = true;
	}
}


function init() {
	if($('form01')) {
		new Form.EventObserver('form01', formHandler );
		grey();
		assistant_init();
	}
}
