/* $Change: 2021583 $ $DateTime: 2007/09/07 09:32:12 $ $Revision: #3 $ */


/* $Change: 2021583 $ $DateTime: 2007/09/07 09:32:12 $ $Revision: #3 $ */

/***************************************************************\
*								*
*	VALIDATESTATE						*
*								*
*	This function makes sure the state field is filled	*
*	out on a lead contact info form f for United States  *
*								*
*	The state field must be a single select named "State".	*
*								*
\***************************************************************/
function validateState(f)
{
   var stateReqd = false;
   var state = f.State.options[f.State.selectedIndex].value;

      if(f.Country.options[f.Country.selectedIndex].value == "United States" )
	{
	stateReqd = true;
	}
		
	if (	stateReqd &&
		(
 			state == "Choose One"
		)
	   )
	{	alert("The State field is required in " + f.Country.options[f.Country.selectedIndex].value) + ".";
		scrollTo(f.State);
		return false;
	}
	return true;

	
	
} // End validateState
/****************************************************************\
*                                                *
*    VALIDATEPROVINCE                                          *
* This function makes sure that Province field is filled if the country *
* is not United States.*
*                                             *
\*******************************************************************/
function validateProvince(f)
{
	var provinceReqdCountries = new Array(
		"Brazil",
		"Canada",
		"Japan",
		"Mexico",
		"United Kingdom",		
		"Venezuala",
		"Argentina",
		"Australia",
		"Bolivia",
		"China",
		"Colombia",
		"Costa Rica",
		"Dominican Republic",
		"Ecuador",
		"Paraguay",
		"Peru",
		"Uruguay"
	);
	var provinceReqd = false;
	for (var i=0; i<provinceReqdCountries.length;i++){
		if (f.Country.options[f.Country.selectedIndex].value
			== provinceReqdCountries[i]) {
			provinceReqd = true;
			break;
		}
	}
	if ( provinceReqd &&
		(
			f.province.value == "" ||
			f.province.value == null ||
			isblank(f.province.value) ||
			f.province.value == "undefined"
		)
	)
	{	alert("The Province field is required in " +
f.Country.options[f.Country.selectedIndex].value) + ".";
		scrollTo(f.province);
		return false;
	}
	return true;
	
} // End validateProvince

/***************************************************************\
*								*
*	VALIDATEPOSTALCODE					*
*								*
*	This function makes sure the postal code is filled	*
*	out on a lead contact info form f for certain 		*
*	countries.						*
*								*
*	The postal code field must be a text input named 	*
*	"PostalCode".						*
*								*
*	Put the list of countries with required postal code	*
*	into the postReqdCountries string array.  Make SURE that*
*	the string literals in the array match the choices from *
*	the select element verbatim.				*
*								*
\***************************************************************/
function validatePostalCode(f)
{
	var postReqdCountries = new Array (

		"Australia",
		"Austria",
		"Argentina",
		"Belgium",
		"Brazil",
		"Canada",
		"China",
		"Denmark",
		"Finland",
		"France",
		"Germany",
		"Israel",
		"Italy",
		"Japan",
		"Korea,Republic of",
		"Luxemburg",
		"Mexico",
		"Netherlands",
		"Norway",
		"Philippines",
		"Singapore",
		"Spain",
		"Sweden",
		"Switzerland",
		"United Kingdom",
		"United States"
	);

	var postReqd = false;
	for (var i=0; i < postReqdCountries.length; i++) {
		if (f.Country.options[f.Country.selectedIndex].value
			== postReqdCountries[i] ) {
			postReqd = true;
			break;
		}
	}
	if (	postReqd &&
		(
			f.PostalCode.value == "" ||
			f.PostalCode.value == null ||
			isblank(f.PostalCode.value) ||
			f.PostalCode.value == "undefined"
		)
	   )
	{	alert("Postal Code/Zip is required in " + f.Country.options[f.Country.selectedIndex].value);
		scrollTo(f.PostalCode);
		return false;
	}
	return true;

} // End validatePostalCode







/*******************************************************\
*							*
*	VALIDATEEMAIL					*
*							*
*	Make sure the e-mail address contains an @ 	*
*	character if it is filled out at all.		*
*							*
\*******************************************************/
function validateEmail(f)
{
	if (f.Email != null)
	{
		if(f.Email.value.length > 0 && f.Email.value.indexOf("@") == -1)
		{	
			alert("Please provide a valid e-mail address");
			scrollTo(f.Email);
			return false;
		}
	}
	return true;

} // End validateEmail


/***************************************************************\
*							        *
*  ISBLANK							*
*								*
*  This function checks a string to see if it is interesting.	*
*								*
\***************************************************************/
function isblank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if((c != ' ') && (c != '\n')) return false;
	}
	return true;

} // End isblank

/***********************************************\
*						*
*  SCROLLTO					*
*						*
*	This function performs a nice scroll	*
*	to the specified form element and	*
*	gives it the keyboard focus.		*
*						*
\***********************************************/
function scrollTo(e)
{
	window.scroll(0, 0);
	e.focus();

} // End scrollTo


