$(document).ready(function() {
	$("#filedel").click(function(){
		$.ajax({
			type: "POST",
			data: [{"name":"action", "value":"filedel"}],
		    url: '/modules/filedel.php',
			dataType: "html",
		    success: function(answ){
		    	$("#filerek").remove();
		    }
	    });
		return false;
		
	});
	$('.feedback_del').live('click', function(){
		var id = $(this).attr("id").replace("del","");
		$.ajax({
			type: "POST",
			data: [{"name":"action", "value":"proddel"},{"name":"id","value":id}],
		    url: '/modules/filedel.php',
			dataType: "html",
		    success: function(answ){
		    	$("#product"+id).remove();
		    	$("#productMess"+id).remove();
		    }
	    });
		return false;
	});
	
	$('#feedback_btn_ok').click(function(){
		var prod = $("input.qtyProducts");
		//alert(prod.length);
		var flag = true;
		if (prod.length>0) {
			for (i=0;i<prod.length;i++) {
				valQ = $(prod[i]).val().replace(",",".");
				valQ = valQ*1;
				valQty = $(prod[i]).attr("qty")*1;
				meas = $(prod[i]).attr("meas");
				//alert(valQ+'  '+prod.val());
				num = $(prod[i]).parent(".endproduct").attr("id").replace("endproduct","");
				//alert(valQ);
				if (valQ == parseFloat(valQ)) {
					//alert(valQ);
					var mod = fmod(valQ,valQty);
					if (valQ==0) {
						mod = 100500;
					}
					if (mod != 0)
					{
						//element.className = "short_inp invalid";
						flag = false;
						$(prod[i]).css("background", "#de7878");
						$("#productMess" + num).html('Минимальная партия '+ valQty + ' ' + meas + '. Общее количество должно быть кратно '+valQty+''+meas);
						//arrayBlocked[div] = 1;
					}
					else
					{
						//element.className = "short_inp";
						$(prod[i]).css("background", "#fff");
						$("#productMess" + num).html("");
						//arrayBlocked[div] = 0;
					}
				} else {
					//element.className = "short_inp invalid";
					flag = false;
					$(prod[i]).css("background", "#de7878");
					$("#productMess" + num).html('Минимальная партия '+ valQty + ' ' + meas + '. Общее количество должно быть кратно '+valQty+''+meas);
					//arrayBlocked[div] = 1;
				}
				/*
				if (isNaN(valQ)) {
					flag = false;
				}
				*/
			}
		} else {
			$("#total-error").text('Выберите товар');
			return false;
		}
		//alert(flag);
		if (!flag) {
			//$("#total-error").text('Укажите верное количество.');
			//alert('1');
			return false;
		} else {
			//alert('2');
			$("#total-error").text("");
		}
	});
	
	$('input.qtyProducts').live("keyup",function(){
		valQ = $(this).val().replace(",",".");
		valQ = valQ*1;
		valQty = $(this).attr("qty")*1;
		meas = $(this).attr("meas");
		var flag = true;
		var i = 0;
		i = $(this).parent(".endproduct").attr("id").replace("endproduct","");
		//alert(valQ+' '+parseFloat(valQty)+' '+(parseFloat(valQ) % parseFloat(valQty)));
		if (valQ == parseFloat(valQ)) {
			//var mod = valQ % valQty;
			var mod = fmod(valQ,valQty);
			//alert(valQ+' '+' '+valQty+' '+mod);
			//mod = parseInt(mod);
			if (mod != 0)
			{
				//element.className = "short_inp invalid";
				flag = false;
				$(this).css("background", "#de7878");
				$("#productMess" + i).html('Минимальная партия '+ valQty + ' ' + meas + '. Общее количество должно быть кратно '+valQty+''+meas);
				//arrayBlocked[div] = 1;
			}
			else
			{
				//element.className = "short_inp";
				$(this).css("background", "#fff");
				$("#productMess" + i).html("");
				//arrayBlocked[div] = 0;
			}
		} else {
			//element.className = "short_inp invalid";
			flag = false;
			$(this).css("background", "#de7878");
			$("#productMess" + i).html('Минимальная партия '+ valQty + ' ' + meas + '. Общее количество должно быть кратно '+valQty+''+meas);
			//arrayBlocked[div] = 1;
		}
	});
	//alert(fmod(3670,36.7));
	//alert(parseFloat(99.9999999999999999))
});
function fmod(a, b){
	var x = Math.round(a/b);
	//alert(a/b);
	//alert(x+' '+b*x+' '+(b*x).toFixed(2));
	return a - (b*x).toFixed(2)*1;
	}
