$(document).ready(initLoad);

var is_shown=false;

function initLoad()
	{
	i = new Image(90,23);
	i.src="/confirm/r.gif";
	$("img#confirmImage").bind("click",function(){$("img#confirmImage").attr("src","/confirm/r.gif");r=Math.random().toString().replace('.','')+Math.random().toString().replace('.','');newImg="/confirmimage-"+r+".png";setTimeout("newConfirmImage('"+newImg+"')",2000);});

	menus=$("div#topMenu .mainMenu");
	zi=11+menus.length;
	for(i=0;i<menus.length;i++)
		{
		zi--;
		menus[i].style.zIndex=zi;
		}
	isShadowed=false;
	$("div#topMenu .mainMenu").bind("click",function(){
														 $(".subMenu").removeShadow();
														 $(".subMenu").hide();
														 $(this).find(".subMenu").show();
														 $(this).find(".subMenu").dropShadow({left: 0, top: 6, blur: 3, opacity: 0.3, color:'#000000', swap:false});
														});
	$(".leftColumn").bind("mouseover",function(){$(".subMenu").removeShadow();$(".subMenu").hide();});
	$(".rightColumn").bind("mouseover",function(){$(".subMenu").removeShadow();$(".subMenu").hide();});
	$(".centerColumn fieldset").bind("mouseover",function(){$(".subMenu").removeShadow();$(".subMenu").hide();});
	$(".centerColumn .infoblock").bind("mouseover",function(){$(".subMenu").removeShadow();$(".subMenu").hide();});

	$("img").bind("error",function(){$(this).attr("src","/i/noimage.jpg");});

	$("a#showResponsesLink").bind("click",function(){
													o=$(this).offset().top;
													$("html").animate({scrollTop:o},"slow");
													$(this).hide();
													$("a#hideResponsesLink").show();
													$("div#prodResponsesList").slideDown();
													$("div#prodResponsesList").fadeIn();
													});
	$("a#hideResponsesLink").bind("click",function(){
													o=$("table#productTable").offset().top;
													$("html").animate({scrollTop:o},"slow");
													$(this).hide();
													$("a#showResponsesLink").show();
													$("div#prodResponsesList").slideUp();
													});
/*	$("table#catalogListingTableWP img").bind("click",function(){
																if($(this).attr("src")!="/i/noimage.jpg")
																	{
																	showProductImage($(this).attr("src"));
																	}
																});
*/

	$("div#fullShopAlertMin area").tooltip({ 
	    track:true, 
	    delay:0, 
	    showURL:false, 
	    showBody:" - ", 
	    extraClass:"pretty", 
	    fixPNG:true, 
	    opacity:0.95,
	    left:-220 
	});
	$("#contactsForm input[type=text], input#rospostselect, input#rbkselect, input#webmoneyselect, img#emstitle").tooltip({ 
	    track:true, 
	    delay:0, 
	    showURL:false, 
	    showBody:" - ", 
	    extraClass:"pretty", 
	    fixPNG:true, 
	    opacity:0.95,
	    left:0 
	});
	
	$("form#rbkjsorderform").submit();
	$("div#streetList div").live("mouseover",function(){$(this).css("background","#dcdcdc");});
	$("div#streetList div").live("mouseout",function(){$(this).css("background","#ffffff");});

	$("#popupInfo").draggable({handle:"th"});
	}

function newConfirmImage(i)
	{
	$("img#confirmImage").attr("src",i);
	}

var shopShadowed=false;
function showShopWarning()
	{
	if(shopShadowed==false)
		{
		$("div#shopWarning").show();
		$("div#shopWarning").dropShadow({left: 2, top: 2, blur: 3, opacity: 0.2, color:'#000000', swap:false});
		shopShadowed=true;
		}
	}
function hideShopWarning()
	{
	$("div#shopWarning").hide();
	$("div#shopWarning").removeShadow();
	shopShadowed=false;
	}


