var d = document;
var offsetfromcursorY=15 // y offset of tooltip
var ie=d.all && !window.opera;
var ns6=d.getElementById && !d.all;
 
function ietruebody(){
return (d.compatMode && d.compatMode!="BackCompat")? d.documentElement : d.body
}
 
function positiontip(e) {
    var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
    var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
    var winwidth=ie? ietruebody().clientWidth : window.innerWidth-20
    var winheight=ie? ietruebody().clientHeight : window.innerHeight-20
     
    var rightedge=ie? winwidth-event.clientX : winwidth-e.clientX;
    var bottomedge=ie? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;
 
    if (rightedge < tipobj.offsetWidth)  tipobj.style.left=curX-tipobj.offsetWidth+"px";
    else tipobj.style.left=curX+"px";
 
    if (bottomedge < tipobj.offsetHeight) tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
    else tipobj.style.top=curY+offsetfromcursorY+"px";
}
 
function appear() {
    if(op < 1) {
        op += 0.1;
        tipobj.style.opacity = op;
        tipobj.style.filter = 'alpha(opacity='+op*100+')';
        t = setTimeout('appear()', 30);
    }
}

var GoodsPos=new Array();
	
function CheckIsIE()
{
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') { return true;}
	else { return false; }
} 

function searchInputOnFocus()
{
  var inp=document.getElementById("search-panel-input");
  if (inp.value=="поиск...")
  {
    inp.className="search-panel-input";
    inp.value="";
  }
}

function searchInputOnBlur()
{
  var inp=document.getElementById("search-panel-input");
  if (inp.value=="")
  {
    inp.className="search-panel-input-nonactive";
    inp.value="поиск...";
  }
}
var selectedGoods=0;
var selectedGoodsNum=0;
function doOrder(id)
{
	selectedGoods=id;
	var pageScroll = Ext.fly(document).getScroll();
	var E=Ext.get('GlassWin');
	
	E.setStyle({
		left: pageScroll.left+'px',
		top: pageScroll.top+'px',
		width: Ext.get('SiteBody').getWidth()+'px',
		height: Ext.get('SiteBody').getHeight()+'px',
		filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80, Style=0)",
		opacity: '0.8'
	}).show();
	
	var E=Ext.get('qtyDlg');
	
	E.setStyle({
		left: (pageScroll.left+(Ext.lib.Dom.getViewportWidth()/2)-180/2)+'px',
		top: (pageScroll.top+(Ext.lib.Dom.getViewportHeight()/2)-65/2)+'px'
	}).show();
	Ext.get('GoodsQty').dom.value='1';
	Ext.get('GoodsQty').focus();
}

function cancelOrder()
{
	Ext.get('qtyDlg').hide();
	Ext.get('GlassWin').hide();
}

function addGoods(id,Qty)
{
	if (id)
	{
		selectedGoods=id;	
	}
	Ext.Ajax.request({
   	url: '/catalog',
		params: {AJAX: 1, command: 'AddGoodsToOrder', GoodsID: selectedGoods, GoodsQty: Qty ? Qty : Ext.get('GoodsQty').dom.value, module: 'orders'},
		method: 'POST',
   	success: function(response, opts) {
      var obj = eval('('+response.responseText+')');
			Ext.get('orderGoodsCount').dom.innerHTML=obj.goodsCount+' покупок';
			Ext.get('orderSum').dom.innerHTML='на '+obj.orderSum+' сомов';
   	},
   	failure: function(response, opts) {
      alert('Не удалось добавить товар в заказ.');
   	}
	});
	Ext.get('qtyDlg').hide();
	Ext.get('GlassWin').hide();
}

function AddGoodsPos(pos)
{
	GoodsPos[GoodsPos.length]=pos;
}

