// SPECIAL NOTE:
// In all the functions below the variable 'blnRequired'
// is a boolean variable. If blnRequired is false value is optional
// and if blnRequired is true then value is not optional

/****************************************************
 * CHECK Birth Date
 * SAMPLE USAGE: onBlur="checkBirthDate(document.formname.objectname, false);"
 *****************************************************/
	function checkBirthDate(txtObj, blnRequired)
	{
		var str = txtObj.value;	
		var re = /\b(1[0-2]|0?[1-9])([12][0-9]|3[01]|0?[1-9])((19|20)?\d\d)/		
		var reg = /\b(1[0-2]|0?[1-9])[\-\/](3[01]|[12][0-9]|0?[1-9])[\-\/]((19|20)?\d\d)/

						
		//if Birth Date is optional
		if (txtObj.value == "" && blnRequired == false)
		{				
			return true;			
		}
		if (txtObj.value == "" && blnRequired == true)
		{
			alert("Birth Date is Mandatory. \nPlease enter your Birth Date in format mm/dd/yyyy")		
			txtObj.focus();
			txtObj.select();		
			return false;
		}
		
		var testre = re.test(str)
		var testreg = reg.test(str)
		

		// Validate the Birth Date field
		
		if(testreg)
		{
			ar = reg.exec(str)		
			
		}
		else if(testre)
		{
			ar = re.exec(str)			
		}
		 
		else
		{
			alert("" + str + " is an invalid Birth Date");					
			txtObj.focus();
			txtObj.select();
			return false;	
		}
		
		
			var month = ar[1]
			var day = ar[2]
			var year = ar[3]		
			var onedate = new Date();
			//var curYear = onedate.getYear();
			var curYear = onedate.getFullYear(); //Updated for FireFox on 9/9/08
			var curMonth = (onedate.getMonth())+1;			
			var curDay = onedate.getDate();
			if(month.length == "1")
			{
				month= "0" + month
			}
			if(day.length == "1")
			{
				day="0"+day
			}	
			// HZ - fix for >100 year old
			if(parseInt(year.length) == 2)			
			{
				//var y = "" ;
				//var yr = curYear.toString();
				//y = yr.substring(2,4);
				//if(year > y)
				//{
					year ="20"+year					
				}
				else
				{				
					// year = yr.substr(0,2) + year;					
					
				//}							
			}			
			txtObj.value = month+"/"+day+"/"+year	
            var ageDiff = curYear-year;	
           	if(ageDiff < 0 || ageDiff > 120)
			{
			
				alert("Please check the Birth Date. This is an invalid Birth Date.")
				txtObj.focus();
				txtObj.select();
				return false
			
			}
			else if(ageDiff <= 120)
			{
			 if(ageDiff == 0)
			 {
				if(month <= curMonth)
				{
					if(curMonth == month)
					{
						if(day <= curDay)
						{
							if(!checkLeapYear(month,year,day))
							{
								txtObj.focus();
								txtObj.select();
								return false
							}								
							else
							{
								return true								
							}	
						}
						else
						{
							alert("Please check the Birth Date")
							txtObj.focus();
							txtObj.select();
							return false
						}
					}
					else
					{
							if(!checkLeapYear(month,year,day))
							{
								txtObj.focus();
								txtObj.select();
								return false
							}	
							else
							{
								return true
							}					
					}
				}	
				else
				{
					alert("Please check the Birth Date")
					txtObj.focus();
					txtObj.select();
					return false
				}			
						
			 }	
			 
			else
			{		
			  if(!checkLeapYear(month,year,day))
				{
					txtObj.focus();
					txtObj.select();
					return false
				}	
			  else
				{
					return true
				}			 
			}				
		  }				
	}
	
function checkLeapYear(month, year,day)
{	
	if(month == "02")
	{	
		if((year%4) == 0)
		{
			if(day > 29)
			{
				alert(year+" is a leap year \n The month of Feb cannot have date greater than 29")	
				return false
			}
			else
			{
				return true
			}
		}
		else
		{	
			if(day > 28)
			{
				alert("The month of February cannot have date greater than 28 for year "+year)	
				return false
			}
			else
			{
				return true
			}
		}
	}		
	else if(month=="04" || month=="06" || month=="09" || month=="11")
	{
		if(day>30)
		{
			alert("The day can not have value greater than 30 \nfor the month of April,June,September,November");
			return false
		}
		else
		{
			return true
		}
	}
	else
		return true
}	


