// contactUs.js
//
// 18 Feb 97 created Eric Krock
//
// (c) 1997 Netscape Communications Corporation
//
// Netscape functions: isEmpty, isWhiteSpace, warnEmpty, warnInvalid, checkString
// Original version of checkEmail available from Sandeep V. Tamhankar (stamhankar@hotmail.com)
// All other code (c) 2002 Differentum Ltd
//
// DIFFERENTUM LTD MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. DIFFERENTUM LTD SHALL NOT BE LIABLE FOR
// ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
// DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.

var iEmail = "Your email address is invalid"
var iEmailx = "You must enter only ONE email address"

var mName = "Please enter your Name";
var mCompany = "Please enter your Company Name";

// whitespace characters
var whitespace = " \t\n\r";

// 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;
}

// 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)
{
    alert(s)
    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 checkString (theField, s)
{
    if (isWhitespace(theField.value))
       return warnEmpty (theField, s);
    else return true;
}

function isSentence(theString, s)
{
   foundWord = 0;
   str = theString.value.split(" ");
   for (var i=0; i<str.length; i++)
   {
      if(!isEmpty(str[i]))
      {
         if (foundWord == 0)
         { // found first word
           foundWord = 1;
         } // else second word
         else return warnInvalid(theString,s);
      }
   }
   // if we have got this far then string is only one word
   return true;
}

function isSentence(theString, s)
{
   foundWord = 0;
   str = theString.value.split(" ");
   for (var i=0; i<str.length; i++)
   {
      if(!isEmpty(str[i]))
      {
         if (foundWord == 0)
         { // found first word
           foundWord = 1;
         } // else second word
         else return warnInvalid(theString,s);
      }
   }
   return true;
}

// Check email
function checkEmail (theField)
{   // Changes:  Sandeep V. Tamhankar (stamhankar@hotmail.com)
    var emailPat=/^(.+)@(.+)$/
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    var validChars="\[^\\s" + specialChars + "\]"
    var quotedUser="(\"[^\"]*\")"
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom=validChars + '+'
    var word="(" + atom + "|" + quotedUser + ")"
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
    var iEmail = "Email address seems incorrect"

    if (isWhitespace(theField.value))
       return warnEmpty (theField, "Please enter your email address");

    // left trim email address
    var emailStr = ""
    for(var i = 0 ; i < theField.value.length ; i++)
    {
      if (theField.value.substring(i,i+1) != ' ')
      {
        emailStr = emailStr + theField.value.substring(i,i+1)
      }
    }

    var matchArray=emailStr.match(emailPat)
    if (matchArray==null)
    {
	return warnInvalid(theField,iEmail);
    }
    var user=matchArray[1]
    var domain=matchArray[2]

    if (user.match(userPat)==null)
    {
	return warnInvalid(theField,iEmail);
    }

    var IPArray=domain.match(ipDomainPat)
    if (IPArray!=null)
    {
       // this is an IP address
       for (var i=1;i<=4;i++)
       {
          if (IPArray[i]>255)
          {
	     return warnInvalid(theField,iEmail);
          }
       }
       return true
    }

    var domainArray=domain.match(domainPat)
    if (domainArray==null)
    {
       return warnInvalid(theField,iEmail);
    }

    var atomPat=new RegExp(atom,"g")
    var domArr=domain.match(atomPat)
    var len=domArr.length
    if (domArr[domArr.length-1].length<2 ||
	domArr[domArr.length-1].length>3)
    {
       return warnInvalid(theField,iEmail);
    }

    // Make sure there's a host name preceding the domain.
    if (len<2)
    {
       return warnInvalid(theField,iEmail);
    }

    // If we've gotten this far, everything's valid!
    return true;
}

function trimString(str) {
    var emailStr = ""
    for(var i = 0 ; i < str.length ; i++)
    {
      if (str.substring(i,i+1) != ' ')
      {
        emailStr = emailStr + str.substring(i,i+1)
      }
    }
    return emailStr;
}

