var IE=(window.navigator.appName=="Microsoft Internet Explorer");


function parse_url (str, component) 
{
	var o = {
		strictMode: false,
		key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
		q: {
			name:	"queryKey",
			parser: /(?:^|&)([^&=]*)=?([^&]*)/g
		},
		parser: {
			strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
			loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-protocol to catch file:/// (should restrict this)
		}
	};
	var m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
	uri = {},
	i   = 14;
	while (i--) {uri[o.key[i]] = m[i] || "";}

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
	if ($1) uri[o.q.name][$1] = $2;
	});
	return uri;
}

function fixPNG(element)
{
	if(/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
		var src;
		if(element.tagName=='IMG')
		{
			if(/\.png$/.test(element.src))
			{
				src=element.src;
				element.src="/styles/null.gif";
			}
		}
		else
		{
			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')";
	}
}

function MyURLEncode(str)
{
	var ret = '';

	for (i=0; i<str.length; i++)
	{
		var n = str.charCodeAt(i);
		if (n >= 0x410 && n <= 0x44F)
			n -= 0x350;
		else
			if (n == 0x451)
				n = 0xB8;
			else
				if (n == 0x401)
					n = 0xA8;
		if ((n < 65 || n > 90) && (n < 97 || n > 122) && n < 256)
		{
			if (n < 16)
				ret += '%0'+n.toString(16);
			else
				ret += '%'+n.toString(16);
		}
		else
			ret += String.fromCharCode(n);
	}
	return ret;
}

function js_set_cookies(name,value,path,reload)
{
	var str=name+"="+value+"; domain="+document.domain+"; path="+path;
	document.cookie=str;
	if(reload)
		document.location.reload();
}

function js_set_cookies_noreload(name, value,expires)
{
	var today = new Date();
	var expires_date = new Date( today.getTime() + 1000*expires );
	var str=name+"="+value+"; expires=" + expires_date.toGMTString()+"; domain="+document.domain+"; path=/";
	document.cookie=str;
}

function js_get_cookies(name)
{
	if(document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(name + "=")
		if(c_start!=-1)
		{
			c_start=c_start + name.length+1
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return document.cookie.substring(c_start,c_end);
		}
	}
	return "";
}


function fstyle(name,style,value)
{
	if(document.getElementById(name))
		document.getElementById(name).style[style]=value;
}

function trim(str, charlist)
{
    return ltrim(rtrim(str, charlist), charlist);
}

function ltrim(str, charlist)
{
	if(!charlist)
		charlist = "\\s";
	return str.replace(new RegExp("^[" + charlist + "]+", "g"), "");
}

function rtrim(str, chars)
{
	if(!charlist)
		charlist = "\\s";
	return str.replace(new RegExp("[" + charlist + "]+$", "g"), "");
}

function number_format(number, decimals, dec_point, thousands_sep)
{
	var x = Math.round(number*Math.pow(10,decimals));
	var y = (''+Math.abs(x)).split('');
	var z = y.length - decimals;
	if(z<0) z--;

	for(var i=z; i<0; i++)
		y.unshift('0');
	if(!dec_point)
		dec_point='.';
	y.splice(z,0,dec_point);
	if(Math.abs(number)<1 && x)
		y.unshift('0');
	if(!thousands_sep)
		thousands_sep=" ";
	for(var i=z;i>3;i-=3)
		y.splice(i-3,0,thousands_sep);
	return ((x<0)?'-':'')+y.join('');
}

function fadedel(id,pid)
{
	var elem;

	if(!(elem=document.getElementById(id)))
		return;
	if(!elem.opacityfilter)
	{
		elem.style.backgroundColor="#ffa0a0";
		elem.opacityfilter=1;
		elem.opacity=100;
	}
	opacity=elem.opacity
	if(elem.opacity > 0)
	{
		elem.opacity-=5;
		if(elem.filters && elem.filters.alpha)
			elem.filters.alpha.opacity = elem.opacity;
		else
		{
			elem.style.MozOpacity = elem.opacity/100;
			elem.style.opacity = elem.opacity/100;
		}
		setTimeout('fadedel("'+id+'","'+pid+'","+type+")', 50);
	}
	else
	{
		document.getElementById(pid).deleteRow(document.getElementById(id).rowIndex);
	}
}

/**/
function fYes(f,x)
{
	if(typeof(f)=="undefined") return (0);
	if(typeof(f.elements)=="undefined") return (0);
	if(typeof(f.elements[x])=="undefined") return (0); else return (f.elements[x]);
}

function fSet(f,x,y,type,text)
{
	var Work;
	switch((Work=fYes(f,x))?Work.type:0)
	{
	case "text":Work.value=y;break;
	case "select-one":
		if(type==1)
		{
			for(var i=0;i<Work.length;i++)
				if(Work.options[i].value==y) {Work.options[i].selected=1;break;}
			if(i==Work.length)
				Work.options[i]=new Option(text,y);
		}
		else
			Work.options[y].selected=1;
		break;
	}
}

function fGet(f,x,type,y)
{
	var Work;
	switch((Work=fYes(f,x))?Work.type:0)
	{
	case "text":return(Work.value);
	case "select-one":
		switch(type)
		{
		case 0:
			return(Work.selectedIndex);
		case 1:
			return(Work.options[Work.selectedIndex].text);
		case 2:
			return(Work.options[Work.selectedIndex].value);
		case 3:
			for(var i=0;i<Work.length;i++)
				if(Work.options[i].value==y) return i;
			return false;
		}
	}
	return (0);
}

function FGet(f,x,type)
{
	var Work;
	switch((Work=fYes(f,x))?Work.type:0)
	{
	case "text":return(Work.value);
	case "select-one":
		switch(type)
		{
		case 'elem':
			return(Work.options[Work.selectedIndex]);
		case 'value':
			return(Work.options[Work.selectedIndex].value);
		case 'text':
			return(Work.options[Work.selectedIndex].text);
		case 'index':
			return(Work.selectedIndex);
		}
	}
	return (0);
}

function form_clean(f)
{
	var i,j;
	for(i=0;i<f.length;i++)
	{
		e=f.elements[i];
		switch(e.type)
		{
		case "text":
			e.value='';
			break;
		case "select-one":
			if(!e.length)
				break;
			e.options[0].selected=1;
			break;
		}
	}
}

function is_number(sText)
{
	var ValidChars = "0123456789.,";
	var Char;

   	for(var i=0;i<sText.length;i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
			return false;
	}
	return true;
}

function is_dmy(sText)
{
	adate = sText.split('.');
	if(adate.length!=3)
		return false;
	for(i=0;i<3;i++)
	{
		adate[i]=parseInt(adate[i],10);
		if(isNaN(adate[i]) || adate[i]<=0)
			return false;
	}
	odate=new Date(adate[2],adate[1]-1,adate[0]);
	if(odate.getDate()!=adate[0] || odate.getMonth()!=(adate[1]-1) || odate.getFullYear()!=adate[2])
		return false;
	return true;
}

function intformat(n,m)
{
	var s=""+n;
	if(s.length>=m)
		return s;
	//add m-s.length '0'
	var r="";
	for(ii=0;ii<m-s.length;ii++)
		r+="0";
	r+=s;
	return r;
}

var floatpoint='.';

function floatformat(n,m)
{
	res='0.00';
	var s=""+n;
	var idot=s.indexOf(".");
	if(s.length<=0)
		res="0.00";
	else
		if(idot<0)
			res=s+".00";
		else
			if(idot==s.length-1)
				res=s+"00";
			else
				if(idot==s.length-2)
					res=s+"0";
				else
					res=s.substring(0,idot+m+1);
	if(floatpoint!='.')
	{
		nres='';
		for(j=0;j<res.length;j++)
		{
			if(res.charAt(j)=='.'){nres+=',';continue;}
			nres+=res.charAt(j);
		}
		return nres;
	}
	return res;
}

function offsetx(e)
{
	var x = e.offsetLeft;
	for(var i=e.offsetParent; i; i=i.offsetParent) x+=i.offsetLeft;
	return x;
}

function offsety(e)
{
	var y = e.offsetTop;
	for(var i=e.offsetParent; i; i=i.offsetParent)y+=i.offsetTop;
	return y;
}

function date_parse(sdate)
{
	var adate = sdate.split('.');
	if(adate.length!=3)
		return 1;
	for(var i=0;i<3;i++)
	{
		adate[i]=parseInt(adate[i],10);
		if(isNaN(adate[i]) || adate[i]<=0)
			return 2;
	}
	var odate=new Date(adate[2],adate[1]-1,adate[0]);
	if(odate.getDate()!=adate[0] || odate.getMonth()!=(adate[1]-1) || odate.getFullYear()!=adate[2])
		return 3;
	return odate;
}

var stormDialog=null;
var stormDialogForm=null;
var stormDialogField=null;
var stormDialogElem=null;

function storm_showModalDialog(url,param)
{
	if(stormDialog==null || stormDialog.closed)
	{
		stormDialog=window.open (url,'stormdialog',param);
		if(stormDialog==null)
		{
			alert('Cannot open dialog window');
			return;
		}
	}
	stormDialog.window.focus();
}
function dialog_param(parentelem,w,h)
{
	return "left="+(screen.availWidth-w)/2+",top="+(screen.availHeight-h)/2+",width="+w+", height="+h+", center=yes, help=no, resizable=yes, status=no";
}

function dialog_open(parentelem,ename,fname,href,w,h)
{
	stormDialogForm=fname;
	stormDialogField=ename;
	stormDialogElem=document.forms[fname].elements[ename];
	param=dialog_param(parentelem,w,h);
	storm_showModalDialog(href,param);
}


jQuery.cookie = function(name, value, options) 
{
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		// CAUTION: Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		//options.path='/';
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};


function js_set_event(element, eventName, handler)
{
	  if(element.addEventListener)
			element.addEventListener(eventName, handler, false);
	  else
		if(element.attachEvent)
			element.attachEvent('on' + eventName, handler);
}

function onload_menuhover()
{
	if(!document.getElementById("hmenu"))
		return;
	var lis = document.getElementById("hmenu").getElementsByTagName("LI");
	for (var i=0;i<lis.length;i++)
	{
		lis[i].onmouseover=function(){this.className+=" iehover";}
		lis[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" iehover\\b"), "");}
	}
	var lis = document.getElementById("vmenu").getElementsByTagName("LI");
	for (var i=0;i<lis.length;i++)
	{
		lis[i].onmouseover=function(){this.className+=" iehover";}
		lis[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" iehover\\b"), "");}
	}
}

