//Just one click on confirm in order process.
var click  = 1;

function oneClick() {
  if (click > 1) {
    alert("Ordren blir bearbeidet, vennligst ikke klikk flere ganger.");
    return false;
  }
  click = 2;
  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_str) {
    alert("E-Post adressen må fylles inn.");
    return false;
  }
  else if (!email_filter.test(email_str)){
    alert("E-Post adressen er ikke gyldig: Eksempel pa riktig adresse er: fornavn.etternavn@firma.no");
    return false;
  }
  else {
    return true;
  }
}

function validate_zipcode_no(zipcode_str){
  var zipcode_filter = /^([\d]+)$/i;

  if (!zipcode_filter.test(zipcode_str)){
    alert("Postnummer kan bare inneholde tall");
    return false;
  }
  else if(zipcode_str < 0001 || zipcode_str > 9999){
    alert("Postnummer må være et tall mellom 0001 og 9999");
    return false;
  }
  else {
    return true;
  }
}

/* Validates that all required fields are filled in before submitting the form. */
function validate_order(){

  var ifname   = document.orders.elements['ORDERS.I_FNAME'].value;
  var ilname   = document.orders.elements['ORDERS.I_LNAME'].value;
  var izipcode = document.orders.elements['ORDERS.I_ZIPCODE'].value;
  var icity    = document.orders.elements['ORDERS.I_CITY'].value;
  var iphone   = document.orders.elements['ORDERS.I_TELEPHONE'].value;
  var iemail   = document.orders.elements['ORDERS.I_EMAIL'].value;

  var dfname   = document.orders.elements['ORDERS.D_FNAME'].value;
  var dlname   = document.orders.elements['ORDERS.D_LNAME'].value;
  var dcity    = document.orders.elements['ORDERS.D_CITY'].value;
  var dzipcode = document.orders.elements['ORDERS.D_ZIPCODE'].value;
  var dphone   = document.orders.elements['ORDERS.D_TELEPHONE'].value;
  var demail   = document.orders.elements['ORDERS.D_EMAIL'].value;
  
  if(!ifname || !dfname) {
    alert("Fornavnet må fylles inn.");
    return false;
  }
  if(!ilname || !dlname) {
    alert("Etternavnet må fylles inn.");
    return false;
  }
  if(!icity || !dcity) {
    alert("Poststed må fylles inn.");
    return false;
  }
  if(!iphone || !dphone) {
    alert("Telefon må fylles inn.");
    return false;
  }
  if(!validate_email(iemail) || !validate_email(demail)){
    return false;
  }
  if(!validate_zipcode_no(izipcode) || !validate_zipcode_no(dzipcode)){
    return false;
  }

  if (!oneClick()) {
    return false;
  }

  enable_delivery_address();
  return true;
}

/* Enable/disable doordelivery according to array of supported zip-codes. */
function check_zipcode() {

  var rectype = get_receiver_option();
  if(rectype == 1) var zipcode = document.getElementById('ctl00_MainContentPlaceHolder_TextBoxCustomerZipCode').value;
  else if(rectype == 2) var zipcode = document.getElementById('ctl00_MainContentPlaceHolder_TextBoxCustomerZipCode').value;

  var enable_dd = false;

  if(zipcode && zipcode >= 0001 && zipcode < 9999) {
    for(var i=0; i<zipcodes.length; i++) {
      if(zipcode == zipcodes[i]) {
        enable_dd = true;
        break;
      }
    }
  }
  //enable_doordelivery(enable_dd);
}

/* Changes the deltypes drop-down to only offer what is available at the current zip. */
var deldd = new Option("", "");

function enable_doordelivery(enable_dd) {

  var deloptions = document.getElementById('deltype_default').value; //'deltype_default'].options;

  if(enable_dd && deloptions.length == 1) {
    deloptions.length = 2;
    deloptions[1] = deldd;
  } else if(!enable_dd) {
    if(deloptions[1] != null) deldd = deloptions[1];
    deloptions[1] = null;
    deloptions.selectedIndex = 0;
  }
  else {
  }
}

