function validateSearch() {
	var f = document.searchForm;
	if (f.search.value == '' || f.search.value == 'Skriv sökord här') {
		return false;
	}
	return true;
}

function validateLogin(f) {
	var mail = f.mail.value;
	var pass = f.pass.value;
	if (!is_email(mail)) {
		alert('Vg kontrollera epost.');
		f.mail.focus();
		return false;
	} else if (pass.length < 1) {
		alert('Vg skriv lösenord.');
		f.pass.focus();
		return false;
	}
}

function newPass(f) {
	var mail = f.mail.value;
	if (!is_email(mail)) {
		alert('Vg kontrollera epost.');
		f.mail.focus();
		return false;
	} else {
		return confirm('Skapa och skicka nytt lösenord till ' + 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++] = "Förnamn";
		f.first_name.style.border = "2px solid #f00";
		lblFirstName.style.color = "#f00";
	}
	if (last_name.length < 2) {
		errors[i] = "last_name";
		err_str[i++] = "Efternamn";
		f.last_name.style.border = "2px solid #f00";
		lblLastName.style.color = "#f00";
	}
	if (!is_email(mail)) {
		errors[i] = "mail";
		err_str[i++] = "Epost";
		f.mail.style.border = "2px solid #f00";
		lblMail.style.color = "#f00";
	}
	if (phone.length < 5 || !is_phone(phone)) {
		errors[i] = "phone";
		err_str[i++] = "Tel";
		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 = "Var god kontrollera markerade fält:";
		} else {
			for (i = 0; i < errors.length; i++) {
				str += err_str[i] + "\n";
			}
			alert ("Var god kontrollera följande fält:\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('Fortsätt utan avsändarnamn?')) {
			// nothing...
		} else {
			f.sender_name.focus();
			f.sender_name.select();
			return false;
		}
	}
	if (!is_email(sender_email)) {
		alert('Vg kontrollera epost.');
		f.sender_email.focus();
		f.sender_email.select();
		return false;
	} else if (subject.length < 1) {
		if (confirm('Fortsätt utan ämne?')) {
			// nothing...
		} else {
			f.subject.focus();
			return false;
		}
	}
	if (message.length < 1) {
		alert('Vg skriv ditt meddelande.');
		f.message.focus();
		f.message.select();
		return false;
	}
}

function validateOrderForm(f) {
	if (f.sender_name.value.length < 1) {
		alert('Vg skriv ditt namn.');
		f.sender_name.focus();
		return false;
	} else if (!is_email(f.sender_email.value)) {
		alert('Vg kontrollera epost-adressen.');
		f.sender_email.focus();
		f.sender_email.select();
		return false;
	} else if (f.address.value.length < 1) {
		alert('Vg skriv adressen.');
		f.address.focus();
		return false;
	} else if (f.zip.value.length < 1) {
		alert('Vg skriv postnummer.');
		f.zip.focus();
		return false;
	} else if (f.city.value.length < 1) {
		alert('Vg skriv orten.');
		f.city.focus();
		return false;
	}
}

function validateUser(f) {
	if (f.user_email.value.length > 0 && !is_email(f.user_email.value)) {
		alert('Vg kontrollera epost...');
		f.user_email.focus();
		f.user_email.select();
		return false;
	}
}
function validateDeleteUser(user, str) {
	return confirm('Ta bort ' + user + ' från ' + 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++] = "Förnamn";
		f.first_name.style.border = "2px solid #f00";
		lblFirstName.style.color = "#f00";
	}
	if (last_name.length < 2) {
		errors[i] = "last_name";
		err_str[i++] = "Efternamn";
		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++] = "Företag";
					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++] = "Företag";
			f.company.style.border = "2px solid #f00";
			lblCompany.style.color = "#f00";
		}
	}
	
	if (address.length < 2) {
		errors[i] = "address";
		err_str[i++] = "Adress";
		f.address.style.border = "2px solid #f00";
		lblAddress.style.color = "#f00";
	}
	if (zip.length < 2) {
		errors[i] = "zip";
		err_str[i++] = "Postnummer";
		f.zip.style.border = "2px solid #f00";
		lblZip_and_city.style.color = "#f00";
	}
	if (city.length < 2) {
		errors[i] = "city";
		err_str[i++] = "Ort";
		f.city.style.border = "2px solid #f00";
		lblZip_and_city.style.color = "#f00";
	}
	if (tld == " unknown ") {
		errors[i] = "tld";
		err_str[i++] = "Land";
		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++] = "Betalningsmetod";
		pay_method.style.color = "#f00";
		lblPayment_method.style.color = "#f00";
	}
	
	if (!is_email(email)) {
		errors[i] = "email";
		err_str[i++] = "Epost";
		f.email.style.border = "2px solid #f00";
		lblEmail.style.color = "#f00";
	}
	if (phone.length < 5) {
		errors[i] = "phone";
		err_str[i++] = "Tel";
		f.phone.style.border = "2px solid #f00";
		lblPhone.style.color = "#f00";
	}
	if (!f.conditions_approved.checked) {
		errors[i] = "conditions_approved";
		err_str[i++] = "Godkänn villkoren";
		cond_approved.style.color = "#f00";
	}
	
	if (errors.length > 0) {
		if (document.getElementById) {
			var message = document.getElementById("message");
			message.innerHTML = "Vg kontrollera markerade fält:";
		} else {
			for (i = 0; i < errors.length; i++) {
				str += err_str[i] + "\n";
			}
			alert ("Vg kontrollera följande fält:\t\n\n" + str);
		}
		// Can not 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() {
	return confirm('Din vara läggs nu i varukorgen\n' +
	'När du handlat klart går du till kassan.');
}

function confirmLoginAsMember() {
	if (confirm("Du måste vara inloggad som medlem för att få tillgång till filen.\n" +
	"Vill du gå till inloggnings-sidan nu?")) {
		location.href='/login.php';
	}
}

/* Calendar */
function validateCalendar(f) {
	if (!is_date(f.datum.value)) {
		alert('Var god kontrollera datum...');
		f.datum.focus();
		f.datum.select();
		return false;
	} else if (f.time.value.length > 0 && !is_time(f.time.value)) {
		alert('Var god kontrollera tid...');
		f.time.focus();
		f.time.select();
		return false;
	} else if (f.event.value.length < 1) {
		alert('Var god skriv en händelse...');
		f.event.focus();
		return false;
	}
}
function validateDeleteCalendarEvent() {
	return confirm('Ta bort kalenderhändelse?');
}

/* News-letter subscription */
function validateSubscribe(f) {
	if (!is_email(f.mail.value)) {
		alert('Var vänlig kontrollera Epost.');
		f.mail.focus();
		return false;
	}
	if (!f.subscribe[0].checked && !f.subscribe[1].checked) {
		alert('Vänligen välj vad du vill göra.');
		return false;
	}
	return true;
}
