var MaxShortListLength = 900;

function ChangeTabs(form, tab)
{
	document.getElementById('Prices').className = (tab == 'Prices' ? 'tabOn' : 'tabOff');
	document.getElementById('PerfC').className = (tab == 'PerfC' ? 'tabOn' : 'tabOff');
	document.getElementById('PerfD').className = (tab == 'PerfD' ? 'tabOn' : 'tabOff');
	document.getElementById('Ratings').className = (tab == 'Ratings' ? 'tabOn' : 'tabOff');
	document.getElementById('Details').className = (tab == 'Details' ? 'tabOn' : 'tabOff');		
	document.getElementById('Ratios').className = (tab == 'Ratios' ? 'tabOn' : 'tabOff');
	document.getElementById('FundCodes').className = (tab == 'FundCodes' ? 'tabOn' : 'tabOff');
	
	document.getElementById('tabPrices').className = (tab == 'Prices' ? 'selected' : '');
	document.getElementById('tabPerfC').className = (tab == 'PerfC' ? 'selected' : '');
	document.getElementById('tabPerfD').className = (tab == 'PerfD' ? 'selected' : '');
	document.getElementById('tabRatings').className = (tab == 'Ratings' ? 'selected' : '');
	document.getElementById('tabDetails').className = (tab == 'Details' ? 'selected' : '');		
	document.getElementById('tabFundCodes').className = (tab == 'FundCodes' ? 'selected' : '');
	document.getElementById('tabRatios').className = (tab == 'Ratios' ? 'selected' : '');
	
	document.getElementById('tabLiPrices').className = (tab == 'Prices' ? 'selected' : '');
	document.getElementById('tabLiPerfC').className = (tab == 'PerfC' ? 'selected' : '');
	document.getElementById('tabLiPerfD').className = (tab == 'PerfD' ? 'selected' : '');
	document.getElementById('tabLiRatings').className = (tab == 'Ratings' ? 'selected' : '');
	document.getElementById('tabLiDetails').className = (tab == 'Details' ? 'selected' : '');		
	document.getElementById('tabLiFundCodes').className = (tab == 'FundCodes' ? 'selected' : '');
	document.getElementById('tabLiRatios').className = (tab == 'Ratios' ? 'selected' : '');
	
	if ($("ProductTypeHdn").value != "COLLECTIVES")
	{
	    document.getElementById('Fees').className = (tab == 'Fees' ? 'tabOn' : 'tabOff');
	    document.getElementById('tabFees').className = (tab == 'Fees' ? 'selected' : '');
	    document.getElementById('tabLiFees').className = (tab == 'Fees' ? 'selected' : '');
	}
	
	document.getElementById('SelectedTab').value = tab;	
	
}

function LoadFlt(url,height,width,scrollbars)
{
	var browser = navigator.appName + " " + navigator.appVersion; 
    	var Newwindow = window.open(url,"choose","resizable=no,height=" + height + ",width=" + width + ",scrollbars=" + scrollbars + ",top=20,left=250");
	
      	if ( browser.substring(0, 8) =="Netscape") 
      	{ 
        	Newwindow.creator = self; 
      	}
        else 
	    {
          	if (Newwindow.opener == null) 
          	{
			    Newwindow.opener = self;
		    } 
      	}	
}

function ShowHide(id){ 
	if (document.getElementById){ 
		obj = document.getElementById(id); 
		if (obj.style.display == "none"){ 
			obj.style.display = "";
			document.getElementById('FilterHdn').value = "block";
			document.getElementById('submitDiv').style.display = "";
		} else { 
			obj.style.display = "none"; 
			document.getElementById('FilterHdn').value = "none";
			document.getElementById('submitDiv').style.display = "none";
		} 
	} 
}

//Set checked true for selected fund in all the tables
function SelectInstrument(id, selectedControl)
{
	var priceControl = document.getElementById('tablePrices' + selectedControl.value);
	var detailsControl = document.getElementById('tableDetails' + selectedControl.value);
	var ratingsControl = document.getElementById('tableRatings' + selectedControl.value);
	var ratiosControl = document.getElementById('tableRatios' + selectedControl.value);	
	var perfCControl = document.getElementById('tablePerfC' + selectedControl.value);
	var perfDControl = document.getElementById('tablePerfD' + selectedControl.value);
	var fundCodesControl = document.getElementById('tableFundCodes' + selectedControl.value);	
	
	priceControl.checked = selectedControl.checked;
	detailsControl.checked = selectedControl.checked;
	ratingsControl.checked = selectedControl.checked;
	ratiosControl.checked = selectedControl.checked;
	perfCControl.checked = selectedControl.checked;
	perfDControl.checked = selectedControl.checked;
	fundCodesControl.checked = selectedControl.checked;	
	
	if ($("ProductTypeHdn").value != "COLLECTIVES")
	{
	    var feesControl = document.getElementById('tableFees' + selectedControl.value);
	    feesControl.checked = selectedControl.checked;
	}
}

