var RND=parseInt(Math.random()*100000);
var weight=0;
var index=0;


// При поиске
var counter=0;
var nameAr = new Array();
var cna = 0;
var flag = 0;
function begTr(n,name){
	flag = 0;
	for ( var j=0; j<nameAr.length; j++ ){
					if ( nameAr[j] == name ){
								flag = 1;
								j = nameAr.length;
					}
	}
	if ( flag == 0 )
		nameAr[cna++] = name;
}

function showcpecimg(im)
{
var j=0;
if (im==3) { document.write("<img class=immarg src=/i/hot.gif>"); j=1; }

if (im==1) { document.write("<img class=immarg src=/i/new.gif>"); j=1; }

if (im==2) { document.write("<img class=immarg src=/i/garant.gif>"); j=1; }
if (j==0) document.write("<br><br><br>");
j=0;
}
function endTr(n){
	if ( n < 4 ){
		document.write("</td>");
		if ( flag == 1 )
			counter--;
	}
	if ( n == 4 ){
		document.write("</td></tr>");
		if ( flag == 1 )
			counter--;
		else
			counter=0;
	}
}

function endTr2(n){
		if ( n != 0 ){
				for ( var i=n; i < 4; i++ ){
								document.write("<td width=25% class=spec>&nbsp;</td>");
				}
				document.write("</tr>");
		}
}







function normalizename(s) {

var i = 0;
var g = "";

for (i=1;i<s.length+1; i++)  
{


	if (s.substring(i-1,i) == " ")
	{

g+="%20";
}
if (s.substring(i-1,i) != " " )
	{
g+=s.substring(i-1,i);
}
if (s.substring(i,i+1) == " " && s.substring(i+1,i+2) == " ") break;
}
document.write(g);
}






function searchResult(idcat,idgoods,n,name,content){
	counter++;
	n++;
	begTr(n,name);
	if ( flag == 0 ){
			if ( n == 1 )
				document.write("<tr valign=top><td width=25% class=spec>");
			if ( n <= 4 && n > 1 ) 
			

			document.write("<td width=25% class=spec>");

			document.write(content+"<a href='javascript:show_win(500,200,\"/basket_add.html?idc="+idcat+"&idg="+idgoods+"&c=addbask&ctype=showbask&r="+RND+"\")'><img src='/i/basketNew.gif' width=79 height=17 border=0 alt='в корзину' align=absmiddle></a></div></div>");



			if ( n < 4 )
				document.write("</td>");
			if ( n == 4 ){
				document.write("</td></tr>");
				counter=0;
			}
	}

	if ( flag == 1 )
		counter--;
}

function searchfirm(idcat,idgoods,n,name,content){
	counter++;
	n++;
	begTr(n,name);
	if ( flag == 0 ){
			if ( n == 1 )
				document.write("<tr valign=top><td width=25% class=spec>");
			if ( n <= 3 && n > 1 )
				document.write("<td width=25% class=spec>");

			document.write(content+"<a href='javascript:show_win(500,200,\"/basket_add.html?idc="+idcat+"&idg="+idgoods+"&c=addbask&ctype=showbask&r="+RND+"\")'><img src='/i/basketNew.gif' width=79 height=17 border=0 alt='в корзину' align=absmiddle></a></div></div>");

			if ( n < 3 )
				document.write("</td>");
			if ( n == 3 ){
				document.write("</td></tr>");
				counter=0;
			}
	}

	if ( flag == 1 )
		counter--;
}

















function show_win(width,height,s_url){
	var p_top=(window.screen.height-height)/2, p_left=(window.screen.width-width)/2;
	var win=window.open(s_url,"imgswin","resizable=yes,scrollbars=yes,toolbar=no,location=no,directoties=no,status=no,menubar=no,width="+width+",height="+height+",top="+p_top+",left="+p_left);
	win.focus();
}

function show_print(width,height,s_url,idg){
	var p_top=(window.screen.height-height)/2, p_left=(window.screen.width-width)/2;
	var win_name="win_"+idg;
	var win_print=window.open(s_url,win_name,"resizable=yes,scrollbars=yes,toolbar=yes,location=no,directoties=no,status=no,menubar=no,width="+width+",height="+height+",top="+p_top+",left="+p_left);
	win_print.focus();
}