function formSubmit(fid)
	{
	arr=$("form#"+fid+" td input[type=text],form#"+fid+" td textarea");
	counter=0;
	for(i=0;i<arr.length;i++)
		{
		if(arr[i].className.indexOf('notNeed')+1) {}
		else
			{
			if(arr[i].value=="")
				{
				arr[i].style.background="#9be76f";
				counter++;
				}
			else
				{
				arr[i].style.background="#ffffff";
				}
			}
		}
	if(counter!=0)
		{
		alert("Вы не заполнили обязательные поля (выделены цветом).");
		}
	else
		{
		if(fid=="contactsForm")
			{
			isvalid=true;
			email=$("form#"+fid+" input[name=param[13]]").val().replace(/^\s+|\s+$/g,'');
			$("form#"+fid+" input[name=param[13]]").val(email);
			if(!(/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email))
				{
				alert("Вы неправильно ввели E-Mail, проверьте.");
				isvalid=false;
				}
			phone=$("form#"+fid+" input[name=param[14]]").val().replace(/^\s+|\s+$/g,'');
			$("form#"+fid+" input[name=param[14]]").val(phone);
			if(!(/^[0-9]*$/i).test(phone) || phone.length<11)
				{
				alert("Вы неправильно ввели телефон, проверьте.");
				isvalid=false;
				}
			pindx=$("form#"+fid+" input[name=param[9]]").val().replace(/^\s+|\s+$/g,'');
			$("form#"+fid+" input[name=param[9]]").val(pindx);
			if(!(/^[0-9]*$/i).test(pindx) || (pindx.length<6 && pindx.length>0))
				{
				alert("Вы неправильно ввели индекс, проверьте.");
				isvalid=false;
				}
			if(isvalid==true)
				{
				$("form#"+fid).submit();
				}
			}
		else
			{
			$("form#"+fid).submit();
			}
		}
	}




function addproduct(p,c)
	{
	if(!(/^[0-9]*$/i).test(c) || c.length==0 || c==0) alert("Введите количество товара (число)!");
	else
		{
	    $("div#ajaxOpacity").css("height",$(document).height()+200);
	    $("div#ajaxOpacity").show();
	    $("div#ajaxOpacity").fadeTo("fast",0.7);
		$.post("/shopping.html",
			{
			ajax:"yes",
			actiontype:"add",
			product:p,
			count:c
			},
		function(data)
			{
			$dataArray=data.split("iee1394dak");
	
			$("div#ajaxTable").html($dataArray[5]);
			$("span#fullShopAlertCount").html($dataArray[0]);
			$("span#fullShopAlertCost").html($dataArray[1]);
			$("span#fullShopAlertMinCount").html($dataArray[0]);
			$("span#fullShopAlertMinCost").html($dataArray[1]);
			$("div#emptyShopAlert").css("display",$dataArray[2]);
			$("div#fullShopAlert").css("display",$dataArray[3]);
			$("div#fullShopAlertMin").css("display",$dataArray[4]);
	
			$("div#ajaxTable").css("top",$(document).scrollTop()+100);
			$("div#ajaxTable").fadeIn("fast");
			$("div#productAddingFormBlock").html("<p>Этот товар уже есть в Вашей корзине.</p>");
			},
			"html");
		}
	}

function closeAjaxTable()
	{
    $("div#ajaxOpacity").fadeOut("slow");
//	$("div#ajaxTable").fadeOut("fast");
	$("div#ajaxTable").css("overflow","hidden").animate({width:"0px",height:"0px",top:"100px"},"fast").hide();
	}

