letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
digits = "0123456789";

function validation(form)
{
	var ccValid = true;
	var type = form['type'];
	var document = form['document'];
	
	var fax = form['fax'];
	
	var s_name = form['s_name'];
	var s_address_1 = form['s_address_1'];
	var s_address_2 = form['s_address_2'];
	var s_city = form['s_city'];
	var s_state = form['s_state'];
	var s_zip = form['s_zip'];
	
	var b_name = form['b_name'];
	var b_address_1 = form['b_address_1'];
	var b_address_2 = form['b_address_2'];
	var b_city = form['b_city'];
	var b_state = form['b_state'];
	var b_zip = form['b_zip'];
	var email = form['email'];
	var work_phone = form['work_phone'];
	var home_phone = form['home_phone'];
	var cell_phone = form['cell_phone'];
	
	var cc_first_name = form['cc_first_name'];
	var cc_last_name = form['cc_last_name'];
	var cc_num = form['cc_num'];
	var cc_exp_month = form['cc_exp_month'];
	var cc_exp_year = form['cc_exp_year'];
	
	var first_name_1 = form['first_name_1'];
	var middle_inital_1 = form['middle_inital_1'];
	var last_name_1 = form['last_name_1'];
	var suffix_1 = form['suffix_1'];
	var ss_1_1 = form['ss_1_1'];
	var ss_1_2 = form['ss_1_2'];
	var ss_1_3 = form['ss_1_3'];
	var b_day_1_month = form['b_day_1_month'];
	var b_day_1_day = form['b_day_1_day'];
	var b_day_1_year = form['b_day_1_year'];
	
	var first_name_2 = form['first_name_2'];
	var middle_inital_2 = form['middle_inital_2'];
	var last_name_2 = form['last_name_2'];
	var suffix_2 = form['suffix_2'];
	var ss_2_1 = form['ss_2_1'];
	var ss_2_2 = form['ss_2_2'];
	var ss_2_3 = form['ss_2_3'];
	var b_day_2_month = form['b_day_2_month'];
	var b_day_2_day = form['b_day_2_day'];
	var b_day_2_year = form['b_day_2_year'];
	var accept = form['accept'];
	
	if (type.value == "")
	{
		type.focus();
		alert("Please select the application type.");
		return false;
	}
	
	if (document.value == "")
	{
		document.focus();
		alert("Please select a forwarding type.");
		return false;
	}
	
	if (document.value == "Private Fax")
	{
		if (fax.value == "" && !validString(fax.value, digits + " ()-.[]"))
		{
			fax.focus();
			alert("Please enter a valid private fax #.");
			return false;
		}
	}
	
	if (document.value == "Other Address")
	{
		if (s_name.value == "" || !validString(s_name.value, letters + " .-,"))
		{
			s_name.focus();
			alert("Please enter a valid shipping name.");
			return false;
		}
		if (s_address_1.value == "" || !validString(s_address_1.value, letters + digits + " .-_,#"))
		{
			s_address_1.focus();
			alert("Please enter a valid shipping address line 1.");
			return false;
		}
		if (s_address_2.value != "" && !validString(s_address_2.value, letters + digits + " .-_,#"))
		{
			s_address_2.focus();
			alert("Please enter a valid shipping address line 2.");
			return false;
		}
		if (s_city.value == "" || !validString(s_city.value, letters + digits + " .-_"))
		{
			s_city.focus();
			alert("Please enter a valid shipping city.");
			return false;
		}
		if (s_state.value == "")
		{
			s_state.focus();
			alert("Please select a shipping state.");
			return false;
		}
		if (s_zip.value.length < 5 || !validString(s_zip.value, digits))
		{
			s_zip.focus();
			alert("Please enter a valid shipping zip.");
			return false;
		}
	}
	
	if (b_name.value == "" || !validString(b_name.value, letters + " .-,"))
	{
		b_name.focus();
		alert("Please enter a valid billing name.");
		return false;
	}
	if (b_address_1.value == "" || !validString(b_address_1.value, letters + digits + " .-_,#"))
	{
		b_address_1.focus();
		alert("Please enter a valid billing address line 1.");
		return false;
	}
	if (b_address_2.value != "" && !validString(b_address_2.value, letters + digits + " .-_,#"))
	{
		b_address_2.focus();
		alert("Please enter a valid billing address line 2.");
		return false;
	}
	if (b_city.value == "" || !validString(b_city.value, letters + digits + " .-_"))
	{
		b_city.focus();
		alert("Please enter a valid billing city.");
		return false;
	}
	if (b_state.value == "")
	{
		b_state.focus();
		alert("Please select a billing state.");
		return false;
	}
	if (b_zip.value.length < 5 || !validString(b_zip.value, digits))
	{
		b_zip.focus();
		alert("Please enter a valid billing zip.");
		return false;
	}
	if (email.value == "")
	{
		email.focus();
		alert("Please enter a valid email.");
		return false;
	}
	if (work_phone.value != "" && !validString(work_phone.value, digits + " ()-.[]"))
	{
		work_phone.focus();
		alert("Please enter a valid work phone.");
		return false;
	}
	if (home_phone.value == "" || !validString(home_phone.value, digits + " ()-.[]"))
	{
		home_phone.focus();
		alert("Please enter a valid home phone.");
		return false;
	}
	if (cell_phone.value != "" && !validString(cell_phone.value, digits + " ()-.[]"))
	{
		cell_phone.focus();
		alert("Please enter a valid cell phone.");
		return false;
	}
	
	if (cc_first_name.value == "" || !validString(cc_first_name.value, letters + " .,-"))
	{
		cc_first_name.focus();
		alert("Please enter a valid credit card name.");
		return false;
	}
	if (cc_last_name.value == "" || !validString(cc_last_name.value, letters + " .,-"))
	{
		cc_last_name.focus();
		alert("Please enter a valid credit card name.");
		return false;
	}
	if (cc_num.value.length < 13 || !validString(cc_num.value, digits))
	{
		cc_num.focus();
		alert("Please enter a valid credit card number.");
		return false;
	}
	if (cc_exp_month.value == "")
	{
		cc_exp_month.focus();
		alert("Please select an expiration month for the credit card.");
		return false;
	}

	if (cc_exp_year.value == "")
	{
		cc_exp_year.focus();
		alert("Please select an expiration year for the credit card.");
		return false;
	}
	document.getElementById('credit_card_expiration_date').value = cc_exp_month.value + "/" + cc_exp_year.value;
	
	if (first_name_1.value == "" || !validString(first_name_1.value, letters + " .-"))
	{
		first_name_1.focus();
		alert("Please enter a valid first name for the applicant.");
		return false;
	}
	if (middle_name_1.value == "" || !validString(middle_name_1.value, letters))
	{
		middle_name_1.focus();
		alert("Please enter a valid middle initial for the applicant.");
		return false;
	}
	if (last_name_1.value == "" || !validString(last_name_1.value, letters + " .-"))
	{
		last_name_1.focus();
		alert("Please enter a valid last name for the applicant.");
		return false;
	}
	if (ss_1_1.value.length < 3 || !validString(ss_1_1.value, digits))
	{
		ss_1_1.focus();
		alert("Please enter a valid social security number.");
		return false;
	}
	if (ss_1_2.value.length < 2 || !validString(ss_1_2.value, digits))
	{
		ss_1_2.focus();
		alert("Please enter a valid social security number.");
		return false;
	}
	if (ss_1_3.value.length < 4 || !validString(ss_1_3.value, digits))
	{
		ss_1_3.focus();
		alert("Please enter a valid social security number.");
		return false;
	}
	if (b_day_1_month.value == "")
	{
		b_day_1_month.focus();
		alert("Please select a valid birth date.");
		return false;
	}
	if (b_day_1_day.value == "")
	{
		b_day_1_day.focus();
		alert("Please select a valid birth date.");
		return false;
	}
	if (b_day_1_year.value == "")
	{
		b_day_1_year.focus();
		alert("Please select a valid birth date.");
		return false;
	}
	
	if (type.value == "Joint Application")
	{
		if (first_name_2.value == "" || !validString(first_name_2.value, letters + " .-"))
		{
			first_name_2.focus();
			alert("Please enter a valid first name for the applicant.");
			return false;
		}
		if (middle_name_2.value == "" || !validString(middle_name_2.value, letters))
		{
			middle_name_2.focus();
			alert("Please enter a valid middle initial for the applicant.");
			return false;
		}
		if (last_name_2.value == "" || !validString(last_name_2.value, letters + " .-"))
		{
			last_name_2.focus();
			alert("Please enter a valid last name for the applicant.");
			return false;
		}
		if (ss_2_1.value.length < 3 || !validString(ss_2_1.value, digits))
		{
			ss_2_1.focus();
			alert("Please enter a valid social security number.");
			return false;
		}
		if (ss_2_2.value.length < 2 || !validString(ss_2_2.value, digits))
		{
			ss_2_2.focus();
			alert("Please enter a valid social security number.");
			return false;
		}
		if (ss_2_3.value.length < 4 || !validString(ss_2_3.value, digits))
		{
			ss_2_3.focus();
			alert("Please enter a valid social security number.");
			return false;
		}
		if (b_day_2_month.value == "")
		{
			b_day_2_month.focus();
			alert("Please select a valid birth date.");
			return false;
		}
		if (b_day_2_day.value == "")
		{
			b_day_2_day.focus();
			alert("Please select a valid birth date.");
			return false;
		}
		if (b_day_2_year.value == "")
		{
			b_day_2_year.focus();
			alert("Please select a valid birth date.");
			return false;
		}
	}
	
	if (accept.checked == false)
	{
		accept.focus();
		alert("You must agree to the terms before continuing.");
		return false;
	}
	
	return true;
}

