$(function() { 
    
       $("input#graph").click(function(event){   
                var length = $('#length :selected').text();
                var seriesid = $('#seriesid').val();
                var airflow = $('#airflow :selected').text();    
                var degcw_passive = $('#degcw_passive').val();
                var degcw_forced = $('#degcw_forced').val();
                var params = 'length='+length+'&seriesid='+seriesid+'&action=calculate&airflow='+airflow+'&degcw_passive='+degcw_passive+'&degcw_forced='+degcw_forced;
                $.ajax({
                type: "POST",
                url: "ajax.php",
                data: params,
                    success : function(resp) {    
                    var results = resp.split('__');
                    $('#coolingvalspassive').html(results[0]);
                    $('#coolingvalsforced').html(results[1]);
                    }
                }); 
                get_data();
       });
       
       $("input#getproducts").click(function(event){
                var device = $('#devices :selected').val();
                var params = 'device='+device+'&action=getproducts';
                $.ajax({
                    type: "POST",
                    url: "ajax.php",
                    data: params,
                    success : function(resp) {
                    $('div#deviceresult').html(resp);
                }
                });
       });
       
       $("a.devicelink").click(function(event){
           event.preventDefault();
           var device = $(this).attr('id').split('_')[1];
           var params = 'device='+device+'&action=getproducts';
           $.ajax({
                    type: "POST",
                    url: "ajax.php",
                    data: params,
                    success : function(resp) {
                    $('div#deviceresult').html(resp);
                }
                });
       });
    });
  
  function get_data() {
        
      var existingdata = getCurrentGraphData();
      var options = { series: { 
        lines: { show: true },
        points: { show: true } } };
        
      var productid = $('#productid').val();
      var airflow = $('#airflow :selected').text();
      var params = 'action=updateforced&productid='+productid+'&airflow='+airflow+'&existingdata='+existingdata.data+'&existinglabel='+existingdata.label+'&existingcolour='+existingdata.color;
      $.ajax({   
            method: 'POST',
            url: 'ajax.php',
            data: params,
            dataType: 'json',
            success: function(series) {   
            var newdata = new Array();
            newdata[0] = existingdata.data;
            newdata[1] = series; 
            plot = $.plot($("#placeholder"), series, options); 
            }
        });     
  } 
  
  
  
function onDataReceived(series) {
                data = [ series ];
                var plot = $.plot($("#placeholder"), data);
                var a = plot.getData();
                for (var i = 0; i < a.length; ++i)
                alert(a[i].data);
            }
            
            function getCurrentGraphData() {
                var series = plot.getData();
                return series[0];
            }   
            
           

    
