//when source 1 is changed, the associated objects and entities are added to their respective ddls
    protected void SourceDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        // clears the drop down lists when the user changes the source
        // (OS)
        ObjectDropDown.Items.Clear();
        CounterDropDown.Items.Clear();
        ServerDropDown.Items.Clear();
        ResultsListBox.Items.Clear();

        // (TB)
        CorrelatorService service = new CorrelatorService();

        List<OCMObject> objects = service.getObjects(int.Parse(SourceDropDown.SelectedValue));
        // add items to 1st object list, dependent on source
        foreach (OCMObject obj in objects)
            ObjectDropDown.Items.Add(new ListItem(obj.name, obj.name));

           List<OCMEntity> entities = service.getEntities(int.Parse(SourceDropDown.SelectedValue));
        // add items to 1st entity list, dependent on source
           foreach (OCMEntity entity in entities)
        ServerDropDown.Items.Add(new ListItem(entity.name, entity.id.ToString()));
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // Gets value that indicates whether the page is rendered for the first time or is being loaded in response to a post back.
        // (TB)
        if (IsPostBack) return;

        CorrelatorService service = new CorrelatorService();

        // uses WCF web service to get the sources
        List<Source> sources = service.getSources();

        // add items to 1st source list
        foreach (Source source in sources)
            SourceDropDown.Items.Add(new ListItem(source.name, source.id.ToString()));
        // add items to 2nd source list
        foreach (Source source in sources)
            SourceDropDown2.Items.Add(new ListItem(source.name, source.id.ToString()));
    }
    // When source 2 is changed, the associated objects are shown in the 2nd object ddl
    // (OS)
    protected void SourceDropDown2_SelectedIndexChanged(object sender, EventArgs e)
    {
        ObjectDropDown2.Items.Clear();
        CounterDropDown2.Items.Clear();
        ServerDropDown2.Items.Clear();
        ResultsListBox2.Items.Clear();

        CorrelatorService service = new CorrelatorService();

        List<OCMObject> objects = service.getObjects(int.Parse(SourceDropDown2.SelectedValue)); //using value from SourceDropDown2
        //add items to second Objects list
        foreach (OCMObject obj in objects)
        ObjectDropDown2.Items.Add(new ListItem(obj.name, obj.name));

        List<OCMEntity> entities = service.getEntities(int.Parse(SourceDropDown2.SelectedValue));
        //add items to 1st entity list, dependent on source
        foreach (OCMEntity entity in entities)
            ServerDropDown2.Items.Add(new ListItem(entity.name, entity.id.ToString()));
    }
    // When object 1 is changed, the associated counters are shown in the counter ddl
    // (OS)
    protected void ObjectDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        CounterDropDown.Items.Clear();
        CorrelatorService service = new CorrelatorService();

        List<OCMCounter> counters = service.getCounters(int.Parse(SourceDropDown.SelectedValue));
        //add items to 1st counter list
        foreach (OCMCounter counter in counters)
            CounterDropDown.Items.Add(new ListItem(counter.name));
    }
    // Calculate correlation between the two sets of values
    public void Button1_Click(object sender, EventArgs e)
    {
        // (OS)
        CorrelatorService service = new CorrelatorService();

        List<OCMRawData> rawdata = service.getRawData(int.Parse(SourceDropDown.SelectedValue), CounterDropDown.SelectedValue, int.Parse(ServerDropDown.SelectedValue));
        List<OCMRawData> rawdata2 = service.getRawData(int.Parse(SourceDropDown2.SelectedValue), CounterDropDown2.SelectedValue, int.Parse(ServerDropDown2.SelectedValue));

        // (AM)
        double[] array1 = colData(rawdata);
        double[] array2 = colData(rawdata2);

        // (AM & OS)
        double[] arr = new double[rawdata.Count];
        double[] arr2 = new double[rawdata2.Count];

        // (The following (START-END) is taken from the following website: http://mantascode.com/c-how-to-get-correlation-coefficient-of-two-arrays/ )
        // What is used to work out one- to- one correlation
        double[] array_xy = new double[arr.Length];
        double[] array_xp2 = new double[arr.Length];
        double[] array_yp2 = new double[arr2.Length];

        for (int i = 0; i < arr.Length; i++)
            array_xy[i] = arr[i] * arr2[i];

        for (int i = 0; i < arr.Length; i++)
            array_xp2[i] = Math.Pow(arr[i], 2.0);

        for (int i = 0; i < arr2.Length; i++)
            array_yp2[i] = Math.Pow(arr2[i], 2.0);

        double sum_x = 0;
        double sum_y = 0;

        foreach (double n in arr)
            sum_x += n;

        foreach (double n in arr2)
            sum_y += n;

        double sum_xy = 0;
        foreach (double n in array_xy)
            sum_xy += n;
        double sum_xpow2 = 0;
        foreach (double n in array_xp2)
            sum_xpow2 += n;
        double sum_ypow2 = 0;
        foreach (double n in array_yp2)
            sum_ypow2 += n;
        double Ex2 = Math.Pow(sum_x, 2.00);
        double Ey2 = Math.Pow(sum_y, 2.00);

        // The actual correlation calculation
        double Correl =
        (arr.Length * sum_xy - sum_x * sum_y) /
        Math.Sqrt((arr.Length * sum_xpow2 - Ex2) * (arr.Length * sum_ypow2 - Ey2));

        // Display correlation coefficient of one to one
        correlationtextbox.Text = ("CORREL : "+ Correl);

        // Idea of correlation algorithm before searching online for help..
        // (OS)

        // strongcorrelation = false

        // get 1st entity instance from user, store in variable
        // get second entity instance from user, store in variable
        // get counter from user, store in variable

        // get total (t) value of each array

        // while traversing through 1st array
             //traverse through second.

        // if 2nd array value is > x and 1st array value > x
        // then increment by 1. increments stored in i

        // then we can say if i > (0.75 x t), 3/4 being the threshold of a somewhat 'good' correlation

        // calculate correlation, store value in variable

        // strongcorrelation = true
    }
    // When counter 1 is changed, the associated entities are shown in the object ddl
    // (OS)
    protected void CounterDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        CorrelatorService service = new CorrelatorService();

        List<OCMEntity> entities = service.getEntities(int.Parse(SourceDropDown.SelectedValue));
        //add items to 1st entity list
        foreach (OCMEntity entity in entities)
            ServerDropDown.Items.Add(new ListItem(entity.name, entity.id.ToString()));
    }
    // When counter 2 is changed, the associated objects are stored in the 2nd object ddl
    // (OS)
    protected void CounterDropDown2_SelectedIndexChanged(object sender, EventArgs e)
    {
        CorrelatorService service = new CorrelatorService();

        List<OCMEntity> entities = service.getEntities(int.Parse(SourceDropDown2.SelectedValue));
        //add items to second object list, dependent on the source
        foreach (OCMEntity entity in entities)
            ObjectDropDown2.Items.Add(new ListItem(entity.name, entity.id.ToString()));
    }
    // When server (entity) is changed the values in the 1st list box change
    // (OS)
    public void ServerDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        CorrelatorService service = new CorrelatorService();
           ResultsListBox.Items.Clear();

           List<OCMRawData> rawdata = service.getRawData(int.Parse(SourceDropDown.SelectedValue), CounterDropDown.SelectedValue, int.Parse(ServerDropDown.SelectedValue));
           //add items to 1st results list
         foreach (OCMRawData rw in rawdata)
            ResultsListBox.Items.Add(new ListItem(rw.value.ToString()));
    }