/* DIV menu. By Xpoft. V1.0 */


function setProp(attrName, value)
{
    this[attrName] = value;
}

function removeProp(attrName)
{
    delete this[attrName];
}

function saveToString()
{
    ret = "";
    for (scan in client_state)
    {
        if (typeof(this[scan]) == "number" || typeof(this[scan]) == "string")
            ret += scan.toString() + "=" + this[scan].toString() + "&";
    }
    ret = ret.substr(0, ret.length - 1);
    return ret;
}

function loadFromString(str)
{
    if (str == "")
        return;
    attrs = str.split("&");
    for (i = 0; i != attrs.length; i++)
    {
        ind = attrs[i].indexOf("=");
        if (ind != -1)
            this[attrs[i].substr(0, ind)] = attrs[i].substr(ind+1);
        else
            this[attrs[i]] = "";
    }
}


function ClientState()
{
    this.setProp = setProp;
    this.removeProp = removeProp;
    this.saveToString = saveToString;
    this.loadFromString = loadFromString;
}

var client_state = new ClientState();
client_state.loadFromString(document.location.search.substr(1));


function xpTreeExpand(id)
{
	var status = document.getElementById('xp_menu_nav_' + id).className;
	if (status == "xpNavFolderClosed")
	{
		document.getElementById('xp_menu_nav_' + id).className="xpNavFolderOpened";
		document.getElementById('xp_menu_tree_' + id).style.display="block";
	}
	else if (status == "xpNavFolderClosedLast")
	{
		document.getElementById('xp_menu_nav_' + id).className="xpNavFolderOpenedLast";
		document.getElementById('xp_menu_tree_' + id).style.display="block";
	}
	else if (status == "xpNavFolderOpened")
	{
		document.getElementById('xp_menu_nav_' + id).className="xpNavFolderClosed";
		document.getElementById('xp_menu_tree_' + id).style.display="none";
	}
	else if (status == "xpNavFolderOpenedLast")
	{
		document.getElementById('xp_menu_nav_' + id).className="xpNavFolderClosedLast";
		document.getElementById('xp_menu_tree_' + id).style.display="none";
	}
}

function exTreeSearch(id, tree_id)
{
	var idPref = "xp_menu_nav_";

	var elem = document.getElementById(idPref + id);
	if (elem != null)
	{
		// Mark text
		var href = elem.getElementsByTagName("a");
		for (i = 0; i < href.length; i++)
		{
			href[i].className = "xpTreeSelected";
		}
		exTreeSearchParent(elem, tree_id);
	}
}

function exTreeSearchParent(node, tree_id)
{
	var idPref = "xp_menu_nav_";
	var idPref2 = "xp_menu_tree_";
	
	// GetID
	if (node.id != null)
	{
		var id = 0;
		if (node.id.indexOf(idPref) == 0)
		{
			id = parseInt(node.id.substr(idPref.length));
		}
		else if (node.id.indexOf(idPref2) == 0)
		{
			id = parseInt(node.id.substr(idPref2.length));
		}
		
		if (id > 0)
		{
			xpTreeExpand(id);
		}
	}
	
	var parentNode = node.parentNode;
	if (parentNode != null && parentNode.id != tree_id)
	{
		exTreeSearchParent(parentNode, tree_id);
	}
}
function GetCookieVal(n) {
	var endstr=document.cookie.indexOf(";",n);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(n,endstr));
}

