// JavaScript Document
/*
* Function to check forms that have mandatory fields.
* Usage: <FORM name="formName" action="actionUrl.php" method="POST" onSubmit="checkForm(new Array("fieldName1","fieldName2"),new Array("type1","type2"))">
* Types:
* notEmpty
* email
* onlyNumbers
* onlyDoubles
* initials
* password
* selectNotEmpty (empty or initial select item should have value -1)
* checkbox
* * = not implemented yet
*/
function checkForm(myForm, fields, types){

  var result = true;
  var isEmpty = false;
  var invalidEmail = false;
  var notANumber = false;
  var notADouble = false;
  var invalidPassword = false;
  var invalidPasswordLength = false;
  var selectIsEmpty = false;
  var checkboxNotChecked = false;
  var notInitial = false;
   
  for (i=0; i<fields.length; i++){
    switch(types[i]){
      case '':
        if(!checkNotEmpty(myForm[fields[i]])){result=false;isEmpty=true;}
      break;
      case null:
        if(!checkNotEmpty(myForm[fields[i]])){result=false;isEmpty=true;}
      break;
      case 'checkbox':
        if(!checkboxChecked(myForm[fields[i]])){result=false; checkboxNotChecked=true;}
      break;
      case 'notEmpty':
        if(!checkNotEmpty(myForm[fields[i]])){result=false;isEmpty=true;}
      break;
      case 'selectNotEmpty':
        if(!checkSelectNotEmpty(myForm[fields[i]])){result=false;selectIsEmpty=true;}
      break;
      case 'email':
        if(!validateEmail(myForm[fields[i]])){result=false;invalidEmail=true;}
      break;
      case 'onlyNumbers':
        if(!validateOnlyNumber(myForm[fields[i]])){result=false;notANumber=true;}
      break;
      case 'onlyDoubles':
        if(!validateOnlyDouble(myForm[fields[i]])){result=false;notADouble=true;}
      break;
      case 'password':
        if(!validatePassword(myForm[fields[i][0]],myForm[fields[i][1]])){
          result=false;
          invalidPassword=true;
        }else if(!checkPasswordLength(myForm[fields[i][0]],5)){
          result=false;
          invalidPasswordLength=5;
        }
      break;
    }
  }
  if(isEmpty){
    alert("U heeft niet alle verplichte velden ingevuld.");
  }
  if(selectIsEmpty){
    alert("Please select an item in the select box.");
  }
  if(invalidEmail){
    alert("U heeft geen geldig e-mail adres ingevuld.");
  }
  if(notANumber){
    alert("De rode velden mogen alleen cijfers bevatten.");
  }
  if(notADouble){
    alert("The red fields can only contain decimal numbers.");
  }
  if(invalidPassword){
    alert("The two passwords do not match.");
  }
  if(invalidPasswordLength!=false){
    alert("The password length should be a minimum of "+invalidPasswordLength+" characters.");
  }
  if(checkboxNotChecked){
    alert("Please check the checkbox to continue.");
  }
  return result;
}

function checkPasswordLength(lengthPass, passLength){
  if(lengthPass.value.length<passLength){
    lengthPass.focus();
    lengthPass.style.backgroundColor="#99CCFF";
    return false;
  }
  lengthPass.style.backgroundColor="";
  return true;
}

function validatePassword(firstPass, secondPass){
    if(!(firstPass.value==secondPass.value)){
      firstPass.focus();
      firstPass.style.backgroundColor="#99CCFF";
      secondPass.style.backgroundColor="#99CCFF";
      return false;
    }
    firstPass.style.backgroundColor="";
    secondPass.style.backgroundColor="";
    return true;
}

function checkboxChecked(myObject){
  if(!myObject.checked){
      myObject.focus();
      myObject.style.backgroundColor="#99CCFF";
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function checkNotEmpty(myObject){
  if(myObject.value==""){
      myObject.focus();
      myObject.style.backgroundColor="#99CCFF";
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function checkSelectNotEmpty(myObject){
  if(myObject.options[myObject.selectedIndex].value=="-1"){
    myObject.focus();
    myObject.style.backgroundColor="#E0A724";
    return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function validateEmail(myObject){
  
if(!myObject.value.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)){
      myObject.style.backgroundColor="#99CCFF";
      myObject.focus();
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}


function validateOnlyNumber(myObject){
  if(!myObject.value.match(/^[0-9]+$/)){
      myObject.style.backgroundColor="#FF6633";
      myObject.focus();
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function validateOnlyDouble(myObject){
  if(!myObject.value.match(/^[0-9]+\.?[0-9]*$/)){
      myObject.style.backgroundColor="#FF6633";
      myObject.focus();
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

// Andere Javascript functies

function showStatus(sMsg) {
    window.status = sMsg ;
    return true ;
}

