function start(step3Vis)
{
	changeType("typeFund");
	if (step3Vis)
	{
		//disable dateRange DD
		var dr = document.getElementById("dateRange");
		dr.disabled = 'disabled';
	}
	greyOut(step3Vis,"step3");    
    //To update the Current Type of Instrument    
    changeType(GetValueById("CurrType"));
}

function changeType(type)
{
	type = type.replace("type",""); //remove prefix from HTML IDs
	var typeArr = new Array("Fund","Sector","Index");
	var showDiv, hideDiv;
	for (var i = 0; i < typeArr.length; i++)
	{
		if (type == typeArr[i])
		{
			showDiv = GetElementById("step2" + type);
			showDiv.className = "";
			GetElementById("type"+type).checked=true;
		}
		else{
			hideDiv = GetElementById("step2" + typeArr[i]);
			hideDiv.className = "step2Hide";
		}
	}
}

function showPerf(e)
{
	var type = e.id.replace("tab","");
	var typeArr = new Array("Discrete","Cumulative","Annualised");
	var showDiv, hideDiv, showTable, hideTable;
	for (var i = 0; i < typeArr.length; i++)
	{
		if (type == typeArr[i])
		{
			showDiv = document.getElementById("tab" + type);
			showDiv.className = "perfTabOn";
			showTable = document.getElementById(type);
			showTable.className = "perfTableOn";
		}
		else
		{
			hideDiv = document.getElementById("tab" + typeArr[i]);
			hideDiv.className = "perfTab";
			hideTable = document.getElementById(typeArr[i]);
			hideTable.className = "perfTable";
		}
	}
}


function openDatePickerDialog(wnd, field, dateFormat) 
{
	myDateDialog.retDay="";
	myDateDialog.retMonth="";
	myDateDialog.retYear="";

	var dlgURL = "datepicker.htm"
	if(wnd.showModalDialog(dlgURL,myDateDialog,"dialogHeight:350px;dialogWidth:290px;center;scroll:off")==true)
	{
		field.value=dateFormat.replace(/yyyy/, myDateDialog.retYear).replace(/mm/, myDateDialog.retMonth).replace(/dd/, myDateDialog.retDay);
	}
	else
	{
		return;
	}
}
function myDateDialog()
{
	var retDay;
	var retMonth;
	var retYear;
}

//opens a popup.  reactivates popup if minimised/hidden by other windows, unlike standard window.open
function windowOpener(url,name,args)
{
	if (typeof(popupWin)!="object")
	{
	    popupWin = window.open(url,name,args);
	}
    else
    {
	    if (!popupWin.closed)
	    {
	        popupWin.location.href = url;
	    }
	    else
	    {
	        popupWin = window.open(url,name,args);
	    }
	}
	popupWin.focus();
}


function removeInstrument(typeCode)
{
	document.forms["charting"].removeCode.value = typeCode;
	UpdateChart('Remove');   
}

function UpdateChart(type,element)
{
	if(type == "TimeUpdate")
	{
	    GetElementById("isDeletedOrUpdated").value="yes";
	    var isValidDate=UserDateValidation();
	    if(isValidDate)
	    {
	        document.forms["charting"].submit();
	    }	    
	}
	else
	{
	    if(type == "Remove" || type == "Hide")
	    {
	       if(GetElementById("currentUsedSpan").value == "no")
	       {
	       	    var isValidDate=UserDateValidation();
	            if(isValidDate)
	            {
	                GetElementById("isDeletedOrUpdated").value="yes";
	                document.forms["charting"].submit();
	            }
	            else if(type == "Hide")
	            {
	                element.checked = (element.checked)?false:true;
	                synchCheckboxes(element,false)
	            }
	            else if(type == "Remove")
	            {
	              document.forms["charting"].removeCode.value="";  
	            }
	            
	       }
	       else
	       {	       
	            GetElementById("isDeletedOrUpdated").value="yes";
	            document.forms["charting"].submit();
	       }
	    }
	    else
	    {	    
            if(GetValueById("ChartCodes") >= 22 )
            {
                alert("No more instrument can be added");
            }
            else 
            {
               if(GetElementById("currentUsedSpan").value == "no")
               {
       	            var isValidDate=UserDateValidation();
                    if(isValidDate)
                    {
                        document.forms["charting"].submit();
                    }
               }
               else
               {	       
                    document.forms["charting"].submit();
               }
            }
        }
	}	
}


