// Fonction trim pour la classe String
String.prototype.trim = function()
{
  return this.replace(/(?:^\s+|\s+$)/g, "");
}

// Retourne true si l'élément dont l'id est passé en paramètre est vide
function emptyValue(strId_P)
{
  return document.getElementById(strId_P).value.trim()=='';
}

// Retourne true si strNumber_P est un entier, false sinon
// iNumberOfChar_P permet de préciser si besoin est le nombre exact de caractères requis
function isInt(strNumber_P, iNumberOfChar_P)
{
  iNumberOfChar_P = iNumberOfChar_P || strNumber_P.length;
  var bInt_Ret=true;
  
  if (iNumberOfChar_P == strNumber_P.length)
  {
		var strValidChars = "0123456789";
		var char;
		var i=0;
		
		while (i < iNumberOfChar_P && bInt_Ret)
		{
			char = strNumber_P.charAt(i);
			if (strValidChars.indexOf(char) == -1)
			{
			  bInt_Ret = false;
			}
			i++;
		}
  }
  else
  {
    bInt_Ret = false;
  }
	
	return bInt_Ret;
}

function addErrorClass(strId_P)
{
  document.getElementById(strId_P).className = document.getElementById(strId_P).className+' erreur';
}

function removeErrorClass(strId_P)
{
  document.getElementById(strId_P).className = document.getElementById(strId_P).className.replace('erreur','');
}

function isChoiceSelected(strId_P)
{
  var oSelect = document.getElementById(strId_P);
  var i = 0;
  
  while (i<oSelect.length && !oSelect.options[i].selected)
  {
    i++;
  }
  
  return (i<oSelect.length && oSelect.options[i].selected);
}

function isEmail(strMail_P)
{
	strMail_P=strMail_P.toLowerCase();
	return (strMail_P.search(/^([a-z0-9])([a-z0-9\-\_])*(\.[a-z0-9\-\_]+)*\@([a-z0-9])([a-z0-9\-]*)(\.[a-z0-9\-]+)*\.([a-z]{2,6})$/) != -1);
}

function showErrorMessage()
{
  alert("Merci de remplir et/ou de vérifier les champs affichés en rouge.");
}

// Retourne true si 
// strType_P in (checkbox, radio, select, text)
// strOption_P in (mail, postCode, phone)
function checkElement(strId_P, strType_P, strOption_P)
{
  strType_P = strType_P||'text';
  strOption_P = strOption_P||'';
  
  var bNoError_Ret = true;
  
  removeErrorClass(strId_P);
  
  switch (strType_P)
  {
	  case 'text': 
	  {
	    bNoError_Ret=checkTextElement(strId_P, strOption_P);
	    break;
	  }
    
	  case 'checkbox': 
	  {
	    bNoError_Ret=checkCheckboxElement(strId_P);
	    break;
	  }
	  
	  default:
	  {
      break;
	  }
	}
  
  return bNoError_Ret;
}

// Retourne 
function checkTextElement(strId_P, strOption_P)
{
  var bNoError_Ret=true;

  if(emptyValue(strId_P))
  {
    bNoError_Ret=false;
  }
  else
  {
    if (strOption_P != '')
    {
      var strText = document.getElementById(strId_P).value;
      
      switch (strOption_P)
      {
        case 'mail': 
        {
          if(!isEmail(strText))
          {
            bNoError_Ret=false;
          }
          break;
        }
        
        case 'phone': 
        {
          if(!isInt(strText,10))
          {
            bNoError_Ret=false;
          }
          break;
        }
        
        case 'postCode': 
        {
          if(!isInt(strText,5))
          {
            bNoError_Ret=false;
          }
          break;
        }

        default:
        {
          break;
        }
      }
    }
  }
  
  if(!bNoError_Ret)
  {
    addErrorClass(strId_P);
  }
  
  return bNoError_Ret;
}

// Prend en paramètre l'id du bloc contenant les checkbox
function checkCheckboxElement(strId_P)
{
  var bNoError=false;
  var i=0;
  var arrayInput = document.getElementById('prestation').getElementsByTagName('input');
  
  while (!bNoError && i<arrayInput.length)
  {
    if(arrayInput[i].checked)
    {
      bNoError=true;
    }
    i++;
  }
  
  if (!bNoError)
  {
    addErrorClass('prestation');
  }
  
  return bNoError;
}

function validateForm()
{
  var bNoError=true;
  
  if(!checkElement('prestation', 'checkbox'))
  {
    bNoError=false;
  }
  
  if(!checkElement('project', 'text'))
  {
    bNoError=false;
  }

  if(!checkElement('name', 'text'))
  {
    bNoError=false;
  }
  
  if(!checkElement('address', 'text'))
  {
    bNoError=false;
  }
  
  if(!checkElement('city', 'text'))
  {
    bNoError=false;
  }

  if(!checkElement('mail', 'text','mail'))
  {
    bNoError=false;
  }

  if(!checkElement('postCode', 'text','postCode'))
  {
    bNoError=false;
  }

  if(!checkElement('phone', 'text','phone'))
  {
    bNoError=false;
  }

  if(!emptyValue('mobile'))
  {
    if(!checkElement('mobile', 'text','phone',true))
    {
      bNoError=false;
    }
  }

  if (!bNoError)
  {
    showErrorMessage();
  }
  
  return bNoError;
}

