

//  $Id: ctype.js,v 1.4 2002/02/20 18:42:46 nwiggins Exp $
//  $Log: ctype.js,v $
//  Revision 1.4  2002/02/20 18:42:46  nwiggins
//
//  # removes script tags - forces usage as a javascript src include. no php includes
//
//  Revision 1.3  2001/07/18 23:07:57  jneil
//  Fixed some problems.
//
//  Revision 1.1  2001/05/21 15:53:53  jneil
//  Initial Upload.
//
//

/**********************************************************************
Depends on: isEmpty(), isWhitespace() functions string.js library 

NOTE: 
  -- always alltrim field value before calling any of these functions
  -- if field is empty, the function call will return true
**********************************************************************/

// isDigit: check if val has digits (0-9)
function isDigit(val)
{
	var strBuffer = new String(val);
	var nPos = 0;

	if (isEmpty(strBuffer))
		return false;

	for (nPos = 0; nPos < strBuffer.length; nPos++)
		if (strBuffer.charAt(nPos) < '0' || strBuffer.charAt(nPos) > '9')
			return false;

	return true;
}

// isAlpha: check if val has alphabets only (a-z, A-Z)
function isAlpha(val)
{
	var strBuffer = new String(val);
	var nPos = 0;

	if (isEmpty(strBuffer))
		return false;

	for (nPos = 0; nPos < strBuffer.length; nPos++)
		if (!((strBuffer.charAt(nPos) >= 'a' && strBuffer.charAt(nPos) <= 'z') ||
			(strBuffer.charAt(nPos) >= 'A' && strBuffer.charAt(nPos) <= 'Z')))
			return false;

	return true;
}

// isInteger: check if nVal is integer type
function isInteger(nVal)
{
	var strBuffer = new String(nVal);
	var nPos = 0, nStart = 0;

	if (isEmpty(strBuffer))
		return true;
	if (isWhitespace(strBuffer))
		return false;

	// check if -ve or +ve sign occurs in the beginning
	if ((strBuffer.charAt(0) == '-') || (strBuffer.charAt(0) == '+'))
		nStart = 1;
	else
		nStart = 0;

	for (nPos = nStart; nPos < strBuffer.length; nPos++)
		if (strBuffer.charAt(nPos) < '0' || strBuffer.charAt(nPos) > '9')
			return false;

	return true;	
}

// isFloat: check if fVal is floating-point type
//	LIMITATION: no scientific notation (for eg: xxxx.xxE+xx)
function isFloat(fVal)
{
	var strBuffer = new String(fVal);
	var nPos = 0, nStart = 0;

	if (isEmpty(strBuffer))
		return true;
	if (isWhitespace(strBuffer))
		return false;

	// check if -ve or +ve sign occurs in the beginning
	if ((strBuffer.charAt(0) == '-') || (strBuffer.charAt(0) == '+'))
		nStart = 1;
	else
		nStart = 0;

	for (nPos = nStart; nPos < strBuffer.length; nPos++)
		if ((strBuffer.charAt(nPos) < '0' || strBuffer.charAt(nPos) > '9') && 
				(strBuffer.charAt(nPos) != '.'))
			return false;

	return true;
}

/************************************************************
isDate	: Check if dVal is valid date
	: valid date format - 
		MM (or) M / DD (or) D / Y (or) YY (or) YYYY
	: YY > 50 should be 19YY
	: YY <= 50 should be 20YY
*************************************************************/
function isDate(dVal)
{
	var strBuffer= new String(dVal);
	var cDelimiter='';
	var strMonth=0, strDay=0, strYear=0;
	var nPos=-1;

	if (isEmpty(strBuffer))
		return true;
	if (isWhitespace(strBuffer))
		return false;

	// Get the delimiter used
	if (occurs('/', strBuffer) == 2)
		cDelimiter = '/';
	else if (occurs('-', strBuffer) == 2)
		cDelimiter = '-';

	// If no '/' or '-' found return false
	if (cDelimiter == '')
		return false;

	// validate month, date, and year (Y, YY, YYYY are valid year formats)
	nPos = strBuffer.indexOf(cDelimiter);
	strMonth = strBuffer.substring(0, nPos);
	if (strMonth.length > 2 || !isDigit(strMonth))
		return false;
	strBuffer = strBuffer.substring(nPos+1);
	nPos = strBuffer.indexOf(cDelimiter);
	strDay = strBuffer.substring(0, nPos);
	if (strDay.length > 2 || !isDigit(strDay))
		return false;
	strBuffer = strBuffer.substring(nPos+1);
	strYear = strBuffer;
	if ((strYear.length > 4) || (strYear.length == 3) || !isDigit(strYear))
		return false;

	// if YY < 50 then YYYY=20YY, else if YY >= 50 then YYYY=19YY
	var iYear = parseInt(strYear);
	if (iYear < 50)
		strYear = "20" + (strYear < 10 ? '0' + strYear:strYear);
	else if (iYear >= 50 && iYear < 100)
		strYear = "19" + strYear;

	strBuffer = strMonth + cDelimiter + strDay + cDelimiter + strYear;

	// validate date
	var dBuffer = new Date(strBuffer);
	if (dBuffer.getDate() != parseInt(strDay) || 
				dBuffer.getMonth()+1 != parseInt(strMonth) || 
				dBuffer.getFullYear() != parseInt(strYear))
		return false;

	return true;
}

// isZipcode: check if valid US zip code (##### or #####-####)
function isZipcode(strZip)
{
	var strLeft="", strRight="", strVal = new String(strZip);

	if (isEmpty(strVal))
		return true;
	if (isWhitespace(strVal))
		return false;

	if (strVal.length != 5 && strVal.length != 10)
		return false;

	if ((strVal.length == 5) && isDigit(strVal))
		return true;

	if ((strVal.length == 10) && isDigit(strVal.substring(0, 5)) && 
						isDigit(strVal.substring(6)))
		return true;

	return false;
}

// isDatePart: check if nVal is valid day: 1-31, month:1-12, year:YYYY
function isDatePart(nVal, strType)
{
	var strBuffer = new String(nVal);

	if (isEmpty(strBuffer) || isWhitespace(strBuffer))
		return false;
	
	nVal = parseInt(strBuffer);
	if (!isDigit(nVal))
		return false;

	if ((strType == "Year") && (nVal < 0 || (nVal > 99 && nVal < 1000) || nVal > 9999))
		return false;
	else if ((strType == "Month") && (nVal < 1 || nVal > 12))
		return false;
	else if ((strType == "Day") && (nVal < 1 || nVal > 31))
		return false;

	return true;
}

