function openwindow(url, width, height) {
	// hack for IE 7
	if(navigator.appVersion.indexOf('MSIE 7') != -1) {
		height += 20;
	}

	xposition = 20; yposition = 20;
	if ((parseInt(navigator.appVersion) >= 4 )){
		xposition = (screen.width - width) / 2;
		yposition = (screen.height - height) / 2;
	}

	args = "width=" + width + ","
		    + "height=" + height + ","
			+ "location=0,"
			+ "menubar=0,"
			+ "resizable=0,"
			+ "scrollbars=no,"
			+ "status=0,"
			+ "titlebar=0,"
			+ "toolbar=0,"
			+ "hotkeys=0,"
			+ "screenx=" + xposition + ","  //NN Only
			+ "screeny=" + yposition + ","  //NN Only
			+ "left=" + xposition + ","     //IE Only
			+ "top=" + yposition;           //IE Only
	ww = window.open(url, '', args);
}



function addEvent(obj, sType, fn, rmFn)
{
	if(obj.addEventListener) {
		try
		{
			obj.removeEventListener(sType, rmFn, false);
		}
		catch(e)
		{
		}
		obj.addEventListener(sType, fn, false);
	}
	else if (obj.attachEvent)
	{
		try
		{
			var r = obj.detachEvent('on'+sType, rmFn);
		}
		catch(e)
		{
		}
		var r = obj.attachEvent('on'+sType, fn);
	}
	else
	{
		alert("Neimanoma prisieti veiksmo!");
	}
}

function removeEvent(obj, sType, fn) {
	if (obj.removeEventListener) {
		obj.removeEventListener(sType, fn, false);
	}
	else {
		obj.detachEvent('on' + sType, fn);
	}
}

function removeAllEditors() {
	for(i = 0; i < document.forms.length; i++) {
		if(document.forms[i] != null)
			for(j = 0; j < document.forms[i].elements.length; j++) {
				element = document.forms[i].elements[j];
				if (tinyMCE.getInstanceById(element.id) != null) {
					element.value = tinyMCE.getInstanceById(element.id).getHTML();
					tinyMCE.execCommand('mceRemoveControl', false, element.id);
				}
			}
	}
}

function toggleEditor(id) {
	var elm = document.getElementById(id);
	// submitinant tevine forma reikia nuimti redaktoriu
	if (tinyMCE.getInstanceById(id) == null) {
		$('.toggle-editor-on').hide();
		$('.toggle-editor-off').show();
		tinyMCE.execCommand('mceAddControl', false, id);
	}
	else {
		$('.toggle-editor-off').hide();
		$('.toggle-editor-on').show();
		tinyMCE.execCommand('mceRemoveControl', false, id);
	}
}

function money_format(num) {
	num = num.toString();
	has_dot = false;
	dot_pos = 0;

	for(i = 0; i < num.length; i++) {
		ch = num.charAt(i);
		if(ch == ".") {
			has_dot = true;
			dot_pos = i;
			break;
		}
	}

	if(has_dot) {
		if((dot_pos + 1) == num.length-1) {
			return (num.substr(0, dot_pos + 2) + "0");
		}
		else if(dot_pos == num.length-1) {
			return (num + "00");
		}
		else {
			return num.substr(0, dot_pos + 3);
		}
	}
	else {
		return num + ".00";
	}
	return false;
}

function showCost(price) {
    cost = parseFloat($('#total').html());
    cost += parseFloat(price);
    $('#total_with_delivery').html(money_format(cost));
    $('#hidden').show();
}

function submitFrm() {
    if($('#total_with_delivery').html() == '') { alert('Choose delivery type'); return false; }
    return true;
}