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;  //Changed for Firefox
		

	// 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;
}	

function NumberLength(p,l,f) {
  var lentest = p.value.replace(/[^\d+$]/gi,'');
  var dastr = p.value;
  var dalength = l;
  var formattype = f;
  var dasleng = lentest.length;
  if (dasleng > 0 && dasleng < dalength) {
//    t.select();
    alert("There are "+dalength+" digits in a "+formattype+", please enter the entire number");
    p.focus();
	p.select();
	return false;
  }
}

function Commify(n) { 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 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 statemask(t) {
  var str = t.value;
  var sleng = str.length;
  if (sleng > 0 && sleng < 2) {
    alert("There are 2 letters in a complete state abbreviation, please enter the correct abbreviation");
    t.focus();
    t.select();
    return;
  }
  t.value = t.value.toUpperCase();
}

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

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

function numbersonly(t) {
//  var valid = "1234567890-,."
//  var ok = "yes";
//  var temp;
//  for (var i=0; i<t.value.length; i++) {
//    temp = "" + t.value.substring(i, i+1);
//    if (valid.indexOf(temp) == "-1") {
//      ok = "no";
//    }
//    if (ok == "no") {
      t.value = t.value.replace(/[^\d-.,]/gi,'');
//      alert("Invalid entry!  Only numbers allowed!");
//      t.focus();
//      t.select();
//    }
//  }
}

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 currencymask(t) {
var patt = /(\d*)\.{1}(\d{0,2})/;
var donepatt = /^(\d*)\.{1}(\d{2})$/;
var str = t.value;
var result;
if (!str.match(donepatt))
{result = str.match(patt);
if (result!= null)
{t.value = t.value.replace(/[^\d]/gi,'');
str = result[1] + '.' + result[2] ;
t.value = str;
}else{
if (t.value.match(/[^\d]/gi))
t.value = t.value.replace(/[^\d]/gi,'');}
}
}


function CheckDate(t,dadiff) {
	NumberLength(t,8,'Date');
	var str = t.value;
	var patt = /(\d{2}).*(\d{2}).*(\d{4})/;
	var result = str.match(patt);
	if (result!= null) {
		var dcheck = t.value.replace(/[^\d]/gi,'');
		var day = Number(result[2]);
		var month = (Number(result[1]) - 1);
		var year = result[3];
		if (month > 11) {
			alert("There are only 12 months in a month, please try again.");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		var myDate = new Date(year,month,day);
		var myMonth = myDate.getMonth();
		if (myMonth != month) {
			alert('That date is not valid!');
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		if (dadiff!=null) {
			tnd = new Date();
			tndy = tnd.getFullYear();
			var thisdiff = tndy - year;
			if (thisdiff > dadiff) {
				alert("The year is more than "+dadiff+" years, please check your answer");
			}
		}
	}
}

function CheckTime(t) {
	NumberLength(t,6,'Time');
	var str = t.value;
	var patt = /(\d{2}).*(\d{2}).*(\d{2})/;
	var result = str.match(patt);
	if (result!= null) {
		var dcheck = t.value.replace(/[^\d]/gi,'');
		var hour = Number(result[2]);
		var minute = (Number(result[1]) - 1);
		var second = result[3];
		if (hour > 23) {
			alert("Hours must be from 00 to 23 (Military Time)");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		if (minute > 59) {
			alert("Minutes cannot be greater than 59");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		if (second > 59) {
			alert("Seconds cannot be greater than 59");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
	}
}

function CheckDateTime(t,dadiff) {
	NumberLength(t,14,'Date and Time');
	var str = t.value;
	var patt = /(\d{2}).*(\d{2}).*(\d{4}).*(\d{2}).*(\d{2}).*(\d{2})/;
	var result = str.match(patt);
	if (result!= null) {
		var dcheck = t.value.replace(/[^\d]/gi,'');
		var day = Number(result[2]);
		var month = (Number(result[1]) - 1);
		var year = result[3];
		var hour = result[4];
		var minute = result[5];
		var second = result[6];
		if (month > 11) {
			alert("There are only 12 months in a month, please try again.");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		var myDate = new Date(year,month,day);
		var myMonth = myDate.getMonth();
		if (myMonth != month) {
			alert('That date is not valid!');
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		if (dadiff!=null) {
			tnd = new Date();
			tndy = tnd.getFullYear();
			var thisdiff = tndy - year;
			if (thisdiff > dadiff) {
				alert("The year is more than "+dadiff+" years, please check your answer");
			}
		}
		if (hour > 23) {
			alert("Hours must be from 00 to 23 (Military Time)");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		if (minute > 59) {
			alert("Minutes cannot be greater than 59");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
		if (second > 59) {
			alert("Seconds cannot be greater than 59");
			t.value = '';
			t.focus();
			t.select();
			return;
		}
	}
}

function NoEnter (field, event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13) {
    var i;
    for (i = 0; i < field.form.elements.length; i++) {
      if (field == field.form.elements[i]) {
        break;
      }
    }
    i = (i + 1) % field.form.elements.length;
    field.form.elements[i].focus();
    return false;
  } else {
    return true;
  }
}

function DCKiller(dform) {
  if (document.getElementById) {
   for (var sch = 0; sch < dform.length; sch++) {
    if (dform.elements[sch].type.toLowerCase() == "submit") dform.elements[sch].disabled = true;
   }
  }
return true;
}

function PutDate(theform,thefield) {

var thedate=new Date()
var theyear = thedate.getFullYear();
var themonth = thedate.getMonth() + 1;
var theday = thedate.getDate();

if (themonth<10) {
  themonth="0"+themonth;
}
if (theday<10) {
  theday="0"+theday;
}

var thenewdate = themonth+"-"+theday+"-"+theyear;

eval('document.forms.'+theform+'.'+thefield+'.value=thenewdate')

}

function CursorChange(t, tid) {
  var sid = t.id;
  alert(sid);
//  document.getElementById(sid).style.cursor = tid;
}

function CursorDefault(sid) {
  var sid = t.id;
//  document.getElementById(sid).style.cursor = 'default';
}
