var defaultSkin = "newquote";

// массив переменных для URL
var VARS = new Array();
VARS['stock_paper'] = 'explorer?caption=Информация о бумаге&width=580&height=460&url={http://quote-otc.rbc.ru/empassp/instrument/Stock.jsp';
VARS['bond_paper'] = 'explorer?caption=Информация о бумаге&width=580&height=460&url={http://quote-otc.rbc.ru/empassp/instrument/Bond.jsp';
VARS['adr_paper'] = 'explorer?caption=Информация о бумаге&width=580&height=460&url={http://quote-otc.rbc.ru/empassp/instrument/Adr.jsp';
VARS['company_info'] = 'explorer?caption=Поиск компаний&$company_info_size$url=http://quote-otc.rbc.ru/empassp/em_search.jsp';
VARS['company_info_params'] = 'explorer?caption=Поиск компаний&$company_info_size$url={http://quote-otc.rbc.ru/empassp/em_search.jsp';
VARS['balance_info_params'] = 'explorer?caption=Поиск компаний&$company_info_size$url={http://quote-otc.rbc.ru/empassp/Balance.jsp';
VARS['gaap_balance_info_params'] = 'explorer?caption=Поиск компаний&$company_info_size$url={http://quote-otc.rbc.ru/empassp/GAAPBalance.jsp';
VARS['ticker_info_params'] = 'explorer?caption=Поиск компаний&width=700&height=400&url={http://quote-otc.rbc.ru/empassp/instrument/SecurityCard.jsp';
VARS['security_info_params'] = 'explorer?caption=Поиск компаний&$company_info_size$url={http://quote-otc.rbc.ru/empassp/em_search.jsp';
VARS['company_info_withparams'] = 'explorer?caption=Поиск компаний&$company_info_size$url={http://quote-otc.rbc.ru/empassp/';
VARS['company_info_size'] = 'minheight=500&minwidth=750&width=750&height=500&resizeable=no&ie_fontsize=-2&';
VARS['company_search'] = 'explorer?caption=Поиск компаний&left=570&ie_fontsize=-2&minheight=350&minwidth=260&width=260&height=350&url={http://quote-otc.rbc.ru/empassp/em_search.jsp';
VARS['ticker_stock'] = 'explorer?caption=Карточка тикера&left=570&ie_fontsize=-2&minheight=260&minwidth=340&width=340&height=260&url={http://quote-otc.rbc.ru/empassp/instrument/StockGeneral.jsp';
VARS['ticker_bond'] = 'explorer?caption=Карточка тикера&left=570&ie_fontsize=-2&minheight=260&minwidth=340&width=340&height=260&url={http://quote-otc.rbc.ru/empassp/instrument/BondGeneral.jsp';
VARS['ticker_adr'] = 'explorer?caption=Карточка тикера&left=570&ie_fontsize=-2&minheight=260&minwidth=340&width=340&height=260&url={http://quote-otc.rbc.ru/empassp/instrument/AdrGeneral.jsp';
VARS['fundamentials'] = 'explorer?caption=Фундаментальный анализ&left=200&top=200&width=800&height=600&url={http://quote-otc.rbc.ru/table/index.jsp';
VARS['stock_predict'] = 'explorer?caption=Прогнозы и рекомендации профучастников по российским акциям&left=50&top=50&width=800&height=600&url={http://otc.rbc.ru/targets/index.jsp';
//VARS['bond'] = 'explorer?caption=Облигационный терминал&left=200&top=200&width=800&height=600&url={http://quote-otc.rbc.ru/bond/pay/index.jsp';
VARS['bond'] = 'explorer?caption=Облигационный терминал&left=200&top=200&width=800&height=600&url={http://otc.rbc.ru/bond/index.jsp';
//VARS['stock'] = 'explorer?caption=Фондовый терминал&left=200&top=200&width=800&height=600&url={http://quote-otc.rbc.ru/stock/pay/index.jsp';
VARS['stock'] = 'explorer?caption=Фондовый терминал&left=200&top=200&width=800&height=600&url={http://otc.rbc.ru/stock/pay/index.jsp';
//VARS['vecs'] = 'explorer?caption=Вексельный терминал&left=200&top=200&width=800&height=600&url={http://quote-otc.rbc.ru/vecs/pay/index.jsp';
VARS['vecs'] = 'explorer?caption=Вексельный терминал&left=200&top=200&width=800&height=600&url={http://otc.rbc.ru/vecs/index.jsp';
VARS['bond_calc'] = 'explorer?caption=Облигационный калькулятор&resizeable=false&ie_fontsize=-2&minheight=470&minwidth=790&width=790&height=470&top=0&left=0&url={http://otc.rbc.ru/bond_calc/index.jsp';
VARS['pif'] = 'explorer?caption=ПИФы&left=100&top=100&width=1000&height=600&url={http://quote-otc.rbc.ru/pif/index.jsp';
VARS['pif_info'] = 'explorer?caption=Информация о ПИФе&left=100&top=100&width=700&height=500&url={http://quote-otc.rbc.ru/pif/view.jsp';