var parent_href="";
function onload_linkthis()
{

	var href=document.location.href;

	for(var i=0;i<document.links.length;i++)
	{
		if(document.links[i].className == "link" && href == document.links[i].href)
		{
			document.links[i].className="this";
		}
		if(parent_href && document.links[i].className == "link" && parent_href == document.links[i].href)
		{
			document.links[i].className="this";
		}
	}
}

function c_backet()
{
	this.cookie='NULL';
	this.order_sum=0;
	this.field=0;
	this.cost=new Array();
	this.count=new Array();
	this.prod=new Array();
	this.downcode=0;
	this.downnumb=0;
}

var backet=new c_backet();

c_backet.prototype.getfield = function()
{
	this.field++;
	return this.field;
}

c_backet.prototype.parse = function()
{
	var read=unescape(js_get_cookies('BACKET')),prod=read.split("|");
	for(var i=0;i<prod.length;i++)
	{
		var elem=prod[i].split("#");
		if(elem.length!=3)
			continue;
		if(elem[1]*1<=0)
			elem[1]=0;
		this.cost[elem[0]]=elem[2]*1;
		this.count[elem[0]]=elem[1]*1;
	}
}

c_backet.prototype.sum = function()
{
	var data=0;

	for(prod in this.count)
		data+=this.count[prod]*this.cost[prod];
	this.order_sum=number_format(Math.round(100*data)/100.0, 2, '.',' ');
	return this.order_sum;
}