function active_win(surl){
	window.opener.document.location=surl;
}

function add_bask_img(idcat,idgoods){
	document.write("<a href='javascript:show_win(500,200,\"/basket_add.html?idc="+idcat+"&idg="+idgoods+"&c=addbask&ctype=showbask&r="+RND+"\")'><img src='/i/basketNew.gif' width=79 height=17 border=0 alt='в корзину' align=absmiddle></a>");
}

function add_bask_text(idcat,idgoods){
	document.write("<a href='javascript:show_win(500,200,\"/basket_add.html?idc="+idcat+"&idg="+idgoods+"&c=addbask&ctype=showbask&r="+RND+"\")' class=baskLink><nobr>купить<img src='/i/basket.gif' width=12 height=10 border=0 alt='в корзину' align=absbottom class=img1></nobr></a>");
}

function set_selected(p,v){
	for (i=0; i < p.options.length; i++) {
		if (p.options[i].value==v) { p.options[i].selected=true; break; }
	}
}

function set_radio(p,v){
	for (i=0; i < p.length; i++) {
		if (p[i].value==v) { p[i].checked=true; break; }
	}
}

function get_radio(p){
var res="";
	for (i=0; i < p.length; i++) {
		if (p[i].checked) { res=p[i].value; break; }
	}
	return res;
}


function set_checked(p,v){
	if (p.value==v) p.checked=true; 
}


function show_parent_div(div_par){
    var IDParentElement="", NextParentElement="";
		var obj=null; 
		var obj1=null;
    if (document.getElementById(div_par)!=null)
    {
			obj1=document.getElementById(div_par);
			for(var i=0; i<50; i++) {
				obj=obj1;
				if (obj!=null) {
					if (obj.id!="" && obj.style.display!="block") {
						obj.style.display="block";
//////////////////// для смены класса у рубрики с #LEVEL# = 0 ////////////////////

var str=obj.id;
var s = new Array();
s = str.split("cat");
for ( var j=0; j < s.length; j++ ){
				if ( s[j] != "" ){
					str = document.getElementById("class"+s[j]);
					if ( str != null )
						str.style.fontWeight = "bold";
				}
}
//////////////////// для смены класса у рубрики с #LEVEL# = 0 ////////////////////

					}
				}
				else break;
				obj1=eval("obj.parentElement");
			}
		}
}


function show_div(id_cur){
	var div_set=null;
	div_set=document.getElementById(id_cur);
	if (div_set!=null) {
		if (div_set.style.display=="block") div_set.style.display="none";
		else div_set.style.display="block";
	}
}


function show_div_city(id_cur,indel)
{
	var div_set=null;
	div_set=document.getElementById(id_cur);


	if (div_set.style.display=="none" && indel==5) div_set.style.display="block";
		else div_set.style.display="none";



}










CurCurse='1.0';
CurValuteFlag='0'; // 0 - не установлен, 1-доллары, 2-рубли.

function normalize(price)
{	var pr, str;
	pr=parseInt(Math.round(price*100));
	str = new String(pr);
	pr="";
	if (str.length>2)
		pr += str.substring(0, str.length-2);
	else
		pr += "0";
	pr += ".";
	if (str.length>2)
		pr += str.substring(str.length-2, str.length);
	else
	{	if (str.length==1)
			pr += "0";
		pr += str;
	}
	return pr;
}

function price_alt(in_price,in_usd,in_rur){
	if (in_price=='' || (in_usd=='' && in_rur=='')) return;
	var normal_price=parseFloat("0"+in_price);
	if (in_usd!='') { document.write( normalize(normal_price*CurCurse)+" руб." ); return; } // если цена товара в USD, то пересчитаем в рублях и отобразим альтернативную цену
	if (in_rur!='') { document.write( normalize(normal_price/CurCurse)+" $" ); return; } // если цена товара в USD, то пересчитаем в рублях и отобразим альтернативную цену
}

function price_alt2(in_price){
	var normal_price=parseFloat("0"+in_price);
	if (CurValuteFlag=='1') { document.write( normalize(normal_price*CurCurse)+" руб." ); return; } // если цена товара в USD, то пересчитаем в рублях и отобразим альтернативную цену
	if (CurValuteFlag=='2') { document.write( normalize(normal_price/CurCurse)+" $" ); return; } // если цена товара в USD, то пересчитаем в рублях и отобразим альтернативную цену
}

