// Generic form validation functions
// Include before any of the form validation scripts
function validate_text_field(text, minLength) {
	if (text.length < minLength)
		return false;
	return true;
}

function validate_text_field(text, minLength, maxLength) {
	if (text.length < minLength || text.length > maxLength)
		return false;
	return true;
}

function validate_email_address(email, maxLength) {
	if (email.length > maxLength)
		return false;

	var at_position = email.indexOf("@");
	if (at_position > 0) {
		var dot_position = email.indexOf(".", at_position);
		if ((dot_position > at_position+1) && (email.length > dot_position+1))
			return true;
	}

	return false;
}

// Validate a single select input (usually a drop-down)
// User must have selected an option different from the default NULL/blank value
function validate_select_single(select_array) {
	var opts = select_array.options;
	for (var i = 0; i < opts.length; i++) {
		if ((opts[i].selected) && (opts[i].value != "NULL") && (opts[i].value != "null"))
			return true;
	}
	return false;
}

// Validate a multiple select box
// User must have selected at least minSelected items
function validate_select_multiple(select_array, minSelected) {
	var opts = select_array.options;
	var num_selected = 0;
	for (var i = 0; i < opts.length; i++) {
		if (opts[i].selected)
			num_selected++;
		if (num_selected >= minSelected)
			return true;
	}
	return false;
}

// Check one of the radio options is selected
function validate_radio_group(radio_group) {
	for (var i = 0; i < radio_group.length; i++) {
		if (radio_group[i].checked) // True if it is selected
			return true;
	}
	return false;
}

// Check at least one of the checkboxes is selected
function validate_checkbox_group(checkbox_group) {
	for (var i = 0; i < checkbox_group.length; i++) {
		if (checkbox_group[i].checked) // True if it is selected
			return true;
	}
	return false;
}

// Check it is a valid numerical price
function validate_price_field(price_str) {
	if (price_str.length < 1)
		return false;
	// Reg exp: Will match up to 8 digits, optionally followed by a decimal point and a further 2 digits
	var reg_exp = /^\d{1,8}(\.\d{2})?$/;
	if (price_str.search(reg_exp) == -1)
		return false;
	return true;
}

function validate_price(part_number, pricing_qty, end_user_price, dist_price) {
	// If none are set, the price is empty
	if ((part_number.length == 0) && (pricing_qty.length == 0) && (end_user_price.length == 0) && (dist_price.length == 0))
		return "NOT SET";

	if (!validate_text_field(part_number, 1, 50))
		return "PART_NUM_ERROR";
	if (!validate_text_field(pricing_qty, 1, 30))
		return "PRICING_QTY_ERROR";
	if (!validate_price_field(end_user_price))
		return "END_USER_PRICE_ERROR";
	if (!validate_price_field(dist_price))
		return "DIST_PRICE_ERROR";

	return "OK";
}

// CVS: $Id: enquiry_validator.js,v 1.1 2007/11/02 10:49:32 edward Exp $

// JavaScript to validate enquiry form data prior to being sent
function validate_enquiry_form() {
	var f = document.enquiry_form;
	var msg = new String();

	// Name
	if (!validate_text_field(f.name.value, 1)) {
		f.name.select();
		f.name.focus();
		msg = "Name Error:\n";
		msg += " - Please specify your name";
		alert(msg);
		return false;
	}

	// Company
	if (!validate_text_field(f.company.value, 1)) {
		f.company.select();
		f.company.focus();
		msg = "Company Error:\n";
		msg += " - Please specify company name";
		alert(msg);
		return false;
	}
	// Country
	if (!validate_select_single(f.country)) {
		f.country.focus();
		msg = "Country Error:\n";
		msg += " - Please select your country";
		alert(msg);
		return false;
	}

	// Email
	if (!validate_email_address(f.email.value, 150)) {
		f.email.select();
		f.email.focus();
		msg = "Email Address Error:\n";
		msg += " - An email address is required\n";
		msg += " - It should be in the format 'user@domain.com'";
		alert(msg);
		return false;
	}
	// Email Confirm
	if (!validate_email_address(f.email_conf.value, 150)) {
		f.email_conf.select();
		f.email_conf.focus();
		msg = "Confirm Email Address Error:\n";
		msg += " - An email address is required\n";
		msg += " - It should be in the format 'user@domain.com'";
		alert(msg);
		return false;
	}
	if( f.email_conf.value!=f.email.value ) {
		f.email_conf.select();
		f.email_conf.focus();
		msg = "Confirm Email Address Error:\n";
		msg += " - Email addresses don't match\n";
		alert(msg);
		return false;
	}

	// Tel
	if (!validate_text_field(f.tel.value, 1)) {
		f.tel.select();
		f.tel.focus();
		msg = "Contact No. Error:\n";
		msg += " - Please specify your telephone number";
		alert(msg);
		return false;
	}

	// Message
	if (!validate_text_field(f.message.value, 1)) {
		f.message.select();
		f.message.focus();
		msg = "Message Error:\n";
		msg += " - Please enter your message/enquiry";
		alert(msg);
		return false;
	}

	return true;
}
