function Commify(n) { 
  var n = String(n)
  var ReplaceScript = /^(.*\s)?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/
  return n == (n=n.replace(ReplaceScript, "$1$2,$3")) ? n : Commify(n) 
}
function thousands(t) {
  t.value = t.value.replace(/[^\d\-\.]/gi,'');
  t.value = Commify(t.value);
}

function uppercase(t) {
  t.value = t.value.toUpperCase();
}

function checkChoice(field, i) {
  if (i == 0) { // "All" checkbox selected.
    if (field[0].checked == true) {
      for (i = 1; i < field.length; i++)
      field[i].checked = false;
     }
  }  else  {  // A checkbox other than "Any" selected.
    if (field[i].checked == true) {
      field[0].checked = false;
    }
  }
}

function lettersonly(t) {
  t.value = t.value.replace(/[^a-zA-Z]/gi,'');
}

function numbersonly(t) {
      t.value = t.value.replace(/[^\d\-\.\,]/gi,'');
}

function wholenumbersonly(t) {
  t.value = t.value.replace(/[^\d\+\$]/gi,'');
}

function textCounter(t,maxlimit) {
  if (t.value.length > maxlimit) {// if too long...trim it!
    t.value = t.value.substring(0, maxlimit);
    alert(maxlimit+' Characters are the limit to this field.  Please re-phrase your answer.');
    t.focus();
    t.select();
    return;
  }
}

function FormatNumber(num, format, shortformat)
{
	if(format==null)
	{
		//format = "#-(###) ###-#### ";
		//format = "(###) ###-#### ";
		format = "###-###-####";
		//format = "###-##-####";
	}					
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
	if(shortformat==null)
	{
		//var shortformat = "###-#### ";
		var shortformat = "";
	}
	
	var validchars = "0123456789";
	var tempstring = "";
	var returnstring = "";
	var extension = "";
	var tempstringpointer = 0;
	var returnstringpointer = 0;
	count = 0;

	// Get the length so we can go through and remove all non-numeric characters
	var length = num.value.length;
		

	// We are only concerned with the format of the phone number - extensions can be left alone.
	if (length > format.length)
	{
		length = format.length;
	};
	
	// scroll through what the user has typed
	for (var x=0; x<length; x++)
	{
		if (validchars.indexOf(num.value.charAt(x))!=-1)
		{
		tempstring = tempstring + num.value.charAt(x);
		};
	};
	// We should now have just the #'s - extract the extension if needed
	if (num.value.length > format.length)
	{
		length = format.length;
		extension = num.value.substr(format.length, (num.value.length-format.length));
	};
	
	// if we have fewer characters than our short format, we'll default to the short version.
	for (x=0; x<shortformat.length;x++)
	{
		if (shortformat.substr(x, 1)=="#")
		{
			count++;
		};
	}
	if (tempstring.length <= count)
	{
		format = shortformat;
	};

	
	//Loop through the format string and insert the numbers where we find a # sign
	for (x=0; x<format.length;x++)
	{
		if (tempstringpointer <= tempstring.length)
		{
			if (format.substr(x, 1)=="#")
			{
				returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
				tempstringpointer++;
			}else{
				returnstring = returnstring + format.substr(x, 1);
			}
		}
		
	}

	// We have gone through the entire format, let's add the extension back on.
		returnstring = returnstring + extension;
	
	//we're done - let's return our value to the field.
	num.value = returnstring;
}	
