var xmlhttp = false;
try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e) {
  xmlhttp = false;
  }
}
if(!xmlhttp && typeof (XMLHttpRequest != 'undefined')) {
  xmlhttp = new XMLHttpRequest();
}

window.onload = initialise;
function initialise() {
  new_window_links();
  if(document.getElementById('filter_results')) {
    document.getElementById('filter_results').onclick = function() {
      filter_results();
      return false;
    }
  }
  if(document.getElementById('enquire_now_form')) {
    add_field_value('form_name', 'Name');
    add_field_value('form_email_address', 'Email address');
    add_field_value('form_phone_number', 'Phone number');
    add_field_value('form_enquiry', 'Enquiry');
  }
}
function new_window_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 regenerate_captcha(imageID, viewName) {
  xmlhttp.open("GET", "ajax/generatecaptcha.php?imageid=" + imageID + "&viewname=" + viewName);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById(imageID).src = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}
function filter_results() {
  var getString = '';
  if(document.getElementById('filter_resultsForm')) {
    for(x=0;x<document.getElementById('filter_resultsForm').elements.length;x++) {
      if(document.getElementById('filter_resultsForm').elements[x].value != 'All') {
        getString = getString + document.getElementById('filter_resultsForm').elements[x].name + '=' + document.getElementById('filter_resultsForm').elements[x].value + '&';
      }
    }
  }
  getString = getString.substring(0, getString.length - 1);
  if(getString.length > 0) {
    window.location = document.getElementById('filter_resultsForm').action + '?' + getString;
  } else {
    window.location = document.getElementById('filter_resultsForm').action;
  }
}
function navigate(url) {
  window.location = url;
}
function add_field_value(element, val) {
  if(document.getElementById(element)) {
    document.getElementById(element).onfocus = function () { clearfield(this, val); };
    document.getElementById(element).onblur = function () { reinstatefield(this, 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 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;
  }
}