function ico_img(graph)
{
	if (graph == '/i/null.gif') document.write('<img src="/i/ico_noimg.gif" width=30 height=30 border=0 alt="Изображение отсутствует">');
	else if (graph == '/i/noimage.gif') document.write('<img src="/i/ico_noimg.gif" width=30 height=30 border=0 alt="Изображение отсутствует">');
	else document.write('<img src="/i/ico_img.gif" width=30 height=30 border=0 alt="Описание с изображением">')
}

function CreateCookie(name,value,ex_date){
	var today=new Date();
	today.setDate(today.getDate()+ex_date);
	document.cookie=name+"="+value+"; expires="+today.toGMTString()+"; path=/;";
}

function CreateTmpCookie(name,value){
	document.cookie=name+"="+value+"; path=/;";
}

function RemoveCookie(name){
	CreateCookie(name,"",-1);
}

function ReadCookie(name){
	var tmpName=name+"=";
	if ((startName=atStr(tmpName,document.cookie))!=-1){
		var startVal=startName+tmpName.length;
		if ((endVal=document.cookie.indexOf(";",startVal))==-1) 
			endVal=document.cookie.length;
		return unescape(document.cookie.substring(startVal,endVal));
	}
	return null;
}

function SetCompare(obj,id_goods){
	if (!obj.checked) RemoveCookie("idg"+id_goods); else CreateTmpCookie("idg"+id_goods,id_goods);
}

function SpellCompare(obj){
	var str="";
	var start_IDG=0;
	var end_IDG=-1;
	var break_point=0;
	var flag;
	while (start_IDG!=-1){
		start_IDG=document.cookie.indexOf("idg",start_IDG);
		end_IDG=document.cookie.indexOf("=",start_IDG);
		if (start_IDG==-1 || end_IDG==-1) break;
		idg=unescape(document.cookie.substring(start_IDG+3,end_IDG));
		start_IDG=end_IDG;
		flag=false;
		//alert("idg="+idg+" , obj.idg.length="+obj.idg.length);
		if (isNaN(obj.idg.length)) { 
			if (obj.idg.value==idg) { obj.idg.checked=true; flag=true; }
		}
		else {
			for (i=0;i<obj.idg.length;i++) {
				if (obj.idg[i].value==idg) { obj.idg[i].checked=true; flag=true; break; }
			}
		}
		//alert("flag="+flag);
		if (!flag) { 
			//alert("!!! idg="+idg);
			str+="<input type=hidden name=idg value="+idg+">"
		}
		break_point++; if (break_point>10) break;
	}
	return str;
}

function SearchScroll(FileAction,pgsize,numpage,stype,str_url){
	var s1=new RegExp("pgsize=[^&]*&|pgsize=.*$", "gi");
	var s2=new RegExp("page=[^&]*&|page=.*$", "gi");
	var s3=new RegExp("stype=[^&]*&|stype=.*$", "gi");
	var str_url_r='';
	var ch='';
	var WWWWW=str_url;
	str_url=str_url.replace(s1,''); str_url=str_url.replace(s2,''); str_url=str_url.replace(s3,''); 
	for (i=0;i<str_url.length;i++) {
		ch=str_url.substr(i,1);
		if (ch!='=' && ch!='&' && ch!='%') str_url_r+=escape(ch);	else str_url_r+=ch;
	}
	if ( str_url_r.substr(str_url_r.length-1,1)=='&' ) str_url_r=str_url_r.substr(0,str_url_r.length-1);
	document.location = FileAction + '?c=search&pgsize=' + pgsize + '&page=' + numpage + '&stype=' + stype + '&' + str_url_r
}

function logoff(file_templ,idc,stype,page,idr,idt,idn,idg){
	RemoveCookie("login");
	RemoveCookie("pass");
	RemoveCookie("userid");
	document.location='/'+ file_templ +'?idc='+idc +'&stype='+stype +'&page='+page +'&idr='+idr +'&idt='+idt +'&idn='+idn +'&idg='+idg +'&r='+RND;
	//location.reload();
}

function ReadStatusOrder(val){
	var aStatus=val.split(";");
	for (i=0; i<aStatus.length-1; i++) {
		aVal=aStatus[i].split("=");
		document.write( " <br> " + aVal[0] + " : " + aVal[1]);
	}
}



