// JavaScript Document
function testtt()
{
	alert ("1");
}
function win(w,h) 
{
	var w1;
		w1=open("","w1","toolbar=0,directories=0,status=0,menubars=0,scrollbars=0,resizable=0,width="+w+",height="+h); 
		w1.resizeTo(w, h);		
		w1.focus();
} 
function win1(w,h) 
{
	var w1;
		w1=open("","w2","toolbar=1,directories=0,status=1,menubars=1,scrollbars=1,resizable=1,width="+w+",height="+h); 
		w1.resizeTo(w, h);		
		w1.focus();
}

function win2(w,h) 
{
	var w1;
		w1=open("","w3","toolbar=1,directories=0,status=1,menubars=1,scrollbars=1,resizable=1,width="+w+",height="+h); 
		w1.resizeTo(w, h);		
		w1.focus();
} 

function size(x,y)
{
	window.resizeTo(x,y);
}

function toggle()
	{
	if(document.getElementById('rndm').style.width=='171px')
		{
		document.getElementById('rndm').style.width='10px';
		document.getElementById('rndm').style.height='10px';
		document.getElementById('hid').style.visibility='hidden'; 
		}
	else
		{
		document.getElementById('rndm').style.width='171px';
		document.getElementById('hid').style.visibility='visible'; 
		document.getElementById('rndm').style.height='auto';
		}
	}

/* -------------------------- */
/*   XMLHTTPRequest Enable    */
/* -------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	else {
		request_type = new XMLHttpRequest();
		}
	return request_type;
	}

var http = createObject();

/* -------------------------- */
/*        SEARCH              */
/* -------------------------- */
var p_iid;




function increase(iid, action)
{
p_iid=iid;	
http.open('GET', 'ajax/increase.php?itemid='+iid+'&act='+action);
http.onreadystatechange =  increaseReply;
http.send(null);
}

function additem(iid) {

p_iid=iid;	
//sel=null;

//pos=getElPos("searchstring");

//earchstring = encodeURI(document.getElementById('searchstring').value);
// Set te random number to add to URL request
http.open('GET', 'ajax/additem.php?itemid='+p_iid);
http.onreadystatechange =  additemReply;
http.send(null);
}


function additemReply() {
if(http.readyState == 4){
var response = http.responseText;
refreshTotal();
document.getElementById('add'+p_iid).innerHTML = response;
}
else
	{
document.getElementById('add'+p_iid).innerHTML = "Подождите...";
}
}

function increaseReply() {
if(http.readyState == 4){
var response = http.responseText;
if(response=="up")
	{
document.location.reload(true);
	}
else
	{
		refreshTotal();
		document.getElementById('c'+p_iid).innerHTML = response;
	}
}
else
	{
document.getElementById('c'+p_iid).innerHTML = "Подождите...";
}
}

function refreshTotal()
{
http.open('GET', 'ajax/reftotal.php');
http.onreadystatechange =  totalReply;
http.send(null);
}
function totalReply() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('total1').innerHTML = response;
document.getElementById('total').innerHTML =response; 

}
else
	{
document.getElementById('total').innerHTML= document.getElementById('total1').innerHTML  = "Подождите...";
}
}
function showbanner()
	{
	var ptr=Math.random();
	if(ptr<0.5)
		{
		document.getElementById('hbanner').innerHTML="<div style=\"position: static; width: 100%; text-align: center;\"><a href=\"privacy2.php?n2=71\"><img src=\"images/banner_vr.jpg\" border=\"0\" /></a></div>";
		}
	else
		{
		document.getElementById('hbanner').innerHTML="<div style=\"position: static; width: 100%; text-align: center;\"><a href=\"privacy.php?n=71\"><img src=\"images/banner_sp_header.jpg\" border=\"0\" title=\"Специальное предложение\" /></a></div><div style=\"margin-top: -3px; position: static; width: 100%; text-align: center;\"><a href=\"privacy.php?n=73\"><img src=\"images/banner_sp_k.jpg\" border=\"0\"></a><a href=\"privacy.php?n=71\"><img src=\"images/banner_sp_middle.jpg\" border=\"0\"></a><a href=\"privacy.php?n=72\"><img src=\"images/banner_sp_pk.jpg\" border=\"0\"></a></div>";
		}
	}
function isempty(sid)	{
	if(document.getElementById(sid).value=='')	{
		return false;
	}
	else	{
		return true;
	}
}

function checkmail(sid)	{
	if(isempty(sid))	{
		var adr=document.getElementById(sid).value;
		var reg=/\w+[@]\w+[.]\w{2,3}$/;
		return reg.test(adr);
	}
	else	{
		return true;
	}
}

function z_checkmail(sid, did) {
	
	if(isempty(sid))
		{
		if(checkmail(sid))	{
			value="<span class='correct'>Адрес введен корректно</span>"; 
			document.getElementById(did).innerHTML=value;
			
		}
		else	{
			value="<span class='incorrect'>Адрес введен некорректно</span>" ;
			document.getElementById(did).innerHTML=value;
			}
		
	}
	else
		value="<span class='incorrect'></span>" ;
		document.getElementById(did).innerHTML=value;


	
}


function z_checkempty(sid, did)	{
	 if(isempty(sid))	{
			value="<span class='correct'>Заполнено корректно</span>"; 
	 }
	 else	{
		 value="<span class='incorrect'>Поле не должно быть пустым</span>" ;
	 }
	 
	 document.getElementById(did).innerHTML=value;
}


