
//Global Variables
var XmlHttp;

//Creating object of XMLHTTP For AJAX Method
function CreateXmlHttp()
	{
		//Creating object of XMLHTTP in IE
		try
		{
			XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(oc)
			{
				XmlHttp = null;	
			}
		}
		//Creating object of XMLHTTP in Mozilla and Safari 
		if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
		{
			XmlHttp = new XMLHttpRequest();
		}
	}
//Global Variables
var XmlHttp_One,XmlHttp_two,XmlHttp_three;

//Creating object of XMLHTTP For AJAX Method
function CreateXmlHttpNew()
	{
		//Creating object of XMLHTTP in IE
		try
		{
			XmlHttp_One = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				XmlHttp_One = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(oc)
			{
				XmlHttp_One = null;	
			}
		}
		//Creating object of XMLHTTP in Mozilla and Safari 
		if(!XmlHttp_One && typeof XMLHttpRequest != "undefined") 
		{
			XmlHttp_One = new XMLHttpRequest();
		}
	}	
//Creating object of XMLHTTP For AJAX Method
function CreateXmlHttptwo()
	{
		//Creating object of XMLHTTP in IE
		try
		{
			XmlHttp_two = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				XmlHttp_two = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(oc)
			{
				XmlHttp_two = null;	
			}
		}
		//Creating object of XMLHTTP in Mozilla and Safari 
		if(!XmlHttp_two && typeof XMLHttpRequest != "undefined") 
		{
			XmlHttp_two = new XMLHttpRequest();
		}
	}	
	//Creating object of XMLHTTP For AJAX Method
function CreateXmlHttpthree()
	{
		//Creating object of XMLHTTP in IE
		try
		{
			XmlHttp_three = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				XmlHttp_three = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(oc)
			{
				XmlHttp_three = null;	
			}
		}
		//Creating object of XMLHTTP in Mozilla and Safari 
		if(!XmlHttp_three && typeof XMLHttpRequest != "undefined") 
		{
			XmlHttp_three = new XMLHttpRequest();
		}
	}
//For Loading ticker from CategoryRetData.aspx Using AJAX
function loadCatRet(Period)
	{
		CreateXmlHttp();
		document.body.style.cursor = "progress";
		if(Period=='')
			Period = "1YEAR";
		var requestUrl = "CategoryRetData.aspx?Period="+Period;
		if(XmlHttp)	{
					XmlHttp.onreadystatechange = CatRetResponse;
					XmlHttp.open("GET", requestUrl,  true);
					XmlHttp.send(null);
				}
	}	
	
//For Loading ticker from MfTopGLData.aspx Using AJAX
/*function loadGLData(Period,Category,TopGl)
		{
		CreateXmlHttp();
		document.body.style.cursor = "progress";
		var TopGl;
		if(Period=='')
			Period = "1YEAR";
		if(Category=='')	
		Category="ALL";
		//var requestUrl = "MfTopGLData.aspx?Period="+ Period +"&Cat="+ Category +"&GL="+ TopGl;
		var requestUrl = "..MFtopglData.aspx?Period="+ Period +"&Cat="+ Category +"&TopGl="+ TopGl;
		if(XmlHttp)	{
			XmlHttp.onreadystatechange = function(){GLResponse(TopGl)}
					XmlHttp.open("GET", requestUrl,  true);
					XmlHttp.send(null);
				}
}	*/

