
function openConfigurator(fileName, windowName, windowWidth, windowHeight) {
	var windowParams = 'height=' + windowHeight + ',width=' + windowWidth + ',top=' + (screen.availHeight / 2 - windowHeight / 2) + ',left=' + (screen.availWidth / 2 - windowWidth / 2) + ',scrollbars=no';
	window.open(fileName, 'Konfigurator', windowParams);
}

function chooseZestaw(form) {
	if (form.value != "") 
		document.location = "http://chocolissimo.best.net.pl/do/cart?f=add&item=" + form.value;
}

function chocoMailing(url, name, width, height, obj) {
	email = obj.form.email.value;
	popup = window.open(url + "?email=" + email, name, 'height = ' + height + ', width = ' + width + ', top = ' + (screen.availHeight / 2 - height / 2) + ', left = ' + (screen.availWidth / 2 - width / 2));
	return false;
}

function subscriptionFill() {
	params = document.location.search;
	pos = params.indexOf("email=");
	document.subscription.email.value = params.substr(pos + 6);
}

function enableDeliveryElements() {
	document.getElementById('prop\(_delivery_name\)').disabled = false;
	document.getElementById('prop\(_delivery_companyName\)').disabled = false;
	document.getElementById('prop\(_delivery_street\)').disabled = false;
	document.getElementById('prop\(_delivery_postcode\)').disabled = false;
	document.getElementById('prop\(_delivery_city\)').disabled = false;
	document.getElementById('prop\(_delivery_country\)').disabled = false;
	document.getElementById('copyAddress').className = switchStyleClasses(document.getElementById('copyAddress').className, 'hide', 'show');
}

function disableDeliveryElements() {
	document.getElementById('prop\(_delivery_name\)').disabled = true;
	document.getElementById('prop\(_delivery_name\)').value = "";
	document.getElementById('prop\(_delivery_companyName\)').disabled = true;
	document.getElementById('prop\(_delivery_companyName\)').value = "";
	document.getElementById('prop\(_delivery_street\)').disabled = true;
	document.getElementById('prop\(_delivery_street\)').value = "";
	document.getElementById('prop\(_delivery_postcode\)').disabled = true;
	document.getElementById('prop\(_delivery_postcode\)').value = "";
	document.getElementById('prop\(_delivery_city\)').disabled = true;
	document.getElementById('prop\(_delivery_city\)').value = "";
	document.getElementById('prop\(_delivery_country\)').disabled = true;
	document.getElementById('prop\(_delivery_country\)').value = "pl";
	document.getElementById('copyAddress').className = switchStyleClasses(document.getElementById('copyAddress').className, 'hide', 'show');
}

function validateGraver(field) {
	var maxLength = 25;
	var f = document.getElementById(field);
	if (f.value.length > maxLength) {
		alert('Podany tekst do wygrawerowania jest za długi.\nMaksymalna długość to ' + maxLength + ' znaków.');
		return false;
	}
	if (!validCharacters(f.value)) {
		alert("W tekście zastosowane niedozwolone znaki.\nPoprawne znaki to: litery, cyfry oraz podstawowe znaki interpunkcyjne.");
		return false;
	}
	return true;
}