/***************************************************************\	
*								*
*  VALIDATEHTMLFORM						*
*								*
*    	This function verifies that all required fields are	*
*	filled out on an HTML form.				*
*								*
*	This function should work with more or less all HTML	*
*	input forms.						*
*								*
*	This function considers an input elements on an HTML 	*
*	form to be REQUIRED unless you flag it as NON-required	*
*	as follows:						*
*								*
*	To make an input element NON-required, set a property	*
*	"optional" on the element's JavaScript object to 	*
*	boolean "true".						*
*								*
*	You'll also want to set a property "nicename" on the	*
*	object.  This should be what appears as the element's	*
*	"name" in the alert box if the field isn't filled out.	*
*	For example, an input element for someone's street	*
*	address might be named "s_addr", but you wouldn't want	*
*	the validation alert to say "Please fill out your	*
*	s_addr"!  If you define the nicename to "Street Address"*
*	you get "Please fill out your Street Address."		*
*								*
*	SINGLE-SELECT ELEMENTS are considered "not filled out" 	*
*	if the value "Choose One" is submitted.			*
*								*
*	MULTIPLE-SELECT ELEMENTS are "not filled out" if the 	*
*	"selectedIndex" property is -1 at submit time.		*
*								*
*	RADIO BUTTONS are a pain in the ass.  The DOM array	*
*	document.forms[0].radio-name can't have object values	*
*	assigned to it because it's not actually an object that	*
*	is physically on the document.  Only the radio button	*
*	input tags appear, so all the objects have to be	*
*	assigned to f.radio-name[0]; i.e., the zeroth element	*
*	of the radio group array, the first of the radio	*
*	buttons.  So you have to defined "optional" and		*
*	"handled" to the f.radio-name[0] button; for example,	*
*	f.radio-name[0].optional=true;				*
*								*
*	"OTHER" FIELDS -- This function supports the checking	*
*	of "Other" fields for single- and multiple-selects	*
*	and radio button groups.				*
*								*
*	To add an "Other" field, just create a text input	*
*	element with a name equal to the select's name but	*
*	prefixed by the string "Other".  For example, for the	*
*	single-select named "Country", you would make a text	*
*	input named "OtherCountry".				*
*								*
*	On each "Other" text input element, make sure to define	*
*	and set the property "optional" to boolean "true" and 	*
*	"nicename" to some alert string as described above.	*
*	It may seem weird to give a "nicename" to such a 	*
*	"nonrequired" form element, but this function REQUIRES	*
*	the "Other" input text element to have a value if the 	*
*	choice "Other" is chosen in the select, even if the	*
*	select element itself is not required.			*
*								*
\***************************************************************/
function validateHTMLForm (f)
{
	// globals
	var radio_group;

	// Check for empty, non-optional fields and alert.
	for(var i = 0; i < f.elements.length; i++)
	{
		var e = f.elements[i];
		othername = "Other" + e.name;
		
		
		// If it's a text thing, make sure it's filled out.
		if (e.type == "text" ||	e.type == "textarea" || e.type =="password")
		{
			if (	!e.optional &&
				(
					(e.value == null) ||
					e.value == "" ||
					isblank(e.value) ||
					e.value == "undefined"
				)
			   )
		
			{	alert("Please provide your " + e.nicename);
				scrollTo(e);
				return false;
			}

		} // End text inputs
		
		// If it's a single select...
		if (e.type == "select-one")
		{

			// If the field is required, make sure something is chosen.
			if  (!e.optional && e.options[e.selectedIndex].value == "Choose One")
			{	alert("Please choose an option for " + e.nicename);
				scrollTo(e);
				return false;
			}

			// If "Other" is chosen, make sure it's filled out.
			if (f[othername] != null)
			{
				if (	e.options[e.selectedIndex].value == "Other" &&
					(	f[othername].value == "" ||
						f[othername].value == null ||
						f[othername].value == "undefined" ||
						isblank(f[othername].value)
					)
				   )
				{	alert('Please enter your "Other" choice for ' + e.nicename);
					scrollTo(f[othername]);
					return false;
				}
			}

		} // End select-one
		

		if (e.type == "select-multiple")
		{
			// Determine if the select has anything at all selected.
			var isSomethingSelected = false;
			for (var k=0; k < e.options.length; k++)
			{
				if (e.options[k].selected) { isSomethingSelected = true; }
			}

			// Determine if the "Other" option is selected.
			var isOtherSelected = false;
			for (var l=0; l < e.options.length; l++)
			{
				if (	e.options[l].selected && 
					e.options[l].value == "Other") { isOtherSelected = true; }
			}


			// If the select element is required, something has to be chosen!
			if (!e.optional && !isSomethingSelected)
			{	alert("Please select at least one option for " + e.nicename);
				scrollTo(e);
				return false;
			}
			
			// If "Other" is selected, make sure it's filled out.
			if (f[othername] != null)
			{
				if (	isOtherSelected &&
					(	f[othername].value == "" ||
						f[othername].value == null ||
						f[othername].value == "undefined" ||
						isblank(f[othername].value)
					)
				   )
				{	alert('Please enter your "Other" choice for ' + e.nicename);
					scrollTo(f[othername]);
					return false;
				}
			}
		
		} // End select-multiple

		if (e.type == "radio")
		{	
			var isChecked = false;
			radio_group = f[e.name];
		
			if (!radio_group[0].handled && !radio_group[0].optional)
			{	for (var j=0; j < radio_group.length; j++)
					if (radio_group[j].checked)
					{	isChecked = true;
						break;
					}

				if (!isChecked)
				{	alert('Please select an option for ' + radio_group[0].nicename);
					scrollTo(e);
					return false;
				}
					
				if (	isChecked
					&& radio_group[j].value == "Other"
					&& ( 	f[othername].value == "" ||
						f[othername].value == null ||
						f[othername].value == "undefined" ||
						isblank(f[othername].value)
					   )
				   )
				{	alert('Please enter your "Other" choice for ' + radio_group[0].nicename);
					scrollTo(f[othername]);
					return false;
				}
					
			}

			radio_group[0].handled = true;

		} // End radio
					
	} // end looping through form elements.

	// If we got through the loop without returning, we're OK!
	return true;

} // End validateHTMLForm


