var gsIdFrame = "";

function openIFrame(asIdFrame)
{
   gsIdFrame = asIdFrame;
   if (document.getElementById(asIdFrame).style.display=='block')
   {
      document.getElementById(asIdFrame).style.display = 'none';
   }
   else
   {
      try
      {
         document.onclick = check;
      }
      catch(e) {}
      document.getElementById(asIdFrame).style.display = 'block';
      try
      {
      	 document.getElementById(asIdFrame).contentWindow.init();
      }
      catch(e) {}
   }
}

function closeIFrame(asIdFrame)
{
   document.getElementById(asIdFrame).style.display = 'none';
}

function check(aoEvt)
{
   var target = (aoEvt && aoEvt.target) || (event && event.srcElement);
   var obj = document.getElementById(gsIdFrame);
   var obj2 = document.getElementById(gsIdFrame+'Img');
   if (target == obj2)
   {
      if (obj.style.display == "block")
      {
         obj.style.display = 'block';
      }
      else
      {
         obj.style.display = 'none';
      }
   }
   else if (checkParent(target))
   {
      obj.style.display = 'none';
   }
}

/**
 * @FunctionFR checkParent » Vérifier si l'objet est dans l'iframe
 * @FunctionEN checkParent » Check if the object is in the iframe
 * @ParamFR aoObject Object / » Objet du DOM
 * @ParamEN aoObject Object / » Object of the DOM
 * @ReturnFR Boolean » Objet est dans l'iframe
 * @ReturnEN Boolean » Object is in the iframe
 */
function checkParent(aoObject)
{
   while (aoObject.parentNode)
   {
      if (aoObject == document.getElementById(gsIdFrame))
      {
         return false;
      }
      aoObject = aoObject.parentNode;
   }
   return true;
}

function openWindow(asPage, anWidth, anHeight)
{
   if (!anWidth)
   {
      anWidth = screen.width;
   }
   if (!anHeight)
   {
      anHeight = screen.height;
   }
   if (anHeight > screen.height)
   {
      anHeight = screen.height;
      anWidth += 24;
   }
   anPosX = (screen.width - anWidth) / 2;
   anPosY = (screen.height - anHeight) / 2;
   asName = 'window1';
   window1 = window.open(asPage,asName,"toolbar=no,location=no,dependent=yes,directories=no,status=yes,menubar=no,resizable=yes,scrollbars=yes,left="+anPosX+",screenX="+anPosX+",top="+anPosY+",screenY="+anPosY+",width="+anWidth+",height="+anHeight);
}

function CbRbChecked(aaCheckList)
{
   for (var n = 0 ; n < aaCheckList.length ; n++)
   {
      if (document.getElementById(aaCheckList[n]))
      {
         if (document.getElementById(aaCheckList[n]).checked)
         {
            return false;
         }
      }
   }
   return true;
}

function ctlTextArea(asFieldId, anMaxLength)
{
   var txt = document.getElementById(asFieldId).value;
   var nb  = document.getElementById(asFieldId).value.length;
   if (nb > anMaxLength)
   {
      document.getElementById(asFieldId).value=txt.substring(0, anMaxLength);
      nb = anMaxLength;
   }
   if (document.getElementById(asFieldId + 'NbCar'))
   {
      document.getElementById(asFieldId + 'NbCar').innerHTML = anMaxLength - document.getElementById(asFieldId).value.length;
   }
}

function initCheckList(asIdCheckList)
{
   if (document.getElementById(asIdCheckList))
   {
      var laCheckList = document.getElementById(asIdCheckList).getElementsByTagName('input');
      for (var n = 0 ; n < laCheckList.length ; n++)
      {
         if (document.getElementById(laCheckList[n].id.substr(0, laCheckList[n].id.length-2)))
         {
            var loDivCheckBox = document.getElementById(laCheckList[n].id.substr(0, laCheckList[n].id.length-2));
            if (laCheckList[n].checked)
            {
               loDivCheckBox.className = "checklistChecked";
            }
            loDivCheckBox.onmouseover = function()
            {
               if (this.className == "option")
               {
                  this.className = "option checklistHover";
               }
            }
            loDivCheckBox.onmouseout = function()
            {
               if (this.className == "option checklistHover")
               {
                  this.className = "option";
               }
            }
            loDivCheckBox.onclick = function()
            {
               if (document.getElementById(this.id+"Cb"))
               {
                  var loCheckBox = document.getElementById(this.id+"Cb");
                  if (loCheckBox.checked)
                  {
                     loCheckBox.checked = false;
                     this.className = "option checklistHover";
                  }
                  else
                  {
                     loCheckBox.checked = true;
                     this.className = "option checklistChecked";
                  }
               }
               checkControls(new Array(new Array('Obligatory',document.getElementById(asIdCheckList),'checkListChecked(\''+asIdCheckList+'\')','FR','O', asIdCheckList+'Span', document.getElementById(asIdCheckList))));
            }
            laCheckList[n].onclick = function()
            {
               if (this.checked)
               {
                  this.checked = false;
               }
               else
               {
                  this.checked = true;
               }
            }
         }
      }
   }
}

function checkListChecked(asIdCheckList)
{
   if (document.getElementById(asIdCheckList))
   {
      var laCheckList = document.getElementById(asIdCheckList).getElementsByTagName('input');
      for (var n = 0 ; n < laCheckList.length ; n++)
      {
         if (laCheckList[n].checked)
         {
            return false;
         }
      }
      return true;
   }
}

function ctlTelephone(asFieldId)
{
   var txt = document.getElementById(asFieldId).value;
   var re1 = /^\+?[0-9 ]*(\([0-9 ]+\))*[0-9 ]*$/;
   var re2 = /[0-9]{6,19}$/;
   if (txt.length == 0) return false;
   return !(re1.test(txt) && re2.test(txt.replace(/[+ \(\)]/g, "")));
}

function urlCorrect(asFieldId) {
   if (document.getElementById(asFieldId).value.length == 0) return false;
   if (document.getElementById(asFieldId).value.substr(0,7) == 'http://' || document.getElementById(asFieldId).value.substr(0,8) == 'https://') {
      return false;
   } else {
      return true;
   }
}

function popup(asUrl, anWidth, anHeight)
{
   window.open(asUrl, 'confirmation', 'resizable=no, location=no, width='+anWidth+', height='+anHeight+', menubar=no, status=no, scrollbars=no, menubar=no, top='+(screen.height - anHeight)/2+', left='+(screen.width - anWidth)/2+', dependent=yes, alwaysRaised=yes');
}

function addEventsTextbox()
{
   inputs = document.getElementsByTagName('input');
   //cycle trough the input fields
   for (var i=0; i < inputs.length; i++)
   {
      //check if the input is a textbox
      if (inputs[i].getAttribute('type') == 'text')
      {
         if (inputs[i].onkeyup && !document.getElementById(inputs[i].id+'Id') && !document.getElementById(inputs[i].id+'Name'))
         {
            inputs[i].onblur = inputs[i].onkeyup;
            if (inputs[i].onchange)
            {
               inputs[i].onblur += inputs[i].onchange;
            }
         }
      }
   }
}
