var m1=document.generalForm.m1;      
var m2=document.generalForm.m2;
var p1=document.generalForm.p1;
var p2=document.generalForm.p2;

function initialize(){
    hideLoadingImage();
    changeP1Position();
    changeP2Position();
    initializeUnitMoves();
    updateChart();
}

function updateSpreads()
{
    Ajax.getSpreads(c1.value, m1.value,c2.value, m2.value, createList);
}

function createList(data)
{
    dwr.util.removeAllOptions("spreads");
    dwr.util.addOptions("spreads", data);
}

var setM1ValuesInDWR = function(data){
    var fieldId = m1;
    var valueText = 'monthCode';
    var nameText = 'monthName';
    dwr.util.removeAllOptions(fieldId);
    dwr.util.addOptions(fieldId, data, valueText, nameText);
    updateSpreads();
};    


function updateM1()
{
    Ajax.getMonths(c1.value, setM1ValuesInDWR);
}  

var setM2ValuesInDWR = function(data){
    var fieldId = m2;
    var valueText = 'monthCode';
    var nameText = 'monthName';
    dwr.util.removeAllOptions(fieldId);
    dwr.util.addOptions(fieldId, data, valueText, nameText);
    updateSpreads();
};    

function updateM2()
{
    Ajax.getMonths(c2.value, setM2ValuesInDWR);
}  

function initializeUnitMoves(){
    initializeUnitMove(c1,uM1);    
    initializeUnitMove(c2,uM2);
    
    if(c1.value==c2.value){
        hideUnitMove1();
        hideUnitMove2();
    }else{
    displayUnitMove1();
    displayUnitMove2();
}
}

function changeP1Position(){
    if(p1.value=="-1"){
        p2.value="1";
    }
    if(p1.value=="1"){
        p2.value="-1";
    }  
}

function changeP2Position(){
    if(p2.value=="-1"){
        p1.value="1";
    }
    if(p2.value=="1"){
        p1.value="-1";
    }
}

//Update the chart according to user selection
function updateChart()
{
    //Check for selection.
    var number=0;
    var length=generalForm.spreads.options.length;
    for (var i = 0; i < length; ++i){
        if(generalForm.spreads.options[i].selected) number++;
    }
    if(number==0) {
        alert("Please select spreads.");
    }else{
    
    displayLoadingImage();
    //
    //we encode the values of all form elements as query parameters
    //
    var elements = document.generalForm.elements;
    url =document.generalForm.elements.action.value + "?";
    for (var i = 0; i < elements.length; ++i)
        {
            var e = elements[i];        
            if (e.name == "spreads"){
                for(var j=0;j<e.options.length;j++){
                    if(e.options[j].selected){
                        url = url + e.name + "=" + escape(e.options[j].value)+"&";
                    }        		 
                }
            }
            if(e.name != "spreads"){
                url = url + e.name + "=" + escape(e.value)+"&";
            } 
            
        }
        url = url + "D=dummyParameter";
        
        //Now we update the URL of the image to update the chart
        // document.write(url);
        getChart(url);
    }
}

if (window.addEventListener)
    {
        window.addEventListener("load", init, false);
    }
    else if (window.attachEvent)
        {
            window.attachEvent("onload", init);
        }
        else
            {
                window.onload = init;
            }
            
            function init()
            {
                // dwr.util.useLoadingMessage();
               // updateSpreads();
            }
            
            Event.observe(window, 'load', function() {
                Event.observe('c1', 'change',    function(){
                    initializeUnitMoves(); updateM1();
                });
                Event.observe('m1', 'change',    function(){
                    updateSpreads();
                });
                Event.observe('p1', 'change',    function(){
                    changeP1Position();
                });
                Event.observe('c2', 'change',    function(){
                    initializeUnitMoves(); updateM2();
                });
                Event.observe('m2', 'change',    function(){
                    updateSpreads();
                });
                Event.observe('p2', 'change',    function(){
                    changeP2Position();
                });
            });   
            