// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors .length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
      anchor.target = "_blank";
      anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "opens in a new window";
      anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
    }
  }
}
window.onload = externalLinks;

function toggle(subject, arg) {
  // arg is the ID for the enquiry set in v_category (Iceland Data Schema) - not a random number!
  if(arg == "RE8700") {
    new Effect.BlindDown('product_packaging', {duration: 1})
  } else {
    new Effect.BlindUp('product_packaging', {duration: 1})
    document.getElementById("contact_prod_details").value = "";
    document.getElementById("contact_barcode").value = "";
    document.getElementById("contact_bestbefore").value = "";
    document.getElementById("contact_othercodes").value = "";
    document.getElementById("contact_pricepaid").value = "";
  }
  document.getElementById("contact_subject_code").value = arg;

  // Depending on which subject they have chosen we now need to assign the actual value to v_category (Iceland Schema)
  switch(arg) {
    case "RE7920":
      document.getElementById("contact_v_category").value = "Opening Times";
      break
    case "RE8700":
      document.getElementById("contact_v_category").value = "Product / Packaging";
      break
    case "RE8701":
      document.getElementById("contact_v_category").value = "Store Service";
      break
    case "RE8702":
      document.getElementById("contact_v_category").value = "Home Delivery";
      break
    case "RE8703":
      document.getElementById("contact_v_category").value = "Appliances";
      break
    case "RE8704":
      document.getElementById("contact_v_category").value = "Advertising";
      break
    case "RE7993":
      document.getElementById("contact_v_category").value = "Vacancies";
      break
    case "RE8705":
      document.getElementById("contact_v_category").value = "About Iceland";
      break
    case "RE8707":
      document.getElementById("contact_v_category").value = "Compliment";
      break
    case "RE8706":
      document.getElementById("contact_v_category").value = "Website";
      break
    case "RE8708":
      document.getElementById("contact_v_category").value = "Bonus Card";
      break
    case "RE7966":
      document.getElementById("contact_v_category").value = "New Face of Iceland";
      break
    default:
      document.getElementById("contact_v_category").value = "";
  }
}

function previewContent(frm, actn) {
        prev_actn = frm.action;
        frm.target = "_blank";
        frm.action = actn;
        frm.submit();
        frm.target = "";

        // reset action once window has been opened
        frm.action = prev_actn;
}

function BlindDownUnlessDown(id) {
        if (!Element.visible(id)) {
                Effect.BlindDown(id);
        }
}

function toggleDetailsFormFor(checkbox, target) {
    if (checkbox.checked) {
        new Effect.BlindDown(target, {duration: 0.3});
    }
    else {
        new Effect.BlindUp(target,  {duration: 0.3});
    }
}


String.prototype.trim = function() {
        // Strip leading and trailing white-space
        return this.replace(/^\s*|\s*$/g, "");
}

function completeAddress(link) {
    link.innerHTML='Please wait...';
    postcode = $F('postcode');
    try {
        postcode = postcode + $F('postcode2');
    } catch(err) {}
    new Ajax.Updater('complete_address', '/change_of_details/find_address',
                     {parameters: { premise:'', postcode:postcode},
                             onComplete: function() { link.innerHTML='Complete my address'; }});
    return false;
}

function fillAddress(options) {
    options = options.split('|').map(function(n) { return n.split(':');});
    /* turn it to a hash */
    h = new Hash();
    options.each(function(x) { h[x[0]] = x[1]; });
    $("premise").value = h['house'];
    $("address").value = h['address'];
    $("town").value = h['city'];
    $("county").value = h['county'];
}