c_backet.prototype.elem = function(elem,code,cost)
{
	if(elem && elem.value)
		this.count[code]=elem.value*1;
	else
		this.count[code]=0;
	this.set(code,cost,this.count[code]);
	if(elem && elem.type && elem.type=="text")
		elem.value=this.count[code];
}

c_backet.prototype.del = function(code)
{
	this.reload=1;
	delete this.count[code];
	delete this.cost[code];
	var data="BACKET=";
	for(var prod in this.count)
		if(prod!=code)
			data+=prod+'#'+this.count[prod]+'#'+this.cost[prod]+'|';
	document.cookie=data+"; path=/";
}

c_backet.prototype.set = function(code,cost,numb)
{
	this.reload=0;
	if(!this.cost[code])
		this.reload=1;
	this.count[code]=numb;
	this.cost[code]=cost;
	if(this.count[code]<0 || isNaN(this.count[code]))
		this.count[code]=0;
	var data="BACKET=";
	for(var prod in this.count)
//		if(this.count[prod]>0)
			data+=prod+'#'+this.count[prod]+'#'+this.cost[prod]+'|';
	document.cookie=data+"; path=/";
}

c_backet.prototype.tabload = function()
{
	$('#TAB_9').html("<div class='ajaxload'><img vspace=60 src='/styles/20100125/load.gif'></div>");
	settab(9,0);
	$.post("/products/", {COMM:"LOAD_BACKET"},
		function(data){
			$('#TAB_9').html(data);
			onload_products();
	});
}

c_backet.prototype.add = function(code,cost,numb,fnumb)
{
	if(!this.count[code])
		this.count[code]=0;
	this.count[code]=this.count[code]*1+numb*1;
	this.set(code,cost,this.count[code]);
	if(document.forms['PRODUCT'].elements['P_'+fnumb])
		document.forms['PRODUCT'].elements['P_'+fnumb].value=this.count[code];
}

c_backet.prototype.get = function(code)
{
	return (this.count[code]?this.count[code]*1:0);
}

c_backet.prototype.update = function()
{
	if(this.cookie==document.cookie)
	{
		setTimeout("backet.update()",200);
		return;
	}
	this.parse();
	this.cookie=document.cookie;
	$('#BACKET_SUMM').html(backet.sum()+' грн');
	this.fill();
	setTimeout("backet.update()",200);
}