function synchCheckboxes(elm,isChartToBeUpdated)
{
	var boolState = elm.checked;
	var typeCode = elm.value;
	var elements = document.forms["charting"].elements;
	for (var i=0; i<elements.length; i++)
	{
		if (elements[i].type == 'checkbox')
		{
			if (elements[i].name.substr(elements[i].name.indexOf("CB")+2).toLowerCase() == typeCode.toLowerCase())
			{
				if (boolState)
				{
					elements[i].checked = true;
					elements["PlotCode" + typeCode].value = "1";
				}
				else{
					elements[i].checked = false;
					elements["PlotCode" + typeCode].value = "0";
				}
			}
		}
	}
	if(isChartToBeUpdated)
	{
	    UpdateChart('Hide',elm);
	}
}


function greyOut(vis, targetElement, options) 
{
	// Pass true to gray out screen, false to ungray
	// options are optional.  This is a JSON object with the following (optional) properties
	// opacity:0-100         
	// Lower number = less grayout higher = more of a blackout
	// zindex: #             
	// HTML elements with a higher zindex appear on top of the gray out  
	// bgcolor: (#xxxxxx)    
	// Standard RGB Hex color code  
	// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});  
	// Because options is JSON opacity/zindex/bgcolor are all optional and can appear  
	// in any order.  Pass only the properties you need to set.  
    
    var isMozilla = !document.all;     
	var options = options || {};   
	var zindex = options.zindex || 50;
	var opacity = options.opacity || 70;  
	var opaque = (opacity / 100);  
	var bgcolor = options.bgcolor || '#FFFFFF';  
	var dark=document.getElementById('darkenScreenObject');  
	var tgWidth,tgHeight;
	if (!dark) 
	{    
		// The dark layer doesn't exist, it's never been created.  So we'll    
		// create it here and apply some basic styles.    
		// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917    
		
		var tbody = document.getElementById(targetElement);
		if(!isMozilla)
		{
		    var tgTop = tbody.offsetTop + 15;
		    var tgLeft = tbody.offsetLeft + 10;
		}
		else
		{
		    var tgTop = tbody.offsetTop;
		    var tgLeft = tbody.offsetLeft;
		}
		tgWidth = tbody.offsetWidth;
		tgHeight = tbody.offsetHeight;
		var tnode = document.createElement('div');	// Create the layer.        
		tnode.style.position='absolute';			// Position absolutely        
		tnode.style.top= tgTop+"px";						// In the top        
		tnode.style.left=tgLeft+"px";
		tnode.style.width=tgWidth+"px";
		tnode.style.height=tgHeight+"px";        
		tnode.style.overflow='hidden';				// Try to avoid making scroll bars                    
		tnode.style.display='none';					// Start out Hidden        
		tnode.id='darkenScreenObject';// Name it so we can find it later
		tbody.appendChild(tnode);					// Add it to the web page    
		dark=document.getElementById('darkenScreenObject');	// Get the object.
	}  
	if (vis) 
	{	// Calculate the page width and height     
		if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) 
		{        
			var pageWidth = document.body.scrollWidth+'px';        
			var pageHeight = document.body.scrollHeight+'px';    
		} 
		else if( document.body.offsetWidth ) 
		{      
			var pageWidth = document.body.offsetWidth+'px';     
			var pageHeight = document.body.offsetHeight+'px';
		} 
		else 
		{
			var pageWidth='100%';
			var pageHeight='100%';
		}
		//set the shader to cover the entire page and make it visible.    
		dark.style.top=tgTop+"px";       
		dark.style.left=tgLeft+"px";
		dark.style.opacity=opaque;                          
		dark.style.MozOpacity=opaque;                       
		dark.style.filter='alpha(opacity='+opacity+')'; 
		dark.style.zIndex=zindex;            
		dark.style.backgroundColor=bgcolor;      
		dark.style.width=tgWidth+"px";    
		dark.style.height=tgHeight+"px";    
		dark.style.display='block';  
	} 
	else 
	{     
		dark.style.display='none';
	}	
}


//---Latest Functions