function autofil(code)
	{
	$("img#ajaxloader_post").css("visibility","");
	$.post("/modules/postcodes.php",
		{
		postindex:code
		},
	function(data)
		{
		if(data.found=="1")
			{
			if(data.AUTONOM!="")
				{
				$("form#contactsForm input[name=param[1]]").val(data.AUTONOM+", "+data.REGION);
				}
			else
				{
				$("form#contactsForm input[name=param[1]]").val(data.REGION);
				}
			if(data.CITY=="")
				{
				$("form#contactsForm input[name=param[2]]").val(data.REGION);
				}
			else
				{
				$("form#contactsForm input[name=param[2]]").val(data.CITY);
				}
			$("form#contactsForm input[name=param[3]]").val(data.AREA);
			if(data.CITY_1!="")
				{
				$("form#contactsForm input[name=param[2]]").val(data.CITY+", "+data.CITY_1);
				$("form#contactsForm input[name=param[4]]").val(data.CITY_1);
				}
			if(data.INDEX!=code)
				{
				$("form#contactsForm input[name=param[9]]").val(data.INDEX);
				alert("Вы ввели индекс в старом формате. Новый индекс Ващего отделения "+data.INDEX);
				}
			if(data.streets!="empty") { $("div#streetList").html(data.streets); }
			}
		else
			{
			alert("Указанный индекс не найден.");
			}
		$("img#ajaxloader_post").css("visibility","hidden");
		},
		"json");
	}

function autofilByMail(code)
	{
	$("img#ajaxloader_mail").css("visibility","");
	$.post("/modules/mails.php",
		{
		postindex:code
		},
	function(data)
		{
		if(data.found=="1")
			{
			$("form#contactsForm input[name=param[1]]").val(data.uregion);
			$("form#contactsForm input[name=param[2]]").val(data.ucity);
			$("form#contactsForm input[name=param[3]]").val(data.uarea);
			$("form#contactsForm input[name=param[4]]").val(data.uplace);
			$("form#contactsForm input[name=param[5]]").val(data.ustreet);
			$("form#contactsForm input[name=param[6]]").val(data.uhouse);
			$("form#contactsForm input[name=param[7]]").val(data.ublock);
			$("form#contactsForm input[name=param[8]]").val(data.uflat);
			$("form#contactsForm input[name=param[9]]").val(data.uindex);
			$("form#contactsForm input[name=param[10]]").val(data.name1);
			$("form#contactsForm input[name=param[11]]").val(data.name2);
			$("form#contactsForm input[name=param[12]]").val(data.name3);
			$("form#contactsForm input[name=param[13]]").val(data.umail);
			$("form#contactsForm input[name=param[14]]").val(data.uphone);
			}
		else
			{
			if(confirm("Пользователь с таким E-Mail не найден. Очистить поля формы?"))
				{
				$("form#contactsForm input[name=param[1]]").val("");
				$("form#contactsForm input[name=param[2]]").val("");
				$("form#contactsForm input[name=param[3]]").val("");
				$("form#contactsForm input[name=param[4]]").val("");
				$("form#contactsForm input[name=param[5]]").val("");
				$("form#contactsForm input[name=param[6]]").val("");
				$("form#contactsForm input[name=param[7]]").val("");
				$("form#contactsForm input[name=param[8]]").val("");
				$("form#contactsForm input[name=param[9]]").val("");
				$("form#contactsForm input[name=param[10]]").val("");
				$("form#contactsForm input[name=param[11]]").val("");
				$("form#contactsForm input[name=param[12]]").val("");
				$("form#contactsForm input[name=param[13]]").val("");
				$("form#contactsForm input[name=param[14]]").val("");
				}
			}
		$("img#ajaxloader_mail").css("visibility","hidden");
		},
		"json");
	}

function showProductImage(l)
	{
    $("div#ajaxOpacity").css("height",$(document).height()+200).show().fadeTo("fast",0.7);
	$("div#ajaxTable").html("<table align=\"center\" style=\"background:#ffffff;border:none;border-collapse:collapse;\"><tr><td style=\"padding:10px;text-align:right;\"><img src=\"/i/closeShopWarning.gif\" alt=\"\" style=\"cursor:hand;cursor:pointer;\" onclick=\"hideProductImage();\" /></td></tr><tr><td style=\"padding:0px 10px 10px 10px;\"><img src=\""+l+"\" alt=\"\" /></td></tr></table>").css("top",$(document).scrollTop()+100).fadeIn("fast");
	}
