var ie = navigator.appName.indexOf('Microsoft') != -1;

function newWin(nazwa,szer,wys) {
	odlewa=(screen.width-szer)/2;
	odgory=(screen.height-wys)/2;
	szer = szer+28;
	wys = wys+28;
	config='left='+odlewa+',top='+odgory+',width='+szer+',height='+wys+',innerheight='+wys+',innerwidth='+szer+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no';
	cel=window.open('','okno',config)
	cel.document.write('<HTML><HEAD>');
	cel.document.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">');
	cel.document.write('<TITLE></title>');
	cel.document.write('</HEAD>');
	cel.document.write('<body  leftmargin=10 topmargin=14 marginheight=10 marginwidth=10  style="background-image: url(/gfx/baccara.gif);background-position: center center; background-repeat: no-repeat; text-align:center;">');
	cel.document.write('<a href=# onclick="javascript:self.close();"><img src='+nazwa+' class=obw alt="Kliknij aby zamkn?? okno" style="border: 1px solid #000;" align=center></a>');
	cel.document.write('</body></html>');
	cel.focus();
}

function newhtm(nazwa,szer,wys) {
	odlewa=(screen.width-szer)/2;
	odgory=(screen.height-wys)/2;		
	config='left='+odlewa+',top='+odgory+',width='+szer+',height='+wys+',innerheight='+wys+',innerwidth='+szer+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes';
	pralines=window.open(nazwa,'okno',config)
	pralines.focus();
}

function openNewWindow(fileName, windowName, windowWidth, windowHeight, parameter) {
	if (ie) {
		windowWidth += 20;
		windowHeight += 30;
	}
	popup = window.open(shopBase + fileName, windowName, 'height = ' + windowHeight + ', width = ' + windowWidth + ', top = ' + (screen.availHeight / 2 - windowHeight / 2) + ', left = ' + (screen.availWidth / 2 - windowWidth / 2) + ', scrollbars = yes');
	popup.param = parameter;
}

function openNewScrollableWindow(fileName, windowName, windowHeight, windowWidth) {
	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 openNewBlankWindow(fileName, windowName, windowHeight, windowWidth, setId) {
	popup = window.open(fileName + "?setId=" + setId, windowName, 'height = ' + windowHeight + ', width = ' + windowWidth + ', top = ' + (screen.availHeight / 2 - windowHeight / 2) + ', left = ' + (screen.availWidth / 2 - windowWidth / 2));
}

/* passes parameters inside order form */
/*	param1 - imie i nazwisko
	param2 - nazwa firmy
	param3 - ulica z numerem
	param4 - kod pocztowy
	param5 - miasto
	param6 - panstwo
*/
function passParams(param1, param2, param3, param4, param5, param6, param7) {
	document.getElementById('prop\(_delivery_name\)').value = param1;
	document.getElementById('prop\(_delivery_companyName\)').value = param2;
	document.getElementById('prop\(_delivery_street\)').value = param3;
	document.getElementById('prop\(_delivery_postcode\)').value = param4;
	document.getElementById('prop\(_delivery_city\)').value = param5;
	document.getElementById('prop\(_delivery_country\)').value = param6;
	document.getElementById('prop\(delivery_phone\)').value = param7;
}

function waitAndForward(seconds) {
	setTimeout('window.location="/do/index.jsp"',seconds);
}

function changeElementDisplay(elementName) {
	document.getElementById(elementName).className = switchStyleClasses(document.getElementById(elementName).className, 'hide', 'show');
}

function switchStyleClasses(actualClassName, firstClass, secondClass) {
	var classes = new Array();
	classes = actualClassName.split(' ');
	for (var i = 0; i < classes.length; i++) {
		if (classes[i] == firstClass)
			classes[i] = secondClass;
		else {
			if (classes[i] == secondClass)
				classes[i] = firstClass;
		}
	}
	
	var newClassName = '';
	for (var i = 0; i < classes.length; i++) {
		if (i == classes.length - 1)
			newClassName += classes[i];
		else
			newClassName += classes[i] + ' ';
	}
	
	return newClassName;
}

function toTop() {
	window.scroll(0, 0);
}

/*
	This function generates list of objects's values that were selected by user (using
	checkboxes). As a list separator is used given sequenceSeparator.
*/
function generateObjectsSequence(objectsName, sequenceSeparator) {
	var objects = new Array();
	
	for (var i = 0; i < document.getElementsByName(objectsName).length; i++) {
		if (document.getElementsByName(objectsName)[i].checked == true)
			objects[objects.length] = document.getElementsByName(objectsName)[i].value;
	}
	
	if (objects.length > 0) {
		var objectsSequence = '';
		for (var i = 0; i < objects.length; i++) {
			objectsSequence += objects[i];
			if ((i + 1) < objects.length)
				objectsSequence += sequenceSeparator;
		}
		return objectsSequence;
	}
	return '';
}

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

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

function submitForm(form) {
	var form = document.getElementById(form);

	try {
		form.submit();
	} catch (err) {
		alert("Wybrany obiekt nie jest formularzem.");
	}
}

function refreshCart(price, id) {
	var src = document.getElementsByName('quantities[' + id + ']')[0];
	var target = document.getElementById('cartItemValue[' + id + ']');
	
	var sum = Math.round(src.value * price * 100.00) / 100.00;
	target.innerHTML = sum.toFixed(2);

	var sum = 0;
	var amounts = document.getElementsByName('cartItemValue');
	
	for (var i = 0; i < amounts.length; i++)
		sum += Math.round(amounts[i].innerHTML * 100) / 100;
	
	document.getElementById('cartSum').innerHTML = sum.toFixed(2);
}

function onlyDigits(e) {
	var key;
	var keychar;
	
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27))
		return true;
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function cartInc(field) {
	var value = parseInt(document.getElementById(field).value);
	if (typeof value == "number")
		value++;
	document.getElementById(field).value = value;
}

function cartDec(field) {
	var value = parseInt(document.getElementById(field).value);
	if (typeof value == "number" && value > 0)
		value--;
	document.getElementById(field).value = value;
}

function getElementsByClassName(className) {
	var result = new Array();
	var regexp = new RegExp("\\b" + className + "\\b");
	var elements = document.getElementsByTagName("*");
	for (var i = 0; i < elements.length; i++)
		if (regexp.test(elements[i].className))
			result.push(elements[i]);

	return result;
}

function nopl(s) {
	var polish = "ąćęłńóśżź";
	var nonPolish = "acelnoszz";
	
	for (var i = 0; i < polish.length; i++)
		s = replace(s, polish.charAt(i), nonPolish.charAt(i));
	
	return s;
}
					
function replace(s, arg0, arg1) {
	for (var i = 0; i < s.length; i++)
		if (s.charAt(i) == arg0)
			s = s.substring(0, i) + arg1 + s.substring(i + 1, s.length);

	return s;
}