//Validating the date value while updating the chart
function UserDateValidation()
{
    var isValidDate=true;
    var endDate=GetValueById("endDate"),startDate=GetValueById("startDate");
    if(GetElementById("dateRange").selectedIndex == 0 || endDate != "" || startDate != "")
    {        
        GetElementById("dateRange").selectedIndex=0;
        var daysValues=new Array();         
        daysValues=startDate.split("/");
        if(daysValues.length == 3)
        {
            startDate=new Date();            
            if(daysValues[1]== 2 && daysValues[0] > 28 && daysValues[0] < 32)
            {
                isValidDate=CheckForLeapYear(daysValues[0],daysValues[2]);                               
            }
            if(isValidDate)
            {
                startDate.setFullYear(daysValues[2],(daysValues[1]-1),daysValues[0]);
                daysValues=endDate.split("/"); 
                if(daysValues.length == 3)
                {
                  endDate=new Date();
                  if(daysValues[1]== 2 && daysValues[0] > 28 && daysValues[0] < 32)
                  {
                    isValidDate=CheckForLeapYear(daysValues[0],daysValues[2]);                    
                  }
                  if(isValidDate)
                  {                  
                      endDate.setFullYear(daysValues[2],(daysValues[1]-1),daysValues[0]);
                      isValidDate=CheckDates(startDate,endDate);  
                  }
                  else
                  {
                    alert("End date is not valid"); 
                  }
                }
                else
                {
                    isValidDate=false;
                    alert("End date is not valid"); 
                } 
             }
             else
             {
                alert("Start date is not valid");
             }         
        }
        else
        {
            isValidDate=false;
            endDate=GetValueById("endDate");
            daysValues=endDate.split("/"); 
            if(daysValues.length == 3)
            {
                alert("Start date is not valid");                 
            }
            else
            {
                alert("Start date and end date are not valid");                 
            }             
        }
    }
    if(isValidDate)
    {
        if(GetElementById("dateRange").selectedIndex != 0)
        {
            GetElementById("currentUsedSpan").value=GetElementById("dateRange").value;
        }
        else
        {
           GetElementById("currentUsedSpan").value=GetValueById("startDate")+":"+GetValueById("endDate");
        }
        //GetElementById("currentUsedSpan").value = "yes";
    }
    else
    {
        GetElementById("currentUsedSpan").value = "no";
    }
    return isValidDate;
}

//Checking a valid date 
function CheckDates(startDate,endDate)
{
    var isValidDate =true;
    var today=new Date();
    if(startDate == 'NaN' && endDate == 'NaN')
    {
        isValidDate=false;
        alert("Start date and end date are not valid");
    }
    else if(startDate == 'NaN')
    {
        isValidDate=false;
        alert("Start date is not valid");
    }
    else if(endDate == 'NaN')
    {
        isValidDate=false;
        alert("End date is not valid");
    }       
    else if(startDate > endDate)
    {
        alert("Start date should not be grater than end date");
        isValidDate=false;
    }
    else if(startDate == endDate)
    {
        alert("Start date and end date are same");
        isValidDate=false;
    }
    else if(startDate > today && endDate > today)
    {
        isValidDate=false;
        alert("Start date and end date  are not valid"); 
    }
    else if(startDate > today)
    {
        isValidDate=false;
        alert("Start date is not valid");
    }
    else if(endDate > today)
    {
        isValidDate=false;
        alert("End date is not valid");        
    }
    return isValidDate;
}


//To generate PDF for the charting Tool
function GeneratePDF(Address)
{
    if(Address != "" && GetValueById("ChartCodes") > GetValueById("HideChartCodes"))
    {   
        var isValidDate=UserDateValidation();
        if(isValidDate)
        {
            OpenWebsite(Address);
        }
    }
    else
    {
        alert("No instruments to generate PDF");
    }
}

//To clear the start date and end date
function ClearDates()
{
    GetElementById("endDate").value="";
    GetElementById("startDate").value="";
}

//For Dropdown and Fund Loading by FundManager
function ResetSectorDD(id)
{
    GetElementById(id).selectedIndex = 0;;
}

//To open new website in new window
function OpenWebsite(siteAddress)
{
    window.open(siteAddress,'_blank','location=1,status=1,resizable=1,scrollbars=1,menubar=1,toolbar=1,height=750,width=1050');
}

//Getting the element by Id
function GetElementById(id)
{
    return document.getElementById(id);
}

// Getting the value of the element By Id
function GetValueById(id)
{
    return GetElementById(id).value;
}