/****************************************************
 * CHECK Lab Receipt Date
 * SAMPLE USAGE: onBlur="checkLabReceiptDate(document.formname.objectname, false);"
 *****************************************************/
	function checkLabReceiptDate(txtObj, blnRequired)
	{
		var str = txtObj.value;	
		var re = /\b(1[0-2]|0?[1-9])([12][0-9]|3[01]|0?[1-9])((19|20)?\d\d)/		
		var reg = /\b(1[0-2]|0?[1-9])[\-\/](3[01]|[12][0-9]|0?[1-9])[\-\/]((19|20)?\d\d)/

						
		//if Birth Date is optional
		if (txtObj.value == "" && blnRequired == false)
		{				
			return true;			
		}
		if (txtObj.value == "" && blnRequired == true)
		{
			alert("Lab Receipt Date is Mandatory. \nPlease enter your Lab Receipt Date in format mm/dd/yyyy")		
			txtObj.focus();
			txtObj.select();		
			return false;
		}
		
		var testre = re.test(str)
		var testreg = reg.test(str)
		

		// Validate the Lab Receipt Date field
		
		if(testreg)
		{
			ar = reg.exec(str)		
			
		}
		else if(testre)
		{
			ar = re.exec(str)			
		}
		 
		else
		{
			alert("" + str + " is an invalid Lab Receipt Date");					
			txtObj.focus();
			txtObj.select();
			return false;	
		}
		
		
		
		
			var month = ar[1]
			var day = ar[2]
			var year = ar[3]		
			var onedate = new Date()
			var curYear = onedate.getYear();
			var curMonth = (onedate.getMonth())+1;			
			var curDay = onedate.getDate();
			
			if(month.length == "1")
			{
				month= "0" + month
			}
			if(day.length == "1")
			{
				day="0"+day
			}	
			if(parseInt(year.length) == 2)
			{
				var y = curYear.toString();
				y = y.substring(2,4);
				if(year >= y)
				{
					year ="19"+year					
				}							
			}			
			txtObj.value = month+"/"+day+"/"+year
			var ageDiff = curYear-year;	
					
			if(ageDiff < 0 || ageDiff > 100)
			{
			
				alert("Please check the Lab Receipt Date. This is an invalid Lab Receipt Date.")
				txtObj.focus();
				txtObj.select();
				return false
			
			}
			else if(ageDiff <= 100)
			{
			 if(ageDiff == 0)
			 {
				if(month <= curMonth)
				{
					if(curMonth == month)
					{
						if(day <= curDay)
						{
							if(!checkLeapYear(month,year,day))
							{
								txtObj.focus();
								txtObj.select();
								return false
							}								
							else
							{
								return true								
							}	
						}
						else
						{
							alert("Please check the Lab Receipt")
							txtObj.focus();
							txtObj.select();
							return false
						}
					}
					else
					{
							if(!checkLeapYear(month,year,day))
							{
								txtObj.focus();
								txtObj.select();
								return false
							}	
							else
							{
								return true
							}					
					}
				}	
				else
				{
					alert("Please check the Lab Receipt Date")
					txtObj.focus();
					txtObj.select();
					return false
				}			
						
			 }	
			 
			else
			{
			  if(!checkLeapYear(month,year,day))
				{
					txtObj.focus();
					txtObj.select();
					return false
				}	
			  else
				{
					return true
				}			 
			}				
		  }				
	}
	
function checkLeapYear(month, year,day)
{	
	if(month == "02")
	{	
		if((year%4) == 0)
		{
			if(day > 29)
			{
				alert(year+" is a leap year \n The month of Feb cannot have date greater than 29")	
				return false
			}
			else
			{
				return true
			}
		}
		else
		{	
			if(day > 28)
			{
				alert("The month of February cannot have date greater than 28 for year "+year)	
				return false
			}
			else
			{
				return true
			}
		}
	}		
	else if(month=="04" || month=="06" || month=="09" || month=="11")
	{
		if(day>30)
		{
			alert("The day can not have value greater than 30 \nfor the month of April,June,September,November");
			return false
		}
		else
		{
			return true
		}
	}
	else
		return true
}	

/****************************************************
 * CHECK US PHONE NUMBERS
 * SAMPLE USAGE: onBlur="CheckUSPhone(document.formname.objectname, false);"
 *****************************************************/
function CheckUSPhone(txtObj, blnRequired) 
{
	return(checkPhoneNumber(txtObj,blnRequired));
}

/*******************************************************
 * CHECK EMAIL FORMAT
 * SAMPLE USAGE: onBlur="CheckEMail(document.formname.objectname, false);"
 ********************************************************/
