//var CurrentTab = 'Prices';

var selected = ',';
var deselected = ',';


function ChangeSearch(tab)
{
	//CurrentTab = tab;

	document.getElementById('Prices').className = (tab == 'Prices' ? 'tabOn' : 'tabOff');
	document.getElementById('anulPerformance').className = (tab == 'anulPerformance' ? 'tabOn' : 'tabOff');
	document.getElementById('cumPerformance').className = (tab == 'cumPerformance' ? 'tabOn' : 'tabOff');
	document.getElementById('discPerformance').className = (tab == 'discPerformance' ? 'tabOn' : 'tabOff');
	document.getElementById('Historical').className = (tab == 'Historical' ? 'tabOn' : 'tabOff');
	
	document.getElementById('tabPrices').className = (tab == 'Prices' ? 'selected' : '');
	document.getElementById('tabAnul').className = (tab == 'anulPerformance' ? 'selected' : '');
	document.getElementById('tabCum').className = (tab == 'cumPerformance' ? 'selected' : '');
	document.getElementById('tabDisc').className = (tab == 'discPerformance' ? 'selected' : '');
	document.getElementById('tabHist').className = (tab == 'Historical' ? 'selected' : '');
	
	SetSortImage();
		
	//Loading current table value in hidden input
	document.getElementById('CurrentTab').value=tab;

}

	// Pagination.
	function ChangeUniverse()
	{
		document.getElementById("ShortListed").value = ",";
		document.getElementById("hdCategory").value = "";
		document.getElementById("hdFundCode").value = "";
		redirect('PriceTable.aspx', 0);
	}
	
	
	function ChangeDropDown(isCategory)
	{
		  document.getElementById("hdCategory").value = document.getElementById("Category").value;
		  document.getElementById("hdFundCode").value =  (!isCategory) ? document.getElementById("FundName").value : ""; 
		  //if(!isCategory && document.getElementById("FundName").value == "")
		  //{
		//		return false;
		 // }
		  redirect('PriceTable.aspx', 0);
	}
	
	// Pagination.
	function SetPageIndex(pageindex)
	{
		 redirect('PriceTable.aspx', pageindex);
	}
	
	function redirect(page, pageindex)
	{
		var sortField = document.getElementById("SortField").value;
	 	var sortDirection = document.getElementById("SortDirection").value;
	 	var currentTab = document.getElementById('CurrentTab').value;
	 	var userType = document.getElementById('UserType').value;
	 	var universe = "";
	 	if(document.getElementById("Universe") != null)
	 	{
		  universe = document.getElementById("Universe").value;
		}
		else
		{
		    if(document.getElementById("hdUniverse") != null)
		    {
		        universe = document.getElementById("hdUniverse").value;
		    }
		    else
		    {
		        universe = document.getElementById("hdUniverseType").value;		        
		    }
		}
		var pscanShortListed = (document.getElementById("PScanShortListed") ? document.getElementById("PScanShortListed").value : '');
		var shortlisted = document.getElementById("ShortListed").value;
		var bidDate = document.getElementById("bidDate").value;
		
		var Category = document.getElementById("hdCategory").value;
		var FundCode = document.getElementById("hdFundCode").value;
		
		window.location= page + '?startrecord='+pageindex +'&sort='+sortField+'&sortdirection='+sortDirection+"&currenttab=" + currentTab +"&universe=" + universe +"&usertype="+userType+ ((page=="productscan.aspx")?"&citicode=" :"&pscanshortlisted=")+pscanShortListed +"&shortlisted="+shortlisted+"&bidDate=" + bidDate +"&Category="+ Category +"&FundCode="+FundCode;
	}
	
	// On clicking sorting tab.
	function Sort(sortValue, page)
	{
	if(sortValue == document.getElementById("SortField").value)
	{
		if(document.getElementById("SortDirection").value.toUpperCase() != "ASC")
		{
			document.getElementById("SortDirection").value = "ASC"
		}else
		{
		document.getElementById("SortDirection").value = "DESC"
		}
	}else
	{
		 document.getElementById("SortField").value = sortValue;
		 if(sortValue.toUpperCase().indexOf("UNITNAME") >= 0 || sortValue.toUpperCase().indexOf("SECTORNAMELONG") >= 0)
		 {
			document.getElementById("SortDirection").value = "ASC"
			}else
			{
			document.getElementById("SortDirection").value = "DESC"
			}
		}
		//SetPageIndex(0);
		redirect(page, 0);
	}
	
		// Page Load.
	function PageLoad()
	{
	    if(document.getElementById("userType").value == "2")
	    {
	        SetAccountCode();
	    }
		//ChangeTabs(document.getElementById("CurrentTab").value);
		SetSortImage();
		ckBoxselect();
		var count =GetShortListCount(document.getElementById("ShortListed").value);
			if(document.getElementById("divShortListed") != null)
			{
				 if(count> 0)
				 {
					 document.getElementById("divShortListed").innerText="   ["+ count +" fund(s) added to short list]";
					document.getElementById("divShortListed").textContent="   ["+ count +" fund(s) added to short list]";
				}
				else
				{
					document.getElementById("divShortListed").innerText=" ";
					document.getElementById("divShortListed").textContent=" ";
				}
			}
 
		appendDropDown();
		setSelect("Universe", document.getElementById("hdUniverse").value);
		
		
	}
	
	function appendDropDown()
	{

		var fundNameList =  document.getElementById("ddlFundName").value;
		var objSelect = document.getElementById("FundName");

		var group = fundNameList.split('|');
		
		objSelect.appendChild(CreateOption("All", ""));
					
		for(var i=0; i < group.length; i++)
		{
		  var optGroup = document.createElement('optgroup');
		
		  var optsValues = group[i].split(';');
		  
		  for(var j=0; j < optsValues.length; j++)
		  {
            var subValues = optsValues[j].split(',');
            if(j==0)
            {
              optGroup.label = subValues[0];
            }
            else
            if(subValues != null && subValues[0]!= "")
            {
		      optGroup.appendChild(CreateOption(subValues[0], subValues[1]));
            }
            
		  }
		   objSelect.appendChild(optGroup);

		}
	
		setTimeout("setSelect('FundName','"+ document.getElementById('hdFundCode').value + "')",100);

	}
	
	
	function CreateOption(innerHtml, value)
	{
		objOption=document.createElement("option");
		if(navigator.appName=='Netscape')
	    {
	        objOption.textContent= innerHtml;
	    }
	    else
	    {
	        objOption.innerText  = innerHtml;
	    }
		objOption.value =value;
		return objOption;
	}
	
	function GetBrowserVersion()
	{
		 var browserName=navigator.appName; 
		 var version;
		
		 if (browserName=="Microsoft Internet Explorer")
		 {
		  var borwerVersion = navigator.appVersion;
		  var subVer = borwerVersion.split('(');
		  if(subVer.length == 2)
		  {
				var getVersion = subVer[1].split(';');
				for(var i = 0; i< getVersion.length; i++)
				{
					if(getVersion[i].indexOf('MSIE') >= 0)
					{
						var MSIE = getVersion[i].split(' ');
						if(MSIE.length == 3)
						{
							version = parseFloat(MSIE[2]);
						}
					}
				}
		  }
		 
		 }
		 return version;
	}
	
	
	function SetSortImage()
	{
		var sortField = document.getElementById("SortField").value;
		var sortDirection = document.getElementById("SortDirection").value  ;
        if(document.getElementById("UnitNameLong"))
        {
	        document.getElementById("UnitNameLong").src = (sortField == 'UnitName' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("UnitNameLongA").src = (sortField == 'UnitName' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("UnitNameLongC").src = (sortField == 'UnitName' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("UnitNameLongD").src = (sortField == 'UnitName' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');

	        document.getElementById("SectorNameLong").src = (sortField == 'SectorNameLong' ? 'images/' + sortDirection.toUpperCase() + '.gif':(sortField == 'IrishLifeNetSectorNameLong')? 'images/' + sortDirection.toUpperCase() + '.gif' : 'images/expande.gif');
	        document.getElementById("Price").src = (sortField == 'Price' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("PercentageChange").src = (sortField == 'PercentageChange' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("PriceDate").src = (sortField == 'PriceDate' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
    		
	        document.getElementById("Pa12m").src = (sortField == 'Pa12m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        //document.getElementById("Pa24m").src = (sortField == 'Pa24m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("Pa36m").src = (sortField == 'Pa36m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        //document.getElementById("Pa48m").src = (sortField == 'Pa48m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("Pa60m").src = (sortField == 'Pa60m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        /*document.getElementById("Pa120m").src = (sortField == 'Pa120m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("PaBase").src = (sortField == 'PaBase' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');*/
	        document.getElementById("LaunchDateA").src = (sortField == 'LaunchDate' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');

	        document.getElementById("P12m").src = (sortField == 'P12m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        //document.getElementById("P24m").src = (sortField == 'P24m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("P36m").src = (sortField == 'P36m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        //document.getElementById("P48m").src = (sortField == 'P48m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("P60m").src = (sortField == 'P60m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        //document.getElementById("P120m").src = (sortField == 'P120m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("PBase").src = (sortField == 'PBase' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("LaunchDate").src = (sortField == 'LaunchDate' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');

	        document.getElementById("PD12m").src = (sortField == 'P12m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("P12t24m").src = (sortField == 'P12t24m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("P24t36m").src = (sortField == 'P24t36m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("P36t48m").src = (sortField == 'P36t48m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
	        document.getElementById("P48t60m").src = (sortField == 'P48t60m' ? 'images/' + sortDirection.toUpperCase() + '.gif': 'images/expande.gif');
        }
	}
	
	
	
	
	
	function AddShortList()
	{
		var shortListed = document.getElementById("ShortListed").value;
		//var selecteds = selected.split(',');
		
		//for(var i=0; i< selecteds.length; i++)
		//{
		//	alert(selecteds[i]);
		//}
		
		if(selected != '')
		{
			shortListed += selected;
			shortListed = shortListed.replace(",,",",");
			
		}
		
		if(deselected != "")
		{
			var deselecteds = deselected.split(',');

			for(var i=0; i< deselecteds.length; i++)
			{
				shortListed = shortListed.replace("," + deselecteds[i] + ",",",");
			}
		}
		
		
		var count =GetShortListCount(shortListed);
		
		if(count > 20)
		{
			alert("A maximum of 20 funds can only be added to the shortlist.");
		}
		else
		{ 
			selected = ",";
			deselected = ",";
			document.getElementById("ShortListed").value = shortListed;
			if( document.getElementById("divShortListed") != null)
			{
			if(count > 0)
			{
					document.getElementById("divShortListed").innerText="   ["+ count +" fund(s) added to short list]";
					document.getElementById("divShortListed").textContent="   ["+ count +" fund(s) added to short list]";
				}
				else
				{
					document.getElementById("divShortListed").innerText=" ";
					document.getElementById("divShortListed").textContent=" ";
				}
			}
		}
	}
	
	
	
function GetShortListCount(shortListed)
	{
		var shortListeds = shortListed.split(',');
		var count = shortListeds.length;
		
		if(count > 0)
		{
			if(shortListeds[0] != null &&  shortListeds[0] == "")
			{
				count--;
			}
			
			if(shortListeds[shortListeds.length-1] != null &&  shortListeds[shortListeds.length-1] =="")
			{
				count--;
			}
		}
		
		return count;
	}
	
	function ViewShortList()
	{
			redirect('ShortList.aspx', 0);
	}
	
	function CheckBoxCheck(checked, value)
	{
		var obj = document.forms[0].cbFund;
		for(var i=0; i <  obj.length; i++)
		{
			if(obj[i].value == value)
			{
				obj[i].checked = checked;
			}
		}
		
		var name = (document.getElementById("PScanShortListed") ? "PScanShortListed" : "ShortListed");
		
		var shortListed = document.getElementById(name).value;
		
		var selValue = "," + value + ",";
		if(checked)
		{
			if(selected.indexOf("," + value + ",") < 0)
			{
				if(shortListed.indexOf("," + value + ",") < 0)
				{
					selected += value +","
				}
				
				if(deselected.indexOf(selValue) >= 0)
				{
					deselected = deselected.replace(selValue,"");
				} 			
			} 
		}
		else
		{		    
		    if(shortListed.indexOf(selValue) >= 0)
		    {
		        var result="";
		        var shortListedArray=shortListed.split(',');
		        for(var i=0;i<shortListedArray.length;i++)
		        {
		            if(shortListedArray[i] != value && shortListedArray[i] != "" && shortListedArray[i] != null  )
		            {
		               result += shortListedArray[i] + ","; 
		            }	            
		        }
		        document.getElementById(name).value = result;		        
		    }
			if(selected.indexOf(selValue) >= 0)
			{
				selected = selected.replace(selValue,"");
			}
			else if(deselected.indexOf(selValue) < 0)
			{
				deselected  += value +","
			}
		}
	}
	
	function SelectForPScanShortlist(checked,value)
	{
	    var shortlisted = $('PScanShortListed').value;
	    shortlisted = ','+shortlisted+',';
	    value = ','+value+',';
	    $('PScanShortListed').value='';
	    if(checked)
	    {
	        if(shortlisted.indexOf(value)<0)
	        {
	            shortlisted= shortlisted + value;
	        }
	    }
	    else
	    {
	        shortlisted= shortlisted.replace(value,'');
	    }
	    $('PScanShortListed').value = TrimFull(shortlisted,',');
	}
	
	function $(id)
	{
	    return document.getElementById(id);
	}
	
	function ckBoxselect(isShortlistPage)
	{
		var shortListed = document.getElementById(isShortlistPage ? "PScanShortListed" : "ShortListed").value;
		shortListed = TrimFull(shortListed, ',');
		shortListed = ',' + shortListed + ',';
	    var obj = document.forms[0].cbFund;	
		if(obj != null)
		{
			for(var i=0; i< obj.length; i++)
			{
				if(shortListed.indexOf("," + obj[i].value + ",")>=0)
				{
					obj[i].checked = true;
				}
			}
		}
	}
	
	
	function Trim(code,params)
    {
	    if(code != null)
	    {
		    return TrimStart(TrimEnd(code,params),params);
	    }
	    return code;
    }

    function TrimFull(code,params)
    {
        var previousCode = code;
	    if(code != null)
	    {
	        for(;;)
	        {
	            code = Trim(code,params);
	            if(previousCode == code)
	            {
	                break;
	            }
	            else
	            {
	                previousCode = code;
	            } 
		    }
	    }
	    return code;
    }
            
    function TrimStart(code,params)
    {
	    if(code != null && code.substring(0,1) == params)
	    {
		    return code.substring(1, code.length);
	    }
	    return code;
    }

    function TrimEnd(code,params)
    {
	    if(code != null && code.substring(code.length-1,code.length) == params)
	    {
		    return code.substring(0, code.length-1);
	    }
	    return code;
    }
	
	function DeleteShortlist(pageindex)
	{
		var shortListed = document.getElementById("ShortListed").value;
		shortListed = TrimFull(shortListed,',');
		shortListed = ','+ shortListed +',';
		var pscanShortListed = document.getElementById("PScanShortListed").value;
		pscanShortListed = TrimFull(pscanShortListed,',');
		pscanShortListed = ','+ pscanShortListed +',';
		var obj = document.forms[0].cbFund;
		
		for(var i=0; i< obj.length; i++)
		{
			if(obj[i].checked && shortListed.indexOf("," + obj[i].value + ",")>=0)
			{
				shortListed =  shortListed.replace("," + obj[i].value + "," , ",");
			}
			if(obj[i].checked && pscanShortListed.indexOf("," + obj[i].value + ",")>=0)
			{
				pscanShortListed =  pscanShortListed.replace("," + obj[i].value + "," , ",");
			}
		}
		document.getElementById("ShortListed").value = shortListed;
		document.getElementById("PScanShortListed").value = shortListed;
		redirect('ShortList.aspx', pageindex);
	}
	
	function clearShortlist(pageindex)
	{
		document.getElementById("ShortListed").value = ",";
		redirect('PriceTable.aspx', pageindex);
	}
	
	
	function OpenChart(page)
	{
		window.open(page);
	}
	
	function OpenDisclaimer()
	{
		window.open('Disclaimer.htm', 'Disclaimer', 'width=715,height=230');
	}	
	
	function ChangeBidDate(page, pageindex)
	{
		if(isDate())
		{
			document.getElementById("bidDate").value = document.getElementById("textPriceDate").value ;
			//SetPageIndex(pageindex)
			redirect(page,pageindex)
		}
	}
	
	// Setting the intial values for dropdown.
	function setSelect(sboxID, value) {
	 sbox = document.getElementById(sboxID);
    for (i = 0 ; i < sbox.options.length; i++) {
      if (sbox.options[i].value == value) {
        sbox.selectedIndex = i;
		return;
      }
    }
  }
  
  
  //-----------------------------------------------------------------------------------------------------//
  //                                    PDF LOADER
  //-----------------------------------------------------------------------------------------------------//
  
  function PDFPageLoad()
  {
       
      var pdfName = document.getElementById("PDFName").value;
      var url = "http://factsheets.financialexpress.net/HIB/" + pdfName;
      window.location = url;
  }
  

//----------------------------------------------------------------------------------------------------//
//													Date Validation                                                //
//----------------------------------------------------------------------------------------------------//

var dtCh= "/";
var minYear=1900;


function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(){
	var dtStr = document.getElementById("textPriceDate").value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	//var strMonth=dtStr.substring(0,pos1)
	//var strDay=dtStr.substring(pos1+1,pos2)
	var strDay = dtStr.substring(0,pos1)
	var strMonth = dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		alert("Please use the following format for your entry: 'dd/mm/yyyy'");
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		alert("Please use the following format for your entry: 'dd/mm/yyyy'");
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		alert("Please use the following format for your entry: 'dd/mm/yyyy'");
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear ){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		alert("Please use the following format for your entry: 'dd/mm/yyyy'");
		return false
	}
	
	if(year > maxYear)
	{
		alert('Please insert valid date');
		return false
	}
	else if(year == maxYear)
	{
		if(strMonth > curMonth)
		{
			alert('Please insert valid date');
			return false
		}
		else if(strMonth == curMonth)
		{
			if(strDay > curDate)
			{
				alert('Please insert valid date');
				return false
			}
		}
	}
	
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
 
 function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   if(KeyID == 13)
   {
		ChangeBidDate(0);
	}
}
 
 //----------------------------------------------------------------------------------------------------//
//													Date Validation Ends                                           //
//----------------------------------------------------------------------------------------------------//

