
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//character counter function

function countchr(oname, cname, val, max){
	mchr=max;
	cval = val; 
	if (cval.length > mchr) {
	  alert('Sorry, you are over the limit of '+ mchr +' characters');
	  oname.value = val.substring(0,mchr);
	  oname.focus()
	}
	cname.value=mchr-parseInt(oname.value.length);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// --------------- format Phone ------------------------
function formatPhone (phone) {
 	if (phone.length==0)
		return "";

	var checkphone=getnumbers(phone);

	if (checkphone.length != 0)
		if (checkphone.length < 10 || checkphone.length > 10)
			alert('Telephone Numbers must be 10 numbers in length,\nincluding the Area Code.\n\nExample: 206-555-1212');
//	else

	checkphone="("+checkphone.substring(0,3)+") "+checkphone.substring(3,6)+"-"+checkphone.substring(6,10);
	return checkphone;		 

} //func formatPhone


// --------------- format Date --------------------------
function formatDate (date,warn,formid) {

	if (warn=="")
		warn=false;
		
	if (date.length==0)
		return "";

	// check to see if anything but numbers are in date string
	var checkOK = "0123456789";
	var numsonly=true;
	var isnumber;

	for (i = 0;  i < date.length;  i++)
	{
	  ch = date.charAt(i);
	  isnumber=false;
	  for (j = 0;  j < 10;  j++)
	    if (ch == checkOK.charAt(j))
	      {isnumber=true; break;}
	      
	if (!isnumber)
		{numsonly=false; break;}
	 } //for i=0
	
	if (!numsonly) // check for proper date structure: 00/00/0000
	{
	  var checkOK = "0123456789";
	  var checkStr = "";
	  var count=0;
	  var addchar="";
	  var eop=false;
	  
	  for (i = 0;  i < date.length;  i++)
	  {
	    ch = date.charAt(i);
	    isnumber=false;
	    
	    for (j = 0;  j < 11;  j++)
	      if (ch == checkOK.charAt(j))
	        {isnumber=true;break;}
		
		if (isnumber) // number found, add it to addchar
			{addchar=addchar+ch;count++;}
		  else // no number found, must be separator, precede it with a zero
			if (count==1)
				{addchar="0"+addchar;eop=true;}
			else
				if (count > 1)
					{eop=true;}

		//alert('addchar='+addchar+'\n\ncount='+count+'\n\neop='+eop+'\n\ncheckStr.length='+checkStr.length);
		
		if (eop || count==4) // if end of part, add it to checkStr
			{checkStr=checkStr+addchar; addchar=""; eop=false; count=0;}
		
		//alert('checkStr='+checkStr);
	   } // for
	 var checkdate=checkStr;
	  //alert('checkdate='+checkdate);
	 } 
	 else
	 var checkdate=date;
	// !numsonly

	 
	// if other characters exist, make all months, days contain two digits
	// example: month of 1 (jan.) should read 01   i.e. 01/22/1980	not 1/22/1980
	
	if (checkdate.length < 8)
		allValid=false;
	else
		allValid=true;

	if (!allValid && warn)
		{
		alert('Date is invalid!\n\nPlease re-enter the Date before submitting the form.\n\nDate fields MUST be in the format: MM/DD/YYYY');
		//var obid=eval("document.Personalform." + formid);
		this.focus();
		}

	checkdate=checkdate.substring(0,2)+"/"+checkdate.substring(2,4)+"/"+checkdate.substring(4,8);
		
	return checkdate;
	//	return date;
	
} //func formatDate

// ----------------- getnumbers(str) --------------------
// Returns only numbers in the string passed in, strips everything else
function getnumbers(str) {

var checkOK = "0123456789";
  var checkStr = "";
  
  for (i = 0;  i < str.length;  i++)
  {
    ch = str.charAt(i);
    for (j = 0;  j < 10;  j++)
      if (ch == checkOK.charAt(j))
        checkStr=checkStr+ch
   }

	return (checkStr);

} // func getnumbers