//------------------------------------------------------------------------------
//--------------------------  переменные --------------------------------------
//------------------------------------------------------------------------------
var ie=document.all
var ie4=document.all
var ie5=document.all

var ns6=document.getElementById && !document.all
var ns4=document.layers
var req;
var ch;
var clc=0;


//browserJS();

//------------------------------------------------------------------------------
//--------------------------  переменные --------------------------------------
//------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
//------------------------------- ajax -----------------------------------------
//------------------------------------------------------------------------------
function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest){
httprequest=new XMLHttpRequest()
if (httprequest.overrideMimeType)
httprequest.overrideMimeType('text/xml')
}
else if (window.ActiveXObject){
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest
}
//------------------------------------------------------------------------------
var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.ajaxobj=createAjaxObj()
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=1

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj()
if (ajaxpack.addrandomnumber==1)
var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
if (this.ajaxobj){
this.filetype=filetype
this.ajaxobj.onreadystatechange=callbackfunc
this.ajaxobj.open('GET', url+"?"+parameters, true)
this.ajaxobj.send(null)
}
}
//------------------------------------------------------------------------------
var current_id='';
//------------------------------------------------------------------------------
function processGetPost(){
var myajax=ajaxpack.ajaxobj
var result='';

if (myajax.readyState == 4){
	if (myajax.status==200){		result=myajax.responseText;
		///alert(result);
		}
}

switch(result)
{

	case "_ok_"  :{
		message_popup(document.getElementById('but'+current_id),"Заказ изменен");
		} break;

	case "_noshow_"  :{
		error_popup(document.getElementById('but'+current_id),"Извините, товар недоступен для заказа");
		} break;

	case "noavt" :{
		document.getElementById(current_id).value='';		noavtorisation_popup(document.getElementById('but'+current_id))
		} break;

	case "badid" :{		document.getElementById(current_id).value='';
		alert("Неправильный id");
		} break;

	case "badkol":{		document.getElementById(current_id).value='';
		error_popup(document.getElementById('but'+current_id),"Недопустимое значение");
		} break;
	default:  break;
}


}
//------------------------------------------------------------------------------
function test(xxx){
   	var val=document.getElementById(xxx).value;

   	if (val=='') {
   		val=1;
    	document.getElementById(xxx).value=1;
    	}
   	current_id=xxx;
	ajaxpack.getAjaxRequest('buy.php', 'id='+xxx+'&kol='+val, processGetPost, 'txt');
}
//------------------------------------------------------------------------------
//------------------------------- ajax -----------------------------------------
//------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
//------------------------------- tool-tip -------------------------------------
//------------------------------------------------------------------------------

document.write('<div id="dhtmltooltip">tooltip</div>')
document.onmousemove=positiontip

var offsetxpoint=0
var offsetypoint=22
var enabletip=false

if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function positiontip(e){
if (typeof enabletip == "undefined")	return;

if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : 0

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}


//------------------------------------------------------------------------------
function t1(thetext, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"

switch(thetext)
		{
			case "L1":{	thetext="<strong>Есть в наличии</strong><br> - мало";	} break;
			case "L2":{	thetext="<strong>Есть в наличии</strong><br> - достаточно";	} break;
			case "L3":{	thetext="<strong>Есть в наличии</strong><br> - много";	} break;
			case "T1":{	thetext="<strong>Ожидается</strong><br> - мало";	} break;
			case "T2":{	thetext="<strong>Ожидается</strong><br> - достаточно";	} break;
			case "T3":{	thetext="<strong>Ожидается</strong><br> - много";	} break;
			case "D":{	thetext="Доставка под заказ<br> - 10 дней";	} break;
			case "N":{	thetext="Доставка под заказ<br> - 2 дня";	} break;

			default:  break;
		}

tipobj.innerHTML=thetext
enabletip=true
return false
}
}
//------------------------------------------------------------------------------
function t2(){
enabletip=false

if (ns6||ie){

	try{
		tipobj.style.visibility="hidden"
		tipobj.style.left="0px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
	}
		catch (e){}
}
}
//------------------------------------------------------------------------------
//---------------------------- popup -------------------------------------------
//------------------------------------------------------------------------------

//drag drop function for NS 4////
/////////////////////////////////

var dragswitch=0
var nsx
var nsy
var nstemp

function drag_dropns(name){
if (!ns4)
return
temp=eval(name)
temp.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP)
temp.onmousedown=gons
temp.onmousemove=dragns
temp.onmouseup=stopns
}

function gons(e){
temp.captureEvents(Event.MOUSEMOVE)
nsx=e.x
nsy=e.y
}
function dragns(e){
if (dragswitch==1){
temp.moveBy(e.x-nsx,e.y-nsy)
return false
}
}

function stopns(){
temp.releaseEvents(Event.MOUSEMOVE)
}

//drag drop function for ie4+ and NS6////
/////////////////////////////////


function drag_drop(e){
if (ie4&&dragapproved){
crossobj.style.left=tempx+event.clientX-offsetx
crossobj.style.top=tempy+event.clientY-offsety
return false
}
else if (ns6&&dragapproved){
crossobj.style.left=tempx+e.clientX-offsetx+"px"
crossobj.style.top=tempy+e.clientY-offsety+"px"
return false
}
}

