$(document).ready(function(){

$("a[rel=lightbox]").fancybox({zoomOpacity: false, overlayOpacity: 0.85, hideOnContentClick: false, centerOnScroll: true});														
$("a[rel=lightbox-gallery]").fancybox({zoomOpacity: false, overlayOpacity: 0.85, hideOnContentClick: false, centerOnScroll: true});																																														
$("div#customer-type a").fancybox({zoomOpacity: false, overlayOpacity: 0.85, hideOnContentClick: false, centerOnScroll: true, frameWidth: 265, frameHeight: 70});

CheckAllFields();

$("form.standard .value *").change(function(){ CheckField($(this)); });																																																																																																																	$("form.standard .value *").keyup(function(){ CheckField($(this)); });	
$("form.standard input[name=equals-legal-address]").click(function(){ EqualAddress("real"); });																																																																																																																																																									$("form.standard input[name=equals-real-address]").click(function(){ EqualAddress("delivery"); });	
$("form.standard *[name*='-paf']").change(function(){ BuildPhone($(this)); });																																																																																																																																																																																																					$("form.standard *[name*='-paf']").keyup(function(){ BuildPhone($(this)); });																																																
$("form.standard").submit(function(){ OnSubmit($(this)); });	
																																																
GetSearchResults();
AddBranchesListToCart();

$("#YMapsID").show();	

$("div.offers div.arrow-left img").hide();
$("div.offers div.arrow-left img.unactive").show();

});

setTimeout("ChangeBanner(0, false)", 5000);

var blnStopBanners = false;

var arrBranches = Array();

document.onkeydown = Navigate;

function Navigate(event)
{	
	link = null;
	direction = null;
	
	if (window.event) event = window.event;
	
	if (event.ctrlKey)
	{
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
		{
			case 0x25:
				link = $("a[rel=prev]").attr("href");
				direction = 1;
				break;
			case 0x27:
				link = $("a[rel=next]").attr("href");
				direction = 2;
				break;
		}
	}
	
	if (link) document.location = link;
	if (direction) ChangeBanner(direction, true);
}


function ChangeCustomerType()
{
	$.cookie("customer_type", $("#customer-select option:selected").val(), { expires: 365, path: "/" });
	
	$.fn.fancybox.close();
	document.location.reload();
}


function SwitchAuthForm()
{
	if ($("div#authorization:visible").size())
	{
		$("div#authorization-background").hide();
		$("div#authorization").hide();
	}
	else
	{
		$("div#authorization-background").show();
		$("div#authorization").show();
	}
}


function ShowSubmenu()
{
	$("div.submenu div.hidden").show();
	$("div.submenu div.slider").hide();
}


function ShowCities(intNumCities)
{	
	objCurrentCity = $("div#contacts > div:not(.hidden) div.city .name");
	strCurrentCity = objCurrentCity.text();
	
	arrCities = Array();
	arrCitiesCodes = Array();
	
	k = 0;
	
	for (i = 0; i < intNumCities; i++)
	{
		strCity = $("div#contacts div.city:eq(" + i + ") .name").text();
		
		if (strCity != strCurrentCity)
		{
			arrCities[k] = strCity;
			arrCitiesCodes[k] = i + 1;
			k++;
		}
	}
	
	strCities = "";
	
	for (i = 0; i < arrCities.length; i++)
	{
		strCities += " <span class='link' onclick='ChangeCity(\"" + arrCitiesCodes[i] + "\")'>";
		strCities += arrCities[i];
		strCities += "</span>";
		
		if (i <= arrCities.length - 3)
		{
			strCities += ",";
		}
		
		if (i == arrCities.length - 2)
		{
			strCities += " или";
		}
	}
	
	
	$("div#contacts > div:not(.hidden) .city .city-selector span.change").hide();
	$("div#contacts > div:not(.hidden) .city .city-selector span.select").html("сменить на" + strCities);
	$("div#contacts > div:not(.hidden) .city .city-selector span.select").show();
}


function ChangeCity(intCity)
{
	$.cookie("city_selector", intCity, { expires: 365, path: "/" });
	
	$("div#contacts > div:not(.hidden)").attr("class", "hidden");
	$("div#contacts > div#city-" + intCity).attr("class", "");
	$("div#contacts > div:not(.hidden) .city .city-selector span.change").show();
	$("div#contacts > div:not(.hidden) .city .city-selector span.select").hide();
}


