
var isVSMVersion=false;
function OnloadFunctions()
{    
    OnloadChangeTab();
    RetainDDs();    
    isVSMVersion=(document.getElementById('Version').value == "vsm");
    if(!isVSMVersion)
    {
        document.getElementById('advance').style.display = document.getElementById('FilterHdn').value == "block" ? "" : document.getElementById('FilterHdn').value;    
        document.getElementById('submitDiv').style.display = document.getElementById('FilterHdn').value == "block" ? "" : document.getElementById('FilterHdn').value;
    }
    if(document.location.href.toUpperCase().indexOf('PRICETABLE.ASPX') > 0 && $('ProductCodeHdn').value == 'ALL')
    {
        var elements = document.forms[0].CBSelectAll;
	    if(elements)
	    {
	        for (var i = 0; i < elements.length; i++)
	        {
	            elements[i].disabled = true;
	        }
        }
    }
}

function OnloadChangeTab()
{
    ChangeTabs('this.form', $('SelectedTab').value);
    if(document.location.href.toUpperCase().indexOf('SHORTLIST.ASPX')>0)
	{
	    isVSMVersion=(document.getElementById('Version').value == "vsm");
	    var elements = document.forms[0].CBSelectAll;
	    if(elements)
	    {
	        for (var i = 0; i < elements.length; i++)
	        {
	            elements[i].checked = true;
	        }
	        SelectAll(elements[0].id);
        }
	}
}
function SetVSMVersion()
{
    isVSMVersion=(document.getElementById('Version').value == "vsm");
}
function LoadData()
{
    //The below function is here just to maintain the selected filter's values while loading
    SetSelectedValue();
    //$("ShortListed").value = '';
    document.forms[0].action = GetURL();//url;
    document.forms[0].submit();
    OnloadFunctions();
}

function DropDownChanged()
{
    document.forms[0].submit();
    OnloadFunctions();
}

function DoUnderline(Obj,Setting)
{
    if(Setting) 
    {
        Obj.style.textDecoration='underline'; 
    }
    else    
    {
        Obj.style.textDecoration='none'; 
    }
}

function RedirectToPriceTable()
{    
    document.forms[0].action = GetURL();
    document.forms[0].submit();
}

function ViewShortList()
{
    if($("ShortListed").value != '')
    {
        $('PageIndex').value = 1;
        document.forms[0].method = 'post';
        document.forms[0].action = 'ShortList.aspx'+(isVSMVersion?"?Version=VSM":"");
        document.forms[0].submit();
    }
}

function AddMoreFunds()
{
    $('PageIndex').value = 1;
    document.forms[0].method = 'post';
    document.forms[0].action = 'PriceTable.aspx'+(isVSMVersion?"?Version=VSM":"");    
    document.forms[0].submit();
}

function LoadCSVExtractPage()
{
    AddToShortList(true);
    if($("ShortListed").value != '')
    {
        document.forms[0].method = 'post';
        document.forms[0].action = 'DataExtract.aspx'+(isVSMVersion?"?Version=VSM":"");
        document.forms[0].submit();
    }
}

function GetURL()
{
    var url = '';
    if(document.location.href.toUpperCase().indexOf('PRICETABLE.ASPX')>0)
	{
	    url = 'PriceTable.aspx?ProductType='+$('ProductType').value+'&CurrentLegacy='+$('CurrentLegacy').value+'&ProductCode='+$('ProductCode').value;
        url += '&ProductSeries='+$('ProductSeries').value+'&Sector='+$('Sector').value+'&Performance='+$('Performance').value+'&SPRating='+$('SPRating').value;
        url += '&CrownRating='+$('CrownRating').value+'&FundAge='+$('FundAge').value+'&CityRating='+$('CityRating').value+'&OBSRRating='+$('OBSRRating').value;
        url += '&PageIndex='+$('PageIndex').value;
	}
	else
	{
	    url = 'ShortList.aspx';
	}    
    return url;
}

function ProductTypeChange()
{
    $('CurrentLegacy').value = 'ALL';
    $('ProductCode').value = 'ALL';
    $('ProductSeries').value = 'ALL';
    $('Sector').value = 'ALL';
    $('CityRating').value = 'ALL';
    $('FundAge').value = 'ALL';
    $('CrownRating').value = 'ALL';
    $('Performance').value = 'ALL';
    $('SPRating').value = 'ALL';
    $('OBSRRating').value = 'ALL';
    $("ShortListed").value = '';
    $("AllFundsList").value = '';
    $('PageIndex').value = 1;
    RedirectToPriceTable();
}