function openLink(purl, windowType)
{	
		isLightWeight = (windowType == 'modal' || windowType == 'modeless');
		var url = makeTemplate(purl);
		var params = '';
		var resparam = 	getParam(url, 'resizeable');
		if (resparam != '')
			params += 'resizable=no';
		else
			params += 'resizable=yes';

		var scroll = getParam(url, 'scroll');
		if (scroll == '')
			;
		else
		{
			params += ',scrollbars=yes';
		}
		

		var widthparam = getParam(url, 'width');
		params += ((isLightWeight) ? ',dialogWidth=' : ',width=');
		if (widthparam != '')
			params += ((isLightWeight) ? parseInt(widthparam) + 10 : widthparam);
		else
			params += '100';
		params += "px";

		var heightparam = getParam(url, 'height');
		params += ((isLightWeight) ? ',dialogHeight=' : ',height=');
		if (heightparam != '')
			params +=  ((isLightWeight) ? parseInt(heightparam) + 30 : heightparam);
		else
			params += '100';
		params += "px";

		if (windowType == "nocontrol")
			params += ',menubar=no,toolbar=no,location=no,status=no,left=100';
		else
			params += ',menubar=yes,toolbar=yes,location=yes,status=yes';

		var rurl = getParam(url, 'url');

		//alert(rurl);
		//alert(params);
		
		if (rurl.indexOf("?") == -1)
			rurl = rurl + "?web=1";
		else
			rurl = rurl + "&web=1";

		// пока всё бесплатно
		rurl = rurl;// + "&login=rbcanonymous&password=rbcanonymous"
		//alert(rurl);

		switch (windowType){
			case "modeless":
				window.showModelessDialog(rurl, window, params.replace(/=/g, ":").replace(/,/g, ";"));
				break;
			case "modal":
				window.showModalDialog(rurl, window, params.replace(/=/g, ":").replace(/,/g, ";"));
				break;
			case "normal":
				window.open(rurl, "", params);
				break;
			case "nocontrol":
				window.open(rurl, "", params);
				break;
			case "redirect":
				document.location.href = rurl;
		}
}
function showTerminal(terminalId, windowType, style, free){
	if (!windowType)
		windowType = "nocontrol";
		
	if (!style)
		//style = defaultSkin;
		style = '';
		
	var url = "";
	if (terminalId != "stock" && terminalId != "bond" && terminalId != "vecs")
		return;
		
	url = "rbcwin://$" + terminalId + "$?web=1&skin=" + style + (free ? "&login=rbcanonymous&password=rbcanonymous" : "") + "}";
	openLink(url, windowType);
	return false;
}
function showEDBPaper(type, eid, pid, windowType, style)
{
        if (!style)
                style=defaultSkin;
 
        if (!windowType)
                windowType="nocontrol";
 
 
        var url = "rbcwin://$stock_paper$?id=" + pid + "&emit=" + eid + "&skin=" + style + "&folder=tickers}"; 
        if (type == 'bond')
                url = "rbcwin://$bond_paper$?id=" + pid + "&bissue=" + pid + "&emit=" + eid + "&skin=" + style + "&folder=tickers}"; 
        else if (type == 'adr')
                url = "rbcwin://$adr_paper$?id=" + pid + "&emit=" + eid + "&skin=" + style + "&folder=tickers}"; 
 
        openLink(url, windowType);
        return false;
}
function showEDBPaperByTicker(type, ticker, source, windowType, style)
{
	if (!style)
		style=defaultSkin;

	if (!windowType)
		windowType="nocontrol";


	 var url = "rbcwin://$stock_paper$?ticker=" + ticker + "&source=" + source + "&skin=" + style + "&folder=tickers}";

	if (type == 'bond')
        	url = "rbcwin://$bond_paper$?ticker=" + ticker + "&source=" + source + "&skin=" + style + "&folder=tickers}";
	else if (type == 'adr')
        	url = "rbcwin://$adr_paper$?ticker=" + ticker + "&source=" + source + "&skin=" + style + "&folder=tickers}";
		

        openLink(url, windowType);
        return false;
}
function showEDBPapersFolder(eid, part, subfolder, windowType, style)
{
        if (!style)
                style=defaultSkin;
 
        if (!windowType)
                windowType="nocontrol";
 
 
        var url = "rbcwin://$company_info_params$?id=" + eid + "&folder=tickers&paperpart=" + part + "&subfolder=" + subfolder + "&skin=" + style + "&folder=tickers}";
 
        openLink(url, windowType);
        return false;
}
function openPif(windowType, style, id)
{
        if (!style) style="quote";

	var parameters = "menubar=no, status=no, toolbar=no, resizable=yes, location=no, directories=no, height=600, width=1000, top=50, left=50";
	var url;
	if (!id) url = "http://quote-otc.rbc.ru/pif/index.jsp?web=1&skin=" + style;
	else url = "http://quote-otc.rbc.ru/pif/index.jsp?web=1&skin=" + style + "&pifID=" + id;
	window.open(url, "_blank", parameters);

        return false;
}
function openFundamentials(windowType, style)
{
        if (!style)
                style=defaultSkin;

        if (!windowType)
                windowType="nocontrol";

	var url = "rbcwin://$fundamentials$?web=1&skin=" + style + "}";

        openLink(url, windowType);
        return false;
}

