// JavaScript Document

var open_window;

if (Cookie.read('cal_depot') == null) Cookie.write('cal_depot', 0);
if (Cookie.read('cal_interest') == null) Cookie.write('cal_interest', 6);
if (Cookie.read('cal_frequence') == null) Cookie.write('cal_frequence', 24);
if (Cookie.read('cal_duree') == null) Cookie.write('cal_duree', 25);

function switchLang(langID) {
	currentURL = window.location.href;
	if (langID == 4105) { 
		location.href=currentURL.replace("_f.", "_e.");
	} else {
	location.href=currentURL.replace("_e.", "_f.");
	}
}


function calculate_montgage(emprunt)
{
	var paiement_mensuel = 0;
	
	var depot = Cookie.read('cal_depot');
	var taux_interet = Cookie.read('cal_interest');
	var frequence = Cookie.read('cal_frequence');
	var duree = Cookie.read('cal_duree');
	
	if (depot > 0) {
		if (isValidDec(depot) == false) {
			return false;
		}
	}
	if (emprunt > 0) {
		if (isValidDec(emprunt) == false) {
			return false;
		}
	}
	if (taux_interet > 0) {
		if (isValidDec(taux_interet) == false) {
			return false;
		}
	}		
	if (duree < 1 || duree >= "a") {
		alert("Period must be secified (in year).");
	}	
	
	taux_interet = taux_interet / 100;
	emprunt = emprunt - depot;
	
	if (frequence == 12) {
		paiement_mensuel = emprunt/a(duree*frequence,r(taux_interet,frequence));
	} else if (frequence == 24) {
		paiement_mensuel = emprunt/a(duree*frequence,r(taux_interet,frequence));
	} else if (frequence == 26) {
		paiement_mensuel = (emprunt/a(duree*12,r(taux_interet,12)))/2.1666666;
	} else if (frequence == 52) {
		paiement_mensuel = (emprunt/a(duree*12,r(taux_interet,12)))/4.3333333;
	}
	if (!isNaN(paiement_mensuel)) {
		paiement_mensuel =  Math.round(paiement_mensuel*100) / 100;
		//document.outils.paiement_mensuel.value = checkDecimals(paiement_mensuel);
		return checkDecimals(paiement_mensuel);
	}
	return paiement_mensuel;
}

function r (taux_interet, frequence) {
	var r1 = taux_interet/2;
	var r2 = Math.pow((1+r1), 2) - 1;
	var r3 = Math.pow((1+r2), (1/frequence)) - 1;
	return r3;
}

function a (duree, r) {
	var a = (1 - Math.pow((1+r), (-1*duree))) / r;
	return a;
}

function checkDecimals(mnt) {
	var mnt = ""+mnt;
	var mntlength = mnt.length;
	var pos =  mnt.indexOf('.');
	if (pos == -1) 
	{
		mnt += ".00";
	} 
	else if ( mntlength - pos == 2) 
	{
		mnt += "0";
	} 
	else if (mntlength - pos == 0) 
	{
		mnt += "00";
	} 
	return mnt;
}

function isValidNum(fieldValue)
{
	if(fieldValue.value.search(/^[-]?[^0-9 ]*$/) == -1)
	{
		return (false);
	}

	return (true);
}


function isValidDec(fieldValue)
{		
	var sDecimalSep = "\.";
	var sDecimalPattern = "/^[-]?\\d*[" + sDecimalSep + "]?\\d*$/";

	if(eval("fieldValue.search(" + sDecimalPattern + ")") == -1)
	{    	
		return (false);
	}

	return (true);
}

function refresh_montgage() 
{
	var content = '';
	var montant = $('montant_frequence');
	if (montant != null) {
		var frequence = Cookie.read('cal_frequence');
		var emprunt = $('emprunt_total');
		
		content = calculate_montgage(emprunt.firstChild.nodeValue.replace(/\s/g, ""));
		content = number_format(content, 2, '.', ' ');
		if (frequence == 12) {
			content += " / monthly";
		} else if (frequence == 24) {
			content += " / bi-monthly";
		} else if (frequence == 26) {
			content += " / every 2 weeks";
		} else if (frequence == 52) {
			content += " / weekly";
		}
		montant.set('html', content);
	}
}

function refresh_parent_montgage()
{
	var content = '';
	var montant = $(parent.document.getElementById('montant_frequence'));
	//var montant = $('montant_frequence');
	if (montant != null) {
		var frequence = Cookie.read('cal_frequence');
		var emprunt = $(parent.document.getElementById('emprunt_total'));
		//var emprunt = $('emprunt_total');
		
		content = calculate_montgage(emprunt.firstChild.nodeValue.replace(/\s/g, ""));
		content = number_format(content, 2, '.', ' ');
		if (frequence == 12) {
			content += " / monthly";
		} else if (frequence == 24) {
			content += " / bi-monthly";
		} else if (frequence == 26) {
			content += " / every 2 weeks";
		} else if (frequence == 52) {
			content += " / weekly";
		}
		montant.set('html', content);
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function open_map(url) {
	
	//var lat_lng = url.replace(/(.*)sll=([^\&]*)\&(.*)/, "$2");
	//var arr = lat_lng.split(',');
	
	//var url = content.replace(/(.*)src="([^\"]*)"(.*)/, "$2").replace("&amp;", "&");
	
    Shadowbox.open({
        type:     'iframe',
        content:    url,
        height:     300,
        width:      500
        /*options:    {
            onFinish: function(item){
                if (GBrowserIsCompatible()){
                    var map = new GMap2($('shadowbox_content'));
					var gll = new GLatLng(arr[0], arr[1]);
                    map.setCenter(gll, 13);

                    map.addOverlay(new GMarker(gll));

                    // add some simple controls
                    map.addControl(new GSmallMapControl());
                    map.addControl(new GMapTypeControl());
                }
            }
        }*/
    });
	
	// open a welcome message
    /*Shadowbox.open({
        type:     'html',
        title:      'Welcome',
        content:    '<div id="welcome-msg">Welcome to my website!</div>',
        height:     350,
        width:      350
    });*/
}
