var myBaseURL = '' ;
var BaseURL = '';
var bigChartSize = false;

var MainQuoteSimbol = '';
var QuoteBoxSimbol = '';



function Update( myLocation, urlContent, preLoader)
{

	this.d = document.getElementById( myLocation );
	if ( !this.d ) {
		return ( false );
	}

	var contentString = new String;
	var contentHeight = GetObjectHeight( myLocation );

	if ( preLoader )
	{
		this.d.innerHTML = '<div id="loading" style="height: ' + contentHeight + 'px;"><br>Loading...</div>';
	}
	contentString = urlContent.indexOf( "?" ) > 0 ? GetFileContent( urlContent + "&anticache=" + Math.random() ) : GetFileContent( urlContent + "?anticache=" + Math.random() );

	this.d.innerHTML   = contentString;
	return ( true );
 }

 function UpdateQuoteData()
 {
	var contentString = new String;
	var urlContent = myBaseURL + "";
	contentString = urlContent.indexOf( "?" ) > 0 ? GetFileContent( urlContent + "&anticache=" + Math.random() ) : GetFileContent( urlContent + "?anticache=" + Math.random() );

	contentString;
	return ( true );

 }
 

 // Rollover for QuoteBox Menu

 function actionQIMenu(theMenuItem)
 {
	var i;
	var menuItems = new Array();
	var actions = new Array();

	
	menuItems[1] = document.getElementById('qi_quote');
	//menuItems[2] = document.getElementById('qi_mesaje');
	menuItems[2] = document.getElementById('qi_index');
	menuItems[3] = document.getElementById('qi_cont');

	actions[1] = "quickquote";
	//actions[2] = "quickmsg";
	actions[2] = "quickindex";
	actions[3] = "quickcont";


	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";

		Update( 'qi_content'  , myBaseURL+'quick_info_actions?action='+actions[i], false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}
 }

function GetBoxQuote(myQuote)
{
	Update( 'qi_content'  ,myBaseURL+'quick_info_actions?action=quickquote&simbol='+myQuote, true );
}

function GetBoxIndex(myRegion)
{
	Update( 'qi_content'  ,myBaseURL+'quick_info_actions?action=quickindex&region='+myRegion, true );
}


// Rollover for portofolii

function actionPortofoliiMenu(theMenuItem)
 {
	var i;
	var menuItems = new Array();
	var menuAccount = new Array();
	
	menuItems[1] = document.getElementById('p_act');
	menuItems[2] = document.getElementById('p_der');

	menuAccount[1] = "portofoliu?action=currency";
	menuAccount[2] = "portofoliu_bmfms?action=currency";

	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";
		Update( 'p_container'  ,myBaseURL+menuAccount[i], false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}
 }

 function actionConfirmariMenu(theMenuItem)
 {
	var i;
	var menuItems = new Array();
	var menuAccount = new Array();

	menuItems[1] = document.getElementById('p_act');
	menuItems[2] = document.getElementById('p_der');

	menuAccount[1] = "confirmari";
	menuAccount[2] = "confirmari_bmfms";

	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";
		//alert ( myBaseURL+menuAccount[i] )
		Update( 'cf_container'  ,myBaseURL+menuAccount[i], false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}
 }


function actionOrderDetailsMenu(theMenuItem, orderID )
{
	var i;
	var menuItems = new Array();
	var menuAction = new Array();

	
	menuItems[1] = document.getElementById('od_history');
	menuItems[2] = document.getElementById('od_modify');
	menuItems[3] = document.getElementById('od_cancel');

	menuAction[1] = 'orders_history/' + orderID + '/?orders_history_action=ajax';
	menuAction[2] = 'orders_modify/'+ orderID + '/?orders_modify_action=ajax';
	menuAction[3] = 'orders_cancel/' + orderID + '/?orders_cancel_action=ajax';

	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";
		
		Update( 'p_container'  ,myBaseURL+menuAction[i], false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}

}

 function actionQuoteMenu(theMenuItem)
 {
	var i;
	var menuItems = new Array();
	var menuQuote = new Array();
	// use a hidden field with symbol value - qs

	mySymbol = document.getElementById('qs').value;

	menuItems[1] = document.getElementById('qm_quote');
	menuItems[2] = document.getElementById('qm_charts');
	menuItems[6] = document.getElementById('qm_trades');

	menuQuote[1] = "quote?action=quote&simbol="+mySymbol;
	menuQuote[2] = "quote?action=charts&simbol="+mySymbol;
	menuQuote[6] = "quote?action=transactions&simbol="+mySymbol;

	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";

		Update( 'q_container'  ,myBaseURL+menuQuote[i], false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}
 }

  function actionFuturesMenu(theMenuItem)
 {
	var i;
	var menuItems = new Array();
	var menuQuote = new Array();

	menuItems[1] = document.getElementById('f_market');
	menuItems[2] = document.getElementById('f_quote');
	menuItems[3] = document.getElementById('f_trades');
	
	menuQuote[1] = "cotatii_futures?cotatii_futures_action=market&simbol=";
	menuQuote[2] = "cotatii_futures?cotatii_futures_action=quote&simbol=";
	menuQuote[3] = "cotatii_futures?cotatii_futures_action=trades&simbol=";
	
	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";
		Update( 'f_container'  ,myBaseURL+menuQuote[i], false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}
 }
 