function openStockPredict(windowType, style)
{
        if (!style)
                style=defaultSkin;

        if (!windowType)
                windowType="nocontrol";

	var url = "rbcwin://$stock_predict$?skin=" + style + "}";

        openLink(url, windowType);
        return false;
}
function showEDBTicker(ticker, source, subfolder, windowType, style)
{
        if (!style)
                style=defaultSkin;
 
        if (!windowType)
                windowType="nocontrol";
 
 
        var url = "rbcwin://$company_info_params$?ticker=" + ticker + "&exchange=" + source + "&folder=tickers" + "&subfolder=" + subfolder + "&skin=" + style + "}";
 
        openLink(url, windowType);
        return false;
 
}
function showTicker(type, ticker, source, windowType, style)
{
        if (!style)
                style=defaultSkin;

        if (!windowType)
                windowType="nocontrol";

	var url = "";
	if (type == 'stock')
		{url = "rbcwin://$ticker_stock$?ticker=" + ticker + "&exchange=" + source + "&skin=" + style + "}";}
	else if (type == 'bond')
		{url = "rbcwin://$ticker_bond$?ticker=" + ticker + "&exchange=" + source + "&skin=" + style + "}";}
	else if (type == 'adr')
		{url = "rbcwin://$ticker_adr$?ticker=" + ticker + "&exchange=" + source + "&skin=" + style + "}";}
	else
		{return false;}

	openLink(url, windowType);
	return false;
}
function showEDBFolder(otype, oid, windowType, style)
{
 
        if (!style)
                style=defaultSkin;
 
        if (!windowType)
                windowType="nocontrol";
 
        var url="";
        if (otype == "search")
        {
                url = "rbcwin://$company_search$?skin=" + style + "}";
        }
        else
        {
                url = "rbcwin://$company_info_params$?id=" + oid + "&skin=" + style + "&folder=" + otype + "}";
        }
 
        openLink(url, windowType);
        return false;
}
function showBondCalc(windowType, style)
{
        if (!style)
                style=defaultSkin;

        if (!windowType)
                windowType="nocontrol";

        var url="rbcwin://$bond_calc$?skin=" + style + '}';
        
	openLink(url, windowType);
        return false;
}
function makeTemplate(url, stackLen)
{
	var off1 = url.indexOf('$');
	if (off1 == -1)
		return url;

	var off2 = url.indexOf('$', off1 + 1);
	if (off2 == -1)
		return url;

	var key = url.substring(off1 + 1, off2);
	if (VARS[key] && (!stackLen || stackLen < 10))
	{
		var ret = url.substring(0, off1) + makeTemplate(VARS[key], (stackLen ? (stackLen + 1) : 0)) + url.substring(off2 + 1);
		return ret;
	}

	return url;
}
function getParam(url, param)
{
	var off;
	if ((off = url.indexOf(param+"=")) == -1)
		return '';

	var foff = url.indexOf(param+"={")
	var npoff;
	if (foff == off)
	{
		var loff = url.indexOf("}", foff);
		npoff = url.indexOf('&', loff);
	}
	else
	{
		npoff = url.indexOf('&', off);
	}

	var ret;
	if (npoff == -1)
		ret = url.substring(off + param.length + 1);
	else
		ret = url.substring(off + param.length + 1, npoff);

	if (ret.indexOf('{') == 0)
		{ret = ret.substring(1);}

	if (ret.lastIndexOf('}') == ret.length - 1)
		{ret = ret.substring(0, ret.length - 1);}

	return ret;
}

