var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
				&& (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
				&& (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

function replace_string(txt,cut_str,paste_str)
{ 
	var f=0;
	var ht='';
	ht = ht + txt;
	f=ht.indexOf(cut_str);
	while (f!=-1)
	{ 
		//цикл для вырезания всех имеющихся подстрок 
		f=ht.indexOf(cut_str);
		if (f>0)
		{
			ht = ht.substr(0,f) + paste_str + ht.substr(f+cut_str.length);
		}
	}
	return ht;
}

function new_escape(s)
{
	// Stupid JS escape() does not quote '+'
	return escape(s).replace(new RegExp('\\+','g'), '%2B');
}

/*
Функция получает сумму в формате xx,xxx.xx
и убирает символ "," из строки
*/
function decodeCurrency(sum)
{
	sum1 = '';
	for (i=0; i<sum.length; i++)
	{
		if (sum.substr(i, 1) != ',')
		{
			sum1 = sum1+sum.substr(i, 1);
		}
	}
	return sum1;	
}

/*
Функция получает сумму и преобразует в формат xx,xxx.xx
*/
function encodeCurrency(sum)
{
	//alert(sum);
	
	sum1 = '';
	// Ищем десятичную точку
	dot_pos = -1;
	for (i=0; i<sum.length; i++)
	{
		if (sum.substr(i, 1) == '.')
		{
			dot_pos = i;
		}
	}
	// Если точка найдена, то отрежем часть строки и будем работать только с целой частью
	if (dot_pos == -1)
	{
		int_sum = sum;
		dec_sum = '00';
	}
	else
	{
		int_sum = sum.substr(0, dot_pos);
		dec_sum = sum.substr(dot_pos+1);
	}
	// Движемся от конца строки к началу
	new_sum = '';
	n=1;
	for (i=int_sum.length-1; i>=0; i--)
	{
		if (n==3 && i>0)
		{
			new_sum = ','+int_sum.substr(i,1)+new_sum;
			n=1;
		}
		else
		{
			new_sum = int_sum.substr(i,1)+new_sum;
			n++;
		}
	}
	
	sum1 = new_sum + '.' + dec_sum;
	return sum1;	
}

/*
Функция форматирует полученную сумму, округляя ее до 2-х знаков после дес. запятой
*/
function formatCurrency (sum)
{
	var x1=Math.round(sum*100)/100;
	var x2=String(x1);
	x1 = Math.floor(x1);
	var s1='';
	for (var x3=1;x3<=x2.length;x3++)
	{
		if (x2.substr(x3,1)=='.')
		{
			s1=s1+x2.substr(x3+1,1);
			if (x2.length-x3>1)
			{
				s1='';
				s1=s1+x2.substr(x3+1,2);
			}
		} 
	}
	if (s1.length == 0)
	{
		//return parseInt(x1)+'.00';
		return encodeCurrency(parseInt(x1)+'.00');
	}
	else if (s1.length == 1)
	{
		//return parseInt(x1)+'.'+s1+'0';
		return encodeCurrency(parseInt(x1)+'.'+s1+'0');
	}
	else
	{
		//return parseInt(x1)+'.'+s1;
		return encodeCurrency(parseInt(x1)+'.'+s1);
	}
}

function AddToBasket(id, path)
{
	qty = document.getElementById('buy_qty_'+id).value;
	PHPtoDIV('shop_basket', path+'includes/shop_addtobasket.php?id='+id+'&qty='+qty);
	return false;
}

function Basket_ChangeQty(id)
{
	var qty;
	qty = document.getElementById('qty_'+id).value;
	
	if ( parseInt(qty) >= 0 )
	{
		old_sum = parseFloat( decodeCurrency(document.getElementById('sum_'+id).innerText) );
		price   = parseFloat( decodeCurrency(document.getElementById('price_'+id).innerText) );
		old_qty = parseInt(old_sum/price);
		new_qty = parseInt(qty);
		new_sum = new_qty * price;
		d_sum   = parseFloat(new_sum) - parseFloat(old_sum);
		
		document.getElementById('sum_'+id).innerText = formatCurrency(new_sum);
		
		old_sum_global = parseFloat( decodeCurrency(document.getElementById('sum_global').innerText) );
		new_sum_global = parseFloat(old_sum_global) + parseFloat(d_sum);

		document.getElementById('sum_global').innerText = formatCurrency(new_sum_global);
		
		old_qty_global = parseInt(document.getElementById('qty_global').innerText);
		new_qty_global = old_qty_global + (new_qty - old_qty);
		document.getElementById('qty_global').innerText = new_qty_global;
	}
	else
	{
		document.getElementById('qty_'+id).value = 0;
	}

	return false;
}

function Basket_UpQty(id)
{
	var qty;
	qty = document.getElementById('qty_'+id).value;
	if ( parseInt(qty) >= 0 )
	{
		old_qty = parseInt( document.getElementById('qty_'+id).value );
		new_qty = old_qty + 1;
		
		price   = parseFloat( decodeCurrency(document.getElementById('price_'+id).innerText) );
		old_sum = old_qty * price;
		new_sum = new_qty * price;
		d_sum   = parseFloat(new_sum) - parseFloat(old_sum);
		
		old_sum_global = parseFloat( decodeCurrency(document.getElementById('sum_global').innerText) );
		new_sum_global = parseFloat(old_sum_global) + parseFloat(d_sum);
		
		old_qty_global = parseInt(document.getElementById('qty_global').innerText);
		new_qty_global = old_qty_global + 1;
		
		document.getElementById('qty_'+id).value        = new_qty;
		document.getElementById('sum_'+id).innerText    = formatCurrency(new_sum);
		document.getElementById('sum_global').innerText = formatCurrency(new_sum_global);
		document.getElementById('qty_global').innerText = new_qty_global;
	}

	return false;
}

function Basket_DownQty(id)
{
	var qty;
	qty = document.getElementById('qty_'+id).value;
	
	if ( parseInt(qty) >= 1 )
	{
		old_qty = parseInt( document.getElementById('qty_'+id).value );
		new_qty = old_qty - 1;
		
		price   = parseFloat( decodeCurrency(document.getElementById('price_'+id).innerText) );
		old_sum = old_qty * price;
		new_sum = new_qty * price;
		d_sum   = parseFloat(old_sum) - parseFloat(new_sum);
		
		old_sum_global = parseFloat( decodeCurrency(document.getElementById('sum_global').innerText) );
		new_sum_global = parseFloat(old_sum_global) - parseFloat(d_sum);
		
		old_qty_global = parseInt(document.getElementById('qty_global').innerText);
		new_qty_global = old_qty_global - 1;
		
		document.getElementById('qty_'+id).value        = new_qty;
		document.getElementById('sum_'+id).innerText    = formatCurrency(new_sum);
		document.getElementById('sum_global').innerText = formatCurrency(new_sum_global);
		document.getElementById('qty_global').innerText = new_qty_global;
	}

	return false;
}

function BuyProduct_UpQty(id)
{
	var qty;
	qty = document.getElementById('buy_qty_'+id).value;
	if ( parseInt(qty) >= 0 )
	{
		document.getElementById('buy_qty_'+id).value = parseInt(qty) + 1;
	}
	
	return false;
}

function BuyProduct_DownQty(id)
{
	var qty;
	qty = document.getElementById('buy_qty_'+id).value;
	if ( parseInt(qty) >= 1 )
	{
		document.getElementById('buy_qty_'+id).value = parseInt(qty) - 1;
	}

	return false;
}

function OrderToPrint(id)
{
	nw = window.open('includes/shop_order_toprint.php?id='+id, 'OrderToPrint', 'toolbar=1,location=0,directories=0,resizable=1,status=1,menubar=1,scrollbars=1,width=700,height=600,top='+eval(screen.height/2-300)+',left='+eval(screen.width/2-350));
	nw.focus();

	return false;
}

function ZoomProductImage(img_src, product_name)
{
	nw = window.open('', 'ZoomProductImageWindow', 'toolbar=0,location=0,directories=0,resizable=0,status=0,menubar=0,scrollbars=0,width=500,height=500,top='+eval(screen.height/2-250)+',left='+eval(screen.width/2-250));
	nw.focus();
	nw.document.write('<html><head><title>Просмотр изображения</title><meta http-equiv="Content-Type" content="text/html; charset=windows-1251"></head><body style="margin:0px; padding:0px;"><style type="text/css"> body, div, td { font-family: "Lucida Grande", "Trebuchet MS", "Bitstream Vera Sans", Verdana, Helvetica, sans-serif; font-size: 15px; color: #000000; font-weight: bold; } a { font-size: 13px; color: #0055CC; font-weight: normal; text-decoration:underline; } a:hover { font-size: 13px; color: #CC0000; font-weight: normal; text-decoration:underline; } </style><br><div align="center">'+product_name+'<br><img src="'+img_src+'" border="0" onClick="window.close();" alt="Щелкните, чтобы закрыть окно" title="Щелкните, чтобы закрыть окно" style="cursor:pointer;"><br><a href="" onClick="window.close();">Закрыть окно</a></div></body></html>');
	nw.focus();

	return false;
}

function ZoomImage(img_src, w, h)
{
	nw = window.open('', 'ZoomImageWindow', 'toolbar=0,location=0,directories=0,resizable=0,status=0,menubar=0,scrollbars=0,width='+w+',height='+h+',top='+eval(screen.height/2-parseInt(h)/2)+',left='+eval(screen.width/2-parseInt(w)/2));
	nw.focus();
	nw.document.write('<html><head><title>Просмотр изображения</title><meta http-equiv="Content-Type" content="text/html; charset=windows-1251"></head><body style="margin:0px; padding:0px;"><style type="text/css"> body, div, td { font-family: "Lucida Grande", "Trebuchet MS", "Bitstream Vera Sans", Verdana, Helvetica, sans-serif; font-size: 15px; color: #000000; font-weight: bold; } a { font-size: 13px; color: #0055CC; font-weight: normal; text-decoration:underline; } a:hover { font-size: 13px; color: #CC0000; font-weight: normal; text-decoration:underline; } </style><br><div align="center"><img src="'+img_src+'" border="0" onClick="window.close();" alt="Щелкните, чтобы закрыть окно" title="Щелкните, чтобы закрыть окно" style="cursor:pointer;"><br><a href="" onClick="window.close();">Закрыть окно</a></div></body></html>');
	nw.focus();

	return false;
}

function ShopOrders_Info(obj, id, path)
{
	// id - order_id
	dd = document.getElementById('items_'+id);
	// Hide or Show order items
	if(obj.open) 
	{
		dd.style.display = 'none';
	}
	else
	{
		PHPtoDIV('order_content_'+id, path+'includes/shop_order_info.php?id='+id);
		dd.style.display = 'block';
	}
	obj.open=!obj.open;
	return false;
}

function ShopProducts_Info(obj, id, path)
{
	// id - product_id
	dd = document.getElementById('productinfo'+id);
	// Hide or Show submenu
	if(obj.open) 
	{
		dd.style.display = 'none';
	}
	else
	{
		PHPtoDIV('productinfo'+id, path+'includes/shop_product_info.php?id='+id);
		dd.style.display = 'block';
	}
	obj.open=!obj.open;
	return false;
}

function ProductsGroup_Processing(obj, id)
{
	// id - pgroup_id
	dd = document.getElementById('products_g'+id);
	// Hide or Show submenu
	if(obj.open) 
	{
		dd.style.display = 'none';
		//document.images['lmenu_img_'+(parseInt(id)-1)].src = img_path+'lmenu_arr_closed.gif';
	}
	else
	{
		PHPtoDIV('products_g'+id, 'includes/pc_cpanel_products_pg.php?id='+id);
		dd.style.display = 'block';
		//document.images['lmenu_img_'+(parseInt(id)-1)].src = img_path+'lmenu_arr_opened.gif';
	}
	obj.open=!obj.open;
	return false;
}

function PHPtoDIV(div_id, php_str)
{
	var AddScr=document.createElement('script');
	
	found = 0;
	i = 0;
	while(ch = php_str.substr(i,1))
	{
		if ( ch == '?' )
		{
			found = 1;
			break;
		}
		i++;
	}
	
	if ( found == 1)
	{
		str = '&';
	}
	else
	{
		str = '?';
	}
	
	AddScr.src=php_str+str+'div_id='+div_id;
	document.body.appendChild(AddScr);
	return false;
}

function TR_Open_Info(obj, id, php_str)
{
	if ( ChangeDisplayTR(obj) )
	{
		PHPtoDIV(id, php_str);
	}
	return false;
}

function ChangeDisplayTR(obj)
{
	if(obj.open) 
	{
		obj.style.display='none';
	}
	else
	{
		if(is_nav)
		{
			obj.style.display='table-row';
		}
		else
		{
			obj.style.display='block';
		}
		
	}
	obj.open=!obj.open;
	return obj.open;
}

function ChangeDisplay(obj)
{

	if(obj.open) 
	{
		obj.style.display='none';
	}
	else
	{
		obj.style.display='block';
	}
	obj.open=!obj.open;
	return false;
	
}

function validate(field)
{
		var valid = "abcdefghijklmnopqrstuvwxyz0123456789_."
		var ok = "yes";
		var temp;
		for (var i=0; i<field.value.length; i++)
		{
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if ( field.value.length == 0 )
		{
			ok = "no";
		}
		if (ok == "no")
		{
			alert("Неверный ввод !");
			field.focus();
			field.select();
			return false;
		}
		return true;
}

function Editor_LoadImage()
{
	document.frm_EditorImage.txt_editor_hidden.value=document.getElementById('txt_editor').value;
	document.frm_EditorImage.BeginLoadImage.value=1;
	
	document.frm_EditorImage.id.value       = document.frm_Editor.id.value;
	
	document.frm_EditorImage.noname1.value  = document.frm_Editor.noname1.value;
	document.frm_EditorImage.noname2.value  = document.frm_Editor.noname2.value;
	document.frm_EditorImage.noname3.value  = document.frm_Editor.noname3.value;
	document.frm_EditorImage.noname4.value  = document.frm_Editor.noname4.value;
	document.frm_EditorImage.noname5.value  = document.frm_Editor.noname5.value;
	document.frm_EditorImage.noname6.value  = document.frm_Editor.noname6.value;
	document.frm_EditorImage.noname7.value  = document.frm_Editor.noname7.value;
	document.frm_EditorImage.noname8.value  = document.frm_Editor.noname8.value;
	document.frm_EditorImage.noname9.value  = document.frm_Editor.noname9.value;
	document.frm_EditorImage.noname10.value = document.frm_Editor.noname10.value;
	
	document.frm_EditorImage.submit();	
}

function Editor_LoadDocument()
{
	document.frm_EditorDocument.txt_editor_hidden.value=document.getElementById('txt_editor').value;
	document.frm_EditorDocument.BeginLoadDocument.value=1;
	
	document.frm_EditorDocument.id.value       = document.frm_Editor.id.value;
	
	document.frm_EditorDocument.noname1.value  = document.frm_Editor.noname1.value;
	document.frm_EditorDocument.noname2.value  = document.frm_Editor.noname2.value;
	document.frm_EditorDocument.noname3.value  = document.frm_Editor.noname3.value;
	document.frm_EditorDocument.noname4.value  = document.frm_Editor.noname4.value;
	document.frm_EditorDocument.noname5.value  = document.frm_Editor.noname5.value;
	document.frm_EditorDocument.noname6.value  = document.frm_Editor.noname6.value;
	document.frm_EditorDocument.noname7.value  = document.frm_Editor.noname7.value;
	document.frm_EditorDocument.noname8.value  = document.frm_Editor.noname8.value;
	document.frm_EditorDocument.noname9.value  = document.frm_Editor.noname9.value;
	document.frm_EditorDocument.noname10.value = document.frm_Editor.noname10.value;
	
	document.frm_EditorDocument.submit();	
}

function MenuClick(menu_id, menu_pos, is_parent, script)
{
	// Сменим рисунок слева от пункта меню
	// Рисунок размещен как background в блоке DIV с некоторым ID,
	// в CSS задан фон для определенноко класса.
	// Меняя имя класса блока DIV, мы, тем самым, сменим рисунок
	// ID блока DIV с рисунком формируется по следующему правилу:
	// <menu_pos>_img_<is_parent>_<1-выбран; 0 - не выбран>
	// Пример: lmenu_img_1_0, lmenu_img_0_1, ...
	class_name = document.getElementById(menu_pos + '_' + menu_id + '_img').className;
	img_status = class_name.substr(class_name.length-1, 1);
	if (img_status == '1')
	{
		img_status = '0';
	}
	else
	{
		img_status = '1';
	}
	document.getElementById(menu_pos + '_' + menu_id + '_img').className = menu_pos + '_img_' + is_parent + '_' + img_status;
	
	// Если это меню содержит подменю, то покажем или скроем блок подменю
	// иначе обработаем переход на определенную станицу
	if ( document.getElementById(menu_pos + '_' + menu_id + '_submenu') )
	{
		ChangeDisplay(document.getElementById(menu_pos + '_' + menu_id + '_submenu'));
	}
	else
	{
		PHPtoDIV('', script);
	}
	
	return false;
}

function TopMenuClick(script)
{
	PHPtoDIV('', script);
	
	return false;
}

function BottomMenuClick(script)
{
	PHPtoDIV('', script);
	
	return false;
}

function BreadcrumbsClick(script)
{
	PHPtoDIV('', script);
	
	return false;
}