c_backet.prototype.fill = function()
{
	var sum=backet.sum();

	$('#TAB_9_SUMM').html('Корзина:&nbsp; ' + sum + ' грн');
	$('#BTAB_9_SUMM').html('Корзина:&nbsp; ' + sum + ' грн');
	$('#SUM1').html(sum+' грн');
	$('#SUM2').html(sum+' грн');
	$('#BACKET_SUMM').html(sum+' грн');

	for(var code in this.count)
	{
		if(this.count[code])
		{
			var numb=number_format(Math.round(100*this.count[code]*this.cost[code])/100.0, 2, '.',' ')+' грн';
			$("#S_"+code).html(numb);
			$("#BS_"+code).html(numb);
			$('#C_'+code+' img').css('display','block');
			
			$('#C_'+code).html("<img alt='Очистить' title='Очистить' src='/styles/20100125/del.gif'>");
			$('#C_'+code+" img").data('code',code).click(function () {backet.set($(this).data('code'),0,0);});
			
			$('#BC_'+code).html("<img alt='Очистить' title='Очистить' src='/styles/20100125/del.gif'>");
			$('#BC_'+code+" img").data('code',code).click(function () {backet.set($(this).data('code'),0,0);});
			
			$("input[prod="+code+"]").val(this.count[code]).attr("class","orderactv");
		}
		else
		{
			$("#S_"+code).html('');
			$("#BS_"+code).html('');
			$('#C_'+code).html('');
			$('#BC_'+code).html('');
			$('#BC_'+code).html("<img alt='Удалить' title='Удалить' src='/styles/20100125/delred.gif'>");
			$('#BC_'+code+" img").data('code',code).click(function () { 
				backet.del($(this).data('code'));
				$(this).parent().parent().parent().remove();
				});
			$("input[prod="+code+"]").val(0).attr("class","order");
		}
	}
}

c_backet.prototype.onmousedown = function(code,cost,numb,fnumb)
{
	this.add(code,cost,numb,fnumb);
	this.downcode=code;
	this.downnumb=numb;
}
c_backet.prototype.onmouseup = function()
{
	this.downcode=0;
	this.downnumb=0;
}
c_backet.prototype.down = function(fnumb)
{
	if(this.downcode && this.downnumb)
	{
		this.add(this.downcode,this.cost[this.downcode],this.downnumb,fnumb);
		setTimeout("backet.down("+fnumb+")",200);
	}
}

function js_add_id(name,code,cost)
{
	var numb=backet.getfield();
	$("#"+name+"_"+code).html('<input type="hidden" name="H_'+numb+'" value="'+code+'"><input type="text" maxlength="8" class="order" autocomplete="off" prod="'+code+'" name="P_'+numb+'" onchange="backet.elem(this,\''+code+'\','+cost+');" value="0" onkeydown="return add_onkey(event,this,\''+code+'\','+cost+');"  onfocus="add_onfocus(this,\''+code+'\','+cost+');" onblur="add_onblur(this,\''+code+'\','+cost+');" ondblclick="add_ondblclick(this,\''+code+'\','+cost+');">');
}

function js_add(code,cost,type)
{
	var data='',numb=backet.getfield();

	data+='<table class=null border=0 cellpadding=0 cellspacing=0 width="100%">';
	data+='<tr>';
	data+='<td><input type="hidden" name="H_'+numb+'" value="'+code+'"><input type="text" maxlength="8" class="order" prod="'+code+'" name="P_'+numb+'" onchange="backet.elem(this,\''+code+'\','+cost+');" value="0" onkeydown="add_onkey(event,this,\''+code+'\','+cost+');" onfocus="add_onfocus(this,\''+code+'\','+cost+');" onblur="add_onblur(this,\''+code+'\','+cost+');" ondblclick="add_ondblclick(this,\''+code+'\','+cost+');" ></td>';
	if(type==1)
		data+='<td><img style="padding-left:10px;cursor:pointer" src="/styles/20100125/del.gif" title="Удалить товар из корзины" onclick="backet.set(\''+code+'\','+cost+',0); rowdel(\'ROW_'+code+'\',\'PRODTABLE\');"></td>';
	data+='<td><input type="text" maxlength="8" class="ordersumm" readonly prodsumm="'+code+'" name="S_'+numb+'"></td>';
	data+='</tr>';
	data+='</table>';
	return data;
}

function add_onfocus(e,code,cost)
{
	if(e.value==0) e.value='';
}

function add_onblur(e,code,cost)
{
	backet.fill();
}

function add_ondblclick(e,code,cost)
{
	e.value=e.value*1+1;
	backet.set(code,cost,e.value);
}

