function wine_forgot_password() {
	document.getElementById("wine-password-login").value="";
	document.getElementById("password").style.display="none";
	document.getElementById("forgot-password").style.display="block";
	return false;
}
function wine_forgot_password_cancel() {
	document.getElementById("password").style.display="block";
	document.getElementById("forgot-password").style.display="none";
	return false;
}
function wine_gray_password(toggle) {
	var field = document.getElementById("wine-password");
	if (toggle) {
		field.value = "xxxxxxxxxx";
		field.style.backgroundColor = "#eee";
	} else {
		field.value = "";
		field.style.backgroundColor = "#fff";
	}
}
var isfocus = false;
var noerrors = true;
function wine_validate_profile() { // Validate Winery Form
	isfocus = false;
	noerrors = true;
	var tocheck = new Array();
	tocheck.push('wine-email');
	tocheck.push('wine-password');
	tocheck.push('wine-company_name');
	tocheck.push('wine-contact_first_name');
	tocheck.push('wine-contact_last_name');
	tocheck.push('wine-address');
	tocheck.push('wine-city');
	tocheck.push('wine-state_id');
	tocheck.push('wine-country_id');
	wine_validate(tocheck);
	return noerrors;
}
function wine_validate_payment() { // Validate Payment Form
	var elem = document.getElementById('wine-payment_type_id');
	if (elem.value == "2") { return true; }
	isfocus = false;
	noerrors = true;
	// Numeric Validations Follow
	elem = document.getElementById('wine-cc_num');
	if (!isNumber(elem.value)) {
		wine_validate_show(elem, "Please enter a 16 digit numeric credit card number.");
	} else {
		elem.style.border = "";
	}
	elem = document.getElementById('wine-cc_exp');
	if (!isNumber(elem.value)) {
		wine_validate_show(elem, "Please enter a 4 digit numeric credit card expiration date.");
	} else {
		elem.style.border = "";
	}
	elem = document.getElementById('wine-cc_cvv');
	if (!isNumber(elem.value)) {
		wine_validate_show(elem, "Please enter the verification value (last few digits) from the back of your card.");
	} else {
		elem.style.border = "";
	}
	var tocheck = new Array();
	tocheck.push('wine-cc_contact_first_name');
	tocheck.push('wine-cc_contact_last_name');
	tocheck.push('wine-cc_address');
	tocheck.push('wine-cc_city');
	tocheck.push('wine-cc_state_id');
	tocheck.push('wine-cc_country_id');
	tocheck.push('wine-cc_email');
	wine_validate(tocheck);
	return noerrors;
}
function wine_validate_item() { // Validate Wine Form
	isfocus = false;
	noerrors = true;
	var tocheck = new Array();
	var d = new Date();
	tocheck.push('wine-brand_name');
	wine_validate(tocheck);
	var elem = document.getElementById('wine-appellation_id');
	if (
		elem.value == ""
		&& document.getElementById('wine-appellation_id-custom').value == ""
	) {
		wine_validate_show(elem);
	} else {
		elem.style.border = "";
	}
	var elem = document.getElementById('wine-category_id');
	if (
		elem.value == ""
		&& document.getElementById('wine-category_id-custom').value == ""
	) {
		wine_validate_show(elem);
	} else {
		elem.style.border = "";
	}
	// Numeric Validations Follow
	elem = document.getElementById('wine-category_id-custom');
	if (elem.value != "" && !isNumber(elem.value)) {
		wine_validate_show(elem, "Please enter the category ID in numbers only.");
	} else if (elem.value != "" && !inArray(elem.value, document.getElementById('wine-category_id-set').value.split('|'))) {
		wine_validate_show(elem, "The category number you entered does not exist. Please enter another number or choose a category from the list above.");
	} else {
		elem.style.border = "";
	}
	var elem = document.getElementById('wine-vintage');
	if (
		elem.value != '0' && (
			elem.value < (d.getFullYear() - 50) || elem.value > d.getFullYear()
		)
	) {
		wine_validate_show(elem, "Please enter a 4-digit numeric year under Vintage.");
	} else {
		elem.style.border = "";
	}
	elem = document.getElementById('wine-alcohol');
	if (!isNumber(elem.value)) {
		wine_validate_show(elem, "Please enter the alcohol content in numbers only.");
	} else {
		elem.style.border = "";
	}
	elem = document.getElementById('wine-residual_sugar');
	if (!isNumber(elem.value)) {
		wine_validate_show(elem, "Please enter the total residual sugar in numbers only.");
	} else {
		elem.style.border = "";
	}
	elem = document.getElementById('wine-retail_price');
	if (!isNumber(elem.value)) {
		wine_validate_show(elem, "Please enter the complete retail price in numbers only.");
	} else {
		elem.style.border = "";
	}
	return noerrors;
}
function wine_validate(tocheck) { // Plain Validation Logic
	for (var i = 0; i < tocheck.length; i++) {
		var elem = document.getElementById(tocheck[i]);
		if (elem.value == "") {
			wine_validate_show(elem, false);
		} else {
			elem.style.border = "";
		}
	}
}
function wine_validate_show(elem, msg) { // Report First Validation Problem Only
	noerrors = false;
	if (!isfocus) {
		elem.style.border = "2px solid red";
		elem.focus();
		isfocus = true;
		if (msg) { alert(msg); }
	}
}
function wine_delete(delid) {
	if (confirm("Are you sure you wish to delete this wine?")) {
		var theForm = document.getElementById('wine-delete-form');
		theForm.wineid.value = delid;
		theForm.submit();
	}
	return false;
}
function wine_populate_ddm(elem, val, id) {
	jQuery("#wine-" + elem + "-toggle").click();
	document.getElementById("wine-" + elem).value = val;
	document.getElementById("wine-" + elem + "-custom").value = "";
	document.getElementById("wine-" + elem + "-id").value = id;
}
function wine_toggle_cc() {
	var toggle = "block";
	switch (document.getElementById('wine-payment_type_id').value) {
		case "1": toggle = "block"; break;
		case "2": toggle = "none"; break;
		default: alert(1);
	}
	document.getElementById('wine-cc-payment').style.display = toggle;
}
function isNumber(n) {
	return !isNaN(parseFloat(n)) && isFinite(n);
}
function inArray(needle, haystack) {
	var length = haystack.length;
	for (var i = 0; i < length; i++) {
		if (haystack[i] == needle) return true;
	}
	return false;
}