function GetPortfolio(myCurrency)
{
	Update( 'p_container'  ,BaseURL+'portofoliu?action=currency&mycurrency='+myCurrency, true );
}

function ChangeList (myListID)
{
	Update ( 'lst_container', BaseURL+'lists?action=change_list&lid='+myListID, true );
}

function AddNewList (myListName)
{
	Update( 'lst_container', BaseURL+'lists?action=add_list&ln='+myListName, true );

	var listIDObj = document.getElementById( 'resultListID' );
	if ( listIDObj )
	{
		UpdateComboLists( myListName, listIDObj.value );
	}

}

function AddNewSymbolToList (mySymbol,myListID)
{
	Update( 'lst_container', BaseURL+'lists?action=add&sm='+mySymbol+"&lid="+myListID, true );
}

function UpdateComboLists(myListName, myListID)
{
	var Sel = document.getElementById('combolists');
	var OptNew = document.createElement('option');
	OptNew.text = myListName;
    OptNew.value = myListID;
    
	try {
		Sel.add(OptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		Sel.add(OptNew); // IE only
	}
	
	// set the selected index
	Sel.selectedIndex = Sel.length -1 ;

}

function UpdateMainChart(  )
{
	var chartSimbol		= document.getElementById ('MainChartSimbol').value;
	var chartTimeRange	= document.getElementById ('TimeRange').value;
	var chartType		= document.getElementById ('ChartType').value;
	var chartBand		= document.getElementById ('Band').value;
	var chartAvgType1	= document.getElementById ('avgType1').value;
	var chartMovAvg1	= document.getElementById ('movAvg1').value;
	var chartIndicator1	= document.getElementById ('Indicator1').value;

	theParams = 'simbol='+chartSimbol+'&TimeRange='+chartTimeRange;
	theParams += '&ChartType='+chartType+'&action2=update';
	theParams += '&Band='+chartBand+'&avgType1='+chartAvgType1+'&movAvg1='+chartMovAvg1;
	theParams += '&Indicator1='+chartIndicator1;
	if (bigChartSize)
	{
		theParams += '&ChartSize=H';		
	} else {
		theParams += '&ChartSize=M';
	}
	
	if (document.getElementById ('Volume').checked == true)
	{
		theParams += '&Volume=on';
	}

	if (bigChartSize)
	{
		var theContent = "<img src=\""+myBaseURL+"chart_extended?"+theParams+"\" alt=\"grafic\"/>"
	} else {
		var theContent = "<a href=\"javascript:void();\" onclick=\"OpenGraph('"+myBaseURL+"grafic_bvb/"+chartSimbol+"/charts/');\"><img src=\""+myBaseURL+"chart_extended?"+theParams+"\" alt=\"grafic\" title=\"click pentru zoom\" border=\"0\" /></a>"	
	}

	var d = document.getElementById( 'mChart_container' );
	

	d.innerHTML   = theContent;

	return ( false );
}



function ChangeMarket (myMarketID)
{
	var i;
	var arrayMarket = new Array();
	
	arrayMarket[1] = 'linkID_REGS';
	arrayMarket[2] = 'linkID_RGBS';
	arrayMarket[3] = 'linkID_UNLS';
	
	var linkID = "linkID_"+myMarketID;
	
	var mySortField = document.getElementById('sfld').value;
	var myOrder = document.getElementById('ord').value;
	
	newStatisticiUrl = myBaseURL+'market_statistici?action=change_market&market_id='+myMarketID;
	newTopUrl = myBaseURL+'market_top?action2=change_market&market_id='+myMarketID+'&sort_field='+mySortField+'&order='+myOrder;
	
	Update ( 'market_container', newStatisticiUrl, true );
	Update ( 'top_container', newTopUrl, true );
		
	for (i in arrayMarket)
	{
	 if ( arrayMarket[i] == linkID)
	 {
		document.getElementById(arrayMarket[i]).style.color = '#000000';
		
	 } else {
		document.getElementById(arrayMarket[i]).style.color = '#234885;';
	 }
	
	}
}

function ShowOrderDetails (divID)
{
	myDiv = 'order_list_order_info_' + divID;
	
	openDivID = document.getElementById('openOrderDiv').value;
	//if we have an open div close the previous
	if (openDivID != '0' )
	{
		openDiv = 'order_list_order_info_' + openDivID;
		
		document.getElementById (openDiv).style.display  = 'none';	
	}

	document.getElementById (myDiv).style.display  = 'block';
	Update ( myDiv, myBaseURL+'orders_history/' + divID + '/?orders_history_action=show', true );
	document.getElementById('openOrderDiv').value = divID;
	

}

function ShowOrderBmfmsDetails (divID)
{
	myDiv = 'order_list_order_info_' + divID;
	
	openDivID = document.getElementById('openOrderDiv').value;
	//if we have an open div close the previous
	if (openDivID != '0' )
	{
		openDiv = 'order_list_order_info_' + openDivID;
		
		document.getElementById (openDiv).style.display  = 'none';	
	}

	document.getElementById (myDiv).style.display  = 'block';
	Update ( myDiv, myBaseURL+'orders_bmfms_history/' + divID + '/?orders_bmfms_history_action=show', true );
	document.getElementById('openOrderDiv').value = divID;
	

}

function ToggleQsGraph(theMenu, theSimbol, theSimbolID, TimeRange )
{
 	var menuItems = new Array();
	var labelItems = new Array();

	var graphElement = document.getElementById('qs_graph_img');
	
	menuItems[1] = document.getElementById('qs_graph_day');
	menuItems[2] = document.getElementById('qs_graph_week');
	menuItems[3] = document.getElementById('qs_graph_month');
	menuItems[4] = document.getElementById('qs_graph_6month');
	menuItems[5] = document.getElementById('qs_graph_year');
	
	labelItems[1] = 'ultimele 24 ore';
	labelItems[2] = 'ultimele 7 zile';
	labelItems[3] = 'ultimele 30 zile';
	labelItems[4] = 'ultimele 6 luni';
	labelItems[5] = 'ultimele 12 luni';
	

	theMenuItem = document.getElementById (theMenu);

	
	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].className ="selected";
		graphElement.src = myBaseURL+'chart_regularquote/?id='+theSimbolID+'&timeRange='+TimeRange;
		graphElement.alt = 'grafic';
		graphElement.title = 'Evolutie '+ theSimbol +' in '+labelItems[i];		
	 } else {
		menuItems[i].className ="";
	 }
	}
	
 }
 
