using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using System.Net.Mail;
using System.Net.Sockets;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;

public partial class MobilePortal_MobileAppRech : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    static double WLbalance;
    int Availablebalance, status1, cutoffBalance;
    string WLOpStatus, refrenceno, txn;
    string footerXML = "</response>";
    string headXML = "<?xml version=\"1.0\" ?><response>";    
    string headerXML = "<?xml version=\"1.0\" ?><response>";
    WBMobile_Noverification _ObjWBMNverify = new WBMobile_Noverification();
    string IPAddress = HttpContext.Current.Request.UserHostAddress.ToString(); 
    string agent_Id, txn_key, serviceoperator, service, amount, mobile_no, TXNKEY, url, WLID;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        url = HttpContext.Current.Request.Url.AbsoluteUri;
        //url = "http://kashishstudypoint.in/MobilePortal/MobileAppRech.aspx?agent_Id=1000000005&txn_key=64305663907048&serviceoperator=Vodafone&service=mobile&amount=10&mobile_no=9999999999";
        agent_Id = Request.QueryString["agent_Id"];
        txn_key = Request.QueryString["txn_key"];
        serviceoperator = Request.QueryString["serviceoperator"];
        service = Request.QueryString["service"];
        amount = Request.QueryString["amount"];
        mobile_no = Request.QueryString["mobile_no"];


        //agent_Id = "1000000005";
        //txn_key = "64305663907048";
        //serviceoperator = "Vodafone";
        //service = "mobile";
        //amount = "10";
        //mobile_no = "1234567890";

        //agent_Id = "1000000044";
        //txn_key = "44614762237340";
        //serviceoperator = "VODAFONE";//
        //service = "mobile";
        //amount = "10";
        //mobile_no = "8888888888";//9548737341
        GetOperatorID(serviceoperator);
        GetVendor();
        if (ViewState["ServiceProvider"].Equals("eGenApi"))
        {
            refrenceno = TID_GEN();
        }
        else
        {
            refrenceno = TID_GEN1();
        }
        saveinputoutput();        
        if (agent_Id!=null && serviceoperator!=null)
        {
            GetWLID();
            CheckWLOPStatus();
            if (WLOpStatus.Equals("1"))
            {                             
                GetTxnKey();
                writeXMLTXNKEY(TXNKEY);
                if (txn_key.Equals((TXNKEY)))
                {
                    WLAmount();
                    if (WLbalance > Convert.ToInt32(amount))
                    {
                        userdetails();
                        UserCutOffDetails();
                        if (Availablebalance > cutoffBalance)
                        {
                            if (Availablebalance > Convert.ToInt32(amount))
                            {
                                try
                                {
                                    if (ViewState["ServiceProvider"].Equals("eGenApi"))
                                    {
                                        eGenApi();
                                    }
                                    else if (ViewState["ServiceProvider"].Equals("ExcelRecharge"))
                                    {
                                        ExcelRecharge();
                                    }
                                    else if (ViewState["ServiceProvider"].Equals("IWTApi"))
                                    {
                                        IWTApi();
                                    }
                                    else if (ViewState["ServiceProvider"].Equals("ApiWorld"))
                                    {
                                        ApiWorld();
                                    }
                                    else
                                    {
                                        headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Currently this services is not available !</trans-response-message>" + footerXML;
                                        WriteXML(headXML);
                                    }
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                            else
                            {
                                headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Insufficient Balance!</trans-response-message>" + footerXML;
                                WriteXML(headXML);
                            }
                        }
                        else
                        {
                            headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Your balance is low from cutoff balance!</trans-response-message>" + footerXML;
                            WriteXML(headXML);
                        }
                    }
                    else
                    {
                        headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Your Administrater have Insufficient Balance!</trans-response-message>" + footerXML;
                        WriteXML(headXML);
                    }
                }
                else
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Txn Key Do not Match</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                }
            }
            else
            {
                headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Currently this service is not available.</trans-response-message>" + footerXML;
                WriteXML(headXML);
            }
        }
        else
        {
            headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Invalid request!</trans-response-message>" + footerXML;
            WriteXML(headXML);
        }
    }

    public void saveinputoutput()
    {
        SqlParameter[] inputrequest = new SqlParameter[]
        {
            new SqlParameter("@mobileno",mobile_no),
            new SqlParameter("@url",url),
            new SqlParameter("@txnid",refrenceno)
        };
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_insert_input_request_mobileApps", inputrequest);
    }

    public void CheckWLOPStatus()
    {
        SqlParameter[] getoperatorstatus = new SqlParameter[]
        {
            new SqlParameter("@WLID",WLID),
            new SqlParameter("@operator",serviceoperator),
            new SqlParameter("@ServiceType",service)
        };
        DataSet Opds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_Getoperatorstatus", getoperatorstatus);
        if (Opds.Tables[0].Rows.Count > 0)
        {
            WLOpStatus = Opds.Tables[0].Rows[0]["Flag"].ToString();
        }
        else
        {
            WLOpStatus = "0";
        }
    }

    public void updateresponce(string responce)
    {
        SqlParameter[] inputrequest = new SqlParameter[]
        {
            new SqlParameter("@txnid",refrenceno),
            new SqlParameter("@Responce",responce)
        };
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_update_responce_mobileApps", inputrequest);
    }

    public void writeXMLTXNKEY(string RQ_MRecharge)
    {
        string xml = "<CurrentT><CurrentN>" + TXNKEY + "</CurrentN></CurrentT>";
        DataSet dsxml = new DataSet();
        dsxml.ReadXml(new StringReader(xml));
        dsxml.WriteXml(Server.MapPath("~/MobilePortal/XMLResponce/" + agent_Id + "" + txn_key + "" + ".xml"));
    }

    private void WriteXML(string str)
    {
        updateresponce(str);
        Response.ClearHeaders();
        Response.AddHeader("content-type", "text/xml");
        Response.Write(str);
        Response.End();
    }

    public void eGenApi()
    {
        try
        {                       
            if (service.Equals("DTH") || service.Equals("dth"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator,
                            ViewState["OperatorID"].ToString(), "All CIRCLE", mobile_no, amount, " ", "eGenApi", "Venus Recharge", "DTH Recharge-ADD");
              
                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                   txn = CommonFunction.eGenAPIRecharge("dth", ViewState["eGenApiOpID"].ToString(), "ALL", mobile_no, amount, refrenceno, ViewState["Mobileno"].ToString(), agent_Id);
                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            else if (service.Equals("mobile"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
                                    "All CIRCLE", mobile_no, amount, " ", "eGenApi", "Venus Recharge", "Mobile Recharge-ADD");
                
                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    txn = CommonFunction.eGenAPIRecharge("mobile", ViewState["eGenApiOpID"].ToString(), "ALL", mobile_no, amount, refrenceno, mobile_no, agent_Id);

                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            else if (service.Equals("Data Card"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
                                    "All CIRCLE", mobile_no, amount, " ", "eGenApi", "Venus Recharge", "DataCard Recharge-ADD");
                
                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    txn = CommonFunction.eGenAPIRecharge("datacard", ViewState["eGenApiOpID"].ToString(), "ALL", mobile_no, amount, refrenceno, ViewState["Mobileno"].ToString(), agent_Id);

                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            if (status1 == 2)
            {

                string res = txn;
                if (!(res.Contains("error") || res.Contains("Error")))
                {
                    ds.ReadXml(new StringReader(res));
                    if (ds.Tables["Response"].Rows.Count > 0)
                    {
                        ViewState["resultCode"] = Convert.ToString(ds.Tables["Response"].Rows[0]["resultCode"]);
                        ViewState["Status"] = Convert.ToString(ds.Tables["Response"].Rows[0]["resultValue"]);
                        ViewState["VendorTxnID"] = Convert.ToString(ds.Tables["Response"].Rows[0]["tranId"]);
                        ViewState["operatorId"] = Convert.ToString(ds.Tables["Response"].Rows[0]["operatorId"]);
                        if (ViewState["resultCode"].Equals("125") || ViewState["resultCode"].Equals("121") || ViewState["resultCode"].Equals("122"))
                        {
                            if (ViewState["resultCode"].Equals("125"))
                            {
                                _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Success", "Successfully Completed", txn, ViewState["VendorTxnID"].ToString(), ViewState["operatorId"].ToString());
                                headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + "Successfully Completed" + "</trans-response-message>" + footerXML;
                                WriteXML(headerXML);
                            }
                            else if (ViewState["resultCode"].Equals("121") || ViewState["resultCode"].Equals("122"))
                            {
                                _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Pending", "Teansaction Pending", txn, ViewState["VendorTxnID"].ToString(), ViewState["operatorId"].ToString());
                                headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + "Teansaction Pending" + "</trans-response-message>" + footerXML;
                                WriteXML(headerXML);
                            }
                        }
                        else
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", ViewState["Status"].ToString(), txn, refrenceno, "");
                            headXML += "<trans-response-code>1</trans-response-code><trans-response-message>" + ViewState["Status"].ToString() + "</trans-response-message>" + footerXML;
                            WriteXML(headXML);
                        }
                    }
                }
                else
                {
                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", "Fail", txn, refrenceno, "");
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Fail</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                }
            }
        }
        catch (Exception ex)
        { }           
    }

    public void ExcelRecharge()
    {
        try
        {
            if (service.Equals("DTH") || service.Equals("dth"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator,
                            ViewState["OperatorID"].ToString(), "All CIRCLE", mobile_no, amount, " ", "ExcelRecharge", "Venus Recharge", "DTH Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {                    
                    txn = CommonFunction.ExcelApiRecharge(agent_Id, "19", "123456", amount, mobile_no, ViewState["ExcelRecharge"].ToString(), refrenceno, "", "", "DTH Recharge-ADD");
                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            else if (service.Equals("mobile"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
                                    "All CIRCLE", mobile_no, amount, " ", "ExcelRecharge", "Venus Recharge", "Mobile Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    txn = CommonFunction.ExcelApiRecharge(agent_Id, "19", "123456", amount, mobile_no, ViewState["ExcelRecharge"].ToString(), refrenceno, "", "", "Mobile Recharge-ADD");

                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            else if (service.Equals("Data Card"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
                                    "All CIRCLE", mobile_no, amount, " ", "ExcelRecharge", "Venus Recharge", "DataCard Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    txn = CommonFunction.ExcelApiRecharge(agent_Id, "19", "123456", amount, mobile_no, ViewState["ExcelRecharge"].ToString(), refrenceno, "", "", "DataCard Recharge-ADD");

                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            if (status1 == 2)
            {

                string res = txn;
                if (res.Contains("Pending") || res.Contains("PENDING"))
                {
                    ds.ReadXml(new StringReader(res));
                    if (ds.Tables["Data"].Rows.Count > 0)
                    {
                        ViewState["OrderId"] = Convert.ToString(ds.Tables["Data"].Rows[0]["OrderId"]);
                        ViewState["Status"] = Convert.ToString(ds.Tables["Data"].Rows[0]["Status"]);
                        string TxnID = Convert.ToString(ds.Tables["Data"].Rows[0]["MerOid"]);
                        if (ViewState["Status"].Equals("Success") || ViewState["Status"].Equals("SUCCESS") || ViewState["Status"].Equals("success"))
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Success", ViewState["Status"].ToString(), txn, TxnID, ViewState["OrderId"].ToString());
                            headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + "Successfully Completed" + "</trans-response-message>" + footerXML;
                            WriteXML(headerXML);
                        }
                        else if (ViewState["Status"].Equals("Pending") || ViewState["Status"].Equals("PENDING"))
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Pending", ViewState["Status"].ToString(), txn, TxnID, ViewState["OrderId"].ToString());
                            headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + "Transaction Pending" + "</trans-response-message>" + footerXML;
                            WriteXML(headerXML);
                        }                        
                        else
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", ViewState["Status"].ToString(), txn, refrenceno, "");
                            headXML += "<trans-response-code>1</trans-response-code><trans-response-message>" + ViewState["Status"].ToString() + "</trans-response-message>" + footerXML;
                            WriteXML(headXML);
                        }
                    }
                }
                else
                {
                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", "Fail", txn, refrenceno, "");
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Fail</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                }
            }
        }
        catch (Exception ex)
        { }
    }

    //public void IWTApi()
    //{
    //    try
    //    {
    //        if (service.Equals("DTH") || service.Equals("dth"))
    //        {
    //            status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator,
    //                        ViewState["OperatorID"].ToString(), "All CIRCLE", mobile_no, amount, " ", "IWTApi", "Venus Recharge", "DTH Recharge-ADD");

    //            if (status1 == 1)
    //            {
    //                headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
    //                WriteXML(headXML);
    //                Response.Redirect("MobileAppLogin.aspx");
    //            }
    //            else if (status1 == 2)
    //            {
    //                txn = CommonFunction.IWTApiRecharge(agent_Id, amount, mobile_no, ViewState["IWTOpID"].ToString(), refrenceno, "", "", "MOBILE-WEB");
    //                SqlParameter[] TxnResponce = new SqlParameter[]
    //                {
    //                     new SqlParameter("@TXNRESPONCE",txn),
    //                     new SqlParameter("@REFRENCENO",refrenceno)
    //                };
    //                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
    //            }
    //        }
    //        else if (service.Equals("mobile"))
    //        {
    //            status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
    //                                "All CIRCLE", mobile_no, amount, " ", "IWTApi", "Venus Recharge", "Mobile Recharge-ADD");

    //            if (status1 == 1)
    //            {
    //                headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
    //                WriteXML(headXML);
    //                Response.Redirect("MobileAppLogin.aspx");
    //            }
    //            else if (status1 == 2)
    //            {
    //                txn = CommonFunction.IWTApiRecharge(agent_Id, amount, mobile_no, ViewState["IWTOpID"].ToString(), refrenceno, "", "", "MOBILE-WEB");

    //                SqlParameter[] TxnResponce = new SqlParameter[]
    //                {
    //                     new SqlParameter("@TXNRESPONCE",txn),
    //                     new SqlParameter("@REFRENCENO",refrenceno)
    //                };
    //                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
    //            }
    //        }
    //        else if (service.Equals("Data Card"))
    //        {
    //            status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
    //                                "All CIRCLE", mobile_no, amount, " ", "IWTApi", "Venus Recharge", "DataCard Recharge-ADD");

    //            if (status1 == 1)
    //            {
    //                headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
    //                WriteXML(headXML);
    //                Response.Redirect("MobileAppLogin.aspx");
    //            }
    //            else if (status1 == 2)
    //            {
    //                txn = CommonFunction.IWTApiRecharge(agent_Id, amount, mobile_no, ViewState["IWTOpID"].ToString(), refrenceno, "", "", "MOBILE-WEB");

    //                SqlParameter[] TxnResponce = new SqlParameter[]
    //                {
    //                     new SqlParameter("@TXNRESPONCE",txn),
    //                     new SqlParameter("@REFRENCENO",refrenceno)
    //                };
    //                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
    //            }
    //        }
    //        if (status1 == 2)
    //        {

    //            string res = txn;
    //            ds.ReadXml(new StringReader(res));
    //            if (ds.Tables["RechargeResponse"].Rows.Count > 0)
    //            {
    //                ViewState["ErrorCode"] = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["ErrorCode"]);
    //                ViewState["Desc"] = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["Desc"]);
    //                if (ViewState["ErrorCode"].Equals("00000"))
    //                {
    //                    string TxnID = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["TxnID"]);
    //                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Success", ViewState["Desc"].ToString(), txn, TxnID, "");
    //                    headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + ViewState["Desc"].ToString() + "</trans-response-message>" + footerXML;
    //                    WriteXML(headerXML);
    //                }

    //                else
    //                {
    //                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", ViewState["Desc"].ToString(), txn, refrenceno, "");
    //                    headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + ViewState["Desc"].ToString() + "</trans-response-message>" + footerXML;
    //                    WriteXML(headerXML);
    //                }
    //            }
    //            else
    //            {
    //                _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", "Fail", txn, refrenceno, "");
    //                headXML += "<trans-response-code>1</trans-response-code><trans-response-message>" + "Failed" + "</trans-response-message>" + footerXML;
    //                WriteXML(headXML);
    //            }
                
                                
    //        }
    //    }
    //    catch (Exception ex)
    //    { }
    //}


    public void IWTApi()
    {
        try
        {
            string usertrackid = refrenceno + TID_GEN();
            if (service.Equals("DTH") || service.Equals("dth"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator,
                            ViewState["OperatorID"].ToString(), "All CIRCLE", mobile_no, amount, " ", "IWTApi", "Venus Recharge", "DTH Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    Hermes_Rech_BAL obj_bill = new Hermes_Rech_BAL();
                    txn = obj_bill.mobile_Rech(agent_Id, usertrackid, refrenceno, ViewState["IWTOpID"].ToString(), mobile_no, Convert.ToDouble(amount), "", "DTH-WEB");
                        
                   // txn = CommonFunction.IWTApiRecharge(agent_Id, amount, mobile_no, ViewState["IWTOpID"].ToString(), refrenceno, "", "", "MOBILE-WEB");
                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            else if (service.Equals("mobile"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
                                    "All CIRCLE", mobile_no, amount, " ", "IWTApi", "Venus Recharge", "Mobile Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    Hermes_Rech_BAL obj_bill = new Hermes_Rech_BAL();
                    txn = obj_bill.mobile_Rech(agent_Id, usertrackid, refrenceno, ViewState["IWTOpID"].ToString(), mobile_no, Convert.ToDouble(amount), "", "MOBILE-WEB");
                     
                   // txn = CommonFunction.IWTApiRecharge(agent_Id, amount, mobile_no, ViewState["IWTOpID"].ToString(), refrenceno, "", "", "MOBILE-WEB");

                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            else if (service.Equals("Data Card"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
                                    "All CIRCLE", mobile_no, amount, " ", "IWTApi", "Venus Recharge", "DataCard Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    Hermes_Rech_BAL obj_bill = new Hermes_Rech_BAL();
                    txn = obj_bill.mobile_Rech(agent_Id, usertrackid, refrenceno, ViewState["IWTOpID"].ToString(), mobile_no, Convert.ToDouble(amount), "", "Data Card-WEB");
                     
                   // txn = CommonFunction.IWTApiRecharge(agent_Id, amount, mobile_no, ViewState["IWTOpID"].ToString(), refrenceno, "", "", "MOBILE-WEB");

                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            if (status1 == 2)
            {

                string res = txn;
                string[] result = res.Split('&');
                //ds.ReadXml(new StringReader(res));
                if (Convert.ToInt32(result[0]) == 1)
                {
                    //ViewState["ErrorCode"] = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["ErrorCode"]);
                    //ViewState["Desc"] = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["Desc"]);
                    if (Convert.ToInt32(result[6]) == 1)
                    {
                        ViewState["Desc"] = Convert.ToString(result[2]);
                        string TxnID = Convert.ToString(result[3]);
                        string OperatorTxnId = Convert.ToString(result[7]);
                       // string TxnID = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["TxnID"]);
                        _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Success", ViewState["Desc"].ToString(), txn, TxnID, OperatorTxnId);
                        headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + ViewState["Desc"].ToString() + "</trans-response-message>" + footerXML;
                        WriteXML(headerXML);
                    }
                    else if (Convert.ToInt32(result[6]) == 0 || Convert.ToInt32(result[6]) == 2)
                    {
                        ViewState["Desc"] = Convert.ToString(result[2]);
                        string TxnID = Convert.ToString(result[3]);
                        _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Pending", ViewState["Desc"].ToString(), txn, TxnID, "");
                        headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + ViewState["Desc"].ToString() + "</trans-response-message>" + footerXML;
                        WriteXML(headerXML);
                    }
                    else
                    {
                        _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", ViewState["Desc"].ToString(), txn, refrenceno, "");
                        headerXML += "<trans-response-code>1</trans-response-code><trans-response-message>" + ViewState["Desc"].ToString() + "</trans-response-message>" + footerXML;
                        WriteXML(headerXML);
                    }
                }
                else
                {
                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Pending", "Pending", txn, refrenceno, "");
                    headXML += "<trans-response-code>0</trans-response-code><trans-response-message>Pending</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                }


            }
        }
        catch (Exception ex)
        { }
    }

    public void ApiWorld()
    {
        try
        {
            if (service.Equals("DTH") || service.Equals("dth"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator,
                            ViewState["OperatorID"].ToString(), "All CIRCLE", mobile_no, amount, " ", "ApiWorld", "Venus Recharge", "DTH Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    txn = CommonFunction.ApiWorldRecharge(agent_Id, "19", "DH", amount, mobile_no, ViewState["ApiWorld"].ToString(), refrenceno, "", "", "DTH Recharge-ADD");
                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }
            else if (service.Equals("mobile"))
            {
                status1 = CommonFunction.InsertTransactionWithPendingStatus(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, service, serviceoperator, ViewState["OperatorID"].ToString(),
                                    "All CIRCLE", mobile_no, amount, " ", "ApiWorld", "Venus Recharge", "Mobile Recharge-ADD");

                if (status1 == 1)
                {
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>There is some Technical Problem Please try Again</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                    Response.Redirect("MobileAppLogin.aspx");
                }
                else if (status1 == 2)
                {
                    txn = CommonFunction.ApiWorldRecharge(agent_Id, "19", "MR", amount, mobile_no, ViewState["ApiWorld"].ToString(), refrenceno, "", "", "Mobile Recharge-ADD");

                    SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                         new SqlParameter("@TXNRESPONCE",txn),
                         new SqlParameter("@REFRENCENO",refrenceno)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                }
            }

            if (status1 == 2)
            {

                string res = txn;
                if (res.Contains("PROCESSED") || res.Contains("SUCCESS") || res.Contains("Processed") || res.Contains("Success"))
                {
                    string[] result_array = res.Split(' ');
                    if (result_array.Length > 2)
                    {
                        ViewState["OrderId"] = Convert.ToString(result_array[0]);
                        ViewState["Status"] = Convert.ToString(result_array[1]);
                        string TxnID = Convert.ToString(result_array[3]);
                        if (ViewState["Status"].Equals("Success") || ViewState["Status"].Equals("SUCCESS") || ViewState["Status"].Equals("success"))
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Success", ViewState["Status"].ToString(), txn, ViewState["OrderId"].ToString(), "");
                            headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + "Successfully Completed" + "</trans-response-message>" + footerXML;
                            WriteXML(headerXML);
                        }
                        else if (ViewState["Status"].Equals("Processed") || ViewState["Status"].Equals("PROCESSED"))
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Success", ViewState["Status"].ToString(), txn, ViewState["OrderId"].ToString(), "");
                            headerXML += "<trans-response-code>0</trans-response-code><trans-response-message>" + "Transaction Processed" + "</trans-response-message>" + footerXML;
                            WriteXML(headerXML);
                        }
                        else if (ViewState["Status"].Equals("Failure") || ViewState["Status"].Equals("FAILURE"))
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", ViewState["Status"].ToString(), txn, ViewState["OrderId"].ToString(), "");
                            headXML += "<trans-response-code>1</trans-response-code><trans-response-message>" + ViewState["Status"].ToString() + "</trans-response-message>" + footerXML;
                            WriteXML(headXML);
                        }
                    }
                }
                else
                {
                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, amount, "Fail", "Fail", txn, refrenceno, "");
                    headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Fail</trans-response-message>" + footerXML;
                    WriteXML(headXML);
                }
            }
        }
        catch (Exception ex)
        { }
    }
    private void GetOperatorID(string mobileOperator)
    {
        if (service.Equals("datacard"))
        {
            service = "Data Card";
        }
        SqlParameter[] ONLINEOFFLINEOPERATOR = new SqlParameter[]
        {
            new SqlParameter("@opname", serviceoperator),
            new SqlParameter("@OperatorType",service)
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GETONLINEOFFLINEOPERATOR", ONLINEOFFLINEOPERATOR);
        if (ds.Tables[0].Rows.Count != 0)
        {
            ViewState["OperatorID"] = ds.Tables[0].Rows[0]["MobileDthOperatorDetailsID"];
            ViewState["ServiceType"] = ds.Tables[0].Rows[0]["ServiceType"];
            ViewState["eGenApiOpID"] = ds.Tables[0].Rows[0]["eGenApiOpID"];
            ViewState["ExcelRecharge"] = ds.Tables[0].Rows[0]["ExcelRecharge"];
            ViewState["IWTOpID"] = ds.Tables[0].Rows[0]["IWTOpID"];
            ViewState["ApiWorld"] = ds.Tables[0].Rows[0]["ApiWorld"];
        }
    }


    public void UserCutOffDetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",agent_Id)
            };

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getcutoffbalance", get_user_details);
            if (dr.Read())
            {
                cutoffBalance = Convert.ToInt32(dr["CuttOffAmount"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }
   
    public void userdetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",agent_Id)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getavailablebalance", get_user_details);           
            if (ds.Tables[0].Rows.Count > 0)
            {
               Availablebalance= Convert.ToInt32(ds.Tables[0].Rows[0]["balance"]);               
            }
        }
        catch (Exception ex)
        { }
    }    

    public void GetTxnKey()
    {
        try
        {
            SqlParameter[] gettxnkey = new SqlParameter[]
            {
                new SqlParameter("@userid",agent_Id)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "Usp_GetTxnKey", gettxnkey);
            if (ds.Tables[0].Rows.Count > 0)
            {
                TXNKEY = Convert.ToString(ds.Tables[0].Rows[0]["TXNKEY"]);
                ViewState["Mobileno"] = Convert.ToString(ds.Tables[0].Rows[0]["Mobileno"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }

    public void GetVendor()
    {
        if (service == "Mobile" || service == "mobile")
        {
            SqlParameter[] GetCurrentVendor = new SqlParameter[]
            {
                new SqlParameter("@operator",serviceoperator),
                new SqlParameter("@operatortype","MobileRecharge")
            };
            DataSet dsetGetCurrentVendor = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetServiceVendorOperatorwise_TEST", GetCurrentVendor);
            if (dsetGetCurrentVendor.Tables[0].Rows.Count > 0)
            {
                if (dsetGetCurrentVendor.Tables[0].Rows[0]["OutVal"].ToString().Equals("1"))
                {
                    ViewState["ServiceProvider"] = dsetGetCurrentVendor.Tables[0].Rows[0]["ServiceProvider"].ToString();
                }
                else
                {
                    ViewState["ServiceProvider"] = "none";
                }
            }
            else
            {
                ViewState["ServiceProvider"] = "none";
            }
        }
        else
        {
            SqlParameter[] GetCurrentVendor = new SqlParameter[]
            {
                new SqlParameter("@operator",serviceoperator),
                new SqlParameter("@operatortype",service)
            };
            DataSet dsetGetCurrentVendor = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetServiceVendorOperatorwise_TEST", GetCurrentVendor);
            if (dsetGetCurrentVendor.Tables[0].Rows.Count > 0)
            {
                if (dsetGetCurrentVendor.Tables[0].Rows[0]["OutVal"].ToString().Equals("1"))
                {
                    ViewState["ServiceProvider"] = dsetGetCurrentVendor.Tables[0].Rows[0]["ServiceProvider"].ToString();
                }
                else
                {
                    ViewState["ServiceProvider"] = "none";
                }
            }
            else
            {
                ViewState["ServiceProvider"] = "none";
            }
        }
    }

    public void GetWLID()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@agent_Id",agent_Id)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetWLIDForVB", get_user_details);
            if (ds.Tables[0].Rows.Count > 0)
            {
                WLID = ds.Tables[0].Rows[0]["WLID"].ToString();
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }

    public void WLAmount()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",WLID)
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getavailablebalance", get_user_details);
            if (dr.Read())
            {
                WLbalance = Convert.ToDouble(dr["balance"]);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem please try after some time');"), true);
        }
    }

    public static string TID_GEN()
    {
        string new_tid = System.Guid.NewGuid().ToString();
        new_tid = new_tid.Replace("-", string.Empty).ToLower();
        new_tid = new_tid.Substring(0, 14).Replace("a", "1");
        new_tid = new_tid.Substring(0, 14).Replace("b", "2");
        new_tid = new_tid.Substring(0, 14).Replace("c", "3");
        new_tid = new_tid.Substring(0, 14).Replace("d", "4");
        new_tid = new_tid.Substring(0, 14).Replace("e", "5");
        new_tid = new_tid.Substring(0, 14).Replace("f", "6");
        new_tid = new_tid.Substring(0, 14).Replace("g", "14");
        new_tid = new_tid.Substring(0, 14).Replace("h", "8");
        new_tid = new_tid.Substring(0, 14).Replace("i", "9");
        new_tid = new_tid.Substring(0, 14).Replace("j", "0");
        new_tid = new_tid.Substring(0, 14).Replace("k", "1");
        new_tid = new_tid.Substring(0, 14).Replace("l", "2");
        new_tid = new_tid.Substring(0, 14).Replace("m", "3");
        new_tid = new_tid.Substring(0, 14).Replace("n", "4");
        new_tid = new_tid.Substring(0, 14).Replace("o", "5");
        new_tid = new_tid.Substring(0, 14).Replace("p", "0");
        new_tid = new_tid.Substring(0, 14).Replace("q", "9");
        new_tid = new_tid.Substring(0, 14).Replace("r", "8");
        new_tid = new_tid.Substring(0, 14).Replace("s", "14");
        new_tid = new_tid.Substring(0, 14).Replace("t", "6");
        new_tid = new_tid.Substring(0, 14).Replace("u", "5");
        new_tid = new_tid.Substring(0, 14).Replace("v", "4");
        new_tid = new_tid.Substring(0, 14).Replace("w", "3");
        new_tid = new_tid.Substring(0, 14).Replace("x", "2");
        new_tid = new_tid.Substring(0, 14).Replace("y", "1");
        new_tid = new_tid.Substring(0, 14).Replace("z", "0");
        return new_tid;
    }

    public static string TID_GEN1()
    {
        string new_tid = System.Guid.NewGuid().ToString();
        new_tid = new_tid.Replace("-", string.Empty).ToLower();
        new_tid = new_tid.Substring(0, 16).Replace("a", "1");
        new_tid = new_tid.Substring(0, 16).Replace("b", "2");
        new_tid = new_tid.Substring(0, 16).Replace("c", "3");
        new_tid = new_tid.Substring(0, 16).Replace("d", "4");
        new_tid = new_tid.Substring(0, 16).Replace("e", "5");
        new_tid = new_tid.Substring(0, 16).Replace("f", "6");
        new_tid = new_tid.Substring(0, 16).Replace("g", "7");
        new_tid = new_tid.Substring(0, 16).Replace("h", "8");
        new_tid = new_tid.Substring(0, 16).Replace("i", "9");
        new_tid = new_tid.Substring(0, 16).Replace("j", "0");
        new_tid = new_tid.Substring(0, 16).Replace("k", "1");
        new_tid = new_tid.Substring(0, 16).Replace("l", "2");
        new_tid = new_tid.Substring(0, 16).Replace("m", "3");
        new_tid = new_tid.Substring(0, 16).Replace("n", "4");
        new_tid = new_tid.Substring(0, 16).Replace("o", "5");
        new_tid = new_tid.Substring(0, 16).Replace("p", "0");
        new_tid = new_tid.Substring(0, 16).Replace("q", "9");
        new_tid = new_tid.Substring(0, 16).Replace("r", "8");
        new_tid = new_tid.Substring(0, 16).Replace("s", "7");
        new_tid = new_tid.Substring(0, 16).Replace("t", "6");
        new_tid = new_tid.Substring(0, 16).Replace("u", "5");
        new_tid = new_tid.Substring(0, 16).Replace("v", "4");
        new_tid = new_tid.Substring(0, 16).Replace("w", "3");
        new_tid = new_tid.Substring(0, 16).Replace("x", "2");
        new_tid = new_tid.Substring(0, 16).Replace("y", "1");
        new_tid = new_tid.Substring(0, 16).Replace("z", "0");
        return new_tid;
    }
}