/*
0$ - 50 000$ =
0,5% de la somme qui se situe dans cette tranche.

50 000$ - 250 000$ =
1,0% de la somme qui se situe dans cette tranche.

250 000$ et plus =
1,5% de la somme qui se situe dans cette tranche.
*/

function addSeparator(fldID) { 
        var posCaret = getPosition(fldID); 
        var fldVal = fldID.value; 
           if((fldVal.length === 3 || 7 || 11 || 15 || 19) && (fldVal.length === posCaret)) { 
             posCaret = posCaret +1;  
             } 
        nStr = fldVal.replace(/ /g,'');    
        nStr += ''; 
        x = nStr.split('.'); 
        x1 = x[0]; 
        x2 = x.length > 1 ? '.' + x[1] : ''; 
        var rgx = /(\d+)(\d{3})/;
		var espace = ' ';
        while (rgx.test(x1)) { 
           x1 = x1.replace(rgx, '$1' + espace + '$2'); 
        } 
		x1 = x1.replace(/^\s*|\s*$/g,'');
        
		//fldID.value = x1+x2;
		fldID.value = x1;
		/*alert(x1);*/
        setCaretPosition(fldID, posCaret); 
    }
 
function setCaretPosition(elem, caretPos) {
    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
        else
          elem.focus();
        }
    }
}
 
function getPosition(amtFld) {
     var iCaretPos = 0;
     if (document.selection) { 
       amtFld.focus ();
       var oSel = document.selection.createRange ();
       oSel.moveStart ('character', - amtFld.value.length);
       iCaretPos = oSel.text.length;
     }
     else if (amtFld.selectionStart || amtFld.selectionStart == '0')
       iCaretPos = amtFld.selectionStart;
     return(iCaretPos);
 }
 

     

function calculerTaxeDeBienvenue(){
	 var radioButtons = document.getElementsByName("radio");
      for (var x = 0; x < radioButtons.length; x ++) {
        if (radioButtons[x].checked) {
          laRegion = radioButtons[x].id;
        }
      }
	  
	  if(laRegion == "region"){
	
		var montant = "";
		
		montant = document.getElementById("taxedebienvenue_montant").value;
		
		montant = montant.replace(' ', '');
		montant = montant.replace(/\s/gi,"");
		/*alert(montant);*/
		montant = montant.replace(',', '', 'g');
		montant = montant.replace('.', '', 'g');
		var resultat = 0;
		
		
		/*pageTracker._trackPageview('/calcul/'+montant);*/
		
		montant = parseInt(montant);
		
		tranche4 = montant - 500000;
		if(tranche4>0) {
			resultat += (tranche4*2/100);
			montant -= tranche4;
		}
	
		tranche3 = montant - 250000;
		if(tranche3>0 && tranche3<250001) {
			resultat += (tranche3*1.5/100);
			montant -= tranche3;
		}
		
		tranche2 = montant - 50000;
		if(tranche2>0) {
			resultat += (tranche2*1.0/100);
			montant -= tranche2;
		}
		
		tranche1 = montant;
		if(tranche1>0) {
			resultat += (tranche1*0.5/100);
			montant -= tranche1;
		}
	
		//if(resultat>=500) document.getElementById('pub-hugo-calcul').style.display='block';
		
		resultat = resultat + "$";
		
		document.getElementById("taxedebienvenue_resultat").innerHTML = resultat;
	  }else{
			var montant = "";
		
		montant = document.getElementById("taxedebienvenue_montant").value;
		
		montant = montant.replace(' ', '');
		montant = montant.replace(/\s/gi,"");
		/*alert(montant);*/
		montant = montant.replace(',', '', 'g');
		montant = montant.replace('.', '', 'g');
		var resultat = 0;
		
		
		/*pageTracker._trackPageview('/calcul/'+montant);*/
		
		montant = parseInt(montant);
	
		tranche3 = montant - 250000;
		if(tranche3>0) {
			resultat += (tranche3*1.5/100);
			montant -= tranche3;
		}
		
		tranche2 = montant - 50000;
		if(tranche2>0) {
			resultat += (tranche2*1.0/100);
			montant -= tranche2;
		}
		
		tranche1 = montant;
		if(tranche1>0) {
			resultat += (tranche1*0.5/100);
			montant -= tranche1;
		}
	
		//if(resultat>=500) document.getElementById('pub-hugo-calcul').style.display='block';
		
		resultat = resultat + "$";
		
		document.getElementById("taxedebienvenue_resultat").innerHTML = resultat;  
	  }
}

//<![CDATA[

// copyright - begin
//
//   Tous droits réservés (c) 1998 Espace Courbe inc.
//   Site Web: http://www.espacecourbe.com/
//
//   Auteur: Benoit Dubuc - benoit.dubuc@espacecourbe.com
//   Création: 29 décembre 1998
//   Modification: 2 mai 2011
//
// copyright - end


Number.prototype.formatNumber = function (precision) {
	if (this.toFixed) { // on devrait aussi éviter dans IE...
		return this.toFixed(precision);
	}
	else {
		var m = Math.round(this * 100) / 100;
		m = (m + 0.001).toString();
		return m.substring(0, m.indexOf('.') + 3);
	}
}

function getRadioValue (f,theRadioGroup) {
    for (var i = 0; i < f[theRadioGroup].length; i++) {
        if (f[theRadioGroup][i].checked) {
                return f[theRadioGroup][i].value;
        }
    }
}

function afficheCalculs(){
	var f = document.getElementById('formulaire');
	var buildingLocation = getRadioValue(f,'lieu');
	var isMontreal = (buildingLocation == 'montreal');
	var rates = (isMontreal?new Array(0.005,0.01,0.015,0.020):new Array(0.005,0.01,0.015));
	var bounds = (isMontreal?new Array(50000,200000,250000,100000000):new Array(50000,200000,100000000));
	if (isNaN(parseFloat(f.montantpaye.value))) {
		f.montantpaye.value = "0.00";
	}
	var amount = parseFloat(f.montantpaye.value).formatNumber(2);
	var taxemutation = 0;
	for (i=0; i<rates.length; i++) {
		taxemutation += Math.min(bounds[i],amount)*rates[i];
		amount -= Math.min(bounds[i],amount);
	}
	f.taxemutation.value = taxemutation.formatNumber(2);
	return 1;
}
//]]>

