/*------------------------Ajax--------------------------------------*/
var isGecko;var iframe = new Array(99);var iWin = new Array(99);var iDoc = new Array(99);
function createRequestObject(){/* Создание нового объекта XMLHttpRequest для общения с Web-сервером */
  var xmlHttp = false;
  /*@cc_on @*//*@if (@_jscript_version >= 5)
  try	{	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
  catch (e)	{
  	try{xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
  	catch (e2){xmlHttp = false;}
  	}
  @end @*/
  if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){xmlHttp = new XMLHttpRequest();}
  return xmlHttp;
}
xmlHttp = createRequestObject();
/*------------------------DOMReady--------------------------------------*/
DOMReady=function(A){var C=false;ready=function(){if(!C){A();C=true}};try{document.addEventListener("DOMContentLoaded",ready,false)}catch(B){timer=setInterval(function(){if(/loaded|complete/.test(document.readyState)){clearInterval(timer);ready()}},10)}window.onload=function(){ready()}};
/*----------------------------------------------------------------------*/

function DM_Ajax_Login(user, psw)
    {
    	var url = "../../cgi-bin/users/ajax.cgi?in_user=" + user +  "&in_passw=" + psw + "&mode=login";
    	xmlHttp.open ("GET", url, true);
    	xmlHttp.onreadystatechange = DM_Ajax_LoginReport;
    	xmlHttp.send(null);
    }
function DM_Ajax_LoginReport()
{if ( xmlHttp.readyState == 4 )
	{if ( xmlHttp.status == 200 )
		{
			var response = xmlHttp.responseText;
			// проверить
			var re = new RegExp("(error)","ig");
			if ( re.exec(response) )
			{
				alert("Ошибка авторизации: " + response);
			}
			else
			{
				setCookie("session", response);
				setCookie("user", document.getElementById('login').value );
				document.getElementById("l1").style.display='none';
				document.getElementById("login_wrap2").style.display='none';
				document.getElementById("l2").style.display='block';
				document.getElementById('user_').innerHTML = getCookie("user");
			}
		}
		else if ( xmlHttp.status == 404 )		{         		alert("Аторизация: Request URL does not exist");		}
		else if ( xmlHttp.status == 0 )		{ }
		else		{         		alert("Error: Авторизация status code is " + xmlHttp.status );		}
	}
}
myDomain = document.location.hostname;//Alex
myDomain = myDomain.replace(/www./g, '');//Alex  
function setCookiesa (name, value, expires)
{

document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + "; path=/;domain="+myDomain;}

function setCookie (name, value)
{
	//pathname = location.pathname;
	//myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
	var largeExpDate = new Date ();
	largeExpDate.setTime(largeExpDate.getTime() + (24 * 3600000));
	setCookiesa ( name, value, largeExpDate);
}

function unLogin()
{
	setCookie ("user", "");
	setCookie ("passw", "");
	setCookie ("session", "");

	document.getElementById("l2").style.display='none';
	document.getElementById("l1").style.display='block';
	//doUpdateOrder ();
	document.location.href = "/";//Alex
}

function getCookie(name)
{ 
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1) return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length) 
	if (cookieEndIndex == -1) 
	cookieEndIndex = document.cookie.length;

	var ret = unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	if(ret == '') return null;
	else return ret;
}
/*-----------------------расширенный поиск------------------*/
function onSelectWender ( wender )
{
url = "../../cgi-bin/catalog/ajax.cgi?mode=pos_select&id=" + wender;
// Открыть соединение с сервером
xmlHttp.open ("GET", url, true);
// Установить функцию для сервера, которая выполнится после его ответа
xmlHttp.onreadystatechange = DM_Ajax_SelectWender;
// Передать запрос
xmlHttp.send(null);
}

function DM_Ajax_SelectWender ()
{if ( xmlHttp.readyState == 4 )
	{if ( xmlHttp.status == 200 )
		{
		document.getElementById("atm_pos_span").innerHTML = "";
		var response = xmlHttp.responseText;
		document.getElementById("atm_pos_span").innerHTML = response.replace(/\n/g, "");
		// alert ( "clear" );		
		}
       else if ( xmlHttp.status == 404 ){alert("Request URL does not exist");}
	   else if ( xmlHttp.status == 0 )		{ }
       else	{alert("Error: status code is " + xmlHttp.status );}
	}
}

function onSelectPos ( pos )
{
var url = "../../cgi-bin/catalog/viewpos.cgi?in_id=" + pos;
location.href = url;
}
/*-----------------------Ajax корзина------------------*/
function clearOdersAjax()
{
	var url = "/cgi-bin/order/ajax.cgi?mode=clear";
	xmlHttp.open ("GET", url, true);
	xmlHttp.onreadystatechange = clearOdersAjaxR;
	xmlHttp.send(null);
}

function clearOdersAjaxR()
	{if ( xmlHttp.readyState == 4 )
		{if ( xmlHttp.status == 200 ){doUpdateOrder ();alert("Ok")}
	   else if ( xmlHttp.status == 404 ){alert("clearOdersAjaxR Request URL does not exist");}
	   else if ( xmlHttp.status == 0 )		{ }
	   else{alert("clearOdersAjaxR Error: status code is " + xmlHttp.status );}
		}
	}
function clearOders()
{
	if (confirm("Уверены, что хотите удалить все товары из корзины?"))
	{
		setCookie("order_counts","");
		setCookie("order_goods","");

		if (( getCookie("user") != null )&&( getCookie("user") != "" )&&( getCookie("user") != "guest" ))
		{clearOdersAjax();}
		document.getElementById('ajorder_count').innerHTML = '0';
	}
}

function doUpdateOrder ()
{
var url = "/cgi-bin/order/ajax.cgi?mode=get_sum_count";
xmlHttp.open ("GET", url, true);
xmlHttp.onreadystatechange = DM_UpdateOrder;
xmlHttp.send(null);
}

function DM_UpdateOrder ()
{if ( xmlHttp.readyState == 4 )
	{if ( xmlHttp.status == 200 )
		{
		var response = xmlHttp.responseText;
		vars = response.split(";");
		//document.getElementById("ajorder_sum").innerHTML = vars[0];
		document.getElementById("ajorder_count").innerHTML = vars[1];
		}
   else if ( xmlHttp.status == 404 )		{	alert("UpdateOrder Request URL does not exist");}
   else if ( xmlHttp.status == 0 )		{ }
   else		{	alert("UpdateOrder Error: status code is " + xmlHttp.status );}
	}
}
/*-----------------------запуск всего после загрузки DOM------------------*/
new DOMReady(function(){
    var usr = getCookie("user");
    if ((usr != null)&&(usr != "")&&(usr != "guest"))
    {
    	document.getElementById("l1").style.display='none';
    	document.getElementById("l2").style.display='block';
    	document.getElementById('user_').innerHTML = usr;
    }
    doUpdateOrder ();
    
    if(typeof(idmenu) != "undefined"){document.getElementById('a'+idmenu).className='f_menu11'};// выделение меню
});

