
/// dna mini js lib
function $object(id) {return document.getElementById(id);}
Node = {
    create : function(n, a, t) {
        var e = document.createElement(n);
        if (t) e.appendChild(document.createTextNode(t));
        for (at in a) e[at] = a[at];
        return e;
    },
    hide : function(id) {$object(id).style.visibility = "hidden";},
    show : function(id) {$object(id).style.visibility = "visible";},
    undisplay : function(id) {$object(id).style.display = "none";},
    display : function(id) {$object(id).style.display = "block";},
    write: function(id, c) {$object(id).innerHTML = c;},
    read : function(id) {return $object(id).innerHTML}
};
// end of mini lib

function failureHandler(e) {
    //location.hash = e.Message;
    //location.href = "/error"; 
}

function callback(o) {
    var loader = o.loader==null?"loader":o.loader;
    if ($object(loader)) Node.show(loader);
    return function(r) {
       r.error == null ? o.onSuccess(r.value) : failureHandler(r.error);
       if ($object(loader)) Node.hide(loader);
    };
}

Number.prototype.format = function() {
  var sep = ",";
  var x = this.toString();
  var z = "";
  // reverse the digits. regexp works from left to right.
  for (i=x.length-1;i>=0;i--)
    z += x.charAt(i);
  // add seperators. but undo the trailing one, if there
  z = z.replace(/(\d{3})/g, "$1" + sep);
  if (z.slice(-sep.length) == sep)
    z = z.slice(0, -sep.length);
  x = "";
  // reverse again to get back the number
  for (i=z.length-1;i>=0;i--)
    x += z.charAt(i);
  return x;
}

var chars = ["[abcdefghijklmnoprstuwyz]", "[abcdefghklmnopqrstuvwxy]", "[abcdefghjkstuw]", "[abehmnprvwxy]", "[abdefghjlnpqrstuwxyz]"];
var pcexp = [
    new RegExp ("^(" + chars[0] + "{1}" + chars[1] + "?[0-9]{1,2})(\\s*)([0-9]{1}" + chars[4] + "{2})$","i"),
    new RegExp ("^(" + chars[0] + "{1}[0-9]{1}" + chars[2] + "{1})(\\s*)([0-9]{1}" + chars[4] + "{2})$","i"),
    new RegExp ("^(" + chars[0] + "{1}" + chars[1] + "?[0-9]{1}" + chars[3] +"{1})(\\s*)([0-9]{1}" + chars[4] + "{2})$","i"),
    /^(GIR)(\s*)(0AA)$/i,
    /^(bfpo)(\s*)([0-9]{1,4})$/i,
    /^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i];

String.prototype.isUkPostcode = function() {
  var postCode = "";
  var valid = false;
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(this)) {
      pcexp[i].exec(this);
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      valid = true;
      break;
    }
  }
  return valid ? postCode : false;
}

String.prototype.isAlpha = function() {
  var iChars = "*|,\":<>[]{}`';()&$#%1234567890";
	var eLength = this.length;
	for (var i=0; i < eLength; i++){
		if (iChars.indexOf(this.charAt(i)) != -1)
		{
			return false;
		}
	}

	return true;
}

String.prototype.isTel = function() {
  var iChars = "*|,\":<>[]{}`';&$#%abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ";
	var eLength = this.length;
	for (var i=0; i < eLength; i++){
		if (iChars.indexOf(this.charAt(i)) != -1)
		{
			return false;
		}
	}

	return true;
}

String.prototype.isEmail = function() {

	if (this.length < 5) { return false; }

	var iChars = "*|,\":<>[]{}`';()&$#%";
	var eLength = this.length;

	for (var i=0; i < eLength; i++){
		if (iChars.indexOf(this.charAt(i)) != -1)
		{
			return false;
		}
	}

	var atIndex = this.lastIndexOf("@");
	if(atIndex < 1 || (atIndex == eLength - 1)) {
		return false;
	}

	var pIndex = this.lastIndexOf(".");
	if(pIndex < 4 || (pIndex == eLength - 1)) {
		return false;
	}

	if(atIndex > pIndex) {
		return false;
	}

	return true;

};


String.prototype.isDate = function() { 
    return isNaN(Date.parse(this)); 
}

function getViewportWidth() {
	var w = 0;
	if(window.innerWidth) w = window.innerWidth;
	if(document.documentElement){
		var w2 = document.documentElement.clientWidth;
		if(!w || w2 && w2 < w) w = w2;
		return w;
	}
	if(document.body) return document.body.clientWidth;
	return 0;
}

function getViewportHeight() {
	if (window.innerHeight) return window.innerHeight;
	if (document.documentElement) return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return 0;
}

//global vars
var app = {};