function validString(str, validChars)
{
	for (i = 0; i < str.length; i++)
	{
		if (validChars.indexOf(str.charAt(i)) < 0)
		{
			return false;
		}
	}
	
	return true;
}

function show_ship()
{
	val = document.getElementById('document').value;
	document.getElementById('fax_div').style.display = 'none';
	document.getElementById('s_name_div').style.display = 'none';
	document.getElementById('s_label_div').style.display = 'none';
	document.getElementById('s_address_1_div').style.display = 'none';
	document.getElementById('s_address_2_div').style.display = 'none';
	document.getElementById('s_city_div').style.display = 'none';
	document.getElementById('s_state_div').style.display = 'none';
	document.getElementById('s_zip_div').style.display = 'none';
	
	if (val == 'Other Address')
	{
		document.getElementById('s_name_div').style.display = '';
		document.getElementById('s_label_div').style.display = '';
		document.getElementById('s_address_1_div').style.display = '';
		document.getElementById('s_address_2_div').style.display = '';
		document.getElementById('s_city_div').style.display = '';
		document.getElementById('s_state_div').style.display = '';
		document.getElementById('s_zip_div').style.display = '';
	}
	else if (val == "Private Fax")
	{
		document.getElementById('fax_div').style.display = '';
	}
}

function update_type()
{
	type = document.getElementById('type').value;
	
	document.getElementById('co_label_div').style.display = 'none';
	document.getElementById('co_name_div').style.display = 'none';
	document.getElementById('co_social_div').style.display = 'none';
	document.getElementById('co_bday_div').style.display = 'none';
	
	if (type == 'Single Application')
	{
		document.getElementById('price_div').innerHTML = "$ 169.95";
		document.getElementById('total_cart').value = "169.95";
	}
	else if (type == 'Joint Application')
	{
		document.getElementById('price_div').innerHTML = "$ 229.95";
		document.getElementById('total_cart').value = "229.95";
	
		document.getElementById('co_label_div').style.display = '';
		document.getElementById('co_name_div').style.display = '';
		document.getElementById('co_social_div').style.display = '';
		document.getElementById('co_bday_div').style.display = '';
	}
	else
	{
		document.getElementById('price_div').innerHTML = "N/A";
	}
}