function ChangeDivisionType(strSelectorState, strDivisionType)
{
	$("div#contacts div.division-type-selector span").removeClass("selected");
	$("div#contacts div.division-type-selector span." + strSelectorState).addClass("selected");
	
	if (strSelectorState == "e-shop")
	{
		$("div#contacts div.divisions").hide();
		$("div#contacts div.e-shop").show();
	}
	else
	{
		$("div#contacts div.e-shop").hide();
		$("div#contacts div.divisions").show();
		$("div#contacts div.divisions div.col div.item").removeClass("useless");
		$("div#contacts div.divisions div.col > div:not(." + strDivisionType + ")").addClass("useless");
		$("div#contacts div.divisions div.col div.title").removeClass("useless");
	}
}


function ChangeBanner(intDirection, blnStop)
{	
	if (blnStop == true) blnStopBanners = true;
	else if (blnStopBanners == true && blnStop == false) return false;
	
	intAutoDirection = intDirection;
	
	objBanners = $("div#banners-shop > div.images");
	
	objBanners.stop();
	
	intBannersNum = $("div#banners-shop div.images img").size();
	
	if (intBannersNum == 0)
	{
		return false;
	}
	
	strLeft = objBanners.css("left");
	intLeft = parseFloat(strLeft.substring(0, strLeft.length - 2));
	
	if (intDirection == 1)
	{
		if (intLeft + 240 > 0)
		{
			objBanners.animate({left: "0"}, 500);	
			
			intAutoDirection = 0;
		}
		else
		{
			objBanners.animate({left: (intLeft + 240) + "px"}, 500);
		}
	}
	else
	{
		if ((intLeft - 240) < -(240 * intBannersNum - 240))
		{
			objBanners.animate({left: "-" + (240 * intBannersNum - 240) + "px"}, 500);
			
			intAutoDirection = 1;
		}
		else
		{
			objBanners.animate({left: (intLeft - 240) + "px"}, 500);
		}
	}
	
	if (blnStop !== true)
	{
		setTimeout("ChangeBanner(" + intAutoDirection + ", false)", 5000);
	}
}


function ChangeOffers(intDirection)
{
	blnLeftUnactive = false;
	blnRightUnactive = false;
	
	intOffersNum = $("div.offers div.items-block div.item").size();
	
	objOffers = $("div.offers div.items-block div.items");
	
	strLeft = objOffers.css("left");
	intLeft = parseFloat(strLeft.substring(0, strLeft.length - 2));
	
	objItem = $("div.offers div.items-block div.item");
	
	strOfferWidth = objItem.width();
	strOfferWidth = strOfferWidth * 5;
	
	if (intDirection == 1)
	{
		if (intLeft + strOfferWidth > 0)
		{
			objOffers.animate({left: "0"}, 500);
			
			blnLeftUnactive = true;
		}
		else
		{
			objOffers.animate({left: (intLeft + strOfferWidth) + "px"}, 500);
		}
	}
	else
	{
		if ((intLeft - strOfferWidth) < -(strOfferWidth * intOffersNum / 5 - strOfferWidth))
		{
			objOffers.animate({left: "-" + (strOfferWidth * intOffersNum / 5 - strOfferWidth) + "px"}, 500);
			
			blnRightUnactive = true;
		}
		else
		{
			objOffers.animate({left: (intLeft - strOfferWidth) + "px"}, 500);
		}
	}
	
	if (blnLeftUnactive === true)
	{
		$("div.offers div.arrow-left img").hide();
		$("div.offers div.arrow-left img.unactive").show();
	}
	else
	{
		$("div.offers div.arrow-left img").show();
		$("div.offers div.arrow-left img.unactive").hide();
	}
	
	if (blnRightUnactive === true)
	{
		$("div.offers div.arrow-right img").hide();
		$("div.offers div.arrow-right img.unactive").show();
	}
	else
	{
		$("div.offers div.arrow-right img").show();
		$("div.offers div.arrow-right img.unactive").hide();
	}
}



function SwitchSectionsList()
{	
	objPreviousSlider = $("div.sections-list > div.slider");
	objPrevious = $("div.sections-list > div.list");
	
	$("div.sections-list > div.slider-hidden").attr("class", "slider");
	$("div.sections-list > div.list-hidden").attr("class", "list");
	objPreviousSlider.attr("class", "slider-hidden");
	objPrevious.attr("class", "list-hidden");
}


function SetSearchText(strText)
{	
	$("#search input[type=text]").val(strText);
}


