// ********************************************************************************************
// Begin : Validate Step 1 form
// ********************************************************************************************

// these are global

// click_counter keeps track of how many times this script is called
// focus will be set on the first item each time
var click_counter = 0;
// the_focus_item is a global, and will be set only on the first item found
var b_first_focus = false;// set the focus only on the first item found
var focus_click = 0;//we start with both click_counter and focus_click set to 0
var the_focus_item;
var error_start = "The Fields marked with a * are required\nPlease complete the following items: \n\n";
var error_close = "Thank You.";

function validateStep1(theForm, count)
{
	/* 
	You need dump this to make the server side purpose[] checkbox validation method work
	javascript doesn't recognize the purpose[] syntax, although php does.
	Currently the site uses the standard purpose, no []
	*/
	var countChecked = 0;
	var error_message = '';

	// iterate through all the elements in the checkbox array

	// this step is necessary to deal with the [] name field
	var options = theForm.elements['purpose[]'];
	
	for(i=0;i<count;i++)
	{
		// and check to see if each is checked
		if(options[i].checked==true)
		{ 
			countChecked++; 
		}
	}  
	// is the count too high? 
	if(countChecked > 3)
	{ 
		error_message += "* You can request a quote on at most 3 different forms of annuities\n\n";
		which_item_to_focus( options[0] );
	}
	
	if(countChecked == 0)
	{ 
		error_message += "* Please select at least one form of annuity that you might be interested in.\n\n";
		which_item_to_focus( options[0] );
	}   
	//alert( theForm.purpose[0].length );
	
	// Check for the first name	
	if (theForm.first_name.value == "")
	{
		error_message += "* Please enter a \"First Name\"\n\n";
		which_item_to_focus( theForm.first_name );
	}

	// Check for the last name
	if (theForm.last_name.value == "")
	{
		error_message += "* Please enter a \"Last Name\"\n\n";
		which_item_to_focus( theForm.last_name );
	}

	// Check for the daytime phone  
	if (theForm.daytime_phone.value == "")
	{
		error_message += "* Please enter a \"Daytime Phone\"\n\n";
		which_item_to_focus( theForm.daytime_phone );
	}

	// Check for the first email
	if (theForm.email_1.value == "")
	{
		error_message += "* Please enter your \"E-mail Address\"\n\n";
		which_item_to_focus( theForm.email_1 );
	}

	/* 
	//Check for valid email address format
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.email1.value)))
	{
	  error_message += "* Invalid E-mail Address! Please re-enter.\n\n";
	  which_item_to_focus( theForm.email_1 );
	} 
	*/
	
	// Check for the street address
	if (theForm.street_address.value == "")
	{
		error_message += "* Please enter your \"Street Address\"\n\n";
		which_item_to_focus( theForm.street_address );
	}

	// Check for the city and state
	if (theForm.city_state.value == "")
	{
		error_message += "* Please enter your \"City and State\"\n\n";
		which_item_to_focus( theForm.city_state );
	}
	
	// Check for the city, state and zip  
	if (theForm.zip_code.value == "")
	{
		error_message += "* Please enter your 5 digits \"Zip Code\"\n\n";
		which_item_to_focus( theForm.zip_code );
	}  

	if (theForm.state_of_residence.selectedIndex == 0)
	{
		error_message += "* Please select a state in the  \"Annuitant's State of Residence\" section.\n\n";
		which_item_to_focus( theForm.state_of_residence );
	}
	
	// Check for the purchase date
	var radioSelected = false;
	for (i = 0;  i < theForm.purchase_date.length;  i++)
	{
		if (theForm.purchase_date[i].checked)
				radioSelected = true;
	}

	if (!radioSelected)
	{
		error_message += "* Please answer the \"When are you buying this annuity?\" question.\n\n";
		which_item_to_focus( theForm.purchase_date[0] );
	}		
	 
	// Check for the prefer to buy
	var radioSelected = false;
	for (i = 0;  i < theForm.how_prefer_to_buy.length;  i++)
	{
		if (theForm.how_prefer_to_buy[i].checked)
				radioSelected = true;
	}
	if (!radioSelected)
	{
		error_message += "* Please answer the \"Do you prefer to buy your annuity...?\" question\n\n";
		which_item_to_focus( theForm.how_prefer_to_buy[0] );
	}

	// process for error messages, create the error message alert text, set focus on first item
	if ( error_message )// if any of the tests triggered an error
	{
		if ( focus_click == click_counter ) // this will always be true the first call
		{
			click_counter++;//increment click_counter
		}
		else // update focus_click to match click_counter, set the b_first_focus back to false
		{
			focus_click++;
		}
		b_first_focus = false;// reset first focus
		error_message = error_start + error_message + error_close;
		alert( error_message );
		the_focus_item.focus();
		return (false);
	}
	else
	{
		return (true);
	}
}
/*****************************************
End : Validate Step 1 form
Begin : Validate Step 2 form
*****************************************/
function validateStep2(theForm)
{
	var error_message = '';

	// Check for the source of funds
	if (theForm.source_of_funds.selectedIndex < 0)
	{
		error_message += "* Please \"Select the source of funds used to buy this annuity...\"\n\n";
		which_item_to_focus( theForm.source_of_funds );
	}

	if (theForm.source_of_funds.selectedIndex == 0)
	{
		error_message += "* Please \"Select the source of funds used to buy this annuity...\"\n\n";
		which_item_to_focus( theForm.source_of_funds );
	}  

	// Check for the relationship to the buyer
	var radioSelected = false;
	for (i = 0;  i < theForm.relationship_to_buyer.length;  i++)
	{
		if (theForm.relationship_to_buyer[i].checked)
				radioSelected = true;
	}
	if (!radioSelected)
	{
		error_message += "* Please describe \"Your relationship to the person buying this annuity...\"\n\n";
		which_item_to_focus( theForm.relationship_to_buyer[0] );
	}	

	// Check for the commencement date
 var radioSelected = false;
	for (i = 0;  i < theForm.commencement_date.length;  i++)
	{
		if (theForm.commencement_date[i].checked)
				radioSelected = true;
	}
	if (!radioSelected)
	{
		error_message += "* Please answer the \"When should the insurance company begin to send monthly income payments to the annuitant...?\" question\n\n";
		which_item_to_focus( theForm.commencement_date[0] );
	} 

// Check for the DOB
	if (theForm.annuitant1_date_of_birth.value == "")
	{
		error_message += "* Please enter the Annuitant's Date of birth\n\n";
		which_item_to_focus( theForm.annuitant1_date_of_birth );
	}

	// Check for the Sex
	var radioSelected = false;
	for (i = 0;  i < theForm.annuitant1_sex.length;  i++)
	{
		if (theForm.annuitant1_sex[i].checked)
				radioSelected = true;
	}
	if (!radioSelected)
	{
		error_message += "* Please select the \"Annuitant's Gender\"\n\n";
		which_item_to_focus( theForm.annuitant1_sex[0] );
	}
	// process for error messages, create the error message alert text, set focus on first item
	if ( error_message )// if any of the tests triggered an error
	{
		if ( focus_click == click_counter ) // this will always be true the first call
		{
			click_counter++;//increment click_counter
		}
		else // update focus_click to match click_counter, set the b_first_focus back to false
		{
			focus_click++;
		}
		b_first_focus = false;// reset first focus
		error_message = error_start + error_message + error_close;
		alert( error_message );
		the_focus_item.focus();
		return (false);
	}
	else
	{
		return (true);
	}
}
/***************************
End : Validate Step 2 form

begin: utilities, set focus
***************************/
// is it used?
function takeAction()
{
	alert("You are closing this window");
}
// if it's the first item found, set focus on it
// b_first_focus is reset every user click
function which_item_to_focus( focus_on )
{
	if ( !b_first_focus )// if it has not been tripped yet on this call, that is.
	{
		b_first_focus = true;
		the_focus_item = focus_on;
	}
}
