function validateSearch() {
	var f = document.searchForm;
	if (f.search.value == '' || f.search.value == 'Write a word') {
		return false;
	}
	return true;
}

function validateLogin(f) {
	var mail = f.mail.value;
	var pass = f.pass.value;
	if (!is_email(mail)) {
		alert('Please, check email.');
		f.mail.focus();
		return false;
	} else if (pass.length < 1) {
		alert('Please, write your password.');
		f.pass.focus();
		return false;
	}
}

function newPass(f) {
	var mail = f.mail.value;
	if (!is_email(mail)) {
		alert('Please, check email.');
		f.mail.focus();
		return false;
	} else {
		return confirm('Create and send a new password to ' + mail + '?\t');
	}
}

function newUser(f) {

	// Unset all styles...
	var lblFirstName = document.getElementById("lblFirstName");
	lblFirstName.style.cssText = "";
	f.first_name.style.cssText = "";
	var lblLastName = document.getElementById("lblLastName");
	lblLastName.style.cssText = "";
	f.last_name.style.cssText = "";
	var lblMail = document.getElementById("lblMail");
	lblMail.style.cssText = "";
	f.mail.style.cssText = "";
	var lblPhone = document.getElementById("lblPhone");
	lblPhone.style.cssText = "";
	f.phone.style.cssText = "";

	// Get values...
	var first_name = f.first_name.value;
	var last_name = f.last_name.value;
	var mail = f.mail.value;
	var phone = f.phone.value;
	
	// Two arrays to hold element name and element label...
	var errors = new Array();
	var err_str = new Array();
	var i = 0;
	var str = "";
	if (first_name.length < 2) {
		errors[i] = "first_name";
		err_str[i++] = "First Name";
		f.first_name.style.border = "2px solid #f00";
		lblFirstName.style.color = "#f00";
	}
	if (last_name.length < 2) {
		errors[i] = "last_name";
		err_str[i++] = "Last Name";
		f.last_name.style.border = "2px solid #f00";
		lblLastName.style.color = "#f00";
	}
	if (!is_email(mail)) {
		errors[i] = "mail";
		err_str[i++] = "Email";
		f.mail.style.border = "2px solid #f00";
		lblMail.style.color = "#f00";
	}
	if (phone.length < 5) {
		errors[i] = "phone";
		err_str[i++] = "Phone";
		f.phone.style.border = "2px solid #f00";
		lblPhone.style.color = "#f00";
	}	
	if (errors.length > 0) {
		if (document.getElementById) {
			var message = document.getElementById("message");
			message.innerHTML = "Please check the marked fields:";
		} else {
			for (i = 0; i < errors.length; i++) {
				str += err_str[i] + "\n";
			}
			alert ("Please check the following fields:\t\n\n" + str);
		}
		return false;
	}
	
}

function validateMail(f) {
	var sender_name = f.sender_name.value;
	var sender_email = f.sender_email.value;
	var subject = f.subject.value;
	var message = f.message.value;
	if (sender_name.length < 1) {
		if (confirm('Continue without Sender name?')) {
			// nothing...
		} else {
			f.sender_name.focus();
			f.sender_name.select();
			return false;
		}
	}
	if (!is_email(sender_email)) {
		alert('Please, check email.');
		f.sender_email.focus();
		f.sender_email.select();
		return false;
	} else if (subject.length < 1) {
		if (confirm('Continue without a subject?')) {
			// nothing...
		} else {
			f.subject.focus();
			return false;
		}
	}
	if (message.length < 1) {
		alert('Please, write your message.');
		f.message.focus();
		f.message.select();
		return false;
	}
}