function ReadCookie(name) {
 	var xname=name+"=";
	var xlen=xname.length;
	var clen=document.cookie.length;
	var i=0;
	
	while (i<clen) {
		var j=i+xlen;
		if (document.cookie.substring(i,j)==xname) return GetCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}
function WriteCookie(name,value,expires,path,domain,secure) {
	document.cookie=name+"="+escape(value)+
	((expires) ? "; expires="+expires.toGMTString() : "") +
	((path) ? "; path="+path : "") +
	((domain) ? "; domain="+domain : "") +
	((secure) ? "; secure="+secure : "");
	
}

function Basket(tovar)
{	

    nameimg="pic"+tovar;  
    tekimg=document.images[nameimg].src.substring(document.images[nameimg].src.length-8,document.images[nameimg].src.length-5);	
 	tk=document.all.korzina.innerHTML;

   	


	home_tk=tk.substr(0,tk.indexOf("(")+1);
	end_tk=tk.substr(tk.indexOf(")"));
	tk=tk.substr(tk.indexOf("(")+1);
	newlenght=tk.indexOf(")");
	tk=tk.substr(0,newlenght);
	el = document.getElementById('amount');
	if (el) client_state.kol = document.all.amount.value;	
	else client_state.kol = 1;
	
   
	if (tekimg == "add")	
	{
	 tk = tk-1+2;
  	 document.images[nameimg].src="images/del1.gif";
  	 document.images[nameimg].alt="Удалить из корзины";
 	 document.images[nameimg].title="Удалить из корзины";
    	client_state.action = "add"; 
    	CookTovar=ReadCookie("tovar");

		if (CookTovar==null) {
			WriteCookie("tovar",tovar);
			WriteCookie("kolstr",client_state.kol);
			WriteCookie("kol",1);
			WriteCookie("kol",1,"","/");
		}
		else {
			CookKol=ReadCookie("kolstr");	
			WriteCookie("tovar",CookTovar+"|"+tovar);
			WriteCookie("kolstr",CookKol+"|"+client_state.kol);
			CookRealKol=ReadCookie("kol");
			num_kol=Number(CookRealKol);
			num_kol++;
			WriteCookie("kol",num_kol);
			WriteCookie("kol",num_kol,"","/");
		}
 	}
    else
	{
		tk = tk-1;
		if (tk<0) tk=0;
  		document.images[nameimg].src="images/add1.gif";
  		document.images[nameimg].alt="Добавить в корзину";
 		document.images[nameimg].title="Добавить в корзину";
    	client_state.action = "del"; 

		CookTovar=ReadCookie("tovar");
		arr=CookTovar.split("|");
		new_arr= new Array();
		j=0;
		for (i=0;i<=arr.length;i++) {
	 		if (!arr[i]) continue;
			if (arr[i]==tovar) nom_udal=i;
			else {
				new_arr[j]=arr[i];
				j++; 
			}
		}
  		newCookTovar=new_arr.join("|");
		WriteCookie("tovar",newCookTovar);
 	
		CookKol=ReadCookie("kolstr");	
 	
		arr=CookKol.split("|");
		new_arr= new Array();
		j=0;
		for (i=0;i<=arr.length;i++) {
	 		if (!arr[i]) continue;
			if (i!=nom_udal) {
				new_arr[j]=arr[i];
				j++; 
			}
		}
  		newCookKol=new_arr.join("|");
		WriteCookie("kolstr",newCookKol);

		CookRealKol=ReadCookie("kol");
		WriteCookie("kol",Number(CookRealKol)-1);
		WriteCookie("kol",Number(CookRealKol)-1,"","/");

    	
		}
//	document.all.korzina.innerHTML="<a href='tovarpost.asp?action=bask'><font style='font-size:11pt;'><img src='images/korz.gif' style='margin:0 5 0 30; vertical-align:middle;' border=0></a>("+tk+")</font><a href='tovarpost.asp?action=bask'><font color=000000>Перейти в корзину</font></a><a href='/web/tovarpost.asp?action=dost#dostavka'><font color=434343 style='margin:0 33 0 0;'><img src='images/inf.gif' style='margin-left:20px; margin-right:4px;' border=0>О доставке</font></a></span>";

	document.all.korzina.innerHTML=home_tk+tk+end_tk;


	if (tk>9) tk=9;
	document.all.postman.innerHTML="<img src='images/postman"+tk+".png' border=0>";

	
   	client_state.elem = tovar;
	k_x = (screen.width-250)/2;
	k_y = (screen.height-100)/2;

 //  	window.open("basket.php?"+ client_state.saveToString(), "","height=101,width=251,status=no,resizable=no,toolbar=no,menubar=no,location=no,screenX="+k_x+",screenY="+k_y+'"');

}
function BasketFast(tovar)
{	

    nameimg="pic"+tovar;  
    tekimg=document.images[nameimg].src.substring(document.images[nameimg].src.length-8,document.images[nameimg].src.length-5);	
 	tk=document.all.korzina.innerHTML;

   	


	home_tk=tk.substr(0,tk.indexOf("(")+1);
	end_tk=tk.substr(tk.indexOf(")"));
	tk=tk.substr(tk.indexOf("(")+1);
	newlenght=tk.indexOf(")");
	tk=tk.substr(0,newlenght);
	el = document.getElementById('amount');
	if (el) client_state.kol = document.all.amount.value;	
	else client_state.kol = 1;
	
   
	if (tekimg == "add")	
	{
	 tk = tk-1+2;
  	 document.images[nameimg].src="images/del1.gif";
  	 document.images[nameimg].alt="Удалить из корзины";
 	 document.images[nameimg].title="Удалить из корзины";
    	client_state.action = "add"; 
    	CookTovar=ReadCookie("tovar");

		if (CookTovar==null) {
			WriteCookie("tovar",tovar);
			WriteCookie("kolstr",client_state.kol);
			WriteCookie("kol",1);
			WriteCookie("kol",1,"","/");
		}
		else {
			CookKol=ReadCookie("kolstr");	
			WriteCookie("tovar",CookTovar+"|"+tovar);
			WriteCookie("kolstr",CookKol+"|"+client_state.kol);
			CookRealKol=ReadCookie("kol");
			num_kol=Number(CookRealKol);
			num_kol++;
			WriteCookie("kol",num_kol);
			WriteCookie("kol",num_kol,"","/");
		}
 	}
	document.all.korzina.innerHTML=home_tk+tk+end_tk;


	if (tk>9) tk=9;
	document.all.postman.innerHTML="<img src='images/postman"+tk+".png' border=0>";

	
     	client_state.action = "bask";
    	document.forms[0].method = "post";
    	document.forms[0].action="tovarpost.asp?" + client_state.saveToString();
    	document.forms[0].submit();

}
function onRecalcBasketClick()
{

	CookTovar=ReadCookie("tovar");
	if (CookTovar.charAt(0) == "|") {
		CookTovar=CookTovar.substr(1);
	}
//	alert(CookTovar);

	arr2=CookTovar.split("|");
	CookRealKol=ReadCookie("kol");
	
	new_arr1= new Array();
	new_arr2= new Array();
	j=0;
	
    StrLine="";	

	for (i=0; i != document.koltov.elements.length-5; i++)
	{
   		tek_kol=document.koltov.elements[i].value;
 		
   		if (tek_kol!=0) {
		//	alert(tek_kol+" "+arr2[i]+" "+i+" "+j);
		//	alert(arr2[i]);
   		 
			new_arr1[j]=tek_kol;
			new_arr2[j]=arr2[i];
			j++; 

		}		
		else CookRealKol--;
	}
	newCookKol=new_arr1.join("|");
//	alert(newCookKol);
	WriteCookie("kolstr",newCookKol);
	newCookTovar=new_arr2.join("|");
//	alert(newCookTovar);
	WriteCookie("tovar",newCookTovar);
	WriteCookie("kol",CookRealKol);
	WriteCookie("kol",CookRealKol,"","/");

    client_state.action = "bask";
    document.forms[0].method = "post";
    document.forms[0].action="tovarpost.asp?" + client_state.saveToString();
    document.forms[0].submit();
}

function onClearBasketClick()
{
	WriteCookie("tovar","");
	WriteCookie("kolstr","");
	WriteCookie("kol",0);
	WriteCookie("kol",0,"","/");

    client_state.action = "bask";

    document.forms[0].method = "post";
    document.forms[0].action="tovarpost.asp?" + client_state.saveToString();
    document.forms[0].submit();
}
function CheckOrder() 
{
  	var CustEmailString = document.rekv.elements["CustEmail"].value;
	var nowdate = new Date();
	var hours = nowdate.getHours();
	var gmt = nowdate.getTimezoneOffset(); 

	if (document.koltov.elements.length == 3)
	{
		alert("Ваша корзина пуста!");
		return;
	}	
	if (document.rekv.elements["agree"].checked == false)
	{
		alert("Поставьте галочку об ознакомлении с условиями выполнения заказа!");
		return;
	}	
	if ( ( CustEmailString.indexOf('@',1) == -1 ) || ( CustEmailString.indexOf('.',CustEmailString.indexOf('@',1)) == -1 ) )
	{
		alert("Введите корректный e-mail (user@domain.name)!!!");
		return;
	}
	if (document.rekv.elements["CustPhone"].value == "")
	{
		alert("Введите номер телефона !!!");
		return;
	}
	if (document.rekv.elements["CustFIO"].value == "")
	{
		alert("Введите ФИО !!!");
		return;
	}

	
	if (document.rekv.elements["CustAdr"].value == "")
	{
		alert("Введите почтовый адрес !!!");
		return;
	}
	if (document.rekv.elements["CustInd"].value == "")
	{
		alert("Введите почтовый индекс !!!");
		return;
	}

	showBusyLayer();

    client_state.fio = document.rekv.elements["CustFIO"].value;
    client_state.adr = document.rekv.elements["CustAdr"].value;
    client_state.tel = document.rekv.elements["CustPhone"].value;
    client_state.mail = document.rekv.elements["CustEmail"].value;
    client_state.ind = document.rekv.elements["CustInd"].value;
    client_state.chs = hours;
    
    client_state.action = "order";
    document.forms[0].method = "post";
    document.forms[0].action = "tovarpost.asp?" + client_state.saveToString();
    document.forms[0].submit();
}
function AutoFill()
{
	var d1=new Date();
	var d2=d1.getTime()+(60*60*24*365000);
	
	var CustEmailString = document.rekv.elements["CustEmail"].value;
	if ( ( CustEmailString.indexOf('@',1) == -1 ) || ( CustEmailString.indexOf('.',CustEmailString.indexOf('@',1)) == -1 ) )
	{
		alert("Введите корректный e-mail (user@domain.name)!!!");
		return;
	}
//	client_state.mail = document.rekv.elements["CustEmail"].value;
		d1.setTime(d2);
		WriteCookie("mail",CustEmailString,d1);

    	client_state.action = "bask";
    	document.forms[0].method = "post";
    	document.forms[0].action = "tovarpost.asp?" + client_state.saveToString();
    	document.forms[0].submit();

}
function Kupon()
{

	var now = new Date();
	day = now.getDate();
	month = now.getMonth()+1;
	year = now.getYear()-100;
	nom=document.koltov.elements["nom_kupon"].value;
	kl1=parseInt(nom.charAt(0));
	kl2=parseInt(nom.charAt(kl1-1));
	itog='';
    for (var i = 1; i <= nom.length-1; i++) {
     	if (i!=kl1-1){
			tek_nom=nom.charAt(i);
			tek_nom=tek_nom-kl2;
			if (tek_nom<0) tek_nom=tek_nom+10;
			itog=itog+tek_nom;
		}
     }
	tek_day=itog.substring(0,2);
	tek_mes=itog.substring(2,4);
	tek_god=itog.substring(4,6);
	
	delta_day=tek_day-day;
	if (delta_day<0) tek_mes=tek_mes-1;
	delta_mes=tek_mes-month;
	if (delta_mes<0) tek_god=tek_god-1;	 
	delta_god=tek_god-year;
	if (delta_god<0){
		alert("срок действия купона истек "+itog.substring(0,2)+"."+itog.substring(2,4)+"."+itog.substring(4,6));	
		return;
		
	} 

	if (Math.ceil(kl2/3)==kl2/3) tek_sk=3;
	else if (Math.ceil(kl2/2)==kl2/2) tek_sk=2;
	else tek_sk=1;
	
	WriteCookie("sk",tek_sk);
	
    client_state.action = "bask";
    document.forms[0].method = "post";
    document.forms[0].action = "tovarpost.asp?" + client_state.saveToString();
    document.forms[0].submit();
	
	
}
function Poisk()
{
    client_state.action = "poisk";
    client_state.ctl_code = document.forms[0].elements["ctl_code"].value;
    document.forms[0].method = "post";
    document.forms[0].action = "tovarpost.asp?" + client_state.saveToString();
    document.forms[0].submit();
}
function Poisk1()
{
    client_state.action = "poisk";
    client_state.ctl_code = document.forms[0].elements["ctl_code"].value;
    document.forms[0].method = "post";
    document.forms[0].action = "http://www.tovarpost.ru/web/tovarpost.asp?" + client_state.saveToString();
    document.forms[0].submit();
}
function GoToOrder()
{
 	tk=document.all.korzina.innerHTML;
	tk=tk.substr(tk.indexOf("(")+1);
	tk=tk.substr(0,tk.length-1);

	if ( tk == 0 )
	{
		alert("Ваша корзина пуста");
		return;	
	}	
    client_state.action = "bask";
    document.forms[0].method = "post";
    document.forms[0].action = "tovarpost.asp?" + client_state.saveToString();
    document.forms[0].submit();
}
function open_window(link,w,h) {
var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
newWin = window.open(link,'newWin',win);
newWin.focus();
}

function open_window_rpo() {
//alert(document.forms["rpo"].elements["rpo_code"].value);
var win = "width=700,height=800,menubar=no,location=no,resizable=yes,scrollbars=yes";
link="http://www.tovarpost.ru/web/rpo.php?rpo="+ document.forms["rpo"].elements["rpo_code"].value;
newWin = window.open(link,'newWin',win);
newWin.focus();
//location.href="http://gdeposylka.ru/"+ document.forms["rpo"].elements["rpo_code"].value+"?tos=accept&apikey=76568.64832e3e5f";
}

var img = new Array();
var i;
var first = -1;
var last = -1;
var speed_ms = 10;             
var speed_px = 10;
var width_max = 200;
var width_min = 75;
var padding_top = 2;
var padding_left = 0;
var zoom_mode = 2;

function nameByWidth(name, width)
{
	vrname=name.replace("S","B");

	return vrname.replace('/s','/b');
}

var image_change = function (image_i) {
	var image = img[image_i];
	var width = parseInt(image.style.width) + image.speed;
	var top = parseInt(image.style.top) + image.top;
	var left = parseInt(image.style.left) + image.left;
	if (width < width_min) {
		image.setAttribute('src', nameByWidth(image.src, width_min));
		image.parentNode.style.position = 'static';
		image.style.position = 'static';
		clearInterval(image.interval);
	} else if (width > width_max) {
		clearInterval(image.interval);
	} else {
		image.style.width = width + 'px';
		image.style.height = width + 'px';
		image.style.top = top + 'px';
		image.style.left = left + 'px';
	}
}

function getAbsolutePos(el)
{
   var r = { x: el.offsetLeft, y: el.offsetTop };
   if (navigator.appName != "Microsoft Internet Explorer")

	{	
	
	   if (el.offsetParent)
   		{
       		var tmp = getAbsolutePos(el.offsetParent);
       		r.x += tmp.x;
       		r.y += tmp.y;
   		}

	}
   return r;
}

window.onload = function() {
	img = document.getElementsByTagName('img');
	for (i = 0; i < img.length; i++) {
		if (img[i].id == 'banner1') {  
		 //banner
			if (zoom_mode == 1) {
				if (first == -1 && img[i].offsetTop < width_max/2 + 10) first = i;
				else if (img[i].offsetTop + (width_min + width_max)/2 > document.height) last = i;
			}
			img[i].style.width = width_min + 'px';
			img[i].style.height = width_min + 'px';
			img[i].parentNode.style.top = 0 + 'px';
			img[i].parentNode.style.left = 0 + 'px';
			pos = getAbsolutePos(img[i]);
			img[i].style.top = pos.y + 'px';
			img[i].style.left = pos.x + 'px';

			img[i].xi = i;
			img[i].onmouseover = function () {
				this.speed = speed_px;
				this.style.zIndex = 10;
				
				this.parentNode.style.position = 'relative';
				this.style.position = 'absolute';
				if (this.xi == first || zoom_mode == 2) this.top = 0;
				else if (this.xi == last) this.top = -speed_px;
				else this.top = -Math.round(speed_px/2);
				if (zoom_mode == 1 || zoom_mode == 2) this.left = 0;
				else this.left = -Math.round(speed_px/2);
				if (this.interval) clearInterval(this.interval);
				this.src = nameByWidth(this.src, width_max);
				this.interval = setInterval("image_change("+this.xi+")", speed_ms);
			};
			img[i].onmouseout = function () {
				this.speed = -speed_px;
				this.style.zIndex = 1;
				
				if (this.xi == first || zoom_mode == 2) this.top = 0;
				else if (this.xi == last) this.top = speed_px;
				else this.top = Math.round(speed_px/2);
				if (zoom_mode == 1 || zoom_mode == 2) this.left = 0;
				else this.left = Math.round(speed_px/2);
				
				if (this.interval) clearInterval(this.interval);
				this.interval = setInterval("image_change("+this.xi+")", speed_ms);
			};
		}
	}
}

function err_report(tovar) {
   	client_state.elem = tovar;
	k_x = (screen.width-250)/2;
	k_y = (screen.height-100)/2;

  	window.open("reperr.php?"+ client_state.saveToString(), "","height=101,width=251,status=no,resizable=no,toolbar=no,menubar=no,location=no,screenX="+k_x+",screenY="+k_y+'"');

}
function forumsend(tovar,tm) {

        p_email=document.forumstr.elements["ForumEmail"].value;
        p_text=document.forumstr.elements["otziv"].value;
  //      p_email=document.forumstr.ForumEmail.value;
        
    	if (!p_email) {
    		alert("Введите e-mail !!!");
            document.forumstr.ForumEmail.focus();
       		return;
        }
        else
         { t=p_email.indexOf('@');
          if ((p_email.indexOf('.')==-1)||(t==-1)||(t < 1)||
           (t > p_email.length - 5) || (p_email.charAt(t - 1)=='.') || (p_email.charAt(t + 1)=='.') || (p_email.charAt(p_email.length-1)=='.')|| (p_email.charAt(0)=='.'))
          { alert("Введите корректрый e-mail !!!");
            document.forumstr.elements["ForumEmail"].focus();
//            document.forumstr.ForumEmail.focus();
            return false;
          }
        }    


   	client_state.elem = tovar;
   	client_state.tm = tm;
   	client_state.em = p_email;
   	client_state.otziv = p_text;
    
	k_x = (screen.width-250)/2;
	k_y = (screen.height-100)/2;

  	window.open("forum.php?"+ client_state.saveToString(), "","height=101,width=251,status=no,resizable=no,toolbar=no,menubar=no,location=no,screenX="+k_x+",screenY="+k_y+'"');

}
