function addToNegotiation (id, domain, optMsg, quaMsg, nooptMsg)
{
	sels = $$('#shop_options select');
	optId = 'opt_id';
	err = false;
	var i_el = 0;
	var arr_option = new Array();

	sels.each (function (val) {
			if(val.value!=0){
				arr_option[i_el] = val.value;
				i_el++;
			}
			else{
				err = true;
			}
		});
	var arr_sort_option = arr_option.sort(sortNumber);
		arr_sort_option.each (function (val) {
				optId+='_'+val;
		});

	if (err){
		alert(optMsg);
		return;
	}

	if(!$(optId) && nooptMsg){
		alert(nooptMsg);
		return;
		}

	if (parseInt($('quantity').value) < 1) {
		alert ($('quantity').value);
		alert (parseInt($('quantity').value));
		alert (quaMsg);
		return;
	}
	var quantity = parseInt($('quantity').value);
	optStr = '';
	if ($(optId)) {
		optStr = '&opt=' + $(optId).value;
	}

	url = domain + '/negotiations/?a=add&id='+id+'&q='+quantity+optStr;
	var myAjax = new Request({
		method: 'get',
		url: url,
		onSuccess: function(obj){ ansAddToNegotiation(obj,domain); }
	}).send();
	showPreloader();
}

function addToNegotiationFlash (id, domain, optionString, quantity, optMsg, quaMsg, nooptMsg)
{
	alert(optionString);

	var optId = 'opt_id';
	var optStr = '';
	var arr_option = new Array();
	var arr_sort_option = new Array();

	arr_option = optionString.split(" ");
	arr_sort_option = arr_option.sort(sortNumber);

	arr_sort_option.each (function (val) {
			optId+='_'+val;
	});

	if(!$(optId) && nooptMsg){
		alert(nooptMsg);
		return;
		}

	if (parseInt(quantity) < 1) {
		alert (quaMsg);
		return;
	}

	optStr = '';
	if ($(optId)) {
		optStr = '&opt=' + $(optId).value;
	}

	url = domain + '/negotiations/?a=add&id='+id+'&q='+parseInt(quantity)+optStr;
	var myAjax = new Request({
		method: 'get',
		url: url,
		onSuccess: ansAddToNegotiation
	}).send();
}

function addFreeItem(id, domain, parent)
{
	url = domain + '/negotiations/?a=add_free&id='+id+'&parent='+parent;
	var myAjax = new Request({
		method: 'get',
		url: url,
		onSuccess: ansAddFreeItem
	}).send();
	showPreloader();
}

function removeNegotiationItem(id, domain)
{
	url = domain + '/negotiations/?a=remove&id='+id;
	var myAjax = new Request({
		method: 'get',
		url: url,
		onSuccess: ansRemoveNegotiationItem
	}).send();
	showPreloader();

}

function ansRemoveNegotiationItem(res)
{
	hidePreloader();
	ret = JSON.decode (res);
	if (ret['msg']) {
		alert (ret['msg']);
	}
	if (ret['success'] == 1) {
		window.location.reload();
	}
}

function ansAddFreeItem(res)
{
	hidePreloader();
	ret = JSON.decode (res);
	if (ret['msg']) {
		alert (ret['msg']);
	}
	if (ret['success'] == 1) {
		window.location.reload();
	}
}

function ansAddToNegotiation(res,domain)
{
	//hidePreloader();
	ret = JSON.decode (res);

	if (ret['success'] == 1) {
		var myAjax = new Request({
			method: 'get',
			url: domain + '/negotiations/?a=short',
			onSuccess: function(obj) {
				document.getElementById('simple_negotiations').innerHTML = obj;
			}
		}).send();
		//showPreloader();
		//window.location.reload();
	}
	if (ret['msg']) {
		alert (ret['msg']);
	}
	hidePreloader();
}

function negotiationSend()
{
	if($('new_price').value=='' || $('new_price').value<=0 || $('new_price').value=='0,00'){alert('Kwota negocjacji musi by� wi�ksza od zera !');return false;}
	$('negotiation_form').operation.value = 'negotiation_send';
	$('negotiation_form').submit();
}

function negotiationAccept()
{
	$('negotiation_form').operation.value = 'negotiation_accept';
	$('negotiation_form').submit();
}


