$(
	function()
	{
		$("input[rel='inp_points']").numeric();
		$("input[rel='inp_points']").change(function(event)
		{
			if ($(this).val() % 50 != "")
			{
				$(this).val(0);
				return false;
			}
		});
		
		//$("a[rel='colorbox']").colorbox();
		$("a[rel='colorbox']").fancybox();
		
		$("#search").emptyText("Поиск по магазину", "inp_inactive");
		
		

		$("input[name=delivery]").click(function()
		{
			var allow = $(this).attr("data-payment-allow").split(",");
			$("#payment input").attr("disabled", "disabled");
			for (i in allow)
			{
				$("#payment input[value=" + allow[i] + "]").attr("disabled", "");
			}
			
			if ($("input[name=user_type][rel=jur]:checked").size() > 0)
			{
				$("#payment input[value=1], #payment input[value=3], #payment input[value=4], #payment input[value=5]").attr("disabled", "disabled");
			}
			$("#payment input:disabled").attr("checked", "");
			triggerDeliveryCountry(true);
		});
		
		$("input[name=user_type]").click(function()
		{
			var allow = $(this).attr("rel");
			
			$("#delivery input").attr("disabled", "disabled");
			
			$("#delivery input").each(function()
			{
				if ($(this).attr("data-user-allow").match(allow))
				{
					$(this).attr("disabled", "");
				}
			});
			$("#delivery input:disabled").attr("checked", "");
		});

		triggerDeliveryCountry(false);
	}
);


function triggerDelivery(v)
{
	$("#delivery input").attr("disabled", "disabled");
	if (v == 10)
	{
		$("#delivery input").attr("disabled", "");
	}
	else
	{
		$("#delivery input:not(:first)").attr("disabled", "");
	}
	
	$("#payment input, #delivery input").attr("checked", "");
}


function triggerDeliveryCountry(v)
{
	var country = $('#addr_country').val();
	if (country == "3159") {
		if (!v) {
			$("#payment input[value=8]").attr("disabled", "");
		}
		$("#payment input[value=7]").attr("disabled", "disabled");

		$("#payment input[value=0]").attr("disabled", "");
		$("#payment input[value=5]").attr("disabled", "");
		$("#payment input[value=4]").attr("disabled", "");
		$("#payment input[value=5]").attr("disabled", "");
		$("#payment input[value=2]").attr("disabled", "");
		$("#payment input[value=1]").attr("disabled", "");

		$("#delivery input[value=1276]").attr("disabled", "");
		$("#delivery input[value=1560]").attr("disabled", "");
		$("#delivery input[value=1561]").attr("disabled", "");
		$("#delivery input[value=1611]").attr("disabled", "");

		$("#addr_2").attr("disabled", "");
		var region = document.getElementById("addr_2");
		region.options[0].text = "-выберите регион-";

	} else {
		$("#payment input[value=8]").attr("disabled", "disabled");
		if (!v) {
			$("#payment input[value=7]").attr("disabled", "");
		}

		$("#payment input[value=0]").attr("disabled", "disabled");
		$("#payment input[value=5]").attr("disabled", "disabled");
		$("#payment input[value=4]").attr("disabled", "disabled");
		$("#payment input[value=5]").attr("disabled", "disabled");
		$("#payment input[value=2]").attr("disabled", "disabled");
		$("#payment input[value=1]").attr("disabled", "disabled");

		$("#delivery input[value=1276]").attr("disabled", "disabled");
		$("#delivery input[value=1560]").attr("disabled", "disabled");
		$("#delivery input[value=1561]").attr("disabled", "disabled");
		$("#delivery input[value=1611]").attr("disabled", "disabled");

		$("#addr_2").attr("disabled", "disabled");
		var region = document.getElementById("addr_2");
		region.options[0].selected = true;
		region.options[0].text = "нет";
	}
}


function strReplace(s, r, w)
{
	return s.split(r).join(w);
}