function validCharacters(s) {
	if (s.length == 0) 
		return true;
	var regexp = /^[- a-zA-Z0-9ąćęłńóśżźĄĆĘŁŃÓŚŻŹ<>?+=_'"\\!@#$%^&*(){}\[\]\.,]+$/;
	var reg = new RegExp(regexp);
	return reg.test(s);
}

function addChocotelegramToCart(type, value, packageColor) {
	var pckColor = document.getElementById(packageColor);
	if (pckColor.value != 'wood' && (pckColor.value == null || pckColor.value == '')) {
		alert('Wybierz preferowany kolor opakowania');
		return;
	}
	document.getElementById('chocoTelegramType').value = type;
	document.getElementById('chocoTelegramValue').value = value;
	document.getElementById('chocoTelegramPackageColor').value = pckColor.value;
	document.getElementById('chocoTelegramForm').submit();
}

function validateChocotelegramPackageColor(sField) {
	var oField = document.getElementById(sField);
	if (oField == null || oField.options[oField.selectedIndex].value == null || oField.options[oField.selectedIndex].value == "") {
		alert("Wybierz preferowany kolor opakowania");
		return false;
	}
	return true;
}

function chocoTelegram(chocoTelegramId) {
	window.open('/do/choco/chocotelegramConfigurator?type=' + chocoTelegramId, 'chocoTelegram', 'width = 750, height = 450, top = ' + (screen.availHeight / 2 - 450 / 2) + ', left = ' + (screen.availWidth / 2 - 750 / 2) + ', scrollbars = no');
}

function pralinesVoting() {
	window.open('/do/choco/pralinesVoting', 'pralinesVoting', 'height = 565, width = 750, top = ' + (screen.availHeight / 2 - 565 / 2) + ', left = ' + (screen.availWidth / 2 - 750 / 2));
}

function removeReminders() {
	var sequence = generateObjectsSequence('reminders', ',');
	document.getElementById('ids').value = sequence;
	document.getElementById('remindersRemove').submit();
}

function removeReminder(id) {
	document.getElementById('ids').value = id;
	document.getElementById('remindersRemove').submit();
}

function useCommonDate(commonDate, commonDateName) {
	document.getElementById('date').value = commonDate;
	document.getElementById('name').value = commonDateName;
	document.getElementById('periodic').checked = true;
}

function ShowPralines(pralineId) {
	openNewWin('do/page/showpralines.jsp?type=' + pralineId, 'ShowPralines', 570, 410);
}

function checkPaymentType(submit, differ) {
	var payments = document.getElementsByName("payment");
	var selected = -1;
	for (var i = 0; i < payments.length; i++) 
		if (payments[i].checked == true) 
			selected = i;
	if (selected == -1 || payments[selected].value != "1") 
		return true;
	if (!differ) 
		return true;
	if (submit == true) 
		return confirm("Uwaga!\nAdres dostawy różni się od adresu zamawiającego - wybrałeś opcję Płatność za pobraniem - odbiorca przesyłki będzie musiał za nią zapłacić.\nKliknij OK jeśli akceptujesz lub Anuluj jeśli chcesz to poprawić.");
	else alert("Uwaga!\nAdres dostawy różni się od adresu zamawiającego - wybrałeś opcję Płatność za pobraniem - odbiorca przesyłki będzie musiał za nią zapłacić.");
}

function submitOrderForm(submit, differ, obj) {
	var form = document.getElementById("orderForm");
	if (checkPaymentType(submit, differ) == true) {
		if (obj.form.entryId) 
			obj.form.action = 'do/addressBook/orderCheck';
		form.submit()
	} else 		
		return;
}

function handleEvent(e) {
	var target;
	if (ie) 
		target = e.srcElement;
	else target = e.target;
	var form = target.form;
	if (e.keyCode == 13) {
		if (e.type != "keyup") 
			return false;
		for (var i = 0; i < form.elements.length - 1; i++) {
			if (form.elements[i] == target) {
				form.elements[i + 1].focus();
				return false;
			}
		}
		return false;
	}
	if (target.value.length >= 21) {
		target.value = target.value.substring(0, 21);
		if (allowable(e.keyCode)) 
			return true;
		alert("Podany tekst jest za długi.\nW jednej linii może znajdować się max. 21 znaków.");
		return false;
	}
	return true;
}

function allowable(keyCode) {
	return keyCode == 8 || keyCode == 16 || keyCode == 35 || keyCode == 36 || keyCode == 37 || keyCode == 38 || keyCode == 39 || keyCode == 40 || keyCode == 45 || keyCode == 46;
}

function commentLength(e) {
	var target;
	if (ie) 
		target = e.srcElement;
	else target = e.target;
	if (target.value.length >= 300) {
		if (allowable(e.keyCode)) 
			return true;
		alert("Podany tekst jest za długi.\nMaksymalna długość komentarza to 300 znaków.");
		return false;
	}
}

function selectProperValue(id) {
	if (id == null || id.length == 0) 
		return;
	var radios = document.getElementsByName("delivery");
	for (var i = 0; i < radios.length; i++) 
		if (radios[i].value == id && radios[i].disabled == false) 
			radios[i].checked = true;
}

function chocoAlert(msg) {
	var alert = document.getElementById("alert");
	if (alert != null) {
		alert.style.display = "block";
		alert.blur();
		return;
	}
	var screenWidth = screen.width;
	var screenHeight = screen.height;
	var destWidth = 300;
	var destHeight = 120;
	var left = (screenWidth - destWidth) / 2;
	var top = (screenHeight - destHeight) / 2;
	var style = "display: block; ";
	style += "width: " + destWidth + "px; ";
	style += "height: " + destHeight + "px; ";
	style += "top: " + top + "px; ";
	style += "left: " + left + "px;";
	var div = document.createElement("div");
	div.setAttribute("class", "alert");
	div.setAttribute("id", "alert");
	div.setAttribute("style", style);
	var message = document.createTextNode(msg);
	var br = document.createElement("br");
	var input = document.createElement("input");
	input.setAttribute("type", "button");
	input.setAttribute("class", "button");
	input.setAttribute("value", "OK");
	input.setAttribute("onclick", "hideChocoAlert();");
	var p = document.createElement("p");
	p.appendChild(message);
	p.appendChild(br);
	p.appendChild(input);
	div.appendChild(p);
	var body = document.getElementsByTagName("body")[0];
	body.appendChild(div);
	div.blur();
}

function hideChocoAlert() {
	var alert = document.getElementById("alert");
	if (alert != null) 
		alert.style.display = "none";
}

function openNewScrollableWindow(fileName, windowName, windowWidth, windowHeight) {
	popup = window.open(shopBase + fileName, windowName, 'height = ' + windowHeight + ', width = ' + windowWidth + ', top = ' + (screen.availHeight / 2 - windowHeight / 2) + ', left = ' + (screen.availWidth / 2 - windowWidth / 2) + ', scrollbars = yes');
}

function show(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display = 'block';
	}
}

