﻿var iipUpPortfoliosTimer;
var currentEditBox = undefined;
function IipUpPortfoliosNew() {
    if (jQuery("#newPortfolioName").val() == "")
        alert("Please enter index name");
    else
        //TabsUpdateCurrent(GetControlParameters("TabControlLevel1", "ADD"));
	TabsActivateTab("IipProfilePortfolioDetails","portfolioid=-1");
}

function IipUpPortfoliosDelete() {
    var checkedRows = IipUpGetCheckedRows();
    if (checkedRows.length == 0)
        alert("Please select portfolios to delete");
	else if (confirm('Do you really want to delete selected portfolios?')) {
	    var portfolios = "";
	    checkedRows.each(function(index) {portfolios += jQuery(this).val() + ",";});
	    TabsUpdateCurrent("Action=DELETE&portfolios=" + portfolios);
    }
}

function IipUpPortfoliosMerge() {
    var checkedRows = IipUpGetCheckedRows();
    if (checkedRows.length < 2)
        alert("Please select at least two portfolios to merge");
	else if (confirm('Do you really want to merge selected portfolios?'))
	    TabsUpdateCurrent(GetControlParameters("TabControlLevel1", "MERGE"));
}

function IipUpGetCheckedRows() {
    return jQuery("input._select:checked","#TabControlLevel1");
}

function IIpUpGoToDetails(portfolioID) {
     if (iipUpPortfoliosTimer)
        clearTimeout(iipUpPortfoliosTimer);
        
     if (TabsIsLoading)
        iipUpPortfoliosTimer = setTimeout("IIpUpGoToDetails(" + portfolioID + ")", 300);
     else     
        TabsActivateTab('IipProfilePortfolioDetails', 'portfolioid='+ portfolioID);
}

function IIpUpGoToDetails(portfolioID, addedStocks) {
    if (iipUpPortfoliosTimer)
        clearTimeout(iipUpPortfoliosTimer);

    if (TabsIsLoading)
        iipUpPortfoliosTimer = setTimeout("IIpUpGoToDetails(" + portfolioID + "," + addedStocks + ")", 300);
    else
        TabsActivateTab('IipProfilePortfolioDetails', 'portfolioid=' + portfolioID + "&addedStocks="+addedStocks);
}

function changePage(params) {
    TabsUpdateCurrent(GetControlParameters('iip_user_portfolio_portfolios') + '&p=' + params);
}

function findPosX(obj){
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj){
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}
  


function showEditBox(target, obj){
	if (currentEditBox!=undefined) {
		hideEditBox(currentEditBox);
	}
	var xoffset = findPosX(obj);
	var yoffset = findPosY(obj) + 15;
	var mywidth = 350;
	//var myheight = 130;
	document.getElementById(target).style.width = mywidth + "px";
	//document.getElementById(target).style.height = myheight + "px";
	document.getElementById(target).style.left = xoffset + "px";
	document.getElementById(target).style.top = yoffset + "px";
	document.getElementById(target).style.display = "block";
	currentEditBox = target;
}

function hideEditBox(obj){
	document.getElementById(obj).style.display = "none";
	currentEditBox = undefined;
}	


function miniTabs_onClick(tabbarId, sender, param1,isProfilePage)
{
    if (tabbarId == 'miniTab_Zoom') {
        var pageData = document.getElementById("iip_user_portfolio_grid_page");
        if (pageData != null && pageData != undefined) {
            param1 = param1 + '&p=' + pageData.value;
        }
        iip_updateData("ZoomType", param1, isProfilePage);
	}
	/*
    var elems = sender.parentNode.parentNode.parentNode.getElementsByTagName("TR");
	for (var i =0; i<elems.length; i++){
		elems[i].style.display="block";
	}
	
	sender.parentNode.parentNode.style.display = "none";	
	var	periodLink  = document.getElementById("periodLink");
	periodLink.innerHTML = sender.innerHTML;*/
}

function iip_updateData(changedParamKey, changedParamValue, isProfilePage)
{
    //updateChart("PeriodID=" + changedParamValue);
	updateTable("PeriodID=" + changedParamValue,isProfilePage);
}

function updateChart(params)
{
	IndPmPortfolioUpdateChart(null, params + "&StartDate=");//StartDate should be abolished. It demands of fix of bug in soap generation in flash.
}

