// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";
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;
}
/****************************************************************/
/****************************************************************/
// Changes color of text and focuses to alert for an error
// To work all text to be changed must be in an
// identified div block
function changeTextColor(name, color)
{
	var texttochange = document.getElementById(name);
	var colorid;
	if(color == 'red')
	{
		colorid = '#FF0000';
	}
	else if(color == 'black')
	{
		colorid = '#000000';
	}
	texttochange.style.color = colorid;	
}
/****************************************************************/
/****************************************************************/
// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
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;
}
/****************************************************************/
/****************************************************************/

/* 
	Remove single quotes
*/
function sqlSafe (s)
{
	var new_s = s;
	var regex1 = /'/g;
	new_s = new_s.replace(regex1, "");
	return new_s;
}
/****************************************************************/
/****************************************************************/

/* 
	Remove whitespace quotes
*/
function removeWhitespace (s)
{
	var new_s = s;
	var regex1 = /\s/g;
	new_s = new_s.replace(regex1, "");
	return new_s;
}
/****************************************************************/
/*****************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
 */
function  isDecimal( strValue ) {



  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}
/******************************************************************************/
function ValidateData() {
   var CanSubmit = false;

   // Check to make sure that the name field is not empty.
   CanSubmit = ForceEntry(document.forms[0].info1,"You must supply your Name.");
	   //alert("checked name OK");
   //check Company Name
   if (CanSubmit == true){
   		CanSubmit = ForceEntry(document.forms[0].info3,"You must supply a Company Name.");
   }
   //alert("checked company OK");
   //check the phone number
   if (CanSubmit == true){
	   	//var tstr = document.forms[0].info10.value;
   		CanSubmit = validatePhone(document.forms[0].info10);
   }
   //alert("checked phone OK");
   //check for valid email
   if (CanSubmit == true){
	   	CanSubmit = checkemail();
   }
   //alert("checked email OK");  

   return CanSubmit;
}
/******************************************************************************/
function ForceEntry(val, str) {
   var strInput = new String(val.value);

   if (isWhitespace(strInput)) {
	    val.style.background = 'Yellow';
		alert(str);
		return false;
   } else
   		val.style.background = 'White';
		return true;

}
/******************************************************************************/

function checkemail(){
var testresults;
var obj=document.forms[0].info13;
var str=document.forms[0].info13.value;
var filter= new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
if (filter.test(str)){
testresults=true;
obj.style.background = 'White';
}
else{
alert("Please input a valid Email Address.");
obj.style.background = 'Yellow';
testresults=false;
}
return (testresults);
}
/******************************************************************************/
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        alert("You didn't enter a phone number.");
        fld.style.background = 'Yellow';
		return false;
    } else if (isNaN(parseInt(stripped))) {
        alert("The phone number contains illegal characters.");
        fld.style.background = 'Yellow';
		return false;
    } else if (!(stripped.length == 10)) {
        alert("The phone number is the wrong length. Make sure you included an area code.");
        fld.style.background = 'Yellow';
		return false;
    }
	fld.style.background = 'White';
	return true;
}

