function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c)
{  
	return ((c >= "0") && (c <= "9")) ;
}

function isInteger (s)
{   var i;

	if (isEmpty(s))
	{ 
		if (isInteger.arguments.length == 1)
		{
			return false;						  
		}
		else 
		{
			return (isInteger.arguments[1] == true);
		}
	}

	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);
		if (!isDigit(c)) 
		{
			return false;
		}
	}
	// All characters are numbers.
	return true;
}

function checkValue(val,say)
{
	if (val.value == "") 
	{
		alert("Error:  Required entry is missing.\nPlease enter "+say);
		val.focus(); 
		return false;
	}
	return true;
}

function checkZip(val)
{		 
	if(val.length==5 || val.length==10)
	{		 
		if(val.length==5)
		{
			if(isInteger(val))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if(val.charAt(5)=="-")
			{
				if(isInteger(val.substring(0,5)) && isInteger(val.substring(6,10)))
				{
					return true;
				}
				else
				{
					return false;		
				}
			}
		}
	}
	else
	{
		return false;
	}

}


function checkEmail(val,say) 
{
	if (val.value == "") 
	{
		alert("Error:  Required entry is missing.\nPlease enter your "+say);
		val.focus(); 
		return false;
	}
	eMail = val.value;
	if ((eMail.length < 6)||(eMail.indexOf("@",1) == -1)||(eMail.indexOf(".",3) == -1)||(eMail.lastIndexOf(".",eMail.length-3) == -1))
	{
		alert("Error:  Bad email format.\nPlease correct your email address.");
		val.focus(); 
		return false;
	}
	return true;
}


function validatePhone(phoneA,phoneB,phoneC)
{
	if (!isInteger(d = phoneA.value) || (d.length != 3)) 
	{
		alert("Please enter your telephone area code.\n");
		phoneA.focus();
		return false;
	}

	if (!isInteger(d = phoneB.value) || (d.length != 3)) 
	{
		alert("Please enter the middle 3 digits of your telephone number.\n");
		phoneB.focus();
		return false;
	}

	if (!isInteger(d = phoneC.value) || (d.length != 4)) 
	{
		alert("Please enter the last 4 digits of your telephone number.\n");
		phoneC.focus();
		return false;
	}
	return true;
}

function validateFax(faxA,faxB,faxC)
{
	if (!isInteger(d = faxA.value) || (d.length != 3)) 
	{
		alert("Please enter your fax area code.\n");
		faxB.focus();
		return false;
	}

	if (!isInteger(d = faxB.value) || (d.length != 3)) 
	{
		alert("Please enter the middle 3 digits of your fax number.\n");
		faxB.focus();
		return false;
	}

	if (!isInteger(d = faxC.value) || (d.length != 4)) 
	{
		alert("Please enter the last 4 digits of your fax number.\n");
		faxC.focus();
		return false;
	}
	return true;
}

//Returns the current element's index
function getIndex(myForm,myName)
{	 
 	for(i=0;i<myForm.elements.length;i++)
	{	   
		if(myForm.elements[i].name == myName)		//Loop through all emements on a form until you find a match 
			return i;
	}					 
	return -1;										//No match found
}


function autoTab(myForm,myName,myValue,myMax)
{
	var myIndex=-1;  

 	if(myValue.length >= myMax)						//Check to see if its time to move focus
	{		
		myIndex = getIndex(myForm, myName);			//Get the index of the element that currently has focus

		if(myIndex>=0)								//Make sure we got a valid index
		{
			myForm.elements[myIndex+1].focus();		//Give next element focus
			myForm.elements[myIndex+1].select();
			return true;	
		}
		else
			return false;							//Invalid index
	}						
	else
		return false;						  		//Not time to change focus
}


function trim(inputString)
{
	string = new String(inputString);
	string = string.replace(/\s/g,'');
	return string;
}