function updateTable(params, isProfilePage)
{
	/*UpdateControlEx(
		"iip_user_portfolio_grid",
		"iip_profiles_grid",
		iipHeatmapStaticparams + "&" + params,//BrkGetControlParameters("rve_pm_scorecard", "SaveCategoryWeights"),
		null,
		null,
		3,
		true
		);*/
    if (isProfilePage) {
        UpdateControlEx("iip_you_profiles_details", "iip_user_profile_details",
		 params,
		null,
		null,
		3,
		true
		);
    } else {
    TabsUpdateCurrent(params);
    }
    //iip_user_portfolio_details
    //
}


//+++ balloon
var callbacks = new Array();
var isCellOver = false;

function s(e) //cellOnMouseOverHandler
{
	var tempX, tempY, sender;

	if (navigator.appName=="Microsoft Internet Explorer") { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
		sender = event.srcElement;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
		sender = e.target;
	}  
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	// show the position values in the form named Show
	// in the text fields named MouseX and MouseY

	if (sender.getAttribute("bs"))
	{
		var params = sender.getAttribute("bs");
		bs(sender, params, tempX, tempY);	  
	}
	else
	{
		bs(sender);
	}
	return true;
}
function h(e) //cellOnMouseOutHandler
{
	if (navigator.appName=="Microsoft Internet Explorer")
		bh(event.srcElement);
	else 
		bh(e.target);
}

function bs(sender, data, x, y) //show balloon (mouseover)
{
	while (callbacks.length > 0)
	{
		clearTimeout(callbacks.pop());
	}

	var divInnerHtml = data;
	
	if (divInnerHtml!=undefined && divInnerHtml.length > 0)
	{
		//show_help_text(sender, '<div align="left" nowrap>' + divInnerHtml + '</div>',210);
		bln.Show(sender, '<div align="left" nowrap>' + divInnerHtml + '</div>', x, y);	
	}
	isCellOver = true;
}

function bh(sender) //hide balloon (mouseout)
{
	isCellOver = false;
	callbacks.push(setTimeout("_bh()", 1000));
}

function _bh()
{
	callbacks.pop();
	
	if (callbacks.length == 0) //last invoke
	{
		if (!isCellOver && !bln.IsMouseOver()) bln.Hide(); //hide_help();  
		else
		{
			callbacks.push(setTimeout("_bh()", 1000));
		}
	}
}

var IipYouUpLaunchIndexDialog;
var IipYouUpHideIndexDialog;
function IipYouUpGetElementValue(name) {
    var myelem = document.getElementById(name);
    return myelem.value != "";
}
function IipYouUpLaunchIndexClick() {
    if (!IipYouUpGetElementValue('CreatorCompany')) {
        alert('Please enter your company name');
        return;
    }
    if (!IipYouUpGetElementValue('CreatorName')) {
        alert('Please enter your name');
        return;
    }
    if (!IipYouUpGetElementValue('CreatorAddress')) {
        alert('Please enter your address');
        return;
    }
    if (!IipYouUpGetElementValue('CreatorPhone')) {
        alert('Please enter your phone number');
        return;
    }
    if (!IsPhone(document.getElementById('CreatorPhone').value)) {
        alert('Please enter correct phone number');
        return;
    }
    if (!IipYouUpGetElementValue('CreatorEmail')) {
        alert('Please enter your email');
        return;
    }
    if (!IsEmail(document.getElementById('CreatorEmail').value)) {
        alert('Please enter correct email');
        return;
    }
    var params = GetControlParameters('iip_user_portfolio_details');
    TabsUpdateCurrent(params + '&Action=LAUNCH',true, IipYouUpLaunchIndex);
    //UpdateControlEx('iip_user_portfolio_details', 'LaunchIndexResultArea', params + '&Action=LAUNCH', IipYouUpLaunchIndex, null, null)
}

function IipYouUpHideIndexClick() {
    TabsUpdateCurrent(params + '&Action=HIDEINDEX', true, IipYouUpHideIndex);
    //UpdateControlEx('iip_user_portfolio_details', 'LaunchIndexResultArea', params + '&Action=LAUNCH', IipYouUpLaunchIndex, null, null)
}
function IipYouUpLaunchIndex() {
    var myDiv = document.getElementById("launchIndexArea");
    if (IipYouUpLaunchIndexDialog != undefined) {
        IipYouUpLaunchIndexDialog = null;
    }
    IipYouUpLaunchIndexDialog = new YAHOO.widget.Panel(myDiv, { width: 350, fixedcenter: true, visible: false, constraintoviewport: false, modal: true, zIndex: 1000 });
    IipYouUpLaunchIndexDialog.render();
    IipYouUpLaunchIndexDialog.header.innerHTML = "Launch index";
    
    myDiv.style.display = "block";
    IipYouUpLaunchIndexDialog.show();
    /*document.getElementById('spnLaunch').style.display = 'none';
    document.getElementById('spnAprowal').style.display = 'block' ;*/
}