function add_onkey(e,elem,code,cost)
{
	var unicode=e.charCode? e.charCode : e.keyCode;
//$("#AAA").html("CODE="+$.charcode(e)+" VALUE="+elem.value);
	switch($.charcode(e))
	{
	default:
		return false;
	case 'backspace':
	case 'end':
	case 'home':
	case 'left arrow':
	case 'right arrow':
	case 'delete':
		return true;
/*
	case 'page up':
	case 'up arrow':
		if(elem.value*1<=0) elem.value=1; else elem.value=elem.value*1+($.charcode(e)=='page up'?10:1);
		backet.set(code,cost,elem.value*1);
		backet.fill();
		if(elem.value*1<=0) elem.value='';
		return true;
	case 'page down':
	case 'down arrow':
		if(elem.value*1<=0) elem.value=0; else elem.value=elem.value*1-($.charcode(e)=='page down'?10:1);
		backet.set(code,cost,elem.value*1);
		backet.fill();
		if(elem.value*1<=0) elem.value='';
		return true;
*/
	case '0': case '1': case '2': case '3': case '4': 
	case '5': case '6': case '7': case '8': case '9': 
		if(elem.value.indexOf(".")!=-1 && elem.value.length-elem.value.indexOf(".")-1)
			return false;
		elem.value=(''+elem.value+$.charcode(e))*1;
		if(elem.value*1<=0) elem.value='';
		backet.set(code,cost,elem.value*1);
		backet.fill();
		if(elem.value*1<=0) elem.value='';
		return false;
	case 'numpad 0': case 'numpad 1': case 'numpad 2': case 'numpad 3': case 'numpad 4': 
	case 'numpad 5': case 'numpad 6': case 'numpad 7': case 'numpad 8': case 'numpad 9': 
		if(elem.value.indexOf(".")!=-1 && elem.value.length-elem.value.indexOf(".")-1)
			return false;
		var read=substr($.charcode(e),-1)*1;
		elem.value=(''+elem.value+read)*1;
		if(elem.value*1<=0) elem.value='';
		backet.set(code,cost,elem.value*1);
		backet.fill();
		if(elem.value*1<=0) elem.value='';
		return false;
	case 'period':
	case 'decimal point':
		if(elem.value.indexOf(".")!=-1)
			return false;
		elem.value=(''+elem.value+'.');
		return false;
	}
}

function rowdel(id,pid)
{
	var elem;

	if(!(elem=document.getElementById(id)))
		return;
	document.getElementById(pid).deleteRow(document.getElementById(id).rowIndex);
}

var a_tree=new Array();

function getid(id)
{
	if(document.getElementById(id))
		return document.getElementById(id);
	return '';
}

function onload_tree()
{
	var TREE=js_get_cookies('TREE'),id=TREE.split("#");
	for(var i=0;i<id.length;i++)
	{
		if(!id[i])
			continue;
		a_tree[id[i]]=1;
		if(getid("IMG"+id[i]))
			getid("IMG"+id[i]).src="/styles/20100125/tminus.gif";
		if(getid("TR"+id[i]))
			getid("TR"+id[i]).style.display="block";
	}
}

function tree(id)
{
	if(!getid("IMG"+id))
		return;
	if(!getid("TR"+id))
		return;

	if(getid("TR"+id).style.display=="block")
	{
		a_tree[id]=0;
		getid("IMG"+id).src="/styles/20100125/tplus.gif";
		getid("TR"+id).style.display="none";
	}
	else
	{
		a_tree[id]=1;
		getid("IMG"+id).src="/styles/20100125/tminus.gif";
		getid("TR"+id).style.display="block";
	}
	var TREE='';
	for(var i in a_tree)
		if(a_tree[i])
			TREE+=i+'#';
	js_set_cookies('TREE',TREE,'/',0);
}

function js_user_order(elem)
{
	if(!document.getElementById('userorg'))
		return;
	if(elem.value==1)
		document.getElementById('userorg').style.display="block";
	else
		document.getElementById('userorg').style.display="none";
}

function settab(active,count)
{
	if(!count)count=10;
	for(var i=1;i<=count;i++)
	{
		$("#TAB_"+i).css('display','none');
		$("#TAB_LI_"+i).attr("class","passive");
		$("#BTAB_LI_"+i).attr("class","passive");
	}
	$("#TAB_"+active).css('display','block');
	$("#TAB_LI_"+active).attr("class","active");
	$("#BTAB_LI_"+active).attr("class","active");
}

//?? 
function go_backet()
{
	var href=js_get_cookies("GO_BACK");
	if(!href)
		document.location.href="/";
	else
		document.location.href=href;
}

function onload_go_backet()
{
	var href=document.location.pathname;
	var start=document.location.hostname.length+7;

	if(document.location.href.substring(start,start+10)=='/products/')
		js_set_cookies("GO_BACK",document.location.href,"/",0);
}

/**/
var property_path='';
var compare_path='';
function clean_filter()
{
	js_set_cookies('FILTER','',property_path,0);
	document.location.reload();
}

function add_filter(group,data,type)
{
	data=MyURLEncode(data);

	if(type==2)
	{
		js_set_cookies('FILTER',"",property_path,0);
		document.location.reload();
	}
	var text='',read=js_get_cookies('FILTER');
	read='';
	var prop=read.split("|"),save=new Array();
	for(var i=0;i<prop.length;i++)
	{
		var elem=prop[i].split("#");
		if(elem.length!=2)
			continue;
		if(elem[0]==group && elem[1]==data)
			continue;
		if(save[elem[0]+'#'+elem[1]])
			continue;
		save[elem[0]+'#'+elem[1]]=1;
		text+=elem[0]+'#'+elem[1]+'|';
	}
	if(type==1)
		text+=group+'#'+data+'|';
	js_set_cookies('FILTER',text,property_path,0);
	document.location.reload();
}

