// Version 1.03
// Copyrights VirtualNerds.Com

// private functions
function IsIntPriv(theInt)
{
	return theInt==parseInt(RemoveLeadZeros(theInt));
}

function IsFloatPriv(theFloat)
{
	return theFloat==parseFloat(theFloat);
}

function RemoveLeadZeros(sString)
{
	while( sString.charAt(0)=="0" )
		sString = sString.substr( 1 );
	
	if( sString==""	)
		sString = "0";
	
	return sString;
}

// public functions
function IsInteger(theElem)
{
	if( theElem.value!="" )
		result = IsIntPriv(theElem.value);
	else
		result = true;

	// show error message
	if( !result )
	{
		alert("This field requires a whole number value!");
		theElem.focus();
	}
	
	return result;
}

function IsFloat(theElem, theDot)
{
	aStr = theElem.value;
	if( aStr!="" )
	{
		aStr = aStr.replace( theDot, "." );
		result = IsFloatPriv(aStr);
	}else
		result = true;
	
	// show error message
	if( !result )
	{
		alert("This field requires a decimal number value!");
		theElem.focus();
	}
	
	return result;
}

function IsRangeInteger(theElem, minValue, maxValue)
{
	result = true;
	aInt = theElem.value;

	if( aInt!="" )
	{
		if( !IsIntPriv(aInt) )
			result = false;
		else
		{
			tempInt = parseInt(RemoveLeadZeros(aInt))
			if ( !(( tempInt>=minValue )&&( tempInt<=maxValue )))
				result = false;
		}

		// show error message
		if( !result )
		{
			error = "This field requires an whole number value from " + minValue + " to " + maxValue + "!"
			alert(error);
			theElem.focus();
		}
	}
	
	return result;
}

function IsRangeFloat(theElem, theDot, minValue, maxValue)
{
	result = true;
	aFloat = theElem.value;

	if( aFloat!="" )
	{
		aFloat = aFloat.replace( theDot, "." );		
		if( !IsFloatPriv(aFloat) )
			result = false;
		else
		{
			tempFloat = parseFloat(aFloat);
			if ( !(( tempFloat>=minValue )&&( tempFloat<=maxValue )))
				result = false;
		}

		// show error message
		if( !result )
		{
			error = "This field requires a decimal number value from " + minValue + " to " + maxValue + "!"
			alert(error);
			theElem.focus();
		}
	}
	
	return result;
}

function IsDate(theElem)
{
	result = true;
	aStr = theElem.value;
	count = aStr.length;	

	if( count!=0 )
	{
		result = false;
		
		// get month
		firstInd = aStr.indexOf("/",0)
		if(firstInd!=-1)
		{
			if( IsIntPriv(aStr.substr(0,firstInd)) )
			{
				month = parseInt(RemoveLeadZeros(aStr.substr(0,firstInd)))-1;
		
				// get day and year
				secInd = aStr.indexOf("/",firstInd+1)
				if(secInd!=-1)
				{
					if( (IsIntPriv(aStr.substr(firstInd+1,secInd-firstInd-1))) && (IsIntPriv(aStr.substr(secInd+1,count-secInd-1))) )
					{
						day = parseInt(RemoveLeadZeros(aStr.substr(firstInd+1,secInd-firstInd-1)));
						year = parseInt(RemoveLeadZeros(aStr.substr(secInd+1,count-secInd-1)));

						// validate date
						mydate = new Date( year, month, day )
						if((day==mydate.getDate())&&(month==mydate.getMonth())&&(year==mydate.getFullYear())&&(year>1753))
							result = true;
					}
				}
			}
		}

		// show error message
		if( !result )
		{
			alert("This field requires a date (mm/dd/yyyy)!");
			theElem.focus();
		}
	}

	return result;
}

function IsEmail(theElem)
{
	result = true;
	aStr = theElem.value;
	count = aStr.length;
	
	if( count!=0 )
	{
		// get index of @
		inda = aStr.indexOf("@",0)
	
		// check for more @ and first/last @
		if( (aStr.indexOf("@",inda+1)!=-1)||(inda==0)||(inda==count-1))
			result = false;

		// check dot before @
		indd = aStr.indexOf(".",0)
		if(indd==inda-1)
			result = false;
		
		// get index of dot after @
		indd = aStr.indexOf(".",inda+1)
	
		// dot must be available. It can't be near and or @
		if( (indd==-1)||(indd==inda+1)||(indd==count-1))
			result = false;

		// show error message
		if( !result )
		{
			alert("This field requires a correct email address!");
			theElem.focus();
		}
	}

	return result;
}

function IsEmpty(theElem)
{
	myValue = theElem.value
	while( myValue.charAt(0)==" " )
		myValue = myValue.substr( 1 );
	if(myValue == "")
	{
		alert("This field is required!");
		theElem.focus();
		return true;
	}
	return false;
}

function IsTime(theElem)
{
	result = true;
	aStr = theElem.value;
	count = aStr.length;	

	if( count!=0 )
	{
		result = false;
		
		firstInd = aStr.indexOf(":",0)
		if(firstInd!=-1)
		{
			if( IsIntPriv(aStr.substr(0,firstInd)) )
			{
				s1 = parseInt(RemoveLeadZeros(aStr.substr(0,firstInd)));	
				secInd = aStr.indexOf(":",firstInd+1)
				if(secInd!=-1)
				{
					if( (IsIntPriv(aStr.substr(firstInd+1,secInd-firstInd-1))) && (IsIntPriv(aStr.substr(secInd+1,count-secInd-1))) )
					{
						s2 = parseInt(RemoveLeadZeros(aStr.substr(firstInd+1,secInd-firstInd-1)));
						s3 = parseInt(RemoveLeadZeros(aStr.substr(secInd+1,count-secInd-1)));
						
						if ((s1 < 13)&&(s1>=0)&&(s2<60)&&(s2>=0)&&(s3<60)&&(s3>=0))
							result = true;
					}
				}
			}
		}

		// show error message
		if( !result )
		{
			alert("This field requires a time (hh:mm:ss)!");
			theElem.focus();
		}
	}

	return result;
}
