String.prototype.ltrim=new Function("return this.replace(/^\\s+/,'')");
String.prototype.rtrim=new Function("return this.replace(/\\s+$/,'')");
String.prototype.trim=new Function("return this.replace(/^\\s+|\\s+$/g,'')");
String.prototype.nocr=new Function("return this.replace(/(\\r\\n|[\\r\\n])/g,'')");
String.prototype.makeplain=new Function("return this.toLowerCase().replace(/\\s/g,'_')");
String.prototype.startsWith = function (str) { return (this.match("^" + str) == str); }
String.prototype.endsWith = function (s) { return (new RegExp(s + "$")).test(this); }
String.prototype.replaceAll = function(from,to) { return this.replace(new RegExp(from,'g'),to); }

function obj(id) {
   return document.getElementById(id);
}
function clearList(l) {
   for (var i = l.length - 1; i >= 1; i -= 1) l.options[i] = null;
}
function setSelected(l, v) {
   var iSelIndex = 0;
   if (v.length != 0) {
      for (var i = 0; i < l.length; i++)
         if (l.options[i].value == v) iSelIndex = i;
   }
   l.selectedIndex = iSelIndex;
}
function classOn(id, cls) {
   var o = obj(id);
   if (o != null) {
      if (o.className == '') o.className = cls;
      else if (o.className.indexOf(' ' + cls) == -1) o.className += ' ' + cls;
   }
}
function classOff(id, cls) {
   var o = obj(id);
   if (o != null) {
      if (o.className == cls) o.className = '';
      else if (o.className.startsWith(cls)) o.className = o.className.substring(cls.length);
      else if (o.className.indexOf(' ' + cls) != -1) o.className = o.className.replace(' ' + cls, '');
   }
}

function setDTab(idx) {
   for (var i=0 ; i<3 ; i++) {
      obj('dtab'+i).className=(i==idx ? 'sel' : '');
      obj('dtabinfo'+i).className=(i==idx ? 'spec sel' : 'spec');
   }
}
function focustxt(o, msg) {
   if (o.value == msg) o.value = '';
}
function blurtxt(o, msg) {
   if (o.value == '') o.value = msg;
}
function getXMLToken(xml,tag) {
   var ret='';
   var ifrom=xml.indexOf('<'+tag+'>');
   if (ifrom!=-1) {
      ifrom+=tag.length+2;
      var ito=xml.indexOf('</'+tag+'>');
      if (ito!=-1)
         ret=xml.substring(ifrom,ito);
   }
   return ret;
}
function formatNumber(n, dp) {
   return ((n+"").indexOf(".") == -1 ? n : (parseInt(n) + "." + parseInt((n - parseInt(n)) * Math.pow(10, dp))));
}
function formatPrice(sIn) {
//   sIn = sIn.toString();
   sIn = formatNumber(sIn,2)+"";
   var iEnd = (sIn).indexOf(".");
   var sDec = "";
   var sInt = "";
   if (iEnd == -1) sInt = sIn;
   else {
      sInt = sIn.substring(0, iEnd);
      sDec = sIn.substring(iEnd, sIn.length);
   }
   if (sInt.length > 6) sInt = sInt.substring(0, sInt.length - 6) + "," + sInt.substring(sInt.length - 6, sInt.length);
   if (sInt.length > 3) sInt = sInt.substring(0, sInt.length - 3) + "," + sInt.substring(sInt.length - 3, sInt.length);
   if ((sDec.length < 3) && (sDec.length != 0)) sDec = (sDec + "00").substring(0, 3);
   return "&pound;" + sInt + sDec;
}
function swapImg(id,src) {
   var img = obj(id);
   if (img!=null) img.src=src;
}
function validFAD(fldPC) {
   if (!validPostcode(fldPC.value.trim())) {
       alert('Your postcode is not recognised, ensure you have included a space within it.');
      return false;
   }
   else {
      return true;
   }
}
function validName(sVal) {
   var reg = /^[a-z\-\ \']+$/i;
   return reg.test(sVal);
}
function validPostcode(pc) {
   var reg = /^[a-z]{1,2}[\da-z]{1,2} \d[a-z][a-z]$/i;
   return reg.test(pc);
}
function validEmail(email) {
   var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i;
   return reg.test(email);
}
function validPhone(sIn) {
   phonepat = /^([0-9 +()])+$/i
   return phonepat.test(sIn);
}
function validAlphanumeric(val) {
   var alphaExp = /^[0-9a-zA-Z]+$/;
   return (val.match(alphaExp));
}
function mapLoad(divid, lat, lon, lvl, lbl) {
   var latlng = new google.maps.LatLng(lat, lon);
   var opts = { zoom: lvl, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP };
   map = new google.maps.Map(obj(divid), opts);
   var mark0 = new google.maps.Marker({ position: new google.maps.LatLng(lat, lon), map: map, title: lbl });
}
function openclose(id) {
   var o = obj(id);
   if (o!=null) {
      if (!isopen(id))
         o.style.display = 'block';
      else
         o.style.display = 'none';
   }
}
function open(id) {
   var o = obj(id);
   if (o != null) o.style.display = 'block';
}
function close(id) {
   var o = obj(id);
   if (o != null) o.style.display = 'none';
}
function isopen(id) {
   return (styleDisplay(id)=='block');
}
function selectedValue(o) {
   return (o!=null ? o.options[o.selectedIndex].value : '');
}
function styleDisplay(id) {
   var o=obj(id);
   var ret=o.style.display;
   if (o.currentStyle)
      ret = o.currentStyle.display;
   else if (window.getComputedStyle)
      ret= window.getComputedStyle(o, null).getPropertyValue("display");

   return ret;
}
function addHidden(frm, name, value) {
   if (value != '') {
      if (frm[name] == null) {
         var el = document.createElement('input');
         el.type = 'hidden';
         el.name = name;
         el.value = value;
         frm.appendChild(el);
      }
      else {
         frm[name].value = value;
      }
   }
}
