/* $Id: blomsterhallen.js,v 1.4 2009-07-16 16:56:05 tommy Exp $ */

window.onload = function() {
	if($('weekday') != null) {
		update_weekday();
	}
}

/**
 * Updates the weekday field for a given date with the day name.
 */
function update_weekday() {
	var weekdays = ['Søndag', 'Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lørdag'];

	var dateObj = new Date();
	dateObj.setFullYear($F('year'), $F('month')-1, $F('day'));
	
	if($F('day') > 1 && dateObj.getDate() == 1) {
		$('day').value = dateObj.getDate();
		$('month').value = dateObj.getMonth()+1;
	}

	$('weekday').value = weekdays[dateObj.getDay()];
}

function validate_checkout() {
	var form = $('checkout');
	var errors = '';

	if(!form['time'].value.match(/\d{2}:\d{2}/)) {
		errors += '- Tidspunktet for levering skal udfyldes i formatet TT:MM, fx 10:30\n';
	}

	// purchaser
	errors += is_field_empty(form['from_name'])					? '- Betalers navn\n' : '';
	errors += is_field_empty(form['from_street'])				? '- Betalers gade\n' : '';
	errors += is_field_empty(form['from_zip'])					? '- Betalers postnummer\n' : '';
	errors += is_field_empty(form['from_city'])					? '- Betalers by\n' : '';
	errors += is_field_empty(form['from_country'])			? '- Betalers land\n' : '';
	errors += is_field_empty(form['from_phonenumber'])	? '- Betalers telefonnummer\n' : '';

	if($('newsletter') && $('newsletter').checked && is_field_empty(form['from_email'])) {
		errors += '- Betalers emailadresse skal oplyses for at modtage nyhedsbrevet\n';
	}

	// recipient
	if($('delivery_other').checked) {
		errors += is_field_empty(form['to_name'])						? '- Modtagers navn\n' : '';
		errors += is_field_empty(form['to_street'])					? '- Modtagers gade\n' : '';
		errors += is_field_empty(form['to_zip'])						? '- Modtagers postnummer\n' : '';
		errors += is_field_empty(form['to_city'])						? '- Modtagers by\n' : '';
		errors += is_field_empty(form['to_country'])				? '- Modtagers land\n' : '';
	}

	if(errors != '') {
		alert('Følgende felter mangler eller er ikke korrekt udfyldt:\n\n' + errors);
		return false;
	}

	return true;
}

function is_field_empty(field) {
	return field.type != 'hidden' && field.value == '';
}

/**
 * Opens a new window for the DIBS payment process.
 */
function dibs_payment() {
	var paywin = window.open('', 'paywin', 'scrollbars, status, width=550, height=600');
	paywin.focus();
	return true;
}

/**
 * Returns from the DIBS payment process.
 */
function dibs_payment_finish(url) {
	window.opener.location.href = url;
	window.close();
}

function show_business_login() {
	win = window.open('shop_business.php', 'business_login', 'height=220, width=400');
}

function perform_business_login() {
	var url = 'shop_category.php?do=login&amp;username=' + $F('username') + '&amp;password=' + $F('password');
	window.opener.location.href = url;
	window.close();
}

function show_contact() {
	window.opener.location.href = 'contact.php';
	window.close();
}

/* --- */

function show_preview(id) {
//	window.open('shop.php?do=preview&itemid=' + id, 'preview', 'height=400, width=400');
	window.open('images/item/' + id + '.jpg', 'preview', 'height=400, width=400');
}






