window.onload = initForm;
function initForm() { 
  external_links();
  
  addFieldVal('quickcontact_name', 'Name');
  addFieldVal('quickcontact_email', 'Email');
  addFieldVal('quickcontact_telephone', 'Tel');
  addFieldVal('quickcontact_enquiry', 'Enquiry');
  
  addFieldVal('mailinglist_email', 'Enter email address...');
  
  addFieldVal('how_heard_other', 'Please specify');
  
  if(document.getElementById('how_heard')) {
    bookingHeard(); // runs on page load
    document.getElementById('how_heard').onchange = bookingHeard; // runs if 'how heard' option changed
  }
}

function addFieldVal(element, val) {
  if(document.getElementById(element)) {
    document.getElementById(element).onfocus = function () {clearfield(this, val);};
    document.getElementById(element).onblur = function () {reinstatefield(this, val);};
  }
}

function clearfield(element, val) {
  trimValue = trim(element.value);
  if(trimValue == val) {
    element.value = '';
  }
}

function reinstatefield(element, val) {
  trimValue = trim(element.value);
  if(trimValue == '') {
    element.value = val;
  }
}

function trim(str, chars) {
  var replaceLeft, replaceRight;
  chars = chars || "\\s";
  replaceLeft = str.replace(new RegExp("^[" + chars + "]+", "g"), "");
  replaceRight = replaceLeft.replace(new RegExp("[" + chars + "]+$", "g"), "");
  return replaceRight;
}

function external_links() { 
  if(!document.getElementsByTagName) {
    return;
  } else { 
    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.getAttribute("rel") == "external nofollow") { 
        anchor.target = "_blank";
      }
    }
  }
}

function bookingHeard(){
  var howHeard = document.getElementById('how_heard');
  var howHeardOther = document.getElementById('how_heard_other');
  if(howHeard.value == 1){
    howHeardOther.style.display = 'block';
  } else {
    howHeardOther.style.display = 'none';
  }
}

function switch_product_image(image, title) {  
  var pImage;
  pImage = document.getElementById('product_image_src');
  pImage.src = 'images/products/main/' + image;
  pImage.title = title;
}

function reset_product_image(image, title) {
  var pImage;
  pImage = document.getElementById('product_image_src');
  pImage.src = 'images/products/main/' + image;
  pImage.title = title;
}

function getGlossary(letter) {
  xmlhttp.open("GET", "logicajax/glossary.php?letter=" + letter);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {    
      document.getElementById('glossary').innerHTML =  xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

function changePicture(src, alt, title) {
  image = document.getElementById('display_image');
  image.src = src;
  image.alt = alt;
  image.title = title;
}
