/******************************************************************************

          ----------------------------------------------------------
          |  Copyright  (C) 2005-2005  Vorum Research Corporation  |
          |                  All Rights Reserved                   |
          ----------------------------------------------------------


-------------------------------------------------------------------------------

	CLASS/MODULE NAME : formvalidate.js

	DESCRIPTION       : Javascript functions for basic form validation.
                        This module should have no language-dependent strings in it.

	SEE ALSO          :

	REVISION HISTORY  :
-------------------------------------------------------------------------------
	Ver     Date        By  Description
	~~~     ~~~~        ~~  ~~~~~~~~~~~
    1.1     2005.Apr.03 DAC Initial revision.
    
    1.2     2005.Jun.10 DAC Task #82 - Language dependent strings are now 
                            variables sMsgMissingName, sMsgInvalidEmail, and
                            sMsgMissingCompany.  These must be defined in the 
                            PHP form that is calling the functions.
                                       
    1.3     2005.Aug.11 DAC Task #114 - Email input field renamed to email to 
                            be compatible with the 3rd party formmail.php.
                            
******************************************************************************/

/****************************************************************/
// function ValidateEmail(sEmail)
//
// References:
// http://www.regexlib.com/REDetails.aspx?regexp_id=711
// http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=22
// Email protocol reference, http://www.w3.org/Protocols/rfc822/
function ValidateEmail(sEmail)
{
	// Other regular expressions considered were:
	// 1. http://www.qiksearch.com/javascripts/email-validator.htm
	//    var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	// 2. http://www.4guysfromrolla.com/webtech/052899-1.shtml
	//    var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	// However the following yielded the best results.
	var regex = new RegExp("^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+\.[a-zA-Z]{2,4}$");
	return regex.test(sEmail);
}

/****************************************************************/
// function IsEmpty(s)
// Check whether string s is empty.
// http://www.4guysfromrolla.com/webtech/091998-1.shtml
function IsEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

