// ********************************************************************************
// Function:	isEmpty()
// Arguments:	Field
// Usage:
// It returns FALSE if the Field is empty
// ********************************************************************************

function isEmpty(input)
{
	if ( trim(input.value) == "" )
	{
		input.focus();
		return true;
	}
	else
	{
		return false;
	}
}

// ********************************************************************************
// Function:	isInteger()
// Arguments:	Field
// Usage:
// It returns FALSE if the Field is not an integer
// ********************************************************************************

function isInteger(input)
{
	checkString = input.value;

    // LOOP THROUGH STRING CHARACTER BY CHARACTER
    for (i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i+1);

        // ENSURE CHARACTER IS A DIGIT
        if (!(ch >= "0" && ch <= "9")) {
        	input.focus();
            return false;
        }
    }
	return true;
}



// ********************************************************************************
// Function:	isEmail()
// Arguments:	Field
// Usage:
// It returns FALSE if the Field does not have a valid email format
// ********************************************************************************
function isEmail(input)
{
	var checkString = input.value;
	var newstr = "";
	var at = false;
	var dot = false;

	// DO SOME PRELIMINARY CHECKS ON THE DATA

	// IF EMAIL ADDRESS HAS A '@' CHARACTER
	if (checkString.indexOf("@") != -1) {
		at = true;

	// IF EMAIL ADDRESS HAS A '.' CHARACTER
	} else if (checkString.indexOf(".") != -1) {
		dot = true;
	}
	// PARSE REMAINDER OF STRING
	for (var i = 0; i < checkString.length; i++) {
		ch = checkString.substring(i, i + 1)
		if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
			|| (ch == "@") || (ch == ".") || (ch == "_")
			|| (ch == "-") || (ch >= "0" && ch <= "9")) {
			newstr += ch;
			if (ch == "@") {
				at=true;
			}
			if (ch == ".") {
				dot=true;
			}
		}
	}
	if ((at == true) && (dot == true)) {
		input.value = newstr;
		return true;
    }
    else {
      // DISPLAY ERROR MESSAGE
      alert ("Sorry, the email address you\nentered is not in the correct\nformat.");
      input.focus();
      return false;
    }
}

// ********************************************************************************
// Function:	toAlpha()
// Arguments:	String
// Usage:
// Remove all non-alpha characters in a string and return a modified string
// ********************************************************************************
function toAlpha(checkString)
{
	newString = "";    // REVISED/CORRECTED STRING
	count = 0;         // COUNTER FOR LOOPING THROUGH STRING

	// LOOP THROUGH STRING CHARACTER BY CHARACTER
    for (i = 0; i < checkString.length; i++) {
		ch = checkString.substring(i, i+1);

		// ENSURE CHARACTER IS AN ALPHA CHARACTER
		if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" )) {
			newString += ch;
		}
	}
	return newString;
}

// ********************************************************************************
// Function:	toNumber()
// Arguments:	String
// Usage:
// Remove all non-alpha characters, and return a pure numerical string
// ********************************************************************************
function toNumber(checkString)
{
	newString = "";    // REVISED/CORRECTED STRING
	count = 0;         // COUNTER FOR LOOPING THROUGH STRING

	// LOOP THROUGH STRING CHARACTER BY CHARACTER
	for (i = 0; i < checkString.length; i++) {
		ch = checkString.substring(i, i+1);

		// ENSURE CHARACTER IS AN ALPHA OR NUMERIC CHARACTER
		if (ch >= "0" && ch <= "9") {
			newString += ch;
		}
	}
	return newString;
}

// ********************************************************************************
// Function:	toAlphaNumber()
// Arguments:	String
// Usage:
// Remove all alpha and special characters, except Numeric Value, and return a modified string
// ********************************************************************************
function toAlphaNumber(checkString)
{
	newString = "";    // REVISED/CORRECTED STRING
	count = 0;         // COUNTER FOR LOOPING THROUGH STRING

	// LOOP THROUGH STRING CHARACTER BY CHARACTER
	for (i = 0; i < checkString.length; i++) {
		ch = checkString.substring(i, i+1);

		// ENSURE CHARACTER IS AN ALPHA OR NUMERIC CHARACTER
		if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") ||
			(ch >= "0" && ch <= "9")) {
			newString += ch;
		}
	}
	return newString;
}

// ********************************************************************************
// Function:	trim()
// Arguments:	String
// Usage:
// Remove all spaces in a string and return a modified string
// ********************************************************************************
function trim(checkString)
{
	newString = "";    // REVISED/CORRECTED STRING
	count = 0;         // COUNTER FOR LOOPING THROUGH STRING

	// LOOP THROUGH STRING CHARACTER BY CHARACTER
	for (i = 0; i < checkString.length; i++) {
		ch = checkString.substring(i, i+1);

		// ENSURE CHARACTER IS AN ALPHA CHARACTER
		if (ch != " ") {
			newString += ch;
		}
	}
	return newString;
}

// ********************************************************************************
// Function:	isNumeric()
// Arguments:	( var ) _value 
// Usage:
// Check if the argument is numeric value, integer of decimal value is acceptable.
// ********************************************************************************
function isNumeric( _value )
{
    var currchar = "", decimalcount = 0, filteredstring = "", i = 0
    
 	/* scan the string, char by char */
	for ( var i = 0 ; i < _value.length ; i++ ) 
	{		
		currchar = _value.substring( i, i + 1 )
		
        /* look for valid characters */
		if ( ( currchar >= "0" && currchar <= "9" ) || ( currchar == "." || currchar == "," || currchar == "-" ) ) 
	    {
		    if ( currchar != "," ) 
			{
				filteredstring += currchar ;
			}
			
			if ( currchar == "-" && i > 0 )  /* neg sign only allowed as 1st char */
			{
				return false ;
			}
			
			if( ( currchar == '.' ) && ( _value.length == 1 ) )
				return false ;
				
			if ( currchar == "." ) 
			{
				/* only one decimal point allowed */
           		decimalcount ++ ;
				if ( decimalcount > 1 )
				{
					return false ;
				}
			}
		}
     	else 
     	{
			return false ;
	   	}
	}
		
	return true ;
}

function toUpperCase( _field )
{
	_field.value = _field.value.toUpperCase() ; 
}

function toLowerCase( _field )
{
	_field.value = _field.value.toLowerCase() ; 
}