﻿// JScript File
var communitystrWatermark = "Consult the Community. Enter a question or comment here.";
var fundlookupstrWatermark="Enter Scheme Name";
var specialchars=new Array('\'','%','\\','&','<','>','*','^','#');

var rootpath="";
function showCategories()
{
  $.ajax({
  type:"POST",
  contentType:"application/json; charset=uft-8",
  url:rootpath+"/themes/yogi/webservices/MutualFunds/IYFundComparison.asmx/GetCategories",
  data:"{}",
  dataType:"json",
  success:function(data)
  {
     $('#MainCategoryTabs').html(data);
  },
  error:function(XMLHttpRequest, textStatus, errorThrown)
  {
    alert("Error Occured!"+XMLHttpRequest.responseText);
  }
  });
}
function showSubCategories(position,partialname,count)
{
for(i=1;i<=count;i++)
{
    var tabctn=$("#"+partialname+"_"+i);
    var tab=$("#"+partialname+"_"+i+"m");
    if(i==position)
    {
    tabctn.show();
    tab.attr("class","mfequity_t_a");
    }
    else
    { 
    tabctn.hide();
    }
}
}

function getCategoryFunds(ele,divid,name)
{
var f=$("#c"+ele);
if(f.length==0)
{
$.ajax({
type:"POST",
contentType:"application/json; charset=uft-8",
url:rootpath+"/themes/yogi/webservices/MutualFunds/IYFundComparison.asmx/GetFunds",
data:"{'categoryname':'"+name+"','contentid':'"+ele+"'}",
dataType:"json",
async:false,
success:function(data)
{
 var divele=$('#'+divid);
 var htmlstr=divele.html();
 divele.html(htmlstr+data);
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
//alert("Error Occured!"+XMLHttpRequest.responseText);
}
});
}
}

function showFunds(eleid)
{
var content=$('#c'+eleid);
var count=$("[id*=diva_]").length;
$("#"+eleid).attr("class","mfequity_t_a");
content.show();
var str=eleid.split('_');
for(i=1;i<=count;i++)
{
var id=str[0]+"_"+i;var tab=$("#"+id);var ctn=$("#c"+id);
if(eleid!=id)
{
tab.removeAttr("class");
ctn.hide();
}
}
}

function showImage(self,css)
{
  $(self).attr("class",css);
}

function getFunds(self)
{
var v=$(self).val();var ele=$("#MFBuyerFundName");

if(v!="-1")
{
 ele.html("");
 ele.append(
        $('<option></option>').val("").html("Please wait..."));
 $('#buyimgbtn').attr("disabled","disabled");
 $.ajax({
type:"POST",
contentType:"application/json; charset=uft-8",
url:rootpath+"/themes/yogi/webservices/MutualFunds/IYFundComparison.asmx/GetFundIdsAndNames",
data:"{'id':'"+v+"'}",
dataType:"json",
success:function(data)
{
 ele.html("");
 var obj=eval("("+data+")");
 processObj(obj,ele);
 $('#buyimgbtn').removeAttr("disabled");
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
alert("Error Occured!"+XMLHttpRequest.responseText);
}
});
}
else
{
ele.html("");
}
}

function sendBuyMutualFundRequest()
{
var amcnameele=$("[id$=ucBuyaFund_MFBuyerAmcName] option:selected");var fnameele=$("#MFBuyerFundName option:selected");
var quantityele=$("#MFBuyerFundQuantity");var nameele=$("#MFBuyerName");var emailidele=$("#MFBuyerEmailId");
var mobilenoele=$("#MFBuyerMobileNo");var addressele=$("#MFBuyerAddress");
var f=true;var msg="";var emailflag=validateEmail(emailidele.val());var phoneflag=validatePhone(mobilenoele.val());
if(amcnameele.text()=="Choose an AMC")
{
 f=false;
 msg+="Choose an AMC\n";
}
if(emailflag==false || emailidele.val()=="")
{
emailflag=false;
msg+="Enter valid Email Address\n";
}
if(phoneflag==false)
{
msg+="Enter valid Mobile Number\n";
}
if(f==false || emailflag==false|| phoneflag==false)
{
topErrDiv.html("");bottomErrDiv.html("");
alert(msg); 
}
else if(f==true && emailflag==true && phoneflag==true)
{
var mfgblock=$('#MFBuyLoadingMsg');
mfgblock.show();
var param='[{"compnayname":"'+amcnameele.text()+'","fundname":"'+fnameele.text()+'","quantity":"'+quantityele.val()+'","buyername":"'+nameele.val()+'","emailid":"'+emailidele.val()+'","mobileno":"'+mobilenoele.val()+'","address":"'+addressele.val()+'"}]';
$.ajax({
type:"POST",
contentType:"application/json; charset=uft-8",
url:rootpath+"/themes/yogi/webservices/MutualFunds/IYFundComparison.asmx/SendRequetToBuyaMF",
data:"{'values':'"+param+"'}",
dataType:"json",
success:function(data)
{
 alert(data);
 if(data!="fail")
 {
   quantityele.val("");
   nameele.val("");
   emailidele.val("");
   mobilenoele.val("");
   addressele.val("");
 }
 mfgblock.hide();
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
alert("Error Occured!"+XMLHttpRequest.responseText);mfgblock.hide();
}
});
}
}

