// newUser.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) 2010 CHAMPAGNEFIZZ
//
// CHAMPAGNEFIZZ 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. CHAMPAGNEFIZZ 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 iEmailMisMatch = "Your email addresses do not match.  Please re-enter.";


// 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;
}

// Password checking scripts
function checkPassword(thePassword)
{
  if (thePassword.value == "" || thePassword.value.length < 6) return warnInvalid(thePassword,mPassword);
  else return true;
}

// Password authentication
function checkPassword(thePassword)
{
  if (thePassword.value == "") return warnInvalid(thePassword,mPassword);
  else if (thePassword.value.length < 6) return warnInvalid(thePassword,iPassword);
  else return true;
}
function checkPassword2(thePassword)
{
  if (thePassword.value == "") return warnInvalid(thePassword,mPassword2);
  else if (thePassword.value.length < 6) return warnInvalid(thePassword,iPassword);
  else return true;
}
function checkPasswordsMatch(password1,password2)
{
  if (password1.value != password2.value)
    return warnInvalid(password2,iMisMatch);
  else return true;
}

// Check email
function checkEmail (theField)
{   // Changes:  Sandeep V. Tamhankar (stamhankar@hotmail.com)

	theString="Please enter your email address";
	
    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, theString);

    // 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>4)
    {
       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;
}

function checkEmailsMatch(email1,email2)
{
  if (trimString(email1.value) != trimString(email2.value))
    return warnInvalid(email2,iEmailMisMatch);
  else return true;
}

function countIgnoringWhitespace(s)
{
   var value;
   var count = 0;
   for(var i = 0; i < s.length; i++)
   {
      // Check that current character isn't whitespace.
      var c = s.charAt(i);
      if (whitespace.indexOf(c) == -1)
      {
         count++;
      }
   }
   return count;
}

function checkReferrel(referrel, referrelText)
{
  if(referrel.value == 'Other')
  { // make sure something has been typed into the referrel box
    if(countIgnoringWhitespace(referrelText.value) < 3)
    {
      return warnInvalid(referrelText, "Please specify where you heard about us.");      
    }
    else
    { // referrel text is okay
    	referrel.value = referrelText.value;
    	return true;
    }
  }
  else return true;
}

function checkMarketingOK(theField)
{
  if(theField.checked)
  {
    msg = "** Warning **\n\nYou will only be entered for the iPod draw if you accept marketing material from us.\nPlease accept by clicking on 'OK' or reject marketing from us by clicking on 'Cancel'"

    if(confirm(msg))
    {
    	theField.checked = false;
    }
  }
  else return true;
}