function z_check()	{
	

		mail=checkmail('mainmail');

	name=isempty('mainname');
	phone=isempty('mainphone');
	if(mail&&name&&phone)	{
		return true;
	}
	else	{
		z_checkempty('mainname', 'mainnameinfo');
		z_checkempty('mainphone', 'mainphoneinfo');
		z_checkmail('mainmail', 'mainmailinfo');
		document.getElementById('mainforminfo').innerHTML="Пожалуйста, заполните обязательные поля. Проверьте, правильно ли Вы ввели адрес электронной почты, если Вы начали его вводить.";
		document.getElementById('mainforminfo').style.visibility="visible";
		return false;
	}
	
}

function drawcounter()
{
	cnsd=document;cnsd.cookie="b=b";cnsc=cnsd.cookie?1:0;
					document.getElementById("counter").innerHTML="<img src=\"stat/cntg.php?c="+cnsc+"&amp;r="+escape(cnsd.referrer)+"&amp;p="+escape(cnsd.location)+"\" width=\"88\" height=\"31\" border=\"0\" alt=\"Счетчик\" id=\"statLoginForm\"/>";
}

function supertree(cats){
	trbl=cats.toString().split (',');
	var i=0;
	while(i<trbl.length-2){
	document.setTimeOut(tree(trbl[i],i+1,null,trbl[trbl.length-2]), 1000);
//	alert("tree("+trbl[i]+",1,null,"+trbl[trbl.length]+");");
	i++;	
	}
	
}
function getOffset(elem) {
    if (elem.getBoundingClientRect) {
        // "правильный" вариант
        return getOffsetRect(elem)
    } else {
        // пусть работает хоть как-то
        return getOffsetSum(elem)
    }
}

function getOffsetSum(elem) {
    var top=0, left=0
    while(elem) {
        top = top + parseInt(elem.offsetTop)
        left = left + parseInt(elem.offsetLeft)
        elem = elem.offsetParent
    }

    return {top: top, left: left}
}

function getOffsetRect(elem) {
    // (1)
    var box = elem.getBoundingClientRect()

    // (2)
    var body = document.body
    var docElem = document.documentElement

    // (3)
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft

    // (4)
    var clientTop = docElem.clientTop || body.clientTop || 0
    var clientLeft = docElem.clientLeft || body.clientLeft || 0

    // (5)
    var top  = box.top +  scrollTop - clientTop
    var left = box.left + scrollLeft - clientLeft

    return { top: Math.round(top), left: Math.round(left) }
}
function pup(){
		var coords=getOffset(document.getElementById('counter'));
		var c=coords.top-85;
		$("#statlogin").css('top', c).css('left', coords.left).fadeIn('slow');
	
		};
function pdn(){
	$("#statlogin").fadeOut('slow');
}




//Первичная отрисовка баннера
function makeBanner(num){



var error = 0;
			

var items = new Array(); //будет хранить уже загруженные баннеры
		
		 $.ajax({
		        type:		"post",
		        url:		"ajax/buildbanner.php",
			    timeout:	5000,
		        data:		"ids=100",
		        
			    success: function(data){
				
				 	items = eval('('+data+')');

				 	for (var i = 0; i<num; i++) {
				 					 		
				 		$("#b" + i).html("<div class='imgcontainer'><a href='view_product.php?product="+items.data[i].id+"'><img src='/images/product/"+items.data[i].image+"' /></a></div><div class='textcontainer'><a href='view_product.php?product="+items.data[i].id+"'>"+items.data[i].title+"</a></div><div class='pricecontainer'><s>"+items.data[i].price+"</s><br />"+items.data[i].sale_price+" р.<div></div>");
				 		$("#b" + i).fadeIn(2000);
				 	}

setInterval(	function(){
	var num = Math.floor(Math.random () * 3)
	var rnd = Math.floor(Math.random () * items.data.length)
	//alert(items.data.length);


	$("#b" + num).fadeOut(2000, function(){$("#b" + num).html("<div class='imgcontainer'><a href='view_product.php?product="+items.data[rnd].id+"'><img src='/images/product/"+items.data[rnd].image+"' /></a></div><div class='textcontainer'><a href='view_product.php?product="+items.data[rnd].id+"'>"+items.data[rnd].title+"</a></div><div class='pricecontainer'><s>"+items.data[rnd].price+"</s><br />"+items.data[rnd].sale_price+" р.<div></div>")});
	$("#b" + num).fadeIn(2000);
}, 4000);	 	
				 
				},
		        error: function(data){
					//alert('err');
					error = 1;
				}
			 });

		
	
		
	
	 
	 
		 
		
}


$(document).ready(function(){
	makeBanner(5);
	
})



function getonebanner (items) {
	
	var num = Math.floor(Math.random () * 3)
	var rnd = Math.floor(Math.random () * items.length)


	
	
	$("#b" + num).fadeOut(2000, function(){$("#b" + num).html("<div class='imgcontainer'><a href='view_product.php?product="+items.data[rnd].id+"'><img src='/images/product/"+items.data[rnd].image+"' /></a></div><div class='textcontainer'><a href='view_product.php?product="+items.data[rnd].id+"'>"+items.data[rnd].title+"</a></div><div class='pricecontainer'><s>"+items.data[rnd].price+"</s><br />"+items.data[rnd].sale_price+" р.<div></div>")});
	$("#b" + num).fadeIn(2000);

			 		
			 			 	
			 
	
	
	 
	
	
	
	 
}

$(document).ready(function(){
//	
})