function initializedrag(e){
crossobj=ns6? document.getElementById("popup") : document.all.popup
var firedobj=ns6? e.target : event.srcElement
var topelement=ns6? "html" : document.compatMode && document.compatMode!="BackCompat"? "documentElement" : "body"
while (firedobj.tagName!=topelement.toUpperCase() && firedobj.id!="dragbar"){
firedobj=ns6? firedobj.parentNode : firedobj.parentElement
}

if (firedobj.id=="dragbar"){
offsetx=ie4? event.clientX : e.clientX
offsety=ie4? event.clientY : e.clientY

tempx=parseInt(crossobj.style.left)
tempy=parseInt(crossobj.style.top)

dragapproved=true
document.onmousemove=drag_drop
}
}

//---- не забываем восстановить tooltip после drag
document.onmouseup=new Function("dragapproved=false;document.onmousemove=positiontip;")

////drag drop functions end here//////

function hidebox(){
crossobj=ns6? document.getElementById("popup") : document.all.popup
if (ie4||ns6)
crossobj.style.visibility="hidden"
else if (ns4)
document.popup.visibility="hide"
}

//********************************
function showbox(obj,thetext,thewidth){

www=200;

popuptext=document.getElementById("popup_text")
popuptext.innerHTML=thetext

crossobj=document.getElementById("popup")

if (thewidth){www=thewidth; }
else{www=220;}

crossobj.style.width=www+"px";
if (obj){

	var L=getposOffset(obj,"left")-(crossobj.clientWidth/2)+(obj.clientWidth/2)
	var T=getposOffset(obj)-crossobj.clientHeight-10

	if (L<0) {L=5;}
	if (T<0) {T=5;}

	// правый край экрана
    var deltaL=screen.width-(L+crossobj.clientWidth)
    if (deltaL<0) {L=L+deltaL-28;}

    // верхний край экрана
    var curY=ietruebody().scrollTop;
    if (T<curY) T=curY+5;

    // нижний край экрана
    var deltaY=(curY+screen.height)-(T+crossobj.clientHeight);
    if (deltaY<0) {T=T+deltaY;}

//    popuptext.innerHTML=popuptext.innerHTML+'<br>delta='+deltaL
//    popuptext.innerHTML=popuptext.innerHTML+'<br>curY='+curY
//    popuptext.innerHTML=popuptext.innerHTML+'<br>T='+T

	crossobj.style.left=L+"px"
	crossobj.style.top=T+"px"
}

else{
crossobj.style.left="100px"
crossobj.style.top="100px"
}
crossobj.style.visibility="visible"

}
//*********************************

function noavtorisation_popup(obj){

showbox(obj,'<table border="0" width="100%"><tr><td><img src="i/stopnew.gif"></td><td align="left"><strong>Извините, но Вы не авторизованы.</strong></td></tr></table>Если у Вас нет учетной записи, то для оформления заказа Вам необходимо <a href="index.php?act=reg"><b>зарегистрироваться</b></a>',280);

}

function message_popup(obj,text){
showbox(obj,'<table border="0" width="100%"><tr><td><img src="i/about.gif"></td><td align="left"><strong>'+text+'</strong></td></tr></table>');}

function error_popup(obj,text){

showbox(obj,'<table border="0" width="100%"><tr><td><img src="i/stopnew.gif"></td><td align="left"><strong>'+text+'</strong></td></tr></table>',280);
}

//------------------------------------------------------------------------------
//---------------------------- popup -------------------------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//---------------------------- menu --------------------------------------------
//------------------------------------------------------------------------------
var disappeardelay=250  //menu disappear speed onMouseout (in miliseconds)
var enableanchorlink=0 //Enable or disable the anchor link when clicked on? (1=e, 0=d)
var hidemenu_onclick=1 //hide menu when user clicks within menu? (1=yes, 0=no)

//------------------------------------------------------------------------------
function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function showhide(obj, e, visible, hidden){
if (ie5||ns6)
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=0
if (whichedge=="rightedge"){
var windowedge=ie5 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var topedge=ie5 && !window.opera? iecompattest().scrollTop : window.pageYOffset
var windowedge=ie5 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
}
}
return edgeoffset
}

