var messages = '';
var errors = '';

function formControlListener(nForm) {
  var bIsValide = true;
  var aFormCtrlSchemes = [["isNotEmpty","Le champ \"%s\" doit être renseigné."],
    ["isCheck","Le champs \"%s\" est obligatoire."],
    ["isInt","Le champ \"%s\" ne correspond pas à un entier valide."]];
  var cLabels = nForm.getElementsByTagName("label");
  var nField;
  for (var i=0; bIsValide && i<cLabels.length; i++) {
    if ((cLabels[i].htmlFor=="") ||
      !(nField=document.getElementById(cLabels[i].htmlFor))) continue;
    for (var j=0; bIsValide && aFormCtrlSchemes[j]; j++) {
      if (hasClassName(cLabels[i],aFormCtrlSchemes[j][0])) {
        if (!eval(aFormCtrlSchemes[j][0]+"(nField.value, cLabels[i].htmlFor, nField.checked)")) {
          bIsValide = false;
          var textContent = getTextContent(cLabels[i]).replace(/\s{2,}/g," ");
          textContent = textContent.replace(/^[\s:*]+|[\s:*]+$/g,"");
          alert(aFormCtrlSchemes[j][1].replace("%s",textContent));
          if (aFormCtrlSchemes[j][0] != "isCombo"){
          	nField.focus();
          }
        }
      }
    }
  }

  return bIsValide;
}
function isNotEmpty(s) {return s.replace(/^\s+|\s+$/g,"")!="";}
function isInt(s) {return isNotEmpty(s)?parseInt(s, 10)==s:true;}
function hasClassName(oNode,className) {
    return ((" "+oNode.className+" ").indexOf(" "+className+" ")!=-1);
}
function isCheck(a,b,c) {
	return c;
}
function getTextContent(oNode) {
  if (typeof(oNode.textContent)!="undefined") {return oNode.textContent;}
  switch (oNode.nodeType) {
    case 3: // TEXT_NODE
    case 4: // CDATA_SECTION_NODE
      return oNode.nodeValue;
      break;
    case 8: // COMMENT_NODE
    case 7: // PROCESSING_INSTRUCTION_NODE
      if (getTextContent.caller!=getTextContent) {
        return oNode.nodeValue;
      }
      break;
    case 9: // DOCUMENT_NODE
    case 10: // DOCUMENT_TYPE_NODE
    case 12: // NOTATION_NODE
      return null;
      break;
  }
  var _textContent = "";
  oNode = oNode.firstChild;
  while (oNode) {
    _textContent += getTextContent(oNode);
    oNode = oNode.nextSibling;
  }
  return _textContent;
}

function initpatchword(id){  
  document.getElementById(id).value = '';
}

function initpatchwordedit(id){  
  document.getElementById(id).value = '';
  document.getElementById(id+'_id').value = '';
}

function showslide(img){
  document.getElementById('detail_slide_img').src = img;
}

function initpatchcolor(id,input_id){  
  document.getElementById(id).style.backgroundColor = '#191919';
  document.getElementById(input_id).value = '';
}

function initallpatchcolor(){  
  document.getElementById('select_colors1').style.backgroundColor = '#191919';
  document.getElementById('color1').value = '';
  document.getElementById('select_colors2').style.backgroundColor = '#191919';
  document.getElementById('color2').value = '';
  document.getElementById('select_colors3').style.backgroundColor = '#191919';
  document.getElementById('color3').value = '';
  document.getElementById('select_colors4').style.backgroundColor = '#191919';
  document.getElementById('color4').value = '';
  document.getElementById('select_colors5').style.backgroundColor = '#191919';
  document.getElementById('color5').value = '';
  document.getElementById('select_colors6').style.backgroundColor = '#191919';
  document.getElementById('color6').value = '';
}

