// FormCheck.js

// BASIC DATA VALIDATION FUNCTIONS:
// isWhitespace (s)                    Check whether string s is empty or whitespace.
// isEmail (s [,eok])                  True if string s is a valid email address.


// FUNCTIONS TO PROMPT USER:
// prompt (s)                          Display prompt string s in status bar.
// promptEntry (s)                     Display data entry prompt string s in status bar.
// warnEmpty (theField, s)             Notify user that required field theField is empty.
// warnInvalid (theField, s)           Notify user that contents of field theField are invalid.


// FUNCTIONS TO INTERACTIVELY CHECK FIELD CONTENTS:
// checkString (theField, s [,eok])    Check that theField.value is not empty or all whitespace.
// checkEmail (theField [,eok])        Check that theField.value is a valid Email.


// VARIABLE DECLARATIONS
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// whitespace characters
var whitespace = " \t\n\r";
// decimal point character differs by language and culture
var decimalPointDelimiter = "."

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)
// m is an abbreviation for "missing"

var mPrefix = "You did not enter a value into the\n"
var mSuffix = " field. This is a required field. \nPlease enter it now."

var mPrefix2 = "You did not enter a value into the\n"
var mSuffix2 = " field. This should be a valid value. \nPlease enter it now."


// s is an abbreviation for "string"
var sEmail = "Email"

// i is an abbreviation for "invalid"
var iEmail = "This field must be a valid email address (like yourname@yourdomain.com.au). \n\n Please re-enter it now."

// p is an abbreviation for "prompt"
var pEntryPrompt = "Please enter a "
var pEmail = "valid email address (like yourname@yourdomain.com.au)."


var defaultEmptyOK = false



// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;
   
    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Returns true if string has a single quote on it 
function SingleQuoteExist(s)
{ 
	if (s.indexOf("'") != -1)
		{
      	return true;
    		}
	else
		{
		return false;
		}
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// Returns true if character c is a letter or digit.

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}


// isInteger (STRING s [, BOOLEAN emptyOK])
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return 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;
}

// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// isEmail (STRING s [, BOOLEAN emptyOK])
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */
// Display prompt string s in status bar.

function prompt (s)
{   window.status = s
}


// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s
}

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{  theField.focus();
    alert(mPrefix + s + mSuffix);
    return false;
}


// Notify user that field contains single quote
// Put focus in theField and return false.

function warnSingleQuoteExists(theField)
{   	theField.focus();
	theField.select();
	alert("Invalid Entry, a single quote ' characters  \nhas been entered.");
	return false;
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

function warnInvalid2 (theField, s)
{  theField.focus();
    alert(mPrefix2 + s + mSuffix2);
    return false;
}



/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkString(theField, s, emptyOK)
{	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.

 	if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    	if ((emptyOK == true) && (isWhitespace(theField.value))) return true;           // Inserted by dean 4-1-2000
	if (isWhitespace(theField.value))	
		{
      	return warnEmpty (theField, s);
		}
   	else
		{ 
		if (SingleQuoteExist(theField.value))
			{
			return warnSingleQuoteExists(theField, s);
			}
		else	
			{
			return true;
			}
		}
}
function checkString2(theField, s, emptyOK)
{	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.

 	if (checkString2.arguments.length == 2) emptyOK = defaultEmptyOK;
    	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    	if ((emptyOK == true) && (isWhitespace(theField.value))) return true;           // Inserted by dean 4-1-2000
	if (isWhitespace(theField.value))	
		{
      	return warnEmpty (theField, s);
		}
   	else
		{ 
		return true;
		}
}


function checkInteger(theField, s, emptyOK)
{	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.

 	if (checkInteger.arguments.length == 2) emptyOK = defaultEmptyOK;
    	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    	if ((emptyOK == true) && (isWhitespace(theField.value))) return true;           // Inserted by dean 4-1-2000
	if (isInteger(theField.value))	
		{
		return true;
		}
   	else
		{
      	return warnInvalid2(theField, s);
		}
}

function checkCurrency(theField, s, emptyOK)
{	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.

 	if (checkCurrency.arguments.length == 2) emptyOK = defaultEmptyOK;
    	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    	if ((emptyOK == true) && (isWhitespace(theField.value))) return true;           // Inserted by dean 4-1-2000
	if (isFloat(theField.value))	
		{
		return true;
		}
   	else
		{
      	return warnInvalid2(theField, s);
		}
}



/* FUNCTIONS TO INTERACTIVELY CHECK Form Selection FIELDS. */
// checkSelection(TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkSelection (theField, s)
{   	var myindex=theField.selectedIndex 
	var selectionvalue=theField.options[myindex].value;

	if (isWhitespace(selectionvalue))
		{	
       	return warnEmpty (theField, s);
		}
     else
		{
		 return true;
		}
}
// Validate radio information.

function checkRadioButton(radio, s)
{   var radiovalid = RadioButtonValueChecked(radio)

	if (radiovalid)
		{
		 return true;
		}
     else
		{
      	alert(s);
		return false;
		}
}
// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkEmail(theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}

function checkForSingleQuote(checkString)
{
   var newstr = "";
   var singlequote  = false;

    // DO SOME PRELIMINARY CHECKS ON THE DATA

    // IF String HAS  A SINGLE QUOTE  ' CHARACTER
    if (checkString.indexOf("'") != -1) {
      singlequote  = true;
    }

    // PARSE REMAINDER OF STRING
    for (var i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i + 1)
        if (ch != "'"){
           newstr += ch;
        }
    }
    if (singlequote == true) {
      // DISPLAY ERROR MESSAGE
      alert ("Invalid Entry, a single quote ' characters  \nhas been entered.");
      return newstr;
    }
    else {
      return newstr;
    }
}

function RadioButtonValueChecked(radio){   
     	var radiobutton = false;  	
	for (var i = 0; i < radio.length; i++)
    	{   
		if (radio[i].checked) { 
		radiobutton = true; 
		}
    	}
	return radiobutton
}


function validateFormInfo()
{   
    return (
      	checkString(document.register.elements["name"],"Name") &&
		checkString(document.register.elements["phone"],"Phone") &&
      	checkEmail(document.register.elements["email"]) 
      )
}

function cmdValidate()
{
if (validateFormInfo()) 
	{
	return true;
	}
else 
	{
	return false;
	 }
}