/*
function c_compare(code,path,title)
{
	this.code=code;
	this.path=path;
	this.title=title;
}

var a_compare=new Array();

function js_compare_click(code,path,title)
{
	js_compare(0,0,code,path,title);
}

function js_compare(block,elem,code,path,title)
{
	var j=0,type=elem?elem.checked:1;

	for(var i in a_compare)
		j++;
	if(j>=10)
	{
		alert('Максимальное количество товаров для сравнения 10');
		if(elem)
			elem.checked=0;
		return;
	}

	if(type)
		a_compare[code]=new c_compare(code,path,title);
	else
		if(a_compare[code])
			delete a_compare[code];

	var text='';
	for(var i in a_compare)
		text+=MyURLEncode(a_compare[i].code)+'||';
	js_set_cookies('COMPARE',text,compare_path,0);
	js_compare_draw();
}

function js_compare_clean()
{
	a_compare=new Array();
	js_set_cookies('COMPARE','',compare_path,0);
	js_compare_draw();

	for(var i=0;i<document.forms['PRODUCT'].length;i++)
		if(document.forms['PRODUCT'].elements[i].checked)
			document.forms['PRODUCT'].elements[i].checked=0;
}

function js_compare_del(code,type)
{
	if(a_compare[code])
		delete a_compare[code];
	var text='';
	for(var i in a_compare)
		text+=MyURLEncode(a_compare[i].code)+'||';
	js_set_cookies('COMPARE',text,compare_path,type);
	if(type==0)
		js_compare_draw();
	if(type==0)
	{
		for(var i=0;i<document.forms['PRODUCT'].length;i++)
			if(document.forms['PRODUCT'].elements[i].value==code)
				document.forms['PRODUCT'].elements[i].checked=0;
	}
}

function js_compare_draw()
{
	var i,j=0,data='';

	for(var i in a_compare)
		j++;
	if(!j)
	{
		document.getElementById('divcompare').style.display='none';
		document.getElementById('divcompare_data').innerHTML='';
		return;
	}

	document.getElementById('divcompare').style.display='block';
	data+="<form name='COMPARE' action='' method='GET'>";
	data+='<table border=0 width="100%" cellspacing="0" cellpadding="1" style="padding-top:6px;padding-bottom:6px;">';
	for(i in a_compare)
	{
		data+='<tr>';
		data+="<td style='padding-left:4px;padding-right:4px;'><input name=FF"+j+" type=checkbox checked value='ON' onclick='js_compare_del(\""+a_compare[i].code+"\",0);'></td>";
		data+="<td width='100%'><a class=compare href='"+a_compare[i].path+"'>"+a_compare[i].title+"</a></td>";
		data+='</tr>';
		j++;
	}
	data+='</table>';
	data+='</form>';
	document.getElementById('divcompare_data').innerHTML=data;
}

function js_compare_submit()
{
	document.location.href=compare_path+'compare.html';
}
*/

/*PRODUCTS*/
function c_products()
{
	this.loadhtml="<div class='ajaxload'><img vspace=60 src='/styles/20100125/load.gif'></div>";
	this.page=0;
	this.path=document.location.pathname;
	this.sortname={
		rank:			'по умолчанию',
		name:			'по наименованию [а-я]',
		namedesc:		'по наименованию [я-а]',
		costdesc:		'по убыванию цены',
		cost:			'по возрастанию цены'
		};
	this.sizename={
		'10':		'10',
		'20':		'20',
		'40':		'40',
		'60':		'60',
		'100':		'100'
		};
	this.filter_group_a=new Array();
	this.filter_group=0;
	this.filter_prop=0;
	this.search_text='';
	this.action=0;
	this.sale=0;
	this.actv='';
	this.actvdraw_path='';
	this.actvdraw_id='';
	this.actvdraw_code='';
	
	this.actvsave=new Array();
	this.descsave=new Array();
}

var products=new c_products();

c_products.prototype.init = function()
{
	this.view=$.cookie('view');
	if(!this.view) this.view='gallery';
	this.sort=$.cookie('sort');
	if(!this.sort) this.sort='name';
	this.size=$.cookie('size');
	if(!this.size) this.size='20';
	this.actv=$.cookie('actv');
}

c_products.prototype.setsearch = function(text)
{
	this.search_text=text;
}

