// ==================================================================
// - - - - - - - - - - - - D I S C L A I M E R - - - - - - - - - - -
// By accepting this software ("Software"), you ("Customer") agree that
// use of the Software is subject to the following terms and
// conditions:
//
// (i)  the Software is supplied on a strict "AS IS" basis, without
//      warranty of any kind or nature.  DCJ CONSULTING DISCLAIMS ANY
//      AND ALL EXPRESS OR IMPLIED WARRANTIES RELATIVE TO THE SOFTWARE,
//      INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTY OF
//      MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, and
//
// (ii) in no event shall DCJ Consulting be liable to Customer for
//      any damages, whether direct, indirect, special, incidental,
//      consequential or punitive, which in any way arise out of or
//      relate to the Software, and
//
// (iii)DCJ Consulting shall not provide support of any kind for the
//      Software. DCJ Consulting retains title to all portions of the
//      Software and any copies thereof.
//
// Assistance with tailoring/customizing this code can be purchased on
// a time and materials basis from DCJ Consulting.
//
// (c) Copyright DCJ Consulting 2002. All rights reserved.
// ==================================================================
// Written: 2002/04/24 by D.C. Jones, per DCJ Consulting
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Modified: yyyy/mm/dd by ??
// Purpose: ??
// ==================================================================


//Name		:	calcTotal
//Programmer:	Dave Jones
//Company	:	DCJ Consulting
//Input		:	Form object upon which to operate
//Output	:	Boolean value indicating whether the function
//				succeeded in its task
//Purpose	:	To calculate total cost of merchandise purchased
//				by the customer and set a hidden form field to that
//				value for passing to LinkPoint
function calcTotal(form)
{
	var COST_CHRIS = 125.00;			//Cost of CHRIS subscription in US dollars
	var COST_OHU = 299.00;				//Cost of Optimal Health University subscription in USD
	var COST_OHU_AND_CHRIS = 369.00;	//Cost of Optimal Health University/CHRIS Combo subscription in USD

	var amtToCharge = 0;		//Total amount to charge customer

	for (var i = 0; i < form.product.length; i++)
	{
		if (form.product[i].checked)
		{
			//if customer ordered CHRIS, that is the total
			if (form.product[i].value == "OHU")
			{
				amtToCharge += COST_OHU;
				break;
			}
			//if customer ordered OHU, that is the total
			else if (form.product[i].value == "CHRIS")
			{
				amtToCharge = COST_CHRIS;
				break;
			}
			//if customer ordered OHU/CHRIS combo, that is the total (no other product can currently be ordered on-line)
			else if (form.product[i].value == "OHU_and_CHRIS")
			{
				amtToCharge = COST_OHU_AND_CHRIS;
				break;
			}	//end if
		}	//end if
	}	//end for

	//Dollar amount of merchandise ordered by customer
	//Assigned to hidden input field to pass to LinkPoint
	form.chargetotal.value = amtToCharge;

	return true;
}	//end calcTotal(form)


//Name		:	submitForm
//Programmer:	Dave Jones
//Company	:	DCJ Consulting
//Input		:	Form object upon which to operate
//Output	:	Boolean value indicating whether the function
//				succeeded in its task
//Purpose	:	Runs validation scripts prior to submitting form
//				to LinkPoint
function submitForm(form)
{
	if (formValidate(form))
	{
		if (formUpdate(form))
		{
			return true;
		}
		else
		{
			return false;
		}	//end if
	}
	else
	{
		return false;
	}	//end if
}	//end submitForm(form)


//Name		:	formUpdate
//Programmer:	Dave Jones
//Company	:	DCJ Consulting
//Input		:	Form object upon which to operate
//Output	:	Boolean value indicating whether the function
//				succeeded in its task
//Purpose	:	Sets hidden form fields required by LinkPoint based
//				on values set in the custom PreventiCare fields
function formUpdate(form)
{
	calcTotal(form);
	form.bname.value = form.name.value;
	form.baddr1.value = form.address1.value;
	form.baddr2.value = form.address2.value;
	form.bcity.value = form.city.value;
	form.bstate.value = form.state.value;
	form.bstate2.value = form.provence.value;
	form.bzip.value = form.zip.value;
	form.bcountry.value = form.country.value;
	form.phone.value = form.telephone.value;
	form.fax.value = form.facsimile.value;
	form.email.value = form.e_mail.value;

	return calcTotal(form);

}	//end formUpdate(form)