//For Loading ticker from watchlist/mfschemedata.aspx Using AJAX
function loadSchemeData()
		{
		CreateXmlHttp();
		document.body.style.cursor = "progress";
		var fund    = document.getElementById('FundFamilyList')[document.getElementById('FundFamilyList').selectedIndex].value;
		var Cat     = document.getElementById('CategoryList')[document.getElementById('CategoryList').selectedIndex].value;
		if(Cat!="")
		{
			document.getElementById('SubCategoryList').disabled = false;
			ChangeSubCat(Cat);
		}
		else
			document.getElementById('SubCategoryList').disabled = true;
		
		var SubCat  = document.getElementById('SubCategoryList')[document.getElementById('SubCategoryList').selectedIndex].value;
		var Struct  = document.getElementById('StructureList')[document.getElementById('StructureList').selectedIndex].value;
		var Opt     = document.getElementById('OptionList')[document.getElementById('OptionList').selectedIndex].value;
		
		var requestUrl = "mfSchemedata.aspx?fund="+ fund +"&Cat="+ Cat +"&SubCat="+ SubCat +"&Struct="+ Struct +"&Opt="+ Opt ;
		if(XmlHttp)	{
					XmlHttp.onreadystatechange = SchmDataResponse;
					XmlHttp.open("GET", requestUrl,  true);
					XmlHttp.send(null);
				}
	}

	
//Called when response comes back from server Only For Ticker
/*function GLResponse(TopGl)
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	
			var GainerId = document.getElementById("GainerLoserCtrl_GainerId");
			var LoserId  = document.getElementById("GainerLoserCtrl_LoserId");
			var strGlData = XmlHttp.responseText
			if(TopGl=='T')
				GainerId.innerHTML = strGlData;
			  else
				LoserId.innerHTML = strGlData;
				
			document.body.style.cursor = "auto";		
		}
		else
		{
			if(TopGl=='T')
				GainerId.innerHTML = "There was a problem retrieving data from the server.";
			else
				LoserId.innerHTML =  "There was a problem retrieving data from the server.";
			
			document.body.style.cursor = "auto";
		}
	}
}*/
	
//Called when response comes back from server Only For Ticker
function HandleResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			var MFTickerDiv = document.getElementById("MFTicker_MFTickerDiv");
			MFTickerDiv.innerHTML = XmlHttp.responseText;
			document.body.style.cursor = "auto";
		}
		else
		{
			MFTickerDiv.innerHTML = "There was a problem retrieving data from the server.";
			document.body.style.cursor = "auto";
		}
	}
}

//Called when response comes back from server Only For Category Return
function CatRetResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			var EquityId   = document.getElementById("CategoryRetCtrl_EquityId");
			var DebtId     = document.getElementById("CategoryRetCtrl_DebtId");
			var BalancedId = document.getElementById("CategoryRetCtrl_BalancedId");
			//window.alert(XmlHttp.responseText);
			var strData = XmlHttp.responseText
			if(strData != "")
			{
				var arrDateSplit = strData.split("|");
				EquityId.innerHTML   = arrDateSplit[0];
				DebtId.innerHTML     = arrDateSplit[1];
				BalancedId.innerHTML = arrDateSplit[2];
			}
			document.body.style.cursor = "auto";	
		}
		else
		{
			EquityId.innerHTML = "There was a problem retrieving data from the server.";
			DebtId.innerHTML = "There was a problem retrieving data from the server.";
			BalancedId.innerHTML = "There was a problem retrieving data from the server.";
			document.body.style.cursor = "auto";
		}
	}
}


//Called when response comes back from server Only For add schemes option transfer
function SchmDataResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			var availSchemes   = document.getElementById("availSchemes");
			var strData = XmlHttp.responseText
			if(strData != "")
			{	
				var arrSchm = strData.split("|");
				availSchemes.length = 0; 	
				for(i=0; i<arrSchm.length-1; i++)
				{	
					var strSchm = arrSchm[i];
					var arrSchmCode = strSchm.split("~");
					availSchemes.options[i] = new Option();
					availSchemes.options[i].value = arrSchmCode[0];
					availSchemes.options[i].text = arrSchmCode[1];
				}
			}
			else
			{
					availSchemes.length = 0;
					availSchemes.options[0] = new Option(); 
					availSchemes.options[0].value = "";
					availSchemes.options[0].text = "Scheme is not available";			
			}
			document.body.style.cursor = "auto";
		}
		else
		{
					availSchemes.length = 0;
					availSchemes.options[0] = new Option(); 
					availSchemes.options[0].value = "";
					availSchemes.options[0].text = "server is not ready";
					document.body.style.cursor = "auto";
		}
	}
}