function ProductCodeChange()
{
    $('CurrentLegacy').value = $('CurrentLegacyHdn').value;
    $('ProductSeries').value = 'ALL';
    $('Sector').value = 'ALL';
    $('CityRating').value = 'ALL';
    $('FundAge').value = 'ALL';
    $('CrownRating').value = 'ALL';
    $('Performance').value = 'ALL';
    $('SPRating').value = 'ALL';
    $('OBSRRating').value = 'ALL';
    $("ShortListed").value = '';
    RedirectToPriceTable();
}

function CurrentLegacyChange()
{
    $('ProductCode').value = $('ProductCodeHdn').value;
    $('ProductSeries').value = 'ALL';
    $('Sector').value = 'ALL';
    $('CityRating').value = 'ALL';
    $('FundAge').value = 'ALL';
    $('CrownRating').value = 'ALL';
    $('Performance').value = 'ALL';
    $('SPRating').value = 'ALL';
    $('OBSRRating').value = 'ALL';
    $("ShortListed").value = '';
//    document.forms[0].action = 'PriceTable.aspx';
//    document.forms[0].submit();
    RedirectToPriceTable();
}

function Sort(sortColumn,sortDirection)
{
    sortDirection = (sortDirection=='asc')?'desc':'asc';
    $('SortColumn').value = sortColumn;
    $('SortDirection').value = sortDirection;
    RedirectToPriceTable();
}

function OpenDisclaimer()
{
    window.open("Disclaimer.aspx","","menubar=0,resizable=0,width=750,height=400");
}

function OpenChart(params)
{
    document.forms[0].method = 'post';
    document.forms[0].target="_blank";    
    document.forms[0].action = 'Chart.aspx?'+params+(isVSMVersion?"&Version=VSM":"");
    document.forms[0].submit();
    document.forms[0].target = ""; 
}

function OpenWindow(link)
{
    window.open(link);
}

function SetPageIndex(pageNo)
{
    $('PageIndex').value = pageNo;
    RedirectToPriceTable();
}

function RetainDDs()
{
    $('ProductType').value = $('ProductTypeHdn').value;
    $('CurrentLegacy').value = $('CurrentLegacyHdn').value;
    $('ProductCode').value = $('ProductCodeHdn').value;
    $('ProductSeries').value = $('ProductSeriesHdn').value;
    $('Sector').value=$('SectorHdn').value;
    $('CityRating').value=$('CityRatingHdn').value;
    $('FundAge').value=$('FundAgeHdn').value;
    $('CrownRating').value=$('CrownRatingHdn').value;    
    $('Performance').value=$('PerformanceHdn').value;
    $('SPRating').value=$('SPRatingHdn').value;
    $('OBSRRating').value=$('OBSRRatingHdn').value;
    
    if(document.location.href.toUpperCase().indexOf('PRICETABLE.ASPX')>0)
	{	   
        if($("ShortListed").value != '')//if(GetCookie('AvivaHUser')!= null && GetCookie('AvivaHUser')!='')
        {
           CheckSelected();
        }
	}	
}

function CheckSelected()
{
    var array = ','+ $("ShortListed").value + ',';//','+GetCookie('AvivaHUser')+',';
    var cboxes = document.forms[0].CBFund;//document.getElementsByTagName('INPUT');
    var allSelected = ',,';
    if(cboxes)
    {
        for(i=0;i<cboxes.length;i++)
	    {
	        if(IsCheckBox(cboxes[i]))
	        {
                if(array.indexOf(','+cboxes[i].value+',')>=0)
                {
                    cboxes[i].checked = true;
                    if(allSelected.indexOf(','+cboxes[i].value+',') < 0)
                    {
                        allSelected += cboxes[i].value+',';
                    }
                }
                else
                {
                    cboxes[i].checked = false;
                }
	        }
        }   
    }    
    
    if(array != ',,')
    {
        array = $("ShortListed").value.split(',');//GetCookie('AvivaHUser').split(',');
        var plural = (array.length == 1) ? "" : "(s)";
		var text = "[" + array.length + " Fund" + plural + " in short list]";
		ShortListMsg(text);
    }
    
    allSelected = allSelected.indexOf(',,') > -1 ? allSelected.replace(',,', '') : allSelected;
    var codes = allSelected.split(',');
	allSelected = new Array();
	for (var i = 0; i < codes.length; i++)
	{	    
	    var code = codes[i];
		if (code + '' != '')
		{			
			allSelected[i] = code;
		}
	}
      
    if(allSelected.length == 25)
    {
        var cbSelectAll = document.forms[0].CBSelectAll;
        for(var a=0; a < cbSelectAll.length; a++)
        {
            cbSelectAll[a].checked = true; 
        }
    }

    array = array.join(',');
    array = array.indexOf(',,') > -1 ? array.replace(',,', '') : array;
    if(array == '')
    {
        $("ShortListed").value = '';
    }    
}