function processObj(obj,ele)
{
  for(var i=0;i<obj.length;i++)
  {
    ele.append(
        $('<option></option>').val(obj[i].fid).html(obj[i].name));
  }
}

function addTextCell(parentElem, _title, _cssText) {
    var tdElem = document.createElement('td');
    if(_title != null) tdElem.innerHTML = _title;
    if(_cssText != null) tdElem.style.cssText = _cssText;
    parentElem.appendChild(tdElem);
    return tdElem; 
}

function buidTable(_id,celpad,celsp,_width,_class,_table)
{
  var tb=document.createElement('tbody');
  _table.id=_id;
  if(celpad!=null)
  _table.setAttribute("cellpadding",celpad);
  if(celsp!=null)
  _table.setAttribute("cellspacing",celsp);
  if(_width!=null)
  _table.setAttribute("width",_width);
  if(_class!=null)
  {_table.setAttribute("class",_class); _table.setAttribute("className",_class);}
  _table.appendChild(tb);
  return tb;
}

function buidFundRatingHeader(parentElem)
{
   var trfundsheader=document.createElement('tr');
   trfundsheader.setAttribute("class","mfblockheader");
   trfundsheader.setAttribute("className","mfblockheader");
   trfundsheader.style.cssText="text-align:left;line-height:0px;";
   var tdName=addTextCell(trfundsheader,"Name","width:275px;padding-top:5px;");
   var tdRating=addTextCell(trfundsheader,"Rating","padding-top:5px;");
   var tdNav=addTextCell(trfundsheader,"NAV (Rs.)","padding-top:5px;");
   var tdReturn1Yr=addTextCell(trfundsheader,"1 Year Return (%)","padding-top:5px;");
   parentElem.appendChild(trfundsheader);
}

function getfundratings(ratingid,categoryid)
{
var loadingele=$('#FundRatingLoadingBlock');
var mfgblock=$('#FundRatingsBlock');
mfgblock.html("");
loadingele.show();
var ratingele=$("[id$="+ratingid+"]");
var categoryele=$("[id$="+categoryid+"]");
var categoryval=categoryele.val();
if(categoryval=="-1")
categoryval="";
 $.ajax({
type:"POST",
contentType:"application/json; charset=uft-8",
url:rootpath+"/themes/yogi/webservices/MutualFunds/IYFundComparison.asmx/GetFundRatings",
data:"{'rating':'"+ratingele.val()+"','category':'"+categoryval+"'}",
dataType:"json",
success:function(data)
{
 var obj=eval("("+data+")");
 var divele=document.createElement('div');
 var len=obj.length;
 if(len>0)
 {
  for(var i=0;i<obj.length;i++)
  {
   var table=document.createElement('table');
   var tb=buidTable("FundRating_"+i,"5","0","100%","mfblockborder",table);
   table.style.cssText="padding:0px;margin-left:0px";
   var h1ele=document.createElement('h1');
   h1ele.innerHTML=obj[i].Category;
   h1ele.style.cssText="display:none";
   buidFundRatingHeader(tb);
   for(var j=0;j<(obj[i].Funds).length;j++)
  {
     var trfunds=document.createElement('tr');
     if((j%2)==0)
     {trfunds.setAttribute("class","admbg_n");trfunds.setAttribute("className","admbg_n");}
     else
     {trfunds.setAttribute("class","admbg");trfunds.setAttribute("className","admbg");}
     var tdName=addTextCell(trfunds,"",null);
     var fundName=(obj[i].Funds)[j].FundName;
     var achName=document.createElement('a');
     achName.innerHTML=fundName;
     for(var k=0;k<specialchars.length;k++)
    {
     if(fundName.indexOf(specialchars[k])>=0)
     fundName=fundName.replace(specialchars[k],'');
    }
     achName.setAttribute("href","FundDetails/"+fundName+".aspx?"+"FundId="+(obj[i].Funds)[j].Fid);
     tdName.appendChild(achName);
     var tdRating=addTextCell(trfunds,"",null);
     var imgele=document.createElement("img");
     var ratingimgpath=(obj[i].Funds)[j].Rating;
     imgele.setAttribute("src",ratingimgpath);
     tdRating.appendChild(imgele);
     var tdNav=addTextCell(trfunds,(obj[i].Funds)[j].NAV,null);
     var tdreturn1yr=addTextCell(trfunds,(obj[i].Funds)[j].Return1Year,null);
    tb.appendChild(trfunds);
  }
  divele.appendChild(h1ele);
  divele.appendChild(table);
  }
  }
  else
  {
    var table1=document.createElement('table');
    var tb1=buidTable("FundRating_"+i,"5","0","100%","grid",table1);
    buidFundRatingHeader(tb1);
    var tr1=document.createElement('tr');
    var td1=addTextCell(tr1,"No Funds found with rating "+ratingele.val(),null);
    td1.setAttribute("colspan","4");
    tb1.appendChild(tr1);
    divele.appendChild(table1);
  }
  mfgblock.html("");
  mfgblock.append(divele);
  loadingele.hide();
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
alert("Error Occured!"+XMLHttpRequest.responseText);loadingele.hide();
}
});
}

