var sBar = {
	e: null, ii: false,
	hide: function () {if (!this.init ()) return; this.e.style.display = 'none';},
	show: function () {if (!this.init ()) return; this.e.style.display = ''; try {this.e.style.top = document.body.scrollTop + 200;} catch (e) {this.e.style.top = 0;} try {this.e.style.left = document.body.scrollLeft + (document.body.offsetWidth - this.e.offsetWidth) / 2;} catch (e) {this.e.style.left = 0;}},
	init: function () {if (this.ii) return true; ii = true; if (!document.body.appendChild || !document.createElement || !document.createTextNode) return false; this.e = document.body.appendChild (document.createElement ('SPAN')); with (this.e.style) {position = 'absolute'; padding = 5; background = '#990000'; color = 'white'; display = 'none';}; this.e.appendChild (document.createTextNode ('Загрузка...')); this.ii = true; return true;}
}

var request = {
	req: null, onInit: null, onLoad: null, onError: null, responseText: '',
	init: function () {this.req = (window.XMLHttpRequest ? new XMLHttpRequest () : (window.ActiveXObject ? new ActiveXObject ("Microsoft.XMLHTTP") : null)); if (!this.req) return false; var o = this; this.req.onreadystatechange = function () {if (o.req.readyState == 4) if (o.req.status == 200) {o.responseText = o.req.responseText; if (o.onLoad) o.onLoad ();} else if (o.onError) o.onError ();}; return true;},
	sendGet: function (u) {if (!this.init ()) return false; if (this.onInit) this.onInit (); u += (u.indexOf ('?') == -1 ? '?__r=' : '&__r=') + Math.random (); this.req.open ('GET', u, true); this.req.send (null); return true;}
}

function setHomePage (elem) {
    var url = document.location.toString ();
    if (url.indexOf ('?') != -1) url = url.substr (0, url.indexOf ('?'));
    elem.style.behavior='url(#default#homepage)';
    elem.setHomePage(url);
}

function addBookmark() {
    var url = document.location.toString ();
    if (url.indexOf ('?') != -1) url = url.substr (0, url.indexOf ('?'));
    var title = document.title;
    if (window.sidebar)
        window.sidebar.addPanel (title, url, "");
    else if (document.all)
        window.external.AddFavorite (url, title);
    else if(window.opera && window.print)
        return true;
}

function g (e) {
	return document.getElementById (e);
}

var CACHE = {};
request.onInit = function () {sBar.show ();};
request.onError = function () {sBar.hide (); alert ('Не удалось загрузить страницу')};

function setElemContent (url, id) {
	if (CACHE [url]) {
		g (id).innerHTML = CACHE [url]; 
		return true;
	}
	request.onLoad = function () {
		CACHE [url] = request.responseText;
		g (id).innerHTML = CACHE [url];
        updateDivs();
		sBar.hide ();
	}
	if (!request.sendGet (url)) return false;
	return true;
}





function getTabContent (e) {
	var tabsCount = 4;
	var j = 0;
	var tabId = 0;
	try {
		for (var i = 1; i <= tabsCount; i ++) {
			var id = 'complex' + i;
			if (id != e.id) {
				g (id).className = 'tlink' + j;
				j ++;
			} else tabId = i;
		}
		e.className = 'tlink3';
		if (!setElemContent ('?xml&tab=' + tabId, 'complexContent')) return true;
	} catch (e) {
		return true;
	}
	return false;
}

function getFaqContent (id) {
	try {
		var _this = g ('faqLink' + id);
		var i = g ('faqImg' + id);
		if (_this.className == 'jmlinkopened' || _this.className == 'jmlinkmainopened') {
			_this. className = _this.className == 'jmlinkopened' ? 'jmlinkclosed' : 'jmlinkmainclosed';
			if (i) i.src = 'img/plus.gif';
			g ('faqTr' + id).className    = 'jmhidden';
		} else {
			_this. className = _this.className == 'jmlinkclosed' ? 'jmlinkopened' : 'jmlinkmainopened';
			if (i) i.src = 'img/minus.gif';
			g ('faqTr'   + id).className  = 'jmenuvisible';
			if (!setElemContent ('?xml&i=' + id, 'faqTd' + id)) return true;
		}
	} catch (e) {	    
		return true;
	}	
	return false;
}

var shopStateForm = null;

function getState () {
	var tmp = null;
	try {
		tmp = g ('shopStateArea').innerHTML;
		var u = '?xml&orderId=' + g ('shopOrderId').value;
		CACHE [u] = null;
		if (!setElemContent (u, 'shopStateArea')) return true;
	} catch (e) {
		return true;
	}
	shopStateForm = tmp;
	return false;
}

function closeCheck () {
	if (!shopStateForm)
		return true;
	g ('shopStateArea').innerHTML = shopStateForm;	
	return false;
}

function getShopguns (e) {
	try {
		if (!setElemContent ('?xml&cid=' + e.value, 'shopgunsContent')) return true;
		g('shopgunsCurName').innerHTML = e.options [e.options.selectedIndex].innerHTML;
	} catch (e) {
		return true;
	}
	return false;
}

function getShopNamesSelect (e) {
	try {
		if (!setElemContent ('?xml2&cid=' + e.value, 'shopNamesSelect')) return true;
//		alert (g ('shopNamesSelect').innerHTML);
	} catch (e) {
		return true;
	}
	return false;
}