function ChangeFinancials (myFinID, simbol, period1, period2, period3, period4)
{
	var i;
	var arrayFin = new Array();
	
	arrayFin[1] = 'linkIDBilant';
	arrayFin[2] = 'linkIDCPP';
	arrayFin[3] = 'linkIDBVC';
	
	var linkID = "linkID"+myFinID;
	var newURL = '';
	
	newURL = BaseURL+'financials/' + simbol + '/?action=change_financials&sablon='+myFinID;
	newURL += '&period1='+period1+'&period2='+period2+'&period3='+period3+'&period4='+period4;
	
	Update ( 'financials_container', newURL , true );
}

function ChangeQuoteFinancials (myFinID, simbol, period1, period2, period3, period4)
{
	var i;
	var arrayFin = new Array();
	
	arrayFin[1] = 'linkIDBilant';
	arrayFin[2] = 'linkIDCPP';
	arrayFin[3] = 'linkIDBVC';
	
	var linkID = "linkID"+myFinID;
	var newURL = '';
	
	newURL = myBaseURL+'cotatii/' + simbol + '/?action=profile&sablon='+myFinID;
	newURL += '&period1='+period1+'&period2='+period2+'&period3='+period3+'&period4='+period4;
	
	Update ( 'financials_container', newURL , true );
}

function ChangeIndicatori (simbol, period1, period2, period3, period4)
{
	var i;
	var newURL = '';
	
	newURL = BaseURL+'indicatori_financials/' + simbol + '/?action=change_indicatori';
	newURL += '&period1='+period1+'&period2='+period2+'&period3='+period3+'&period4='+period4;
	
	
	Update ( 'indicatori_financials_container', newURL , true );
}

function RefreshFuturesPiataPaliere ( simbol, settleDate )
{
	newURL = BaseURL+'futures_piata_paliere/piata_paliere/?simbol_bmfms=' + simbol + '&settleDate='+settleDate;
	Update ( 'futures_piata_paliere', newURL , true );
}

function RefreshTopQuote ( simbol )
{
	newURL = myBaseURL+'cotatii/' + simbol + '/refresh/';
	Update ( 'top_quote_container', newURL , true );
}


function RefreshLists ( listID )
{
	newURL = BaseURL+'liste_proprii/?action=refresh&id=' + listID ;
	Update ( 'liste_proprii', newURL , true );
}

function RefreshProvider( action, simbol )
{
	switch( action )
	{
		case 'Q':
			if ( simbol  == MainQuoteSimbol )
			{
				RefreshTopQuote ( simbol );	
			}
			break;
		default:
			break;
	}
}


