//var month = new Array("Januar","Februar","Mars","April","Mai","Juni","Juli","August","September","Oktober","November","Desember");

/**************************************************************************
*   Function that updates the AntStations table
*
*   Creates a ajax request
*****************************************************************************/
function getAntStations(){
    createRequest();
    var toktnummer = document.getElementById("toktnummer").value;
    var platform = document.getElementById("platform").value;
    var fradato = document.getElementById("fradato").value;
    var tildato = document.getElementById("tildato").value;
    var north = document.getElementById("lengdenord").value;
    var south = document.getElementById("lengdesor").value;
    var east = document.getElementById("breddeeast").value;
    var west = document.getElementById("breddevest").value;
    
//    var uri = "ajax/stations.ajx?toktnummer="+toktnummer+"&platform="+platform+"&fradato="+fradato+"&tildato="+tildato+"&lengdenord="+north+"&lengdesor="+south+"&breddeeast="+east+"&breddevest="+west;
    var uri = "numstations.map?toktnummer="+toktnummer+"&platform="+platform+"&fradato="+fradato+"&tildato="+tildato+"&lengdenord="+north+"&lengdesor="+south+"&breddeeast="+east+"&breddevest="+west;
    request.open("GET",uri,true);
    request.onreadystatechange = updateAntStations;
    request.send(null);
}
/************************************************************************
*   Function that updates the AntStations table
*
*   Creates a new table
*************************************************************************/
function updateAntStations(){
    if(request.readyState == 4){
        if(request.status == 200){
            var theAnswer = request.responseText;
            var xmlDoc = request.responseXML;

            var resultOutputDiv = document.getElementById("antStationsResultDiv");
            resultOutputDiv.style.display = "block";
            if(ie){
                resultOutput = resultOutputDiv.children[0];
            }else{
                resultOutput = document.getElementById("antstationsresult");
            }

            if(resultOutput != null){
                resultOutputDiv.removeChild(resultOutput);
            }
            resultOutput = document.createElement("TABLE");
            resultOutputDiv.appendChild(resultOutput);
            resultOutput.setAttribute("ID","antstationsresult");
            
            //creates the heading
            var theading = document.createElement("THEAD");
            resultOutput.appendChild(theading);
            var tr = document.createElement("TR");            
            theading.appendChild(tr);
            tr.setAttribute("class","headingImg");
            var aarHeading = document.createElement("TH");
            tr.appendChild(aarHeading);
            aarHeading.appendChild(document.createTextNode("År"));

            var staHeading = document.createElement("TH");
            tr.appendChild(staHeading);
            staHeading.appendChild(document.createTextNode("Stasjoner"));
            //finished heading
            
            //writes the number of stations based on years contained in results
            var tbody = document.createElement("TBODY");
            resultOutput.appendChild(tbody);
            var total = 0;            
            var results = xmlDoc.getElementsByTagName("stations");
            for(var i = 0; i < results.length; i++){
                if(results[i].getElementsByTagName("station")[0].firstChild.nodeValue != "null"){
                    var tr = document.createElement("TR");
                    tbody.appendChild(tr);
                    if(i != 0){
                        if((i % 2) != 0){
                            tr.setAttribute("class","evenrow");
                        }else{
                            tr.setAttribute("class","oddrow");
                        }
                    }else{
                        tr.setAttribute("class","oddrow");
                    }

                    var aar = document.createElement("TD");
                    tr.appendChild(aar);
                    aar.appendChild(document.createTextNode(results[i].getElementsByTagName("aar")[0].firstChild.nodeValue));
                    
                    var stasjon = document.createElement("TD");
                    tr.appendChild(stasjon);
                    stasjon.appendChild(document.createTextNode(results[i].getElementsByTagName("station")[0].firstChild.nodeValue));
                    total = total + parseInt(results[i].getElementsByTagName("station")[0].firstChild.nodeValue);
                }
            }
            //finished writing stations
            
            //writes the total amount of stations
            var tr = document.createElement("TR");
            tbody.appendChild(tr);
            tr.setAttribute("class","oddrow");
            var aar = document.createElement("TD");
            tr.appendChild(aar);
            aar.appendChild(document.createTextNode("Total"));
            var stasjon = document.createElement("TD");
            tr.appendChild(stasjon);
            stasjon.appendChild(document.createTextNode(total));
        //finished total amount
        }
    }
}

/*******************************************************************
*   Function that updates the cruise numbers when the user changes
*   the boat he's searching for
*
*********************************************************************/
function visToktnummer(){
    createRequest();
    var ele = document.getElementById("platform");
    //  var uri = "ajax/changePlatform.ajx?type=baatskifte&platformid="+ele.value;
    var uri = "changePlatform.map?platformid="+ele.value;
    request.open("GET",uri,true);
    request.onreadystatechange = nyeToktnummer;
    request.send(null);
}

/**************************************************************************
*   Updates the cruise numbers that is returned from visToktnummer
*
*
***************************************************************************/
function nyeToktnummer(){
    if(request.readyState == 4){
        if(request.status == 200){
            var theAnswer = request.responseText;
            var xmlDoc = request.responseXML;
            var toktnummerele = document.getElementById("toktnummer");
            toktnummerele.options.length = 0;
            toktnummerele.options[0] = new Option("","");
            var ting = xmlDoc.getElementsByTagName('toktnr');
            for(var i = 0; i < ting.length; i++){
                if(ting[i].firstChild.nodeValue != "null"){
                    toktnummerele.options[i+1] =  new Option(ting[i].firstChild.nodeValue,ting[i].firstChild.nodeValue);
                }
            }
            
        }
    }
}
