function addToCompare(elm){
	var http_request = false;
	if (window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
	}else {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!http_request){
		alert('Error XMLHTTP');
		return false;
	}
	http_request.open('GET', '/backend/addtocompare.php'+'?id='+elm, true);
	http_request.send(null);
	return true;
}

function addToBasket(elm){
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		var http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		var http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!http_request) {
		alert('Error XMLHTTP');
		return false;
	} else {
		//var elm = document.getElementById('num'+id);
		http_request.onreadystatechange = function(){
			if (http_request.readyState !== 4) {
				return;//not ready yet
			}
			if (http_request.status !== 200){
				return;
			}

			var xmldoc = http_request.responseXML;
			window.location.reload();
			//var id = xmldoc.getElementsByTagName("uid").item(0).data;
			//var ok = xmldoc.getElementsByTagName('ok').item(0).firstChild.data;
			//if (parseInt(ok)>0){
			//	$("#i"+elm).attr("value",parseInt(ok));
			//}else{
			//	$("#i"+elm).attr('value','0');
			//}
			//$("#bnum").html(xmldoc.getElementsByTagName('num').item(0).firstChild.data);
			//$("#bsum").html(xmldoc.getElementsByTagName('price').item(0).firstChild.data);
		};
		http_request.open('GET', '/backend/addtobasket.php'+'?amount='+$("#i"+elm).attr('value')+'&elm='+elm, true);
		http_request.send(null);
	}
	return false;
//	$.post('/backend/addtobasket.php',{amount: $("#i"+elm).attr('value'),elm: elm});
//	window.location.reload(true);
//	return 0;
}

function spinAdd(elm){
	var tmp = parseInt($("#"+elm).attr('value'));
	$("#"+elm).attr('value',tmp+1);
	return false;
}

function spinSub(elm){
	var tmp = parseInt($("#"+elm).attr('value'));
	if (tmp>0){
		$("#"+elm).attr('value',tmp-1);
	}else{
		$("#"+elm).attr('value',0);
	}
	return false;
}

function nonSubm(v){
	if (v==13){
		return false;
	}
	return true;
}
function confirmRequest(clink, msg)
{
	if (typeof(window.opera) != 'undefined' && parseFloat(navigator.appVersion) < 8) {
		/*clink.href += '&is_confirmed=1';*/
		return true;
	}
	var is_confirmed = confirm("Вы завершили формирование заказа и готовы оформить его?:\n");
	/*if (is_confirmed) {
		clink.href += '&is_confirmed=1';
	}*/
	return is_confirmed;
}

function goCompare(){
	myWin = window.open('compare.php?'+$("#catalog_form").serialize());
}

function goTo(elm){
	//var loc =;
	window.location.href=$(elm).attr('value');
}
/* see http://fancy.klade.lv/howto */
$(document).ready(function() {
	$("#comparebutton").colorbox({href:'/catalog/compare.php'});
	$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': false });
	//htmltooltip.render();
	$('#offer_req_button').click(function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		jConfirm(
			'Вы уверены, что ты хотите отправить заказ?',
			'Подтверждение заказа',
			function(b){
				if (b){
					$('#id_cp_button').attr('value','1');
					$('#basketform').submit();
				}
			}
		);
	});
});

function sumChange(id) {
	var pattern = /\d*.\d?\d?/;
	var amount_field = document.getElementById('i'+id);
	var price_field = document.getElementById('price'+id);
	price_field.value = price_field.innerHTML.replace(',', '.');
	var sum_field = document.getElementById('sum'+id);
	if (!isNaN(amount_field.value) > 0 && !isNaN(price_field.value) > 0) {
		var tmp = price_field.value * amount_field.value;
		tmp = pattern.exec(tmp);
		if (tmp*100%100==0){
			tmp = ''+tmp+'.00';
		}else if(tmp*100%10==0){
			tmp = ''+tmp+'0';
		}
		sum_field.innerHTML = tmp;
	}
	if (sum_field.innerHTML == 0) {
		sum_field.innerHTML = '';
	}
	var fs = 0;
	$("div[@id^=sum]").each(function(){	fs += parseFloat(this.innerHTML); });
  fs = pattern.exec(fs);
	if (fs*100%100==0){
		fs = ''+fs+'.00';
	}else if(tmp*100%10==0){
		fs = ''+fs+'0';
	}
	document.getElementById('field_sum').innerHTML = fs;

}

function formSubmit(v){
	if (v==1){
		document.getElementById('id_cert').value = 1;
	} else if (v==2){
		document.getElementById('id_delete').value = 1;
	}

	document.getElementById('basketform').submit();
}