function IipYouUpHideIndex() {
    var myDiv = document.getElementById("hideIndexArea");
    if (IipYouUpHideIndexDialog == undefined) {
        IipYouUpHideIndexDialog = new YAHOO.widget.Panel(myDiv, { width: 350, fixedcenter: true, visible: false, constraintoviewport: false, modal: true, zIndex: 1000 });
        IipYouUpHideIndexDialog.render();
        IipYouUpHideIndexDialog.header.innerHTML = "Hide index";
    }
    myDiv.style.display = "block";
    IipYouUpHideIndexDialog.show();
    /*document.getElementById('spnLaunch').style.display = 'none';
    document.getElementById('spnAprowal').style.display = 'block' ;*/
}


function checkLaunchIndexPossible(isMyIndex) {
    document.getElementById('spnLaunch').style.display = isMyIndex ? 'none' : 'block';
}


function IIPYouMoveIndexIntoYou(constructorID, areaId, portfolioId, formulaId) {
    var params = "&ConstructorID=" + constructorID + "&Type=SAVE&AreaID=" + areaId + "&PortfolioID=" + portfolioId + "&FormulaID=" + formulaId;
    if (areaId == 1) {
        UpdateControlEx("iip_you_portfolio_service", "IipYouServerAnswers", params, IIPYouMoveIndexIntoYouCallBack, 3);
    } else {
        UpdateControlEx("iip_you_portfolio_service", "IipYouServerAnswers", params, IIPYouMoveIndexIntoNotYouCallBack, 3);
    }
}

function IIPYouMoveIndexIntoYouCallBack() {
    alert("Index successfully added in your portfolio (YOU section)");
    document.getElementById("divAddToYou").innerHTML = "";
    window.location = "/iip_you_portfolio.aspx";
}

function IIPYouMoveIndexIntoNotYouCallBack() {
    alert("Index successfully added in your portfolio (NOT YOU section)");
    document.getElementById("divAddToYou").innerHTML = "";
    window.location = "/iip_you_portfolio.aspx";
}

function IipYouIndexProfilesShowAwaitedIndicies() {
    ShowDialog('iip_you_profiles_awaited_list', "", null, null, 350, "List of awaited indicies");
}
function IipYouIndexProfilesLaunchIndex(portfolioid) {
    UpdateControlEx('iip_you_profiles_details', 'iip_user_profile_details', 'Action=LAUNCHINDEX&portfolioid=' + portfolioid, null, null, null)
}

function IipYouUPScrollPageToTop() {
    if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        document.body.scrollTop = 0;
        
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        document.documentElement.scrollTop = 0;
    }
}
//--- Balloon

function IipUpSearchIndexes() {
    var searchValue = document.getElementById("inpIndexSearch").value;
    UpdateControlEx('iip_user_potrtfolio_import_stoks', 'IipYouImportIndex', 'SearchValue=' + searchValue, null, null, null)
}


function IipUpImportStocks() {
    var indexID = document.getElementById("sctImportIndex").value;
    var importType = document.getElementById("rdAddIndex").value;
    UpdateControlEx("iip_user_portfolio_details", "iip_user_portfolio_details", 'Action=IMPORT&IndexID=' + indexID + "&importType=" + importType, null, null, null);
}

function IipUPShowAverageMarketCapInfo() {
    var elem = document.getElementById("IipUPAverageMarketCapInfo");
    if (elem != null) {
        elem.style.display = "block";
    }

}

function IipUPHideAverageMarketCapInfo() {
    var elem = document.getElementById("IipUPAverageMarketCapInfo");
    if (elem != null) {
        elem.style.display = "none";
    }

}
function IipUPShowLastPriceInfo() {
    var elem = document.getElementById("IipUPLastPriceInfo");
    if (elem != null) {
        elem.style.display = "block";
    }

}

function IipUPHideLastPriceCapInfo() {
    var elem = document.getElementById("IipUPLastPriceInfo");
    if (elem != null) {
        elem.style.display = "none";
    }

}