/*==================================================================================
	mmd_simulgen.js 28/04/1999
	par Serge Juillerat
	Modification: 03/03/2000
	Philippe Ney
        Modification: juillet 2002
        BCV - CGI
        Fonction generale utilisée dans les simulations
	---------------------------------------------------------------
===================================================================================*/

/*===================================================================================*/

//window.onerror = top.the_error();
var details_calcul = "detail calcul";
var details_calcul_final = "";
var resultat_calcul = "resultat calcul";
var printable = 0;

// Check the type of calculation chosen by user


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Format the number with a "'" for each thousand it find
//
function Format(number)
{
  var number_instring = new String(parseInt(number))
  var new_number = "";

//  alert("longueur: " + number_instring.length);

  if (number_instring.length > 3)
  {
    if (number_instring.length > 9)
    {
      new_number = number_instring.substring(0,(number_instring.length)-9) + "'" + number_instring.substring((number_instring.length)-9,(number_instring.length)-6) + "'" + number_instring.substring((number_instring.length)-6,(number_instring.length)-3) + "'" + number_instring.substring((number_instring.length)-3,number_instring.length);
    }
    else if (number_instring.length > 6)
    {
      new_number = number_instring.substring(0,(number_instring.length)-6) + "'" + number_instring.substring((number_instring.length)-6,(number_instring.length)-3) + "'" + number_instring.substring((number_instring.length)-3,number_instring.length);
    }      
    else if (number_instring.length > 3)
    {
      new_number = number_instring.substring(0,(number_instring.length)-3) + "'" + number_instring.substring((number_instring.length)-3,number_instring.length);
   }     
//    alert("formate: " + new_number);
    return new_number;
  }
  else
  {
    return number_instring;
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check if user has entered something in the field
//
function ValidLength(item,len)
{
  return (item.length > len);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check if user has entered a number under "len" for the month field
//
function ValidMonth(item,len)
{
  return (item.value < len);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check if user has entered a correct date
//
function ValidDate(item,len)
{
  return (item.value < len);
}

//
// Check if the date is 4 caracter long
// If the date is 4 caracter long, it verify that there is only number in the string
//
function ValidYear(item)
{
  errfound = false;
  if (item.value.length < 4) 
    error(item, "Année de naissance non valide");
  else
    verifNum(item);
}

//
// Check if user has entered a number under 100 for the percent field
//
function ValidPercent(item)
{
  return (item.value <= 100)
}

//
// Check if user has entered a valid float number
//
function ValidCote(item)
{
  var check = item.value.indexOf(",");
  if (check)
  {
    item.value = item.value.substring(0,check) + "." + item.value.substring(check+1,item.value.length);
  }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Alert user and show him the wrong field
//
function error(elem, text)
{
  if (errfound) return;
  window.alert(text);
  elem.select();
  elem.focus();
  errfound = true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check the field so that only number can pass through the test
//
function verifNum(valeur)
{
  for (var i=0; i < valeur.value.length; i++)
  {
    var caractere = valeur.value.substring(i,i+1);

    if (caractere < "0" || caractere > "9")
    {
      if (caractere == ".")
        break;

      alert ("entrez des chiffres uniquement !");
      valeur.value = "";
      valeur.select();
      valeur.focus();
      return false;
    }
  }
  return true;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Show the string in parameter in a new window
//
function print_Result()
{
  if (printable == 0)
  {
	result = self.top.open("","","height=400,width=400,menubar");
    result.focus();
    result.document.open();
  
    result.document.writeln('<html>');
    result.document.writeln('<link rel="stylesheet" href="http://www.bcv.ch/static/style/site/popup.css" type="text/css">');
    result.document.writeln('<link rel="stylesheet" href="http://www.bcv.ch/static/style/site/popup-print.css" type="text/css">');
    result.document.writeln('<body>');
    result.document.writeln('<img src="http://www.bcv.ch/static/images/commun/bcv_logo.gif" border="0">'); 
    result.document.writeln('<table border="0" width="400" class="tabSimple" style="padding-left:15px;padding-right:15px;">');
    result.document.writeln('<th width="350"></th>');
    result.document.writeln('<tr>');
    result.document.writeln('<td>');
    result.document.writeln('<h2>R&eacute;sultat du calcul</h2><p></p>');
    result.document.writeln('</td>');
    result.document.writeln('</tr>');
    result.document.writeln('<tr>');
    result.document.writeln('<td><p></p>');
    result.document.write(resultat_calcul);
    result.document.writeln('<p>');
    result.document.writeln('Envie d&#146;ouvrir un plan d&#146;&eacute;pargne en fonds ? Remplissez sans plus attendre notre <A HREF="JavaScript:window.opener.top.location.href=\'/bcv/header/contact/particuliers/commander_une_prestation\';window.close();"> formulaire de commande</A>.<br /><br /> ');
    result.document.writeln('<p>');
    result.document.writeln('Des questions? Besoin de pr&eacute;cisions? Un <A HREF="JavaScript:window.opener.top.location.href=\'/bcv/header/contact/particuliers/prendre_un_rendez_vous\';window.close();"> conseiller BCV</A> ');
    result.document.writeln('se tient volontiers &agrave; votre disposition pour envisager avec vous des solutions en fonction de votre situation propre.<p></p>');  
    result.document.writeln('</td>');
    result.document.writeln('</table>');
    result.document.writeln("</body>");
    result.document.writeln("</html>");
    result.document.close();
    resultat_calcul = "";
  }
  else if (printable == 2)
  {
	result = self.top.open("","","height=300,width=400,menubar,scrollbars");
    result.focus();
    result.document.open();
  
    result.document.writeln('<html>');
    result.document.writeln('<link rel="stylesheet" href="http://www.bcv.ch/static/style/site/popup.css" type="text/css">');
    result.document.writeln('<link rel="stylesheet" href="http://www.bcv.ch/static/style/site/popup-print.css" type="text/css">');
    result.document.writeln('<body bgcolor="#FFFFFF">');
    result.document.writeln('<img src="http://www.bcv.ch/static/images/commun/bcv_logo.gif" border="0">'); 
    result.document.writeln('<table border="0" width="400">');
    result.document.writeln('<th width="40"></th>');
    result.document.writeln('<th width="15"></th>');
    result.document.writeln('<th width="305"></th>');
    result.document.writeln('<th width="40"></th>');
    result.document.writeln('<tr>');
    result.document.writeln('<td>&nbsp;</td>');
    result.document.writeln('<td colspan="2">');
    result.document.writeln('<strong>R&eacute;sultat du calcul</strong>');
    result.document.writeln('</td>');
    result.document.writeln('<td></td>');
    result.document.writeln('</tr>');
    result.document.writeln('<tr>');
    result.document.writeln('<td colspan="2">&nbsp;</td>');
    result.document.writeln('<td>');
    result.document.write(resultat_calcul);
    result.document.writeln('<p>');
    result.document.writeln('Des questions? Besoin de pr&eacute;cisions? Un <A HREF="JavaScript:window.opener.top.location.href=\'/bcv/header/contact/particuliers/prendre_un_rendez_vous\';window.close();"> conseiller BCV</A> ');
    result.document.writeln('se tient volontiers &agrave; votre disposition pour envisager avec vous des solutions en fonction de votre situation propre.');  
    result.document.writeln('</td>');
    result.document.writeln('<td></td>');
    result.document.writeln('</table>');
    result.document.writeln('<table width="98%" border=0><tr><td ALIGN=RIGHT>');
    result.document.writeln('<A HREF="javascript:window.close();">');
    result.document.writeln('Fermer</a></td></tr>');
    result.document.writeln('</table>');
    result.document.writeln("</body>");
    result.document.writeln("</html>");
    result.document.close();
    resultat_calcul = "";

    printable = 0;
  }
}



// Fonction pour loader les images suivant IHB
function loadImage(sectionName,contentName,imageName)
{
  var s1;
  var s2;
  var position;
  
  s1 = window.location.pathname;
  position = s1.indexOf("/content/");
  s2 = s1.substring(0,position);
  s2 += "/media/images/fr/ihb_content/" + sectionName + "/" + contentName + "/" + imageName;
  return s2;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Show the string in parameter
//
function print_Detail()
{
  if (printable == 0)
  {
  	result = self.top.open("","","height=400,width=400,scrollbars,menubar");
    result.focus();
    result.document.open();
    result.document.write(details_calcul_final);
    result.document.close();
    details_calcul = "";
    details_calcul_final = "";
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculate the rest of a calculation
//
function rest(valeur,periode)
{
  tmp = valeur/periode;

  for (var i=0; i < tmp.toString().length; i++)
  {
    var caractere = tmp.toString().substring(i,i+1);

    if (caractere == ".")
    {
      return false;
    }
  }
  return true;
}