function hide(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display = 'none';
	}
}

function toggle(id) {
	var dest = document.getElementById(id);
	if (dest == null) 
		return;
	var list = getElementsByClassName(document, "div", "toggled");
	for (var i = 0; i < list.length; i++) 
		list[i].style.display = 'none';
	dest.style.display = 'block';
}

function runSetsConfigurator(fileName, windowName, windowWidth, windowHeight) {
	var params = 'width = ' + windowWidth + ', height = ' + windowHeight + ', top = ' + (screen.availHeight / 2 - windowHeight / 2) + ', left = ' + (screen.availWidth / 2 - windowWidth / 2);
	var setsConfigurator = window.open(fileName, windowName, params);
}

function openLink(linkwww) {
	opener.location.href = linkwww;
	window.close();
	opener.focus();
}

function winclose() {
	window.close();
}

function performChocoTelegram(type, value, packageColor) {
	window.opener.document.getElementById("chocoTelegramType").value = type;
	window.opener.document.getElementById("chocoTelegramValue").value = value;
	window.opener.document.getElementById("chocoTelegramPackageColor").value = packageColor;
	window.opener.document.getElementById("chocoTelegramForm").submit();
	window.close();
	opener.focus();
}

function mailing(sFunc) {
	if (document.getElementById("mem")) 
		var email = document.getElementById("mem").value;
	switch (sFunc) {
		case "remove":
			openNewWindow("/do/choco/mailingForm?action=remove&email=" + email, "mailing", 320, 220);
			return;default:
			openNewWindow("/do/choco/mailingForm?action=add&email=" + email, "mailing", 460, 280);
	}
}

function openNewWindow(file, name, width, height, scrollable) {
	var params = "width = " + width +
	", height = " +
	height +
	", left = " +
	(screen.availWidth / 2 - width / 2) +
	", top = " +
	(screen.availHeight / 2 - height / 2);
	if (scrollable && scrollable == true) 
		params += ", scrollbars = yes";
	var popup = window.open(file, name, params);
}

function chToggleMenu(sMenuItem) {
	var oMenuItem = document.getElementById(sMenuItem);
	var oSelectedLi = document.getElementById(sMenuItem + "_li");
	var aLevs = getElementsByClassName(document, "ul", "lev_2");
	var aLevLis = getElementsByClassName(document, "li", "lev2_li");
	if (aLevs && aLevLis && oMenuItem) {
		for (var i = 0; i < aLevs.length; i++) 
			aLevs[i].style.display = "none";
		for (var i = 0; i < aLevLis.length; i++) 
			aLevLis[i].className = "lev2_li";
		oMenuItem.style.display = "block";
		oSelectedLi.className = "lev2_li selected";
	}
}

function ct(ctSymbol) {
	window.open("/do/choco/chocotelegramConfigurator?ctSymbol=" + ctSymbol, "ct", "width = 950, height = 650, top = " + (screen.availHeight / 2 - 650 / 2) + ", left = " + (screen.availWidth / 2 - 850 / 2) + ", scrollbars = no");
}

function ctAdd(ctSymbol, ctValue) {
	window.opener.document.getElementById("ctItem").value = ctSymbol;
	window.opener.document.getElementById("ctValue").value = ctValue;
	window.opener.document.getElementById("ctForm").submit();
	window.close();
	opener.focus();
}

function passParams(sFirstName, sLastName, sCompanyName, sStreet, sPostcode, sCity, sCountry, sPhone) {
	document.getElementById('prop\(_delivery_firstName\)').value = sFirstName;
	document.getElementById('prop\(_delivery_lastName\)').value = sLastName;
	document.getElementById('prop\(_delivery_companyName\)').value = sCompanyName;
	document.getElementById('prop\(_delivery_street\)').value = sStreet;
	document.getElementById('prop\(_delivery_postcode\)').value = sPostcode;
	document.getElementById('prop\(_delivery_city\)').value = sCity;
	document.getElementById('prop\(_delivery_country\)').value = sCountry;
	document.getElementById('prop\(delivery_phone\)').value = sPhone;
}
