$(document).ready(function(){
$("body").append("<div id='imagelayer'></div>");
$('.img_hover').bind('mouseover',
	function(e) 
		{
		var img_url = $(this).attr("alt");
		var pos = $(this).position();
		$("#imagelayer").html("<img id='curimage' src='"+img_url+"' width='300px'>");
		
		$("#imagelayer").show();
		
		if($("#curimage").width() > $("#curimage").height())
			{
			$("#curimage").css("width","400px");
			$("#imagelayer").css("width","400px");
			$("#imagelayer").css("left",(pos.left-$("#curimage").width()/4)+"px");
			$("#imagelayer").css("top",(pos.top-$("#curimage").height()/4)+"px");
			}
			else
			{
			$("#curimage").css("width","300px");
			$("#imagelayer").css("width","300px");
			$("#imagelayer").css("left",(pos.left-$("#curimage").width()/4)+"px");
			$("#imagelayer").css("top",(pos.top-$("#curimage").height()/4)+"px");
			}
		
		});
	$("#imagelayer").bind('mouseout',function() 
		{
		$("#imagelayer").hide();
		});
});

function searchCheck()
	{
	if($("#prodsearch").attr("value") == "")
		{
		alert("необходимо ввести поисковый запрос");
		return false;
		}
		else
		{
		$("#searchform").submit();
		}
	return true;
	}
	
$(document).ready(function(){
		$(".yoxview").yoxview({
	    	autoHideMenu: false,
		autoHideInfo: false,
		titleAttribute: 'alt',
		lang : 'ru',
		});
	});


$(document).ready(function()
	{
	$("#cart_window").dialog(
			{
			autoOpen: false,
			draggable: false,
			modal: true,
			width : 720,
			title : "Корзина"
			}
		)
	
	});


function openLoginForm()
	{
	var pos = $("#login_a").position();
	$("#login_form").css("left",pos.left-75+"px");
	$("#login_form").toggle();
	
	}
	
function closeLoginForm()
	{
	$("#login_form").hide();
	}
	
function clear_cart()
	{
	if(confirm("Вы уверены, что хотите удалить все товары из корзины?"))
		{
		$.post("/ajax.php?action=clearcart",{clear : 1},
			function(data){
					
			$("#order_goods_num").html(data.allgoods);
			$("#order_discount").html(data.discount);
			$("#order_sum").html(data.fullsum);
		
			$("#discount_cart").html(data.discount);
			$("#discount_price_cart").html(data.discount_price_cart);
			$("#fullcount_cart").html(data.allgoods);
			$("#fullprice_cart").html(data.fullsum);
			//$("#delivery_price_cart").html(data.delivery);
			
			},"json");
		}
	}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


function init_inputsend()
{
$(".required").bind('keyup',function()
	{
	if($(this).attr("value") != "")
		{
		$(this).css("border","1px solid #006");
		}
		else
		{
		$(this).css("border","1px solid red");
		}
	});

var order_mail = getCookie("order_email");
var order_fam = getCookie("order_fam");
var order_name = getCookie("order_name");
var order_otch = getCookie("order_otch");
var order_phone = getCookie("order_phone");
var order_country = getCookie("order_country");
var order_region = getCookie("order_region");
var order_city = getCookie("order_city");
var order_street = getCookie("order_street");
var order_house = getCookie("order_house");
var order_korp = getCookie("order_korp");
var order_flat = getCookie("order_flat");
var order_postindex = getCookie("order_postindex");
var order_orderto = getCookie("order_orderto");

if(order_mail != "") $("#i_mail").attr("value",order_mail);
if(order_fam != "") $("#i_fam").attr("value",order_fam);
if(order_name != "") $("#i_name").attr("value",order_name);
if(order_otch != "") $("#i_otch").attr("value",order_otch);
if(order_phone != "") $("#i_phone").attr("value",order_phone);
if(order_country != "" && order_country != null) $("#i_country").attr("value",order_country);
if(order_region != "") $("#i_region").attr("value",order_region);
if(order_city != "") $("#i_city").attr("value",order_city);
if(order_street != "") $("#i_street").attr("value",order_street);
if(order_house != "") $("#i_house").attr("value",order_house);
if(order_korp != "") $("#i_korp").attr("value",order_korp);
if(order_flat != "") $("#i_flat").attr("value",order_flat);
if(order_postindex != "") $("#i_postindex").attr("value",order_postindex);
if(order_orderto != "") $("#i_orderto").attr("value",order_orderto);
}

