var terms_valid = false;
var name_valid = false;
var address_valid = false;
var city_valid = false;
var phone_valid = false;
var email_valid = false;

var ship_name_valid = false;
var ship_address_valid = false;
var ship_city_valid = false;

var cc_type_valid = false;
var cc_num_valid = false;
var cc_name_valid = false;
var cc_csc_valid = false;
var cc_month_valid = false;
var cc_year_valid = false;

function validate_form() {
	if(!terms_valid) {
		alert('You must agree to the terms and conditions\nof abacohealth.com by checking the box\nabove to be able to submit your order.');
		document.checkoutfrm.agreeterms.focus();
		return false;
	}
	else if(!name_valid) {
		alert('You must enter a valid name.');
		document.getElementById('bill-to').focus();
		return false;
	}
	else if(!address_valid) {
		alert('You must enter a valid address.');
		document.getElementById('bill-address').focus();
		return false;
	}
	else if(!city_valid) {
		alert('You must enter a valid city.');
		document.getElementById('bill-city').focus();
		return false;
	}
	else if(!phone_valid) {
		alert('You must enter a valid phone number.');
		document.getElementById('bill-phone').focus();
		return false;
	}
	else if(!email_valid) {
		alert('You must enter a valid email address.');
		document.getElementById('bill-email').focus();
		return false;
	}
	else if(!ship_name_valid) {
		alert('You must enter a valid recipient name.');
		document.getElementById('ship-to').focus();
		return false;
	}
	else if(!ship_address_valid) {
		alert('You must enter a valid shipping address.');
		document.getElementById('ship-address').focus();
		return false;
	}
	else if(!ship_city_valid) {
		alert('You must enter a valid shipping city.');
		document.getElementById('ship-city').focus();
		return false;
	}
	else if(!cc_type_valid) {
		alert('You must enter a valid credit card type.');
		return false;
	}
	else if(!cc_num_valid) {
		alert('You must enter a valid credit card number.');
		document.getElementById('cc-num').focus();
		return false;
	}
	else if(!cc_name_valid) {
		alert('You must enter a valid name on the credit card.');
		document.getElementById('cc-name').focus();
		return false;
	}
	else if(!cc_month_valid) {
		alert('You must enter a valid credit card expiration month.');
		document.getElementById('cc-month').focus();
		return false;
	}
	else if(!cc_year_valid) {
		alert('You must enter a valid credit card expiration year.');
		document.getElementById('cc-year').focus();
		return false;
	}
	else if(!cc_csc_valid) {
		alert('You must enter a valid credit card security code');
		document.getElementById('cc-csc').focus();
		return false;
	}
	
	return true;
}

function check_terms() {
	if(document.checkoutfrm.agreeterms.checked == true)
		terms_valid = true;
	else
		terms_valid = false;
}

function check_name() {
	var name_field = document.getElementById('bill-to');
	if(name_field.value == "") {
		document.getElementById('name-valid').style.visibility = 'hidden';
		document.getElementById('name-invalid').style.visibility = 'visible';
		name_valid = false;
	}
	else {
		document.getElementById('name-valid').style.visibility = 'visible';
		document.getElementById('name-invalid').style.visibility = 'hidden';
		name_valid = true;
	}
}

function check_recipient_name() {
	var name_field = document.getElementById('ship-to');
	if(name_field.value == "") {
		document.getElementById('ship-name-valid').style.visibility = 'hidden';
		document.getElementById('ship-name-invalid').style.visibility = 'visible';
		ship_name_valid = false;
	}
	else {
		document.getElementById('ship-name-valid').style.visibility = 'visible';
		document.getElementById('ship-name-invalid').style.visibility = 'hidden';
		ship_name_valid = true;
	}
}

function check_city() {
	var city_field = document.getElementById('bill-city');
	if(city_field.value == "") {
		document.getElementById('city-valid').style.visibility = 'hidden';
		document.getElementById('city-invalid').style.visibility = 'visible';
		city_valid = false;
	}
	else {
		document.getElementById('city-valid').style.visibility = 'visible';
		document.getElementById('city-invalid').style.visibility = 'hidden';
		city_valid = true;
	}
}

function check_ship_city() {
	var city_field = document.getElementById('ship-city');
	if(city_field.value == "") {
		document.getElementById('ship-city-valid').style.visibility = 'hidden';
		document.getElementById('ship-city-invalid').style.visibility = 'visible';
		ship_city_valid = false;
	}
	else {
		document.getElementById('ship-city-valid').style.visibility = 'visible';
		document.getElementById('ship-city-invalid').style.visibility = 'hidden';
		ship_city_valid = true;
	}
}

// Same as above but no images to deal with
function check_ship_city_same_as_bill() {
	var city_field = document.getElementById('ship-city');
	if(city_field.value == "") {
		ship_city_valid = false;
	}
	else {
		ship_city_valid = true;
	}
}