/*
function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}
*/
/*
function isDisabled()
{
	//
	var disabled = false;
	for ( i in arrayBlocked ) {
		if (arrayBlocked[i] == 1)
		{
			disabled = true;
		}
	}
	if (disabled)
	{
		$('#feedback_btn_ok').attr('disabled', 'true');
	}
	else
	{
		$('#feedback_btn_ok').removeAttr('disabled');
	}
}
function valide(element, value, div, text)
{
	var val = new String(element.value);
	if (val != "0")
	{
		val = str_replace(",", ".", val);
		if (val == parseFloat(val))
		{
			var ostatok = val % value;
			if (ostatok != "0")
			{
				//element.className = "short_inp invalid";
				$(element).css("background", "#de7878");
				$("#productMess" + div).html(text);
				arrayBlocked[div] = 1;
			}
			else
			{
				//element.className = "short_inp";
				$(element).css("background", "#fff");
				$("#productMess" + div).html("");
				arrayBlocked[div] = 0;
			}
		}
		else
		{
			//element.className = "short_inp invalid";
			$(element).css("background", "#de7878");
			$("#productMess" + div).html(text);
			arrayBlocked[div] = 1;
		}
	}
	else
	{
		//element.className = "short_inp invalid";
		$(element).css("background", "#de7878");
		$("#productMess" + div).html(text);
		arrayBlocked[div] = 1;
	}
	isDisabled();
}
*/
function getEndProduct(thisid, id, div, current)
{
	if (products[id][thisid])
	{
		var text = '<input type="text" name="qty[' + div + ']" qty="' + products[id][thisid][2] + '" meas="'+products[id][thisid][1]+'" class="qtyProducts invalid"'; //onKeyUp="valide(this, ' + products[id][thisid][2] + ', ' + div + ', \'Минимальная партия '+ products[id][thisid][2] + ' ' + products[id][thisid][1] + '. Общее количество должно быть кратно ' + products[id][thisid][2] + ' ' + products[id][thisid][1] + '.\');"
		if (current != -1)
			text += ' value="' + current + '"';
		text += '/><span class="params">От ' + products[id][thisid][2] + ' ' + products[id][thisid][1] + ', и кратна ' + products[id][thisid][2] + ' ' + products[id][thisid][1] + '.</span><input type="button" id="del'+div+'" class="feedback_del">';
	}
	else
	{
		var text = '';
	}
	$("#endproduct" + div).html(text);
	$("#productMess" + div).html("");
}

function getProducts(id, div, current)
{
	arrayBlocked[div] = 1;
	if (products[id])
	{
		var text = '<select name="product[' + div + ']" onChange="getEndProduct(this.value, ' + id + ', ' + div + ', -1 ); $(this).css(\'width\',\'192px\');" class="classProducts" onMouseDown="$(this).css(\'width\',\'auto\');" onBlur="$(this).css(\'width\',\'192px\');"><option value="-1">Выберите</option>'; onBlur="$(this).css(\'width\',\'192px\');"
		for ( i in products[id] ) {
			text += '<option';
			if (i == current)
			{
				text += ' selected';
			}
			text += ' value="' + i + '">' + products[id][i][0] + '</option>';
		}
		text += '</select><div id="endproduct' + div + '" class="endproduct"></div>';
	}
	else
	{
		var text = '';
	}
	$("#list_product" + div).html(text);
	//isDisabled();
}

function addProduct(id, current)
{
	var text = '<div id="product' + id + '" class="classCats"><select class="classCatsSelect" onChange="getProducts(this.value, ' + id + ', -1); $(this).css(\'width\',\'192px\');" name="category[' + id + ']" onMouseDown="$(this).css(\'width\',\'auto\');" onBlur="$(this).css(\'width\',\'192px\');">';
	text += '<option value="-1">Выберите</option>';
	for ( i in cats ) {
		text += '<option';
		if (i == current)
		{
			text += ' selected';
		}
		text += ' value="' + i + '">' + cats[i] + '</option>';
	}
	text += '</select><div id="list_product' + id + '" class="listProdcuts"></div></div><div id="productMess' + id + '" class="productMess"></div>';
	return text;
}
var j = 0;
var arrayBlocked = new Array();
function addDivProduct()
{
	var div = addProduct(j, -1);
	$("#products").append(div);
	j++;
}

$(document).ready(function () {
	$(".classProducts").live("click",function(){
		if($(this).hasClass('click')) {
			$(this).removeClass('click');
			$(this).css("width","192px");
		} else {
			$(this).addClass('click');
			$(this).css("width","auto");
		}
	});
	
	$(".classProducts").live("blur",function(){
		$('.classProducts').removeClass('click');
		$('.classProducts').css("width","192px");
	});
	
	/*
	$('body').click(function(){
		$('.classProducts').removeClass('click');
		$('.classProducts').css("width","182px");
	});
	*/
});