/// To Get specified cookie
function GetCookie(name) 
{
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return '';
    if (start == -1) return '';
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

/// To Set specified cookie
function SetCookie(name,value,expires,path,domain,secure)
{
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function AddToShortList(addFundsForCSV)
{
    var shortListString = $("ShortListed").value;//GetCookie('AvivaHUser');
    var elements = document.forms[0].CBSelectAll;
    if(shortListString == '' && elements && document.location.href.toUpperCase().indexOf('PRICETABLE.ASPX')>0)
    {
        shortListString = (elements[0].checked) ? $("AllFundsList").value : '';
    }
    var newUnits = '';
	var newCount = 0;
	var array = new Array();
	array = shortListString.split(',');
	var elements = document.forms[0].CBFund;//document.getElementsByTagName("INPUT");
	if(elements)
	{
	    for (var i = 0; i < elements.length; i++)
	    {
		    if(IsCheckBox(elements[i]))
		    {
			    if (elements[i].checked == true && elements[i].value != 'SetAllChecked')
			    {
				    // Add new ones
				    var checkval = elements[i].value + ',';
			        if((shortListString.indexOf(checkval)<0) && (array[array.length - 1] != elements[i].value) && (shortListString.indexOf(elements[i].value)<0))
				    {
					    shortListString = (shortListString != '' ? shortListString+',' : '');
					    shortListString += elements[i].value;
				    }
			    }
			    else
			    {
			        // Remove if in existing list
			        var checkval = elements[i].value + ',';	
			        if(shortListString.indexOf(checkval)>=0)
				    {
					    shortListString = shortListString.replace(checkval,'');
				    }
				    if(array[array.length - 1] == elements[i].value)
				    {
				          shortListString = shortListString.replace(elements[i].value,'');
				    }												
			    }
		    }
	    }   
	}		
				
	// Count and advise the user
	var totalCount = 0;
	var codes = shortListString.split(',');
	shortListString = '';
	for (var i = 0; i < codes.length; i++)
	{	    
	    var code = codes[i];
		if (code + '' != '')
		{
			totalCount++;
			var split = (i == codes.length -1) ? '' : ',';
			shortListString += code + split;
		}
	}
	
	// Limit
	if (totalCount > MaxShortListLength)
	{
		alert('A maximum of 900 funds can be added to the short list.');
	}
	else
	{
		// Store
		if(addFundsForCSV)
		{
		    $("ShortListedForCSV").value = shortListString;		
		}
		else
		{
		    $("ShortListed").value = shortListString;
		}
		// Advise
		var plural = (totalCount == 1) ? "" : "(s)";
		var text = "[" + totalCount + " Fund" + plural + " in short list]";
		ShortListMsg(text);		
	}
}

function IsCheckBox(chk)
{
	return (chk.type == 'checkbox');
}

function ShortListMsg(msg)
{
	document.getElementById("lblshortlist").innerHTML = msg;
}

function SubmitTo(url)
{
    //document.forms[0].method = "get";
    if(url == 'ShortList.aspx')
    {
        if($("ShortListed").value != '')//if(GetCookie('AvivaHUser')!= null && GetCookie('AvivaHUser')!='')
        {
            document.forms[0].action = url;
	        document.forms[0].submit();
        }                
    }
    else
    {
	    document.forms[0].action = url;
	    document.forms[0].submit();
	}
	
}

function DeleteSelectedFunds()
{
	var hasSelectedFunds = false;
	var shortListString = $("ShortListed").value;//GetCookie('AvivaHUser');
	if(shortListString != null && shortListString !='')
    {
        shortListString = shortListString + ',';
        var elements = document.getElementsByTagName("INPUT");
	    for (var i = 0; i < elements.length; i++)
	    {
	        var chkvalue = shortListString.indexOf(',') > 0 ? elements[i].value + ',' : elements[i].value;
		    if (IsCheckBox(elements[i]) && elements[i].checked == true)
		    {
			    shortListString = shortListString.replace(chkvalue, '');
			    hasSelectedFunds = true;
		    }
	    }
    	
	    if (hasSelectedFunds)
	    {
	        if(shortListString.length > 0)
	        {
	            shortListString = shortListString.substring(0, shortListString.length - 1);
	        }
		    $("ShortListed").value = shortListString;//SetCookie('AvivaHUser',shortListString);
		    document.forms[0].action = 'ShortList.aspx';
		    document.forms[0].submit();
	    }
    }	
}