function CheckEMail(txtObj, blnRequired) 
{
	var str = txtObj.value;        
	var reg = /^.+\@(\[?)[\w\.-]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	
	// If EMail is a required field then
	if (txtObj.value == ""  && blnRequired == true)
	{
		alert("Please enter your e-mail address.")
		
		txtObj.focus();
		txtObj.select();
		return false;
	}
	
	// If EMail is specified then validate
	if (reg.test(str))
	{
		return true; //changed this to return true
	}
	// If EMail is specified and invalid
	else if (txtObj.value != "")
	{
		alert("" + str + " is an invalid e-mail address!");
		
		txtObj.focus();
		txtObj.select();
		return false;
	}
}


/*******************************************************
 * CHECK US Zip Code Format
 * SAMPLE USAGE: CheckUSZipCode="CheckEMail(document.formname.objectname, false);"
 ********************************************************/
function CheckUSZipCode(txtObj, blnRequired) 
{
	var str = txtObj.value;
	var reg1= /^\s*([0-9]{5})[\s\-]*$/;
	var reg = /^\s*([0-9]{5})[\s\-]*([0-9]{4})$/;
	var arone = reg1.exec(str)
	var ar = reg.exec(str)  

	// If Zip Code is not a required field
	if (txtObj.value == "" && blnRequired == false)
	{
		return true
	}
	// If Zip Code is a required field
	if (txtObj.value == "" && blnRequired == true)
	{
		alert("Please enter your zip code.")
		
		txtObj.focus();
		txtObj.select();
		return false;
	}
	// If Zip Code is specified then validate
	if (reg1.test(str)) 
	{
		txtObj.value = (arone[1]);
		return true; //changed to return true
	}
	// If Zip Code is specified then validate
	if (reg.test(str)) 
	{
		txtObj.value = (ar[1] + "-" + ar[2]);
		return true; //changed to return true
	}
	// If Zip Code is specified and invalid
	else if (txtObj.value != "")
	{
		alert("" + str + " is an invalid US Zip Code");
		
		txtObj.focus();
		txtObj.select();		
		return false;
	}
}

/*******************************************************
 * CHECK US Social Security Number Format
 * SAMPLE USAGE: CheckUSSocialSecurity="CheckEMail(document.formname.objectname, false);"
 ********************************************************/
function CheckUSSocialSecurity(txtObj, blnRequired) 
{
	var str = txtObj.value;        
	//var reg = /^\s*\(?([0-9]{3})\)?[\s\-]*([0-9]{2})[\s\-]*([0-9]{4})\s*$/;
	var reg = /^\s*\(?([A-Za-z0-9]{1}[0-9]{2})\)?[\s\-]*([0-9]{2})[\s\-]*([0-9]{4})\s*([A-Za-z]{0,2})\s*$/;
	
	var ar = reg.exec(str)

	// If Social Security Number is not a required field
	if (txtObj.value == "" && blnRequired == false)
	{
		return true
	}
 
	// If Social Security Number is a required field
	if (txtObj.value == "" && blnRequired == true)
 	{
		alert("Please enter your US Social Security number.")
		
		txtObj.focus();
		txtObj.select();
		return false;
	}
	// If Social Security Number is specified then validate	
	if (reg.test(str)) 
	{
		//txtObj.value = (ar[1] + "-" + ar[2] + "-" + ar[3]);
		txtObj.value = (ar[1].toUpperCase() + "-" + ar[2] + "-" + ar[3] + ar[4].toUpperCase());
		return true; //changed to return true
	}
	// If Social Security Number is specified and invalid
	else if (txtObj.value != "")
	{
		alert("" + str + " is an invalid US Social Security number");
		
		txtObj.focus();
		txtObj.select();
		return false;
	}
}

/*******************************************************
 * CHECK First Name and Last Name formats
 * USAGE: Use in the onChange event of the text box as follows:
 * onChange="this.value=check(this.value);checkLastName(document.formname.textfieldname, true)"
 * Similarly use for the first name
 ********************************************************/
function checkName()
{
	if ((checklastname()) && (checkfirstname()))
	{
		return true;      
	}
	else
		return false;
}

function check(txtVal)
{
	sName = "";
	for (k = 0; k < txtVal.length; k++)
	{
		koc = txtVal.substring(k, k+1);
		if ((koc >= "A" && koc <= "Z") || (koc >= "a" && koc <= "z") || (koc == "'") || (koc == ",") || (koc == "-") || (koc == " ")) 
		{
			sName += koc;
		}
	}

	if (txtVal!= sName) 
	{
		if (confirm("You entered invalid characters.\n\nClick OK to fix this.\n\nClick CANCEL to leave unchanged")) 
		{
			return sName;
		}  
		
		return txtVal;
	}
	
	return txtVal;
}

function checkLastName(txtObj, blnRequired)
{
	s = txtObj.value;
	var arr = s.split('"');
	s = "";
	//alert(String(arr.length));
	if (arr.length == 2)
	{
		txtObj.value = arr[0] + "'" + arr[1];
	}
	if (arr.length > 2)
	{
		for (var i=0; i < arr.length; i++)
		{
			s = s + arr[i];
			if (i < arr.length - 1)
			{
				if (parseInt(i%2) == 0)
				{
					s = s +  "(";
				}
				else
				{
					s = s + ")";
				}
			}
		}
		txtObj.value = s;
	}
	var txtVal= txtObj.value
	if (txtObj.value == "" && blnRequired == false)
	{
		return true;
	}
	if (txtObj.value == "" && blnRequired == true)
 	{
		
		alert("Please enter the last name.")
		txtObj.Value = " ";
		txtObj.focus();
		txtObj.select();		
		return false;
	}
	
	b = txtVal.substring(0,1);
	c = txtVal.substring(1,txtVal.length);

	if(!isNaN(b))
	{
		alert("The first character of Last Name should be an alphabet");	
		b = c.substring(0,1);				
		c = c.substring(1,txtVal.length);		
		if(c != "")
			txtObj.value = b.toUpperCase() + c;		// .toLowerCase()
		else
			txtObj.value = ""

		txtObj.focus();
		txtObj.select();
		return false;
	}
	txtObj.value = b.toUpperCase() + c;				// .toLowerCase()
	
	var regexp = /dds/i;
	var s = txtObj.value;
	
	
	
	txtObj.value = s.replace("dds", "DDS");
	//alert(s);
	
	return true;
}

function checkFirstName(txtObj, blnRequired)
{
	s = txtObj.value;
	var arr = s.split('"');
	s = "";
	//alert(String(arr.length));
	if (arr.length == 2)
	{
		txtObj.value = arr[0] + "'" + arr[1];
	}
	
	if (arr.length > 2)
	{
		for (var i=0; i < arr.length; i++)
		{
			s = s + arr[i];
			if (i < arr.length - 1)
			{
				if (parseInt(i%2) == 0)
				{
					s = s +  "(";
				}
				else
				{
					s = s + ")";
				}
			}
		}
		txtObj.value = s;
	}
	var txtVal= txtObj.value

	if (txtObj.value == "" && blnRequired == false)
	{
		return true;
	}

	if (txtObj.value == "" && blnRequired == true)
 	{
		alert("Please enter the first name.")
		txtObj.Value = " ";
		txtObj.focus();
		txtObj.select();
		return false;
	}
	
	
	b = txtVal.substring(0,1);
	c = txtVal.substring(1,txtVal.length);

	if(!isNaN(b))
	{
		alert("The first character of First Name should be an alphabet");	
		b = c.substring(0,1);				
		c = c.substring(1,txtVal.length);		
		if(c != "")
			txtObj.value = b.toUpperCase() + c;		// .toLowerCase()
		else
			txtObj.value = ""

		txtObj.focus();
		txtObj.select();
		return false;
	}

	txtObj.value = b.toUpperCase() + c;				// .toLowerCase()
	
	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////
////          function checks the phone number at least 10 digits & at most 14 digits
////		accept formated phone no & simple digits also
//////////////////////////////////////////////////////////////////////////////////////////

function checkPhoneNumber(elementName,bReq)
{var str=elementName.value
var temp=""
var pattern1=/^\(\d{3}\)\s\d{3}\-\d{4}\s\[\d{1,4}\]$/;
var pattern2=/^\(\d{3}\)\s\d{3}\-\d{4}$/;
var pattern3=/^\d{11,14}$/;
var pattern4=/^\d{10}$/;

if(bReq==false && str=="")
{
	return true;
}
if(bReq==true)
{
if (!pattern1.test(str))
	{ if (!pattern2.test(str))
			{	if (!pattern3.test(str) )
						{	if (!pattern4.test(str))	
								{
								alert("Please Enter Valid Phone Number \n of at least 10 Digits and at most 14 Digits");
								elementName.focus();
								elementName.select();
								return false;
								}
							else
								{
								temp=temp+"("+str.substring(0,3)+") ";
								temp=temp+str.substring(3,6)+"-";
								temp=temp+str.substring(6,10)
								elementName.value=temp;	
								return true;
								
								}	
						}
					else
						{
						temp=temp+"("+str.substring(0,3)+") ";
						temp=temp+str.substring(3,6)+"-";
						temp=temp+str.substring(6,10)+" [";
						temp=temp+str.substring(10,14)+"]";
						elementName.value=temp;	
						return true;
						}
			}
		else 
			return true;
				   
	}
else
	return true;
}	

if(bReq==false && str!="")
{
if (!pattern1.test(str))
	{ if (!pattern2.test(str))
			{	if (!pattern3.test(str))
						{	if (!pattern4.test(str))	
								{
								alert("Please Enter Valid Phone Number \n of at least 10 Digits and at most 14 Digits");
								elementName.focus();
								elementName.select();
								return false;
								}
							else
								{
								temp=temp+"("+str.substring(0,3)+") ";
								temp=temp+str.substring(3,6)+"-";
								temp=temp+str.substring(6,10)
								elementName.value=temp;	
								return true;
								
								}	
						}
					else
						{
						temp=temp+"("+str.substring(0,3)+") ";
						temp=temp+str.substring(3,6)+"-";
						temp=temp+str.substring(6,10)+" [";
						temp=temp+str.substring(10,14)+"]";
						elementName.value=temp;	
						return true;
						}
			}
		else
			return true;	
	   
	}
else
	return true;
}

}

// Function used for comparing current date
// Info				Sudhakar, Phase I, 24 Jul 2000
// Argument			The html element containing the value for date
// Returns			true if current date <  specified date, false if current date > specified date
function compareDate(obj)
{
	// Get today's date
	var curDate = new Date();
	// Get the specified date
	var specDate = new Date(Date.parse(obj.value));
		
	// If today's year > specified year
	if (curDate.getYear() > specDate.getYear())
	{
		//alert("Cannot be an yester year !");
		return (false);
	}
	// If today's month > specified month
	else if ((curDate.getYear() == specDate.getYear()) && (curDate.getMonth() > specDate.getMonth()))
	{
		//alert("Cannot be an yester month !");	
		return (false);
	}
	// If today's date (day) > specified date (day)
	else if ((curDate.getYear() == specDate.getYear()) && (curDate.getMonth() == specDate.getMonth()) && (curDate.getDate() >= specDate.getDate()))
	{
		//alert("Cannot be an yester day !");
		return (false);
	}
	//alert("Is a valid expiration date");
	return (true);
}


// Function used for comparing From Date with To Date
// Info				Manav, PracticeConnect Phase II, 12 Jan 2001
// Argument			The html element containing the value for From date and To Date
// Returns			0 if From date = To date, 1 if From date < To date, 2 if From date > To date, 
//					-1 if any one of them is blank.
function DateComparison(fromDate, toDate) 
{
	if ((toDate.getYear() == fromDate.getYear()) && (toDate.getMonth() == fromDate.getMonth()) && (toDate.getDate() == fromDate.getDate()))
	{
		//alert("Both Dates are equal")
		return(0);
	}
	
	// If to year > From year
	if (toDate.getYear() < fromDate.getYear())
	{
		//alert("Cannot be an yester year !");
		return (2);
	}
	// If to month > From month
	else if ((toDate.getYear() == fromDate.getYear()) && (toDate.getMonth() < fromDate.getMonth()))
	{
		//alert("Cannot be an yester month !");	
		return (2);
	}
	// If to date (day) > From date (day)
	else if ((toDate.getYear() == fromDate.getYear()) && (toDate.getMonth() == fromDate.getMonth()) && (toDate.getDate() < fromDate.getDate()))
	{
		//alert("Cannot be an yester day !");
		return (2);
	}
		
	//alert("From Date is less that To Date.");
	return (1);
}

/******************************************************************	
	*	Credit Card Number Validation JavaScript
******************************************************************/
	// change it to false if you do not want
	//the credit card number to be encrypted
	var encrypt_it = false;


	/* ==================================================================
	   THIS FUNCTION IS TAKEN DIRECTLY FROM NETSCAPE FROM:
	   http://developer.netscape.com/library/examples/...
	                    .../javascript/formval/FormChek.js
	   which is a bunch of functions to validate forms
	    
	   FUNCTION: isCreditCard(st)
	   INPUT:    st - a string representing a credit card number
	   RETURNS:  true, if the credit card number passes the Luhn Mod-10 test
		         false, otherwise
	   ================================================================== */
	
	function isCreditCard(st) 
	{
		// Encoding only works on cards with less than 19 digits
		if (st.length > 19)
			return (false);

		sum = 0; mul = 1; l = st.length;
		
		for (i = 0; i < l; i++) 
		{
			digit = st.substring(l-i-1,l-i);
			tproduct = parseInt(digit ,10)*mul;
			
			if (tproduct >= 10)
				sum += (tproduct % 10) + 1;
			else
				sum += tproduct;
			
			if (mul == 1)
				mul++;
			else
				mul--;
		}

		if ((sum % 10) == 0)
			return (true);
		else
			return (false);
	}

	function getCCNum(default_val) 
	{
		/*
		msg = 'Please enter your credit card number here. '
		+ 'It will be '
		+ ((encrypt_it) ? "encrypted and then " : "")
		+ 'put into the credit card field of the form '
		+ 'after it is validated.';
		*/
		//return prompt(msg,default_val);
		return (default_val);
	}

	// takes in a credit card number, adds one to each digit
	// (9 becomes 0), and then returns the encrypted credit
	// card number with an 'e' tacked on to the end to signal
	// the number has been encrypted
	function encrypt(val) 
	{
		val = "" + val;
		var result = "";
		for (i=0;i<val.length;i++) 
		{
			character = val.charAt(i);
			
			if ("0123456789".indexOf(character) != -1) 
			{
				character = parseInt(character);
				character = (character+1)%10;
			}
			result += character;
		}
		
		if (result != "")
			result += "e";
		return result;
	}

	function unencrypt(val) 
	{
		val = "" + val;
		for (n=0;n<9;n++)
		val = encrypt(val);
		return (val.substring(0,val.indexOf('e')));
	}

	function strip(val) 
	{
		val = "" + val;
		if (!val)
			return "";
		var result = "";
		
		for (i=0;i<val.length;i++) 
		{
			character = val.charAt(i);
			if ("0123456789".indexOf(character) != -1)
			result += character;
		}
		return result;
	}

	var last_entry = "";
	
	// Returns true if character c is an English letter 
	// (A .. Z, a..z).
	//
	// NOTE: Need i18n version to support European characters.
	// This could be tricky due to different character
	// sets and orderings for various languages and platforms.
	
	function isLetter (c)
	{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
	}
	
	// Returns true if character c is a digit 
	// (0 .. 9).

	function isDigit (c)
	{   return ((c >= "0") && (c <= "9") || (c = " "))
	}
	
	// Returns true if character c is a letter or digit.	
	function isLetterOrDigit (c)
	{   return (isLetter(c) || isDigit(c))
	}	
	
	function doCCStuff(form_element) 
	{
		
		if (isDigit(form_element.value) == false)
		{
			alert("Your credit card number is invalid. Please enter valid credit card number.");
			form_element.select();
			form_element.focus();
			return(false);
		}
		if (blur_reset) 
		{
			last_entry = form_element.value;
			
			if (last_entry && last_entry.indexOf('e') != -1)
				last_entry = unencrypt(last_entry);
			
			entry = getCCNum(last_entry);
			stripped_entry = strip(entry);
			
			if (entry && (!isCreditCard(stripped_entry))) 
			{
				alert('The credit card number you entered could not be '
				+ 'validated. Please check the number and try again.');
				last_entry = entry;
				form_element.select();
				form_element.focus();
				return (false);
				
				/*
				entry = getCCNum(last_entry);
				stripped_entry = strip(entry);
				*/
			}
			
			if (entry) 
			{
				if (encrypt_it)
					form_element.value = encrypt(entry);
				else
					form_element.value = entry;
			}
			
			blur_form(form_element);
		}
		return true;
	}
	
	var blur_reset = true;
	
	function blur_form(form_element) 
	{
		form_element.blur();
		blur_reset = false;
		setTimeout("blur_reset=true",2000);
	}


/**
**************************************************************************************************
	* Checks and formats values to currency format after rounding off. Ex: 230.012 => 230.01
**************************************************************************************************	
**/
function checkCurrency(elmntName)
{
  if (elmntName.value == "" )
  {
    elmntName.value = "0.00"
    return (true);
  }
  ///Added By Rahul. PracticeConnect(TM). 27 Dec 2000
  if (parseInt(elmntName.value) == 0 )
  {
    elmntName.value = "0.00"
    return (true);
  }
  
 //If the value is like 23 then onblur convert it to 23.00	..RK
  s = elmntName.value
  sstr = /\./
  r = s.search(sstr)
  if(r == -1)
	elmntName.value = elmntName.value  //+ ".00"
  
  //If the value is like 23.4 then onblur convert it to 23.4 ..RK	
  s1 = elmntName.value
  sstr1 = /\.\d{2}/
  r1 = s1.search(sstr1)
  if(r1 == -1)
	elmntName.value = elmntName.value  //+ "0"
		
  //If no decimal point is there then check ..RK
  s2 =  elmntName.value	
  sstr2 = /\./
  r2 = s2.search(sstr2)
  if ( r2 == -1 )
  	checkUpto = 6
  else
  	checkUpto = 9 
  	
  
  if (elmntName.value.length > checkUpto)
  {
    alert("Please enter at most " + checkUpto + " digits in the field, excluding decimal points.");
    elmntName.focus();
    return (false);
  }

  var checkOK = "0123456789-.";
  var checkStr = elmntName.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter a valid number in the  field. ");
    elmntName.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Please enter a valid number in the  field.");
    elmntName.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseFloat(allNum);
  
  if (chkVal != "" && (prsVal < 0.00))
  {
    alert("Please enter a value greater than or equal to \"0.00\" in the field.");
    elmntName.focus();
    return (false);
  }
  else
  {	
	elmntName.value = roundOff(prsVal, 2)
  } 
  return (true);
}

/*--------------------------------------------------------------------------
Function for Rounding off to desired decimal point
function roundOff(value, precision)
{
        value = "" + value //convert value to string
        
        precision = parseInt(precision);
        var whole = "" + Math.round(value * Math.pow(10, precision));
        var decPoint = whole.length - precision;
		//----------------------------------------
		//Changes done on 5 Feb 2002
        if(decPoint > 0)
        {
                result = whole.substring(0, decPoint);
                result += ".";
                result += whole.substring(decPoint, whole.length);
        }
        else if(decPoint == 0)
        {
                result = "0." + whole;
        }
        else
				result = "0.0" + whole;
		//----------------------------------------		    
        return result;
}
----------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------
Function for Rounding off to desired decimal point - modified - 19 Aug 2002 - SS
----------------------------------------------------------------------------*/
function roundOff(value, precision)
{
        value = "" + value //convert value to string
        
        precision = parseInt(precision);
        var whole = "" + Math.round(parseFloat(value) * Math.pow(10, precision));
        
        var result = 0;
        
        result = String(whole * Math.pow(10, (-1) * precision));
        
        var s = result.split(".");
        if (s.length == 1) result = result + ".00";
        
	// check if the result has more than 'precision' no. of digits after the decimal
        var t = result.split(".");
        if (t[1].length > precision)
        {
		t[1] = t[1].substr(0, precision);
		result = t[0] + "." + t[1];
	}
	
        return result;        
}

//************************************Validate Medicaid No**************Author:Manoj Arora****
// format for Medicaid No : X99999 where X is an alphabet and 9 represents digits
// SS - 18 Nov 2002
// 1) minimum length should be 6 and maximum length should be 15
// 2) can be any or all characters or any or all numeric
//********************************************************************************************
function checkMedicaidNo(ElmntName, blnRequired)
{
	var ElmntVal =ElmntName.value
	//var IstChar = ElmntVal.substring(0,1)
	//var succChars = ElmntVal.substring(1,ElmntVal.length)
	var allValid = true;
	if(ElmntVal == "" && blnRequired == false)
		return true
		
	if(ElmntVal == "" && blnRequired == true)
	{
		alert ("Please enter the Medicaid#")
		ElmntName.focus()
		ElmntName.select()
		return false
	}
	
	if(ElmntVal != "" && (blnRequired == true || blnRequired == false))	
	{
		if(ElmntVal.length < 6 || ElmntVal.length > 15)
		{
			alert("Please enter Medicaid# with minimum 6 and maximum 15 characters. \nEnter only letters and digits.")
			ElmntName.focus()
			ElmntName.select()
			return false;
		}
		// allow only uppercase chars (not abcdefghijklmnopqrstuvwxyz)
		var validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
		for (var i = 0;  i < ElmntVal.length;  i++)
  		{
		    ch = ElmntVal.charAt(i);
		    for (var j = 0;  j < validChars.length;  j++)
		    	if (ch == validChars.charAt(j))
		        	break;
		    	if (j == validChars.length)
    			{
			      allValid = false;
			      break;
			}
		}
  		if (!allValid)
  		{
    			alert("Please check the format for Medicaid#.\nEnter only upper-case letters and digits.")		
			ElmntName.focus()
			ElmntName.select()
			return false
		}
		ElmntName.value = ElmntVal.toUpperCase()
		return (true);
	}	

		//if(isLetter(IstChar))	
		//	val = IstChar.toUpperCase()	
		//else
		//{
		//	alert("The first charcter of Medicaid# should be an alphabet")
		//	ElmntName.focus()
		//	ElmntName.select()
		//	return false;
		//}	
	
		//if(isNaN(succChars))
		//{
		//	alert("Please check the format for Medicaid#.\n\n The first character should be an alphabet and succeding characters should be digits.")		
		//	ElmntName.focus()
		//	ElmntName.select()
		//	return false
		//}	
		//ElmntName.value = val + succChars
		//return true
	//}		
}

// Function for validating the dentist's tax identification (Tax ID)
// Typical format: 12-3456789
function checkTaxID(txtObj, blnRequired)
{
	var str = txtObj.value;
	// TaxID regular expression
	var reg = /^\s*\(?([0-9]{2})\)?[\s\-]*([0-9]{7})\s*$/;
	var ar = reg.exec(str);
	
	// If TaxID is not a required field
	if (txtObj.value == "" && blnRequired == false)
	{
		return (true);
	}
	
	// If TaxID is a required field
	if (txtObj.value == "" && blnRequired == true)
	{
		alert("Please enter your Tax ID.");
		txtObj.focus();
		txtObj.select();
		return(false);
	}
	
	// If TaxID is specified then validate
	if (reg.test(str))
	{
		txtObj.value = (ar[1] + "-" + ar[2]);
		return(true);
	}
	// If TaxID is specified and invalid
	else if (txtObj.value != "")
	{
		alert("" + str + " is an invalid Tax ID. Please specify your valid Tax ID.");
		txtObj.focus();
		txtObj.select();
		return(false);
	}

}

//************************************Validate 7 digit Phone No **************Author:Manoj Arora****
// Validation for 7 digit phone number
// 1) if user inputs 7 digit phone number then area code is placed in front of phone number 
// 2) The 7 digit Phone can be in the following formats
//	"123-4567" OR  "123 4567" OR "1234567"
// 3) This is valid for Gaurantor and Office pages only
//********************************************************************************************

	function setAreaCodeForPhone(ElmntName, blnRequired, AreaCode)
	{
		var ret
		if (ElmntName.value.length != 7)
		{
			return(checkPhoneNumber(ElmntName, blnRequired));
		}
		if(setAreaCode(ElmntName, blnRequired, AreaCode))
		{
			ret = checkPhoneNumber(ElmntName, blnRequired)
			if(String(ret).toUpperCase = "FALSE")
			{
				//alert(ret)
				ElmntName.select()
				ElmntName.focus()								
				return (ret)
			}
		}
		else
		{	
			ElmntName.focus()
			ElmntName.select()							
			return false
		}
		
	}

	function setAreaCode(ElmntName, blnRequired, AreaCode)
	{		
		var rexp = /\b(\d\d\d)([\-\ ])?(\d{4}\b)/		
		var ElmntVal = ElmntName.value
		var blnRequired = String(blnRequired)
		
		if( blnRequired.toUpperCase() == "FALSE" && ElmntVal == "" )
			return true
		
		if( blnRequired.toUpperCase() == "TRUE" && ElmntVal == "" )
		{
			alert("Please enter the Phone Number")			
			return false		
		}	

		var ElmntValPlusAC = ""
		var arr = rexp.exec(ElmntVal)
		if(arr)
		{			
			if(arr[2] == "-" || arr[2] == " ")
			{
				ElmntName.value = AreaCode + arr[1] + arr[3]
			}	
			else
				ElmntName.value = AreaCode + arr[0]			
		}
			
		return true
		//ret = checkPhoneNumber(ElmntName, blnRequired)
		//return (ret)
	}

//------------------------------------------------------------------------------
// Function that checks if the given element's value is a decimal number or not
//------------------------------------------------------------------------------
function checkFloat(e)
{
	var checkOK1 = "0123456789-.";
	var checkStr1 = e.value;
	var allValid = true;
	for (i = 0;  i < checkStr1.length;  i++)
	{
	  ch = checkStr1.charAt(i);
	  for (j = 0;  j < checkOK1.length;  j++)
	    if (ch == checkOK1.charAt(j))
	      break;
	  if (j == checkOK1.length)
	  {
	    alert("Please specify a valid numeric value.");
	    allValid = false;
	    break;
	  }
	}
	return(allValid);
}

//*******************************JAVASCRIPT TRIM**********************************************
//	Author:	Rahul Khot. PracticeConnect(TM). 17 Jan 2001
//  To Left Trim and Right Trim in JavaScript.
//	If suppose document.frm.txtObj.value = "  RK  "
//  Then using javaScriptTrim(document.frm.txtobj.value) will return "RK"
//********************************************************************************************
function javaScriptTrim(txtObj)
{
	// This will get rid of leading spaces 
	while(''+txtObj.charAt(0)==' ')
		txtObj	 =	txtObj.substring(1,txtObj.length);
		
	// This will get rid of trailing spaces 	
	while(''+txtObj.charAt(txtObj.length-1)==' ')
		txtObj	 =	txtObj.substring(0,txtObj.length-1);	
	
	return (txtObj);
}	

//-----------------------------------------------------------
// function to change first letter of each word capitalized
function lowerCaseUpperCase(obj)
{
	var s = obj.value;
	var s1 = s.split(" ");
	var s2 = "";
	var sFirstChar = "";
	var sRemStr = "";
	var iLen = 0;
	
	var i = 0;
	for (i=0; i < s1.length; ++i)
	{
		iLen = s1[i].length;
		sFirstChar = s1[i].substr(0, 1);
		sRemStr = s1[i].substr(1, iLen);
		sRemStr = sRemStr.toLowerCase();
		
		if (i == s1.length - 1)
		{
			s2 += sFirstChar.toUpperCase() + sRemStr;
		}
		else
		{
			s2 += sFirstChar.toUpperCase() + sRemStr + " ";
		}
	}
	obj.value = s2;
}

// functions used for validating zip code - 24 July 2002 - SS
function getZipCodes( objParser, objCity, objState, objZip )
{

	var collPlayers = objParser.selectNodes( "//z:row" );
	var node;
	for( var i = 0; i < collPlayers.length; i++ ) {
			
		node = collPlayers.item( i );
		if (String(objZip.value) == javaScriptTrim(node.getAttribute("ZipCode")))
		{
			// Set the values in the text box
			objZip.value = javaScriptTrim(node.getAttribute("ZipCode"));
			objCity.value = javaScriptTrim(node.getAttribute("City"));
			objState.value = javaScriptTrim(node.getAttribute("State"));
			lowerCaseUpperCase(objCity);			
			//document.frm.txtAreaCode.value = javaScriptTrim(node.getAttribute("AreaCode"));				
			return(true);				
		}			
	}			
	return(true);
}
	
function validateZipCode(objCity, objState, objZip, bRequired)
{
	if (CheckUSZipCode(objZip, bRequired) == false) return(false);
	
	if(javaScriptTrim(objZip.value) != "")
	{
		xmlData.async = false;
		xmlData.src = objZip.path + "?ZipCode=" + String(objZip.value);
		getZipCodes(xmlData, objCity, objState, objZip);
	}
}
//-----------------------------------------------------------

// added on 08 Oct 2002 - SS
// removes extra space, "", ' and ; characters in given string.
// useful in comments builder, procedure description etc.
function setTextContents(obj)
{
	var s = obj.value;
	
	// replace extra spaces between words with a space
	var arr = s.split(" ");
	var i;
	
	s = "";
	for(i=0;i < arr.length;i++)
	{
		if (javaScriptTrim(arr[i]) != "")
		{
			s += arr[i] + " ";			
		}
	}
	
	// replace double quotes
	regexp = /"/g;
	s = s.replace(regexp, "");
	// replace single quotes
	regexp = /'/g;
	s = s.replace(regexp, "");
	// replace semi-colons
	regexp = /;/g;
	s = s.replace(regexp, "");
	obj.value = javaScriptTrim(s);
}

/**
**************************************************************************************************
	* Added by Ruchika Abbi on 2/20/08 to allow credit balance in inactive accounts AR report
	* Checks and formats values to currency format after rounding off. Ex: 230.012 => 230.01
	* Allows negative (for credit balance) 
**************************************************************************************************	
**/
function checkCurrencyAllowCredit(elmntName)
{
  if (elmntName.value == "" )
  {
    elmntName.value = "0.00"
    return (true);
  }
  ///Added By Rahul. PracticeConnect(TM). 27 Dec 2000
  if (parseInt(elmntName.value) == 0 )
  {
    elmntName.value = "0.00"
    return (true);
  }
  
 //If the value is like 23 then onblur convert it to 23.00	..RK
  s = elmntName.value
  sstr = /\./
  r = s.search(sstr)
  if(r == -1)
	elmntName.value = elmntName.value  //+ ".00"
  
  //If the value is like 23.4 then onblur convert it to 23.4 ..RK	
  s1 = elmntName.value
  sstr1 = /\.\d{2}/
  r1 = s1.search(sstr1)
  if(r1 == -1)
	elmntName.value = elmntName.value  //+ "0"
		
  //If no decimal point is there then check ..RK
  s2 =  elmntName.value	
  sstr2 = /\./
  r2 = s2.search(sstr2)
  if ( r2 == -1 )
  	checkUpto = 6
  else
  	checkUpto = 9 
  	
  
  if (elmntName.value.length > checkUpto)
  {
    alert("Please enter at most " + checkUpto + " digits in the field, excluding decimal points.");
    elmntName.focus();
    return (false);
  }

  var checkOK = "0123456789-.";
  var checkStr = elmntName.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter a valid number in the  field. ");
    elmntName.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Please enter a valid number in the  field.");
    elmntName.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseFloat(allNum);
  
  if (chkVal != "" && (prsVal < -9999.99))
  {
    alert("Please enter a value greater than or equal to \"-9999.99\" in the field.");
    elmntName.focus();
    return (false);
  }
  else
  {	
	elmntName.value = roundOff(prsVal, 2)
  }
  return (true);
}