function hideProductImage(l)
	{
    $("div#ajaxOpacity").fadeOut("fast");
	$("div#ajaxTable").fadeOut("fast").html("");
	}


function showhidelayer(l)
	{
	if($('div#'+l).css("display")=="none")
		{
		$('div#'+l).slideDown('fast');
		}
	else
		{
		$('div#'+l).slideUp('fast');
		}
	}

function street(l)
	{
	$("form#contactsForm input[name=param[5]]").val(l.innerHTML);
	$("div#streetList").hide();
	}

function updateEmsPrice(ems)
	{
	emsprice = parseFloat(ems.value);
	orderprice = parseFloat($("input.payform_price_recalc:first").val());
	if(ems.checked == true)
		{
		price = orderprice + emsprice;
		$("input.payform_price_recalc").val(price);
		$("div#ems_select_alert").show();
		$("tr#ruspost_pay_type_tr").hide();
		$("tr#ruspost_pay_type_tr_disable").show();
		}
	else
		{
		price = orderprice - emsprice;
		$("input.payform_price_recalc").val(price);
		$("div#ems_select_alert").hide();
		$("tr#ruspost_pay_type_tr").show();
		$("tr#ruspost_pay_type_tr_disable").hide();
		}
	}

function payTypeSelecting(orderid,finalpriceid,payformid,paytype,postwithoutbox)
	{
	isems = ($("input#emspostchecker").attr("checked")==true) ? 1 : 0;
	finalprice = $("input#"+finalpriceid).val();

	switch(paytype)
		{
		case "post":paytypestring="Pay on delivery";break;
		case "rbk":paytypestring="RBK Money";break;
		case "wm":paytypestring="Web Money";break;
		case "ym":paytypestring="Yandex Money";break;
		default:paytypestring="Undefined";break;
		}

	$.post("/modules/paytypeselect.php",
		{
		orderid:orderid,
		finalprice:finalprice,
		isems:isems,
		paytype:paytype,
		paytypestring:paytypestring,
		postwithoutbox:postwithoutbox
		},
	function(data)
		{
/*
		if(data["selected"]=="yes")
			{
			$("form#"+payformid).submit();
			}
		else
			{
			alert("Ошибка!");
			}
*/
		},
		"json");

	}



function showPopupInfoBlock(obj,info)
	{
	l = 0;
	t = 0;
	l = $(obj).offset().left - 200;
	t = $(obj).offset().top - 40;
	$("div#popupInfo").css("left",l+"px");
	$("div#popupInfo").css("top",t+"px");

	$("div#popupInfo").show();
	$.post("/modules/popupinfo.php",
		{
		nick:info
		},
	function(data)
		{
		if(data["founded"]=="yes")
			{
			$("div#popupInfo .piHeader").html(data["header"]);
			$("div#popupInfo .piContent").html(data["content"]);
			$("div#popupInfo .piLoader").hide();
			$("div#popupInfo .piHeader").slideDown("fast");
			$("div#popupInfo .piContent").slideDown("fast");
			$("div#popupInfo .piFooter").slideDown("fast");
			}
		else
			{
			$("div#popupInfo .piContent").html("Запрашиваемая информация не найдена!");
			$("div#popupInfo .piLoader").hide();
			$("div#popupInfo .piContent").slideDown("fast");
			$("div#popupInfo .piFooter").slideDown("fast");
			}
		},
		"json");
	return false;
	}
function hidePopupInfoBlock()
	{
	$("div#popupInfo").hide();
	$("div#popupInfo .piLoader").show();
	$("div#popupInfo .piHeader").html("");
	$("div#popupInfo .piContent").html("");
	$("div#popupInfo .piHeader").hide();
	$("div#popupInfo .piContent").hide();
	$("div#popupInfo .piFooter").hide();
	$("div#popupInfo").css("left","0px");
	$("div#popupInfo").css("top","0px");
	}
