// EmptyNullUndefined - returns true if there is no value of any kind
function EmptyNullUndefined(x) {
	return (x == undefined || x == null || x == "")
}

// TrapEnter - stops preses of Enter from submitting the form. In some browsers, it will send a TAB instead.
function trapEnter(e){
	var pK, t
	//var oTarget =(e.target)? e.target : e.srcElement;
	if(window.event) {// IE
		pK = window.event.keyCode
		t = window.event.srcElement
		if ((t.type=="text" || t.type=="select-one" || t.type=="select-multiple") && pK==13)
		window.event.keyCode = 9;
	}

	else if(e.which) {    // Netscape/Firefox/Opera
		pK= e.which
		t = e.target
		if ((t.type=="text" || t.type=="select-one" || t.type=="select-multiple") && pK==13) {
			if (e.preventDefault) {
				e.preventDefault(); // Mozilla/firefox
			}
//			e.keyCode = 9;
		}
	}
}


// Levenstein Distance function, to comapre the similarity of two strings
function LD(s, t) {
	var d = new Array();
	var n, m, i, j, s_i, t_j, cost;
	n = s.length;
	m = t.length;
	if (n == 0) return m;
	if (m == 0) return n;
	for(i=0; i<=n; i++) d[i] = new Array();
	for (i = 0; i <= n; i++) {d[i][0] = i;}
	for (j = 0; j <= m; j++) {d[0][j] = j;}
	for (i = 1; i <= n; i++) {
		s_i = s.charAt(i - 1);
		for (j = 1; j <= m; j++) {
			t_j = t.charAt(j - 1);
			if (s_i == t_j) cost = 0;
			else cost = 1;
			d[i][j] = Minimum (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost);
		}
	}
	return d[n][m];
}
function Minimum(a, b, c) {
	var mi;
	mi = a;
	if (b < mi)
		mi = b;
	if (c < mi)
		mi = c;
	return mi;
}

// RemovePunc - removes punctuation
function numbersanddashes(s) {
	return s.replace( /[\"\!\£\%\&\^\*\{\}\@\~\;\<\>\.,;!#\$\/:\?'\(\)\[\]_\\\s\n\tABCDEFGHIJKLNMOPQRSTUVWXYZ]/g,"");
}


// RemovePunc - removes punctuation
function RemovePunc(s) {
	return s.replace( /[\"\!\£\%\&\^\*\{\}\@\~\;\<\>\.,;!#\$\/:\?'\(\)\[\]_\-\\\s\n\t]/g,"");
}
// RemovePunc - removes punctuation
function RemoveHyphen(s) {
	return s.replace( /[-]/g,"");
}

// RemoveNumbers - remove any numbers
function RemoveNumbers(s) {
	return s.replace( /[1234567890]/g,"");
}

// AllTrim - removes leading and trailing spaces
function AllTrim(s) {
                return s.replace(/^\s+|\s+$/g, '');
            }

// reverse the characters in a string
function ReverseString(s) {
	var RetVal=""
	for (var i=s.length-1; i>=0; i--) {
		RetVal=RetVal+s.substr(i,1)
	}
	return RetVal
}


function ChrTran(s,a,b) {
	var RetVal=""
	for (var i=0; i<s.length; i++) {
		if (s.substr(i,1) == a) {
			RetVal=RetVal+b
		} else {
			RetVal=RetVal+s.substr(i,1)
		}
	}
	return RetVal
}

function ContainsUpperChars(s) {
	return s.match(/[A-Z]/);
}

function ContainsLowerChars(s) {
	return s.match(/[a-z]/);
}

function ContainsOnlyConsonants(s) {
	if (s.length > 0)
		return !s.match(/[^bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ]/)
}

function ContainsDigits(s) {
	if (s.length > 0)
		return s.match(/[0-9]/)
}

function StartsWith(s1, s2) {
	return (s1.substring(0,s2.length)==s2);
}

function ExtractCountryCode(s) {
	var CtryCode = "";
	if (Left(s,1)=="1") {
		CtryCode = "1";
	} else {
		for (var i in Country) {
			if (Country[i][1].length > 0 && StartsWith(s,Country[i][1])) {
				return Country[i][1];
			}
		}
	}
	return CtryCode;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function LooksLikeWebAddress(s) {
	s=AllTrim(s);
	if ( s.match(/\./) && ! s.match(/[\s\,]/) ) {
		// last word max 4 chars
		var aWord=s.split(".");
			nWords=aWord.length
			cLastWord=aWord[nWords-1]
		if ( cLastWord.length<=4 ) {
			return true;
		}
	}
	return false;	
}

// tests to see if the object passed is an array
function isArray(testObject) {	 
   return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

// tests to see if the specified value exists in the array
function inArray(arr, val) {
	for (var i in arr) 	if (arr[i] === val) return true;
	return false;
};


String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
   return this.replace(/^\s+/g,"");
}
String.prototype.rtrim = function() {
   return this.replace(/\s+$/g,"");
}


// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


// return true if the character is A-Za-z

function isAlpha(s) {
	var temp
	if (s.length == 0) {
		return false
	}
	temp=s.toUpperCase()
	temp=temp.charAt(0)
	if (temp < "A" ) {
		return false
	}
	if (temp > "Z" ) {
		return false
	}	
	return true
}

// return true if 0-9

function isDigit(s) {
	var temp=s
	if (s.length == 0) {
		return false
	}
	temp=temp.charAt(0)
	if (temp < "0" ) {
		return false
	}
	if (temp > "9" ) {
		return false
	}	
	return true
}

function CountDigits(s) {
	return s.replace( /[^1234567890]/g,"").length;
}

function CountAlpha(s) {
	return s.replace( /[^a-zA-Z]/g,"").length;
}

function SetFocusToControl(Control) {
	Control.focus();
    if (Control.createTextRange) {
        var range = Control.createTextRange();
        range.collapse(false);
        range.select();
    }
    else if (Control.setSelectionRange) {
        var length = Control.value.length;
        Control.setSelectionRange(length, length);
    }
	Control.value+='';
}


function debug(a) {
//	console.log(a);
}



