var g_oFormValidation;
var g_sLastDisplayedPage = "";
function FormValidation(){
  var bPassed = true;
  var oForm = document.forms[0];  
    
  function displayError(sName){  
    if (document.getElementById("idError"+sName)){
      document.getElementById("idError"+sName).style.display = "block";
    }
    bPassed = false;  
  }
  
  function validateField(oFieldElement){
    switch(oFieldElement.getAttribute("validate")){
      case "email":
        sText = oFieldElement.value;
        sFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        processField((sFilter.test && sText!="" && !sFilter.test(sText)) , oFieldElement.name); 
        break;
      case "phone":
        sText = oFieldElement.value;          
        processField(!sText.match(/^(\d{2}-\d{7})|(\d{3}-\d{7})|(^)$/), oFieldElement.name);
        break;
      default:
        clearError(oFieldElement.name);
        break;
    }      
  }
  
  function checkField(oFieldElement){
    var aObligatoire=new Array();
    var sText, sFilter;
    
    
    aObligatoire["sLastName"]  = 1;
    aObligatoire["sFirstName"] = 1;
    aObligatoire["sCity"]      = 1;

    if (typeof(aObligatoire[oFieldElement.name])!="undefined" && oFieldElement.value==""){
      displayError(oFieldElement.name);
    }else {
      validateField(oFieldElement);      
    }
         
  }

    function processField(bFailed, sName){
      if (bFailed){
        displayError(sName);
      }else{
        clearError(sName);
      }      
    }
    
    function clearError(sName){
      if (document.getElementById("idError"+sName)){
        document.getElementById("idError"+sName).style.display = "none";
      }  
    }
  
  
  this.checkForm = function (oForm){
    bPassed = true;
    for (var i=0;i<oForm.elements.length;i++){   
      checkField(oForm.elements[i]);
    }    
    if (bPassed){
      oForm.submit();
    }
  };
  
  
  function initFieldValidation(){
    for (var i=0;i<oForm.elements.length;i++){
      if (oForm.elements[i].getAttribute("showError")==="1"){
         displayError(oForm.elements[i].name);
      }
      oForm.elements[i].onblur = function(){      
         checkField(this);        
      };      
    }
  }
  
  initFieldValidation();
}

window.onload = function(){
  g_oFormValidation = new FormValidation();
};

function backToPetition(oEvent, sDisplayedElemId)
{
  window.document.getElementById(sDisplayedElemId).style.display = "none";
  window.document.getElementById("idPetitionText").style.display = "block";
  cancelEvent(oEvent);
}

function cancelEvent(oEvent){
  //if event implements preventDefault call it
  if (oEvent.preventDefault) { oEvent.preventDefault()}
  //to make sure also follow MS standart to cancel event  
  oEvent.returnValue = false;
}

function showPage(oEvent, sDisplayedElemId)
{
  if (g_sLastDisplayedPage!==""){
    window.document.getElementById(g_sLastDisplayedPage).style.display = "none";
  }
  //save last displayed page.
  g_sLastDisplayedPage = sDisplayedElemId;  
  window.document.getElementById("idPetitionText").style.display = "none";
  window.document.getElementById(sDisplayedElemId).style.display = "block";
  cancelEvent(oEvent);
}