function actionMktOverviewMenu(theMenuItem)
{
	var i;
	var menuItems = new Array();
	var menuQuote = new Array();

	myMarketID = document.getElementById('mkt').value;
	
	menuItems[1] = document.getElementById('m_gain');
	menuItems[2] = document.getElementById('m_lose');
	menuItems[3] = document.getElementById('m_activ');
	
	menuQuote[1] = "market_top?action2=change_market&market_id="+myMarketID+"&sort_field=evolutie %&order=1";
	menuQuote[2] = "market_top?action2=change_market&market_id="+myMarketID+"&sort_field=evolutie %&order=0";
	menuQuote[3] = "market_top?action2=change_market&market_id="+myMarketID+"&sort_field=volum&order=1";

	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";
		//Update( 'top_container'  , BaseURL+'market_top?action2=change_market&market_id='+myMarketID, true, false );
		Update( 'top_container'  , BaseURL+menuQuote[i], true, false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}
	
 }
 
function RefreshPiataPaliere ( theSimbol)
{
	newURL = myBaseURL+'cotatii/'+theSimbol+'/quote/piata_paliere/?action=refresh';
	Update ( 'piata_paliere', newURL , true );
}

function actionOrdinNouMenu ( theMenuItem )
{
	var i;
	var menuItems = new Array();
	var menuAction = new Array();
	
	menuItems[1] = document.getElementById('on_act');
	menuItems[2] = document.getElementById('on_der');

	menuAction[1] = "ordin_bvb/?orders_add_action=add";
	menuAction[2] = "ordin_bmfms/?orders_bmfms_add_action=add";

	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";
		Update( 'o_action'  , myBaseURL+menuAction[i], true, false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}
}

function actionOrderFond ( fondID, operation )
{
	action = "fond_new_order/?action=add&";
	
	Update( 'fond_order_container'  , myBaseURL+action+"fond_id="+fondID+"&operation="+operation, true, false );	

}

function ToggleStopLossOrder ( myOperation, mySimbol )
{


	if ( myOperation != 'stop_gain' )
	{
		Update( 'o_action'  , myBaseURL+'ordin_bvb/?orders_add_action=add&simbol='+mySimbol, true, false );
	} else {
		Update( 'o_action'  , myBaseURL+'ordin_stoploss/?orders_stoploss_add_action=add&simbol='+mySimbol, true, false );
	}
}

function actionOrderBmfmsDetailsMenu(theMenuItem, orderID )
{
	var i;
	var menuItems = new Array();
	var menuAction = new Array();

	
	menuItems[1] = document.getElementById('od_history');
	menuItems[2] = document.getElementById('od_modify');
	menuItems[3] = document.getElementById('od_cancel');

	menuAction[1] = 'orders_bmfms_history/' + orderID + '/?orders_bmfms_history_action=ajax';
	menuAction[2] = 'orders_bmfms_modify/'+ orderID + '/?orders_bmfms_modify_action=ajax';
	menuAction[3] = 'orders_bmfms_cancel/' + orderID + '/?orders_bmfms_cancel_action=ajax';

	for (i in menuItems)
	{
	 if ( menuItems[i] == theMenuItem )
	 {
		menuItems[i].firstChild.className ="selected";
		
		Update( 'p_container'  ,myBaseURL+menuAction[i], false );
	 } else {
		menuItems[i].firstChild.className ="";
	 }
	}

}

function GetMajorariCapital(myCurrency)
{
	Update( 'mc_container'  ,BaseURL+'majorari_capital?accept=accept&action_m=currency&mycurrency='+myCurrency, true );
}

function TriggerType( value, language )
{
	triggerArea = document.getElementById('order_triggers');

	switch (value)
	{
		case "stop":
			  triggerArea.style.visibility="visible";
		break;
		case "if_touch":
			  triggerArea.style.visibility="visible";
		break;
		case "":
			  triggerArea.style.visibility="hidden";
		break;
		default:
			  triggerArea.style.visibility="hidden";
		break;
	}
}

function updateTrFinanciarObsField( selIndex )
{
    if( selIndex == 2 ) // adica transfer in ifond monetar
    {
        $('#observatii').attr( {'value':'Transfer in cont colector RO90RNCB0072114689630002 Ifond Monetar', 'readonly':'true'} );
        $('#extraNotice').text( 'Pentru a finaliza investitia in iFond Monetar apasati butonul ADAUGA de mai jos si urmati linkul si instructiunile ce vor fi afisate dupa apasarea acestuia.' )
    }else
    {
        $('#observatii').removeAttr("readonly");
        $('#observatii').attr( 'value', '' );        
        $('#extraNotice').text( '' );
    }
}