// mailer.js

var ONLINE_URL = 'http://s1webmedia.co.uk/easyedit/data/email.htm';

function em_init() {
 var mf = document.getElementById('mf');
 mf.onsubmit = function() {
  //if (mf.e.value.search(/\w+@\w+\.\w+/) < 0 && mf.p.value.search(/\d+/) < 0) {
  // alert('We need either your email address or phone number.');
  // mf.e.focus();
  //}
  if (mf.e.value.search(/\w+@\w+\.\w+/) < 0) {
   alert('We need your email address to reply to your message.');
   mf.e.focus();
  }
  else if (mf.m.value.search(/\w+/) < 0) {
   alert('What is your message?');
   mf.m.focus();
  }
  else if (mf.m.value.length > 4096) {
   alert('Please be more brief...');
   mf.m.focus();
  }
  else return true;
  return false;
 };
 mf.n_id.onfocus = mf.e_id.onfocus = mf.p_id.onfocus = mf.s_id.onfocus = mf.m_id.onfocus = mf.d_id.onfocus = function() {
  this.style.borderColor = '#333';
 };
 mf.n_id.onblur = mf.e_id.onblur = mf.p_id.onblur = mf.s_id.onblur = mf.m_id.onblur = mf.d_id.onblur = function() {
  this.style.borderColor = '#666';
 };

 function QueryString(link) {
  // this.link, this.values;

  this.getnames = function() {
   var a = [];
   for (var name in this.values) a.push(name);
   return a;
  };
  this.get = function(name) {
   return name in this.values ? this.values[name] : null;
  };
  this.set = function(name, value, store) { // setting store on location changes page!
   if (value === null && name in this.values) delete this.values[name];
   this.values[name] = value; // overwrite
   if (store) this._store();
  };
  this.unset = function(store) {
   this.values = {};
   if (store) this._store();
  };
  this.toString = function() {
   var s = '';
   var sep = this.link == location ? '&' : '&amp;';
   for (var name in this.values) {
    s += sep + encodeURIComponent(name) + '=' + encodeURIComponent(this.values[name]);
   }
   return '?' + s.substring(sep.length);
  };
  this._store = function() { // changing location exits page!
   this.link.search = this.toString();
  };
  this._init = function(link) {
   this.link = link ? link : window.location;
   this.values = {};
   var search = this.link.search;
   if (search.charAt(0) == '?') search = search.substring(1);
   if (!search) return null;
   var names_values = search.split(new RegExp('&amp;|&')); //alert(names_values)
   for (var i = 0, n = names_values.length; i < n; i++) {
    var nv =  names_values[i].split('=');
    if (nv.length == 2)
     this.values[decodeURIComponent(nv[0])] = decodeURIComponent(nv[1]);
   }
  return null;
  };
  this._init(link);
 } // QueryString usage: var q = new QueryString(); ...

 var q = new QueryString();
 var status = q.get('status');

 if (location.protocol.match(/^http/) === null) {
  for (var i = 0, n = mf.length; i < n; i++)
   mf.elements[i].disabled = true;
  status = 'You can only send messages via the <a href="' + ONLINE_URL + '" style="color:red;text-decoration:underline;">ONLINE form</a>';
 }

 if (status) {
  document.getElementById('status').innerHTML = status;
  if ('cListObject' in window) setTimeout('window.cListObject._showItem(0, cListObject.lists[0].length - 1)', 500); // kludge
 }
}

if (!iemac && document.getElementById) // crudely test browser capability
 window.addEventListener ? window.addEventListener('load', em_init, false) : window.attachEvent ? window.attachEvent('onload', em_init) : null;
