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

function validateFormInput(theForm,whichform)
{
	var error_message = '';
	var error_start = "The Fields marked with a * are required\nPlease complete the following items: \n\n";
	var error_close = "Thank You.";

	// assign correct form field names
	if ( whichform == 'imm' )
	{
		ff_state = theForm.state;
		ff_age = theForm.age;
		ff_gender = theForm.gender;
		ff_deposit = theForm.deposit;
		ff_income = theForm.income;
		ff_period = theForm.period;
	}
	else if ( whichform == 'def' )
	{
		ff_state = theForm.propertyID96;
		ff_age = theForm.age;
		ff_gender = theForm.Gender;
		ff_deposit = theForm.propertyID97;
		ff_period = theForm.propertyID99;
	}
	// Check for the State
	if (ff_state.selectedIndex == 0)
	{
		error_message += "* Please select your \"State\"\n\n";
		which_item_to_focus( ff_state );
	}

// Check for the city, state and zip  
	if (ff_age.selectedIndex == 0)
	{
		error_message += "* Please select your \"Age\"\n\n";
		which_item_to_focus( ff_age );
	}

	if (ff_gender.selectedIndex == 0)
	{
		error_message += "* Please select your \"Gender\"\n\n";
		which_item_to_focus( ff_gender );
	}
	if ( whichform == 'imm' )
	{
		if ( ( theForm.joint_gender.selectedIndex == 0 && theForm.joint_age.selectedIndex != 0 ) || 
			( theForm.joint_age.selectedIndex == 0 && theForm.joint_gender.selectedIndex != 0 ) )
		{
			error_message += "* If you are requesting joint annuity information,\nboth the age and gender of the joint annuitant must be filled in.\n\n";
			which_item_to_focus( theForm.joint_gender );
		}
	}
	if ( whichform == 'def' )
	{
		if (ff_period.selectedIndex == 0)
		{
			error_message += "* Please select the \"Guarantee Period in Years\"\n\n";
			which_item_to_focus( ff_period );
		}
		if (ff_deposit.value == "" )
		{
			error_message += "* Please fill in the\n'How much you are investing'\n box then resubmit the form.\n\n";
			which_item_to_focus( ff_deposit );
		}
	}
	else if ( whichform == 'imm' )
	{
		if (ff_deposit.value == "" && ff_income.value== "")
		{
			error_message += "* Please fill in either the\n'How much you are investing'\nor\n'How much you would like to receive'\n box then resubmit the form.\n\n";
			which_item_to_focus( ff_deposit );
		}
		else if (ff_deposit.value != "" && ff_income.value != "")
		{
			error_message += "* You can only fill in one of the\n'How much you are investing'\nor\n'How much you would like to receive'\n boxes.\nPlease fill in only the one you want, then resubmit the form.\n\n";
			which_item_to_focus( ff_deposit );
		}
	}
	
	if ( ff_deposit.value != "" )
	{
		if ( !filter_number(ff_deposit.value) )
		{
			error_message += "* The numbers you enter in the deposit field\nmust be in one of the following formats:\n " +
			"20,000\n20000\n20000.00\n20,000.00\nPlease Correct your entry, Thank You\n\n";
			which_item_to_focus( ff_deposit );
		}
	}
	else if ( whichform == 'imm' && ff_income.value != "" )
	{
		if ( !filter_number(ff_income.value) )
		{
			error_message += "* The numbers you enter in the desired income field\nmust be in one of the following formats:\n " +
			"20,000\n20000\n20000.00\n20,000.00\nPlease Correct your entry, Thank You\n\n";
			which_item_to_focus( ff_income );
		}
	}

	// 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
	{
		error_message = error_start + error_message + error_close;
		alert( error_message );
		the_focus_item.focus();
		return (false);
	}
	else
	{
		return (true);
	}
}

function filter_number(currency)
{
	//pattern = /^\d{1,3}(,\d{3})*(\.\d{1,2})?$|^\d+(\.\d{1,2})?$/;
	// this is a very loose version of the stricter pattern above
	pattern = /^(\$)?\d+(,\d{3})*(\.\d*)?$|^\d+(\.\d*)?$/;

	if ( currency.search( pattern ) != -1 )
	{
		//alert( 'pattern found' );
		return true;
	}
	else
	{
		return false;
	}
}
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;
	}
}

// wcw audit functions
//<script type="text-javascript" src="http://www.whosclickingwho.com/wcwsc.js"></script>
function WCWAudit(parms) {
	var uri='<iframe src="//www.whosclickingwho.com/cgi-bin/wcwauditor.cgi?wcwrequesturi=' + escape(document.URL) + '%26' + escape(parms) + '&' + 'ppcreferer=' + escape(document.referrer) + '" frameborder=0 width=1 height=1></iframe>';
 	document.writeln(uri);
}
function WCWThankYou(value) { 
	var today = new Date();
	var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000); 
	document.cookie="WCWCookie=" + value + "; expires=" + expiry.toGMTString();
} 
