/**
 * Fonction g�n�riques pour tout le site
 */

/**
 * fonction de validation d'un email
 * @param {Object} email: email a valider
 */
function validMail(email)
{
  email = email + "";
  var reg = /^([_a-z0-9-]+(\.[_a-z0-9-]+)*)@([_a-z0-9-]+(\.[_a-z0-9-]+)*)\.([a-z]{2,6})$/i;
  return reg.test(email);
}

/**
 * verifie que tous les champs obligatoires (qui ont une class css appelée "mandatory") sont remplis avant de submiter le form
 * verifie egalement que les champs dont le nom contient "email" sont au bon format
 */
function checkForm(form_id)
{
  if($(form_id))
  {
    elts = $(form_id).elements;
    txt = '';
    txtemail = '';
    for (i=0; i<elts.length; i++)
    {
      if (elts[i].className == "mandatory" && elts[i].value == '')
      {
        txt += '- '+elts[i].name + '\n';
      }
      if (elts[i].name.indexOf('email') > -1)//le nom du champ contient email, on peut supposer que c'est un email et faire les verifs
      {
        if (elts[i].value != '' && !validMail(elts[i].value))
        {
          txtemail = 'The email format is not valid !\n';
        }
      }
    }
    txt = (txt.length > 0) ? 'You must fill the following fields:\n'+txt : '';
    txt = (txtemail.length > 0) ? txt+'\n'+txtemail : txt;
    if(txt.length > 0)
    {
      alert(txt);
      return false;
    }
    $(form_id).submit();
  }
}

/**
 * dans le resumé de la commande, copie l'adresse de la company dans la billing adresse
 */
function copyAdr()
{
  if($('chkcopyAdr'))
  {
    chkbx = $('chkcopyAdr');
    if(chkbx.checked)
    {
      $('bcp_name').value = $F('cpn_name');
      $('bcp_address').value = $F('cpn_address');
      $('bcp_postcode').value = $F('cpn_postcode');
      $('bcp_city').value = $F('cpn_city');
      $('bcp_cty_id').value = $F('cty_id');
      $('bcp_sta_id').value = $F('sta_id');
      $('bcp_tel').value = $F('cpn_tel');
      $('bcp_num_intra').value = $F('cpn_num_intra');
    }
  }
}

function helpAutocomplete()
{
  //TODO: verifier si le focus n'est pas sur la suggest de l'autocomplete
  if($('company_auto_complete'))
  {
    new Ajax.Updater('company_detail', '/company/verifExist', 
      {parameters: {  company: $F('company')},
      asynchronous:true,
      evalScripts:true,
      onComplete:function(request, json){Element.hide('ajax_anim')},
      onLoading:function(request, json){Element.show('ajax_anim')}});
  }
}

/**
 * fait un appel en ajax pour verifier si la company existe. La page appelée affichera un formulaire si c'est pas le cas
 */
function checkVerifExist()
{
  new Ajax.Updater('new_company_contener', '/company_verifExist', 
      {parameters: {  company: $F('company')},
      asynchronous:true,
      evalScripts:true,
      onComplete:function(request, json){Element.hide('ajax_anim')},
      onLoading:function(request, json){Element.show('ajax_anim')}});
}

/**
 * appele sur le onKeyPress d'un input, limite la saisie des donnees a des chiffres et des points et le backspace [\b]
 */
function limitDecimal(e)
{
  if(document.all) e=window.event; // for IE
  curChar = (e.which > 0) ? String.fromCharCode(e.which) : String.fromCharCode(e.keyCode);
  if (!curChar.match(/\d|\.|[\b]/))
  {
    return false;
  }
}

/**
 * cache une div
 * @param {Object} id
 */
function hideDiv(id)
{
  if($(id))
  {
    $(id).style.display = 'none';
  }
}

/**
 * affiche une div
 * @param {Object} id
 */
function showDiv(id)
{
  if($(id))
  {
    $(id).style.display = 'block';
  }
}

/**
 * switch l'affichage d'une div passé en param, entre block et none
 * @param {Object} id
 */
function swapDisplay(id)
{
  var div = $(id);
  div.style.display = (div.style.display == "block") ? "none" : "block";
}

/**
 * Fonctions d'Adobe pour le rollover
 * @param {Object} arg
 */
function newImage(arg) {
  if (document.images) {
    rslt = new Image();
    rslt.src = arg;
    return rslt;
  }
}

function changeImages() {
  if (document.images && (preloadFlag == true)) {
    for (var i=0; i<changeImages.arguments.length; i+=2) {
      document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
    }
  }
}

var preloadFlag = false;
function preloadImages() {
  if (document.images) {
    btn_lyon_over = newImage("btn_lyon-over.gif");
    btn_sanfrancisco_over = newImage("btn_sanfrancisco-over.gif");
    preloadFlag = true;
  }
}

/**
 * limit le nombre de caracteres saisis dans un champ 'obj' a une taille de 'limit'
 * @param {Object} obj : l'objet dont on doit limiter la value (ce n'est pas l'id de l'objet)
 * @param {Object} limit : limite, en nb caracteres
 * @param {Object} tooltip : eventuellement, ID d'un tag ou on indique le nb caracteres tapés (idealement, un SPAN)
 */
function limitChar(obj,limit,tooltip)
{
  if(obj && obj.id && $(obj.id) && $F(obj.id))
  {
    content = $F(obj.id);
    content = content.substring(0,limit);
    $(obj.id).value = content;
    
    if($(tooltip))
    {
      $(tooltip).innerHTML = limit - content.length;
    }
  }
}

/**
 * print_r & r_print_r permettent d'emuler la fonction print_r du PHP
 */
function print_r(obj) {
  win_print_r = window.open('about:blank', 'win_print_r');
  win_print_r.document.write('<html><body>');
  r_print_r(obj, win_print_r);
  win_print_r.document.write('</body></html>');
 }

 function r_print_r(theObj, win_print_r) {
  if(theObj.constructor == Array ||
   theObj.constructor == Object){
   if (win_print_r == null)
    win_print_r = window.open('about:blank', 'win_print_r');
   }
   for(var p in theObj){
    if(theObj[p].constructor == Array||
     theObj[p].constructor == Object){
     win_print_r.document.write("<li>["+p+"] =>"+typeof(theObj)+"</li>");
     win_print_r.document.write("<ul>")
     r_print_r(theObj[p], win_print_r);
     win_print_r.document.write("</ul>")
    } else {
     win_print_r.document.write("<li>["+p+"] =>"+theObj[p]+"</li>");
    }
   }
  win_print_r.document.write("</ul>")
 }
/**********************************************************************/