function SelectAll(id)
{
    var cboxes = document.getElementsByTagName('INPUT');
	for(var i=0;i<cboxes.length;i++)
	{
	    if(IsCheckBox(cboxes[i]))
	    {
            cboxes[i].checked = $(id).checked;
	    }
    }
}

function ClearAll()
{
    $('ProductType').value = $('ProductTypeHdn').value;
    $('CurrentLegacy').value = 'ALL';
    $('ProductCode').value = 'ALL';
    $('ProductSeries').value = 'ALL';
    $('Sector').value = 'ALL';
    $('CityRating').value = 'ALL';
    $('FundAge').value = 'ALL';
    $('CrownRating').value = 'ALL';
    $('Performance').value = 'ALL';
    $('SPRating').value = 'ALL';
    $('OBSRRating').value = 'ALL';    
    $("ShortListed").value = '';
    $("AllFundsList").value = '';
    $('PageIndex').value = 1;
    //ShowHide('advance');
    document.getElementById('advance').style.display = 'none';
    document.getElementById('FilterHdn').value = 'none';    
    RedirectToPriceTable();
}

function SetSelectedValue()
{
    //Get selected values
    $('ProductTypeHdn').value = $('ProductType').value;
    $('CurrentLegacyHdn').value = $('CurrentLegacy').value;
    $('ProductCodeHdn').value = $('ProductCode').value;
    $('ProductSeriesHdn').value = $('ProductSeries').value;
    $('SectorHdn').value=$('Sector').value;
    $('CityRatingHdn').value=$('CityRating').value;
    $('FundAgeHdn').value=$('FundAge').value;
    $('CrownRatingHdn').value=$('CrownRating').value;
    $('PerformanceHdn').value=$('Performance').value;
    $('SPRatingHdn').value=$('SPRating').value;
    $('OBSRRatingHdn').value=$('OBSRRating').value;    
    
    //Set selected values
    $('ProductType').value = $('ProductTypeHdn').value;
    $('CurrentLegacy').value = $('CurrentLegacyHdn').value;
    $('ProductCode').value = $('ProductCodeHdn').value;
    $('ProductSeries').value = $('ProductSeriesHdn').value;
    $('Sector').value=$('SectorHdn').value;
    $('CityRating').value=$('CityRatingHdn').value;
    $('FundAge').value=$('FundAgeHdn').value;
    $('CrownRating').value=$('CrownRatingHdn').value;
    $('Performance').value=$('PerformanceHdn').value;
    $('SPRating').value=$('SPRatingHdn').value;
    $('OBSRRating').value=$('OBSRRatingHdn').value;
}

function $(elementID)
{
    return document.getElementById(elementID);
}

function GetFieldsList()
{
    var selectedFieldsList = '';
	var elements = document.forms[0].CBFields;//document.getElementsByTagName("INPUT");
	if(elements)
	{
	    for (var i = 0; i < elements.length; i++)
	    {
		    if(IsCheckBox(elements[i]))
		    {
			    if (elements[i].checked == true)
			    {
				    // Add new ones
				    var checkval = elements[i].value + ',';
			        if((selectedFieldsList.indexOf(checkval)<0) && (selectedFieldsList.indexOf(elements[i].value)<0))
				    {
					    selectedFieldsList = (selectedFieldsList != '' ? selectedFieldsList+',' : '');
					    selectedFieldsList += elements[i].value;
				    }
			    }
		    }
	    }	
    	
	    var codes = selectedFieldsList.split(',');
	    selectedFieldsList = '';
	    for (i = 0; i < codes.length; i++)
	    {	    
	        var code = codes[i];
		    if (code + '' != '')
		    {
			    var split = (i == codes.length -1) ? '' : ',';
			    selectedFieldsList += code + split;
		    }
	    }
	    selectedFieldsList=selectedFieldsList+'';
	    $('SelectedFields').value = selectedFieldsList;
	    if(selectedFieldsList != '')
	    {
	        document.forms[0].action= 'DataExtract.aspx?dl=1'+(isVSMVersion?"&Version=VSM":"");
	        document.forms[0].submit();
	    }
	}
}