/////////////////////////////// Поиск: прячем рубрику с кодом S101, общую для рубрик в шапке
function hideRub(step,code){
	if ( code == "S101" && step == "1" )
		document.write("<font style='display: none;'>");
	if ( code == "S101" && step == "2" )
		document.write("</font>");
}
















/////////////////////////// "Защита от дурака"
/*
window.setInterval("dropSelection()",5);
function dropSelection(){
		document.selection.empty();
}
*/








//////////////////////////////////////////////// рубрикатор ////////////////////////////////////////////////////
var rubcount = 0;

var div_arr=new Array(1000);
var div_count=0;
var timeout_show=null,timeout_hide=null;
var div_set="";
function show_div2(id_cur, id_par,obj,level){
	var div_cur="d"+id_cur, div_par="d"+id_par;

	if ( !obj.contains(window.event.toElement) || obj.contains(window.event.fromElement) ) return; // это правильно

	if ( level > 0 )
		obj.style.backgroundImage="url('/i/fonSubMenuLeftOver.jpg')";
	else
		obj.style.background="#E8D2A3";

	if (timeout_hide!=null) {	clearTimeout(timeout_hide); timeout_hide=null; }

	if (div_set!="" && div_set!=div_cur && div_set!=div_par && document.getElementById(div_set)!=null) {
		document.all[div_set].style.visibility="hidden"; // скрыли текущий слой
	}
	div_set=div_cur;


	if (document.getElementById(div_cur)!=null) document.all[div_cur].style.visibility="visible";
	if (document.getElementById(id_par)!=null) s_color(id_par);
}

function r_color(obj) {
	if ( !obj.contains(window.event.toElement) && obj.contains(window.event.fromElement) ) obj.style.background="";

}

function hide_all() {
	div_set="";
	for (var i=0; i<div_count; i++) {
		if (document.getElementById(div_arr[i])!=null) document.all[div_arr[i]].style.visibility="hidden";
	}
}

function s_div(obj) { 
	if ( (obj.contains(window.event.toElement)) ) obj.style.visibility="visible"; 
}

function h_div(obj) {	if ( (obj.contains(window.event.fromElement)) ) obj.style.visibility="hidden"; }



// всплывающее меню слева
var fflag = 0;
var mainRubUrl = "";
var tekRubUrl = "";
function beginPopUp(url,idc){
//	if ( url != "" ){
	if ( url == "L101" ){
//1
//alert("1)"+url+" , "+idc);
		document.write("<div class=st1>");
		document.write("<table border=0 cellpadding=0 cellspacing=0 id=d"+idc+" onMouseOver='if ( \""+url+"\" != \"\" ) s_div(this);' onMouseOut='if ( \""+url+"\" != \"\" ) h_div(this);' class=st2><tr valign=top>");
	}
	else
		document.write('<div class=catchild id="cat'+idc+'" style="margin: 4px 0;">');

	rubcount = 0;
	fflag = 0;
	tekRubUrl = url;
}


function endPopUp(url,n,f,idc){
//	if ( url != "" && n > 0 && f == 1 ){
	if ( url == "L101" && n > 0 && f == 1 ){
//4
//alert("4) закрываем td "+url+", "+n+" , "+f+" , "+idc);
		document.write("</td>");
	}
	if ( url != "" ){
//alert("4) закрываем table "+url+", "+n+" , "+f+" , "+idc);
		document.write("</tr></table></div>");
}
	else
		document.write("</div>");


	div_arr[div_count++] = "d"+idc;
	tekRubUrl = "";
}

function beginPopUpTd(n,url){
//	if ( tekRubUrl != "" ){
	if ( tekRubUrl == "L101" ){
//2
//alert("2)"+n+" , "+url);
				if ( n == 0 )
					document.write("<td class=newSubRub"+url+">");
	}
}

function endPopUpTd(n,idc){
	div_arr[div_count++]="d"+idc;
//	if ( tekRubUrl != "" ){
	if ( tekRubUrl == "L101" ){
					if ( n == 19 ){
//3
//alert("3)"+n+" , "+idc);
						document.write("</td>");
						rubcount = -1;
					}
					rubcount++;
					fflag = 1;
	}
}