function copierpatchcolor(color){  
 ok = false;
 if (document.getElementById('color1').value == ''){
     document.getElementById('color1').value = color;
     document.getElementById('select_colors1').style.backgroundColor = '#'+color;
     ok = true;
 }
 if (!ok && document.getElementById('color2').value == ''){
     document.getElementById('color2').value = color;
     document.getElementById('select_colors2').style.backgroundColor = '#'+color;
     ok = true;
 }
 if (!ok && document.getElementById('color3').value == ''){
     document.getElementById('color3').value = color;
     document.getElementById('select_colors3').style.backgroundColor = '#'+color;
     ok = true;
 }
 if (!ok && document.getElementById('color4').value == ''){
     document.getElementById('color4').value = color;
     document.getElementById('select_colors4').style.backgroundColor = '#'+color;
     ok = true;
 }
 if (!ok && document.getElementById('color5').value == ''){
     document.getElementById('color5').value = color;
     document.getElementById('select_colors5').style.backgroundColor = '#'+color;
     ok = true;
 }
 if (!ok && document.getElementById('color6').value == ''){
     document.getElementById('color6').value = color;
     document.getElementById('select_colors6').style.backgroundColor = '#'+color;
     ok = true;
 }
    
}

function copierpatchword(word){  
 ok = false;
 if (document.getElementById('word1').value == ''){
     document.getElementById('word1').value = word;
     ok = true;
 }
 if (!ok && document.getElementById('word2').value == ''){
     document.getElementById('word2').value = word;
     ok = true;
 }
 if (!ok && document.getElementById('word3').value == ''){
     document.getElementById('word3').value = word;
     ok = true;
 }
 if (!ok && document.getElementById('word4').value == ''){
     document.getElementById('word4').value = word;
     ok = true;
 }
 if (!ok && document.getElementById('word5').value == ''){
     document.getElementById('word5').value = word;
     ok = true;
 }
 if (!ok && document.getElementById('word6').value == ''){
     document.getElementById('word6').value = word;
     ok = true;
 }
 if (!ok && document.getElementById('word7').value == ''){
     document.getElementById('word7').value = word;
     ok = true;
 }
 if (!ok && document.getElementById('word8').value == ''){
     document.getElementById('word8').value = word;
     ok = true;
 }
    
}

function copierpatchwordedit(word, id){  
 ok = false;
 if (document.getElementById('word1').value == ''){
     document.getElementById('word1').value = word;
     document.getElementById('word1_id').value = id;
     ok = true;
 }
 if (!ok && document.getElementById('word2').value == ''){
     document.getElementById('word2').value = word;
     document.getElementById('word2_id').value = id;
     ok = true;
 }
 if (!ok && document.getElementById('word3').value == ''){
     document.getElementById('word3').value = word;
     document.getElementById('word3_id').value = id;
     ok = true;
 }
 if (!ok && document.getElementById('word4').value == ''){
     document.getElementById('word4').value = word;
     document.getElementById('word4_id').value = id;
     ok = true;
 }
 if (!ok && document.getElementById('word5').value == ''){
     document.getElementById('word5').value = word;
     document.getElementById('word5_id').value = id;
     ok = true;
 }
 if (!ok && document.getElementById('word6').value == ''){
     document.getElementById('word6').value = word;
     document.getElementById('word6_id').value = id;
     ok = true;
 }
 if (!ok && document.getElementById('word7').value == ''){
     document.getElementById('word7').value = word;
     document.getElementById('word7_id').value = id;
     ok = true;
 }
 if (!ok && document.getElementById('word8').value == ''){
     document.getElementById('word8').value = word;
     document.getElementById('word8_id').value = id;
     ok = true;
 }
    
}

function gotourl(url){  
 window.location= url;  
}

onLoad = function()
{
    if (errors) {
        alertError(errors);
    } else if (messages) {
        alertSuccess(messages);
    }
    $j('a[rel*=popup]').facebox();
}

alertError = function(message)
{
    jQuery.facebox("<div class=\"alert alertError\"><p>" + message + "</p></div>");
}

alertSuccess = function(message)
{
    jQuery.facebox("<div class=\"alert alertSuccess\"><p>" + message + "</p></div>");
    setTimeout("$j.facebox.close()", 3500);
}

id = function(id)
{
    return document.getElementById(id);
}