function addslashes(str) 
{
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripslashes(str) 
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

function trim(str)
{ 
	str = str.replace(/&nbsp;/g, '');
	return str.replace(/(^\s+)|(\s+$)/g, '');
}


function isNumber(val) 
{
	return /^-?((\d+\.?\d?)|(\.\d+))$/.test(val);
}


function makeRequest(url) 
{
	var http_request = false;

	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/html');
			// Читайте ниже об этой строке
		}
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}


	if (!http_request) 
	{
		alert('Unfortunatly your browser doesn\'t support XMLHTTP.');
		return false;
	}

	http_request.onreadystatechange = function() 
	{ 
		if (http_request.readyState == 4)
		{
         if (http_request.status == 200)
			{
				
				var answer = eval('(' + http_request.responseText + ')');
				//alert(http_request.responseText);
				if (answer.callback[0] == "applyFilter")
				{
					if (answer.system_error.length > 0)
					{	
						var error = "<ul>";	
						$.each(answer.system_error, function()
						{
							error += "<li>" +  this + "</li>";
						});
						error += "</ul>";
					}
					else if (answer.user_error.length > 0)
					{	
						
					}				
					else
					{
						//document.location.reload();
					}
				}
				
				
				else if (answer.callback[0] == "signUp")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
							
							$("#" + this[1]).parent().parent().addClass("error");
						});
						
						$(".img_antibot").attr("src", "/antibot.php?rnd=" + Math.random() * 999999);
						$("#key").val("");
					}
					else
					{
						$("#form_signup").html(answer.content[0]);
					}
					$("#form_signup").find("input[type=\"button\"]").attr("disabled", "");
				}
				
				
				else if (answer.callback[0] == "repeatSignup")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$("#" + this[1]).parent().find(".d_error").html(this[0]);
						});	
					}
					else
					{
						$("#form_signup").html(answer.content[0]);
					}
				}
				
				else if (answer.callback[0] == "signIn")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
							
							$("#" + this[1]).parent().parent().addClass("error");
						});
					}
					else
					{
						document.location = "/";
					}
					$("#form_signin").find("input[type=\"button\"]").attr("disabled", "");
				}
				
				
				else if (answer.callback[0] == "amnesia")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
							
							$("#" + this[1]).parent().parent().addClass("error");
						});
					}
					else
					{
						$("#form_amnesia").html(answer.content[0]);
					}
					$("#form_amnesia").find("input[type=\"button\"]").attr("disabled", "");
				}
				
				else if (answer.callback[0] == "saveProfile")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
							
							$("#" + this[1]).parent().addClass("error");
						});
					}
					else
					{
						//$("#form_save_profile .sp_result").html(answer.content[0]);
						$.pnotify({
							pnotify_history: false,
							pnotify_notice_icon: "",
							pnotify_title: "Личные данные",
							pnotify_text: answer.content[0]
						});
					}
					$("#form_save_profile").find("input[type=\"button\"]").attr("disabled", "");
				}
				
				else if (answer.callback[0] == "addToCart")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Корзина",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
						});
					}
					else
					{
						$(".d_cart_info small").html(answer.content[0]);
						
						$.pnotify({
							pnotify_history: false,
							pnotify_notice_icon: "",
							pnotify_title: "Корзина",
							pnotify_text: answer.result[0]
						});
					}
				}
				
				else if (answer.callback[0] == "delFromCart")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$("#" + this[1]).parent().find(".d_error").html(this[0]);
						});
					}
					else
					{
						$(".d_cart_info small").html(answer.content[0]);
					}
				}
				
				
				else if (answer.callback[0] == "sendOrder")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
							
							$("#" + this[1]).parent().parent().addClass("error");
						});
						
						$(".img_antibot").attr("src", "/antibot.php?rnd=" + Math.random() * 999999);
						$("#key").val("");
					}
					else
					{
						$("#order").html(answer.content[0]);
						$("#delivery").html("");
						$("#payment").html("");
						$("#terms_block").html("");
						$(".d_cart_info small").html(answer.cart_content[0]);
						$(".a_cart_prev_step").css("display", "none");
					}
					$("input[type=\"button\"]").attr("disabled", "");
					$("input[type=\"button\"]").val("Заказать");
				}
				
				
				
				else if (answer.callback[0] == "addProdComment")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
						});
					}
					else
					{
						$("#comment").val("");
						$(".d_comments").prepend(answer.content[0]);
					}
					$(".d_comments").find("input[type=button]").attr("disabled", "");
				}
				
				
				else if (answer.callback[0] == "cancelOrder")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
						});
					}
					else
					{
						document.location.reload();
					}
				}
				
				
				
				else if (answer.callback[0] == "recieveOrder")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
						});
					}
					else
					{
						document.location.reload();
					}
				}
				
				else if (answer.callback[0] == "traceProduct")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
						});
					}
					else
					{
						$.pnotify({
							pnotify_history: false,
							pnotify_notice_icon: "",
							pnotify_title: "Наблюдение за товаром",
							pnotify_text: "Мы сразу же сообщим Вам, как только товар появится на складе."
						});
					}
				}
				

				else if (answer.callback[0] == "sendFeedback")
				{
					if (answer.system_error.length > 0)
					{
					}
					else if (answer.user_error.length > 0)
					{
						$.each(answer.user_error, function()
						{
							$.pnotify({
								pnotify_history: false,
								pnotify_notice_icon: "",
								pnotify_title: "Ошибка",
								pnotify_type: "error",
								pnotify_text: this[0]
							});
							
							$("#" + this[1]).parent().parent().addClass("error");
						});
						
						$(".img_antibot").attr("src", "/antibot.php?rnd=" + Math.random() * 999999);
						$("#key").val("");
					}
					else
					{
						$("#form_feedback").html(answer.content[0]);
					}
					$("#form_feedback").find("input[type=\"button\"]").attr("disabled", "");
				}
				
				else if (answer.callback[0] == "showOrderInfo")
				{
					if (answer.system_error.length > 0)
					{	
						var error = "<ul>";	
						$.each(answer.system_error, function()
						{
							error += "<li>" +  this + "</li>";
						});
						error += "</ul>";
						jAlert(error, "Системная ошибка");
					}
					else if (answer.user_error.length > 0)
					{
						var error = "<ul>";	
						$.each(answer.user_error, function()
						{
							error += "<li>" +  this + "</li>";
						});
						error += "</ul>";
						jAlert(error, "Ошибка");
					}
					else
					{
						$.prompt( answer.content[0]);
					}
				}
			}
			else
			{
				alert("Не удалось получить ответ от сервера");
			}
		}
	};


	$("#curtain").css("display", "block");
	//************** ОБРАБАТЫВАЕМ КЛИКИ
	var args = "";
	for (var i = 0; i < arguments.length; i++)
	{
		args += "&p" + i + "=" + arguments[i];
	}
	
	http_request.open('POST', "/getAnswer.php", true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Connection", "close");	
	http_request.send("q=" + url + args);
}

function applyFilter()
{
	var url = "";
	$("input[rel='filter']").each(function()
	{
		if ( $(this).val() != "" )
		{
			//url += "/"
			//alert("p")
		}
	});
}