function check_address() {
	var address_field = document.getElementById('bill-address');
	if(address_field.value == "") {
		document.getElementById('address-valid').style.visibility = 'hidden';
		document.getElementById('address-invalid').style.visibility = 'visible';
		address_valid = false;
	}
	else {
		document.getElementById('address-valid').style.visibility = 'visible';
		document.getElementById('address-invalid').style.visibility = 'hidden';
		address_valid = true;
	}
}

function check_ship_address() {
	var address_field = document.getElementById('ship-address');
	if(address_field.value == "") {
		document.getElementById('ship-address-valid').style.visibility = 'hidden';
		document.getElementById('ship-address-invalid').style.visibility = 'visible';
		ship_address_valid = false;
	}
	else {
		document.getElementById('ship-address-valid').style.visibility = 'visible';
		document.getElementById('ship-address-invalid').style.visibility = 'hidden';
		ship_address_valid = true;
	}
}

// Same as above but no images to deal with
function check_ship_address_same_as_bill() {
	var address_field = document.getElementById('ship-address');
	if(address_field.value == "") {
		ship_address_valid = false;
	}
	else {
		ship_address_valid = true;
	}
}

function check_phone() {
	var pattern = /([0-9]{3})[- ]?([0-9]{3})[- ]?([0-9]{4})/;
	var phone_field = document.getElementById('bill-phone');
	if(!pattern.test(phone_field.value)) {
		document.getElementById('phone-valid').style.visibility = 'hidden';
		document.getElementById('phone-invalid').style.visibility = 'visible';
		phone_valid = false;
	}
	else {
		document.getElementById('phone-valid').style.visibility = 'visible';
		document.getElementById('phone-invalid').style.visibility = 'hidden';
		phone_valid = true;
	}	
}

function check_email() {
	var pattern = /(.+)@(.+)\.(.{2,3})$/;
	var email_field = document.getElementById('bill-email');
	if(!pattern.test(email_field.value)) {
		document.getElementById('email-valid').style.visibility = 'hidden';
		document.getElementById('email-invalid').style.visibility = 'visible';
		email_valid = false;
	}
	else {
		document.getElementById('email-valid').style.visibility = 'visible';
		document.getElementById('email-invalid').style.visibility = 'hidden';
		email_valid = true;
	}	
}

function check_cc_type() {
	if(document.getElementById('CC_VISA').checked || document.getElementById('CC_MC').checked)
		cc_type_valid = true;
	else
		cc_type_valid = false;
}

function check_cc_num() {
//	var pattern = /^[0-9]{13}/;
	var pattern = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$/;
	var cc_field = document.getElementById('cc-num');
	if(!pattern.test(cc_field.value)) {
		document.getElementById('cc-valid').style.visibility = 'hidden';
		document.getElementById('cc-invalid').style.visibility = 'visible';
		cc_num_valid = false;
	}
	else {
		document.getElementById('cc-valid').style.visibility = 'visible';
		document.getElementById('cc-invalid').style.visibility = 'hidden';
		cc_num_valid = true;
	}	
}

function check_cc_name() {
	var cc_name_field = document.getElementById('cc-name');
	if(cc_name_field.value == "") {
		document.getElementById('cc-name-valid').style.visibility = 'hidden';
		document.getElementById('cc-name-invalid').style.visibility = 'visible';
		cc_name_valid = false;
	}
	else {
		document.getElementById('cc-name-valid').style.visibility = 'visible';
		document.getElementById('cc-name-invalid').style.visibility = 'hidden';
		cc_name_valid = true;
	}
}

function check_cc_csc() {
	var pattern = /^[0-9]{3}[0-9]?/;
	var csc_field = document.getElementById('cc-csc');
	if(!pattern.test(csc_field.value)) {
		document.getElementById('csc-valid').style.visibility = 'hidden';
		document.getElementById('csc-invalid').style.visibility = 'visible';
		cc_csc_valid = false;
	}
	else {
		document.getElementById('csc-valid').style.visibility = 'visible';
		document.getElementById('csc-invalid').style.visibility = 'hidden';
		cc_csc_valid = true;
	}	
}

function check_cc_month() {
	var cc_month_field = document.getElementById('cc-month');
	if(cc_month_field.value != 0)
		cc_month_valid = true;
	else
		cc_month_valid = false;
}

function check_cc_year() {
	var cc_year_field = document.getElementById('cc-year');
	if(cc_year_field.value != 0)
		cc_year_valid = true;
	else
		cc_year_valid = false;
}

function checkterms() {
	var cntsbmt = false;
	if(document.checkoutfrm.agreeterms.checked == true) {
		cntsbmt = true;
	}
	else {
		alert('You must agree to the terms and conditions\nof abacohealth.com by checking the box\nabove to be able to submit your order.');
	}
	return cntsbmt;
}
