var imgs = new Array();
var errors = new Array();

if (window.location.href.indexOf('/en/') != -1) {
	errors[0] = 'Name is empty';
	errors[1] = 'E-mail is not valid';
	errors[2] = 'Customer no missing';
	errors[3] = 'First name missing';
	errors[4] = 'Last name missing';
	errors[5] = 'Street address missing';
	errors[6] = 'Zip code missing';
	errors[7] = 'City missing';
	errors[8] = 'Country missing';
	errors[9] = 'Tel.no missing';
	errors[10] = 'Email missing';
	errors[11] = 'Email has wrong format';
	errors[12] = 'Choose payment option';
}
else {
	errors[0] = 'Nimi on tyhjä';
	errors[1] = 'Virheellinen E-mail';
	errors[2] = 'Asiakas nro. puuttuu';
	errors[3] = 'Etunimi puuttuu';
	errors[4] = 'Sukunimi puuttuu';
	errors[5] = 'Katuosoite puuttuu';
	errors[6] = 'Postinumero puuttuu';
	errors[7] = 'Kaupunki puuttuu';
	errors[8] = 'Maa puuttuu';
	errors[9] = 'Puh.nro. puuttuu';
	errors[10] = 'E-mail osoite puuttuu tai se on tuntemattomassa muodossa';
	errors[11] = 'E-mail osoite puuttuu tai se on tuntemattomassa muodossa';
	errors[12] = 'Valitse maksutapavaihtoehto';
}
window.onload = function() {
	var items = document.getElementsByName('menu_item');
	for (var indx = 0; indx < items.length; indx++) {
		var img = items[indx];
		var id = img.getAttribute('id');
		
		var src = img.getAttribute('src').replace('.png', '-h.png');
		imgs[id] = new Array(img.src, new Image());
		imgs[id][1].src = src;
		
		img.onmouseover = function() {
			this.src = imgs[this.id][1].src;
		}
		
		img.onmouseout = function() {
			this.src = imgs[this.id][0];
		}
	}
}

function emailCheck(email) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(email);
}

function checkForm() {
	var err = [];
	if (getValue('name') == '') err[0] = errors[0];	
	if (emailCheck(getValue('email')) == false) err[1] = errors[1];
	
	if (showErr(err)) return false;
	return true;
}

function sendOrder() {
	var err = [];
	if (getValue('fname') == '') err[1] = errors[3];
	if (getValue('flast_name') == '') err[2] = errors[4];
	if (getValue('faddress') == '') err[3] = errors[5];
	if (getValue('fzipcode') == '') err[4] = errors[6];
	if (getValue('fcity') == '') err[5] = errors[7];
	if (getValue('fcountry') == '') err[6] = errors[8];
	if (getValue('fmobile') == '') err[7] = errors[9];
	if (getValue('femail') == '') err[8] = errors[10]; 
	else if (emailCheck(getValue('femail')) == false) err[9] = errors[11]; 
	
	if (!document.getElementById('opt1').checked && !document.getElementById('opt2').checked) err[9] = errors[12]; 
	
	var payment = 0;
	if (document.getElementById('opt1').checked) {
		document.getElementById('opt1').setAttribute('checked', 'checked');		
	}
	else if (document.getElementById('opt2').checked) {
		document.getElementById('opt2').setAttribute('checked', 'checked');
		payment = 1;
	}
	
	if (showErr(err)) return false;	
	
	var params = 'As.nro: ' + getValue('fnum') + '\n' +
				 'Etunimi: ' + getValue('fname') + '\n' +
				 'Sukunimi: ' +  getValue('flast_name')+ '\n' +
				 'Katuosoite: ' + getValue('faddress') + '\n' +
				 'Postinumero: ' + getValue('fzipcode') + '\n' +
				 'Postitoimipaikka: ' + getValue('fcity') + '\n' +
				 'Maa: ' + getValue('fcountry') + '\n' +
				 'Puh.nro: ' + getValue('fmobile') + '\n' +
				 'E-mail ' + getValue('femail') + '\n' +
				 'Maksuvaihtoehto: ' + (payment == 0 ? 'Postiennakko' : 'Ennakkomaksu sähköpostitse');
	
	params += '\n\n\n';
	
	for (var indx = 1; indx != 10; indx++) {
		params += 
		'Työn koodi: ' + getValue('val' + indx + '_1') + '\n' + 
		'Koko: ' + getValue('val' + indx + '_2') + '\n' +
		'Työn nimi: ' + getValue('val' + indx + '_3') + '\n' +
		'Hinta: ' + getValue('val' + indx + '_4') + '\n' +
		'Kpl: ' + getValue('val' + indx + '_5') + '\n\n\n';
	}
	
	params += 'Info: ' + getValue('info') + '\n\n\n';
	params += 'Summa: ' + getValue('total');
	
	var form = document.createElement('form');
	form.setAttribute('method', 'post');
	form.setAttribute('action', document.getElementById('orderform').getAttribute('action'));
	form.innerHTML = '<textarea name="order">'+params+'</textarea>';
	document.body.appendChild(form);
	form.submit();
}

function checkOrderBrochureForm() {
	var err = [];
	if (getValue('fname') == '') err[1] = errors[3];
	if (getValue('faddress') == '') err[3] = errors[5];
	if (getValue('fzipcode') == '') err[4] = errors[6];
	if (getValue('fcity') == '') err[5] = errors[7];
	if (getValue('fphone') == '') err[7] = errors[9];
	if (getValue('femail') == '') err[8] = errors[10]; 
	else if (emailCheck(getValue('femail')) == false) err[9] = errors[11]; 
	
	if (showErr(err)) return false;	
	return true;
}

function getValue(id) {
	return document.getElementById(id).value;
}

function showErr(err) {
	var str = '';
	for (var id in err) {
		if (str) str += '\n';
		str += err[id];
	}
	
	if (str) alert(str);
	return str!='';
}
