var get = function(id) { return document.getElementById(id); }

function uVal(id)
{
	try {
		var relchb = get('relnumchb'+id);
		var rel = get('relnum'+id);
		if (relchb.checked)
		{
			if (rel.value == 0)
				rel.value = 1;
		}
		else
			rel.value = 0;
	}
	catch(e) {};
}

function tabSw(t, v) {

	for(i = 0; i < 20; i++) try {
			document.getElementById('tab' + t + 'Sw' + i).className = 'inactive';
	} catch(e) {};
	document.getElementById('tab' + t + 'Sw' + v).className = 'active';

	for(i = 0; i < 20; i++) try {
			document.getElementById('tab' + t + 'var' + i).style.display = 'none';
	} catch(e) {};
	try {
		document.getElementById('tab' + t + 'Sw' + v).className = 'active';
		document.getElementById('tab' + t + 'var' + v).style.display = '';
	} catch(e) {};

}

function cms_buyitem(id, qty, type) {
	/*
	1 - Заказ на доставку
	2 - Заказ на вынос для Кокоса
	2 - Заказ на вынос для Зеленой поляны
	*/
	try {
	ajax.query(id, qty, type, false);
	} catch(e) {};
}

function Ajax() {

	this.query = function(id, qty, type, async) {
		var http = this.http();
		var url = 'id=' + id + '&ac=' + Math.random();

		http.open('POST', '/my/cart/do.add_cart.id.' + id + '.qty.' + qty + '.type.' + type + '.html', async ? false : true);
		http.setRequestHeader('Content-Type', 'application/octet-stream');
		http.onreadystatechange = function() {
		if(http.readyState == 4) {
			try {
				eval('response = ' + http.responseText + ';');
				if(response['method']) eval(response['method'] + '(response)');
				alert('Товар успешно добавлен в корзину');
				location.reload(true);
			} catch (e) {
				alert('Ошибка: ' + e.message + '\n\nDebug:\n'+http.responseText);
			}
		}
	}
		http.send(url);
	}

	this.http = function() {
	    var xmlhttp;
		/*@cc_on
		@if(@_jscript_version >= 5)
			try {
			    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
			    try {
					xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					xmlhttp = false;
				}
			}
		@else
			xmlhttp = false;
		@end @*/
	    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	        try {
	            xmlhttp = new XMLHttpRequest();
	        } catch (e) {
	            xmlhttp = false;
	        }
	    }
	    return xmlhttp;
	}

}

ajax = new Ajax;

function set_content(id, html) {
	document.getElementById(id).innerHTML = html;
}

function auto_set_content(r) {
	set_content(r['div_id'], r['html']);
}

function refresh_cart(r) {
	auto_set_content(r);
	try {
	var buy = get('buy' + r['id']);
	var order = get('order' + r['id']);
	buy.style.display = 'none';
	order.style.display = '';
	} catch(e) {};
}



function hint(title, text) {
	w = 400;
	h = 200;
	var h_window = window.open('', h_window, 'left=' + ((screen.width - w)/2) + ',top=' + ((screen.height - h)/2 - 50) + ',width=' + (w+20) + ',height=' + (h+20));
	h_window.document.write('<title>' + title + '</title><body style="margin:10px;cursor:pointer;" onClick="window.close()">');
	h_window.document.write('<p><b style="font-family:arial;font-size:14	px">' + title + '</b></p><p style="font-family:arial;font-size:12px">' + text + '</p></body>');
	h_window.focus();
}

function PopupWin(link, title)
{
    var win = window.open('','preview','width=50,height=50,left=0,top=0,screenX=0,screenY=0,resizable=no,scrollbar=no,status=no,menubar=no,titlebar=no,location=no,scrolling=no');
    var winDoc = win.document;
    var content = '<html><head><title>' + title + '</title>' + 
    '<style>body{overflow: auto; padding:0; margin:0}img{border:0;}div{padding:5px; margin: 0 5px;}*div{padding:5px; margin: 0 5px;}html>body div {padding:0px 5px; margin: 0 5px;}</style>' + '</head><body><div><a href="javascript:self.close()">' +
    '<img alt="' + title + '" id="image" title="Закрыть" src="/thumb/640x0xNormal/data/files/' + link + '" /></a></div></body></html>';
    win.document.write(content);
    winDoc.body.onload = function() {
			var obj = winDoc.getElementById('image');
			var w = obj.width, h = obj.height;
			
			var iHeight= document.body.clientHeight, iWidth = self.innerWidth;
			var left = (self.opera ? iWidth : screen.availWidth)/2 - w/2;
			var top = (self.opera ? iHeight : screen.availHeight)/2 - h/2;

			win.resizeTo(w+25, h+58);
			
			win.moveTo(left, top);
    }
    win.onload = winDoc.body.onload; // special for Mozilla
    // !!! Important statement: popup onload won't execute without it!
    win.document.close();
    win.focus();
} 

function cmsSwitch() {
	if(event.keyCode == 123) document.location.href = admin_uri;
}

function recalc(tid, pr) {
	var qt = get('qty'+tid);
	
	if (isNaN(qt.value) || (qt.value <= 0))
		qt.value = 1;
	
	var tot = get('total'+tid);
	var num = parseInt(qt.value)*parseInt(pr);
	var output = num.triads(' ', ',');

	tot.innerHTML = output;
}

Number.prototype.triads = function (triadSeparator, decSeparator, fractNum)
{
  if (typeof(fractNum)=='undefined') fractNum = 0;
  var num = parseInt(this).toString();
  var numd = this.toString();

  numd = numd.toString().substr(numd.indexOf('.')+1, fractNum);
  /* while (numd.length<fractNum) numd += '0'; // add 0 after comma */
  
  var regEx = /(-?\d+)(\d{3})/;
  while (regEx.test(num)) { num = num.replace(regEx,"$1"+triadSeparator+"$2"); }
  if (numd != 0)
  { num += decSeparator+numd; }
  return num;
}