function WatermarkFocus(txtElem, strWatermark) 
{
var txtElement=$(txtElem);
  if (txtElement.val() == strWatermark)
  {
      txtElement.val("");
      txtElement.css("color","");
  }
}
function WatermarkBlur(txtElem, strWatermark) 
{
var txtElement=$(txtElem);
  if (txtElement.val() == "") 
   {
       txtElement.val(strWatermark);
       txtElement.css("color","#808080");
   }
}

//Fund Comparison

function GetDeveloperList_Callback(ele)
{
  var self=$(ele);
  var wrkimgele=$("#WorkingImg");
  $('[id*=CateFundsListBox] option').remove();
  $('[id*=SelectedFundsListBox] option').remove();
  var funddetails=$('#funddetails');
  funddetails.hide();
  None();
   
  if(self.val() != "-1" )
  {
    wrkimgele.show();
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: rootpath+'/themes/yogi/WebServices/MutualFunds/AMCFundList.asmx/AMCDropDownList_SelectedIndexChanged',
    data: "{'selectedvalue':'" + self.val() + "'}",
    dataType:"clientSide",
    success:
    function(data){
        var myObj = eval( '(' + data + ')' );
        if(myObj!= null && typeof(myObj) == "object" && myObj.Funds.length >0)
        {
         var listboxele=$('[id*=CateFundsListBox]');
         fillData(myObj,listboxele);
         wrkimgele.hide();
        }else
        {
         wrkimgele.hide();
         alert("No data to display");
        }
    },
    error:
    function(XMLHttpRequest, textStatus, errorThrown){
       alert( "Error Occured!"+XMLHttpRequest.responseText );
    }
   });
  }
}

function CompareCateFunds_Clicked()
{
    try
    {       
        var $dd = $('[id*=SelectedFundsListBox]');
        var funddetails=$('#funddetails');
        funddetails.hide();
        // get the options and loop through them
        var $options = $('option', $dd);
        if($options.length > 1)
        {
            var fundcompareele=$("#fundcompare");
            var wrkimgele=$("#WorkingImg");
            var strData="",str1value=""
            $("#Graph").hide();
            fundcompareele.hide();
            $("[id*=GraphDropDownList1]").hide();
            wrkimgele.show();
            setTimeout(function(){
            $options.each(function(i)
            {
            if($options[i].text !="" && $options[i].text!=null)
            {
            strData += (escape($options[i].text)+"#");
            str1value+=($options[i].value+"#");
            }
            });//       
            $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: rootpath+'/themes/yogi/WebServices/MutualFunds/IYFundComparison.asmx/CompareCateFunds_Click',
            data: "{'hidenText':'" + strData + "','hidenvalue':'" + str1value + "'}",
            success:
            function(msg){
            fundcompareele.html(eval(msg));
            },
            error:
            function(XMLHttpRequest, textStatus, errorThrown){
            alert( "Error Occured!"+XMLHttpRequest.responseText );
            }
            });
            fundcompareele.show();
            wrkimgele.hide();
            funddetails.show();
            }, 1);
        }
        else{
            alert("Add fund's from the List")
        }
    }
    catch(e)
    {
    alert(e.message)
    }
}
function GraphCateFunds_Clicked()
{
    try
    {
        var fundcompareele=$("#fundcompare");
        var graphele=$("#Graph");
        var wrkimgele=$("#WorkingImg");
        wrkimgele.show();
        fundcompareele.hide();
        graphele.hide();
        var graphddlele=$("[id*=GraphDropDownList1]");
        setTimeout(function(){
        var strData="",str1value=""
        var $dd = $('[id*=SelectedFundsListBox]');
        // get the options and loop through them
        var $options = $('option', $dd);
        $options.each(function(i)
        {
            if($options[i].text !="" && $options[i].text!=null)
            {
            strData += (escape($options[i].text)+"#");
            str1value+=($options[i].value+"#");
            }
        });
        var path="~/Themes/yogi/Controls/Charts/MutualFundsCharts.ascx";//we have to  pass the location of the User Control
        $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: rootpath+'/themes/yogi/WebServices/MutualFunds/IYFundComparison.asmx/DisplayChart',
        data: "{'hidenText':'" + strData + "','hidenvalue':'" + str1value + "','SelectedValue':'" + graphddlele.find('option').filter(":selected").val() + "','path':'"+path+"'}",
        success:
        function(msg){
        graphele.html(eval(msg));
        },
        error:
        function(XMLHttpRequest, textStatus, errorThrown){
        alert( "Error Occured!"+XMLHttpRequest.responseText );
        }
        });
        fundcompareele.show();
        graphddlele.show();
        graphele.show();
        wrkimgele.hide();
        }, 1);
    }
    catch(e)
    {
        alert(e.message)
    }
}
function None()
{
     $("#fundcompare").hide();
     $("#Graph").hide();
     $("[id*=GraphDropDownList1]").hide();
}