function deleteGoods(id,num)
{
	selectedGoods=id;
	selectedGoodsNum=num;
	var pageScroll = Ext.fly(document).getScroll();
	var E=Ext.get('GlassWin');
	
	E.setStyle({
		left: pageScroll.left+'px',
		top: pageScroll.top+'px',
		width: Ext.get('SiteBody').getWidth()+'px',
		height: Ext.get('SiteBody').getHeight()+'px',
		filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80, Style=0)",
		opacity: '0.8'
	}).show();
	
	var E=Ext.get('delDlg');
	
	E.setStyle({
		left: (pageScroll.left+(Ext.lib.Dom.getViewportWidth()/2)-180/2)+'px',
		top: (pageScroll.top+(Ext.lib.Dom.getViewportHeight()/2)-65/2)+'px'
	}).show();
	Ext.get('delOK').focus();
}

function cancelDelete()
{
	Ext.get('delDlg').hide();
	Ext.get('GlassWin').hide();
}

function delGoods(id,Qty)
{
	Ext.get('orderProgress').show();
	cancelDelete();
	if (id)
	{
		selectedGoods=id;	
	}
	Ext.Ajax.request({
   	url: '/catalog',
		params: {AJAX: 1, command: 'DelGoodsFromOrder', GoodsID: selectedGoods, pos: selectedGoodsNum, module: 'orders'},
		method: 'POST',
   	success: function(response, opts) {
      var obj = eval('('+response.responseText+')');
			Ext.get('OrderTotal').dom.innerHTML='Итого: '+obj.orderSum+' сом';

			Ext.get('orderGoodsCount').dom.innerHTML=obj.goodsCount+' покупок';
			Ext.get('orderSum').dom.innerHTML='на '+obj.orderSum+' сомов';

			Ext.get('orderProgress').hide();
			if (obj.pos) 
			{
				var i=-1;
				for (j = 0; j < GoodsPos.length; j++) {
					if (GoodsPos[j] == (obj.pos * 1)) {
						i = j;
						break;
					}
				}
				
				if (i >= 0) {
					document.getElementById('orderTB').tBodies[0].deleteRow(i+1);
					GoodsPos.splice(i,1);
					if (GoodsPos.length==0)
					{
						window.location='/order';
					}
				}
	  	}
   	},
   	failure: function(response, opts) {
			
   	}
	});
	Ext.get('qtyDlg').hide();
	Ext.get('GlassWin').hide();
}

function chQtyGoods(id)
{
	if (id)
	{
		selectedGoods=id;	
	}
	var Qty=Ext.get('qty-'+id).dom.value;
	Ext.Ajax.request({
   	url: '/catalog',
		params: {AJAX: 1, command: 'ChQtyGoodsOrder', GoodsID: selectedGoods, qty: Qty, module: 'orders'},
		method: 'POST',
   	success: function(response, opts) {
      var obj = eval('('+response.responseText+')');
			Ext.get('OrderTotal').dom.innerHTML='Итого: '+obj.orderSum+' сом';

			Ext.get('orderGoodsCount').dom.innerHTML=obj.goodsCount+' покупок';
			Ext.get('orderSum').dom.innerHTML='на '+obj.orderSum+' сомов';
			Ext.get('gs-'+obj.goodsid).dom.innerHTML=obj.goodsSum;

			Ext.get('orderProgress').hide();
   	},
   	failure: function(response, opts) {
			
   	}
	});
	Ext.get('qtyDlg').hide();
	Ext.get('GlassWin').hide();
}

function loginCheck()
{
	var S=Ext.get('new-login').dom.value;
	if (S.length < 4 || S.length > 20) {
  	Ext.get('loginCheck').dom.innerHTML = "<span style='color: #FF0000'>Логин должен быть не короче 4 символов и не длинее 20</span>";
  }
  else {
  	Ext.Ajax.request({
  		url: '/user-registration',
  		params: {
  			AJAX: 1,
  			command: 'loginCheck',
  			login: Ext.get('new-login').dom.value,
  			module: 'usersman'
  		},
  		method: 'POST',
  		success: function(response, opts){
  			var obj = eval('(' + response.responseText + ')');
  			if (obj.result == 1) {
  				Ext.get('loginCheck').dom.innerHTML = "<span style='color: #009c46'>Логин свободен</span>";
  			}
  			else {
  				Ext.get('loginCheck').dom.innerHTML = "<span style='color: #FF0000'>Логин занят</span>";
  			}
  		},
  		failure: function(response, opts){
  		
  		}
  	});
  }
}