//Name		:	formValidate
//Programmer:	Dave Jones
//Company	:	DCJ Consulting
//Input		:	Form object upon which to operate
//Output	:	Boolean value indicating whether the function
//				succeeded in its task
//Purpose	:	Ensures that required values are entered prior to
//				submitting the form to LinkPoint
function formValidate(form)
{
	var prodChosen = false;
	var deliveryChecked = false;
	var prodChecked= "";
	var blankFaxAllowed = true;

	for (var i = 0; i < form.product.length; i++)
	{
		if (form.product[i].checked)
		{
			prodChosen = true;
			prodChecked = form.product[i].value;
			break;
		}	//end if
	}	//end for

	if ((prodChecked == "CHRIS") || (prodChecked == "OHU_and_CHRIS"))
	{
		for (var j = 0; j < form.Chris_Delivery.length; j++)
		{
			if (form.Chris_Delivery[j].checked)
			{
				deliveryChecked = true;
				if (form.Chris_Delivery[j].value == "Fax")
				{
					blankFaxAllowed = false;
				}
				else
				{
					blankFaxAllowed = true;
				}	//end if
			}	//end if
		}	//end for
	}
	else
	{
		deliveryChecked = true;
	}

	if (prodChosen)
	{
		if (deliveryChecked)
		{
			if (form.name.value != "")
			{
				if (form.clinic_name.value != "")
				{
					if (form.address1.value != "")
					{
						if (form.city.value != "")
						{
							if (form.country.value != "")
							{
								if (form.country.value == "US")
								{
									if (form.state.value != "")
									{
										if ((form.zip.value != "") && (isZIPCode(form.zip.value)))
										{
											if (checkUSPhone(form.telephone, false))
											{
												var faxValid = (checkUSPhone(form.facsimile, blankFaxAllowed))
												if (faxValid)
												{
													//do nothing
												}
												else
												{
													if (blankFaxAllowed)
													{
														alert("Please enter a valid fax number or leave blank.");
													}
													else
													{
														alert("Please enter a valid fax number.");
													}	//end if
													form.facsimile.focus();
													form.facsimile.select();
													return false;
												}	//end if valid fax
											}
											else
											{
												alert("Please enter a valid phone number.");
												form.telephone.focus();
												form.telephone.select();
												return false;
											}	//end if valid phone
										}
										else
										{
											alert("Please enter a valid zip code.");
											form.zip.focus();
											form.zip.select();
											return false;
										}	//end if zip code
									}
									else
									{
										alert("Please enter a valid state.");
										form.state.focus();
										return false;
									}	//end if state
								}
								else	//if international country
								{
									if (form.provence.value != "")
									{
										if (form.zip.value != "")
										{
											if (isInternationalPhoneNumber(stripCharsInBag(form.telephone.value, phoneNumberDelimiters)))
											{
												if (isInternationalPhoneNumber(stripCharsInBag(form.facsimile.value, phoneNumberDelimiters), true))
												{
													//do nothing
												}
												else
												{
													alert("Please enter a valid fax number or leave it blank.");
													form.facsimile.focus();
													form.facsimile.select();
													return false;
												}	//end if international fax
											}
											else
											{
												alert("Please enter a valid phone number.");
												form.telephone.focus();
												form.telephone.select();
												return false;
											}	//end if international phone
										}
										else
										{
											alert("Please enter a valid postal code or a dash (-) if you do not have one.");
											form.zip.focus();
											form.zip.select();
											return false;
										}	//end if zip code
									}
									else
									{
										alert("Please enter a valid provence.");
										form.provence.focus();
										form.provence.select();
										return false;
									}	//end if provence
								}	//end if country = "US" or international
								if (checkEmail(form.e_mail, false))
								{
									if (form.howheard.value != "")
									{
										return true;
									}
									else
									{
										alert("Please tell us how you heard about us.");
										form.howheard.focus();
										form.howheard.select();
										return false;
									}	//end if how heard
								}
								else
								{
									form.e_mail.focus();
									form.e_mail.select();
									return false;
								}	//end if e_mail
							}
							else
							{
								alert("Please enter your country.");
								form.country.focus()
								return false;
							}	//end if country not blank
						}
						else
						{
							alert("Please enter your city.");
							form.city.focus();
							form.city.select();
							return false;
						}	//end if city
					}
					else
					{
						alert("Please enter your street address.");
						form.address1.focus();
						form.address1.select();
						return false;
					}	//end if address
				}
				else
				{
					alert("Please enter your clinic's name.");
					form.clinic_name.focus();
					form.clinic_name.select();
					return false;
				}	//end if clinic
			}
			else
			{
				alert("Please enter your name.");
				form.name.focus();
				form.name.select();
				return false;
			}	//end if name
		}
		else
		{
			alert("Please choose a delivery method for CHRIS.");
			return false;
		}
	}
	else
	{
		alert("Please choose a product.");
		return false;
	}	//end if product
}	//end formValidate(form)