/*
  -------------------------------------------------------------------------
	                Validation Script in JavaScript
                                Version 1.0
             JavaScript Code for Family Friendly Homes Website
                             Brisbane - AUSTRALIA
	
	http://www.familyfriendlyhomes.com.au/
    -------------------------------------------------------------------------  
*/
/************************************************************************************
*This file contains various validation functions to test text boxes from a form for
*correct input.
************************************************************************************/

//-----------------------------------variables---------------------------------------

//array of various white space characters
var whitespace = " \t\n\r";
//-----------------------------------functions--------------------------------------

/*************************************isEmpty()**************************************
*this tests for an empty string
*pre: the string to be tested
*post: Returns true if the given string is empty.
*************************************************************************************/
function isEmpty(s)
{
        return ((s == null) || (s.length == 0));
}

/*************************************isWhitespace()**********************************
*tests for whitespace characters in the string
*pre: the string to be tested.
*post: Returns true if the given string contains no text.
**************************************************************************************/
function isWhitespace(s) {
        var i;
        
        if (isEmpty(s))
                return true;
        
        for (i = 0; i < s.length; i++)
        {
                var c = s.charAt(i);
                if (whitespace.indexOf(c) == -1) 
                        return false;   
        }
        
        return true;
}
/*************************************isNum()**********************************
*tests for whitespace characters in the string
*pre: the string to be tested.
*post: Returns true if the given string contains no currency.
**************************************************************************************/
function isNum(s) {
	var valid="0123456789.";
	var ok=1; var checktemp;
	for (var i=0; i<s.length; i++)	{
		checktemp = ""+s.substring(i, i+1);
		if (valid.indexOf(checktemp) == "-1" )
			 return 0;
	}
        
        return 1;
}
/***************************************isNumeric()**********************************
*tests for a numeric string
*pre: the string to be tested.
*post:Returns True if the given string is a price.
*************************************************************************************/
function isNumeric(s)
{
        var i
                
        for (i = 0; i < s.length; i++)
        {
                if ( s.charAt(i) < "0" || s.charAt(i) > "9" )
                        return false;
        }
        
        return true;
}
/***************************************isInString()**********************************
*tests for a valid Invoice No string
*pre: the string to be tested.
*post:Returns True if the given string is a valid string.
*************************************************************************************/
function isInString(s)
{
   var valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	var ok=1; var checktemp;
	for (var i=0; i<s.length; i++)	{
		checktemp = ""+s.substring(i, i+1);
		if (valid.indexOf(checktemp) == "-1" )
			 return 0;
	}
        
        return 1;
}
/***************************************form_validator()**********************************
*tests for an inputed value
*pre: the string to be tested.
*post:Returns True if the inputted value is valid
*************************************************************************************/
function form_validator()
{
   //test Invoice Number field for white space
        if (isWhitespace(document.myform.mtid.value) ||
            !isInString(document.myform.mtid.value)){
        
                alert("Required Information! \nPlease enter your invoice number");
                
                document.myform.mtid.focus();
                document.myform.mtid.select();
        }
        else if (isWhitespace(document.myform.amt.value) ||
			!isNum(document.myform.amt.value)){
        
                alert("Required Information! \nPlease enter the amount of your invoice");
                
                document.myform.amt.focus();
                document.myform.amt.select();
        }
		else
        {
            //all data entered is ok			  
			return true;
        }
        return false;

}
//====================================================