c_products.prototype.menu = function(elem,name,type)
{
	var pos=$(elem).offset();
	
	if(!$('#menudraw_mask').length)
		$('<div/>').attr('id','menudraw_mask').hide().appendTo('body');
	$('#menudraw_mask').css({'width':$(window).width(),'height':$(document).height(),'left':0,'top':0,'position':'absolute','z-index':900,'background-color':'#000','display':'none','opacity':0});
	$('#menudraw_mask').show(1);
	$('#menudraw_mask').click(function () {$(this).hide();$('#menudraw').hide();});
	
	if(!$('#menudraw').length)
		$('<div/>').attr('id','menudraw').hide().appendTo('body');
	else
	if($('#menudraw').is(':visible'))
	{
		//$('#menudraw').hide('slide', { direction: "up" }, 500);
		$('#menudraw').hide();
		return;
	}
	var data="<table border=1 class=sort width='100%' cellpadding=0 cellspacing=0>";
	for(var i in name)
		data+="<tr><td nowrap onmouseover=\"this.className='hover';\" onmouseout=\"this.className='';\"  onclick='products.menuclick(this,\""+i+"\",\""+type+"\");'>"+name[i]+"</td></tr>";
	data+="</table>";
	$('#menudraw').html(data);
	//$('#menudraw').mouseout(function(){$(this).hide();});
	$('#menudraw').css({'z-index':1000,'position':'absolute','width':($(elem).width()+13)+'px','left':(pos.left+1)+'px','top':(pos.top+20)+'px','display':'block'});
	//$('#menudraw').show('slide', { direction: "up" }, 500);
	$('#menudraw').show();
}

c_products.prototype.menuclick = function(elem,name,type)
{
	$.cookie(type,name,{expires:99,path:'/'});
	$('#menudraw').hide();
	this.loadtab('');
}

c_products.prototype.loadview = function(name)
{
	$.cookie('view',name,{expires:99,path:'/'});
	this.loadtab('');
}
c_products.prototype.loadtab = function(href)
{
	this.init();
	var tabid=(this.view=='index')?1:2,
		url=parse_url(href==''?document.location.pathname:href);
	$('#TAB_1').html(this.loadhtml);
	$('#TAB_2').html(this.loadhtml);
	settab(tabid,0);
	this.comm="LOAD_PRODUCTS";
	if(this.search_text)
		this.comm="SEARCH_PRODUCTS";
	if(this.action)
		this.comm="LOAD_ACTION";
	if(this.sale)
		this.comm="LOAD_SALE";
	this.query="";
	if(url.query)
		this.query=url.query;
	var filter='';
	for(var prop in this.filter_group_a)
		filter+=prop+"#"+this.filter_group_a[prop]+"|";
//	$.post("/products/", {COMM:this.comm,PATH:url.path,QUERY:this.query,TEXT:this.search_text,FILTER_GROUP:this.filter_group,FILTER_PROP:this.filter_prop},
	$.post("/products/", {COMM:this.comm,PATH:url.path,QUERY:this.query,TEXT:this.search_text,FILTER:filter},
		function(data){
			$('#TAB_'+tabid).html(data);
			onload_products();
	});
}
c_products.prototype.actvdraw = function(code,type)
{
	if(type)
		$('#OR_'+code).addClass('actvtr');
	else
		$('#OR_'+code).removeClass('actvtr');
}

c_products.prototype.descclose = function(elem,code)
{
	$(elem).parent().css('display','none');
	this.actvsave[code]=0;
	this.actvdraw(code,0);
}

c_products.prototype.desc = function(path,type,code)
{
	id=type+"_"+code;
	if($('#'+id).css('display')=="block")
	{
		$('#'+id).css('display','none');
		this.actvsave[code]=0;
		this.actvdraw(code,0);
		return false;
	}
	this.actvsave[code]=id;
	this.actvdraw(code,1);

	if(products.descsave[code] && products.descsave[code].length)
	{
		$('#'+id).html(products.descsave[code]);
		$('#'+id).css('display','block');
		return false;
	}
	$.post("/products/", {COMM:"LOAD_DESC",PATH:path},
		function(data){
			products.descsave[code]=data;
			$('#'+id).html(data);
			$('#'+id).css('display','block');
	});
	return false;
}
c_products.prototype.filter = function(groupid,propid,type)
{
	if(type==2)
	{
		this.filter_group_a[groupid]='';
		this.filter_group=this.filter_prop=0;
	}
	else
	{
		this.filter_group_a[groupid]=propid;
		this.filter_group=groupid;
		this.filter_prop=propid;
	}
	this.loadtab('');
}

function onload_products()
{
	products.init();
	
	$("div.page a").bind("click",function(){products.loadtab(this.href);return false;});
	
	$("div.page div.sort").html(products.sortname[products.sort]);
	$("div.page div.sort").bind("click",function(){products.menu(this,products.sortname,'sort');return false;});
	$("div.page div.size").html(products.sizename[products.size]);
	$("div.page div.size").bind("click",function(){products.menu(this,products.sizename,'size');return false;});

	for(var code in products.actvsave)
	{
		if(products.actvsave[code] && products.descsave[code] && products.descsave[code].length)
		{
			$('#'+products.actvsave[code]).html(products.descsave[code]);
			$('#'+products.actvsave[code]).css('display','block');
			products.actvdraw(code,1);
		}
	}

	backet.update();
}

