function removeValue(actual, base){
  if( actual.value == base ) {
    actual.value = '';
  } else if( actual.value == '' ) {
    actual.value = base;
  }
}

function checkValue(actual, base){
  if( actual.value == '' ) {
    actual.value = base;
  }
}

function fold(id, class_name){
  var element = document.getElementById(id);
  if( element.getAttribute("class") == class_name || element.getAttribute("className") == class_name ) {
    element.setAttribute("class", class_name + "_highlight");
    element.setAttribute("className", class_name + "_highlight");
  } else {
    element.setAttribute("class", class_name);
    element.setAttribute("className", class_name);
  }
  return false;
}

function checkOrder(){
  var missing_fields = 0;
  if( document.getElementById('se_domain_required').value == '1') {
    if( document.getElementById('domain_se_agreement1').checked != true || document.getElementById('domain_se_agreement2').checked != true ) {
      missing_fields += 1;
    }
  }
  if( document.getElementById('accept').checked != true ){
    missing_fields += 2;
  }
  
  if( missing_fields == 1 ) {
    alert('Du måste godkänna avtalen för .se domän(er) för att kunna lägga beställningen.');
    return false;
  } else if( missing_fields == 2 ) {
    alert('Du måste godkänna avtalet för att kunna lägga beställningen.');
    return false;
  } else if( missing_fields == 3 ) {
    alert('Du måste godkänna e-butikens avtal samt avtalen för .se domän(er) för att kunna lägga beställningen.');
    return false;
  } else {
    return true;
  }
}

function displayOptions(tag_ids, display_settings){
  var tag_id_array = tag_ids.split(",");
  var display_setting_array = display_settings.split(",");
  for( var j=0 ; j<tag_id_array.length ; j++ ){
    if( display_setting_array[j] == '1' ){
      document.getElementById(tag_id_array[j]).style.display = '';
      var input_array = document.getElementById(tag_id_array[j]).getElementsByTagName('input');
      for( var m=0 ; m<input_array.length ; m++ ){
        input_array[m].value = '';
      }
      var textarea_array = document.getElementById(tag_id_array[j]).getElementsByTagName('textarea');
      for( var m=0 ; m<textarea_array.length ; m++ ){
        textarea_array[m].value = '';
      }
    }else{
      document.getElementById(tag_id_array[j]).style.display = 'none';
    }
  }
}
function displayCheckOptions(checkbox_id, tag_ids){
  var tag_id_array = tag_ids.split(",");
  for( var j=0 ; j<tag_id_array.length ; j++ ){
    if( document.getElementById(checkbox_id).checked == true ){
      document.getElementById(tag_id_array[j]).style.display = '';
      var input_array = document.getElementById(tag_id_array[j]).getElementsByTagName('input');
      for( var m=0 ; m<input_array.length ; m++ ){
        input_array[m].value = '';
      }
      var textarea_array = document.getElementById(tag_id_array[j]).getElementsByTagName('textarea');
      for( var m=0 ; m<textarea_array.length ; m++ ){
        textarea_array[m].value = '';
      }
    }else{
      document.getElementById(tag_id_array[j]).style.display = 'none';
    }
  }
}
function goBack(){
  document.Butik.goBack.value = "1";
  document.Butik.submit();
}

function captchaValidEmail(tag_id){
  email = document.getElementById(tag_id).value
  if( /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(email) ){
    return true;
  } else {
    return false;
  }
}
function captchaInputHasValue(tag_ids){
  var missing_fields = 0;
  var element;
  var container;
  var tag_id_array = tag_ids.split(",");
  var valid_field;
  for( var i=0 ; i<tag_id_array.length ; i++ ){
    valid_field = true;
    element = document.getElementById(tag_id_array[i])
    container = document.getElementById("input_" + tag_id_array[i])
    container_type = "input";
    if( !container ) {
      container = document.getElementById("textarea_" + tag_id_array[i])
      container_type = "textarea";
    }
    if( element.value == '' ){
      valid_field = false;
    } else {
      if( element.name.search("email") >= 0 ){
        if( !captchaValidEmail(element.id) ){
          valid_field = false;
        }
      }
    }
    
    if( valid_field ){
      if( container_type == "input" ) {
        if( container.getAttribute("class") == 'input input_missing' || container.getAttribute("className") == 'input input_missing' ) {
          container.setAttribute("class", 'input');
          container.setAttribute("className", 'input');
        }
      } else if( container_type == "textarea" ) {
        if( container.getAttribute("class") == 'textarea information textarea_missing' || container.getAttribute("className") == 'textarea information textarea_missing' ) {
          container.setAttribute("class", 'textarea information');
          container.setAttribute("className", 'textarea information');
        }
      }
    } else {
      missing_fields++;
      if( container_type == "input" ) {
        if( container.getAttribute("class") == 'input' || container.getAttribute("className") == 'input' ) {
          container.setAttribute("class", 'input input_missing');
          container.setAttribute("className", 'input input_missing');
        }
      } else if( container_type == "textarea" ) {
        if( container.getAttribute("class") == 'textarea information' || container.getAttribute("className") == 'textarea information' ) {
          container.setAttribute("class", 'textarea information textarea_missing');
          container.setAttribute("className", 'textarea information textarea_missing');
        }
      }
    }
  }
  
  if( missing_fields > 0 ){
    alert('Det saknas obligatoriska fält. De uppgifter som saknas är markerade med rött.');
    return false;
  } else {
    return true;
  }
}