<!--




function NewProducts(direction){
	
	newproductsnum = newproductsnum + direction;
	if (newproductsnum >= newproductscount) 	newproductsnum = 0;
	if (newproductsnum < 0) 					newproductsnum = newproductscount - 1;		
	$('#newproductsleft').html(newproducts[newproductsnum]);
	
	var newproductsnumright = newproductsnum + 1;
	if (newproductsnumright >= newproductscount) 	newproductsnumright = 0;
	$('#newproductsright').html(newproducts[newproductsnumright]);
}




function CheckStylesOwen(){
	
	$(".TABLE_striped").find("tr:odd").addClass('bgcolor2');
	$(".TABLE_striped tr").find("td:last").css('border-right', '0');
	
	$(".TABLE_images").find("tr:odd").addClass('bgcolor2');
	$(".TABLE_images").wrap('<table cellpadding="0" cellspacing="0" width="100%"><tr><td style="border:5px solid #d1d8db;"></td></tr></table>');		

	
	$(".IMAGE_border").wrap('<table cellpadding="0" cellspacing="0"><tr><td style="border:10px solid #d1d8db;"></td></tr></table>');	

	$(".TABLE_device a").css("margin-right", "7px");

	


	var tables = $(".TABLE_image_text");
	for (var i=0; i<tables.length; i++){
		var table = tables[i];
		var pic = $(table).find("img:first");

	   $(table).attr("width", pic.attr("width"));
	   $(table).find("td").eq(1).css('padding', '3px 14px 8px');
	}
}





function MainCatalogLev1(id){
	
	$(".maincatalog-tr").show();
	$(".maincatalog-trselected").hide();
	
	$("#tr"+ id).hide();
	$("#trselected"+ id).show();
	
	$(".maincatalog_lev2").hide();
	$("#lev2"+ id).show();
}



function MainCatalogLev2(parentid, id){
	
	var hsearch = $("#lev2"+ parentid);
	var curtd = $(hsearch).find("#lev2td"+ id);
	
	
	
	if ($(curtd).find("#lev2trname"+id).css("display") == "none"){
		$(hsearch).find("td").removeClass('mouseover-on');
		$(hsearch).find("td").removeClass('lev2headeron');
		$(hsearch).find(".lev2trname").hide();
		
		curtd.addClass('mouseover-on')
			 .find(".maincatalog-lev2-header").addClass('lev2headeron');
		
		$(hsearch).find("#lev2trname"+ id).show();
		
		var location = document.location.href;
		
		if (location.indexOf("#topcatalog") + 1)   	document.location = location;
		else										document.location = location +'#topcatalog';
		
	}else{
		curtd.removeClass('mouseover-on')
			 .find(".maincatalog-lev2-header").removeClass('lev2headeron');
		
		$(hsearch).find("#lev2trname"+ id).hide();
	}
}





function SelectDealers(obj, name) {

    if (name == 'country') {
        $("select.cityclass").val('');
        
        $("#dealers").find("div[class=country]").show();
        $("select.cityclass").hide();
        $("select[name="+ $("select[name=country]").val() +"]").show();
        
        if ($("select[name=country]").val())    $("#dealers").find("div[class=country][mytitle!="+ obj.value +"]").hide();
        else                                    $("select[name=city]").hide();
        
        $("#dealers").find("div[class=city]").show();  
    } else {
        $("#dealers").find("div[class=city]").show();
        if (obj.value)  $("#dealers").find("div[class=city][mytitle!="+ obj.value +"]").hide();   
    }
}




function replaceCookieDeviceOwenShop(name, device, oldname) {

	if (name == oldname)  return true;

	var shopstring = $.cookie('SHOP');
	var shop = new Object();
	
	if (shopstring) shop = jQuery.parseJSON(shopstring);
	if (!shop.data) shop.data = new Array();
	else {
	
		for (num in shop.data) 
			if (shop.data[num].name == oldname)  addCookieDeviceOwenShop(name, parseInt(Number(shop.data[num].count)), device);
	}
	
	deleteCookieDeviceOwenShop(oldname);
	
	return false;
}





function addCookieDeviceOwenShop(name, count, device) {


	var checkexists = false;
	var shopstring = $.cookie('SHOP');
	var shop = new Object();

	if (shopstring) shop = jQuery.parseJSON(shopstring);
	if (!shop.data) shop.data = new Array();
	else {
	
		for (num in shop.data) {
		
			if (shop.data[num].name == name) {
			
				if (count == null)  shop.data[num].count += 1;
				else  				shop.data[num].count = count;
				checkexists = true;
			}
		}
	}
	
	if (!checkexists) {

		var newitem = new Object();
		newitem.name = name;
		if (count == null)  newitem.count = 1;
		else  				newitem.count = count;
		newitem.device = device;
		shop.data.push(newitem);
	}
	var newstring = $.toJSON(shop);  
	
	$.cookie('SHOP', newstring, { expires: 2, path: '/' });
	showCurTopCartState();
}