function GetSearchResults()
{	
	strUrl = $("span#whole-site-search a").attr("href");
		
	if (strUrl)
	{
		$("span#whole-site-results").load(strUrl + " div#total-results span");
	}
}


function SwitchSelectionType()
{	
	objCurrentType = $("div.selection div.top span.type-selected");
	objNextType = $("div.selection div.top span.type");
	
	objNextType.attr("class", "type-selected");
	objCurrentType.attr("class", "type");
	
	$("div.selection form tr:not(.main)").toggleClass("visible");
}


function SwitchImage(intNewImage)
{
	newImage = "div#img-" + intNewImage;
	
	$("div#gallery > div.current").attr("class", "hidden");
	$(newImage).attr("class", "current");
}


function SwitchTextBlock(objTextBlock)
{
	if (objTextBlock.className == "hidden")
	{
		objTextBlock.className = "";
	}
	else
	{
		objTextBlock.className = "hidden";
	}
}


function ShowQuantityField(objCart, strId)
{
	if (objCart.className == "text")
	{
		objCart.className = "text-active";
		objCart.nextSibling.style.display = "";
	}
	else
	{
		objCart.className = "text";
		objCart.nextSibling.style.display = "none";
	}
	
	
	if ($(window).width() < 1100)
	{
		$("span#" + strId).parent().find('span.price-type').toggle();
		$("span#" + strId).parent().find('span.quantity').toggle();
		$("span#" + strId).toggleClass("cart-clear-m");
	}
}


function TryAddToCart(e, intComponent, intProduct, intProductDb)
{
	intKey = e.keyCode ? e.keyCode : e.which ? e.which : null;
	
	if (intKey == 13)
	{
		AddToCart(intComponent, intProduct, intProductDb);
	}
	
	return true;
}