function validateOrderForm(f) {
	if (f.sender_name.value.length < 1) {
		alert('Please, write your name.');
		f.sender_name.focus();
		return false;
	} else if (!is_email(f.sender_email.value)) {
		alert('Please, check the mail address.');
		f.sender_email.focus();
		f.sender_email.select();
		return false;
	} else if (f.address.value.length < 1) {
		alert('Please, write your street address.');
		f.address.focus();
		return false;
	} else if (f.zip.value.length < 1) {
		alert('Please, write your zip code.');
		f.zip.focus();
		return false;
	} else if (f.city.value.length < 1) {
		alert('Please, write your city.');
		f.city.focus();
		return false;
	}
}

function validateUser(f) {
	if (f.user_email.value.length > 0 && !is_email(f.user_email.value)) {
		alert('Please, check email...');
		f.user_email.focus();
		f.user_email.select();
		return false;
	}
}
function validateDeleteUser(user, str) {
	return confirm('Remove ' + user + ' from ' + str + '?\t')
}

function validateCheckout(f) {

	// Unset all styles...
	var lblFirstName = document.getElementById("lblFirstName");
	lblFirstName.style.cssText = "";
	f.first_name.style.cssText = "";
	var lblLastName = document.getElementById("lblLastName");
	lblLastName.style.cssText = "";
	f.last_name.style.cssText = "";
	var lblCompany = document.getElementById("lblCompany");
	lblCompany.style.cssText = "";
	f.company.style.cssText = "";
	var lblAddress = document.getElementById("lblAddress");
	lblAddress.style.cssText = "";
	f.address.style.cssText = "";
	var lblZip_and_city = document.getElementById("lblZip_and_city");
	lblZip_and_city.style.cssText = "";
	f.zip.style.cssText = "";
	f.city.style.cssText = "";
	var lblCountry = document.getElementById("lblCountry");
	lblCountry.style.cssText = "";
	f.tld.style.cssText = "";
	var lblPayment_method = document.getElementById("lblPayment_method");
	lblPayment_method.style.cssText = "";
	var pay_method = document.getElementById("pay_method");
	pay_method.style.cssText = "";
	var lblEmail = document.getElementById("lblEmail");
	lblEmail.style.cssText = "";
	f.email.style.cssText = "";
	var lblPhone = document.getElementById("lblPhone");
	lblPhone.style.cssText = "";
	f.phone.style.cssText = "";
	var cond_approved = document.getElementById("cond_approved");
	cond_approved.style.cssText = "";

	// Get values...
	var first_name = f.first_name.value;
	var last_name = f.last_name.value;
	var company = f.company.value;
	var address = f.address.value;
	var zip = f.zip.value;
	var city = f.city.value;
	var tld = f.tld.options[f.tld.selectedIndex].value;
	var email = f.email.value;
	var phone = f.phone.value;
	var cellphone = f.cellphone.value;
	
	// Two arrays to hold element name and element label...
	var errors = new Array();
	var err_str = new Array();
	var i = 0;
	var str = "";
	if (first_name.length < 2) {
		errors[i] = "first_name";
		err_str[i++] = "First Name";
		f.first_name.style.border = "2px solid #f00";
		lblFirstName.style.color = "#f00";
	}
	if (last_name.length < 2) {
		errors[i] = "last_name";
		err_str[i++] = "Last Name";
		f.last_name.style.border = "2px solid #f00";
		lblLastName.style.color = "#f00";
	}
	
	// If payment method is invoice, Company must be filled...
	if (f.payment_method.length != null) {
		// array...
		for (j = 0; j < f.payment_method.length; j++) {
			if (f.payment_method[j].value == 'invoice') {
				// invoice radio button found...
				if (f.payment_method[j].checked && company.length < 1) {
					errors[i] = "company";
					err_str[i++] = "Company";
					f.company.style.border = "2px solid #f00";
					lblCompany.style.color = "#f00";
					break;
				}
			}
		}
	} else {
		// singlet...
		if (f.payment_method.value == 'invoice' && company.length < 1) {
			errors[i] = "company";
			err_str[i++] = "Company";
			f.company.style.border = "2px solid #f00";
			lblCompany.style.color = "#f00";
		}
	}
	
	if (address.length < 2) {
		errors[i] = "address";
		err_str[i++] = "Adsress";
		f.address.style.border = "2px solid #f00";
		lblAddress.style.color = "#f00";
	}
	if (zip.length < 2) {
		errors[i] = "zip";
		err_str[i++] = "Zip";
		f.zip.style.border = "2px solid #f00";
		lblZip_and_city.style.color = "#f00";
	}
	if (city.length < 2) {
		errors[i] = "city";
		err_str[i++] = "City";
		f.city.style.border = "2px solid #f00";
		lblZip_and_city.style.color = "#f00";
	}
	if (tld == " unknown ") {
		errors[i] = "tld";
		err_str[i++] = "Country";
		f.tld.style.backgroundColor = "#f66";
		lblCountry.style.color = "#f00";
	}
	
	var payment_methodChecked = false;
	if (f.payment_method.length != null) {
		// array...
		for (j = 0; j < f.payment_method.length; j++) {
			if (f.payment_method[j].checked) {
				payment_methodChecked = true;
				break;
			}
		}
	} else {
		// singlet...
		if (f.payment_method.checked) {
			payment_methodChecked = true;
		}
	}
	if (!payment_methodChecked) {
		errors[i] = "payment_method";
		err_str[i++] = "Payment method";
		pay_method.style.color = "#f00";
		lblPayment_method.style.color = "#f00";
	}
	
	if (!is_email(email)) {
		errors[i] = "email";
		err_str[i++] = "Email";
		f.email.style.border = "2px solid #f00";
		lblEmail.style.color = "#f00";
	}
	if (phone.length < 5) {
		errors[i] = "phone";
		err_str[i++] = "Phone";
		f.phone.style.border = "2px solid #f00";
		lblPhone.style.color = "#f00";
	}
	if (!f.conditions_approved.checked) {
		errors[i] = "conditions_approved";
		err_str[i++] = "Conditions approved";
		cond_approved.style.color = "#f00";
	}

	if (errors.length > 0) {
		if (document.getElementById) {
			var message = document.getElementById("message");
			message.innerHTML = "Please check the marked fields:";
		} else {
			for (i = 0; i < errors.length; i++) {
				str += err_str[i] + "\n";
			}
			alert ("Please check the following fields:\t\n\n" + str);
		}
		// Canīt focus on a radio group or select a dropdown...
		if (errors[0] != "payment_method") {
			eval("f." + errors[0] + ".focus()");
			if (errors[0] != "tld") {
				eval("f." + errors[0] + ".select()");
			}
		}
		return false;
	}
	
}


function shopStart() {
	confirm('\nThe product is placed in your cart.\n' +
	'Go to "Check Out" when you are done shopping.');
}

function confirmLoginAsMember() {
	if (confirm("You have to be logged in as a Member to access this file.\n" +
	"Do you want to go to the login page now?")) {
		location.href='/login.php';
	}
}

/* Calendar */
function validateCalendar(f) {
	if (!is_date(f.datum.value)) {
		alert('Please, check date...');
		f.datum.focus();
		f.datum.select();
		return false;
	} else if (f.time.value.length > 0 && !is_time(f.time.value)) {
		alert('Please, check time...');
		f.time.focus();
		f.time.select();
		return false;
	} else if (f.event.value.length < 1) {
		alert('Please, check event...');
		f.event.focus();
		return false;
	}
}
function validateDeleteCalendarEvent() {
	return confirm('Delete this calendar event?');
}

/* News-letter subscription */
function validateSubscribe(f) {
	if (!is_email(f.mail.value)) {
		alert('Please, check Email.');
		f.mail.focus();
		return false;
	}
	if (!f.subscribe[0].checked && !f.subscribe[1].checked) {
		alert('Please check what you want to do.');
		return false;
	}
	return true;
}