//Fund Nav
function GetCatDeveloperList_Callback()
{
    var fundsele=$("[id*=FundDropDownList2] option:selected");
    var wrkimgele=$("#WorkingImg");
    if(fundsele.val()!="-1")
    {
        $("#tableDisplay").html("");
        wrkimgele.show();
        $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: rootpath+'/themes/yogi/WebServices/MutualFunds/IYFundNavReport.asmx/Getfund',
        data: "{'pFundId':'" + fundsele.val() + "'}",
        success:
        function(data){
            var myObj = eval( '(' + data + ')' );
            var navlinkele=$("[id*=NavLinkButton1]");
            $("[id*=NavLabel]").html(" NAV");
            navlinkele.show();
            $("[id*=NavLinkButton2]").hide();
            $("[id*=DetailsView1]").hide();
            navlinkele.html(fundsele.text());
            $("#tableDisplay").html(myObj);
            wrkimgele.hide();
            },
        error:
        function(XMLHttpRequest, textStatus, errorThrown){
        wrkimgele.hide();
        alert( "Error Occured!"+XMLHttpRequest.responseText );
        }
        });
    }
    else
    {
        clear();
    }
}
function GetDrop2Values(val)
{
    clear();
    var response,ds
    var fundsddlele=$("[id$=FundDropDownList2]");
    var amcele=$("[id*=AMCDropDownList1] option:selected");
    var wrkimgele=$("#WorkingImg");
    $('[id*=FundDropDownList2] option').remove();
    $("[id*=NavLinkButton2]").hide();
    $("[id*=DetailsView1]").hide();
    if(amcele.val() != -1 )
    {
        document.title=(amcele.text());
        wrkimgele.show();
        $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: rootpath+'/themes/yogi/WebServices/MutualFunds/AMCFundList.asmx/AMCDropDownList_SelectedIndexChanged',
        data: "{'selectedvalue':'" + amcele.val() + "'}",
        success:
        function(data){
            var myObj = eval( '(' + data + ')' );
            if(myObj!= null && typeof(myObj) == "object")
            {
            fillData(myObj,fundsddlele);
            if(val!="")
            {
              fundsddlele.val(val);
            }
             fundsddlele.attr("selected","selected");
             GetCatDeveloperList_Callback();
             wrkimgele.hide();
            }
        },
        error:
        function(XMLHttpRequest, textStatus, errorThrown){
        alert( "Error Occured!"+XMLHttpRequest.responseText ); wrkimgele.hide();
        }
        });
    }
}

function fillData(ds,ele)
{
    for(var i=0; i<ds.Funds.length; i++)
    ele.append($("<option></option>").attr("value",ds.Funds[i].FundID).text(ds.Funds[i].FundName));
}

function clear()
{
   $("#tableDisplay").html("");
   $("[id*=NavLabel]").html("");
   $("[id*=LinkButton1]").html("");
}


function ShowDetails()
{
   var ele=$("[id*=FundDropDownList2]");
   var fundName=ele.find('option').filter(':selected').text();
   for(var i=0;i<specialchars.length;i++)
   {
     if(fundName.indexOf(specialchars[i])>=0)
     fundName=fundName.replace(specialchars[i],'');
   }
   window.location=rootpath+"/MutualFunds/FundDetails/"+fundName+".aspx?FundId="+ele.val();
}

function showCompareFundDetails()
{
  CompareCateFunds_Clicked();
  GraphCateFunds_Clicked();
}