js_set_event(window, 'load', onload_products);
js_set_event(window, 'load', onload_linkthis);

/*charcode*/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
$.charcode = function(input, returnInt) {
	if(typeof(returnInt) == 'undefined')
		returnInt = false;
	if(typeof(input) == 'object')
		input = (input.keyCode ? input.keyCode : input.which);
	if(typeof(input) == 'string' || typeof(input) == 'number')
		input = input;
	var charcodes = {
		'backspace':'8','tab':'9','enter':'13','shift':'16','ctrl':'17','alt':'18','space':'32',
		'pause/break':'19','pause':'19','break':'19','caps lock':'20','escape':'27','page up':'33','page down':'34','end':'35',
		'home':'36','left arrow':'37','up arrow':'38','right arrow':'39','down arrow':'40','insert':'45',
		'delete':'46','0':'48','1':'49','2':'50','3':'51','4':'52',
		'5':'53','6':'54','7':'55','8':'56','9':'57','a':'65',
		'b':'66','c':'67','d':'68','e':'69','f':'70','g':'71',
		'h':'72','i':'73','j':'74','k':'75','l':'76','m':'77',
		'n':'78','o':'79','p':'80','q':'81','r':'82','s':'83',
		't':'84','u':'85','v':'86','w':'87','x':'88','y':'89',
		'z':'90','left window key':'91','right window key':'92','select key':'93','numpad 0':'96','numpad 1':'97',
		'numpad 2':'98','numpad 3':'99','numpad 4':'100','numpad 5':'101','numpad 6':'102','numpad 7':'103',
		'numpad 8':'104','numpad 9':'105','multiply':'106','add':'107','subtract':'109','decimal point':'110',
		'divide':'111','f1':'112','f2':'113','f3':'114','f4':'115','f5':'116',
		'f6':'117','f7':'118','f8':'119','f9':'120','f10':'121','f11':'122',
		'f12':'123','num lock':'144','scroll lock':'145','semi-colon':'186','equal sign':'187','equals sign':'187',
		'comma':'188','dash':'189','period':'190','forward slash':'191','grave accent':'192','open bracket':'219',
		'back slash':'220','close braket':'221','single quote':'222'
	};
	var ReverseCharcodes = {
		'8':'backspace','9':'tab','enter':'13','16':'shift','17':'ctrl','18':'alt','32':'space',
		'19':'pause/break','19':'pause','19':'break','20':'caps lock','27':'escape','33':'page up',
		'34':'page down','35':'end','36':'home','37':'left arrow','38':'up arrow','39':'right arrow',
		'40':'down arrow','45':'insert','46':'delete','48':'0','49':'1','50':'2',
		'51':'3','52':'4','53':'5','54':'6','55':'7','56':'8',
		'57':'9','65':'a','66':'b','67':'c','68':'d','69':'e',
		'70':'f','71':'g','72':'h','73':'i','74':'j','75':'k',
		'76':'l','77':'m','78':'n','79':'o','80':'p','81':'q',
		'82':'r','83':'s','84':'t','85':'u','86':'v','87':'w',
		'88':'x','89':'y','90':'z','91':'left window key','92':'right window key','93':'select key',
		'96':'numpad 0','97':'numpad 1','98':'numpad 2','99':'numpad 3','100':'numpad 4','101':'numpad 5',
		'102':'numpad 6','103':'numpad 7','104':'numpad 8','105':'numpad 9','106':'multiply','107':'add',
		'109':'subtract','110':'decimal point','111':'divide','112':'f1','113':'f2','114':'f3',
		'115':'f4','116':'f5','117':'f6','118':'f7','119':'f8','120':'f9',
		'121':'f10','122':'f11','123':'f12','144':'num lock','145':'scroll lock','186':'semi-colon',
		'187':'equal sign','187':'equals sign','188':'comma','189':'dash','190':'period','191':'forward slash',
		'192':'grave accent','219':'open bracket','220':'back slash','221':'close braket','222':'single quote'
	};
	if(returnInt === false && (typeof(input) == 'string'))
		return charcodes[input.toLowerCase()];
	if(returnInt === true && (typeof(input) == 'string'))
		return parseInt(charcodes[input.toLowerCase()]);
	if(typeof(input) == 'number') {
		return ReverseCharcodes[input];
	}
};
$.isKey = function(e,input) {
	var key = $.charcode(e,true);
	if(typeof(input) == 'string')
		input = input.split(',');
	if(typeof(input) == 'object') {
		var r = false;
		for(i in input) {
			if(input[i].trim() == key)
				r = true;
		}
		return r;
	}
	else {
		input = input.trim();
		return (input == key ? true : false);
	}
};

function substr (str, start, len) {
	str += '';
	var end = str.length;
	if (start < 0) 
		{start += end;}
	end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
	return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
}