// 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."

// s is an abbreviation for "string"
var sEmail = "Email"
var sOlympus = "Olympus"

// i is an abbreviation for "invalid"
var iEmail = "This field must be a valid email address (like foo@bar.com). \n\n Please enter one now."
var iNumber = "The post code field should be numeric (like 2060). \n\n Please reenter it now."
var iOlympus = "Wrong! Check ya camera buddy. \n\n Please enter one now."



// p is an abbreviation for "prompt"
var pEntryPrompt = "Please enter a "
var pEmail = "valid email address (like foo@bar.com)."
var pEmail = "valid serial number like (365sfsfsdsdf)."


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;
		}
}

// Returns true if string has an ampersand (&) on it 
function AmpersandExist(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;
}


// isEmail (STRING s [, BOOLEAN emptyOK])
function isEmail2 (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;
}

function isEmail(s)
{ 
    var arr;
  
   //var temp = "^([0-9a-zA-Z]([_\-.\w]*[0-9a-zA-Z_])*@(([0-9a-zA-Z_])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$"
   var temp = "^[a-zA-Z]+[a-zA-Z0-9\._\'\`~\+-]*@([a-zA-Z0-9_-]+){1}(\.[a-zA-Z0-9]+){1,2}"
   var emailfilter = new RegExp(temp,"ig");
      
    returnval =emailfilter.test(s)
   
   if (returnval==false)
        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;
}

function warnAmpersandExists(theField)
{   theField.focus();
	theField.select();
	alert("Invalid Entry, an ampersand '&' 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
}


/* 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
		{ 
		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
		{ 
		if (SingleQuoteExist(theField.value))
			{
			return warnSingleQuoteExists(theField, s);
			}
		else	
			{
			return true;
			}
		}
}

function checkString3(theField, s, emptyOK)
{	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.

 	if (checkString3.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	
			{

			if (AmpersandExist(theField.value))
				{
				return warnAmpersandExists(theField, s);
				}
			else	
				{
				return true;
				}
			}
		}
}

/* 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;
		}
}


function checkCheckboxButton(checkbox, s)
{   
var checkboxvalid = CheckboxValueChecked(checkbox)

	if (checkboxvalid)
		{
		 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 checkInteger(theField, emptyOK)
{   if (checkInteger.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isInteger(theField.value)) 
       return warnInvalid (theField, iNumber);
    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; 
    if (radio.length > 1){
        for (var i = 0; i < radio.length; i++)
    	    {   
		    if (radio[i].checked) { 
		    radiobutton = true; 
		    }
    	    }
 
    }
    else {	
	  if (radio.checked) { 
            radiobutton = true; 
        }
   }
   
	return radiobutton
}

function CheckboxValueChecked(checkbox){   
     	var checkboxbutton = false;  	
	for (var i = 0; i < checkbox.length; i++)
    	{   
		if (checkbox[i].checked) { 
		checkboxbutton = true; 
		}
    	}
	return checkboxbutton
}

function check2PasswordEntry(theField1,theField2)
{
if (theField1.value != theField2.value)
   {alert ("Password is not the same.\n Please reenter Password twice!");
    //theField1.value = ""
    //theField2.value = ""
    theField1.focus();
    return false;}
else
   return true;
} 