function deleteCookieDeviceOwenShop(name) {

	var shopstring = $.cookie('SHOP');
	var shop = new Object();
	
	if (shopstring) shop = jQuery.parseJSON(shopstring);
	if (!shop.data) shop.data = new Array();
	else {
	
		for (num in shop.data) 
			if (shop.data[num].name == name)  shop.data.splice(num, 1);
	}

	var newstring = $.toJSON(shop);              
	
	$.cookie('SHOP', newstring, { expires: 2, path: '/' });
	showCurTopCartState();
}





function showCurPriceOrderState() {

	var shopstring = $.cookie('SHOP');
	var shop = new Object();
	
	if (shopstring) shop = jQuery.parseJSON(shopstring);
	if (!shop.data)  return true;
	

	$('.addtobaskettd:visible').find('td:first > a, td:first > span').each(function() {
	
		for (num in shop.data) 
			if (shop.data[num].name == $(this).text()) {
			
				//$(this).parents('.addtobaskettd').find('td.addord').css('width', 120).html('<span class="tobasket2"><span class="counttext">'+ shop.data[num].count +' шт. в <a href="/cart">корзине</a></span></span>');
				$(this).parents('.addtobaskettd').find('td.addord').css('width', 120).html('<div class="tobasket2"><div class="counttext">в <a href="/cart" class="linktobasket">корзине</a></div></div>');
				$(this).parents('.addtobaskettd').undelegate();		
				$(this).parents('.addtobaskettd').unbind();
			}
	});
	showCurTopCartState();

	return true;
}



function showCurTopCartState() {

	//$('#topcart').show();
	$('#curcart').html("товаров нет");

	var shopstring = $.cookie('SHOP');
	var shop = new Object();
	
	if (shopstring) shop = jQuery.parseJSON(shopstring);
	if (!shop.data)  return true;
	
	var count = 0;
	for (num in shop.data)  count += shop.data[num].count;

	
	if (count == 0) $('#curcart').html("товаров нет");
	else            $("#curcart").html(count +' '+ declOfNum(count, ['товар', 'товара', 'товаров']));

	return true;
}



function addnewtocart(device, devicename, state, price) {

	var checkexists = false;
	if ($(".tabcart tr:not(.header)").length < 1)  return false;
	
	$(".tabcart .leftalign").each(function() {
	
		if ($(this).text() == device) {
		
			$(this).parent().find("input").val(Number($(this).parent().find("input").val())+1);
			onChangeCount($(this).parent().find("input"))
			checkexists = true;
		}
	});
		
	
	if (checkexists)  return true;
	
	if (!price) {
		price = 'не определена';
		var total = 'не определена';
	} else {
		
		var total = number_format(price, 2, ",", " ");	
		price = number_format(price, 2, ",", " ");
	}
	
	
	if (state == 1)  state = 'со склада';
	else if (state == 0)  state = 'срок поставки от 2 недель';
	else state = 'не определена';	
	
	
	$(".tabcart > tbody > tr:last").clone().appendTo(".tabcart > tbody");

	$(".tabcart > tbody > tr:last .leftalign").html(device);
	$(".tabcart > tbody > tr:last .state").html(state);
	$(".tabcart > tbody > tr:last .price").html(price);
	$(".tabcart > tbody > tr:last input").val(1);
	$(".tabcart > tbody > tr:last input").attr("datadevice", devicename);
	$(".tabcart > tbody > tr:last .total").html(total);
	$(".tabcart > tbody > tr:last a.edit").attr("href", "/moreprices.php?id="+ devicename +"&devicename="+ device);
	
	checkCartTable();
	RecountTotalCart();
	$(".tabcart a").colorbox({ height: 420, width: 760, iframe: true, close: "" });
}



function replacetocart(device, devicename, state, price, oldname) {

	var checkexists = false;
	if ($(".tabcart tr:not(.header)").length < 1)  return false;
	
	$(".tabcart .leftalign").each(function() {
	
		if ($(this).text() == device) {
		
			$(this).parent().find("input").val(Number($(this).parent().find("input").val())+1);
			onChangeCount($(this).parent().find("input"))
			checkexists = true;
		}
	});	
	
	
	if (checkexists) {  
	
		$(".tabcart .leftalign").each(function() { if ($(this).text() == oldname)  $(this).parent().remove(); });	
		RecountTotalCart();
		checkCartTable();
		
		return true;
	}	
		
	var counter = 0;
	var curnum;
	$(".tabcart .leftalign").each(function() { counter++; if ($(this).text() == oldname)  curnum = counter; });		
	
	
	if (!price) {
		price = 'не определена';
		var total = 'не определена';
	} else {
		
		var total = number_format(price*Number($(".tabcart > tbody > tr:eq("+ curnum +")").find("input").val()), 2, ",", " ");	
		price = number_format(price, 2, ",", " ");
	}
	
	
	if (state == 1)  state = 'со склада';
	else if (state == 0)  state = 'срок поставки 2 недели';
	else state = 'не определена';	
	
	
	$(".tabcart > tbody > tr:eq("+ curnum +") .leftalign").html(device);
	$(".tabcart > tbody > tr:eq("+ curnum +") .state").html(state);
	$(".tabcart > tbody > tr:eq("+ curnum +") .price").html(price);
	$(".tabcart > tbody > tr:eq("+ curnum +") .total").html(total);
	$(".tabcart > tbody > tr:eq("+ curnum +") a.edit").attr("href", "/moreprices.php?id="+ devicename +"&devicename="+ device);
	
	checkCartTable();
	RecountTotalCart();
	$(".tabcart a").colorbox({ height: 420, width: 760, iframe: true, close: "" });
}




