﻿// Code for Creating the XMLHTTP Obj
var xmlReq = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xmlIndex = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
xmlIndex[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE


function xhrRequest(type) 
{
    if (!type) 
    {
        type = 'html';
    }
    // xhrsend IS THE xmlIndex POSITION THAT GETS PASSED BACK
    // INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
    var xhrsend = xmlIndex.length;

    // GO THROUGH AVAILABLE xi VALUES
    for (var i=0; i<xmlIndex.length; i++) 
    {
        // IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
        if (xmlIndex[i] == 1) 
        {
            xmlIndex[i] = 0;
            xhrsend = i;
            break;
        }
    }
    // SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
    xmlIndex[xhrsend] = 0;


    // SET UP THE REQUEST
    if (window.ActiveXObject) 
    {
        try 
        {
            xmlReq[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try 
            {
                xmlReq[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) 
            {
                
            }
        }   
    } 
    else if (window.XMLHttpRequest) 
    {
        xmlReq[xhrsend] = new XMLHttpRequest();
        if (xmlReq[xhrsend].overrideMimeType) 
        {
            xmlReq[xhrsend].overrideMimeType('text/' + type);
        }
    }
    return (xhrsend);
}


function loadData()
    {
    //debugger;
    
    LoadRequest('AJAX/Profit.aspx?id=ScriptName');
    }


function LoadRequest(url, reqType)
    {
    //debugger; 
    
    var xhri = xhrRequest('html');
    xmlReq[xhri].open('GET', url, true);
    xmlReq[xhri].onreadystatechange = function() 
    {
        if(xmlReq[xhri].readyState == 3 && url == "AJAX/Profit.aspx?id=ScriptName")
        {
            var ddlSN = document.getElementById('ctl00_cphCalc_ddlScriptName');
           if (ddlSN == 'null')
           {
            (ddlSN).length = 0;  
            ddlSN.options[0] = new Option("Loading...","Loading...");
            document.getElementById('ctl00_cphCalc_ddlScriptName').selectedIndex = 0;
            }
            
        }
        if (xmlReq[xhri].readyState == 4 && xmlReq[xhri].status == 200) 
        {  
            if(url == "AJAX/Profit.aspx?id=ScriptName")
            {   
                var ddlSN = document.getElementById('ctl00_cphCalc_ddlScriptName');
                var Output = xmlReq[xhri].responseText; 
                
                if(Output != '')
                {
                    var arr = Output.split(";");    
                    //To Remove All Items of Drop Down
                    (ddlSN).length = 0;  
                    //To Add Items In Drop Down
                    for(var i = 0; i < arr.length; i++)
                    {
                        (ddlSN).options[i+1] = new Option(arr[i].split("##")[1],arr[i].split("##")[0]);
                    }
                    ddlSN.options[0] = new Option("-- Select Script Name --","-- Select Script Name --");
                    document.getElementById('ctl00_cphCalc_ddlScriptName').selectedIndex = 0;
                }
                else
                {
                    ddlSN.options[0] = new Option("-- No Data --","-- No Data --");
                }            
                
            }
        }
    };
    xmlReq[xhri].send(null);
    }


function loadScriptLTP()
{        
//debugger;    
    
    var xhri = xhrRequest('html');
    var url = "AJAX/Profit.aspx"; 
    url = url + "?sc_name=" + document.getElementById('ctl00_cphCalc_ddlScriptName').value +"&id=scriptLTP";
    xmlReq[xhri].open('GET', url, true);
    xmlReq[xhri].onreadystatechange = function() 
    {                     
        if (xmlReq[xhri].readyState == 3 && url == "AJAX/Profit.aspx?sc_name=" + document.getElementById('ctl00_cphCalc_ddlScriptName').value +"&id=scriptLTP")
        {  
            var txtSLTP = document.getElementById('ctl00_cphCalc_txtScriptLTP');          
            (txtSLTP).length = 0;
        }
         
        if (xmlReq[xhri].readyState == 4 && xmlReq[xhri].status == 200) 
        { 
            if(url == "AJAX/Profit.aspx?sc_name=" + document.getElementById('ctl00_cphCalc_ddlScriptName').value +"&id=scriptLTP")
            {            
                var Output = xmlReq[xhri].responseText; 
                var txtSLTP = document.getElementById('ctl00_cphCalc_txtScriptLTP');
                
                if(Output != '')
                {
                    
                    var arr = Output.split(";");    
                    //To Remove The Items of Text Box
                    (txtSLTP).length = 0;   
                    //To Add Items In Text Box
                    for(var i = 0; i < arr.length; i++)
                    {
                        (txtSLTP)= (arr[i].split("##")[0],arr[i].split("##")[1]);
			(txtSLTP)= txtSLTP.slice(0, -2);
                    }
                    document.getElementById('ctl00_cphCalc_txtScriptLTP').value = txtSLTP;
                }
                else
                {
                    document.getElementById('ctl00_cphCalc_txtScriptLTP').value = ("-- No Data --");
                }  
            }
        }
    };
    xmlReq[xhri].send(null); 
}

   