function AddToCart(intComponent, intProduct, intProductDb)
{
	intQuantity = $("span#c-" + intComponent + "-" + intProduct + " input[type=text]").val();
	
	if (intComponent != "" && !(isNaN(intComponent)) && intProduct != "" && !(isNaN(intProduct)) && intQuantity != "" && !(isNaN(intQuantity)))
	{
		intQuantity = Math.round(intQuantity);
		
		if (intQuantity > 0)
		{			
			$.cookie("cart[" + intComponent + "/" + intProduct + "]", intComponent + "/" + intProduct, { expires: 7, path: "/" });
			$.cookie("order_quantity[" + intComponent + "/" + intProduct + "]", intQuantity, { expires: 7, path: "/" });
			
			if (intProductDb != "" && !(isNaN(intProductDb)))
			{
				$.cookie("cart_db[" + intProductDb + "]", intProductDb, { expires: 7, path: "/" });
				$.cookie("order_quantity_db[" + intProductDb + "]", intQuantity, { expires: 7, path: "/" });
			}
			else
			{
				$.cookie("cart_a[" + intComponent + "/" + intProduct + "]", intComponent + "/" + intProduct, { expires: 7, path: "/" });
				$.cookie("order_quantity_a[" + intComponent + "/" + intProduct + "]", intQuantity, { expires: 7, path: "/" });
			}
			
			$("span#c-" + intComponent + "-" + intProduct + " span.add span.text-active").text("Товар в корзине");
			$("span#c-" + intComponent + "-" + intProduct + " span.add input[type=submit]").val("Изменить");
		}
		else
		{
			$.cookie("cart[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
			$.cookie("order_quantity[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
			
			if (intProductDb != "" && !(isNaN(intProductDb)))
			{
				$.cookie("cart_db[" + intProductDb + "]", 0, { expires: 7, path: "/" });
				$.cookie("order_quantity_db[" + intProductDb + "]", 0, { expires: 7, path: "/" });
			}
			else
			{
				$.cookie("cart_a[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
				$.cookie("order_quantity_a[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
			}
			
			$("span#c-" + intComponent + "-" + intProduct + " span.add span.text-active").text("В корзину");
			$("span#c-" + intComponent + "-" + intProduct + " span.add input[type=submit]").val("Добавить");
		}
		
		$("span#c-" + intComponent + "-" + intProduct + " span.add span.text-active").attr("class", "text");
		$("span#c-" + intComponent + "-" + intProduct + " span.add span:not(.text)").hide();
		$("span#c-" + intComponent + "-" + intProduct + " span.add span.unit").show();
		
		$("span#c-" + intComponent + "-" + intProduct).parent().find('span.price-type').show();
		$("span#c-" + intComponent + "-" + intProduct).parent().find('span.quantity').show();
		$("span#c-" + intComponent + "-" + intProduct).removeClass("cart-clear-m");
		
		$("div#cart-summary").html("<img src='/themes/electron/images/loader-small.gif' alt='' />");
		$("div#cart-summary").load("/cart?_page_element=7 div#cart-summary");
	}
}


function ShowCartQuantityField(intComponent, intProduct)
{	
	if ($("span#q-" + intComponent + "-" + intProduct + " span.for span.text").attr("class") == "text")
	{
		$("span#p-" + intComponent + "-" + intProduct).hide();
		$("span#p-mark-" + intComponent + "-" + intProduct).hide();
		$("span#prs-" + intComponent + "-" + intProduct).hide();
		$("span#q-" + intComponent + "-" + intProduct + " span.for span.text").text("");
		
		$("span#q-" + intComponent + "-" + intProduct + " span.for span.text").attr("class", "text-active");
		$("span#q-" + intComponent + "-" + intProduct + " span.for span:not(.text-active)").show();
	}
	else
	{
		$("span#q-" + intComponent + "-" + intProduct + " span.for span.text-active").text($("span#q-" + intComponent + "-" + intProduct + " span.for input[name=quantity]").val() + " " + $("span#q-" + intComponent + "-" + intProduct + " span.for span:not(.text-active)").text());
		
		$("span#q-" + intComponent + "-" + intProduct + " span.for span.text-active").attr("class", "text");
		$("span#q-" + intComponent + "-" + intProduct + " span.for span:not(.text)").hide();
	}
}


function TryChangeCartPosition(e, intComponent, intProduct, intProductDb)
{
	intKey = e.keyCode ? e.keyCode : e.which ? e.which : null;
	
	if (intKey == 13)
	{
		ChangeCartPosition(intComponent, intProduct, intProductDb);
	}
	
	return true;
}


function ChangeCartPosition(intComponent, intProduct, intProductDb)
{
	$("span#p-" + intComponent + "-" + intProduct).show();
	$("span#p-mark-" + intComponent + "-" + intProduct).show();
	$("span#prs-" + intComponent + "-" + intProduct).show();
	
	intQuantity = $("span#q-" + intComponent + "-" + intProduct + " input[name=quantity]").val();
	intPrice = $("span#q-" + intComponent + "-" + intProduct + " input[name=price]").val();
	
	if (intComponent != "" && !(isNaN(intComponent)) && intProduct != "" && !(isNaN(intProduct)) && intQuantity != "" && !(isNaN(intQuantity)))
	{
		intQuantity = Math.round(intQuantity);
		
		if (intQuantity > 0)
		{			
			$.cookie("cart[" + intComponent + "/" + intProduct + "]", intComponent + "/" + intProduct, { expires: 7, path: "/" });
			$.cookie("order_quantity[" + intComponent + "/" + intProduct + "]", intQuantity, { expires: 7, path: "/" });
			
			if (intProductDb != "" && !(isNaN(intProductDb)))
			{
				$.cookie("cart_db[" + intProductDb + "]", intProductDb, { expires: 7, path: "/" });
				$.cookie("order_quantity_db[" + intProductDb + "]", intQuantity, { expires: 7, path: "/" });
			}
			else
			{
				$.cookie("cart_a[" + intComponent + "/" + intProduct + "]", intComponent + "/" + intProduct, { expires: 7, path: "/" });
				$.cookie("order_quantity_a[" + intComponent + "/" + intProduct + "]", intQuantity, { expires: 7, path: "/" });
			}
		}
		else
		{
			$.cookie("cart[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
			$.cookie("order_quantity[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
			
			if (intProductDb != "" && !(isNaN(intProductDb)))
			{
				$.cookie("cart_db[" + intProductDb + "]", 0, { expires: 7, path: "/" });
				$.cookie("order_quantity_db[" + intProductDb + "]", 0, { expires: 7, path: "/" });
			}
			else
			{
				$.cookie("cart_a[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
				$.cookie("order_quantity_a[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
			}
		}
		
		$("span#q-" + intComponent + "-" + intProduct + " span.for span.text-active").text(intQuantity + " " + $("span#q-" + intComponent + "-" + intProduct + " span.for span:not(.text-active)").text());
		
		$("span#q-" + intComponent + "-" + intProduct + " span.for span.text-active").attr("class", "text");
		$("span#q-" + intComponent + "-" + intProduct + " span.for span:not(.text)").hide();
		
		
		intQuantity = parseFloat(intQuantity);
		intPrice = parseFloat(intPrice);
		
		intSum = intQuantity * intPrice;
		
		intSumRub = parseInt(intSum);
		intSumCop = intSum - intSumRub;	
		
		intSumRub = intSumRub.toString();
		intSumCop = intSumCop.toString();
		intSumCop = intSumCop.substring(2, 4);
		
		if (intSumRub.length > 9)
		{
			intSumRub = intSumRub.substring(0, intSumRub.length - 9) + ' ' + intSumRub.substring(intSumRub.length - 9, intSumRub.length - 6) + ' ' + intSumRub.substring(intSumRub.length - 6, intSumRub.length - 3) + ' ' + intSumRub.substring(intSumRub.length - 3, intSumRub.length);
		}
		else if (intSumRub.length > 6)
		{
			intSumRub = intSumRub.substring(0, intSumRub.length - 6) + ' ' + intSumRub.substring(intSumRub.length - 6, intSumRub.length - 3) + ' ' + intSumRub.substring(intSumRub.length - 3, intSumRub.length);
		}
		else if (intSumRub.length > 3)
		{
			intSumRub = intSumRub.substring(0, intSumRub.length - 3) + ' ' + intSumRub.substring(intSumRub.length - 3, intSumRub.length);
		}
		
		if (intSumCop.length == 0)
		{
			intSumCop = "00";
		}
		else if (intSumCop.length == 1)
		{
			intSumCop = intSumCop + "0";
		}
		
		if (intSumRub == 0 && intSumCop == 0)
		{
			intSumRub = "??";
			intSumCop = "??";
		}
		
		$("span#p-" + intComponent + "-" + intProduct + " span.rub").text(intSumRub + ",");
		$("span#p-" + intComponent + "-" + intProduct + " sup.cop").text(intSumCop);
		
		$("div#cart-summary").html("<img src='/themes/electron/images/loader-small.gif' alt='' />");
		$("div#cart-summary").load("/cart?_page_element=7 div#cart-summary");
		
		$("div#total .standard").html("<img src='/themes/electron/images/loader.gif' alt='' />");
		$("div#total .e-shop").text("");
		$("div#total .standard").load("/cart?_page_element=7 div#cart-summary .sum");
		
		setTimeout("CountDiscount()", 2000);
	}
}


function CountDiscount()
{
	strTotal = $("div#total .standard").text();
	
	if (strTotal != "")
	{
		strTotal = strTotal.replace(/[^\d]/g, "");
		strTotal = strTotal.substring(0, strTotal.length - 2) + "." + strTotal.substring(strTotal.length - 2);
		strTotal = parseFloat(strTotal);
		
		intSum = strTotal * 0.95;
		
		intSumRub = parseInt(intSum);
		intSumCop = intSum - intSumRub;	
		
		intSumRub = intSumRub.toString();
		intSumCop = intSumCop.toString();
		intSumCop = intSumCop.substring(2, 4);
		
		if (intSumRub.length > 9)
		{
			intSumRub = intSumRub.substring(0, intSumRub.length - 9) + ' ' + intSumRub.substring(intSumRub.length - 9, intSumRub.length - 6) + ' ' + intSumRub.substring(intSumRub.length - 6, intSumRub.length - 3) + ' ' + intSumRub.substring(intSumRub.length - 3, intSumRub.length);
		}
		else if (intSumRub.length > 6)
		{
			intSumRub = intSumRub.substring(0, intSumRub.length - 6) + ' ' + intSumRub.substring(intSumRub.length - 6, intSumRub.length - 3) + ' ' + intSumRub.substring(intSumRub.length - 3, intSumRub.length);
		}
		else if (intSumRub.length > 3)
		{
			intSumRub = intSumRub.substring(0, intSumRub.length - 3) + ' ' + intSumRub.substring(intSumRub.length - 3, intSumRub.length);
		}
		
		if (intSumCop.length == 0)
		{
			intSumCop = "00";
		}
		else if (intSumCop.length == 1)
		{
			intSumCop = intSumCop + "0";
		}
		
		if (intSumRub == 0 && intSumCop == 0)
		{
			intSumRub = "??";
			intSumCop = "??";
		}
		
		$("div#total .e-shop").html("Итого со скидкой: " + intSumRub + "," + intSumCop + " руб.&nbsp;&mdash;&nbsp;при заказе через интернет");
	}
}


function DeleteCartPosition(intComponent, intProduct, intProductDb)
{
	intQuantity = "0";
	
	if (intComponent != "" && !(isNaN(intComponent)) && intProduct != "" && !(isNaN(intProduct)) && intQuantity != "" && !(isNaN(intQuantity)))
	{
		intQuantity = Math.round(intQuantity);
		
		$.cookie("cart[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
		$.cookie("order_quantity[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
		
		if (intProductDb != "" && !(isNaN(intProductDb)))
		{
			$.cookie("cart_db[" + intProductDb + "]", 0, { expires: 7, path: "/" });
			$.cookie("order_quantity_db[" + intProductDb + "]", 0, { expires: 7, path: "/" });
		}
		else
		{
			$.cookie("cart_a[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
			$.cookie("order_quantity_a[" + intComponent + "/" + intProduct + "]", 0, { expires: 7, path: "/" });
		}
	
		$("div#item-" + intComponent + "-" + intProduct).remove();
		$("div#cart-summary").html("<img src='/themes/electron/images/loader-small.gif' alt='' />");
		$("div#cart-summary").load("/cart?_page_element=7 div#cart-summary");
		
		$("div#total .standard").html("<img src='/themes/electron/images/loader.gif' alt='' />");
		$("div#total .e-shop").text("");
		$("div#total .standard").load("/cart?_page_element=7 div#cart-summary .sum");
		
		setTimeout("CountDiscount()", 2000);
	}
}


function CheckAllFields()
{
	$("form.standard .value *").map(function(){ CheckField($(this)); });
}

	
function CheckField(objField)
{
	blnError = false;
	
	objParent = objField.parent();
	
	if (objParent.hasClass("required") && !(objParent.hasClass("range-integers") || objParent.hasClass("range-doubles") || objParent.hasClass("range-years") || objParent.hasClass("range-dates")))
	{
		if (objField.val() == "")
		{
			objParent.prev().addClass("error");
			
			blnError = true;
		}
		else
		{
			objParent.prev().removeClass("error");	
		}
	}
	
	if (!blnError)
	{
		if (objParent.hasClass("integer") || objParent.hasClass("multiinteger"))
		{
			if (objField.val() != "" && isNaN(objField.val()))
			{
				objParent.prev().addClass("error");
			}
			else
			{
				objParent.prev().removeClass("error");	
			}
		}
		
		if (objParent.hasClass("mail") || objParent.hasClass("multimail"))
		{
			if (objField.val() != "" && !(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-z]{2,4}|[0-9]{1,3})(\]?)$/.test(objField.val())))
			{
				objParent.prev().addClass("error");
			}
			else
			{
				objParent.prev().removeClass("error");	
			}
		}
		
		if (objParent.hasClass("phone") || objParent.hasClass("multiphone"))
		{
			if (objField.val() != "" && !(/^(\+\d{1,3}\s{1}\d{2,5}\s{1}\d{5,7})$/.test(objField.val())))
			{
				objParent.prev().addClass("error");
			}
			else
			{
				objParent.prev().removeClass("error");	
			}
		}
		
		if (objParent.hasClass("double") || objParent.hasClass("multidouble"))
		{
			if (objField.val() != "" && !(/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/.test(objField.val())))
			{
				objParent.prev().addClass("error");
			}
			else
			{
				objParent.prev().removeClass("error");	
			}
		}
		
		if (objParent.hasClass("password"))
		{
			if (objField.attr("name").substring(objField.attr("name").length - 13) != "-confirmation")
			{
				strConfirmationName = objField.attr("name") + "-confirmation";
			}
			else
			{
				strConfirmationName = objField.attr("name").substring(0, objField.attr("name").length - 13);
			}
			
			objConfirmation = $("form.standard .value *[name=" + strConfirmationName + "]");
			
			if (!((objField.val() == "" && objConfirmation.val() == "") || ((/^(([a-z0-9_-]){4,20})$/.test(objField.val())) && objField.val() == objConfirmation.val())))
			{
				objParent.prev().addClass("error");
				objConfirmation.parent().prev().addClass("error");
			}
			else
			{
				objParent.prev().removeClass("error");	
				objConfirmation.parent().prev().removeClass("error");
			}
		}
	}
	
	
	strFormId = $("form.standard:has(td.value *[id=" + objField.attr("id") + "])").attr("id");
	
	if ($("form.standard[id=" + strFormId + "] td.error").size() == 0)
	{
		$("form.standard[id=" + strFormId + "] .buttons input[name^=save]").attr("disabled", "");
		$("form.standard[id=" + strFormId + "] .buttons input[name^=apply]").attr("disabled", "");
	}
	else
	{
		// $("form.standard[id=" + strFormId + "] .buttons input[name^=save]").attr("disabled", "disabled");
		// $("form.standard[id=" + strFormId + "] .buttons input[name^=apply]").attr("disabled", "disabled");
	}
}

function EqualAddress($strAdressType)
{
	if ($strAdressType == "real")
	{
		if ($("form.standard input[name=equals-legal-address]").attr("checked"))
		{
			$("form.standard .value *[name='real-postal-code']").val($("form.standard .value *[name='legal-postal-code']").val());
			$("form.standard .value *[name='real-region']").val($("form.standard .value *[name='legal-region']").val());
			$("form.standard .value *[name='real-place']").val($("form.standard .value *[name='legal-place']").val());
			$("form.standard .value *[name='real-street']").val($("form.standard .value *[name='legal-street']").val());
			$("form.standard .value *[name='real-house']").val($("form.standard .value *[name='legal-house']").val());
			$("form.standard .value *[name='real-building']").val($("form.standard .value *[name='legal-building']").val());
			$("form.standard .value *[name='real-office']").val($("form.standard .value *[name='legal-office']").val());
			
			$("form.standard .value *[name*='legal-']").change(function(){ OnEqualChange($(this)); });
			
			$("form.standard .value *[name*='real-']").attr("readonly", "readonly");
			$("form.standard .value *[name*='real-']").addClass("readonly");
			
			
			if ($("form.standard input[name=equals-real-address]").attr("checked"))
			{
				$("form.standard .value *[name='delivery-postal-code']").val($("form.standard .value *[name='real-postal-code']").val());
				$("form.standard .value *[name='delivery-region']").val($("form.standard .value *[name='real-region']").val());
				$("form.standard .value *[name='delivery-place']").val($("form.standard .value *[name='real-place']").val());
				$("form.standard .value *[name='delivery-street']").val($("form.standard .value *[name='real-street']").val());
				$("form.standard .value *[name='delivery-house']").val($("form.standard .value *[name='real-house']").val());
				$("form.standard .value *[name='delivery-building']").val($("form.standard .value *[name='real-building']").val());
				$("form.standard .value *[name='delivery-office']").val($("form.standard .value *[name='real-office']").val());
			}
		}
		else
		{
			$("form.standard .value *[name*='legal-']").unbind("change");		
			$("form.standard .value *[name*='real-']").attr("readonly", "");
			$("form.standard .value *[name*='real-']").removeClass("readonly");	
		}
	}
	else
	{
		if ($("form.standard input[name=equals-real-address]").attr("checked"))
		{
			$("form.standard .value *[name='delivery-postal-code']").val($("form.standard .value *[name='real-postal-code']").val());
			$("form.standard .value *[name='delivery-region']").val($("form.standard .value *[name='real-region']").val());
			$("form.standard .value *[name='delivery-place']").val($("form.standard .value *[name='real-place']").val());
			$("form.standard .value *[name='delivery-street']").val($("form.standard .value *[name='real-street']").val());
			$("form.standard .value *[name='delivery-house']").val($("form.standard .value *[name='real-house']").val());
			$("form.standard .value *[name='delivery-building']").val($("form.standard .value *[name='real-building']").val());
			$("form.standard .value *[name='delivery-office']").val($("form.standard .value *[name='real-office']").val());
			
			$("form.standard .value *[name*='real-']").change(function(){ OnEqualChange($(this)); });
			
			$("form.standard .value *[name*='delivery-']").attr("readonly", "readonly");
			$("form.standard .value *[name*='delivery-']").addClass("readonly");
		}
		else
		{
			$("form.standard .value *[name*='real-']").unbind("change");			
			$("form.standard .value *[name*='delivery-']").attr("readonly", "");
			$("form.standard .value *[name*='delivery-']").removeClass("readonly");	
		}
	}
	
	CheckAllFields();
}

function OnEqualChange(objField)
{
	strName = objField.attr("name");
	
	if (strName.substring(0, 6) == "legal-")
	{
		strName = strName.substring(6, strName.length);
		
		$("form.standard .value *[name=real-" + strName + "]").val(objField.val());
		CheckField($("form.standard .value *[name=real-" + strName + "]"));
		
		if ($("form.standard input[name=equals-legal-address]").attr("checked") && $("form.standard input[name=equals-real-address]").attr("checked"))
		{
			$("form.standard .value *[name=delivery-" + strName + "]").val(objField.val());	
			CheckField($("form.standard .value *[name=delivery-" + strName + "]"));
		}
	}
	else
	{
		strName = strName.substring(5, strName.length);
		
		$("form.standard .value *[name=delivery-" + strName + "]").val(objField.val());
		CheckField($("form.standard .value *[name=delivery-" + strName + "]"));
	}
}

function BuildPhone(objField)
{
	strName = objField.attr("id");
	strName = strName.substring(0, strName.length - 2)
	
	strPhone = "+" + $("form.standard .value *[id=" + strName + "-1]").val() + " " + $("form.standard .value *[id=" + strName + "-2]").val() + " " + $("form.standard .value *[id=" + strName + "-3]").val();
	
	if (strPhone == "+  ")
	{
		strPhone = "";
	}
	
	$("form.standard .value *[name=" + strName + "]").val(strPhone);
	
	CheckField($("form.standard .value *[name=" + strName + "]"));
}

function OnSubmit(objForm)
{
	if (!($("form.standard .value *[name=password]").parent().hasClass("required")) && !($("form.standard .value *[name=password-confirmation]").parent().hasClass("required")) && $("form.standard .value *[name=password]").val() == "")
	{		
		$("form.standard .value *[name=password]").parent().empty();
		$("form.standard .value *[name=password-confirmation]").parent().empty();
	}
}


function AddBranchesListToCart()
{
	if ($("form#section-50277").size() || $("form#section-50276").size())
	{
		if ($("div#customer-type").text() == "Я — розничный покупатель")
		{
			strBranchType = "shop";
		}
		else
		{
			strBranchType = "office";
		}
		
		strAppend = "<div class='delivery-branches'>";
		strAppend += "<div>Вы можете указать свой адрес для доставки заказа или выбрать филиал, чтобы забрать заказ самостоятельно:</div>";
		
		k = 0;
		
		$("div#contacts .divisions ." + strBranchType).map(function()
			{ 
				strCity = $(this).closest(".col").find(".title").text();
				
				strAddress = $(this).find("a").text();
				// strAddress = strAddress.split(/ /g).join("");
				arrAddress = strAddress.split(", ");
				
				arrBranches[k] = Array();
				
				arrBranches[k][0] = '000000';
				arrBranches[k][1] = 'Нижегородская область';
				arrBranches[k][2] = strCity;
				arrBranches[k][3] = arrAddress[0];
				arrBranches[k][4] = arrAddress[1];
				arrBranches[k][5] = '';
				arrBranches[k][6] = '';
				
				
				strAppend += "<div>";
				strAppend += "<span class='service' onclick='SetBranchToDelivery(" + k + ");'>";
				strAppend += arrBranches[k][2] + ", " + arrBranches[k][3] + ", " + arrBranches[k][4];
				
				if (arrAddress[2] != undefined)
				{
					arrBranches[k][5] = arrAddress[2];
					strAppend += ", " + arrBranches[k][5];
				}
				
				if (arrAddress[3] != undefined)
				{
					arrBranches[k][6] = arrAddress[3];
					strAppend += ", " + arrBranches[k][6];
				}
				
				strAppend += "</span>";
				strAppend += "</div>";
				
				k++;
			}
		);
		
		strAppend += "</div>";
		
		$("form td.group span.group-title").map(function(){ if ($(this).text() == "Адрес доставки") $(this).closest("td").append(strAppend) });
	}
}


function SetBranchToDelivery(intId)
{
	$("form [name=delivery-postal-code]").val(arrBranches[intId][0]);
	$("form [name=delivery-region]").val(arrBranches[intId][1]);
	$("form [name=delivery-place]").val(arrBranches[intId][2]);
	$("form [name=delivery-street]").val(arrBranches[intId][3]);
	$("form [name=delivery-house]").val(arrBranches[intId][4]);
	$("form [name=delivery-building]").val(arrBranches[intId][5]);
	$("form [name=delivery-office]").val(arrBranches[intId][6]);
	
	CheckAllFields();
}


function fixPNG(element)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{		
		src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
		  
		if (src)
		{
			src = src[1];
			element.runtimeStyle.backgroundImage = "none";
		}

		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
	}
}