/* Updates the address fields according to the selected option. */
function update_recoption(recoption) {
  
  if(recoption == 1) {
    document.orders.elements['ORDERS.D_FNAME'].value = document.orders.elements['ORDERS.I_FNAME'].value;
    document.orders.elements['ORDERS.D_LNAME'].value = document.orders.elements['ORDERS.I_LNAME'].value;
    document.orders.elements['ORDERS.D_ADDRESS'].value = document.orders.elements['ORDERS.I_ADDRESS'].value;
    document.orders.elements['ORDERS.D_CITY'].value = document.orders.elements['ORDERS.I_CITY'].value;
    document.orders.elements['ORDERS.D_ZIPCODE'].value = document.orders.elements['ORDERS.I_ZIPCODE'].value;
    document.orders.elements['ORDERS.D_TELEPHONE'].value = document.orders.elements['ORDERS.I_TELEPHONE'].value;
    document.orders.elements['ORDERS.D_EMAIL'].value = document.orders.elements['ORDERS.I_EMAIL'].value;

    document.orders.elements['ORDERS.D_FNAME'].disabled = true;
    document.orders.elements['ORDERS.D_LNAME'].disabled = true;
    document.orders.elements['ORDERS.D_ADDRESS'].disabled = true;
    document.orders.elements['ORDERS.D_CITY'].disabled = true;
    document.orders.elements['ORDERS.D_ZIPCODE'].disabled = true;
    document.orders.elements['ORDERS.D_TELEPHONE'].disabled = true;
    document.orders.elements['ORDERS.D_EMAIL'].disabled = true;
  }
  else if(recoption == 2) {
    document.orders.elements['ORDERS.D_FNAME'].value = "";
    document.orders.elements['ORDERS.D_LNAME'].value = "";
    document.orders.elements['ORDERS.D_ADDRESS'].value = "";
    document.orders.elements['ORDERS.D_CITY'].value = "";
    document.orders.elements['ORDERS.D_ZIPCODE'].value = "";
    document.orders.elements['ORDERS.D_TELEPHONE'].value = "";
    document.orders.elements['ORDERS.D_EMAIL'].value = "";

    document.orders.elements['ORDERS.D_FNAME'].disabled = false;
    document.orders.elements['ORDERS.D_LNAME'].disabled = false;
    document.orders.elements['ORDERS.D_ADDRESS'].disabled = false;
    document.orders.elements['ORDERS.D_CITY'].disabled = false;
    document.orders.elements['ORDERS.D_ZIPCODE'].disabled = false;
    document.orders.elements['ORDERS.D_TELEPHONE'].disabled = false;
    document.orders.elements['ORDERS.D_EMAIL'].disabled = false;
  }

  check_zipcode();

}

function enable_delivery_address() {
  document.orders.elements['ORDERS.D_FNAME'].disabled = false;
  document.orders.elements['ORDERS.D_LNAME'].disabled = false;
  document.orders.elements['ORDERS.D_ADDRESS'].disabled = false;
  document.orders.elements['ORDERS.D_CITY'].disabled = false;
  document.orders.elements['ORDERS.D_ZIPCODE'].disabled = false;
  document.orders.elements['ORDERS.D_TELEPHONE'].disabled = false;
  document.orders.elements['ORDERS.D_EMAIL'].disabled = false;
}

/* If invoice/delivert address is the same, copies over all info. 
function update_receiver() {
  var rectype = get_receiver_option();

  if(rectype == 1) {
    document.orders.elements['ORDERS.D_FNAME'].value = document.orders.elements['ORDERS.I_FNAME'].value;
    document.orders.elements['ORDERS.D_LNAME'].value = document.orders.elements['ORDERS.I_LNAME'].value;
    document.orders.elements['ORDERS.D_ADDRESS'].value = document.orders.elements['ORDERS.I_ADDRESS'].value;
    document.orders.elements['ORDERS.D_CITY'].value = document.orders.elements['ORDERS.I_CITY'].value;
    document.orders.elements['ORDERS.D_ZIPCODE'].value = document.orders.elements['ORDERS.I_ZIPCODE'].value;
    document.orders.elements['ORDERS.D_TELEPHONE'].value = document.orders.elements['ORDERS.I_TELEPHONE'].value;
    document.orders.elements['ORDERS.D_EMAIL'].value = document.orders.elements['ORDERS.I_EMAIL'].value;
  }
}*/

/* Get whether the invoice and delivery addresses are the same: 1=same, 2=different. 
function get_receiver_option() {
  var rectype = 1;
  var rectypes = document.rectypes.elements["rectype"];
  for(i=0; i<rectypes.length; i++) {
    if(rectypes[i].checked) rectype = rectypes[i].value;
  }
  return rectype;
}*/