function passwordCheck()
{
	var S=Ext.get('new-password').dom.value;
	var S1=Ext.get('conf-new-password').dom.value;
	if (S.length < 6 || S.length > 20) {
  	Ext.get('passwordCheck').dom.innerHTML = "<span style='color: #FF0000'>Пароль должен быть не короче 6 символов и не длинее 20</span>";
  }
  else if (S!=S1){
  	Ext.get('passwordCheck').dom.innerHTML = "<span style='color: #FF0000'>Пароль и подтверждение пароля не совпадают</span>";
  }
	else{
  	Ext.get('passwordCheck').dom.innerHTML = "";
	}
}

function refreshCaptcha()
{
	var dt=new Date();
	document.getElementById('captch').src='/utils/captcha.php?'+dt.getTime();	
}

function searchKeyPress(e)
{
	var keynum;
	if(window.event) // IE
  {
  	keynum = e.keyCode;
  }
	else if(e.which) // Netscape/Firefox/Opera
  {
  	keynum = e.which;
  }
	if (keynum==13)
	{
		window.location='/catalog/?search='+Ext.get('search-panel-input').dom.value+'&from=0&to=9';		
	}
}

function addGoodsKeyPress(e)
{
	var keynum;
	if(window.event) // IE
  {
  	keynum = e.keyCode;
  }
	else if(e.which) // Netscape/Firefox/Opera
  {
  	keynum = e.which;
  }
	if (keynum==13)
	{
		addGoods();		
	}
}

var mapShow=false;
function showMap()
{
	if (!mapShow) {
  	var E = Ext.get('mapDivShadow');
  	E.setStyle({
  		left: Ext.get('map_left').getRight() + 1 + 'px',
  		top: 106 + 'px',
  		opacity: '0.6'
  	}).show();
  	
  	var E = Ext.get('mapDiv');
  	E.setStyle({
  		left: Ext.get('map_left').getRight() - 3 + 'px'
  	}).show();
		mapShow=true;
  }
	else
	{
		Ext.get('mapDivShadow').hide();
		Ext.get('mapDiv').hide();
		mapShow=false;
	}
}
function map_print()
{
  if (CheckIsIE()==true)
  {
		window['prn_map'].focus();
		window['prn_map'].print()
//    document.getElementById('prn_map').contentWindow.focus();
//    document.getElementById('prn_map').contentWindow.print(); 
  }
  else
  {
    window.frames['prn_map'].focus(); 
    window.frames['prn_map'].print(); 
  }
}

var tooltipShowed=false;
function showToolTip(event, text, elem){
	var pageScroll = Ext.fly(document).getScroll();
	if (!tooltipShowed) {
		
		// Определяем какой именно тоолтип
		if ((Ext.get('tooltip').getHeight() + 5) < (Ext.get(elem).getY() - pageScroll.top)) 
		{
			Ext.get('tooltip-text').dom.innerHTML = text;
			var x, y;
			x = Ext.get(elem).getX() + Ext.get(elem).getWidth() / 2 - 276;
			y = Ext.get(elem).getY() - Ext.get('tooltip').getHeight() - 5;
			Ext.get('tooltip').setLeft(x + 'px');
			Ext.get('tooltip').setTop(y + 'px');
			Ext.get('tooltip').show(true);
			tooltipShowed = true;
		}
		else
		{
			Ext.get('tooltip_180-text').dom.innerHTML = text;
			var x, y;
			x = Ext.get(elem).getX() + Ext.get(elem).getWidth() / 2 - 276;
			y = Ext.get(elem).getY() + Ext.get(elem).getHeight() + 5;
			Ext.get('tooltip_180').setLeft(x + 'px');
			Ext.get('tooltip_180').setTop(y + 'px');
			Ext.get('tooltip_180').show(true);
			tooltipShowed = true;
		}
	}
}

function hideToolTip()
{
	tooltipShowed=false;
	Ext.get('tooltip').hide();
	Ext.get('tooltip_180').hide();
}

Ext.onReady (function()
{
	Ext.get('GlassWin').hide();
});
