function check_fields(){
    for (var i = 0; i < document.application.elements.length; i++) {
      if (document.application.elements[i].name == "USER.FIRSTNAME") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut fornavn.");
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.LASTNAME") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut etternavn.");
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.ADDRESS") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut din adresse.");
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.ZIPCODE") {
        if (! validate_zipcode(document.application.elements[i].value)) {
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.CITY") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut ditt poststed.");
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.MEMBERTYPE") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut medlemstype");
          return false;
        }
      }
      //if (document.application.elements[i].name == "USER.EMAIL") {
      //  if (! validate_email(document.application.elements[i].value)) {
      //    return false;
      //  }
      //}
      if (document.application.elements[i].name == "USER.BIRTHDATE") {
        if (! validate_birthdate(document.application.elements[i].value)) {
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.SEX") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut kjønn.");
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.INJURY") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut om du er brannskadd.");
          return false;
        }
      }
      if (document.application.elements[i].name == "USER.MEMBERTYPE") {
        if (document.application.elements[i].value == '') {
          window.alert("Vennligst fyll ut medlemstype.");
          return false;
        }
      }
    }
    return true;
}


function validate_email(email_str){
  var email_filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

  if (!email_filter.test(email_str)){
    alert("Epostadressen er ikke gyldig: Eksempel pa riktig adresse er: fornavn.etternavn@firma.no");
    return false;
  }  
  else {
    return true;
  }
} 

function validate_zipcode(zipcode_str){
  var zipcode_filter = /^([\d]+)$/i;

  if (zipcode_str.length != 4) {
    alert("Postnummeret kan kun inneholde 4 siffer");
    return false;
  }

  if (!zipcode_filter.test(zipcode_str)){
    alert("Postnummeret kan bare inneholde tall");
    return false;
  }
  else if(zipcode_str < 0001 || zipcode_str > 9999){
    alert("Postnummeret må være et tall mellom 0001 og 9999");
    return false;
  }
  else {
    return true;
  }
}

function validate_birthdate(myString){
   var myDay, myMonth, myYear;  

   myArrayDate = myString.split(".");

   myDay = Math.round(parseFloat(myArrayDate[0]));
   myMonth = Math.round(parseFloat(myArrayDate[1]));
   myYear = Math.round(parseFloat(myArrayDate[2]));

   if( isNaN(myDay) || isNaN(myMonth) || isNaN(myYear) || (myYear < 1) || (myDay < 1) || (myMonth < 1) | (myMonth > 12) || (myDay > days_in(myMonth, myYear))){
     alert("Vennligst fyll ut en gyldig fødseldato på formen DD.MM.ÅÅÅÅ.");
       return false;
   } else{
     return true;
   }
}


function isLeap(year){
    if(year % 400 == 0){
        return true;
    } else if((year % 4 == 0) && (year % 100 != 0)){
        return true
    } else return false;
};

function days_in(month, year){
    if(month == 4 | month == 6 || month == 9 || month == 11){ 
        return 30;
    } else if(!isLeap(year) && month == 2){
        return 28;
    } else if(isLeap(year) && month == 2){
        return 29;
    } else return 31;
};


function check_field(){

  if (document.bestilling.fornavn.value == '') {
    window.alert("Vennligst fyll ut fornavn.");
    return false;
  }
  if (document.bestilling.etternavn.value == '') {
    window.alert("Vennligst fyll ut etternavn.");
    return false;
  }
  if (document.bestilling.adresse.value == '') {
    window.alert("Vennligst fyll ut adresse.");
    return false;
  }
  if (! validate_zipcode(document.bestilling.postnr.value)) {
    return false;
  }
  if (document.bestilling.sted.value == '') {
    window.alert("Vennligst fyll ut sted.");
    return false;
  }
  else {
    return true;
  }


}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
  return false;
}