/****************************************************************/
function IsWhitespace(s)
{
	// Is s empty?
	if (IsEmpty(s)) return true;
	
	var i;
	// whitespace characters
	var whitespace = " \t\n\r";

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

// create the prototype on the String object
// http://www.js-x.com
// Author: Brock Weaver
String.prototype.trim = function()
{
	// skip leading and trailing whitespace
	// and return everything in between
	var x=this;
	x=x.replace(/^\s*(.*)/, "$1");
	x=x.replace(/(.*?)\s*$/, "$1");
	return x;
}

function MoveStr(oSrc, oDst)
{
	oDst.value = oSrc.value;
	oSrc.value = "";
}

function CleanupAddress()
{
	// Clean up fields, removing whitespace
	var sValue = new String();
	sValue = document.Inquiry.Address1.value;
	document.Inquiry.Address1.value = sValue.trim();
	sValue = document.Inquiry.Address2.value;
	document.Inquiry.Address2.value = sValue.trim();
	sValue = document.Inquiry.Address3.value;
	document.Inquiry.Address3.value = sValue.trim();
	sValue = document.Inquiry.Address4.value;
	document.Inquiry.Address4.value = sValue.trim();

	// Shift address fields up
	if (IsWhitespace(document.Inquiry.Address1.value))
	{
		// If first line missing but second line has data, swap
		if (!IsWhitespace(document.Inquiry.Address2.value))
			MoveStr(document.Inquiry.Address2, document.Inquiry.Address1);
		// If first line missing but third line has data, swap
		else if (!IsWhitespace(document.Inquiry.Address3.value))
			MoveStr(document.Inquiry.Address3, document.Inquiry.Address1);
		else if (!IsWhitespace(document.Inquiry.Address4.value))
			MoveStr(document.Inquiry.Address4, document.Inquiry.Address1);
	}
	if (IsWhitespace(document.Inquiry.Address2.value))
	{
		// If first line missing but second line has data, swap
		if (!IsWhitespace(document.Inquiry.Address3.value))
			MoveStr(document.Inquiry.Address3, document.Inquiry.Address2);
		// If first line missing but third line has data, swap
		else if (!IsWhitespace(document.Inquiry.Address4.value))
			MoveStr(document.Inquiry.Address4, document.Inquiry.Address2);
	}
	if (IsWhitespace(document.Inquiry.Address3.value))
	{
		// If first line missing but second line has data, swap
		if (!IsWhitespace(document.Inquiry.Address4.value))
			MoveStr(document.Inquiry.Address4, document.Inquiry.Address3);
	}
}


// ValidateCustomer
// A customer inquiry form must supply one of the following:
// - Name, Address, Country, Telephone, Message
// - Name, Email, Message
//
// The following global variables must be defined prior to inclusion of this javascript for
// ValidateCustomer() to function properly.  Examples are given.
//	var sMsgInquiry = new String("Please enter the required information:");
//	var sMsgMissingName = new String("\n- Your name is required."); 
//	var sMsgInvalidEmail = new String("\n- The email address is invalid."); 
function ValidateCustomer()
{
	var FormInquiry = document.Inquiry;
	var sValue = new String();
	
	// Name is always required
	sValue = FormInquiry.Name.value;
	FormInquiry.Name.value = sValue.trim();
	if (IsWhitespace(FormInquiry.Name.value))
	{
		alert(sMsgInquiry + sMsgMissingName);
		FormInquiry.Name.focus();
		return false;
	}
	
	CleanupAddress();
	
	// If an email address has been provided, validate it
	sValue = FormInquiry.email.value;
	FormInquiry.email.value = sValue.trim();
	if (!IsWhitespace(FormInquiry.email.value))
	{
		if (!ValidateEmail(FormInquiry.email.value))
		{
			alert(sMsgInquiry + sMsgInvalidEmail);
			FormInquiry.email.focus();
			return false;
		}
	}
	
	sValue = FormInquiry.Telephone.value;
	FormInquiry.Telephone.value = sValue.trim();
	
	// Must have either (Address, Country, Telephone) or (Email).
	if (!((!IsWhitespace(FormInquiry.Address1.value) &&
		 !IsWhitespace(FormInquiry.Country.value) &&
		 !IsWhitespace(FormInquiry.Telephone.value)) ||
		 !IsWhitespace(FormInquiry.email.value)))
	{
		// Set the focus to the first field missing information.
		if (IsWhitespace(FormInquiry.Address1.value))
			FormInquiry.Address1.focus();
		else if (IsWhitespace(FormInquiry.Country.value))
			FormInquiry.Country.focus();
		else if (IsWhitespace(FormInquiry.Telephone.value))
			FormInquiry.Telephone.focus();
		else if (IsWhitespace(FormInquiry.email.value))
			FormInquiry.email.focus();
		alert(sMsgInquiry);
		return false;
	}

	// Must have a message	 	
	sValue = FormInquiry.Message.value;
	FormInquiry.Message.value = sValue.trim();
	if (IsWhitespace(FormInquiry.Message.value))
	{
		alert(sMsgInquiry);
		FormInquiry.Message.focus();
		return false;
	}

     return true;
}

// ValidateDistributor
// A distributor inquiry form must the company name in addition to all of the 
// information required in a customer inquiry.
// - Name, Address, Country, Telephone, Message
// - Name, Email, Message
//
// In addition to the 3 global strings that must be defined for ValidateCustomer(),
// the following variable is also required for ValidateDistributor.
//	var sMsgMissingCompany = new String("\n- Your company name is required."); 
function ValidateDistributor()
{
	var FormInquiry = document.Inquiry;
	var sValue = new String();
	
	// Name is always required
	sValue = FormInquiry.Company.value;
	FormInquiry.Company.value = sValue.trim();
	if (IsWhitespace(FormInquiry.Company.value))
	{
		alert(sMsgInquiry + sMsgMissingCompany);
		FormInquiry.Company.focus();
		return false;
	}
	
	return ValidateCustomer();
}