function dropdownmenu(obj, e, dropmenuID){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
if (typeof dropmenuobj!="undefined") //hide previous menu
dropmenuobj.style.visibility="hidden"
clearhidemenu()
if (ie5||ns6){
obj.onmouseout=delayhidemenu
dropmenuobj=document.getElementById(dropmenuID)
if (hidemenu_onclick) dropmenuobj.onclick=function(){dropmenuobj.style.visibility='hidden'}
dropmenuobj.onmouseover=clearhidemenu
dropmenuobj.onmouseout=ie5? function(){ dynamichide(event)} : function(event){ dynamichide(event)}
showhide(dropmenuobj.style, e, "visible", "hidden")
dropmenuobj.x=getposOffset(obj, "left")+3
dropmenuobj.y=getposOffset(obj, "top")+1
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
return clickreturnvalue()
}

function clickreturnvalue(){
if ((ie5||ns6) && !enableanchorlink) return false
else return true
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function dynamichide(e){
if (ie5&&!dropmenuobj.contains(e.toElement))
delayhidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
delayhidemenu()
}

function delayhidemenu(){
try{
	delayhide=setTimeout("dropmenuobj.style.visibility='hidden'",disappeardelay)
}
	catch (e){}
}

function clearhidemenu(){
if (typeof delayhide!="undefined"){
	clearTimeout(delayhide)
	}
}
//------------------------------------------------------------------------------
//---------------------------- menu --------------------------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//--------------------------  разное -------------------------------------------
//------------------------------------------------------------------------------
//function submitenter(myfield,e)
function s_e(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
else return true;

if (keycode == 13){	return false;}

return true;
}
//------------------------------------------------------------------------------
function check_psw(obj,psw,chk_psw){

if(psw.length<4){
	error_popup(obj,"Длина пароля должна быть более 4-х символов.")
	return false;
}
if(psw!=chk_psw){
	error_popup(obj,"Подтверждение пароля не верно.");
	return false;
}

return true;
}
//------------------------------------------------------------------------------

function MY_MAKE_DATE(d,m,y,input){
z=y.options[y.selectedIndex].value + "-" + m.options[m.selectedIndex].value + "-" + d.options[d.selectedIndex].value;
input.value=z;
return;
}

//------------------------------------------------------------------------------
function MakeDown(id){
	var val=document.getElementById(id).value;
	if(val<2){return;}
	if(val=='NaN'){val=1;}
	document.getElementById(id).value=val-1;
	if(document.getElementById(id).value=='NaN'){document.getElementById(id).value=1;}
}

//------------------------------------------------------------------------------
function MakeUp(id){
	var val=document.getElementById(id).value;
	if(val=='NaN'){	val=1; }
	document.getElementById(id).value=Math.floor(val)+1;
	if(document.getElementById(id).value=='NaN'){document.getElementById(id).value=1;}
}
//------------------------------------------------------------------------------
function LOGIN_FILD_CLEAR(fild,clear){
	if(fild.value==clear){fild.value="";}
}

//------------------------------------------------------------------------------
function Change_img(img,url){
	img.src=url;
	}

//подсветка строки (highLight)
function hl1(obj){	obj.style.backgroundColor='#EFEFEF';}

function hl2(obj,k){
k1 = Number(k);
clc1 = Number(clc);
   if(k1 != clc1){
 	  obj.style.backgroundColor='#FAFAFA';	}
	else{
		obj.style.backgroundColor='#A7C7F7';
	}

}

function hl3(obj,k){
    obj.style.backgroundColor='#A7C7F7';
    if (clc !=0){
	ll=document.getElementById("z"+clc);
	if (ll != null){
		ll.style.backgroundColor='#FAFAFA';
	}
	}
	clc=k;
}




function processReqChange() {
 if (req.readyState == 4) {
if (req.status == 200) {
  var tb=req.responseText;
  document.getElementById('tabtovar').innerHTML =tb;
} else {
//alert("Не удалось получить данные:" + ch);
ch.disabled=false;
if(ch.checked==true){	ch.checked=false;
}else{	ch.checked=true;
}
}
}
}





function loadXMLDoc(url) {
if (window.XMLHttpRequest) {

req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);

req.send(null);
ab = window.setTimeout("req.abort();", 5000);
}
 else if (window.ActiveXObject) {

req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {

req.onreadystatechange = processReqChange;

req.open("GET", url, true);

req.send();
} }
}


function fil(val,tar){
 ch=val;
 if(val.checked==true){		c=1;
	}else{		c=0;
	}
 val.disabled=true;
 myDomain=window.location.hostname;
 var largeExpDate = new Date ();
 largeExpDate.setTime(largeExpDate.getTime() + (3600 * 1000));
 SetCookie('check_c',c,largeExpDate,myDomain);

	loadXMLDoc('tb.php?tar='+tar+'&c='+c);
}

//function browserJS(){
//alert("dd");
// var largeExpDate=new Date();
// myDomain=window.location.hostname;
// largeExpDate.setTime(largeExpDate.getTime());// SetCookie('bjs',1,largeExpDate,myDomain);
//}

 function SetCookie (name, value, expires, domain) {
 var argv = SetCookie.arguments;
 var argc = SetCookie.arguments.length;

 path = '/';
 if (domain == '') domain = 'localhost';
 var secure = (argc > 5) ? argv[5] : false;
 ab = name + "=" + value +
  ((expires == null) ? "" : ("; expires=" +expires.toGMTString())) +

  ((path == null) ? "" : ("; path=" + path)) +
 ((domain == null) ? "" : ("; domain=" + domain)) +
 ((secure == true) ? "; secure" : "");
 document.cookie = name + "=" + value +
 ((expires == null) ? "" : ("; expires=" +expires.toGMTString())) +
  ((path == null) ? "" : ("; path=" + path));
 }

//------------------------------------------------------------------------------
//--------------------------  разное -------------------------------------------
//------------------------------------------------------------------------------