function check_order_form()
	{
	var e = "";
	if($("#i_mail").attr("value") == "") 
		{
		e += "Введите адрес электронной почты\r\n";
		$("#i_mail").css("border","1px solid red");
		}
	
	if($("#i_fam").attr("value") == "") 
		{
		e += "Введите Фамилию контактного лица\r\n";
		$("#i_fam").css("border","1px solid red");
		}
	
	if($("#i_name").attr("value") == "") 
		{
		e += "Введите Имя контактного лица\r\n";
		$("#i_name").css("border","1px solid red");
		}
	if($("#i_otch").attr("value") == "") 
		{
		e += "Введите Отчество контактного лица\r\n";
		$("#i_otch").css("border","1px solid red");
		}
	
	if($("#i_phone").attr("value") == "") 
		{
		e += "Введите контактный телефон\r\n";
		$("#i_phone").css("border","1px solid red");
		}
	if($("#i_country").attr("value") == "") 
		{
		e += "Введите страну\r\n";
		$("#i_country").css("border","1px solid red");
		}
	if($("#i_region").attr("value") == "") 
		{
		e += "Введите область/край\r\n";
		$("#i_region").css("border","1px solid red");
		}
	if($("#i_city").attr("value") == "") 
		{
		e += "Введите город\r\n";
		$("#i_city").css("border","1px solid red");
		}
	if($("#i_street").attr("value") == "") 
		{
		e += "Введите название улицы адреса\r\n";
		$("#i_street").css("border","1px solid red");
		}
	if($("#i_house").attr("value") == "") 
		{
		e += "Введите номер дома\r\n";
		$("#i_house").css("border","1px solid red");
		}
	if($("#i_postindex").attr("value") == "") 
		{
		e += "Введите почтовый индекс\r\n";
		$("#i_postindex").css("border","1px solid red");
		}
		
		
	if(e != "")
		{
		alert(e);
		return false;
		}
		else
		{
		setCookie("order_email",$("#i_mail").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_fam",$("#i_fam").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_name",$("#i_name").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_otch",$("#i_otch").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_phone",$("#i_phone").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_country",$("#i_country").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_region",$("#i_region").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_city",$("#i_city").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_street",$("#i_street").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_house",$("#i_house").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_korp",$("#i_korp").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_flat",$("#i_flat").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_postindex",$("#i_postindex").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		setCookie("order_orderto",$("#i_orderto").attr("value"),"Mon, 01-Jan-2050 00:00:00 GMT","/");
		
		document.getElementById("order_form").submit();
		return true;
		}
	}

function removeCartItem(item_id)
	{
	$("#fullprice_cart").html("Подождите...");
	$.post("/ajax.php?action=removeitem",{item : item_id},
		function(data){
			
		$("#order_goods_num").html(data.allgoods);
		$("#order_discount").html(data.discount);
		$("#order_sum").html(data.fullsum);
		//$("#delivery_price_cart").html(data.delivery);
		$("#discount_cart").html(data.discount);
		$("#discount_price_cart").html(data.discount_price_cart);
		$("#fullcount_cart").html(data.allgoods);
		$("#fullprice_cart").html(data.fullsum);
		$("#item_count"+item_id).html(data.itemcount);
		$("#item_fullprice"+item_id).html(data.itemfullprice);
		if(data.itemcount == "0")
			{
			removeCartItems(item_id);
			}
	},"json");
	
	}
	
function addCartItem(item_id)
	{
	$("#fullprice_cart").html("Подождите...");
	
	$.post("/ajax.php?action=additem",{item : item_id},
		function(data){
			
		$("#order_goods_num").html(data.allgoods);
		$("#order_discount").html(data.discount);
		$("#order_sum").html(data.fullsum);
		//$("#delivery_price_cart").html(data.delivery);
		$("#discount_cart").html(data.discount);
		$("#discount_price_cart").html(data.discount_price_cart);
		$("#fullcount_cart").html(data.allgoods);
		$("#fullprice_cart").html(data.fullsum);
		$("#item_count"+item_id).html(data.itemcount);
		$("#item_fullprice"+item_id).html(data.itemfullprice);
	},"json");
	
	}
	
function removeCartItems(item_id)
	{
	$("#fullprice_cart").html("Подождите...");
	$.post("/ajax.php?action=removeitems",{item : item_id},
		function(data){
		$("#itemrow"+item_id).fadeOut();
		
		$("#order_goods_num").html(data.allgoods);
		$("#order_discount").html(data.discount);
		$("#order_sum").html(data.fullsum);
		//$("#delivery_price_cart").html(data.delivery);
		$("#discount_cart").html(data.discount);
		$("#discount_price_cart").html(data.discount_price_cart);
		$("#fullcount_cart").html(data.allgoods);
		$("#fullprice_cart").html(data.fullsum);
		
	},"json");
	
	}

function addToOrder(item, num)
	{
	$("#cart_window").hide();
	
	$.post("/ajax.php?action=addtocart",{item : item, count: num},
		function(data){
		$("#order_goods_num").html(data.allgoods);
		$("#order_discount").html(data.discount);
		$("#order_sum").html(data.fullsum);
		//var cart_width = ($("#cart_window").width())/2;
		//var cart_height = ($("#cart_window").height())/2;
		
		//$("#cart_window").css("left",screen.availWidth/2-cart_width+"px");
		//$("#cart_window").css("top",screen.availHeight/2-cart_height-100+"px");
		
		//$("#cart_window").fadeIn();
		$("#order_cart").html(data.cart_data);	
		$("#cart_window").dialog('open');
		
	},"json");
	
	
	}

function openLeftCategory(cat_id)
	{
	if(cat_id != "")
		{	
		$(window).load(function() {
			$("#cat"+cat_id).fadeToggle();
		});
		}
	}
	
function validatepass()
	{
	var pas = document.getElementById('pass1').value;
	var pas2 = document.getElementById('pass2').value;
	if(pas)
	{
	
	if( (pas+"") != (pas2+""))
		{
		
		document.getElementById('passcontent').style.display = "";
		document.getElementById('passcontent').innerHTML = "Пароли не совпадают";
		document.getElementById('passcontent').style.color = "red";
		
		}else
		{
		document.getElementById('passcontent').style.display = "";
		document.getElementById('passcontent').innerHTML = "Пароли совпадают";
		document.getElementById('passcontent').style.color = "green";
		
		}
	}else
	{
	document.getElementById('passcontent').style.display = "none";
	}
	}

function validatelogin()
	{
	var loginval = document.getElementById('reglogin').value;
	if(loginval != "")
		{
		document.getElementById('logincontent').style.display = "";
		get_data('action=validatelogin&login='+document.getElementById('reglogin').value,'logincontent');
		}else
		{
		document.getElementById('logincontent').style.display = "none";
		}
	}
	
function validatemail()
	{
	var mailval = document.getElementById('regmail').value;
	if(mailval != "")
		{
		document.getElementById('mailcontent').style.display = "";
		get_data('action=validatemail&email='+document.getElementById('regmail').value,'mailcontent');
		}else
		{
		document.getElementById('mailcontent').style.display = "none";
		}
	}
	
function get_data(URL, destination){
URL = "/ajax.php?"+URL;
  dest = destination;
  if(window.XMLHttpRequest){
	request = new XMLHttpRequest();
    request.onreadystatechange = processStateChange;
    request.open("GET", URL, true);
    request.send(null);
  } else if (window.ActiveXObject) {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    if (request) {
	request.onreadystatechange = processStateChange;
	request.open("GET", URL, true);
	request.send();
    }
  }
}
/////////////////
function processStateChange(){
  if (request.readyState == 4){
    contentDiv = document.getElementById(dest);
    if (request.status == 200){
	response = request.responseText;
	if(contentDiv) contentDiv.innerHTML = ExecJS(response);
	
	}
  }
}

function ExecJS(s)
{
var js_ScriptFragment = '<script>(.*?)</script>'; 
var matchAll = new RegExp(js_ScriptFragment, 'img');

var matchOne = new RegExp(js_ScriptFragment, 'im');
var arr = s.match(matchAll) || [];
var JSCode = [];
  
for (var i = 0; i < arr.length; i++)
   {
   
   var mtCode = arr[i].match(matchOne);
   if (mtCode && mtCode[1] != '')
   JSCode[JSCode.length] = mtCode[1];
   }
   
s = s.replace(matchAll, '');

for(var i = 0; i < JSCode.length; i++)
	{
	eval(JSCode[i]);
	}
 
return s;
}

function validateloginres()
	{
	document.getElementById('logincontent').style.display = "";
	get_data('action=checklogin&login='+document.getElementById('reslogin').value,'logincontent');
	}
	
function validatemailres()
	{
	document.getElementById('emailcontent').style.display = "";
	get_data('action=checkemail&email='+document.getElementById('resmail').value,'emailcontent');
	}