var arrMININVT = new Array();
var arrMF_SCHCODE = new Array();
//var MinInvAmt=0;
var SchTypeCode="";

//For Loading schemes from SipCalcSchData.aspx Using AJAX
function loadSipScheme1(mf_code)
{
	CreateXmlHttpNew();
	//document.getElementById("MinSip").innerText="";
	document.body.style.cursor = "progress";
	if(mf_code=='')return false;
	var requestUrl = "SipCalcSchData.aspx?MF_Code="+ mf_code;
	
	if(XmlHttp_One)	{
				//alert(requestUrl)
				XmlHttp_One.onreadystatechange = function(){sipSchResp()}
				XmlHttp_One.open("GET", requestUrl,  true);
				XmlHttp_One.send(null);
			}
}

//Called when response comes back from server Only For sip schemes
function sipSchResp()
{
	if(XmlHttp_One.readyState == 4)
	{
		if(XmlHttp_One.status == 200)
		{			
			var DrpScheme   = document.getElementById("DrpScheme");
			
			var strData = XmlHttp_One.responseText
			if(strData != "")
			{	
				if(strData!="NA") {
					var arrSchm = strData.split("|");
					
					var arrSCH_NAME   = eval(arrSchm[1]);
					arrMF_SCHCODE = eval(arrSchm[0]);
					arrMININVT    = eval(arrSchm[2]);

						DrpScheme.length = 0;
						DrpScheme.options[0] = new Option();
						DrpScheme.options[0].value = "";
						DrpScheme.options[0].text  = "Select Scheme..";						
						for(i=1; i<=arrMF_SCHCODE.length; i++) {	
							DrpScheme.options[i] = new Option();
							DrpScheme.options[i].value = arrMF_SCHCODE[i-1];
							DrpScheme.options[i].text  = arrSCH_NAME[i-1].replace(/#/g,"'");
						}
				}
				else
					fillSchDef("Scheme is not available");
			}
			else
				fillSchDef("Scheme is not available");
				document.body.style.cursor = "auto";
		}
		else
		{
			fillSchDef("server is not ready");
			document.body.style.cursor = "auto";
		}
	}
}	

function fillSchDef(txt){
	var DrpScheme   = document.getElementById("DrpScheme");
	DrpScheme.length = 0;
	DrpScheme.options[0] = new Option(); 
	DrpScheme.options[0].value = "";
	DrpScheme.options[0].text = txt;	
}


function MinSipAmt1(schCode){
	//alert(schCode);
	document.body.style.cursor = "progress";
	var inv_Amnt  = document.getElementById("TxtInvstAmt");

	//CreateXmlHttp();
	CreateXmlHttptwo();
	
	var requestUrl = "SipStDtData.aspx?schCode="+ schCode;
	//alert(requestUrl);
	
	if(XmlHttp_two){
	
				XmlHttp_two.onreadystatechange = function(){sipDtResp()}
				XmlHttp_two.open("GET", requestUrl,  true);
				XmlHttp_two.send(null);
			}
}


//Called when response comes back from server Only For sip Start Date
function sipDtResp()
{
	
	if(XmlHttp_two.readyState == 4)
	{
	//alert(XmlHttp.readyState);
		if(XmlHttp_two.status == 200)
		{			
			//var DrpScheme   = document.getElementById("DrpScheme");
			var strData = XmlHttp_two.responseText
			//alert(strData);
			if(strData != "")
			{	
				var stDy = document.getElementById("drpStDay");
				var stMn = document.getElementById("drpStMn");
				var stYr = document.getElementById("drpStYr");
				
				var arrStr = strData.split("|");
				var arrDate = arrStr[0].split("-");
					for(i=0; i<stDy.length; i++)
						if(stDy.options[i].value==arrDate[0])stDy.options[i].selected = true;
					for(i=0; i<stMn.length; i++)
						if(stMn.options[i].value==arrDate[1])stMn.options[i].selected = true;
					for(i=0; i<stYr.length; i++){
						if(stYr.options[i].value==arrDate[2])stYr.options[i].selected = true;										
				}					
				SchTypeCode = arrStr[1];
			}
		}
	}
	document.body.style.cursor = "auto";
}



function validate()
{   
    
    var FundCombo = document.getElementById("DrpAMC");
	//var Scheme = document.getElementById('_ct10_DrpScheme');
	var loading = document.getElementById("loading");
	if(FundCombo.value=="")
	{
	 alert("Please Select AMC");
	 return false;
	}
	//if(CatCombo.value=="")
	//{
	// alert("Please Select Category");
	// return false;
	//}
	//return true;
	
	//var SIPCALC    = document.getElementById("SIPCALC");
	//SIPCALC.style.display='none';
	var tab1    = document.getElementById("tab1");
	tab1.style.display='none';
	var tab2    = document.getElementById("tab2");
	tab2.style.display='none';
	var tab3    = document.getElementById("tab3");
	tab3.style.display='none';
	var SIPTable    = document.getElementById("SIPTable");
	SIPTable.style.display='none';
	
	var sch_Code  = document.getElementById("DrpScheme");
	var inv_Amnt  = document.getElementById("TxtInvstAmt");
	
	var stday     = document.getElementById("drpStDay");
    var drpStmonth= document.getElementById("drpStMn");
    var drpStyear = document.getElementById("drpStYr");
    
	var edday     = document.getElementById("drpEdDay");    
	var drpEnmonth= document.getElementById("drpEdMn");
	var drpEnyear = document.getElementById("drpEdYr");
	if(drpStyear.value=="" || drpEnyear.value=="" || drpStmonth.value=="" || drpEnmonth.value=="")
	    {
			alert("End Date should be Greater than Start Date") 	
			return false; 
	    }
	else if(drpStyear.value==drpEnyear.value)
		{
			if (eval(drpStmonth.value)>= eval(drpEnmonth.value))
			{
				alert("End Date should be Greater than Start Date") 	
				drpStmonth.focus()
				return false; 
			}
		}			
	else if(drpStyear.value > drpEnyear.value)
		{
			alert("End Date should be Greater than Start Date") 	
			drpStyear.focus()
			return false; 
		}
	else if(drpStyear.value%4==0 && drpStmonth.value==02 && stday.value>29)  
		{
			alert("Day can't be greater than 29");
			stday.focus();
			return false;
		}
	else if(drpStmonth.value==02 && stday.value>28)  
		{
			alert("Day can't be greater than 28");
			stday.focus();
			return false;
		}
	else if((drpStmonth.value==04 || drpStmonth.value==06 || drpStmonth.value==09 || drpStmonth.value==11) && stday.value>30)  
		{
			alert("Day can't be greater than 30");
			stday.focus();
			return false;
		}
	else if(drpEnyear.value%4==0 && drpEnmonth.value==02 && edday.value>29)  
		{
			
			alert("Day can't be greater than 29");
			edday.focus();
			return false;
		}
	else if(drpEnmonth.value==02 && edday.value>28)  
		{
			alert("Day can't be greater than 28");
			edday.focus();
			return false;
		}
	else if((drpEnmonth.value==04 || drpEnmonth.value==06 || drpEnmonth.value==09 || drpEnmonth.value==11) && edday.value>30)  
		{
			alert("Day can't be greater than 30");
			edday.focus();
			return false;
		}
     
	st_Date = drpStmonth.value +"/"+ stday.value +"/"+ drpStyear.value;
	ed_Date = drpEnmonth.value +"/"+ edday.value +"/"+ drpEnyear.value;
	/*if(parseInt(inv_Amnt.value)<parseInt(MinInvAmt)){
		alert("Investment Amount must be greater than SIP Amount..!");
		inv_Amnt.focus();
		inv_Amnt.value="";
		return false;
	}*/
	if(sch_Code.value==""){
		alert("Kindly Select any Scheme..!");
		sch_Code.focus();
		return false;
	}
	if(SchTypeCode==""){
		alert("This Scheme is not matching with our data..  Kindly Select another Scheme..!");
		sch_Code.focus();
		return false;
	}
	else if(document.getElementById("TxtInvstAmt").value=="")
	{
		alert("Please provide investment amount");
		document.getElementById("TxtInvstAmt").focus();
		return false;
	}
	loading.innerHTML ="<img src=../images/loading1.gif>";
	var SchmPlan = document.getElementById("SchmPlan");
	var InvAmount= document.getElementById("InvAmount");
	var InvPeriod= document.getElementById("InvPeriod");
	var SchmPlanhead =document.getElementById("SchmPlanhead");
	
	SchmPlanhead.innerText = sch_Code.options[sch_Code.selectedIndex].text;
	SchmPlan.innerText = sch_Code.options[sch_Code.selectedIndex].text;
	InvAmount.innerText = inv_Amnt.value;
	InvPeriod.innerText = stday.value +"-"+ drpStmonth.options[drpStmonth.selectedIndex].text +"-"+ drpStyear.value +" To  "+ edday.value +"-"+ drpEnmonth.options[drpEnmonth.selectedIndex].text +"-"+ drpEnyear.value;
	//document.ImgLoad.src="../images/loading.gif";
	document.body.style.cursor = "progress";
	//CreateXmlHttp();
	CreateXmlHttpthree();
	var requestUrl = "SipCalcData.aspx?sch_Code="+ sch_Code.value +"&sch_Type="+ SchTypeCode +"&st_Date="+ st_Date +"&ed_Date="+ ed_Date +"&inv_Amnt="+inv_Amnt.value;
	//alert(requestUrl);
	if(XmlHttp_three){
				XmlHttp_three.onreadystatechange = function(){sipDataResp()}
				XmlHttp_three.open("GET", requestUrl,  true);
				XmlHttp_three.send(null);
			}	
}



//Called when response comes back from server Only For sip data
function sipDataResp()
{

	//var SIPCALC    = document.getElementById("SIPCALC");
	if(XmlHttp_three.readyState == 4)
	{
		if(XmlHttp_three.status == 200)
		{			
			var strData = XmlHttp_three.responseText
			if(strData != "NA")
			{	
				var Tot_InvAmt  = document.getElementById("Tot_InvAmt");
				var Tot_UnitBuy = document.getElementById("Tot_UnitBuy");
				var Inv_ValDate = document.getElementById("Inv_ValDate");
				var Inv_Val	    = document.getElementById("Inv_Val");
				var Return	    = document.getElementById("Return");
				//var Avg_Price   = document.getElementById("Avg_Price");
				//var Act_AvgPrice= document.getElementById("Act_AvgPrice");
				var TotMonth    = document.getElementById("TotMonth");
				var SIPTable    = document.getElementById("SIPTable");
				var arrStr = strData.split("|");
					SIPTable.innerHTML = arrStr[0];
					Tot_InvAmt.innerHTML = arrStr[1];
					Tot_UnitBuy.innerHTML = arrStr[2];
					Inv_ValDate.innerHTML = arrStr[3];
					Inv_Val.innerHTML = arrStr[4];
					Return.innerHTML = arrStr[5];
					//Avg_Price.innerHTML = arrStr[6];
					//Act_AvgPrice.innerHTML = arrStr[7];
					TotMonth.innerHTML = arrStr[8];
				document.getElementById("loading").style.display = 'none';
				tab1.style.display='inline';
				tab2.style.display='inline';
				tab3.style.display='inline';
				SIPTable.style.display='inline';
				NoData.style.display='none';
				//document.ImgLoad.src="../images/spacer.gif";
			}
			else {
				document.getElementById("loading").style.display = 'none';
				NoData.style.display='inline';
				document.getElementById("NoData").innerHTML = "There was a problem retrieving data from the server.";
				//SIPCALC.style.display='inline';
				//SIPCALC.innerHTML="There was a problem retrieving data from the server.";
			}	
		}
	}
	else
	{
		NoData.style.display='inline';
		document.getElementById("NoData").innerHTML = "There was a problem retrieving data from the server.";
	}
	document.body.style.cursor = "auto";
}