/*******************************************************\
*							*
*	PHONEOREMAIL					*
*							*
*	This is a very specific function, designed	*
*	for the lead info form in the guestbook.  It	*
*	can of course be used in other places.		*
*							*
\*******************************************************/
function phoneOrEmail (f)
{
	if (	(
			f.Email.value == "" ||
		  	f.Email.value == null ||
		  	f.Email.value == "undefined" ||
		  	isblank(f.Email.value)
		)

		&&

		(
			f.Phone.value == "" ||
			f.Phone.value == null ||
			f.Phone.value == "undefined" ||
			isblank(f.Phone.value)
		)
	)
	{
		alert("Please fill out either your phone number or e-mail address.");
		scrollTo(f.Phone);
		return false;
	}
	return true;

} // End phoneOrEmail


/*******************************************************\
*							*
*	REQUIRECONTACTPREFERENCE			*
*							*
*	This function is again specific to the lead	*
*	info form.  It requires the user to fill out	*
*	the field corresponding to whatever they	*
*	specify for their contact preference.		*
*							*
\*******************************************************/
function requireContactPreference(f)
{
	var contact = "";
	var i;
	for (i = 0; i < f.ContactPreference.length; i++) {
		if (f.ContactPreference[i].checked == true) {
			contact = f.ContactPreference[i].value;
			break;
		}
	}
	if (
		contact == "E-mail" &&
		(
			f.Email.value == "" ||
		  	f.Email.value == null ||
		  	f.Email.value == "undefined" ||
		  	isblank(f.Email.value)
		)
	)
	{
		alert("You specified e-mail as your contact preference.  Please fill out the e-mail field.");
		scrollTo(f.Email);
		return false;
	} else if (
			contact == "Phone" &&
			(
				f.Phone.value == "" ||
				f.Phone.value == null ||
				f.Phone.value == "undefined" ||
				isblank(f.Phone.value)
			)
		)
	{
		alert("You specified phone as your contact preference.  Please fill out the phone number field.");
		scrollTo(f.Phone);
		return false;
	}
	return true;

} // End requireContactPreference


/*******************************************************\
*							*
*	VALIDATELEADINFO				*
*							*
*	This function does additional form validation	*
*	on specific fields for lead contact forms.	*
*							*
\*******************************************************/
function validateLeadInfo(f)
{
	if (!validateEmail(f)) return false;
     if (!validateState(f)) return false;
	if (!validateProvince(f)) return false;
      if (!validatePostalCode(f)) return false;
	
	

	// Otherwise, OK.
	return true;

} // End validateLeadInfo

/*******************************************************\
*							*
*	VALIDATELEADEXCCONT				*
*							*
*	This function was built for Notes where one	*
*	doesn't need contact preferences.		*
*							*
\*******************************************************/
function validateLeadExcContact(f)
{
	if (!validateEmail(f)) return false;
	if (!validateState(f)) return false;
	if (!validateProvince(f)) return false;
	if (!validatePostalCode(f)) return false;
	if (!phoneOrEmail(f)) return false;

	// Otherwise, OK.
	return true;

}

/**********************************************************\
*    Checks to see what browser the customer is using     *
\**********************************************************/
function isBrowserAtLeast(b,v)
		{
		/*
		** Check if the current browser is compatible
		**  b  browser name
		**  v  version number (if 0 don't check version)
		** returns true if browser is at least version number
		*/
		browserOk = false;
		versionOk = false;
		browserOk = (navigator.appName.indexOf(b) != -1);
		if (v == 0) versionOk = true;
			else  versionOk = (v <= parseInt(navigator.appVersion));
		return browserOk && versionOk;
		}

