var formulaire_cache = [];
function post_request(url, properties) {
var strProperties = "";
var value, i;
for (pName in properties) {
value = properties[pName];
if (value instanceof Array) {
for (i = 0; i < value.length; i++) {
if (strProperties.length > 0)
strProperties += "&";
strProperties += pName + "=" + encodeURIComponent(value[i]);
}
} else {
if (strProperties.length > 0)
strProperties += "&";
strProperties += pName + "=" + encodeURIComponent(value);
}
}
if (url != null) {
var xhr_object = null;
if (window.XMLHttpRequest) {
xhr_object = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
} else {
return "";
}
xhr_object.open("POST", url, false);
xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr_object.send(strProperties);
return xhr_object.responseText;
} else {
alert(strProperties);
return "";
}
}
/*
** Returns the caret (cursor) position of the specified text field.
** Return value range is 0-oField.length.
*/
function get_caret_position (oField) {
// Initialize
var iCaretPos = 0;
// IE Support
if (document.selection) {
// To get cursor position, get empty selection range
var oSel = document.selection.createRange ();
// Move selection start to 0 position
oSel.moveStart ('character', -oField.value.length);
// The caret position is selection length
iCaretPos = oSel.text.length;
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0')
iCaretPos = oField.selectionStart;
// Return results
return (iCaretPos);
}
/*
** Sets the caret (cursor) position of the specified text field.
** Valid positions are 0-oField.length.
*/
function set_caret_position (oField, iCaretPos) {
// IE Support
if (document.selection) {
// Set focus on the element
oField.focus ();
// Create empty selection range
var oSel = document.selection.createRange ();
// Move selection start and end to 0 position
oSel.moveStart ('character', -oField.value.length);
// Move selection start and end to desired position
oSel.moveStart ('character', iCaretPos);
oSel.moveEnd ('character', 0);
oSel.select ();
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0') {
oField.selectionStart = iCaretPos;
oField.selectionEnd = iCaretPos;
oField.focus ();
}
}
function get_element_value(elem) {
if (elem == null || elem == undefined)
return null;
if (elem.tagName == "INPUT") {
if (elem.type == "radio") {
if (elem.checked)
return elem.value;
} else if (elem.type == "checkbox") {
if (elem.checked)
return elem.value;
} else if (elem.type == "text") {
return elem.value;
} else if (elem.type == "hidden") {
return elem.value;
}
} else if (elem.tagName == "SELECT") {
return elem.options[elem.selectedIndex].value;
}
return null;
}
function get_element_formulaire(identifiant, name) {
var maform = document.forms[identifiant];
var form_elem = maform[name];
if (form_elem != null) {
var scalar = form_elem.length == undefined || form_elem.length == null;
if (!scalar && form_elem.length > 0 && form_elem.tagName != "SELECT") {
for (var champ=0; champ < form_elem.length; champ++) {
elem = form_elem[champ];
if (elem.type == "radio") {
if (elem.checked)
return elem;
}
}
return null;
} else {
return form_elem;
}
}
return form_elem;
}
function add_property(properties, name, value) {
if (name.match(/\[\]$/) == "[]") {
var temp = properties[name];
if (temp == null || temp == undefined) {
temp = [];
properties[name] = temp;
}
temp.push(value);
} else {
properties[name] = value;
}
return properties;
}
function html_content(identifiant, path, id) {
var tableId = path.length == 0 ? "content_" + id : "content_" + path + "_" + id ;
var div = document.getElementById(tableId);
if (div == null || div == undefined)
return;
var maform = document.forms[identifiant];
var properties = Array();
properties["varpath"] = path;
properties["id"] = id;
properties["identifiant"] = identifiant;
var elem, value;
for (var champ=0; champ < maform .elements.length; champ++) {
elem = maform.elements[champ];
if (elem.tagName == "INPUT") {
if (elem.type == "radio") {
if (elem.checked)
properties = add_property(properties, elem.name, elem.value);
} else if (elem.type == "checkbox") {
if (elem.checked)
properties = add_property(properties, elem.name, elem.value);
} else if (elem.type == "text") {
properties = add_property(properties, elem.name, elem.value);
} else if (elem.type == "hidden") {
value = elem.value;
if (elem.name == "validate_" + identifiant)
value = 0;
properties = add_property(properties, elem.name, value);
}
} else if (elem.tagName == "SELECT") {
value = elem.options[elem.selectedIndex].value;
properties = add_property(properties, elem.name, value);
}
}
var content = post_request("html_content.html", properties);
div.innerHTML = '
';
if (typeof check_layout === "function") {check_layout();}
}
function clear_content(path, id) {
var tableId = path.length == 0 ? "content_" + id : "content_" + path + "_" + id ;
var table = document.getElementById(tableId);
if (table == null || table == undefined)
return;
table.innerHTML = '';
if (typeof check_layout === "function") {check_layout();}
}
function init_date(field) {
if (field.value.length == 0) {
field.value = "__/__/____";
set_caret_position(field, 0);
}
}
function validate_date(field) {
var date_pattern=/^[0-9]{2}[\/]{1}[0-9]{2}[\/]{1}[0-9]{4}$/;
if(!field.value.match(date_pattern))
field.value = "";
return true;
}
function priv_del_char_in_date(text, index) {
var newText = text.substr(0, index);
for (var i = index; i < 10; i++) {
var c = text.charAt(i);
if (c == '/') {
newText += '/';
} else if (i < 9) {
c = text.charAt(i + 1);
if (c == '/')
c = text.charAt(i + 2);
newText += c;
} else {
newText += "_";
}
}
return newText;
}
function complete_date(field, event) {
var caret = get_caret_position(field);
var keynum;
if(window.event)
keynum = event.keyCode;
else if(event.which)
keynum = event.which;
var current = field.value.charAt(caret);
if (keynum == 46) {
field.value = priv_del_char_in_date(field.value, caret);
set_caret_position(field, caret);
return false;
} else if (keynum == 8) {
if (caret > 0) {
var previews = field.value.charAt(caret - 1);
if (previews == '/')
caret--;
}
if (caret > 0) {
var newText = field.value.substr(0, caret - 1);
newText += "_";
newText += field.value.substr(caret, 10 - caret);
field.value = newText;
set_caret_position(field, caret - 1);
return false;
}
} else if (current != '' && (current == '_' || !isNaN(current))) {
var n = null;
if (keynum > 47 && keynum < 58 )
n = keynum - 48;
else if (keynum > 95 && keynum < 106 )
n = keynum - 96;
if (n == null)
return true;
newText = field.value.substr(0, caret);
newText += n;
newText += field.value.substr(caret + 1, 10 - (caret + 1));
field.value = newText;
if (caret < 9 && field.value.charAt(caret + 1) == "/")
caret++;
set_caret_position(field, caret + 1);
}
return true;
}
function validate_adulte(field) {
field.style.color = 'black';
var date_pattern=/^[0-9]{2}[\/]{1}[0-9]{2}[\/]{1}[0-9]{4}$/;
if(!field.value.match(date_pattern)) {
field.value = "";
return true;
}
var strDate = field.value;
var day = strDate.substring(0,2);
var month = strDate.substring(3,5);
var year = strDate.substring(6,10);
var cur = new Date();
var max = new Date(cur.getYear() - 17, cur.getMonth(), cur.getDay());
var min = new Date(cur.getYear() - 100, cur.getMonth(), cur.getDay());
var dob = new Date(year, month, day);
if (min.getTime() > dob.getTime())
field.style.color = 'red';
else if (dob.getTime() > max.getTime())
field.style.color = 'red';
return true;
}
function validate_currency(field) {
field.style.color = 'black';
var text = field.value;
var clean = '';
var c;
for (var i = 0; i < text.length; i++) {
c = text.charAt(i);
if (c == ',' || c == '.') {
clean += ".";
} else if (c == 'k') {
clean += "000";
} else if (c == 'm') {
if ((i < text.length - 1) && (text.charAt(i + 1) == 'd'))
clean += "000000000";
else
clean += "000000";
} else if (c != ' ') {
clean += c;
}
}
if (isNaN(clean))
field.style.color = 'red';
var dFloat = parseFloat(clean);
if (dFloat < 0)
field.style.color = 'red';
}
function get_currency(text) {
if (text == null || text == undefined || text == '')
return 0;
var clean = '';
var c;
for (var i = 0; i < text.length; i++) {
c = text.charAt(i);
if (c == ',' || c == '.') {
clean += ".";
} else if (c == 'k') {
clean += "000";
} else if (c == 'm') {
if ((i < text.length - 1) && (text.charAt(i + 1) == 'd'))
clean += "000000000";
else
clean += "000000";
} else if (c != ' ') {
clean += c;
}
}
if (isNaN(clean))
return 0;
var dFloat = parseFloat(clean);
if (dFloat < 0)
return 0;
return dFloat;
}
function validate_integer(field) {
field.style.color = 'black';
if (field.value == '')
return;
if (field.value.search(/^-?[0-9]+$/) == -1)
field.style.color = 'red';
}
function validate_unsigned_integer(field) {
field.style.color = 'black';
if (field.value == '')
return;
if (field.value.search(/^[0-9]+$/) == -1)
field.style.color = 'red';
}
function html_add_row(identifiant, path, id) {
var inputId = path.length == 0 ? "v_" + id : "v_" + path + "_" + id ;
var input = get_element_formulaire(identifiant, inputId);
if (input == null || input == undefined)
return;
var size = parseInt(input.value);
input.value = size + 1;
html_content(identifiant, path, id);
}
function html_suppress_row(identifiant, path, id, index) {
var inputId = path.length == 0 ? "v_suppress_" + id : "v_suppress_" + path + "v_suppress_" + id ;
var input = get_element_formulaire(identifiant, inputId);
if (input == null || input == undefined)
return;
input.value = index;
html_content(identifiant, path, id);
input.value = -1;
inputId = path.length == 0 ? "v_" + id : "v_" + path + "_" + id ;
input = get_element_formulaire(identifiant, inputId);
if (input == null || input == undefined)
return;
var size = parseInt(input.value);
input.value = size - 1;
}
function frais_de_notaire(valeur, ancien) {
var frais_de_notaire = 0.0;
if (ancien != null && valeur != null && valeur > 0) {
var droit = (valeur * 0.0509) + (valeur * 0.001);
if (ancien != 2)
droit = (valeur * 0.00715) + (valeur * 0.001);
var emoluments = 0;
if (valeur <= 6500)
emoluments = valeur * 0.04;
else if (valeur <= 17000)
emoluments = (6500 * 0.04) + ((valeur - 6500) * 0.0165);
else if (valeur <= 60000)
emoluments = (6500 * 0.04) + (10500 * 0.0165) + ((valeur - 17000) * 0.0110);
else
emoluments = (6500 * 0.04) + (10500 * 0.0165) + (43000 * 0.0110) + ((valeur - 60000) * 0.00825);
//TVA
emoluments *= 1.196;
frais_de_notaire = droit + emoluments + 750;
frais_de_notaire = Math.round(frais_de_notaire);
}
return frais_de_notaire;
}
function validate_departement(formulaire, input, index) {
if (input.value == '')
return;
if (input.value.length == 1)
input.value = "0" + input.value;
var annexe;
var annexeName;
if (input.name.match(/\[\]$/) == "[]") {
annexeName = input.name.substr(0, input.name.length - 2) + "__annexe[]";
annexe = get_element_formulaire(formulaire, annexeName);
annexe = annexe != null ? annexe[index] : null;
} else {
annexeName = input.name + "__annexe";
annexe = get_element_formulaire(formulaire, annexeName);
}
if (annexe != null) {
var selected = 0;
for (var i = 1; i < annexe.options.length; i++) {
if (annexe.options[i].value == input.value)
selected = i;
}
annexe.selectedIndex = selected;
}
}
function change_departement(formulaire, annexe, index) {
var input;
var inputName;
if (annexe.name.match(/\[\]$/) == "[]") {
inputName = annexe.name.substr(0, annexe.name.length - 10) + "[]";
input = get_element_formulaire(formulaire, inputName);
input = annexe != null ? input[index] : null;
} else {
inputName = annexe.name.substr(0, annexe.name.length - 8);
input = get_element_formulaire(formulaire, inputName);
}
input.value = annexe.options[annexe.selectedIndex].value;
}
function init_select_check(formulaire, select) {
var cache = formulaire_cache[formulaire];
if (cache == null) {
cache = [];
formulaire_cache[formulaire] = cache;
}
formulaire_cache[formulaire][select.name] = select.selectedIndex;
}
function end_select_check(formulaire, select) {
var oldSelection = formulaire_cache[formulaire][select.name];
var currentSelection = select.selectedIndex;
if (oldSelection != currentSelection)
select.onchange();
}
function validate_name(field) {
var iChars = "`~!@#$%^&*()+=-_:[]\\;,./{}|\":<>?";
field.style.color = 'black';
var count = field.value.length;
var c;
for (var i = 0; i < count; i++) {
c = field.value.charAt(i);
if (iChars.indexOf(c) == -1)
continue;
field.style.color = 'red';
break;
}
}
function validate_zip(field) {
field.style.color = 'black';
if (field.value == '' || field.value == '0')
return;
if (field.value.search(/^[0-9]{5}$/) == -1)
field.style.color = 'red';
}
function mensulate_pour_duree(capital, duree) {
var taux = 0.0;
if (duree <= 5)
taux = 1.9;
else if (duree <= 10)
taux = 1.9;
else if (duree <= 15)
taux = 2.1;
else if (duree <= 20)
taux = 2.2;
else if (duree <= 25)
taux = 2.3;
else
taux = 0;
var taux_nominal = taux / 100.0;
var taux_periodique = taux_nominal / 12.0;
var nombre_echeances = duree * 12;
return mensualite_credit(capital, nombre_echeances, taux_periodique);
}
function mensualite_credit(montant, duree, taux) {
var puissance = Math.pow(1.0 + taux, duree);
var numerateur = montant * taux * puissance;
var denominateur = puissance - 1;
if (denominateur != 0)
return (numerateur / denominateur).toFixed(2);
else
return 0;
}
function set_responce_label(question, responce, label) {
var labelId = "l_" + question + "__" + responce ;
var labelElement = document.getElementById(labelId);
if (labelElement == null || labelElement == undefined)
return;
labelElement.innerHTML = label;
}
function check_password(field) {
var id = field.id;
var confirm = document.getElementById(id + "::confirm");
var image = document.getElementById(id + "::image");
var confirm_image = document.getElementById(id + "::confirm_image");
var pass = field.value;
if (confirm.value.length == 0) {
confirm_image.src = "/images/vide.png";
} else if (confirm.value == pass) {
confirm_image.title = "Votre mot de passe correspond à la confirmation.";
confirm_image.src = "/images/bon.png";
} else {
confirm_image.title = "Votre mot de passe ne correspond pas à la confirmation.";
confirm_image.src = "/images/mauvais.png";
}
if (pass.length == 0) {
image.src = "/images/mauvais.png";
return false;
} else {
var indice = chkPass(pass);
var equals = confirm.value == pass;
if (indice < 40) {
image.title = "Votre mot de passe n'est pas assez solide.";
image.src = "/images/mauvais.png";
return false;
} else if (indice < 70) {
image.title = "Votre mot de passe est assez solide.";
image.src = "/images/moyen.png";
return false;
} else {
image.title = "Votre mot de passe est très solide.";
image.src = "/images/bon.png";
return false;
}
}
}
function check_password_confirm(confirm) {
var id = confirm.id;
var field = id.substring(0, id.length - 9);
check_password(document.getElementById(field));
}
String.prototype.strReverse = function() {
var newstring = "";
for (var s=0; s < this.length; s++)
newstring = this.charAt(s) + newstring;
return newstring;
};
function chkPass(pwd) {
var nScore=0, nLength=0, nAlphaUC=0, nAlphaLC=0, nNumber=0, nSymbol=0, nMidChar=0, nRequirements=0, nAlphasOnly=0, nNumbersOnly=0, nUnqChar=0, nRepChar=0, nRepInc=0, nConsecAlphaUC=0, nConsecAlphaLC=0, nConsecNumber=0, nConsecSymbol=0, nConsecCharType=0, nSeqAlpha=0, nSeqNumber=0, nSeqSymbol=0, nSeqChar=0, nReqChar=0, nMultConsecCharType=0;
var nMultRepChar=1, nMultConsecSymbol=1;
var nMultMidChar=2, nMultRequirements=2, nMultConsecAlphaUC=2, nMultConsecAlphaLC=2, nMultConsecNumber=2;
var nReqCharType=3, nMultAlphaUC=3, nMultAlphaLC=3, nMultSeqAlpha=3, nMultSeqNumber=3, nMultSeqSymbol=3;
var nMultLength=4, nMultNumber=4;
var nMultSymbol=6;
var nTmpAlphaUC="", nTmpAlphaLC="", nTmpNumber="", nTmpSymbol="";
var sAlphas = "abcdefghijklmnopqrstuvwxyz";
var sNumerics = "01234567890";
var sSymbols = ")!@#$%^&*()";
var nMinPwdLen = 8;
nScore = parseInt(pwd.length * nMultLength);
nLength = pwd.length;
var arrPwd = pwd.replace(/\s+/g,"").split(/\s*/);
var arrPwdLen = arrPwd.length;
/* Loop through password to check for Symbol, Numeric, Lowercase and Uppercase pattern matches */
for (var a=0; a < arrPwdLen; a++) {
if (arrPwd[a].match(/[A-Z]/g)) {
if (nTmpAlphaUC !== "") { if ((nTmpAlphaUC + 1) == a) { nConsecAlphaUC++; nConsecCharType++; } }
nTmpAlphaUC = a;
nAlphaUC++;
}
else if (arrPwd[a].match(/[a-z]/g)) {
if (nTmpAlphaLC !== "") { if ((nTmpAlphaLC + 1) == a) { nConsecAlphaLC++; nConsecCharType++; } }
nTmpAlphaLC = a;
nAlphaLC++;
}
else if (arrPwd[a].match(/[0-9]/g)) {
if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
if (nTmpNumber !== "") { if ((nTmpNumber + 1) == a) { nConsecNumber++; nConsecCharType++; } }
nTmpNumber = a;
nNumber++;
}
else if (arrPwd[a].match(/[^a-zA-Z0-9_]/g)) {
if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
if (nTmpSymbol !== "") { if ((nTmpSymbol + 1) == a) { nConsecSymbol++; nConsecCharType++; } }
nTmpSymbol = a;
nSymbol++;
}
/* Internal loop through password to check for repeat characters */
var bCharExists = false;
for (var b=0; b < arrPwdLen; b++) {
if (arrPwd[a] == arrPwd[b] && a != b) { /* repeat character exists */
bCharExists = true;
/*
Calculate icrement deduction based on proximity to identical characters
Deduction is incremented each time a new match is discovered
Deduction amount is based on total password length divided by the
difference of distance between currently selected match
*/
nRepInc += Math.abs(arrPwdLen/(b-a));
}
}
if (bCharExists) {
nRepChar++;
nUnqChar = arrPwdLen-nRepChar;
nRepInc = (nUnqChar) ? Math.ceil(nRepInc/nUnqChar) : Math.ceil(nRepInc);
}
}
/* Check for sequential alpha string patterns (forward and reverse) */
for (var s=0; s < 23; s++) {
var sFwd = sAlphas.substring(s,parseInt(s+3));
var sRev = sFwd.strReverse();
if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqAlpha++; nSeqChar++;}
}
/* Check for sequential numeric string patterns (forward and reverse) */
for (var s=0; s < 8; s++) {
var sFwd = sNumerics.substring(s,parseInt(s+3));
var sRev = sFwd.strReverse();
if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqNumber++; nSeqChar++;}
}
/* Check for sequential symbol string patterns (forward and reverse) */
for (var s=0; s < 8; s++) {
var sFwd = sSymbols.substring(s,parseInt(s+3));
var sRev = sFwd.strReverse();
if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqSymbol++; nSeqChar++;}
}
/* Modify overall score value based on usage vs requirements */
/* General point assignment */
if (nAlphaUC > 0 && nAlphaUC < nLength)
nScore = parseInt(nScore + ((nLength - nAlphaUC) * 2));
if (nAlphaLC > 0 && nAlphaLC < nLength)
nScore = parseInt(nScore + ((nLength - nAlphaLC) * 2));
if (nNumber > 0 && nNumber < nLength)
nScore = parseInt(nScore + (nNumber * nMultNumber));
if (nSymbol > 0)
nScore = parseInt(nScore + (nSymbol * nMultSymbol));
if (nMidChar > 0)
nScore = parseInt(nScore + (nMidChar * nMultMidChar));
/* Point deductions for poor practices */
if ((nAlphaLC > 0 || nAlphaUC > 0) && nSymbol === 0 && nNumber === 0) { // Only Letters
nScore = parseInt(nScore - nLength);
nAlphasOnly = nLength;
}
if (nAlphaLC === 0 && nAlphaUC === 0 && nSymbol === 0 && nNumber > 0) { // Only Numbers
nScore = parseInt(nScore - nLength);
nNumbersOnly = nLength;
}
if (nRepChar > 0)
nScore = parseInt(nScore - nRepInc);
if (nConsecAlphaUC > 0)
nScore = parseInt(nScore - (nConsecAlphaUC * nMultConsecAlphaUC));
if (nConsecAlphaLC > 0)
nScore = parseInt(nScore - (nConsecAlphaLC * nMultConsecAlphaLC));
if (nConsecNumber > 0)
nScore = parseInt(nScore - (nConsecNumber * nMultConsecNumber));
if (nSeqAlpha > 0)
nScore = parseInt(nScore - (nSeqAlpha * nMultSeqAlpha));
if (nSeqNumber > 0)
nScore = parseInt(nScore - (nSeqNumber * nMultSeqNumber));
if (nSeqSymbol > 0)
nScore = parseInt(nScore - (nSeqSymbol * nMultSeqSymbol));
nRequirements = nReqChar;
if (pwd.length >= nMinPwdLen) { var nMinReqChars = 3; } else { var nMinReqChars = 4; }
if (nRequirements > nMinReqChars) // One or more required characters exist
nScore = parseInt(nScore + (nRequirements * 2));
/* Determine complexity based on overall score */
if (nScore > 100) { nScore = 100; } else if (nScore < 0) { nScore = 0; }
return nScore;
}