function RecountTotalCart() {

	if ($(".tabcart tr:not(.header)").length < 1)  return false;
	
	var totalsum = 0;
	var def = 0;	
	var undef = 0;

	
	
	$(".tabcart tr:not(.header)").each(function() {
	
		var newsum = $(this).find(".price").text().replace(" ", "").replace(",", ".") * parseInt(Number($(this).find("input").val()));
	
		if (!isNaN(newsum) && newsum > 0) {
			
			def += 1;
			totalsum += newsum;
		} else {
		
			undef += 1;
		}
	});	
		
	if (def) {
		$(".def-div").show();
		$(".def-count").html(def +" "+ declOfNum(def, ['позиция', 'позиции', 'позиций']));
		$(".def-total").html(number_format(totalsum, 2, ",", " "));
	} else {
		$(".undef-and").hide();
		$(".def-div").hide();
	}
	
	if (undef) {
	
		$(".undef-div").show();
		if (def)  $(".undef-and").show();

		$(".undef-count").html(undef +" "+ declOfNum(undef, ['позиция', 'позиции', 'позиций']));
	} else $(".undef-div").hide();
}





function onChangeCount(obj) {

	var newcount = parseInt(Number($(obj).val()));
	if (isNaN(newcount) || newcount < 1) newcount = 1;
	$(obj).val(newcount);
	
	var newsum = $(obj).parents(".tabcarttr").find(".price").text().replace(" ", "").replace(",", ".") * newcount;
	
	if (!isNaN(newsum) && newsum > 0)	$(obj).parents(".tabcarttr").find(".total").html(number_format(newsum, 2, ",", " "));
	
	addCookieDeviceOwenShop($(obj).parents(".tabcarttr").find("td:first > div").text(), newcount, $(obj).attr('datadevice'));
	RecountTotalCart();
}



function deleteFromCart(obj) {

	$(obj).parents(".tabcarttr").remove(); 
	
	deleteCookieDeviceOwenShop($(obj).parents(".tabcarttr").find("td:first div").text());
	
	checkCartTable();
	RecountTotalCart();
}



function checkCartTable() {

	if ($(".tabcart tr:not(.header)").length < 1) {
	
		$("#fullcart").hide();
		$("#emptycart").show();
		return false;
	}

	$(".tabcart tr:not(.header)").removeClass("strip");
	$(".tabcart tr:not(.header):odd").addClass("strip");	
}


function declOfNum(number, titles) {

	cases = [2, 0, 1, 1, 1, 2];
	return titles[ (number%100>4 && number%100<20) ? 2 : cases[(number%10<5) ? number%10 : 5]  ];
}



function numberOf(numberof, suffix) {

	var keys = [2, 0, 1, 1, 1, 2];
	var mod = numberof % 100;
	var suffix_key = mod > 4 && mod < 20 ? 2 : keys[Math.min(mod%10, 5)];

	return suffix[suffix_key];
}

			



function closecolorbox() {

	$.colorbox.close();
}


function number_format( number, decimals, dec_point, thousands_sep ) {	

	var i, j, kw, kd, km;

	// input sanitation & defaults
	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	//kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


	return km + kw + kd;
}



function myFormCheckNew(obj){

	$(obj).find(".required:visible").filter('[value!=""]').removeClass('inputerror');
	var inp = $(obj).find(".required:visible").filter('[value=""]');
	
	
	if (inp.length != 0){
	
		for (var i=0; i<inp.length; i++)  $(inp[i]).addClass('inputerror');	
		var formcheck = true;
	}
	
	if (formcheck){
		alert('Незаполнены обязательные поля');
		return false;	
	}
	
	return true;
}


function fadeOutCart() {
	$("#gocart").hide();
	setTimeout(function() { fadeInCart(); }, 500);
}

function fadeInCart() {
   $("#gocart").show();
   setTimeout(function() { fadeOutCart(); }, 7000);
}



function onclick_Payment_Nal(obj) {

	var curmethodstate = $(obj).parents('.methodname');	

	$(curmethodstate).find("input[name=delivery2]").removeAttr('checked');
	$(curmethodstate).find("input[name=delivery3]").removeAttr('checked');
	
	$(curmethodstate).find(".delivery_door").hide();
	$(curmethodstate).find(".delivery_self").hide();
	

	$(curmethodstate).find("input.notnal").removeAttr('checked');
	$(curmethodstate).find("input.notnal").attr('disabled', 'disabled');
}


function onclick_Payment_Uri(obj) {

	var curmethodstate = $(obj).parents('.methodname');
	
	$(curmethodstate).find("input.notnal").removeAttr('disabled');
}

// -->
